|
| 1 | +const routes = require('routes'); |
| 2 | +const mqtt = require('mqtt'); |
| 3 | +const DataTypes = require('./datatypes.js'); |
| 4 | +const TYPE = DataTypes.TYPE; |
| 5 | +const UNIT = DataTypes.UNIT; |
| 6 | +const events = require('events'); |
| 7 | +const Router = routes(); |
| 8 | +const THINGS = 'things'; |
| 9 | + |
| 10 | + |
| 11 | +var Cayenne = function(options) { |
| 12 | + apiVersion = "v1"; |
| 13 | + this.options = options; |
| 14 | + |
| 15 | + this.debug = this.options.debug || false; |
| 16 | + this.ssl = this.options.ssl || false; |
| 17 | + |
| 18 | + this.broker = this.options.broker || "mqtt.mydevices.com"; |
| 19 | + |
| 20 | + this.client = null; |
| 21 | + this.connected = false; |
| 22 | + |
| 23 | + this.rootTopic = [ |
| 24 | + apiVersion, |
| 25 | + this.options.username, |
| 26 | + THINGS, |
| 27 | + this.options.clientId |
| 28 | + ].join('/'); |
| 29 | + |
| 30 | +}; |
| 31 | + |
| 32 | +Cayenne.prototype.__proto__ = events.EventEmitter.prototype; |
| 33 | + |
| 34 | +Cayenne.prototype.connect = function(callback) { |
| 35 | + const self = this; |
| 36 | + thingTopic = this.rootTopic; |
| 37 | + |
| 38 | + this.client = mqtt.connect("mqtt://" + broker, { |
| 39 | + username: this.options.username, |
| 40 | + password: this.options.password, |
| 41 | + clientId: this.options.clientId, |
| 42 | + }); |
| 43 | + |
| 44 | + this.client.on('connect', function () { |
| 45 | + self.connected = true; |
| 46 | + self.client.subscribe(self.rootTopic + '/cmd/+'); |
| 47 | + |
| 48 | + return callback(null, self.client); |
| 49 | + }); |
| 50 | + |
| 51 | + this.client.on('message', self.onMessage.bind(self)); |
| 52 | + |
| 53 | + const cmdTopic = apiVersion + '/:username/'+THINGS+'/:thingId/cmd/:channel'; |
| 54 | + Router.addRoute(cmdTopic, this.handleCommand.bind(this)); |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +Cayenne.prototype.rawWrite = function(channel, value, type, unit) { |
| 59 | + var topic = this.getDataTopic(channel); |
| 60 | + var payload; |
| 61 | + |
| 62 | + if (unit === undefined) { |
| 63 | + unit = "null"; |
| 64 | + } |
| 65 | + |
| 66 | + if (type === undefined) { |
| 67 | + payload = value; |
| 68 | + } else { |
| 69 | + payload = type + "," + unit + "=" + value; |
| 70 | + } |
| 71 | + |
| 72 | + this.client.publish(topic, payload); |
| 73 | +}; |
| 74 | + |
| 75 | + |
| 76 | +Cayenne.prototype.getDataTopic = function (channel) { |
| 77 | + return this.rootTopic + '/data/' + channel; |
| 78 | +}; |
| 79 | + |
| 80 | +Cayenne.prototype.handleCommand = function (req) { |
| 81 | + const payload = req.payload.split(','); |
| 82 | + const seq = payload[0]; |
| 83 | + const cmd = payload[1]; |
| 84 | + |
| 85 | + this.client.publish(this.rootTopic + '/response', 'ok,'+seq); |
| 86 | + this.client.publish(this.rootTopic + '/data/' + req.meta.channel, cmd); |
| 87 | + |
| 88 | + this.emit("cmd" + req.meta.channel, req); |
| 89 | +} |
| 90 | + |
| 91 | +Cayenne.prototype.onMessage = function(topic, payload) { |
| 92 | + var route = Router.match(topic); |
| 93 | + |
| 94 | + if (!route) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + var req = { |
| 99 | + path: topic, |
| 100 | + payload: payload.toString(), |
| 101 | + meta: route ? route.params: null, |
| 102 | + splats: route ? route.splats : null |
| 103 | + }; |
| 104 | + |
| 105 | + return route.fn(req); |
| 106 | +} |
| 107 | + |
| 108 | +Cayenne.prototype.celsiusWrite = function (channel, value) { |
| 109 | + this.rawWrite(channel, value, TYPE.TEMPERATURE, UNIT.CELSIUS); |
| 110 | +} |
| 111 | + |
| 112 | +Cayenne.prototype.fahrenheitWrite = function (channel, value) { |
| 113 | + this.rawWrite(channel, value, TYPE.TEMPERATURE, UNIT.FAHRENHEIT) |
| 114 | +} |
| 115 | + |
| 116 | +Cayenne.prototype.kelvinWrite = function (channel, value) { |
| 117 | + this.rawWrite(channel, value, TYPE.TEMPERATURE, UNIT.KELVIN) |
| 118 | +} |
| 119 | + |
| 120 | +Cayenne.prototype.luxWrite = function (channel, value) { |
| 121 | + this.rawWrite(channel, value, TYPE.LUMINOSITY, UNIT.LUX) |
| 122 | +} |
| 123 | + |
| 124 | +Cayenne.prototype.pascalWrite = function (channel, value) { |
| 125 | + this.rawWrite(channel, value, TYPE.BAROMETRIC_PRESSURE, UNIT.PASCAL) |
| 126 | +} |
| 127 | + |
| 128 | +Cayenne.prototype.hectoPascalWrite = function (channel, value) { |
| 129 | + this.rawWrite(channel, value, TYPE.BAROMETRIC_PRESSURE, UNIT.HECTOPASCAL) |
| 130 | +} |
| 131 | + |
| 132 | +module.exports = Cayenne; |
0 commit comments