|
| 1 | +// Connect to an IoT Agent and use fallback values if necessary |
| 2 | + |
| 3 | + |
| 4 | +const IoTDevices = require('../devices'); |
| 5 | +const DEVICE_API_KEY = process.env.DUMMY_DEVICES_API_KEY || '1234'; |
| 6 | + |
| 7 | + |
| 8 | +// A series of constants used by our set of devices |
| 9 | +const OK = ' OK'; |
| 10 | +const NOT_OK = ' NOT OK'; |
| 11 | + |
| 12 | +/* global MQTT_CLIENT */ |
| 13 | + |
| 14 | +// |
| 15 | +// Splits the deviceId from the command sent. |
| 16 | +// |
| 17 | +function getUltralightCommand(string) { |
| 18 | + const command = string.split('@'); |
| 19 | + if (command.length === 1) { |
| 20 | + command.push(''); |
| 21 | + } |
| 22 | + return command[1]; |
| 23 | +} |
| 24 | + |
| 25 | +// This processor sends ultralight payload northbound to |
| 26 | +// the southport of the IoT Agent and sends measures |
| 27 | +// for the motion sensor, door and lamp. |
| 28 | + |
| 29 | +// Ultralight 2.0 is a lightweight text based protocol aimed to constrained |
| 30 | +// devices and communications |
| 31 | +// where the bandwidth and device memory may be limited resources. |
| 32 | +// |
| 33 | +// A device can report new measures to the IoT Platform using an HTTP GET request to the /iot/d path with the following query parameters: |
| 34 | +// |
| 35 | +// i (device ID): Device ID (unique for the API Key). |
| 36 | +// k (API Key): API Key for the service the device is registered on. |
| 37 | +// t (timestamp): Timestamp of the measure. Will override the automatic IoTAgent timestamp (optional). |
| 38 | +// d (Data): Ultralight 2.0 payload. |
| 39 | +// |
| 40 | +// At the moment the API key and timestamp are unused by the simulator. |
| 41 | + |
| 42 | +class UltralightCommand { |
| 43 | + // The bell will respond to the "ring" command. |
| 44 | + // this will briefly set the bell to on. |
| 45 | + // The bell is not a sensor - it will not report state northbound |
| 46 | + actuateBell(req, res) { |
| 47 | + const keyValuePairs = req.body.split('|') || ['']; |
| 48 | + const command = getUltralightCommand(keyValuePairs[0]); |
| 49 | + const deviceId = 'bell' + req.params.id; |
| 50 | + const result = keyValuePairs[0] + '| ' + command; |
| 51 | + |
| 52 | + if (IoTDevices.notFound(deviceId)) { |
| 53 | + return res.status(404).send(result + NOT_OK); |
| 54 | + } else if (IoTDevices.isUnknownCommand('bell', command)) { |
| 55 | + return res.status(422).send(result + NOT_OK); |
| 56 | + } |
| 57 | + |
| 58 | + // Update device state |
| 59 | + IoTDevices.actuateDevice(deviceId, command); |
| 60 | + return res.status(200).send(result + OK); |
| 61 | + } |
| 62 | + |
| 63 | + // The door responds to "open", "close", "lock" and "unlock" commands |
| 64 | + // Each command alters the state of the door. When the door is unlocked |
| 65 | + // it can be opened and shut by external events. |
| 66 | + actuateDoor(req, res) { |
| 67 | + const keyValuePairs = req.body.split('|') || ['']; |
| 68 | + const command = getUltralightCommand(keyValuePairs[0]); |
| 69 | + const deviceId = 'door' + req.params.id; |
| 70 | + const result = keyValuePairs[0] + '| ' + command; |
| 71 | + |
| 72 | + if (IoTDevices.notFound(deviceId)) { |
| 73 | + return res.status(404).send(result + NOT_OK); |
| 74 | + } else if (IoTDevices.isUnknownCommand('door', command)) { |
| 75 | + return res.status(422).send(result + NOT_OK); |
| 76 | + } |
| 77 | + |
| 78 | + // Update device state |
| 79 | + IoTDevices.actuateDevice(deviceId, command); |
| 80 | + return res.status(200).send(result + OK); |
| 81 | + } |
| 82 | + |
| 83 | + // The lamp can be "on" or "off" - it also registers luminosity. |
| 84 | + // It will slowly dim as time passes (provided no movement is detected) |
| 85 | + actuateLamp(req, res) { |
| 86 | + const keyValuePairs = req.body.split('|') || ['']; |
| 87 | + const command = getUltralightCommand(keyValuePairs[0]); |
| 88 | + const deviceId = 'lamp' + req.params.id; |
| 89 | + const result = keyValuePairs[0] + '| ' + command; |
| 90 | + |
| 91 | + if (IoTDevices.notFound(deviceId)) { |
| 92 | + return res.status(404).send(result + NOT_OK); |
| 93 | + } else if (IoTDevices.isUnknownCommand('lamp', command)) { |
| 94 | + return res.status(422).send(result + NOT_OK); |
| 95 | + } |
| 96 | + |
| 97 | + // Update device state |
| 98 | + IoTDevices.actuateDevice(deviceId, command); |
| 99 | + return res.status(200).send(result + OK); |
| 100 | + } |
| 101 | + |
| 102 | + // cmd topics are consumed by the actuators (bell, lamp and door) |
| 103 | + processMqttMessage(topic, message) { |
| 104 | + const path = topic.split('/'); |
| 105 | + if (path.pop() === 'cmd') { |
| 106 | + const keyValuePairs = message.split('|') || ['']; |
| 107 | + const command = getUltralightCommand(keyValuePairs[0]); |
| 108 | + const deviceId = path.pop(); |
| 109 | + const result = keyValuePairs[0] + '| ' + command; |
| 110 | + |
| 111 | + if (!IoTDevices.notFound(deviceId)) { |
| 112 | + IoTDevices.actuateDevice(deviceId, command); |
| 113 | + const topic = '/' + DEVICE_API_KEY + '/' + deviceId + '/cmdexe'; |
| 114 | + MQTT_CLIENT.publish(topic, result + OK); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +module.exports = UltralightCommand; |
| 123 | + |
0 commit comments