-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpimatic-deebot.coffee
631 lines (559 loc) · 21.7 KB
/
pimatic-deebot.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
M = env.matcher
_ = require('lodash')
deebot = require('ecovacs-deebot')
nodeMachineId = require('node-machine-id')
CronJob = env.CronJob or require('cron').CronJob
everyDay = "0 1 0 * * *" # at midnight at 00:01
everyMinute = "0 * * * * *"
class DeebotPlugin extends env.plugins.Plugin
init: (app, @framework, @config) =>
pluginConfigDef = require './pimatic-deebot-config-schema'
@deviceConfigDef = require("./device-config-schema")
@email = @config.email # "[email protected]";
@password = @config.password #"a1b2c3d4";
@countrycode = @config.countrycode # 'DE';
EcoVacsAPI = deebot.EcoVacsAPI
@email = @config.email # "[email protected]";
password = @config.password #"a1b2c3d4";
countrycode = (@config.countrycode ).toUpperCase() # 'DE';
@password_hash = EcoVacsAPI.md5(password)
device_id = EcoVacsAPI.md5(nodeMachineId.machineIdSync())
countries = deebot.countries
@continent = countries[countrycode].continent.toLowerCase()
@api = new EcoVacsAPI(device_id, countrycode, @continent)
@framework.deviceManager.registerDeviceClass('DeebotDevice', {
configDef: @deviceConfigDef.DeebotDevice,
createCallback: (config, lastState) => new DeebotDevice(config, lastState, @, @framework, @api, EcoVacsAPI, @continent)
})
@framework.ruleManager.addActionProvider(new DeebotActionProvider(@framework))
@framework.deviceManager.on('discover', (eventData) =>
@framework.deviceManager.discoverMessage 'pimatic-deebot', 'Searching for new devices'
### vacuum data
"did": "E0000962817609920075",
"name": "E0000962817609920075",
"class": "113",
"resource": "atom",
"nick": null,
"company": "eco-legacy"
###
@api.connect(@email, @password_hash)
.then(() =>
@api.devices()
.then((devices) =>
for vacuum in devices
env.logger.info "Device: " + JSON.stringify(vacuum,null,2)
_did = (if vacuum.did? then vacuum.did else vacuum).toLowerCase()
if _.find(@framework.deviceManager.devicesConfig,(d) => (d.id).indexOf(_did)>=0)
env.logger.info "Device '" + _did + "' already in config"
else
config =
id: _did
name: (if vacuum.nick? then vacuum.nick else if vacuum.name? then vacuum.name else vacuum).toLowerCase()
class: "DeebotDevice"
nickname: (if vacuum.nick? then vacuum.nick else "")
@framework.deviceManager.discoveredDevice( "Deebot", config.name, config)
)
).catch((e) =>
env.logger.error 'Failure in connecting: ' + e.message
)
)
class DeebotDevice extends env.devices.Device
constructor: (config, lastState, @plugin, @framework, api, EcoVacsAPI, continent) ->
@config = config
@id = @config.id
@name = @config.name
@api = api
@EcoVacsAPI_REALM = EcoVacsAPI.REALM
@vacuum = @id
@continent = continent
@waterLevels = ["min","normal","high","max"]
@capabilities =
hasMainBrush: false
hasSpotAreas: false
hasCustomAreas: false
hasMoppingSystem: false
hasVoiceReports: false
modes : ["auto", "edge", "spot", "spot_area", "single_room", "stop"]
currentMode: "auto"
SpotArea: []
CustomArea:
map_position:
x1: 0
y1: 0
x2: 0
y2: 0
cleanings: 1
@attributes = {}
@attributeValues = {}
vacBotListeners = @plugin.deviceConfigDef.DeebotDevice.properties.attributes.items.enum
@show = false
@hide = false
if not @config.show? or @config.show is "all"
@show = false
@hide = true
else
@show = true
@hide = false
@attributes["status"] =
description: "status"
type: "string"
label: "status"
acronym: "status"
displaySparkline: false
@attributeValues["status"] = "offline"
@_createGetter("status", =>
return Promise.resolve @attributeValues["status"]
)
@setAttr "status", @attributeValues["status"]
@numberAttributes = ["BatteryInfo","LifeSpan_filter","LifeSpan_main_brush","LifeSpan_side_brush"]
@booleanAttributes = ["WaterBoxInfo"]
for _attr in vacBotListeners
do (_attr) =>
@attributes[_attr] =
description: _attr
type: "string"
label: _attr
acronym: _attr
hidden: @show
displaySparkline: false
if _.find(@numberAttributes, (n)=> n.indexOf(_attr)>=0)
@attributes[_attr].type = "number"
@attributes[_attr].unit = "%"
@attributes[_attr].displayFormat = "fixed,decimal:0"
_lastState = (if lastState?[_attr]?.value? then Number lastState[_attr].value else 0 )
else if _.find(@booleanAttributes, (n)=> n.indexOf(_attr)>=0)
@attributes[_attr].type = "boolean"
_lastState = (if lastState?[_attr]?.value? then lastState[_attr].value else false )
else
_lastState = (if lastState?[_attr]?.value? then lastState[_attr].value else "" )
@attributeValues[_attr] = _lastState
@_createGetter(_attr, =>
return Promise.resolve @attributeValues[_attr]
)
#@framework.on 'after init', =>
for _attr in @config.attributes
do (_attr) =>
@attributes[_attr].hidden = @hide
@setAttr _attr, @attributeValues[_attr]
initDeebot = () =>
@api.connect(@plugin.email, @plugin.password_hash)
.then(() =>
@setAttr("status","connecting")
@api.devices()
.then((devices) =>
#device = _.find(devices, (d)=> d.name is @vacuum)
device = devices[0]
if device?
@vacbot = @api.getVacBot(@api.uid, @EcoVacsAPI_REALM, @api.resource, @api.user_access_token, device, @continent)
@vacbot.on 'ready', (event) =>
env.logger.debug('connected')
@setAttr("status","Deebot connected")
for listener in vacBotListeners
do(listener)=>
@vacbot.on(listener, (value) =>
unless value?
return
if listener.indexOf("WaterLevel")>= 0
if level<0 then level = 0
if level>3 then level = 3
@setAttr(listener, @waterLevels[level] )
else if (@attributes[listener].type).indexOf("number") >= 0
@setAttr(listener, Math.round(Number value))
else
@setAttr(listener, value)
@setAttr("status","Deebot online")
)
@vacbot.on 'error', (err) =>
env.logger.error "Error vacbot.on, try to reconnect in 5 minutes " + err
# try to reconnect
@vacbot.disconnect()
clearTimeout(@reconnectTimer)
clearTimeout(@statusTimer)
@reconnectTimer = setTimeout(initDeebot, 300000)
getStatus = () =>
@vacbot.run("GetBatteryState")
@vacbot.run('GetChargeState')
@vacbot.run("GetCleanState")
if @capabilities.hasMainBrush
@vacbot.run('GetLifeSpan', 'main_brush')
@vacbot.run('GetLifeSpan', 'side_brush')
@vacbot.run('GetLifeSpan', 'filter')
if @capabilities.hasMoppingSystem
@vacbot.run("GetWaterInfo")
@capabilities.hasMainBrush = @vacbot.hasMainBrush()
@capabilities.hasSpotAreas = @vacbot.hasSpotAreas()
@capabilities.hasCustomAreas = @vacbot.hasCustomAreas()
@capabilities.hasMoppingSystem = @vacbot.hasMoppingSystem()
@capabilities.hasVoiceReports = @vacbot.hasVoiceReports()
@statusTimer = setTimeout(getStatus, 60000)
getStatus()
@vacbot.on 'message', (m) =>
env.logger.debug "Message " + m
@vacbot.connect_and_wait_until_ready()
).catch((err)=>
env.logger.error "Error getting devices, try to reconnect in 5 minutes " + err
if @vacbot
@vacbot.disconnect()
clearTimeout(@reconnectTimer)
clearTimeout(@statusTimer)
@reconnectTimer = setTimeout(initDeebot, 300000)
)
).catch((err) =>
env.logger.error "Error connecting " + err
@reconnectTimer = setTimeout(initDeebot, 120000)
clearTimeout(@statusTimer)
env.logger.info "Deebot is offline, try reconnecting in 2 minutes"
@setAttr("status","offline")
)
initDeebot()
@reconnectingJob = new CronJob
cronTime: everyDay
onTick: =>
env.logger.debug "Daily reconnect of Deebot"
if @vacbot
@vacbot.disconnect()
clearTimeout(@reconnectTimer)
clearTimeout(@statusTimer)
initDeebot()
@reconnectingJob.start()
super()
execute: (command, rooms, speed, water, area, cleanings) =>
return new Promise((resolve,reject) =>
#env.logger.info "Command " + command + ", Rooms " + rooms + ", Speed " + speed + ", area " + JSON.stringify(area) + ", cleanings " + cleanings
#return
switch command
when "clean"
@vacbot.run("Clean") #, @capabilities.currentMode, "start")
when "cleanroom"
@vacbot.run("SpotArea", "start", rooms)
when "pause"
@vacbot.run("Pause") #, @capabilities.currentMode, "pause")
when "resume"
@vacbot.run("Resume") #, @capabilities.currentMode, "resume")
when "stop"
@vacbot.run("Stop") #, @capabilities.currentMode, "stop")
when "charge"
@vacbot.run("Charge")
when "spot"
@vacbot.run("SpotArea", @capabilities.currentMode, area)
when "cleanarea"
@vacbot.run("CustomArea", "start", area, cleanings)
when "speed"
@vacbot.run("SetCleanSpeed", speed)
when "waterlevel"
@vacbot.run("SetWaterLevel", water)
else
env.logger.debug "Unknown command " + command
reject()
resolve()
)
setAttr: (attr, _status) =>
unless @attributeValues[attr] is _status
env.logger.debug "attribute '" + attr + "' with type '" + @attributes[attr].type + "', is set to " + _status
@attributeValues[attr] = _status
@emit attr, _status
destroy:() =>
@vacbot.disconnect() if @vacbot
clearTimeout(@reconnectTimer)
clearTimeout(@statusTimer)
@reconnectingJob.stop() if @reconnectingJob
super()
class DeebotActionProvider extends env.actions.ActionProvider
constructor: (@framework) ->
parseAction: (input, context) =>
beebotDevice = null
@speed = 2
@waterlevel = 1
@roomsArray = []
@roomsStringVar = null
@waterStringVar = null
@area =
x1: 0
y1: 0
x2: 0
y2: 0
@areaStringVar = null
@cleanings = 1
@cleaningsStringVar = null
deebotDevices = _(@framework.deviceManager.devices).values().filter(
(device) => device.config.class == "DeebotDevice"
).value()
setCommand = (command) =>
@command = command
addSpeed = (m,tokens) =>
unless tokens>0 and tokens<5
context?.addError("Speed must be 1, 2, 3 or 4.")
return
setCommand("speed")
@speed = Number tokens
addRoom = (m,tokens) =>
unless tokens >=0
context?.addError("Roomnumber should 0 or higher.")
return
@roomsArray.push Number tokens
setCommand("cleanroom")
addWaterlevel = (m,tokens) =>
unless tokens>0 and tokens<5
context?.addError("Waterlevel must be 1, 2, 3 or 4.")
return
setCommand("waterlevel")
@waterlevel = Number tokens
roomString = (m,tokens) =>
unless tokens?
context?.addError("No variable")
return
@roomsStringVar = tokens
setCommand("cleanroom")
return
speedString = (m,tokens) =>
unless tokens?
context?.addError("No variable")
return
@speedStringVar = tokens
setCommand("speed")
return
waterlevelString = (m,tokens) =>
unless tokens?
context?.addError("No variable")
return
@waterStringVar = tokens
setCommand("waterlevel")
return
addAreaX1 = (m,tokens) =>
@area.x1 = tokens
setCommand("cleanarea")
addAreaY1 = (m,tokens) =>
@area.y1 = tokens
setCommand("cleanarea")
addAreaX2 = (m,tokens) =>
@area.x2 = tokens
setCommand("cleanarea")
addAreaY2 = (m,tokens) =>
@area.y2 = tokens
setCommand("cleanarea")
areaString = (m,tokens) =>
unless tokens?
context?.addError("No variable")
return
@areaStringVar = tokens
setCommand("cleanroom")
return
addCleanings = (m,tokens) =>
env.logger.info "Cleanings " + tokens
unless (Number tokens) == 1 or (Number tokens) == 2
context?.addError("Cleanings should be 1 or 2.")
return
@cleanings = tokens
setCommand("cleanarea")
cleaningsString = (m,tokens) =>
unless tokens?
context?.addError("No variable")
return
@cleaningsStringVar = tokens
setCommand("cleanarea")
return
m = M(input, context)
.match('deebot ')
.matchDevice(deebotDevices, (m, d) ->
# Already had a match with another device?
if beebotDevice? and deebotDevices.id isnt d.id
context?.addError(""""#{input.trim()}" is ambiguous.""")
return
beebotDevice = d
)
.or([
((m) =>
return m.match(' clean', (m) =>
setCommand('clean')
match = m.getFullMatch()
)
),
((m) =>
return m.match(' clean')
.or([
((m) =>
return m.match(' [')
.matchNumber(addRoom)
.match(']')
),
((m) =>
return m.match(' ')
.matchVariable(roomString)
)
])
),
((m) =>
return m.match(' cleanarea ')
.or([
((m) =>
return m.match('[')
.matchNumber(addAreaX1)
.match(",")
.matchNumber(addAreaY1)
.match(",")
.matchNumber(addAreaX2)
.match(",")
.matchNumber(addAreaY2)
.match(']')
),
((m) =>
return m.matchVariable(areaString)
)
])
.match(' cleanings ')
.or([
((m) =>
return m.matchNumber(addCleanings)
),
((m) =>
return m.matchVariable(cleaningsString)
)
])
),
((m) =>
return m.match(' pause', (m) =>
setCommand('pause')
match = m.getFullMatch()
)
),
((m) =>
return m.match(' resume', (m) =>
setCommand('resume')
match = m.getFullMatch()
)
),
((m) =>
return m.match(' stop', (m) =>
setCommand('stop')
match = m.getFullMatch()
)
),
((m) =>
return m.match(' charge', (m)=>
setCommand('charge')
match = m.getFullMatch()
)
),
((m) =>
return m.match(' speed ')
.or([
((m) =>
return m.matchNumber(addSpeed)
),
((m) =>
return m.matchVariable(speedString)
)
])
),
((m) =>
return m.match(' waterlevel ')
.or([
((m) =>
return m.matchNumber(addWaterlevel)
),
((m) =>
return m.matchVariable(waterlevelString)
)
])
)
])
#@rooms = @roomsArray
#convert rooms array into comma seperated string (list)
@rooms = ""
for room,i in @roomsArray
@rooms += room
if i < @roomsArray.length - 1
@rooms += ", "
#@rooms += ")"
#env.logger.debug "command " + @command + ", Roomlist " + @rooms
match = m.getFullMatch()
if match? #m.hadMatch()
env.logger.debug "Rule matched: '", match, "' and passed to Action handler"
return {
token: match
nextInput: input.substring(match.length)
actionHandler: new DeebotActionHandler(@framework, beebotDevice, @command, @rooms, @roomsStringVar, @speed,
@speedStringVar, @waterlevel, @waterStringVar, @area, @areaStringVar, @cleanings, @cleaningsStringVar)
}
else
return null
class DeebotActionHandler extends env.actions.ActionHandler
constructor: (@framework, @beebotDevice, @command, @rooms, @roomsStringVar, @speed,
@speedStringVar, @waterlevel, @waterStringVar, @area, @areaStringVar, @cleanings, @cleaningsStringVar) ->
executeAction: (simulate) =>
if simulate
return __("would have cleaned \"%s\"", "")
else
if @roomsStringVar?
_var = @roomsStringVar.slice(1) if @roomsStringVar.indexOf('$') >= 0
_rooms = @framework.variableManager.getVariableValue(_var)
unless _rooms?
return __("\"%s\" Rule not executed, #{_rooms} is not a valid room string", "")
_roomArr = (String _rooms).split(',')
newRooms = ""
for room,i in _roomArr
_room = (room.trimLeft()).trimEnd()
if Number.isNaN(Number _room) or Number _room < 0 or not (room?) or room is ""
return __("\"%s\" Rule not executed, #{_rooms} is not a valid room string", "")
if i > 0 then newRooms = newRooms + " "
newRooms = newRooms + _room
if i < (_roomArr.length - 1) then newRooms = newRooms + ","
else
newRooms = @rooms
if @speedStringVar?
_var = @speedStringVar.slice(1) if @speedStringVar.indexOf('$') >= 0
_speed = @framework.variableManager.getVariableValue(_var)
unless _speed?
return __("\"%s\" Rule not executed, #{_speed} is not a valid variable", "")
if Number.isNaN(Number _speed) or Number _speed < 1 or Number _speed > 4
return __("\"%s\" Rule not executed, #{_speed} is not a valid speed value", "")
newSpeed = _speed
else
newSpeed = @speed
if @waterStringVar?
_var = @waterStringVar.slice(1) if @waterStringVar.indexOf('$') >= 0
_waterlevel = @framework.variableManager.getVariableValue(_var)
unless _waterlevel?
return __("\"%s\" Rule not executed, #{_waterlevel} is not a valid variable", "")
if Number.isNaN(Number _waterlevel) or Number _waterlevel < 1 or Number _waterlevel > 4
return __("\"%s\" Rule not executed, #{_waterlevel} is not a valid waterlevel value", "")
newWaterlevel = _waterlevel
else
newWaterlevel = @waterlevel
if @areaStringVar?
_var = @areaStringVar.slice(1) if @areaStringVar.indexOf('$') >= 0
_area = @framework.variableManager.getVariableValue(_var)
unless _area?
return __("\"%s\" Rule not executed, #{_area} is not a valid area string", "")
_coordsArr = (String _area).split(',')
newArea = ""
for coord,i in _coordsArr
_coord = (coord.trimLeft()).trimEnd()
if Number.isNaN(Number _coord)
return __("\"%s\" Rule not executed, #{_coord} is not a valid coordinate number", "")
if i > 0 then newArea = newArea + " "
newArea = newArea + _coord
if i < (_coordsArr.length - 1) then newArea = newArea + ","
else
newArea = @area.x1 + ", " + @area.y1 + ", " + @area.x2 + ", " + @area.y2
if @cleaningsStringVar?
_var = @cleaningsStringVar.slice(1) if @cleaningsStringVar.indexOf('$') >= 0
_cleanings = @framework.variableManager.getVariableValue(_var)
unless _cleanings?
return __("\"%s\" Rule not executed, #{_cleanings} does not excist", "")
if Number.isNaN(Number _cleanings) or Number _cleanings < 1 or Number _cleanings > 2
return __("\"%s\" Rule not executed, #{_cleanings} is not a valid cleanings value", "")
newCleanings = _cleanings
else
newCleanings = @cleanings
@beebotDevice.execute(@command, newRooms, newSpeed, newWaterlevel, newArea, newCleanings)
.then(()=>
return __("\"%s\" Rule executed", @command)
).catch((err)=>
return __("\"%s\" Rule not executed", "")
)
deebotPlugin = new DeebotPlugin
return deebotPlugin