Scheduling Functionality - alyoshenka/neo GitHub Wiki
As a user, I want to be able to select an operation, then select a time, then have that operation be performed at that time. For example, I could schedule the LED board to print a message saying "Welcome home from school roommates, there is food on the stove please help yourself" at 3pm tomorrow. From a technical perspective, how is this achieved?
- Operation and schedule data are input via the Dibiasky web application
- This data is formatted into a topic
const publishTopic = 'cmd/neo/schedule/req' // The topic to send this request to
const scheduledOperation = { // The payload to send
responseTopic: 'cmd/neo/schedule/res', // The topic to send a success/failure message of the scheduling process to
executeAt: '', // The time for the operation to run
operationTopic: 'cmd/neo/hubble/req', // The topic to publish the operation to
operation: { // The operation to run; This is what `neo` will interpret
responseTopic: 'cmd/neo/hubble/res', // The topic to send a success/failure message of the operation itself
module: 'neopolitan', // The module operation
subCommand: 'update', // Additional (optional) data the operation needs
friendlyName: 'Run Neopolitan Update with Options', // Optional display name
options: [ say: 'hello world', speed: 'fast', 'wrap': True ], // Additional options, if required by the operation
}
};
- The operation is published to the topic:
publish(publishTopic, scheduledOperation);
- An IoT Topic Rule receives the payload:
Manage
->Message routing
->Rules
, selects from it, then invokes a Lambda using the payload selected using SQLTimeDataToLambda
:SELECT * FROM '/cmd/neo/schedule/req'
->ReceiveScheduleFunction
- The Lambda Function processes the payload
- The payload is validated and a response is generated
- The presence of an
executeAt
field is ensured - It is ensured that the value of the
executeAt
field is a datetime in the future
- The presence of an
- If the payload was valid
- The operation is added to the DynamoDB Table
dynamodb.put_item(TableName='ScheduledOperations', Item={ 'scheduleID':{'S': str(uuid.uuid1())}, 'executeAt':{'S': event['executeAt']}, 'operationTopic': {'S': event['operationTopic']}, 'operation':{'S': event['operation']} )
- The Lambda Function
PublishScheduledOperationsDBContents
is invoked, which publishes the items of the Table todt/neo/scheduled/res
- todo: link function code Gist
- The Step Function
MQTTWaitThenSendBack
is invoked and passed the original payload- This function waits until the specified time
- Then invokes the
TestPublishToMQTT
Lamda Function, passing it the same payload- This function publishes the
operation
as a payload to theoperationTopic
payload
- This function publishes the
- A success/failure message is published to
responseTopic
- The operation is added to the DynamoDB Table
- If the payload was invalid
- A failure message is sent to the
responseTopic
field
- A failure message is sent to the
- The payload is validated and a response is generated