Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
l3v5y committed Jan 15, 2015
1 parent 64ef569 commit ae19435
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

node_modules/
55 changes: 55 additions & 0 deletions app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
mqtt = require("mqtt")
serialport = require "serialport"
SerialPort = serialport.SerialPort
convertHex = require('convert-hex')

class Packet
constructor: (@status, @hardwareId, @size, rawVersion, rawType, rawPayload) ->
if rawPayload
@payload = new Buffer(convertHex.hexToBytes(rawPayload))
else
@payload = null
@version = parseInt(rawVersion)
@type = parseInt(rawType)

class TemperaturePacket
constructor: ->
@temperature = 0

@decode: (payload) ->
temperaturePacket = new TemperaturePacket()
console.log payload
temperaturePacket.temperature = payload.readInt16LE(0) / 10
temperaturePacket

class PacketDispatcher
constructor: ->
@serialPort = new SerialPort "/dev/tty.usbserial-A6007oLa",
baudrate: 115200
parser: serialport.parsers.readline('\r\n')
@serialPort.on 'open', @open
@client = mqtt.connect "tcp://192.168.0.112"

open: =>
@serialPort.on 'data', @onPacketArrived

onPacketArrived: (data) =>
rawPacket = String(data).trim().split(':')
packet = new Packet(rawPacket[0], rawPacket[2], rawPacket[3], rawPacket[4], rawPacket[5], rawPacket[6])


payload = @decodePayload(packet)
@dispatchPayload(packet, payload)

decodePayload: (packet) =>
if packet.status is 'OK'
if packet.type is 1
if packet.version is 1
TemperaturePacket.decode(packet.payload)


dispatchPayload: (packet, payload) =>
console.log 'dispatching', payload, 'from', packet.hardwareId
@client.publish("RF24HOME/#{packet.hardwareId}", JSON.stringify(payload))

packetDispatcher = new PacketDispatcher()
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('coffee-script/register');
require('./app');
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "rf24-mqtt-bridge",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"coffee-script": "^1.8.0",
"convert-hex": "^0.1.0",
"mqtt": "^0.3.13",
"serialport": "^1.4.10"
}
}

0 comments on commit ae19435

Please sign in to comment.