diff --git a/MySensors-config-schema.coffee b/MySensors-config-schema.coffee index eb6f1be..104fc69 100755 --- a/MySensors-config-schema.coffee +++ b/MySensors-config-schema.coffee @@ -10,15 +10,42 @@ module.exports = { type: "boolean" default: true driver: - description: "The diver to connect to the PiGateway" + description: "The driver to connect to the gateway" type: "string" - enum: ["serialport"] + enum: ["serialport", "ethernet"] default: "serialport" + defines: + property: "driverOptions" + options: + serialport: + title: "serialport driver options" + type: "object" + properties: + serialDevice: + description: "The name of the serial device to use" + type: "string" + default: "/dev/ttyUSB0" + baudrate: + description: "The baudrate to use for serial communication" + type: "integer" + default: 115200 + ethernet: + title: "ethernet driver options" + type: "object" + properties: + host: + description: "The IP address of the gateway" + type: "string" + default: "192.168.1.100" + port: + description: "The port of the gateway" + type: "integer" + default: 5003 driverOptions: description: "Options for the driver" type: "object" default: { - "serialDevice": '/dev/ttyUSB0', #"/dev/ttyUSB0", + "serialDevice": "/dev/ttyUSB0", "baudrate": 115200 } protocols: diff --git a/MySensors.coffee b/MySensors.coffee index f5f45a4..0cddee9 100755 --- a/MySensors.coffee +++ b/MySensors.coffee @@ -161,23 +161,31 @@ module.exports = (env) -> constructor: (framework,config) -> @config = config @framework = framework - assert @config.time in ["utc", "local"] - assert @config.driver in ["serialport", "gpio"] + assert @config.time in ["utc", "local"] + assert @config.driver in ["serialport", "ethernet"] + @timeOffset = 0 if @config.time is "local" @timeOffset = ((new Date()).getTimezoneOffset() * 60 ) env.logger.debug "<- TimeOffset ", @timeOffset + # setup a new driver switch @config.driver when "serialport" SerialPortDriver = require('./serialport')(env) @driver = new SerialPortDriver(@config.driverOptions) + when "ethernet" + EthernetDriver = require './ethernet' + @driver = new EthernetDriver(@config.driverOptions) - @driver.on('error', (error) => @emit('error', error) ) - @driver.on('reconnect', (error) => @emit('reconnect', error) ) + @driver.on('error', (error) => + env.logger.error error + ) + @driver.on('reconnect', (error) => + @emit('reconnect', error) + ) @driver.on('close', => - @emit('close') ) @driver.on("data", (data) => @@ -189,7 +197,7 @@ module.exports = (env) -> ) - connect: (timeout = 20000, retries = 3) -> + connect: (timeout = 2500, retries = 3) -> return @pendingConnect = @driver.connect(timeout, retries) diff --git a/ethernet.coffee b/ethernet.coffee new file mode 100755 index 0000000..c93053c --- /dev/null +++ b/ethernet.coffee @@ -0,0 +1,60 @@ +events = require 'events' +net = require 'net' +Promise = require 'bluebird' + +class EthernetDriver extends events.EventEmitter + + constructor: (protocolOptions)-> + @port = protocolOptions.port + @host = protocolOptions.host + + connect: (timeout, retries) -> + + # Create connection + @connection = net.createConnection @port, @host + + # cleanup + @ready = no + + # reject promise on close + @connection.on 'close', () => + @emit('close') + + # setup data listener + @connection.on 'data', (data) => + # Sanitize data + data = data.toString() + line = data.replace(/(\r\n|\n|\r)/gm,"") + @emit('line', line) + + # reject promise on error + @connection.on 'error', (error) => + @emit('error', error) + + #resolve promise on connect + @connection.on 'connect', () => + @ready = yes + @emit 'ready' + return + + return new Promise( (resolve, reject) => + @once("ready", resolve) + @once("error", reject) + ) + + disconnect: -> + @connection.end() + return Promise.resolve() + + write: (data) -> + if not @connection.write(data, 'utf-8', () => + @emit "done" + ) + @emit "error" + + return new Promise( (resolve, reject) => + @once("done", resolve) + @once("error", reject) + ) + +module.exports = EthernetDriver diff --git a/package.json b/package.json index 389a7b6..199829b 100755 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "files": [ "MySensors.coffee", "serialport.coffee", + "ethernet.coffee", "README.md", "device-config-schema.coffee", "MySensors-config-schema.coffee",