-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnocean.coffee
41 lines (30 loc) · 1.03 KB
/
Enocean.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
#
# Copyright (c) 2019 Alexander Sporn. All rights reserved.
#
{EventEmitter} = require 'events'
SerialPort = require 'serialport'
EnoceanJS = require 'enocean-js'
module.exports = class Enocean extends EventEmitter
constructor: (@options) ->
super()
@port = new SerialPort(@options.port, baudRate: 57600)
@parser = @port.pipe new EnoceanJS.ESP3Parser()
@transformer = @parser.pipe new EnoceanJS.ESP3Transformer()
@port.on 'open', =>
console.log 'opened port ' + @options.port
@port.on 'error', (error) =>
@emit 'error', error
@transformer.on 'data', (data) =>
msg = data.decode "f6-02-01"
unless msg? and msg.R2?
return
switch msg.R1.rawValue
when 0 then button = "AI"
when 1 then button = "A0"
when 2 then button = "BI"
when 3 then button = "B0"
else return
switch msg.EB.rawValue
when 0 then @emit 'released', data.senderId, button
when 1 then @emit 'pressed', data.senderId, button
else return