-
Notifications
You must be signed in to change notification settings - Fork 9
/
rumba.groovy
1069 lines (972 loc) · 38.9 KB
/
rumba.groovy
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Rumba v1.7
* for 900/i7/s9 series
*
* Version History:
* 1.7: Implemented separate bin status capability to help distinguish from other errors
* 1.6: Implemented device health check (properly this time?), tweaked state logic.
* 1.0: Initial release
*
* Copyright 2021 Matvei Vevitsis
* with additional contributions from Jon Fields
* Based on iRobot Roomba v2.2 by Steve-Gregory (Copyright 2016)
* with additional modifications by Adrian Caramaliu and Justin Dybedahl
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
/*Known NotReady states*/
def getRoombaStates() {
def ROOMBA_READY = 0
def ROOMBA_STUCK = 1
def ROOMBA_BIN_FULL = 16
def ROOMBA_DEBRIS_EXTRACTOR = 6
def ROOMBA_NOT_UPRIGHT = 7
def ROOMBA_IN_THE_DARK = 8
def ROOMBA_BATTERYLOW = 15
def ROOMBA_STATES = ['ready': ROOMBA_READY, 'stuck': ROOMBA_STUCK, 'full': ROOMBA_BIN_FULL, 'tilted': ROOMBA_NOT_UPRIGHT, 'dark': ROOMBA_IN_THE_DARK, 'batterylow': ROOMBA_BATTERYLOW, 'debrisextractors': ROOMBA_DEBRIS_EXTRACTOR]
return ROOMBA_STATES
}
metadata {
definition (name: "Rumba", namespace: "circlefield05082", author: "Matvei Vevitsis", mnmn: "SmartThingsCommunity", ocfDeviceType: "oic.d.robotcleaner", vid: "2a2f2f85-a021-3da2-9f27-68af294851e5") {
capability "robotCleanerMovement"
//capability "robotCleanerCleaningMode"
//capability "robotCleanerTurboMode"
capability "Battery"
capability "Switch"
capability "Refresh"
capability "Polling"
capability "Consumable"
capability "Timed Session"
capability "Configuration"
capability "Health Check"
capability "circlefield05082.newbinstatus"
command "dock"
command "resume"
command "pause"
command "cancel"
command "pauseAndDock"
attribute "totalJobs", "number"
attribute "totalJobHrs", "number"
attribute "headline", "string"
attribute "robotName", "string"
attribute "robotIpAddress", "string"
attribute "preferences_set", "string"
attribute "status", "string"
attribute "pingTimeoutStage", "number"
//For ETA heuristic
attribute "lastSqft", "number"
attribute "lastRuntime", "number"
attribute "lastDate", "string"
}
}
// simulator metadata
simulator {
}
//Preferences
preferences {
section("Cloud Roomba API Type") {
input "localAPI", "bool", title: "Use a local REST gateway for Roomba", description: "Enable this if you have installed a local REST gateway for Roomba, you will need to provide the IP of that gateway", displayDuringSetup: true
}
section("Roomba Local Settings") {
input type: "paragraph", title: "Fill these parameters if using a local REST gateway"
input "roomba_host", "string", title:"IP of Roomba local REST Gateway", displayDuringSetup: true
input "roomba_port", "number", range: "1..65535", defaultValue: 3000, title:"Port of Roomba local REST Gateway", displayDuringSetup: true
}
section("Roomba Cloud Credentials") {
input type: "paragraph", title: "Please fill in the Roomba credentials below if using a Cloud connection to your robot", description: "The username/password can be retrieved via node.js & dorita980", displayDuringSetup: true
input "roomba_username", "text", title: "Roomba username/blid", displayDuringSetup: true
input "roomba_password", "password", title: "Roomba password", displayDuringSetup: true
}
section("Misc.") {
//input "sendPushMessage", "enum", title: "Push Notifications", description: "Alert if Roomba encounters a problem", options: ["Yes", "No"], defaultValue: "No", required: true
//input "sendAudioMessage", "enum", title: "Audio Notifications", options: ["Yes", "No"], defaultValue: "No", required: true
//input "audioDevices", "capability.audioNotification", title: "Select a speaker", required: false, multiple: true
input type: "paragraph", title: "Polling Interval [minutes]", description: "This feature allows you to change the frequency of polling for the robot in minutes (1-59)"
input "pollInterval", "number", title: "Polling Interval", description: "Change polling frequency (in minutes)", defaultValue:4, range: "1..59", required: true, displayDuringSetup: true
}
}
// UI tile definitions
tiles {
multiAttributeTile(name:"CLEAN", type:"generic", width: 6, height: 4, canChangeIcon: true) {
tileAttribute("device.status", key: "PRIMARY_CONTROL") {
attributeState "error", label: 'Error', icon: "st.switches.switch.off", backgroundColor: "#bc2323"
attributeState "bin-full", label: 'Bin Full', icon: "st.switches.switch.off", backgroundColor: "#bc2323"
attributeState "docked", label: 'Start Clean', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "starting"
attributeState "docking", label: 'Docking', icon: "st.switches.switch.off", backgroundColor: "#ffa81e"
attributeState "starting", label: 'Starting Clean', icon: "st.switches.switch.off", backgroundColor: "#ffffff"
attributeState "cleaning", label: 'Stop Clean', action: "stop", icon: "st.switches.switch.on", backgroundColor: "#79b821", nextState: "pausing"
attributeState "pausing", label: 'Stop Clean', icon: "st.switches.switch.on", backgroundColor: "#79b821"
attributeState "paused", label: 'Send Home', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821", nextState: "docking"
attributeState "resuming", label: 'Stop Clean', icon: "st.switches.switch.on", backgroundColor: "#79b821"
}
tileAttribute("device.headline", key: "SECONDARY_CONTROL") {
attributeState "default", label:'${currentValue}'
}
}
valueTile("DOCK", "device.status", width: 2, height: 2) {
state "docked", label: 'Docked', backgroundColor: "#79b821"
state "docking", label: 'Docking', backgroundColor: "#ffa81e"
state "starting", label: 'UnDocking', backgroundColor: "#ffa81e"
state "cleaning", label: 'Not on Dock', backgroundColor: "#ffffff", nextState: "docking", action: "dock"
state "pausing", label: 'Not on Dock', backgroundColor: "#ffffff", nextState: "docking", action: "dock"
state "paused", label: 'Dock', backgroundColor: "#ffffff", nextState: "docking", action: "dock"
state "bin-full", label: 'Bin full', backgroundColor: "#bc2323", nextState: "docking", action: "dock"
state "resuming", label: 'Not on Dock', backgroundColor: "#ffffff", defaultState: true, nextState: "docking", action: "dock"
}
valueTile("PAUSE", "device.status", width: 2, height: 2) {
state "docked", label: 'Pause', backgroundColor: "#ffffff", defaultState: true
state "docking", label: 'Pause', backgroundColor: "#ffffff"
state "starting", label: 'Pause', backgroundColor: "#ffffff", nextState: "pausing", action: "pause"
state "cleaning", label: 'Pause', backgroundColor: "#ffffff", nextState: "pausing", action: "pause"
state "pausing", label: 'Pausing..', backgroundColor: "#79b821"
state "paused", label: 'Paused', backgroundColor: "#79b821"
state "bin-full", label: 'Bin full', backgroundColor: "#bc2323"
state "resuming", label: 'Pause', backgroundColor: "#ffffff", nextState: "pausing", action: "pause"
}
valueTile("RESUME", "device.status", width: 2, height: 2) {
state "docked", label: 'Resume', backgroundColor: "#ffffff", defaultState: true
state "docking", label: 'Resume', backgroundColor: "#ffffff"
state "starting", label: 'Resume', backgroundColor: "#ffffff"
state "cleaning", label: 'Resume', backgroundColor: "#ffffff"
state "pausing", label: 'Resume', backgroundColor: "#79b821", nextState: "resuming", action: "switch.on"
state "paused", label: 'Resume', backgroundColor: "#ffffff", nextState: "resuming", action: "switch.on"
state "bin-full", label: 'Bin full', backgroundColor: "#bc2323"
state "resuming", label: 'Resuming..', backgroundColor: "#79b821"
}
standardTile("refresh", "device.status", width: 4, height: 2, decoration: "flat") {
state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
}
valueTile("battery", "device.battery", width: 2, height: 2, decoration: "flat") {
state "default", label:'Battery ${currentValue}%'
}
valueTile("job_count", "device.totalJobs", width: 3, height: 1, decoration: "flat") {
state "default", label:'Number of Cleaning Jobs:\n${currentValue} jobs'
}
valueTile("job_hr_count", "device.totalJobHrs", width: 3, height: 1, decoration: "flat") {
state "default", label:'Total Job Time:\n${currentValue} hours'
}
valueTile("current_job_time", "device.runtimeMins", width: 3, height: 1, decoration: "flat") {
state "default", label:'Current Job Runtime:\n${currentValue} minutes'
}
valueTile("current_job_sqft", "device.runtimeSqft", width: 3, height: 1, decoration: "flat") {
state "default", label:'Current Job Sqft:\n${currentValue} ft'
}
valueTile("current_job_time_estimated", "device.timeRemaining", width: 3, height: 1, decoration: "flat") {
state "default", label:'Estimated Completion Time:\n${currentValue} minutes'
}
valueTile("current_job_sqft_estimated", "device.sqftRemaining", width: 3, height: 1, decoration: "flat") {
state "default", label:'Estimated Sqft Remaining:\n${currentValue} ft'
}
main "CLEAN"
details(["STATUS",
"CLEAN", "DOCK", "PAUSE", "RESUME",
"refresh",
"battery",
"current_job_time", "current_job_time_estimated",
"current_job_sqft", "current_job_sqft_estimated",
"job_hr_count", "job_count"
])
}
//Settings updated
def updated() {
//log.debug "Updated settings ${settings}..
def interval
if (settings.pollInterval){
interval = settings.pollInterval
} else {
interval = 4
}
runIn(3, "updateDeviceNetworkID")
schedule("0 0/${interval} * * * ?", poll) // 4min polling is normal for irobots
//poll()
}
//Define custom capabilities
def setNewBinStatus(arg){
sendEvent(name: 'newBinStatus', value: arg)
}
//Installed
def installed() {
initialize()
}
//Configuration
def configure() {
log.debug "Configuring.."
poll()
}
//Initialize capabilities for new app UI display
def initialize() {
state.robotIpAddress = '0.0.0.0'
sendEvent(name: "DeviceWatch-DeviceStatus", value: "online")
//sendEvent(name: "healthStatus", value: "online")
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
sendEvent(name: 'switch', value: 'off')
sendEvent(name: 'robotCleanerMovement', value: 'idle')
sendEvent(name: 'newBinStatus', value: 'normal')
//sendEvent(name: 'robotCleanerCleaningMode', value: 'auto')
//sendEvent(name: 'robotCleanerTurboMode', value: 'off')
}
//Timed Session
def setTimeRemaining(timeNumber) {
log.debug "User requested setting the Time remaining to ${timeNumber}"
return
}
//Consumable
def setConsumableStatus(statusString) {
log.debug "User requested setting the Consumable Status - ${statusString}"
def status = device.latestValue("status")
log.debug "Setting value based on last roomba state - ${status}"
if(roomba_value == "bin-full") {
// Optionally this could be 'replace'?
state.consumable = "maintenance_required"
} else if(roomba_value == "error"){
state.consumable = "missing"
} else {
state.consumable = "good"
}
return state.consumable
}
//Refresh
def refresh() {
log.debug "Executing 'refresh'"
//return poll
return poll()
}
// pingを行うフロント関数
def getPingCommand() {
// 結局ホストは生きているのかフラグ
def pingState = false
// Rest980のステージ
state.pingTimeoutStage = 1
rest980Ping()
if (state.pingTimeoutStage != -1) {
// pingTimeout でセットされた失敗の値以外ならRoombaにPingする
state.pingTimeoutStage = 2
if (state.robotIpAddress == '0.0.0.0') {
// init state, roomba check skip.
pingState = true
} else {
roombaPing()
// pingTimeout でセットされた失敗の値以外ならping成功とする
if (state.pingTimeoutStage != -2) {
pingState = true
}
}
}
//mistake?
return state
}
// ルンバにpingする
// 雰囲気はここを参照
// https://github.com/c99koder/SmartThings/blob/master/smartapps/c99koder/computer-power-control.src/computer-power-control.groovy
def roombaPing() {
unschedule(pingTimeout)
runIn(30, pingTimeout)
def result = new physicalgraph.device.HubAction(
method: "GET",
path: "/",
headers: [
HOST: getRobotAddress()
]
)
}
// rest980にpingする
def rest980Ping() {
unschedule(pingTimeout)
runIn(30, pingTimeout)
def result = new physicalgraph.device.HubAction(
method: "GET",
path: "/",
headers: [
HOST: $roomba_host
]
)
}
// pingのタイムアウト時の処理
def pingTimeout() {
// roomba タイムアウト
if (state.pingTimeoutStage == 1) {
log.debug "rest980 timeout"
state.pingTimeoutStage = -1
}
// roomba タイムアウト
if (state.pingTimeoutStaget == 2) {
log.debug "roomba timeout"
state.pingTimeoutStage = -2
}
}
//Polling
def pollHistory() {
log.debug "Polling for missionHistory ----"
//sendEvent(name: "headline", value: "Polling history API", displayed: false)
state.RoombaCmd = "missionHistory"
return localAPI ? null : apiGet()
}
def poll() {
//Check robot and server connection status at each polling interval
//getPingCommand()
//if(pingState == true){
//Mark device as online in ST app
//sendEvent(name: "DeviceWatch-DeviceStatus", value: "online")
//log.debug "Marked device as online"
//Get historical data
pollHistory()
//Then poll for current status
log.debug "Polling for status ----"
//sendEvent(name: "headline", value: "Polling status API", displayed: false)
state.RoombaCmd = "getStatus"
return localAPI ? local_poll() : apiGet()
//} else {
//Mark device as offline
//sendEvent(name: "DeviceWatch-DeviceStatus", value: "offline")
//log.debug "Marked device as offline"
//}
}
def sendMsg(message){
def msg = message
//Non functioning, removed from preferences
if(sendPushMessage == "Yes") {
sendPush(msg)
}
//Non functioning, removed from prefernces
if(sendAudioMessage == "Yes"){
if(audioDevices){
audioDevices?.each { audioDevice ->
if (audioDevice.hasCommand("playText")) { //Check if speaker supports TTS
audioDevice.playText(msg)
} else {
if (audioDevice.hasCommand("speak")) { //Check if speaker supports speech synthesis
audioDevice.speak(msg.toString())
} else {
audioDevice.playTrack(textToSpeech(msg)?.uri) //All other speakers
}
}
}
}
}
}
//robotCleanerCleaningMode methods
def setRobotCleanerCleaningMode(mode){
if(mode == 'auto'){
//For debug only
sendEvent(name: 'robotCleanerCleaningMode', value: 'auto')
//TODO Set cleaningPasses auto
}
if(mode == 'part'){
//For debug only
sendEvent(name: 'robotCleanerCleaningMode', value: 'part')
}
if(mode == 'repeat'){
//For debug only
sendEvent(name: 'robotCleanerCleaningMode', value: 'repeat')
//TODO Set cleaningPasses two
}
if(mode == 'manual'){
//For debug only
sendEvent(name: 'robotCleanerCleaningMode', value: 'manual')
//TODO Set cleaningPasses one
}
if(mode == 'stop'){
//For debug only
sendEvent(name: 'robotCleanerCleaningMode', value: 'stop')
}
}
//robotCleanerTurboMode methods
def setRobotCleanerTurboMode(mode){
if(mode == 'on'){
//For debug only
sendEvent(name: 'robotCleanerTurboMode', value: 'on')
//TODO Set CarpetBoost 'Performance'
}
if(mode == 'off'){
//For debug only
sendEvent(name: 'robotCleanerTurboMode', value: 'off')
//TODO Set CarpetBoost 'Auto'
}
if(mode == 'silence'){
//For debug only
sendEvent(name: 'robotCleanerTurboMode', value: 'silence')
//TODO Set CarpetBoost 'Eco'
}
}
//robotCleanerMovement methods
def setRobotCleanerMovement(mode){
def status = device.latestValue("status")
if(mode == 'homing'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'homing')
}
if(mode == 'idle'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'idle')
return pause()
}
if(mode == 'charging'){
//For debug only
//sendEvent(name: 'robotCleanerMovement', value: 'charging' )
return off()
}
if(mode == 'alarm'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'alarm' )
}
if(mode == 'powerOff'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'powerOff' )
}
if(mode == 'reserve'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'reserve')
}
if(mode == 'point'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'point')
}
if(mode == 'after'){
//For debug only
sendEvent(name: 'robotCleanerMovement', value: 'after')
}
if(mode == 'cleaning'){
//For debug only
//sendEvent(name: 'robotCleanerMovement', value: 'cleaning')
return on()
}
}
// Switch methods
def on() {
// Always start roomba
def status = device.latestValue("status")
log.debug "On based on state - ${status}"
//For debug only
sendEvent(name: 'switch', value: 'on')
sendEvent(name: 'robotCleanerMovement', value: 'cleaning')
//if(status == "paused")
if(status == "pausing") {
return resume()
} else {
return start()
}
}
def off() {
// Always return to dock..
def status = device.latestValue("status")
log.debug "Off based on state - ${status}"
//For debug only
sendEvent(name: 'switch', value: 'off')
sendEvent(name: 'robotCleanerMovement', value: 'homing')
if(status == "paused") {
return dock()
} else {
return pauseAndDock()
}
}
// Timed Session
def start() {
sendEvent(name: "status", value: "starting")
state.RoombaCmd = "start"
runIn(15, poll)
return localAPI ? local_start() : apiGet()
}
def stop() {
sendEvent(name: "status", value: "stopping")
state.RoombaCmd = "stop"
runIn(15, poll)
return localAPI ? local_stop() : apiGet()
}
def pauseAndDock() {
sendEvent(name: "status", value: "pausing")
state.RoombaCmd = "pause"
return localAPI ? local_pauseAndDock() : apiGet()
}
def pause() {
sendEvent(name: "status", value: "pausing")
state.RoombaCmd = "pause"
runIn(15, poll)
return localAPI ? local_pause() : apiGet()
}
def cancel() {
return off()
}
// Actions
def dock() {
sendEvent(name: "status", value: "docking")
state.RoombaCmd = "dock"
runIn(15, poll)
return localAPI ? local_dock() : apiGet()
}
def resume() {
sendEvent(name: "status", value: "resuming")
state.RoombaCmd = "resume"
runIn(15, poll)
return localAPI ? local_resume() : apiGet()
}
// API methods
def parse(description) {
log.trace "GOT HERE"
def msg = parseLanMessage(description)
log.trace "GOT MSG $msg"
def headersAsString = msg.header // => headers as a string
def headerMap = msg.headers // => headers as a Map
def body = msg.body // => request body as a string
def status = msg.status // => http status code of the response
def json = msg.json // => any JSON included in response body, as a data structure of lists and maps
def xml = msg.xml // => any XML included in response body, as a document tree structure
def data = msg.data // => either JSON or XML in response body (whichever is specified by content-type header in response)
}
def apiGet() {
log.debug "apiget"
if (local) return
def request_query = ""
def request_host = ""
def encoded_str = "${roomba_username}:${roomba_password}".bytes.encodeBase64()
//Handle prefrences
if("${roomba_host}" == "" || "${roomba_host}" == "null") {
request_host = "https://irobot.axeda.com"
} else {
log.debug "Using Roomba Host: ${roomba_host}"
request_host = "${roomba_host}"
}
//Validation before calling the API
if(!roomba_username || !roomba_password) {
def new_status = "Username/Password not set. Configure required before using device."
sendEvent(name: "headline", value: new_status, displayed: false)
sendEvent(name: "preferences_set", value: "missing", displayed: false)
return
} else if(state.preferences_set != "missing") {
sendEvent(name: "preferences_set", value: "ready", displayed: false)
}
state.AssetID = "ElPaso@irobot!${roomba_username}"
state.Authorization = "${encoded_str}"
// Path (No changes required)
def request_path = "/services/v1/rest/Scripto/execute/AspenApiRequest"
// Query manipulation
if( state.RoombaCmd == "getStatus" || state.RoombaCmd == "accumulatedHistorical" || state.RoombaCmd == "missionHistory") {
request_query = "?blid=${roomba_username}&robotpwd=${roomba_password}&method=${state.RoombaCmd}"
} else {
request_query = "?blid=${roomba_username}&robotpwd=${roomba_password}&method=multipleFieldSet&value=%7B%0A%20%20%22remoteCommand%22%20:%20%22${state.RoombaCmd}%22%0A%7D"
}
def requestURI = "${request_host}${request_path}${request_query}"
def httpRequest = [
method:"GET",
uri: "${requestURI}",
headers: [
'User-Agent': 'aspen%20production/2618 CFNetwork/758.3.15 Darwin/15.4.0',
Accept: '*/*',
'Accept-Language': 'en-us',
'ASSET-ID': state.AssetID,
]
]
try {
httpGet(httpRequest) { resp ->
log.debug "response Headers:" + resp.headers.collect { "${it.name}:${it.value}" }
log.debug "response contentType: ${resp.contentType}"
log.debug "response data: ${resp.data}"
parseResponseByCmd(resp, state.RoombaCmd)
}
} catch (e) {
log.error "something went wrong: $e"
}
}
def parseResponseByCmd(resp, command) {
def data = resp.data
if(command == "getStatus") {
setStatus(data)
} else if(command == "accumulatedHistorical" ) {
/*readSummaryInfo -- same as getStatus but easier to parse*/
} else if(command == "missionHistory") {
setMissionHistory(data)
}
}
def convertDate(dateStr) {
return Date.parse("yyyyMMdd H:m", dateStr)
}
def setMissionHistory(data) {
def lastRuntime = -1
def lastSqft = -1
def lastDate = ""
def mstatus = data.status
def robot_history = data.missions
robot_history.sort{ convertDate(it.date) }.each{ mission ->
if(mission.done == 'ok') {
lastSqft = mission.sqft
lastRuntime = mission.runM
lastDate = mission.date
}
}
state.lastRuntime = lastRuntime
state.lastSqft = lastSqft
state.lastDate = lastDate
sendEvent(name: "lastRuntime", value: state.lastRuntime, displayed: false)
sendEvent(name: "lastSqft", value: state.lastSqft, displayed: false)
sendEvent(name: "lastDate", value: state.lastDate, displayed: false)
}
def setStatus(data) {
def rstatus = data.robot_status
def robotName = data.robotName
state.robotName = robotName
def mission = data.mission
def runstats = data.bbrun
def cschedule = data.cleanSchedule
def pmaint = data.preventativeMaintenance
def robot_status = new groovy.json.JsonSlurper().parseText(rstatus)
def robot_history = new groovy.json.JsonSlurper().parseText(mission)
def runtime_stats = new groovy.json.JsonSlurper().parseText(runstats)
def schedule = new groovy.json.JsonSlurper().parseText(cschedule)
def maintenance = new groovy.json.JsonSlurper().parseText(pmaint)
log.debug "Robot status = ${robot_status}"
log.debug "Robot history = ${robot_history}"
log.debug "Runtime stats= ${runtime_stats}"
log.debug "Robot schedule= ${schedule}"
log.debug "Robot maintenance= ${maintenance}"
def current_cycle = robot_status['cycle']
def current_charge = robot_status['batPct']
def current_phase = robot_status['phase']
def current_sqft = robot_status['sqft']
def num_mins_running = robot_status['mssnM']
def flags = robot_status['flags'] // Unknown what 'Flags' 0/1/2/5 mean?
def readyCode = robot_status['notReady']
def num_cleaning_jobs = robot_history['nMssn']
def num_dirt_detected = runtime_stats['nScrubs']
def total_job_time = runtime_stats['hr']
def new_status = get_robot_status(current_phase, current_cycle, current_charge, readyCode)
def roomba_value = get_robot_enum(current_phase, readyCode)
log.debug("Robot updates -- ${roomba_value} + ${new_status}")
//Set robotCleanerMovement state
if(roomba_value == "cleaning"){
state.robotCleanerMovement = "cleaning"
} else if(roomba_value == "paused"){
state.robotCleanerMovement = "idle"
} else if (roomba_value == "docked"){
state.robotCleanerMovement = "charging"
} else if (roomba_value == "docking"){
state.robotCleanerMovement = "homing"
} else if (roomba_value == "error"){
state.robotCleanerMovement = "alarm"
//} else if (roomba_value == "bin-full"){
//state.robotCleanerMovement = "alarm"
}
//Set the state object
if(roomba_value == "cleaning") {
state.switch = "on"
} else {
state.switch = "off"
}
/* Consumable state-changes */
if(roomba_value == "bin-full") {
state.consumable = "maintenance_required"
state.newBinStatus = "full"
} else if(roomba_value == "error"){
state.consumable = "missing"
} else {
state.consumable = "good"
state.newBinStatus = "normal"
}
/* Timed Session state-changes */
if(roomba_value == "cleaning") {
state.sessionStatus = "running"
} else if (roomba_value == "paused") {
state.sessionStatus = "paused"
} else if (roomba_value == "docked" || roomba_value == "docking") {
state.sessionStatus = "canceled"
} else {
state.sessionStatus = "stopped"
}
/* Misc. state-changes */
if(state.lastRuntime == -1) {
state.timeRemaining = -1
} else {
state.timeRemaining = state.lastRuntime - num_mins_running
}
if(state.lastSqft == -1) {
state.sqftRemaining = -1
} else {
state.sqftRemaining = state.lastSqft - current_sqft
}
/*send events, display final event*/
sendEvent(name: "robotName", value: robotName, displayed: false)
sendEvent(name: "runtimeMins", value: num_mins_running, displayed: false)
sendEvent(name: "runtimeSqft", value: current_sqft, displayed: false)
sendEvent(name: "timeRemaining", value: state.timeRemaining, displayed: false)
sendEvent(name: "sqftRemaining", value: state.sqftRemaining, displayed: false)
sendEvent(name: "totalJobHrs", value: total_job_time, displayed: false)
sendEvent(name: "totalJobs", value: num_cleaning_jobs, displayed: false)
sendEvent(name: "battery", value: current_charge, displayed: false)
sendEvent(name: "headline", value: new_status, displayed: false)
sendEvent(name: "status", value: roomba_value)
sendEvent(name: "switch", value: state.switch)
sendEvent(name: "sessionStatus", value: state.sessionStatus)
sendEvent(name: "consumable", value: state.consumable)
sendEvent(name: 'robotCleanerMovement', value: state.robotCleanerMovement)
sendEvent(name: 'newBinStatus', value: state.newBinStatus)
}
def get_robot_enum(current_phase, readyCode) {
def ROOMBA_STATES = getRoombaStates()
if(readyCode != ROOMBA_STATES['ready']) {
if(readyCode == ROOMBA_STATES['full']) {
return "bin-full"
} else if(readyCode != ROOMBA_STATES['dark']) {
return "error"
}
}
if(current_phase == "charge") {
return "docked"
} else if(current_phase == "hmUsrDock") {
return "docking"
} else if(current_phase == "pause" || current_phase == "stop") {
return "paused"
} else if(current_phase == "run") {
return "cleaning"
} else {
//"Stuck" phase falls into this category.
log.error "Unknown phase - Raw 'robot_status': ${status}. Add to 'get_robot_enum'"
return "error"
}
}
def parse_not_ready_status(readyCode) {
def robotName = state.robotName
def ROOMBA_STATES = getRoombaStates()
if(readyCode == ROOMBA_STATES['full']) {
return "${robotName}'s bin is full. Empty bin to continue."
sendMsg("${robotName}'s bin is full. Empty bin to continue.")
} else if(readyCode == ROOMBA_STATES['tilted']) {
return "${robotName} is not upright. Place robot on flat surface to continue."
sendMsg("${robotName} is not upright. Place robot on flat surface to continue.")
} else if (readyCode == ROOMBA_STATES['stuck']) {
return "${robotName} is stuck. Move robot to continue."
sendMsg("${robotName} is stuck. Move robot to continue.")
} else if (readyCode == ROOMBA_STATES['batterylow']) {
return "${robotName}'s battery is low. Please send Roomba to dock or place on dock."
sendMsg("${robotName}'s battery is low. Please send Roomba to dock or place on dock.")
} else if (readyCode == ROOMBA_STATES['debrisextractors']) {
return "${robotName}'s debris extractors are blocked. Please clear them."
sendMsg("${robotName}'s debris extractors are blocked. Please clear them.")
} else {
return "${robotName} returned notReady=${readyCode}. See iRobot app for details."
sendMsg("${robotName} returned notReady=${readyCode}. See iRobot app for details.")
}
}
def get_robot_status(current_phase, current_cycle, current_charge, readyCode) {
def robotName = state.robotName
def ROOMBA_STATES = getRoombaStates()
// 0 and 8 are "okay to run"
if(readyCode != ROOMBA_STATES['ready'] && readyCode != ROOMBA_STATES['dark']) {
return parse_not_ready_status(readyCode)
} else if(current_phase == "charge") {
if (current_charge == 100) {
return "${robotName} is Docked/Fully Charged"
} else {
return "${robotName} is Docked/Charging"
}
} else if(current_phase == "hmUsrDock") {
return "${robotName} is returning home"
} else if(current_phase == "run") {
return "${robotName} is cleaning (${current_cycle} cycle)"
} else if(current_phase == "pause" || current_phase == "stop") {
return "Paused - 'Dock' or 'Resume'?"
}
log.error "Unknown phase - ${current_phase}."
return "Error - refresh to continue. Code changes required if problem persists."
}
/* local REST gw support */
def lanEventHandler(evt) {
log.trace "GOT HERE"
def description = evt.description
def hub = evt?.hubId
def parsedEvent = parseLanMessage(description)
log.trace "RECEIVED LAN EVENT: $parsedEvent"
/*
//ping response
if (parsedEvent.data && parsedEvent.data.service && (parsedEvent.data.service == "hch")) {
def msg = parsedEvent.data
if (msg.result == "pong") {
//log in successful to local server
log.info "Successfully contacted local server"
atomicState.hchPong = true
}
}
*/
}
private local_get(path, cbk) {
def host = "$roomba_host:$roomba_port"
sendHubCommand(new physicalgraph.device.HubAction("""GET $path HTTP/1.1\r\nHOST: $host\r\n\r\n""", physicalgraph.device.Protocol.LAN, null, [callback: cbk]))
//if(hubResponse == null) {
//state.connection = "offline"
//} else {
//state.connection = "online"
//}
//sendEvent(name: "DeviceWatch-DeviceStatus", value: state.connection)
}
void local_dummy_cbk(physicalgraph.device.HubResponse hubResponse) {
}
void local_poll_cbk(physicalgraph.device.HubResponse hubResponse) {
//log.debug "hubResponse: ${hubResponse.class}"
//log.debug hubResponse.dump()
def status = hubResponse.status
//log.debug "http status: ${status}"
if(status == 200){
connected()
}
def data = hubResponse.json
def current_charge = data.batPct
def robotName = data.name
state.robotName = robotName
def mission = data.cleanMissionStatus
def current_cycle = mission.cycle
def current_phase = mission.phase
def current_sqft = mission.sqft
def num_mins_running = mission.mssnM
def readyCode = mission.notReady
def num_cleaning_jobs = mission.nMssn
def num_dirt_detected = data.bbrun.nScrubs
def total_job_time = data.bbrun.hr
def new_status = get_robot_status(current_phase, current_cycle, current_charge, readyCode)
def roomba_value = get_robot_enum(current_phase, readyCode)
log.debug("Robot updates -- 2 ${roomba_value} + ${new_status}")
//Set robotCleanerMovement state
if(roomba_value == "cleaning"){
state.robotCleanerMovement = "cleaning"
} else if(roomba_value == "paused"){
state.robotCleanerMovement = "idle"
} else if (roomba_value == "docked"){
state.robotCleanerMovement = "charging"
} else if (roomba_value == "docking"){
state.robotCleanerMovement = "homing"
} else if (roomba_value == "error"){
state.robotCleanerMovement = "alarm"
//} else if (roomba_value == "bin-full") {
//state.robotCleanerMovement = "alarm"
}
//TODO def carpet_boost = get state from api
//TODO Set state.robotCleanerTurboMode
//TODO def cleaning_passes = get state from api
//TODO Set state.robotCleanerCleaningMode
//Set the state object
if(roomba_value == "cleaning") {
state.switch = "on"
} else {
state.switch = "off"
}
/* Consumable state-changes */
if(roomba_value == "bin-full") {
state.consumable = "maintenance_required"
state.newBinStatus = "full"
} else if(roomba_value == "error"){
state.consumable = "missing"
} else {
state.consumable = "good"
state.newBinStatus = "normal"
}
/* Timed Session state-changes */
if(roomba_value == "cleaning") {
state.sessionStatus = "running"
} else if (roomba_value == "paused") {
state.sessionStatus = "paused"
} else if (roomba_value == "docked" || roomba_value == "docking") {
state.sessionStatus = "canceled"
} else {
state.sessionStatus = "stopped"
}
/* Misc. state-changes */
if(state.lastRuntime == -1) {
state.timeRemaining = -1
} else {
state.timeRemaining = (state.lastRuntime ?: num_mins_running) - num_mins_running
}
if(state.lastSqft == -1) {
state.sqftRemaining = -1
} else {
state.sqftRemaining = (state.lastSqft ?: current_sqft) - current_sqft
}
/*send events, display final event*/
sendEvent(name: "robotName", value: robotName, displayed: false)
sendEvent(name: "runtimeMins", value: num_mins_running, displayed: false)
sendEvent(name: "runtimeSqft", value: current_sqft, displayed: false)
sendEvent(name: "timeRemaining", value: state.timeRemaining, displayed: false)
sendEvent(name: "sqftRemaining", value: state.sqftRemaining, displayed: false)
sendEvent(name: "totalJobHrs", value: total_job_time, displayed: false)
sendEvent(name: "totalJobs", value: num_cleaning_jobs, displayed: false)
sendEvent(name: "battery", value: current_charge, displayed: false)
sendEvent(name: "headline", value: new_status, displayed: false)
sendEvent(name: "status", value: roomba_value)
sendEvent(name: "switch", value: state.switch)
sendEvent(name: "sessionStatus", value: state.sessionStatus)
sendEvent(name: "consumable", value: state.consumable)
sendEvent(name: "robotIpAddress", value: data.netinfo.addr)
sendEvent(name: 'robotCleanerMovement', value: state.robotCleanerMovement)
sendEvent(name: 'newBinStatus', value: state.newBinStatus)
//TODO sendEvent(name: 'robotCleanerTurboMode', value: state.robotCleanerTurboMode)
//TODO sendEvent(name: 'robotCleanerCleaningMode', value: state.robotCleanerCleaningMode)
}
//TODO private local_carpetBoost_auto
//TODO private local_carpetBoost_performance
//TODO private local_carpetBoost_eco
//TODO private local_cleaningPasses_auto
//TODO private local_cleaningPasses_one
//TODO private local_CleaningPasses_two
private local_poll() {
local_get('/api/local/config/preferences', 'local_poll_cbk')
runIn(30,timeout)