Skip to content
Permalink
Browse files

Add state.gesture to Xiaomi cube to simplify rules

Some gestures could only be handled by writing multiple rules for all
possible button events. The state.gesture is a companion for
state.buttonevent to reduce multiple button values into one gesture value.

TODO GESTURE_ROTATE_CLOCKWISE and GESTURE_ROTATE_COUNTER_CLOCKWISE.
  • Loading branch information
manup committed Jun 27, 2019
1 parent 24c7ff9 commit 1436df5dd31e59c50c4c5983dd5d350a22abf6a1
Showing with 47 additions and 7 deletions.
  1. +5 −0 database.cpp
  2. +32 −7 de_web_plugin.cpp
  3. +2 −0 resource.cpp
  4. +1 −0 resource.h
  5. +7 −0 sensor.h
@@ -2917,6 +2917,11 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c
}
item = sensor.addItem(DataTypeInt32, RStateButtonEvent);
item->setValue(0);

if (sensor.modelId().startsWith(QLatin1String("lumi.sensor_cube")))
{
sensor.addItem(DataTypeInt32, RStateGesture);
}
}
else if (sensor.type().endsWith(QLatin1String("LightLevel")))
{
@@ -4326,6 +4326,11 @@ void DeRestPluginPrivate::addSensorNode(const deCONZ::Node *node, const SensorFi
clusterId = MULTISTATE_INPUT_CLUSTER_ID;
}
sensorNode.addItem(DataTypeInt32, RStateButtonEvent);

if (modelId.startsWith(QLatin1String("lumi.sensor_cube")))
{
sensorNode.addItem(DataTypeInt32, RStateGesture);
}
}
else if (sensorNode.type().endsWith(QLatin1String("LightLevel")))
{
@@ -6196,6 +6201,7 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event)
}

qint32 buttonevent = -1;
qint32 gesture = -1; //
ResourceItem *item = i->item(RStateButtonEvent);
int rawValue = ia->numericValue().u16;

@@ -6205,13 +6211,17 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event)
static const int sideMap[] = {1, 3, 5, 6, 4, 2};
int side = sideMap[rawValue & 0x0007];
int previousSide = sideMap[(rawValue & 0x0038) >> 3];
if (rawValue == 0x0002) { buttonevent = 7000; } // wakeup
else if (rawValue == 0x0000) { buttonevent = 7007; } // shake
else if (rawValue == 0x0003) { buttonevent = 7008; } // drop
else if (rawValue & 0x0040) { buttonevent = side * 1000 + previousSide; } // flip 90°
else if (rawValue & 0x0080) { buttonevent = side * 1000 + 7 - side; } // flip 180°
else if (rawValue & 0x0100) { buttonevent = side * 1000; } // push
else if (rawValue & 0x0200) { buttonevent = side * 1000 + side; } // double tap
if (rawValue == 0x0002) { buttonevent = 7000; } // wakeup
else if (rawValue == 0x0000) { buttonevent = 7007; gesture = GESTURE_SHAKE; } // shake
else if (rawValue == 0x0003) { buttonevent = 7008; gesture = GESTURE_DROP; } // drop
else if (rawValue & 0x0040) { buttonevent = side * 1000 + previousSide; // flip 90°
gesture = GESTURE_FLIP_90; }
else if (rawValue & 0x0080) { buttonevent = side * 1000 + 7 - side; // flip 180°
gesture = GESTURE_FLIP_180; }
else if (rawValue & 0x0100) { buttonevent = side * 1000; // push
gesture = GESTURE_PUSH; }
else if (rawValue & 0x0200) { buttonevent = side * 1000 + side; // double tap
gesture = GESTURE_DOUBLE_TAP; }
}
else if (i->modelId() == QLatin1String("lumi.sensor_switch.aq3"))
{
@@ -6262,13 +6272,28 @@ void DeRestPluginPrivate::updateSensorNode(const deCONZ::NodeEvent &event)
}
}
}

if (item && buttonevent != -1)
{
item->setValue(buttonevent);
i->updateStateTimestamp();
i->setNeedSaveDatabase(true);
Event e(RSensors, RStateButtonEvent, i->id(), item);
enqueueEvent(e);
}

item = (gesture != -1) ? i->item(RStateGesture) : nullptr;
if (item && gesture != -1)
{
item->setValue(gesture);
i->updateStateTimestamp();
i->setNeedSaveDatabase(true);
Event e(RSensors, RStateGesture, i->id(), item);
enqueueEvent(e);
}

if (gesture != -1 || buttonevent != -1) // something was updated
{
enqueueEvent(Event(RSensors, RStateLastUpdated, i->id()));
}

@@ -51,6 +51,7 @@ const char *RStateDaylight = "state/daylight";
const char *RStateEffect = "state/effect";
const char *RStateFire = "state/fire";
const char *RStateFlag = "state/flag";
const char *RStateGesture = "state/gesture";
const char *RStateHue = "state/hue";
const char *RStateHumidity = "state/humidity";
const char *RStateLastUpdated = "state/lastupdated";
@@ -172,6 +173,7 @@ void initResourceDescriptors()
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeString, RStateEffect));
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeBool, RStateFire));
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeBool, RStateFlag));
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeInt32, RStateGesture));
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeUInt16, RStateHue));
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeUInt16, RStateHumidity, 0, 10000));
rItemDescriptors.emplace_back(ResourceItemDescriptor(DataTypeTime, RStateLastUpdated));
@@ -66,6 +66,7 @@ extern const char *RStateDaylight;
extern const char *RStateEffect;
extern const char *RStateFire;
extern const char *RStateFlag;
extern const char *RStateGesture;
extern const char *RStateHue;
extern const char *RStateHumidity;
extern const char *RStateLastUpdated;
@@ -31,6 +31,13 @@
#define S_BUTTON_ACTION_TILT 9
#define S_BUTTON_ACTION_MANY_PRESS 10

#define GESTURE_SHAKE 1
#define GESTURE_DROP 2
#define GESTURE_FLIP_90 3
#define GESTURE_FLIP_180 4
#define GESTURE_PUSH 5
#define GESTURE_DOUBLE_TAP 6

#define S_BUTTON_1 1000
#define S_BUTTON_2 2000
#define S_BUTTON_3 3000

0 comments on commit 1436df5

Please sign in to comment.
You can’t perform that action at this time.