Skip to content

Commit

Permalink
SwitchBot Blind Tilt Status decoder (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
DigiH authored Apr 1, 2024
1 parent f3cb71e commit b13baf8
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Theengs Decoder library translates these data chains into human readable data le

It's also a simplified way of defining the thing properties and how to decode these.

Theengs Decoder supports the decoding of [more than 90 Bluetooth devices](https://decoder.theengs.io/devices/devices.html).
Theengs Decoder supports the decoding of [more than 100 Bluetooth devices](https://decoder.theengs.io/devices/devices.html).

Theengs Decoder can be used on memory constraint environments like micro controllers (ESP32, ESP8266, Arduino), on a PC or on a Unix server.
Unit testing assures that the modification done on the library doesn't affect previous capabilities.
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Theengs Decoder library translates these data chains into human readable data le

It's also a simplified way of defining the thing properties and how to decode these.

Theengs Decoder supports the decoding of [more than 90 Bluetooth devices](https://decoder.theengs.io/devices/devices.html).
Theengs Decoder supports the decoding of [more than 100 Bluetooth devices](https://decoder.theengs.io/devices/devices.html).

Theengs Decoder can be used on memory constraint environments like micro controllers (ESP32, ESP8266, Arduino), on a PC or on a Unix server.
Unit testing assures that the modification done on the library doesn't affect previous capabilities.
Expand Down
12 changes: 12 additions & 0 deletions docs/devices/SBBT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SwitchBot Blind Tilt

|Model Id|[W270160X](https://github.com/theengs/decoder/blob/development/src/devices/SBBT_json.h)|
|-|-|
|Brand|SwitchBot|
|Model|W270160X|
|Short Description|Venetian blind tilting actor|
|Communication|BLE broadcast|
|Frequency|2.4Ghz|
|Power source|Solar panel|
|Exchanged data|open, direction, motion, calibrated, light level, battery|
|Encrypted|No|
2 changes: 1 addition & 1 deletion docs/devices/devices_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ <h4>
</header>
<div class="container">
<p>Discover the extensive catalog of Bluetooth devices compatible with Theengs ecosystem, including our MQTT Gateway, Web Parser,
and OpenMQTTGateway. BLE trackers from <b>Tile</b>, <b>Nut</b>, <b>TagIt</b>, <b>iTag</b>, <b>Gigaset G-Tag</b> and the <b>TicWatch GTH (Pro)</b> will also be recognised and published as device trackers. The table also indicates whether each device has been confirmed to work with our [mobile application](https://app.theengs.io/).</p>
and OpenMQTTGateway. BLE trackers from <b>Tile</b>, <b>Nut</b>, <b>TagIt</b>, <b>iTAG</b>, <b>Gigaset G-Tag</b> and the <b>TicWatch GTH (Pro)</b> will also be recognised and published as device trackers. The table also indicates whether each device has been confirmed to work with our [mobile application](https://app.theengs.io/).</p>
{table_content_to_replace}
<div class="custom-block warning"><p class="custom-block-title">Note</p> <p>All product and company names are trademarks or registered trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by or of them.</p></div>
</div>
Expand Down
16 changes: 15 additions & 1 deletion src/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ int TheengsDecoder::decodeBLEJson(JsonObject& jsondata) {
/* use a double for all values and cast later if required */
double temp_val;
static double cal_val = 0;
std::string proc_str = "";

if (data_index_is_valid(src, decoder[2].as<int>(), decoder[3].as<int>())) {
decoder_function dec_fun = &TheengsDecoder::value_from_hex_string;
Expand Down Expand Up @@ -751,7 +752,15 @@ int TheengsDecoder::decodeBLEJson(JsonObject& jsondata) {
}
} else if (strncmp(post_proc[i].as<const char*>(), "abs", 3) == 0) {
temp_val = abs(temp_val);
}
} else if (strncmp(post_proc[i].as<const char*>(), "SBBT-dir", 8) == 0) { // "SBBT" decoder specific post_proc
if (temp_val < 0) {
proc_str = "down";
} else if (temp_val > 0) {
proc_str = "up";
} else {
proc_str = "";
}
}
}
}
}
Expand All @@ -776,6 +785,11 @@ int TheengsDecoder::decodeBLEJson(JsonObject& jsondata) {
jsondata[_key] = temp_val;
}

/* _key as string if proc_str != "" */
if (proc_str != "") {
jsondata[_key] = proc_str;
}

/* If the property is temp in C, make sure to convert and add temp in F */
if (_key.find("tempc", 0, 5) != std::string::npos) {
double tc = jsondata[_key];
Expand Down
1 change: 1 addition & 0 deletions src/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class TheengsDecoder {
INODEEM,
RUUVITAG_RAWV1,
RUUVITAG_RAWV2,
SBBT,
SBCS,
SBCU,
SBMS,
Expand Down
2 changes: 2 additions & 0 deletions src/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "devices/RDL52832_json.h"
#include "devices/RuuviTag_RAWv1_json.h"
#include "devices/RuuviTag_RAWv2_json.h"
#include "devices/SBBT_json.h"
#include "devices/SBCS_json.h"
#include "devices/SBCU_json.h"
#include "devices/SBMS_json.h"
Expand Down Expand Up @@ -164,6 +165,7 @@ const char* _devices[][2] = {
{_iNodeEM_json, _iNodeEM_json_props},
{_RuuviTag_RAWv1_json, _RuuviTag_RAWv1_json_props},
{_RuuviTag_RAWv2_json, _RuuviTag_RAWv2_json_props},
{_SBBT_json, _SBBT_json_props},
{_SBCS_json, _SBCS_json_props},
{_SBCU_json, _SBCU_json_props},
{_SBMS_json, _SBMS_json_props},
Expand Down
70 changes: 70 additions & 0 deletions src/devices/SBBT_json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const char* _SBBT_json = "{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"tag\":\"0d02\",\"condition\":[\"uuid\",\"index\",0,\"0d00\",\"|\",\"uuid\",\"index\",0,\"fd3d\",\"&\",\"servicedata\",\"=\",6,\"index\",0,\"78\",\"&\",\"manufacturerdata\",\"=\",24,\"index\",0,\"6909\"],\"properties\":{\"open\":{\"decoder\":[\"value_from_hex_data\",\"manufacturerdata\",20,2,false,false],\"post_proc\":[\"&\",127,\"-\",50,\"*\",2,\"±\",100,\"abs\"]},\"direction\":{\"decoder\":[\"value_from_hex_data\",\"manufacturerdata\",20,2,false,false],\"post_proc\":[\"&\",127,\"-\",50,\"*\",2,\"SBBT-dir\"]},\"motion\":{\"decoder\":[\"bit_static_value\",\"manufacturerdata\",20,3,false,true]},\"calibrated\":{\"decoder\":[\"bit_static_value\",\"manufacturerdata\",19,0,false,true]},\"lightlevel\":{\"decoder\":[\"value_from_hex_data\",\"manufacturerdata\",18,1,false,false]},\"batt\":{\"decoder\":[\"value_from_hex_data\",\"servicedata\",4,2,false,false],\"post_proc\":[\"&\",127]},\"mac\":{\"decoder\":[\"mac_from_hex_data\",\"manufacturerdata\",4]}}}";
/*R""""(
{
"brand":"SwitchBot",
"model":"Blind Tilt",
"model_id":"W270160X",
"tag":"0d02",
"condition":["uuid", "index", 0, "0d00", "|", "uuid", "index", 0, "fd3d", "&", "servicedata", "=", 6, "index", 0, "78", "&", "manufacturerdata", "=", 24, "index", 0, "6909"],
"properties":{
"open":{
"decoder":["value_from_hex_data", "manufacturerdata", 20, 2, false, false],
"post_proc":["&", 127, "-", 50, "*", 2, "±", 100, "abs"]
},
"direction":{
"decoder":["value_from_hex_data", "manufacturerdata", 20, 2, false, false],
"post_proc":["&", 127, "-", 50, "*", 2, "SBBT-dir"]
},
"motion":{
"decoder":["bit_static_value", "manufacturerdata", 20, 3, false, true]
},
"calibrated":{
"decoder":["bit_static_value", "manufacturerdata", 19, 0, false, true]
},
"lightlevel":{
"decoder":["value_from_hex_data", "manufacturerdata", 18, 1, false, false]
},
"batt":{
"decoder":["value_from_hex_data", "servicedata", 4, 2, false, false],
"post_proc":["&", 127]
},
"mac":{
"decoder":["mac_from_hex_data", "manufacturerdata", 4]
}
}
})"""";*/

const char* _SBBT_json_props = "{\"properties\":{\"open\":{\"unit\":\"%\",\"name\":\"open\"},\"direction\":{\"unit\":\"string\",\"name\":\"direction\"},\"motion\":{\"unit\":\"status\",\"name\":\"motion\"},\"calibrated\":{\"unit\":\"status\",\"name\":\"calibrated\"},\"lightlevel\":{\"unit\":\"int\",\"name\":\"light level\"},\"batt\":{\"unit\":\"%\",\"name\":\"battery\"},\"mac\":{\"unit\":\"string\",\"name\":\"MAC address\"}}}";
/*R""""(
{
"properties":{
"open":{
"unit":"%",
"name":"open"
},
"direction":{
"unit":"string",
"name":"direction"
},
"motion":{
"unit":"status",
"name":"motion"
},
"calibrated":{
"unit":"status",
"name":"calibrated"
},
"lightlevel":{
"unit":"int",
"name":"light level"
},
"batt":{
"unit":"%",
"name":"battery"
},
"mac":{
"unit":"string",
"name":"MAC address"
}
}
})"""";*/
24 changes: 24 additions & 0 deletions tests/BLE/test_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ const char* expected_name_uuid_mfgsvcdata[] = {
"{\"brand\":\"Sensor Easy\",\"model\":\"SE RHT\",\"model_id\":\"SE_RHT\",\"type\":\"THB\",\"hum\":47,\"volt\":3.097}",
"{\"brand\":\"Sensor Easy\",\"model\":\"SE TEMP PROBE\",\"model_id\":\"SE_TPROBE\",\"type\":\"THB\",\"tempc\":20.6,\"tempf\":69.08,\"volt\":3.044}",
"{\"brand\":\"Sensor Easy\",\"model\":\"SE MAG\",\"model_id\":\"SE_MAG\",\"type\":\"CTMO\",\"cont\":true,\"open\":false,\"volt\":3.079}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":30,\"direction\":\"up\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":60,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":0,\"direction\":\"up\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":72,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":100,\"direction\":\"\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":54,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":0,\"direction\":\"down\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":54,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":46,\"direction\":\"up\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":54,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":54,\"direction\":\"up\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":54,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":80,\"direction\":\"down\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":96,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
"{\"brand\":\"SwitchBot\",\"model\":\"Blind Tilt\",\"model_id\":\"W270160X\",\"type\":\"WCVR\",\"acts\":true,\"open\":80,\"direction\":\"up\",\"motion\":false,\"calibrated\":true,\"lightlevel\":2,\"batt\":96,\"mac\":\"AA:BB:CC:DD:EE:FF\"}",
};

const char* expected_name_mac_uuid_mfgsvcdata[] = {
Expand Down Expand Up @@ -715,6 +723,14 @@ const char* test_name_uuid_mfgsvcdata[][5] = {
{"SE RHT volt","P RHT 88888B","0x2a6f","5707f2190c","2f"},
{"SE TEMP PROBE","P TPROBE 000000","0x2a6e","5707f2e40b","0c08"},
{"SE MAG","P MAG CCCCCC","0x2a06","5707f2070c","3b00"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeff0d275514", "78003c"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffd5256414", "780048"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffd8253214", "780036"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffcf270014", "780036"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffe0254d14", "780036"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffd3274914", "780036"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffd3272814", "780060"},
{"Switchbot_BlindTilt", "WoBlindTilt", "0xfd3d", "6909aabbccddeeffd3273c14", "780060"},
};

TheengsDecoder::BLE_ID_NUM test_name_uuid_mfgsvcdata_id_num[]{
Expand All @@ -731,6 +747,14 @@ TheengsDecoder::BLE_ID_NUM test_name_uuid_mfgsvcdata_id_num[]{
TheengsDecoder::BLE_ID_NUM::SE_RHT,
TheengsDecoder::BLE_ID_NUM::SE_TPROBE,
TheengsDecoder::BLE_ID_NUM::SE_MAG,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
TheengsDecoder::BLE_ID_NUM::SBBT,
};

// uuid test input [test name] [mac] [device name] [uuid] [manufacturer data] [service data]
Expand Down

0 comments on commit b13baf8

Please sign in to comment.