forked from maxime1992/linak-2-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (192 loc) · 5.51 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require('dotenv').config();
const {
Observable,
tap,
switchMap,
filter,
shareReplay,
take,
concatMap,
withLatestFrom,
Subject,
map,
timer,
repeat,
} = require('rxjs');
const { spawn, exec } = require('child_process');
const YAML = require('yaml');
const fs = require('fs');
const linakConfigYamlFileContent = fs.readFileSync(
'./linak-config.yaml',
'utf8'
);
const linakConfigYamlParsed = YAML.parse(linakConfigYamlFileContent);
const MQTT_HOST = process.env.MQTT_HOST;
const MQTTS_PORT = +process.env.MQTTS_PORT;
const MQTT_USER = process.env.MQTT_USER;
const MQTT_PASSWORD = process.env.MQTT_PASSWORD;
const mqtt = require('mqtt');
const client = mqtt.connect(`mqtt://${MQTT_HOST}:${MQTTS_PORT}`, {
protocolId: 'MQIsdp',
protocolVersion: 3,
username: MQTT_USER,
password: MQTT_PASSWORD,
rejectUnauthorized: false,
});
const mqttConnect$ = new Observable((observer) => {
client.on('connect', () => {
client.subscribe('linak-2-mqtt/set-desk-height');
client.subscribe('linak-2-mqtt/toggle-desk-position');
observer.next();
});
}).pipe(shareReplay(1));
const connected$ = new Subject();
connected$
.pipe(
tap(() => {
console.log('triggering watch..');
spawn('idasen-controller', ['--forward', '--watch']);
})
)
.subscribe();
const mqttDeskPositionMm$ = mqttConnect$.pipe(
tap(() => console.log('[debug] Connected')),
switchMap(() => startServer()),
tap((x) => console.log(`returned`, x)),
shareReplay(1)
);
const mqttDeskPositionCm$ = mqttDeskPositionMm$.pipe(
map((deskPositionMm) => Math.round(deskPositionMm / 10)),
shareReplay(1)
);
mqttDeskPositionCm$
.pipe(
tap((heightCm) => {
console.log(`publishing updated height`, {
absolute: heightCm,
'process.env.DESK_LOWEST_HEIGHT_CM': +process.env.DESK_LOWEST_HEIGHT_CM,
relative: heightCm - +process.env.DESK_LOWEST_HEIGHT_CM,
});
client.publish(
`linak-2-mqtt/desk-absolute-height-updated`,
heightCm.toString()
);
client.publish(
`linak-2-mqtt/desk-relative-height-updated`,
(heightCm - +process.env.DESK_LOWEST_HEIGHT_CM).toString()
);
})
)
.subscribe();
mqttDeskPositionCm$
.pipe(
switchMap(() =>
timer(60_000).pipe(
tap(() => {
console.log(
'Ping to get the height and make sure the desk appears as connected'
);
spawn('idasen-controller', ['--forward', '--watch']);
}),
repeat()
)
)
)
.subscribe();
const clientMessage$ = mqttConnect$
.pipe(
switchMap(() =>
new Observable((observer) => {
client.on('message', (topic, message) =>
observer.next({ topic, message })
);
}).pipe(
tap((x) =>
console.log(
`client message. Topic "${x.topic}", value: ${x.message.toString()}`
)
)
)
)
)
.pipe(shareReplay(1));
const mqttSetHeightCommand$ = clientMessage$
.pipe(filter(({ topic }) => topic === `linak-2-mqtt/set-desk-height`))
.pipe(shareReplay(1));
const mqttToggleDeskPositionCommand$ = clientMessage$
.pipe(filter(({ topic }) => topic === `linak-2-mqtt/toggle-desk-position`))
.pipe(shareReplay(1));
mqttSetHeightCommand$
.pipe(
tap(({ message }) => {
const messageStr = message.toString();
const heightCm = +messageStr;
const heightMm = heightCm * 10;
console.log(`Attempt to move desk to ${heightCm}cm`);
spawn('idasen-controller', ['--forward', '--move-to', heightMm]);
})
)
.subscribe();
const heightDiffBetweenSitAndUpCm =
(linakConfigYamlParsed.stand_height - linakConfigYamlParsed.sit_height) / 10;
const heightBetweenSitAndUpCm =
linakConfigYamlParsed.sit_height / 10 + heightDiffBetweenSitAndUpCm / 2;
mqttToggleDeskPositionCommand$
.pipe(
withLatestFrom(mqttDeskPositionCm$),
tap(([_, heightCm]) => {
console.log('toggling...', { heightCm, heightBetweenSitAndUpCm });
console.log('received desk position', heightCm);
if (heightCm < heightBetweenSitAndUpCm) {
console.log(
`moving to standing position, from ${heightCm}cm, to ${linakConfigYamlParsed.stand_height}cm`
);
spawn('idasen-controller', [
'--forward',
'--move-to',
linakConfigYamlParsed.stand_height,
]);
} else {
console.log('moving to 750', heightCm);
spawn('idasen-controller', [
'--forward',
'--move-to',
linakConfigYamlParsed.sit_height,
]);
}
})
)
.subscribe();
const HEIGHT_REPORTED_REGXP = /height: *(\d+)mm/i;
const startServer = () => {
return new Observable((observer) => {
console.log('starting idasen server...');
// const ls = spawn('bash', ['./mock.sh']);
const ls = spawn(
// https://stackoverflow.com/a/32636139/2398593
'unbuffer',
['idasen-controller', '--config', './linak-config.yaml', '--server'],
{ shell: true }
);
ls.stdout.on('data', (data) => {
const message = data.toString();
console.log(message);
if (message.startsWith('Connected')) {
connected$.next();
}
const reMatch = message.match(HEIGHT_REPORTED_REGXP);
if (reMatch) {
const height = reMatch[1];
observer.next(+height);
}
});
ls.stderr.on('data', (data) => {
// @todo
console.log('stderr: ' + data.toString());
});
ls.on('exit', (code) => {
// @todo
console.log('child process exited with code ' + code.toString());
});
});
};