34 from .
import tdmascheduleevent_pb2
35 from collections
import namedtuple
44 _FrameEntry = namedtuple(
'FrameEntry', [
'frame',
'slots'])
47 self.
_event = tdmascheduleevent_pb2.TDMAScheduleEvent()
50 for (name,value)
in list(kwargs.items()):
51 if name ==
'frequency':
52 if (isinstance(value,int)
or \
53 isinstance(value,int))
and \
55 self.
_event.frequencyHz = value
57 raise ValueError(
"frequency must be a positive numeric")
59 elif name ==
'datarate':
60 if (isinstance(value,int)
or \
61 isinstance(value,int))
and \
63 self.
_event.dataRatebps = value
65 raise ValueError(
"datarate must be a positive numeric")
67 elif name ==
'service':
68 if (isinstance(value,int)
or \
69 isinstance(value,int))
and \
71 self.
_event.serviceClass = value
73 raise ValueError(
"service must be a positive numeric")
76 if (isinstance(value,int)
or \
77 isinstance(value,int)
or \
78 isinstance(value,float)):
79 self.
_event.powerdBm = value
81 raise ValueError(
"power must be a numeric")
84 raise KeyError(
"unknown parameter: %s" % name)
88 framesPerMultiFrame =
None 89 slotOverheadMicroseconds =
None 90 slotDurationMicroseconds =
None 95 if self.
_event.HasField(
'structure'):
96 return {
'slots': self.
_event.structure.slotsPerFrame,
97 'frames': self.
_event.structure.framesPerMultiFrame,
98 'slotduration':self.
_event.structure.slotDurationMicroseconds,
99 'slotoverhead':self.
_event.structure.slotOverheadMicroseconds,
100 'bandwidth': self.
_event.structure.bandwidthHz}
104 for (name,value)
in list(kwargs.items()):
106 if (isinstance(value,int)
or \
107 isinstance(value,int))
and \
109 slotsPerFrame = value
111 raise ValueError(
"'slots' must be a positive integer greater than 0")
113 elif name ==
'frames':
114 if (isinstance(value,int)
or \
115 isinstance(value,int))
and \
117 framesPerMultiFrame = value
119 raise ValueError(
"'frames' must be a positive integer greater than 0")
121 elif name ==
'slotduration':
122 if (isinstance(value,int)
or \
123 isinstance(value,int))
and \
125 slotDurationMicroseconds = value
127 raise ValueError(
"'slotduration' must be a positive integer greater than 0")
129 elif name ==
'slotoverhead':
130 if (isinstance(value,int)
or \
131 isinstance(value,int))
and \
133 slotOverheadMicroseconds = value
135 raise ValueError(
"'slotoverhead' must be a positive integer (usecs)")
137 elif name ==
'bandwidth':
138 if (isinstance(value,int)
or \
139 isinstance(value,int))
and \
143 raise ValueError(
"'bandwidth' must be a positive integer greater than 0 (Hz)")
146 raise KeyError(
"unknown parameter: %s" % name)
149 if slotsPerFrame ==
None or \
150 framesPerMultiFrame ==
None or \
151 slotOverheadMicroseconds ==
None or \
152 slotDurationMicroseconds ==
None or \
154 raise KeyError(
"Missing one ore more keys: 'slots', 'frames', 'slotduration', 'slotoverhead', 'bandwidth'")
156 self.
_event.structure.slotsPerFrame = slotsPerFrame
157 self.
_event.structure.framesPerMultiFrame = framesPerMultiFrame
158 self.
_event.structure.slotDurationMicroseconds = slotDurationMicroseconds
159 self.
_event.structure.slotOverheadMicroseconds = slotOverheadMicroseconds
160 self.
_event.structure.bandwidthHz = bandwidthHz
163 def append(self,frameIndex,slotIndex,**kwargs):
164 frameFrequencyHz =
None 165 frameDataRatebps =
None 168 slotFrequencyHz =
None 169 slotDataRatebps =
None 173 slotDestination =
None 175 if frameIndex
in self.
_frames and \
176 slotIndex
in self.
_frames[frameIndex].slots:
177 raise ValueError(
"slot index already defined for frame")
179 for (name,value)
in list(kwargs.items()):
180 if name ==
'frame.frequency':
181 if (isinstance(value,int)
or \
182 isinstance(value,int))
and \
184 frameFrequencyHz = value
186 raise ValueError(
"'frame.frequency' must be a integer greater that 0 (Hz)")
188 elif name ==
'frame.datarate':
189 if (isinstance(value,int)
or \
190 isinstance(value,int))
and \
192 frameDataRatebps = value
194 raise ValueError(
"'frame.datarate' must be a positive integer greater than 0 (bps)")
196 elif name ==
'frame.service':
197 if (isinstance(value,int)
or \
198 isinstance(value,int))
and \
199 value >= 0
and value <= 3:
202 raise ValueError(
"'frame.service' must be a positive integer in the set [0,3]")
204 elif name ==
'frame.power':
205 if (isinstance(value,int)
or \
206 isinstance(value,int)
or \
207 isinstance(value,float)):
209 framePowerdBm = value
211 raise ValueError(
"'frame.power' must be a numeric (dBm)")
213 elif name ==
'frequency':
214 if (isinstance(value,int)
or \
215 isinstance(value,int))
and \
217 slotFrequencyHz = value
219 raise ValueError(
"'frequency' must be a integer greater that 0 (Hz)")
221 elif name ==
'datarate':
222 if (isinstance(value,int)
or \
223 isinstance(value,int))
and \
225 slotDataRatebps = value
227 raise ValueError(
"'datarate' must be a positive integer greater than 0 (bps)")
229 elif name ==
'service':
230 if (isinstance(value,int)
or \
231 isinstance(value,int))
and \
232 value >= 0
and value <= 3:
235 raise ValueError(
"'service' must be a positive integer in the set [0,3]")
237 elif name ==
'power':
238 if (isinstance(value,int)
or \
239 isinstance(value,int)
or \
240 isinstance(value,float)):
243 raise ValueError(
"'power' must be a numeric (dBm)")
246 elif name ==
'destination':
247 if (isinstance(value,int)
or \
248 isinstance(value,int))
and \
250 slotDestination = value
252 raise ValueError(
"'destination' must be a positive integer (NEM Id)")
255 if value ==
"tx" or value == TDMAScheduleEvent.TX:
258 elif value ==
"rx" or value == TDMAScheduleEvent.RX:
261 elif value ==
"idle" or value == TDMAScheduleEvent.IDLE:
265 raise ValueError(
"'type' must be one of: tx, rx or idle")
268 raise KeyError(
"unknown parameter: %s" % name)
271 if slotFrequencyHz ==
None and \
272 frameFrequencyHz ==
None and \
273 self.
_event.frequencyHz ==
None:
274 raise KeyError(
"tx slot 'frequency' must be specified when 'frame.frequency' missing and default not set")
276 if slotDataRatebps ==
None and \
277 frameDataRatebps ==
None and \
278 self.
_event.dataRatebps ==
None:
279 raise KeyError(
"tx slot 'datarate' must be specified when 'frame.datarate' missing and default not set")
281 if slotClass ==
None and \
282 frameClass ==
None and \
283 self.
_event.serviceClass ==
None:
284 raise KeyError(
"tx slot 'service' must be specified when 'frame.service' missing and default not set")
286 if slotPowerdBm !=
None and \
287 framePowerdBm ==
None and \
288 self.
_event.powerdBm ==
None:
289 raise KeyError(
"tx slot 'power' must be specified when 'frame.power' missing and default not set")
291 elif slotType ==
"rx":
292 if slotDataRatebps !=
None or \
293 slotClass !=
None or \
294 slotPowerdBm !=
None or \
295 slotDestination !=
None:
296 raise KeyError(
"rx slot cannot have 'datarate', 'service', 'power' and/or 'destination'")
298 if slotFrequencyHz ==
None and \
299 frameFrequencyHz ==
None and \
300 self.
_event.frequencyHz ==
None:
301 raise KeyError(
"rx slot 'frequency' must be specified when 'frame.frequency' missing and default not set")
303 elif slotType ==
"idle":
304 if slotFrequencyHz !=
None or \
305 slotDataRatebps !=
None or \
306 slotClass !=
None or \
307 slotPowerdBm !=
None:
308 raise ValueError(
"idle slot cannot have 'frequency', 'datarate', 'service', 'power', and/or 'destination'")
311 raise KeyError(
"missing 'type'")
314 frame = self.
_frames[frameIndex].frame
316 frame = self.
_event.frames.add()
317 self.
_frames[frameIndex] = TDMAScheduleEvent._FrameEntry(frame,set())
318 frame.index = frameIndex
320 if frameFrequencyHz !=
None:
321 frame.frequencyHz = frameFrequencyHz
323 if frameDataRatebps !=
None:
324 frame.dataRatebps = frameDataRatebps
326 if frameClass !=
None:
327 frame.serviceClass = frameClass
329 if framePowerdBm !=
None:
330 frame.powerdBm = framePowerdBm
332 slot = frame.slots.add()
334 slot.index = slotIndex
337 slot.type = tdmascheduleevent_pb2.TDMAScheduleEvent.Frame.Slot.SLOT_TX
339 if slotFrequencyHz !=
None:
340 slot.tx.frequencyHz = slotFrequencyHz
342 if slotDataRatebps !=
None:
343 slot.tx.dataRatebps = slotDataRatebps
345 if slotClass !=
None:
346 slot.tx.serviceClass = slotClass
348 if slotPowerdBm !=
None:
349 slot.tx.powerdBm = slotPowerdBm
351 if slotDestination !=
None:
352 slot.tx.destination = slotDestination
354 elif slotType ==
"rx":
355 slot.type = tdmascheduleevent_pb2.TDMAScheduleEvent.Frame.Slot.SLOT_RX
357 if slotFrequencyHz !=
None:
358 slot.rx.frequencyHz = slotFrequencyHz
361 slot.type = tdmascheduleevent_pb2.TDMAScheduleEvent.Frame.Slot.SLOT_IDLE
363 self.
_frames[frameIndex].slots.add(slotIndex)
366 return self.
_event.SerializeToString()
369 self.
_event.ParseFromString(data)
372 for frame
in self.
_event.frames:
373 kwargs = {
'index' : frame.index}
375 if frame.HasField(
'frequencyHz'):
376 kwargs[
'frame.frequency'] = frame.frequencyHz
378 if frame.HasField(
'dataRatebps'):
379 kwargs[
'frame.datarate'] = frame.dataRatebps
381 if frame.HasField(
'serviceClass'):
382 kwargs[
'frame.service'] = frame.serviceClass
384 if frame.HasField(
'powerdBm'):
385 kwargs[
'frame.power'] = frame.powerdBm
389 for slot
in frame.slots:
392 if slot.type == tdmascheduleevent_pb2.TDMAScheduleEvent.Frame.Slot.SLOT_TX:
395 if slot.HasField(
'tx'):
396 if slot.tx.HasField(
'frequencyHz'):
397 s[
'frequency'] = slot.tx.frequencyHz
399 if slot.tx.HasField(
'dataRatebps'):
400 s[
'datarate'] = slot.tx.dataRatebps
402 if slot.tx.HasField(
'serviceClass'):
403 s[
'service'] = slot.tx.serviceClass
405 if slot.tx.HasField(
'powerdBm'):
406 s[
'power'] = slot.tx.powerdBm
408 if slot.tx.HasField(
'destination'):
409 s[
'destination'] = slot.tx.destination
411 elif slot.type == tdmascheduleevent_pb2.TDMAScheduleEvent.Frame.Slot.SLOT_RX:
414 if slot.HasField(
'rx')
and slot.rx.HasField(
'frequencyHz'):
415 s[
'frequency'] = slot.rx.frequencyHz
420 slots[slot.index] = s
422 kwargs[
'slots'] = slots
def append(self, frameIndex, slotIndex, kwargs)
def structure(self, kwargs)
def __init__(self, kwargs)