@@ -115,7 +115,7 @@ def run(self):
115
115
if topic or message :
116
116
got_packet = True
117
117
try :
118
- if message :
118
+ if message or topic == cayennemqtt . JOBS_TOPIC :
119
119
# debug('WriterThread, topic: {} {}'.format(topic, message))
120
120
if not isinstance (message , str ):
121
121
message = dumps (message )
@@ -214,6 +214,8 @@ def Start(self):
214
214
TimerThread (self .SendSystemState , 30 , 5 )
215
215
self .updater = Updater (self .config )
216
216
self .updater .start ()
217
+ events = self .schedulerEngine .get_scheduled_events ()
218
+ self .EnqueuePacket (events , cayennemqtt .JOBS_TOPIC )
217
219
# self.sentHistoryData = {}
218
220
# self.historySendFails = 0
219
221
# self.historyThread = Thread(target=self.SendHistoryData)
@@ -367,8 +369,12 @@ def OnMessage(self, message):
367
369
368
370
def RunAction (self , action ):
369
371
"""Run a specified action"""
370
- debug ('RunAction' )
371
- self .ExecuteMessage (action )
372
+ debug ('RunAction: {}' .format (action ))
373
+ result = True
374
+ command = action .copy ()
375
+ self .mqttClient .transform_command (command )
376
+ result = self .ExecuteMessage (command )
377
+ return result
372
378
373
379
def ProcessMessage (self ):
374
380
"""Process a message from the server"""
@@ -381,30 +387,36 @@ def ProcessMessage(self):
381
387
self .ExecuteMessage (messageObject )
382
388
383
389
def ExecuteMessage (self , message ):
384
- """Execute an action described in a message object"""
390
+ """Execute an action described in a message object
391
+
392
+ Returns: True if action was executed, False otherwise."""
393
+ result = False
385
394
if not message :
386
- return
395
+ return result
387
396
channel = message ['channel' ]
388
397
info ('ExecuteMessage: {}' .format (message ))
389
398
if channel in (cayennemqtt .SYS_POWER_RESET , cayennemqtt .SYS_POWER_HALT ):
390
- self .ProcessPowerCommand (message )
399
+ result = self .ProcessPowerCommand (message )
391
400
elif channel .startswith (cayennemqtt .DEV_SENSOR ):
392
- self .ProcessSensorCommand (message )
401
+ result = self .ProcessSensorCommand (message )
393
402
elif channel .startswith (cayennemqtt .SYS_GPIO ):
394
- self .ProcessGpioCommand (message )
403
+ result = self .ProcessGpioCommand (message )
395
404
elif channel == cayennemqtt .AGENT_DEVICES :
396
- self .ProcessDeviceCommand (message )
405
+ result = self .ProcessDeviceCommand (message )
397
406
elif channel in (cayennemqtt .SYS_I2C , cayennemqtt .SYS_SPI , cayennemqtt .SYS_UART , cayennemqtt .SYS_ONEWIRE ):
398
- self .ProcessConfigCommand (message )
407
+ result = self .ProcessConfigCommand (message )
399
408
elif channel == cayennemqtt .AGENT_MANAGE :
400
- self .ProcessAgentCommand (message )
401
- elif channel == cayennemqtt .AGENT_SCHEDULE :
402
- self .ProcessScheduleCommand (message )
409
+ result = self .ProcessAgentCommand (message )
410
+ elif channel == cayennemqtt .AGENT_SCHEDULER :
411
+ result = self .ProcessSchedulerCommand (message )
403
412
else :
404
413
info ('Unknown message' )
414
+ return result
405
415
406
416
def ProcessPowerCommand (self , message ):
407
- """Process command to reboot/shutdown the system"""
417
+ """Process command to reboot/shutdown the system
418
+
419
+ Returns: True if command was processed, False otherwise."""
408
420
error_message = None
409
421
try :
410
422
self .EnqueueCommandResponse (message , error_message )
@@ -427,9 +439,13 @@ def ProcessPowerCommand(self, message):
427
439
data = []
428
440
cayennemqtt .DataChannel .add (data , message ['channel' ], value = 0 )
429
441
self .EnqueuePacket (data )
442
+ raise ExecuteMessageError (error_message )
443
+ return error_message == None
430
444
431
445
def ProcessAgentCommand (self , message ):
432
- """Process command to manage the agent state"""
446
+ """Process command to manage the agent state
447
+
448
+ Returns: True if command was processed, False otherwise."""
433
449
error = None
434
450
try :
435
451
if message ['suffix' ] == 'uninstall' :
@@ -450,9 +466,14 @@ def ProcessAgentCommand(self, message):
450
466
except Exception as ex :
451
467
error = '{}: {}' .format (type (ex ).__name__ , ex )
452
468
self .EnqueueCommandResponse (message , error )
469
+ if error :
470
+ raise ExecuteMessageError (error )
471
+ return error == None
453
472
454
473
def ProcessConfigCommand (self , message ):
455
- """Process system configuration command"""
474
+ """Process system configuration command
475
+
476
+ Returns: True if command was processed, False otherwise."""
456
477
error = None
457
478
try :
458
479
value = 1 - int (message ['payload' ]) #Invert the value since the config script uses 0 for enable and 1 for disable
@@ -464,9 +485,12 @@ def ProcessConfigCommand(self, message):
464
485
except Exception as ex :
465
486
error = '{}: {}' .format (type (ex ).__name__ , ex )
466
487
self .EnqueueCommandResponse (message , error )
467
-
488
+ return error == None
489
+
468
490
def ProcessGpioCommand (self , message ):
469
- """Process GPIO command"""
491
+ """Process GPIO command
492
+
493
+ Returns: True if command was processed, False otherwise."""
470
494
error = None
471
495
try :
472
496
channel = int (message ['channel' ].replace (cayennemqtt .SYS_GPIO + ':' , '' ))
@@ -477,9 +501,12 @@ def ProcessGpioCommand(self, message):
477
501
except Exception as ex :
478
502
error = '{}: {}' .format (type (ex ).__name__ , ex )
479
503
self .EnqueueCommandResponse (message , error )
504
+ return error == None
480
505
481
506
def ProcessSensorCommand (self , message ):
482
- """Process sensor command"""
507
+ """Process sensor command
508
+
509
+ Returns: True if command was processed, False otherwise."""
483
510
error = None
484
511
try :
485
512
sensor_info = message ['channel' ].replace (cayennemqtt .DEV_SENSOR + ':' , '' ).split (':' )
@@ -494,9 +521,12 @@ def ProcessSensorCommand(self, message):
494
521
except Exception as ex :
495
522
error = '{}: {}' .format (type (ex ).__name__ , ex )
496
523
self .EnqueueCommandResponse (message , error )
524
+ return error == None
497
525
498
526
def ProcessDeviceCommand (self , message ):
499
- """Process a device command to add/edit/remove a sensor"""
527
+ """Process a device command to add/edit/remove a sensor
528
+
529
+ Returns: True if command was processed, False otherwise."""
500
530
error = None
501
531
try :
502
532
payload = message ['payload' ]
@@ -515,28 +545,32 @@ def ProcessDeviceCommand(self, message):
515
545
except Exception as ex :
516
546
error = '{}: {}' .format (type (ex ).__name__ , ex )
517
547
self .EnqueueCommandResponse (message , error )
548
+ return error == None
518
549
519
- def ProcessScheduleCommand (self , message ):
520
- """Process command to add/edit/remove a scheduled action"""
550
+ def ProcessSchedulerCommand (self , message ):
551
+ """Process command to add/edit/remove a scheduled action
552
+
553
+ Returns: True if command was processed, False otherwise."""
521
554
error = None
522
555
try :
523
- if 'actions' in message ['payload' ]:
524
- for action in message ['payload' ]['actions' ]:
525
- self .mqttClient .transform_command (action )
526
556
if message ['suffix' ] == 'add' :
527
557
result = self .schedulerEngine .add_scheduled_event (message ['payload' ], True )
528
558
elif message ['suffix' ] == 'edit' :
529
559
result = self .schedulerEngine .update_scheduled_event (message ['payload' ])
530
560
elif message ['suffix' ] == 'delete' :
531
561
result = self .schedulerEngine .remove_scheduled_event (message ['payload' ])
562
+ elif message ['suffix' ] == 'get' :
563
+ events = self .schedulerEngine .get_scheduled_events ()
564
+ self .EnqueuePacket (events , cayennemqtt .JOBS_TOPIC )
532
565
else :
533
566
error = 'Unknown schedule command: {}' .format (message ['suffix' ])
534
- debug ('ProcessScheduleCommand result: {}' .format (result ))
567
+ debug ('ProcessSchedulerCommand result: {}' .format (result ))
535
568
if result is False :
536
569
error = 'Schedule command failed'
537
570
except Exception as ex :
538
571
error = '{}: {}' .format (type (ex ).__name__ , ex )
539
572
self .EnqueueCommandResponse (message , error )
573
+ return error == None
540
574
541
575
def EnqueueCommandResponse (self , message , error ):
542
576
"""Send response after processing a command message"""
0 commit comments