Finally got my side up + gesture events from my Xiaomi cube
My .item is:
Number Pink1CubeButtonEvent { http="<[MiCubePink1GestureCache:500:JSONPATH($.state.buttonevent)]" }
Rule so far (just logging):
rule "Xiaomi Cube action"
when
Item Pink1CubeButtonLastUpdated changed
then
Thread::sleep(20)
var actionName = Pink1CubeButtonEvent.state
var name = "Cube P1"
logInfo(name, "Event: " + actionName)
/*
switch(actionName) {
case "MOVE": {
<ACTION>
}
case "ROTATE_RIGHT": {
<ACTION>
}
case "ROTATE_LEFT": {
<ACTION>
}
case "FLIP90": {
<ACTION>
}
case "FLIP180": {
<ACTION>
}
case "TAP_TWICE": {
<ACTION>
}
case "SHAKE_AIR": {
<ACTION>
}
case "FREE_FALL": {
<ACTION>
}
case "ALERT": {
<ACTION>
}
}
*/
end
The commented out part is what used to be the parsing of the gestures from the Xiaomi Gateway. That did not contain any info about what face of the cube/dice was up.
Now, the numbers received is coded like this:
x000 for push, so 1000, 2000, 3000, 4000, 5000, 6000;
x00x for double tap, so 1001, 2002, 3003, 4004, 5005, 6006;
x00o for 180° flip (where o = 7 - x), so 1006, 2005, 3004, 4003, 5002, 6001;
x00y for 90° flip (where y ≠x and y ≠7 - x), so 1002, 1003, 1004, 1005, 2001, 2003, 2004, 2006, 3001, 3002, 3005, 3006, 4001, 4002, 4005, 4006, 5001, 5003, 5004, 5006, 6002, 6003, 6004, 6005;
7007 for shake.
7008 for free-fall/drop
7000 for wakeup (the cube sends raw value 2 when I just tap it after a while).
Snippet from.
My log looks like this:
2018-06-09 23:57:45.128 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 7000
2018-06-09 23:57:48.219 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 6003
2018-06-09 23:57:51.339 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 2006
2018-06-09 23:57:54.357 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 7007
2018-06-09 23:58:02.505 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 2000
2018-06-09 23:59:36.117 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 5002
2018-06-10 00:01:05.775 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 5005
7000 = wake up (Xiaomi named it ALARM)
6003 = face 6 is up, face was previously up - flip90
2006 = face 2 is up, 6 was up - flip90
7007 = shake
2000 = moved, while face 2 is up
5002 = face 5 is up, 2 was up - flip180
5005 = double tap, face 5 up
What would be the most efficient way to parse these values in the rule?
Possible with a switch(), case construct or a bunch of ifs?
Any suggestions most welcome.