-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
55 lines (43 loc) · 1.6 KB
/
app.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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()