-
Notifications
You must be signed in to change notification settings - Fork 0
/
matsteon.js
executable file
·168 lines (124 loc) · 3.73 KB
/
matsteon.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const IOLINC_ID = "22D4EC"
const LIGHT_CABANON_ID = "1B3F94"
const LIGHT_CUISINE_ID = "1864C0"
const LIGHT_MAISON_ID = "1A21DF"
const LIGHT_PISCINE_ID = "2E5228"
const LIGHT_CABANON_PISCINE_ID = "2E5C21"
const PLM_PORT = "/dev/insteonPLM"
const API_PORT = 3000
console.log('init');
var Insteon = require('home-controller').Insteon;
var express = require('express');
var mqtt = require('mqtt')
hostIp = require('child_process').execSync("ip route show | awk '/default/ {print $3}'").toString('utf8');
var clientMQTT = mqtt.connect('mqtt://' + hostIp)
var app = express();
var plm = new Insteon();
plm.serial(PLM_PORT, function () {
console.log('plm connected');
app.listen(API_PORT);
});
app.get('/light/:id/on', function(req, res){
plm.light(req.params.id).turnOn()
.then(function (status) {
if(status.response) {
res.sendStatus(200);
} else {
console.log(status)
res.sendStatus(404);
}
});
});
app.get('/light/:id/off', function(req, res){
plm.light(req.params.id).turnOff()
.then(function (status) {
if(status.response) {
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
});
app.get('/light/:id/dim/:level', function(req, res){
plm.light(req.params.id).turnOn(req.params.level)
.then(function (status) {
if(status.response) {
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
});
app.get('/light/:id/status', function(req, res){
plm.light(req.params.id).level()
.then(function (level) {
res.send(level.toString());
});
});
app.get('/garage/bouton', function(req, res){
var io = plm.ioLinc(IOLINC_ID);
io.relayOn()
.then(function (status) {
if(status.response) {
io.relayOff()
.then(function (status) {
if(status.response) {
res.sendStatus(200);
} else {
res.sendStatus(404);
}
});
return;
} else {
res.sendStatus(404);
}
});
});
app.get('/garage/status', function(req, res){
plm.ioLinc(IOLINC_ID).status()
.then(function (status) {
res.send(((status.sensor == 'off') ? 'ouvert' : 'fermé'));
});
});
addLightEventsListeners(plm.light(LIGHT_CUISINE_ID))
addLightEventsListeners(plm.light(LIGHT_MAISON_ID))
addLightEventsListeners(plm.light(LIGHT_CABANON_ID))
function addLightEventsListeners(light) {
light.on('turnOn', function () {lightOn(light.id)});
light.on('turnOnFast', function () {lightOn(light.id)});
light.on('brightened', function () {lightOn(light.id)});
light.on('turnOff', function () {lightOff(light.id)});
light.on('turnOffFast', function () {lightOff(light.id)});
light.on('dimmed', function () {lightOff(light.id)});
}
plm.ioLinc(IOLINC_ID).on('sensorOn', function () {garageClosed()});
plm.ioLinc(IOLINC_ID).on('sensorOff', function () {garageOpen()});
function lightOn(id) {
clientMQTT.publish('smartthings/' + id.match(/.{1,2}/g).join(".") + '/switch', 'on')
console.log(id + ' is on')
}
function lightOff(id) {
clientMQTT.publish('smartthings/' + id.match(/.{1,2}/g).join(".") + '/switch', 'off')
console.log(id + ' is off')
}
function garageClosed() {
clientMQTT.publish('smartthings/garage/switch', 'off')
console.log('porte garage fermée')
}
function garageOpen() {
clientMQTT.publish('smartthings/garage/switch', 'on')
console.log('porte garage ouverte')
}
app.get('/init/:id', function(req, res){
plm.link(req.params.id, function(error, link) {
res.send('link ok');
});
});
app.get('/init2/:id', function(req, res){
plm.link(req.params.id, {controller: true}, function(error, link) {
res.send('link2 ok');
});
});
// link devide as controller and as responder
//plm.link('1864C0', function(error, link) {});
//plm.link('1864C0', {controller: true}, function(error, link) {});