-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic features: send and receive commands
- Loading branch information
0 parents
commit f7ee779
Showing
13 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## 0.1.0 | ||
|
||
Base features: | ||
|
||
* Receive and send SmartBus commands | ||
* Simple DSL for "Single Channel Control" command |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Andrey Ivaschenko | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
## SmartBus | ||
|
||
Node.js implementation of HDL SmartBus protocol http://hdlautomation.com | ||
|
||
### Initialization | ||
|
||
Create instance of SmartBus connector | ||
|
||
```js | ||
var SmartBus = require('smart-bus'); | ||
|
||
var bus = new SmartBus({ | ||
subnet: 1, | ||
id: 50, // Device id for connector | ||
gateway: '192.168.1.250', // HDL SmartBus gateway IP | ||
|
||
port: 6000, // Listening port, default: 6000 | ||
}); | ||
``` | ||
|
||
### Receive commands | ||
|
||
Add handler to intercept all commands across bus | ||
|
||
```js | ||
bus.on('command', function(command) { | ||
|
||
// Integer with command operation code | ||
command.code; | ||
|
||
// Device objects | ||
command.sender; | ||
command.target; | ||
|
||
// Object with decoded data or raw buffer | ||
// if data can not be parsed automatically | ||
command.data; | ||
|
||
}); | ||
``` | ||
|
||
Handlers can be added for broadcast messages or specific commands | ||
|
||
```js | ||
bus.on('broadcast', function(command) { ... }); | ||
bus.on(0x0032, function(command) { ... }); | ||
``` | ||
|
||
Listen for commands from specific device | ||
|
||
```js | ||
var sensor = bus.device('1.20'); | ||
|
||
sensor.on(0x1647, function(data, target) { ... }); | ||
``` | ||
|
||
### Send commands | ||
|
||
Use connector to send commands | ||
|
||
```js | ||
bus.send('1.4', 0x0031, { channel: 1, level: 100 }, function(err) { ... }); | ||
``` | ||
|
||
Or use device object | ||
|
||
```js | ||
var logic = bus.device('1.10'); | ||
|
||
logic.send(0xE01C, { switch: 1, status: 1 }, function(err) { ... }); | ||
``` | ||
Both `send` methods accepts raw `Buffer` as data object. In case if | ||
data object can not be encoded error will be passed into callback. | ||
### DSL | ||
Manipulate device channel status. | ||
```js | ||
var dimmer = bus.device('1.4'); | ||
var spotlights = dimmer.channel(2); | ||
|
||
spotlights.level(100, { time: 5 }, function(err, response) { ... }); | ||
``` | ||
`level` function will send `0x0031` command and then pass | ||
contents of `0x0032` response into callback. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/bus'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
var crc = require('crc').crc16xmodem; | ||
var util = require('util'); | ||
var debug = require('debug')('smart-bus'); | ||
var dgram = require('dgram'); | ||
var Device = require('./device'); | ||
var Command = require('./command'); | ||
var EventEmitter = require('events'); | ||
|
||
module.exports = Bus; | ||
|
||
var inspect = util.inspect; | ||
|
||
var isBuffer = Buffer.isBuffer; | ||
|
||
var Constants = new Buffer(12); | ||
|
||
Constants.write('HDLMIRACLE'); | ||
Constants.writeUInt16BE(0xAAAA, 10); | ||
|
||
function Bus(config) { | ||
this.id = config.id; | ||
this.type = 0xFFFE; | ||
this.subnet = config.subnet; | ||
this.address = null; | ||
|
||
this.port = config.port || 6000; | ||
this.gateway = config.gateway; | ||
|
||
this.socket = createSocket(this); | ||
|
||
this.devices = {}; | ||
|
||
EventEmitter.call(this); | ||
} | ||
|
||
util.inherits(Bus, EventEmitter); | ||
|
||
/** | ||
* Return device object by its address | ||
* | ||
* @param {Number|String} subnet or address Subnet or address, e.g. '1.4' | ||
* @param {Number} [id] Device id | ||
* @return {Device} | ||
*/ | ||
Bus.prototype.device = function(address, id) { | ||
var subnet = address; | ||
|
||
if (typeof address === 'number') address = address + '.' + id; | ||
else subnet = subnet.split('.'), id = subnet[1], subnet = subnet[0]; | ||
|
||
var devices = this.devices; | ||
|
||
if (!devices[address]) devices[address] = new Device(this, subnet, id); | ||
|
||
return devices[address]; | ||
}; | ||
|
||
/** | ||
* Send command to device via HDL SmartBus gateway | ||
* | ||
* @param {String|Device} target Device address or object | ||
* @param {Number} code Command code | ||
* @param {Object|Buffer} data Additional data | ||
* @param {Function} callback | ||
*/ | ||
Bus.prototype.send = function(target, code, data, callback) { | ||
if (typeof target === 'string') target = this.device(target); | ||
|
||
var command = new Command(code, { sender: this, target: target }); | ||
var content; | ||
|
||
try { | ||
content = command.message(data); | ||
} catch (err) { | ||
return callback(err); | ||
} | ||
|
||
var checksum = new Buffer(2); | ||
|
||
checksum.writeUInt16BE(crc(content), 0); | ||
|
||
var message = Buffer.concat([this.address, Constants, content, checksum]); | ||
|
||
this.socket.send(message, 0, message.length, | ||
this.port, this.gateway, callback); | ||
}; | ||
|
||
/** | ||
* Parse SmartBus message | ||
* | ||
* @param {Buffer} message | ||
* @return {Command} | ||
*/ | ||
Bus.prototype.parse = function(message) { | ||
if (!isValid(message) || source(message) !== this.gateway) return; | ||
|
||
message = message.slice(16); | ||
|
||
var code = message.readUIntBE(5, 2); | ||
var sender = this.device(message.readUInt8(1), message.readUInt8(2)); | ||
|
||
sender.type = message.readUIntBE(3, 2); | ||
|
||
var command = new Command(code, { | ||
target: this.device(message.readUInt8(7), message.readUInt8(8)), | ||
sender: sender | ||
}); | ||
|
||
command.parse(message.slice(9, message.readUInt8(0) - 2)); | ||
|
||
return command; | ||
}; | ||
|
||
function handler(message) { | ||
var command = this.parse(message); | ||
|
||
if (!command) return; | ||
|
||
var data = command.data; | ||
var code = command.code; | ||
var sender = command.sender; | ||
var target = command.target; | ||
|
||
debug('%d.%d -> %d.%d %s: %s', | ||
sender.subnet, sender.id, target.subnet, target.id, command, | ||
isBuffer(data) ? data.toString('hex').toUpperCase() : inspect(data)); | ||
|
||
this.emit('command', command); | ||
|
||
if (target.subnet === 0xFF && target.id === 0xFF) | ||
this.emit('broadcast', command); | ||
|
||
this.emit(code, command); | ||
sender.emit(code, data, target); | ||
} | ||
|
||
function createSocket(bus) { | ||
var socket = dgram.createSocket('udp4'); | ||
|
||
socket.on('message', handler.bind(bus)); | ||
|
||
socket.on('error', function(err) { | ||
console.error('Error on socket: %s', err.message); | ||
|
||
socket.close(); | ||
}); | ||
|
||
socket.bind(bus.port, function() { | ||
socket.setBroadcast(true); | ||
|
||
var address = socket.address(); | ||
|
||
bus.address = new Buffer(address.address.split('.')); | ||
|
||
debug('UDP Server listening on ' + address.address + ":" + address.port); | ||
}); | ||
|
||
return socket; | ||
} | ||
|
||
/** | ||
* Check if buffer is valid Smart Bus command | ||
* | ||
* @param {Buffer} message | ||
* @return {Boolean} | ||
*/ | ||
function isValid(message) { | ||
if (!Constants.equals(message.slice(4, 16))) return false; | ||
|
||
var checksum = message.readUInt16BE(message.length - 2); | ||
|
||
return checksum === crc(message.slice(16, -2)); | ||
} | ||
|
||
/** | ||
* Return message sender IP address | ||
* | ||
* @param {Buffer} message | ||
* @return {String} | ||
*/ | ||
function source(message) { | ||
var ip = []; | ||
|
||
for (var n of message.slice(0, 4)) ip.push(n); | ||
|
||
return ip.join('.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module.exports = Channel; | ||
|
||
function Channel(device, number) { | ||
this.device = device; | ||
this.number = number; | ||
} | ||
|
||
/** | ||
* Set channel level | ||
* | ||
* @param {Number} value | ||
* @param {Number} options.time Running time | ||
* @param {Function} callback | ||
*/ | ||
Channel.prototype.level = function(value, options, callback) { | ||
if (typeof options === 'function') callback = options, options = null; | ||
|
||
options = options || {}; | ||
|
||
var device = this.device; | ||
var data = { | ||
time: options.time || 0, | ||
level: value, | ||
channel: this.number | ||
}; | ||
|
||
device.once(0x0032, done); | ||
|
||
device.send(0x0031, data, function(err) { | ||
if (!err) return; | ||
|
||
device.removeListener(0x0032, done); | ||
callback(err); | ||
}); | ||
|
||
function done(data) { | ||
callback(null, data); | ||
} | ||
}; |
Oops, something went wrong.