MI32 legacy: add mi32name, small fixes (#19619)

* add mi32name, small fixes

* remove unused variable
This commit is contained in:
Christian Baars 2023-09-29 17:47:17 +02:00 committed by GitHub
parent 540acdc638
commit f0b7984466
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 97 deletions

View File

@ -230,7 +230,7 @@ struct {
#endif //USE_ENERGY_SENSOR #endif //USE_ENERGY_SENSOR
#endif //USE_MI_EXT_GUI #endif //USE_MI_EXT_GUI
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
void *outlet_hap_service[4]; //arbitrary chosen void *outlet_hap_service[4]; //arbitrary chosen
int8_t HKconnectedControllers = 0; //should never be < 0 int8_t HKconnectedControllers = 0; //should never be < 0
uint8_t HKinfoMsg = 0; uint8_t HKinfoMsg = 0;
@ -249,8 +249,8 @@ struct mi_sensor_t{
uint8_t shallSendMQTT; uint8_t shallSendMQTT;
uint8_t MAC[6]; uint8_t MAC[6];
uint16_t PID; uint16_t PID;
uint8_t *key; uint8_t *key = nullptr;
uint32_t lastTimeSeen; char *name = nullptr;
union { union {
struct { struct {
uint32_t needsKey:1; uint32_t needsKey:1;
@ -331,7 +331,7 @@ struct mi_sensor_t{
union { union {
uint8_t bat; // many values seem to be hard-coded garbage (LYWSD0x, GCD1) uint8_t bat; // many values seem to be hard-coded garbage (LYWSD0x, GCD1)
}; };
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
//HAP handles //HAP handles
void *temp_hap_service; void *temp_hap_service;
void *hum_hap_service; void *hum_hap_service;
@ -350,9 +350,9 @@ struct mi_sensor_t{
#define D_CMND_MI32 "MI32" #define D_CMND_MI32 "MI32"
const char kMI32_Commands[] PROGMEM = D_CMND_MI32 "|Key|Cfg|Option"; const char kMI32_Commands[] PROGMEM = D_CMND_MI32 "|Key|Name|Cfg|Option";
void (*const MI32_Commands[])(void) PROGMEM = {&CmndMi32Key, &CmndMi32Cfg, &CmndMi32Option }; void (*const MI32_Commands[])(void) PROGMEM = {&CmndMi32Key, &CmndMi32Name,&CmndMi32Cfg, &CmndMi32Option };
#define UNKNOWN_MI 0 #define UNKNOWN_MI 0
#define FLORA 1 #define FLORA 1
@ -490,25 +490,23 @@ enum MI32_HKInfoMsg {
const char HTTP_BTN_MENU_MI32[] PROGMEM = "<p><form action='m32' method='get'><button>Mi Dashboard</button></form></p>"; const char HTTP_BTN_MENU_MI32[] PROGMEM = "<p><form action='m32' method='get'><button>Mi Dashboard</button></form></p>";
const char HTTP_MI32_SCRIPT_1[] PROGMEM = const char HTTP_MI32_SCRIPT_1[] PROGMEM =
"function setUp(){setInterval(countUp,1000); setInterval(update,100);}" "function setUp(){setInterval(countUp,1000); setInterval(update,1000);}"
"function countUp(){let ti=document.querySelectorAll('.Ti');" "function countUp(){let ti=document.querySelectorAll('.Ti');"
"for(const el of ti){var t=parseInt(el.innerText);el.innerText=t+1;}}" "for(const el of ti){var t=parseInt(el.innerText);el.innerText=t+1;}}"
"function update(){" //source, value "function update(){"
"var xr=new XMLHttpRequest();" "fetch('/m32?wi=1').then(r=>r.text())"
"xr.onreadystatechange=()=>{" ".then((r)=>{"
"if(xr.readyState==4&&xr.status==200){" // console.log(r); // optional
"var r = xr.response;" // new widget "if(r.length>0){"
"if(r.length>2000){return;};if(r.length==0){return;}" "var d=document.createElement('div');"
"var d = document.createElement('div');" "d.innerHTML=r.trim();"
"d.innerHTML = r.trim();" "var old=eb(d.firstChild.id);"
"var old = eb(d.firstChild.id);" "old.parentNode.replaceChild(d.firstChild, old);"
"old.parentNode.replaceChild(d.firstChild,old);" "}"
"};" "})"
"};" //".catch((e) => {console.error(e);});" //optional
"xr.open('GET','/m32?wi=1',true);" "};"
"xr.send();" ;
"};"
;
const char HTTP_MI32_STYLE[] PROGMEM = const char HTTP_MI32_STYLE[] PROGMEM =
"<style onload=setTimeout(setUp,500)>.parent {display: flex;flex-wrap: wrap;justify-content: center;}svg{float:inline-end;}" "<style onload=setTimeout(setUp,500)>.parent {display: flex;flex-wrap: wrap;justify-content: center;}svg{float:inline-end;}"
@ -525,7 +523,7 @@ const char HTTP_MI32_PARENT_START[] PROGMEM =
"<div class='box'><h2>MI32 Bridge</h2>" "<div class='box'><h2>MI32 Bridge</h2>"
"Observing <span id='numDev'>%u</span> devices<br>" "Observing <span id='numDev'>%u</span> devices<br>"
"Uptime: <span class='Ti'>%u</span> seconds<br>" "Uptime: <span class='Ti'>%u</span> seconds<br>"
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
"HomeKit setup code: %s<br>" "HomeKit setup code: %s<br>"
"HAP controller connections: %d<br>" "HAP controller connections: %d<br>"
#else #else
@ -565,4 +563,4 @@ const char HTTP_MI32_POWER_WIDGET[] PROGMEM =
#endif //USE_MI_EXT_GUI #endif //USE_MI_EXT_GUI
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
#endif //USE_MI_ESP32 #endif //USE_MI_ESP32

View File

@ -71,7 +71,7 @@
#if USE_MI_HOMEKIT==0 #if USE_MI_HOMEKIT==0
#undef USE_MI_HOMEKIT #undef USE_MI_HOMEKIT
#endif #endif
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
extern "C" void mi_homekit_main(void); extern "C" void mi_homekit_main(void);
extern "C" void mi_homekit_update_value(void* handle, float value, uint32_t type); extern "C" void mi_homekit_update_value(void* handle, float value, uint32_t type);
extern "C" void mi_homekit_stop(); extern "C" void mi_homekit_stop();
@ -497,7 +497,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.fert=1; _newSensor.feature.fert=1;
_newSensor.feature.lux=1; _newSensor.feature.lux=1;
_newSensor.feature.bat=1; _newSensor.feature.bat=1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.light_hap_service = nullptr; _newSensor.light_hap_service = nullptr;
#endif #endif
break; break;
@ -506,7 +506,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.motion=1; _newSensor.feature.motion=1;
_newSensor.feature.NMT=1; _newSensor.feature.NMT=1;
_newSensor.NMT=0; _newSensor.NMT=0;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.motion_hap_service = nullptr; _newSensor.motion_hap_service = nullptr;
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
break; break;
@ -517,7 +517,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.NMT=1; _newSensor.feature.NMT=1;
_newSensor.feature.lux=1; _newSensor.feature.lux=1;
_newSensor.feature.bat=1; _newSensor.feature.bat=1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.light_hap_service = nullptr; _newSensor.light_hap_service = nullptr;
_newSensor.motion_hap_service = nullptr; _newSensor.motion_hap_service = nullptr;
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
@ -531,7 +531,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.knob = 1; _newSensor.feature.knob = 1;
_newSensor.dimmer = 0; _newSensor.dimmer = 0;
} }
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.button_hap_service[0] = nullptr; _newSensor.button_hap_service[0] = nullptr;
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
break; break;
@ -539,7 +539,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.events=0x00; _newSensor.events=0x00;
_newSensor.feature.bat=1; _newSensor.feature.bat=1;
_newSensor.feature.door=1; _newSensor.feature.door=1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.door_sensor_hap_service = nullptr; _newSensor.door_sensor_hap_service = nullptr;
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
_newSensor.door = 255; _newSensor.door = 255;
@ -549,7 +549,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.bat=1; _newSensor.feature.bat=1;
_newSensor.feature.Btn=1; _newSensor.feature.Btn=1;
_newSensor.Btn=99; _newSensor.Btn=99;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.leak_hap_service = nullptr; _newSensor.leak_hap_service = nullptr;
_newSensor.bat_hap_service = nullptr; _newSensor.bat_hap_service = nullptr;
_newSensor.button_hap_service[0] = nullptr; _newSensor.button_hap_service[0] = nullptr;
@ -562,7 +562,7 @@ uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter)
_newSensor.feature.hum=1; _newSensor.feature.hum=1;
_newSensor.feature.tempHum=1; _newSensor.feature.tempHum=1;
_newSensor.feature.bat=1; _newSensor.feature.bat=1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
_newSensor.temp_hap_service = nullptr; _newSensor.temp_hap_service = nullptr;
_newSensor.hum_hap_service = nullptr; _newSensor.hum_hap_service = nullptr;
_newSensor.bat_hap_service = nullptr; _newSensor.bat_hap_service = nullptr;
@ -711,7 +711,7 @@ void MI32Init(void) {
if(MI32.mode.didGetConfig && !Settings->flag5.zigbee_hide_bridge_topic){ // borrow SO125 1 to turn off HomeKit if(MI32.mode.didGetConfig && !Settings->flag5.zigbee_hide_bridge_topic){ // borrow SO125 1 to turn off HomeKit
MI32.mode.didStartHAP = 0; MI32.mode.didStartHAP = 0;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
MI32getSetupCodeFromMAC(MI32.hk_setup_code); MI32getSetupCodeFromMAC(MI32.hk_setup_code);
AddLog(LOG_LEVEL_INFO,PSTR("M32: Init HAP core")); AddLog(LOG_LEVEL_INFO,PSTR("M32: Init HAP core"));
mi_homekit_main(); mi_homekit_main();
@ -910,6 +910,9 @@ extern "C" {
} }
char * MI32getDeviceName(uint32_t slot){ char * MI32getDeviceName(uint32_t slot){
if(MIBLEsensors[slot].name != nullptr){
return MIBLEsensors[slot].name;
}
static char _name[12]; static char _name[12];
if( MIBLEsensors[slot].type == UNKNOWN_MI){ if( MIBLEsensors[slot].type == UNKNOWN_MI){
snprintf_P(_name,8,PSTR("MI_%04X"),MIBLEsensors[slot].PID); snprintf_P(_name,8,PSTR("MI_%04X"),MIBLEsensors[slot].PID);
@ -924,7 +927,7 @@ extern "C" {
/*********************************************************************************************\ /*********************************************************************************************\
* Homekit section * Homekit section
\*********************************************************************************************/ \*********************************************************************************************/
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
extern "C" { extern "C" {
const char * MI32getSetupCode(){ const char * MI32getSetupCode(){
@ -1093,18 +1096,29 @@ void MI32loadCfg(){
} }
_val = _device[PSTR("key")]; _val = _device[PSTR("key")];
if (_val) { if (_val) {
mi_bindKey_t _keyMAC;
uint8_t *_key = (uint8_t*) malloc(16);
char *_keyStr = (char *)_val.getStr(); char *_keyStr = (char *)_val.getStr();
if(strlen(_keyStr)==0){ if(strlen(_keyStr)>0){
continue; if(strlen(_keyStr)==32){
uint8_t *_key = (uint8_t*) malloc(16);
MI32HexStringToBytes(_keyStr,_key);
MIBLEsensors[_numberOfDevices].key = _key;
}
else{
_error = true;
break;
}
} }
if(strlen(_keyStr)!=32){ }
_error = true; _val = _device[PSTR("name")];
break; if (_val) {
} char *_name = (char *)_val.getStr();
MI32HexStringToBytes(_keyStr,_key); MIBLEsensors[_numberOfDevices].name = new char[strlen(_name) + 1];
MIBLEsensors[_numberOfDevices].key = _key; strcpy(MIBLEsensors[_numberOfDevices].name, _name);
AddLog(LOG_LEVEL_INFO,PSTR("M32: found name: %s"), _name);
}
_val = _device[PSTR("feat")];
if (_val) {
MIBLEsensors[_numberOfDevices].feature.raw = _val.getUInt();
} }
} }
if(!_error){ if(!_error){
@ -1128,7 +1142,14 @@ void MI32saveConfig(){
if(_sensor.key != nullptr){ if(_sensor.key != nullptr){
ToHex_P(_sensor.key,16,_key,33); ToHex_P(_sensor.key,16,_key,33);
} }
uint32_t _inc = snprintf_P(_filebuf+_pos,200,PSTR("{\"MAC\":\"%s\",\"PID\":\"%04x\",\"key\":\"%s\"},"),_MAC,kMI32DeviceID[_sensor.type - 1],_key); char _name_feat[64];
if(_sensor.name != nullptr){
snprintf_P(_name_feat,64,PSTR(",\"name\":\"%s\",\"feat\":%u"),_sensor.name,_sensor.feature.raw);
}
else{
_name_feat[0] = 0;
}
uint32_t _inc = snprintf_P(_filebuf+_pos,200,PSTR("{\"MAC\":\"%s\",\"PID\":\"%04x\",\"key\":\"%s\"%s},"),_MAC,kMI32DeviceID[_sensor.type - 1],_key,_name_feat);
_pos += _inc; _pos += _inc;
} }
_filebuf[_pos-1] = ']'; _filebuf[_pos-1] = ']';
@ -1202,7 +1223,7 @@ void MI32ScanTask(void *pvParameters){
MI32Scan = NimBLEDevice::getScan(); MI32Scan = NimBLEDevice::getScan();
MI32Scan->setScanCallbacks(&MI32ScanCallbacks,false); MI32Scan->setScanCallbacks(&MI32ScanCallbacks,true);
if(NimBLEDevice::getWhiteListCount()>0){ if(NimBLEDevice::getWhiteListCount()>0){
MI32Scan->setFilterPolicy(BLE_HCI_SCAN_FILT_USE_WL); MI32Scan->setFilterPolicy(BLE_HCI_SCAN_FILT_USE_WL);
} }
@ -1630,7 +1651,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].longpress = _payload.Btn.value; MIBLEsensors[_slot].longpress = _payload.Btn.value;
MI32.mode.shallTriggerTele = 1; MI32.mode.shallTriggerTele = 1;
MIBLEsensors[_slot].eventType.longpress = 1; MIBLEsensors[_slot].eventType.longpress = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if((void**)MIBLEsensors[_slot].button_hap_service[0] != nullptr){ if((void**)MIBLEsensors[_slot].button_hap_service[0] != nullptr){
mi_homekit_update_value(MIBLEsensors[_slot].button_hap_service[0], (float)2.0f, 0x01); // only one button, long press = 2 mi_homekit_update_value(MIBLEsensors[_slot].button_hap_service[0], (float)2.0f, 0x01); // only one button, long press = 2
} }
@ -1647,7 +1668,7 @@ if(decryptRet!=0){
} }
MIBLEsensors[_slot].eventType.Btn = 1; MIBLEsensors[_slot].eventType.Btn = 1;
MI32.mode.shallTriggerTele = 1; MI32.mode.shallTriggerTele = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if(MIBLEsensors[_slot].Btn>5) break; // hard coded limit for now if(MIBLEsensors[_slot].Btn>5) break; // hard coded limit for now
if((void**)MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn] != nullptr){ if((void**)MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn] != nullptr){
// AddLog(LOG_LEVEL_DEBUG,PSTR("Send Button %u: SingleLong:%u, pointer: %x"), MIBLEsensors[_slot].Btn,_singleLong,MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn] ); // AddLog(LOG_LEVEL_DEBUG,PSTR("Send Button %u: SingleLong:%u, pointer: %x"), MIBLEsensors[_slot].Btn,_singleLong,MIBLEsensors[_slot].button_hap_service[MIBLEsensors[_slot].Btn] );
@ -1664,7 +1685,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.temp = 1; MIBLEsensors[_slot].eventType.temp = 1;
DEBUG_SENSOR_LOG(PSTR("Mode 4: temp updated")); DEBUG_SENSOR_LOG(PSTR("Mode 4: temp updated"));
} }
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, _tempFloat, 0x04); mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, _tempFloat, 0x04);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI #ifdef USE_MI_EXT_GUI
@ -1680,7 +1701,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.hum = 1; MIBLEsensors[_slot].eventType.hum = 1;
DEBUG_SENSOR_LOG(PSTR("Mode 6: hum updated")); DEBUG_SENSOR_LOG(PSTR("Mode 6: hum updated"));
} }
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, _tempFloat,0x06); mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, _tempFloat,0x06);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI #ifdef USE_MI_EXT_GUI
@ -1695,7 +1716,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.noMotion = 1; MIBLEsensors[_slot].eventType.noMotion = 1;
} }
MIBLEsensors[_slot].eventType.lux = 1; MIBLEsensors[_slot].eventType.lux = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].light_hap_service, (float)MIBLEsensors[_slot].lux,0x07); mi_homekit_update_value(MIBLEsensors[_slot].light_hap_service, (float)MIBLEsensors[_slot].lux,0x07);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI #ifdef USE_MI_EXT_GUI
@ -1728,7 +1749,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].bat = _payload.bat; MIBLEsensors[_slot].bat = _payload.bat;
MIBLEsensors[_slot].eventType.bat = 1; MIBLEsensors[_slot].eventType.bat = 1;
DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated")); DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated"));
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].bat_hap_service, (float)_payload.bat,0xa); mi_homekit_update_value(MIBLEsensors[_slot].bat_hap_service, (float)_payload.bat,0xa);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
} }
@ -1759,7 +1780,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.lux = 1; MIBLEsensors[_slot].eventType.lux = 1;
MIBLEsensors[_slot].NMT = 0; MIBLEsensors[_slot].NMT = 0;
MI32.mode.shallTriggerTele = 1; MI32.mode.shallTriggerTele = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].motion_hap_service, (float)1,0x0f); mi_homekit_update_value(MIBLEsensors[_slot].motion_hap_service, (float)1,0x0f);
mi_homekit_update_value(MIBLEsensors[_slot].light_hap_service, (float)_payload.lux,0x07); mi_homekit_update_value(MIBLEsensors[_slot].light_hap_service, (float)_payload.lux,0x07);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
@ -1773,7 +1794,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].leak = _payload.leak; MIBLEsensors[_slot].leak = _payload.leak;
MIBLEsensors[_slot].eventType.leak = 1; MIBLEsensors[_slot].eventType.leak = 1;
if(_payload.leak>0) MI32.mode.shallTriggerTele = 1; if(_payload.leak>0) MI32.mode.shallTriggerTele = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].leak_hap_service, (float)_payload.leak,0x14); mi_homekit_update_value(MIBLEsensors[_slot].leak_hap_service, (float)_payload.leak,0x14);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
break; break;
@ -1790,7 +1811,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].eventType.door = 1; MIBLEsensors[_slot].eventType.door = 1;
MIBLEsensors[_slot].events++; MIBLEsensors[_slot].events++;
MI32.mode.shallTriggerTele = 1; MI32.mode.shallTriggerTele = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].door_sensor_hap_service, (float)_payload.door,0x19); mi_homekit_update_value(MIBLEsensors[_slot].door_sensor_hap_service, (float)_payload.door,0x19);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
// AddLog(LOG_LEVEL_DEBUG,PSTR("Mode 19: %u"), _payload.door); // AddLog(LOG_LEVEL_DEBUG,PSTR("Mode 19: %u"), _payload.door);
@ -1802,7 +1823,7 @@ if(decryptRet!=0){
MIBLEsensors[_slot].events++; MIBLEsensors[_slot].events++;
MIBLEsensors[_slot].NMT = 0; MIBLEsensors[_slot].NMT = 0;
MI32.mode.shallTriggerTele = 1; MI32.mode.shallTriggerTele = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].motion_hap_service, (float)1,0x0f); mi_homekit_update_value(MIBLEsensors[_slot].motion_hap_service, (float)1,0x0f);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
} }
@ -1844,7 +1865,7 @@ void MI32ParseATCPacket(char * _buf, uint32_t length, uint8_t addr[6], int RSSI)
MIBLEsensors[_slot].eventType.tempHum = 1; MIBLEsensors[_slot].eventType.tempHum = 1;
MIBLEsensors[_slot].eventType.bat = 1; MIBLEsensors[_slot].eventType.bat = 1;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, MIBLEsensors.at(_slot).temp,0x04); mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, MIBLEsensors.at(_slot).temp,0x04);
mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, MIBLEsensors.at(_slot).hum,0x06); mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, MIBLEsensors.at(_slot).hum,0x06);
mi_homekit_update_value(MIBLEsensors[_slot].bat_hap_service, (float)MIBLEsensors.at(_slot).bat,0x0a); mi_homekit_update_value(MIBLEsensors[_slot].bat_hap_service, (float)MIBLEsensors.at(_slot).bat,0x0a);
@ -1877,7 +1898,7 @@ void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int RSSI
MIBLEsensors[_slot].temp = _tempFloat; MIBLEsensors[_slot].temp = _tempFloat;
MIBLEsensors[_slot].eventType.temp = 1; MIBLEsensors[_slot].eventType.temp = 1;
DEBUG_SENSOR_LOG(PSTR("CGD1: temp updated")); DEBUG_SENSOR_LOG(PSTR("CGD1: temp updated"));
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, _tempFloat,0x04); mi_homekit_update_value(MIBLEsensors[_slot].temp_hap_service, _tempFloat,0x04);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI #ifdef USE_MI_EXT_GUI
@ -1889,7 +1910,7 @@ void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int RSSI
MIBLEsensors[_slot].hum = _tempFloat; MIBLEsensors[_slot].hum = _tempFloat;
MIBLEsensors[_slot].eventType.hum = 1; MIBLEsensors[_slot].eventType.hum = 1;
DEBUG_SENSOR_LOG(PSTR("CGD1: hum updated")); DEBUG_SENSOR_LOG(PSTR("CGD1: hum updated"));
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, _tempFloat,0x06); mi_homekit_update_value(MIBLEsensors[_slot].hum_hap_service, _tempFloat,0x06);
#endif //USE_MI_HOMEKIT #endif //USE_MI_HOMEKIT
#ifdef USE_MI_EXT_GUI #ifdef USE_MI_EXT_GUI
@ -2008,7 +2029,7 @@ void MI32Every50mSecond(){
AddLog(LOG_LEVEL_DEBUG,PSTR("M32: %s"),_message); AddLog(LOG_LEVEL_DEBUG,PSTR("M32: %s"),_message);
MI32.infoMsg = 0; MI32.infoMsg = 0;
} }
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if(MI32.HKinfoMsg > 0){ if(MI32.HKinfoMsg > 0){
char _message[32]; char _message[32];
GetTextIndexed(_message, sizeof(_message), MI32.HKinfoMsg-1, kMI32_HKInfoMsg); GetTextIndexed(_message, sizeof(_message), MI32.HKinfoMsg-1, kMI32_HKInfoMsg);
@ -2025,7 +2046,7 @@ void MI32Every50mSecond(){
void MI32EverySecond(bool restart){ void MI32EverySecond(bool restart){
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if(TasmotaGlobal.devices_present>0){ if(TasmotaGlobal.devices_present>0){
for(uint32_t i=0;i<TasmotaGlobal.devices_present;i++){ for(uint32_t i=0;i<TasmotaGlobal.devices_present;i++){
power_t mask = 1 << i; power_t mask = 1 << i;
@ -2039,7 +2060,7 @@ void MI32EverySecond(bool restart){
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) { for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
if(MIBLEsensors[i].type==NLIGHT || MIBLEsensors[i].type==MJYD2S){ if(MIBLEsensors[i].type==NLIGHT || MIBLEsensors[i].type==MJYD2S){
MIBLEsensors[i].NMT++; MIBLEsensors[i].NMT++;
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if(MIBLEsensors[i].NMT > 20){ //TODO: Make a choosable timeout later if(MIBLEsensors[i].NMT > 20){ //TODO: Make a choosable timeout later
mi_homekit_update_value(MIBLEsensors[i].motion_hap_service,0.0f,0x0f); mi_homekit_update_value(MIBLEsensors[i].motion_hap_service,0.0f,0x0f);
} }
@ -2067,6 +2088,20 @@ void CmndMi32Key(void) {
} }
} }
void CmndMi32Name(void) {
if(MIBLEsensors[XdrvMailbox.index].name != nullptr){
delete []MIBLEsensors[XdrvMailbox.index].name;
}
if(XdrvMailbox.data_len==0){
MIBLEsensors[XdrvMailbox.index].name = nullptr;
}
else{
MIBLEsensors[XdrvMailbox.index].name = new char[XdrvMailbox.data_len + 1];
strcpy(MIBLEsensors[XdrvMailbox.index].name,XdrvMailbox.data);
}
ResponseCmndChar((const char*)MI32getDeviceName(XdrvMailbox.index));
}
void CmndMi32Cfg(void) { void CmndMi32Cfg(void) {
MI32saveConfig(); MI32saveConfig();
ResponseCmndDone(); ResponseCmndDone();
@ -2135,14 +2170,14 @@ bool MI32HandleWebGUIResponse(void){
char tmp[16]; char tmp[16];
WebGetArg(PSTR("wi"), tmp, sizeof(tmp)); WebGetArg(PSTR("wi"), tmp, sizeof(tmp));
if (strlen(tmp)) { if (strlen(tmp)) {
WSContentBegin(200, CT_PLAIN); WSContentBegin(200, CT_PLAIN);
if(MI32.widgetSlot==0) {WSContentEnd();return true;} if(MI32.widgetSlot!=0){
for(uint32_t i=0;i<32;i++){ for(uint32_t i=0;i<32;i++){
if(bitRead(MI32.widgetSlot,i)){ if(bitRead(MI32.widgetSlot,i)){
MI32sendWidget(i); MI32sendWidget(i);
WSContentEnd(); bitClear(MI32.widgetSlot,i);
bitClear(MI32.widgetSlot,i); break;
return true; }
} }
} }
WSContentEnd(); WSContentEnd();
@ -2315,13 +2350,12 @@ void MI32InitGUI(void){
MI32.widgetSlot=0; MI32.widgetSlot=0;
WSContentStart_P("m32"); WSContentStart_P("m32");
WSContentSend_P(HTTP_MI32_SCRIPT_1); WSContentSend_P(HTTP_MI32_SCRIPT_1);
// WSContentSend_P(HTTP_MI32_SCRIPT_1);
WSContentSendStyle(); WSContentSendStyle();
WSContentSend_P(HTTP_MI32_STYLE); WSContentSend_P(HTTP_MI32_STYLE);
WSContentSend_P(HTTP_MI32_STYLE_SVG,1,185,124,124,185,124,124); WSContentSend_P(HTTP_MI32_STYLE_SVG,1,185,124,124,185,124,124);
WSContentSend_P(HTTP_MI32_STYLE_SVG,2,151,190,216,151,190,216); WSContentSend_P(HTTP_MI32_STYLE_SVG,2,151,190,216,151,190,216);
WSContentSend_P(HTTP_MI32_STYLE_SVG,3,242,240,176,242,240,176); WSContentSend_P(HTTP_MI32_STYLE_SVG,3,242,240,176,242,240,176);
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
WSContentSend_P((HTTP_MI32_PARENT_START),MIBLEsensors.size(),UpTime(),MI32.hk_setup_code,MI32.HKconnectedControllers,ESP.getFreeHeap()/1024); WSContentSend_P((HTTP_MI32_PARENT_START),MIBLEsensors.size(),UpTime(),MI32.hk_setup_code,MI32.HKconnectedControllers,ESP.getFreeHeap()/1024);
#else #else
const char _setupCode[1] = {0}; const char _setupCode[1] = {0};
@ -2374,24 +2408,30 @@ void MI32ShowContinuation(bool *commaflg) {
void MI32Show(bool json) void MI32Show(bool json)
{ {
if (json) { if (json) {
if(!MI32.mode.triggeredTele){ if(MI32.mode.triggeredTele == 0){
if(MI32.option.noSummary) return; // no message at TELEPERIOD if(MI32.option.noSummary == 1) return; // no message at TELEPERIOD
} }
if(TasmotaGlobal.masterlog_level == LOG_LEVEL_DEBUG_MORE) return; // we want to announce sensors unlinked to the ESP, check for LOG_LEVEL_DEBUG_MORE is medium-safe if(TasmotaGlobal.masterlog_level == LOG_LEVEL_DEBUG_MORE) return; // we want to announce sensors unlinked to the ESP, check for LOG_LEVEL_DEBUG_MORE is medium-safe
MI32suspendScanTask(); MI32suspendScanTask();
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) { for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
if(MI32.mode.triggeredTele && MIBLEsensors[i].eventType.raw == 0) continue; if(MI32.mode.triggeredTele == 1 && MIBLEsensors[i].eventType.raw == 0) continue;
if(MI32.mode.triggeredTele && MIBLEsensors[i].shallSendMQTT==0) continue; if(MI32.mode.triggeredTele == 1 && MIBLEsensors[i].shallSendMQTT==0) continue;
bool commaflg = false; bool commaflg = false;
ResponseAppend_P(PSTR(",\"%s-%02x%02x%02x\":{"), if( MIBLEsensors[i].name == nullptr){
MI32getDeviceName(i), ResponseAppend_P(PSTR(",\"%s-%02x%02x%02x\":{"),
MIBLEsensors[i].MAC[3], MIBLEsensors[i].MAC[4], MIBLEsensors[i].MAC[5]); MI32getDeviceName(i),
MIBLEsensors[i].MAC[3], MIBLEsensors[i].MAC[4], MIBLEsensors[i].MAC[5]);
}
else{
ResponseAppend_P(PSTR(",\"%s\":{"),
MI32getDeviceName(i));
}
if((!MI32.mode.triggeredTele && !MI32.option.minimalSummary)||MI32.mode.triggeredTele){ if((MI32.mode.triggeredTele == 1 && MI32.option.minimalSummary == 0)||MI32.mode.triggeredTele == 1){
bool tempHumSended = false; bool tempHumSended = false;
if(MIBLEsensors[i].feature.tempHum){ if(MIBLEsensors[i].feature.tempHum){
if(MIBLEsensors[i].eventType.tempHum || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ if(MIBLEsensors[i].eventType.tempHum || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1){
if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)) { if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppendTHD(MIBLEsensors[i].temp, MIBLEsensors[i].hum); ResponseAppendTHD(MIBLEsensors[i].temp, MIBLEsensors[i].hum);
@ -2400,7 +2440,7 @@ void MI32Show(bool json)
} }
} }
if(MIBLEsensors[i].feature.temp && !tempHumSended){ if(MIBLEsensors[i].feature.temp && !tempHumSended){
if(MIBLEsensors[i].eventType.temp || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate) { if(MIBLEsensors[i].eventType.temp || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1) {
if (!isnan(MIBLEsensors[i].temp)) { if (!isnan(MIBLEsensors[i].temp)) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "\":%*_f"), ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "\":%*_f"),
@ -2409,7 +2449,7 @@ void MI32Show(bool json)
} }
} }
if(MIBLEsensors[i].feature.hum && !tempHumSended){ if(MIBLEsensors[i].feature.hum && !tempHumSended){
if(MIBLEsensors[i].eventType.hum || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate) { if(MIBLEsensors[i].eventType.hum || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1) {
if (!isnan(MIBLEsensors[i].hum)) { if (!isnan(MIBLEsensors[i].hum)) {
char hum[FLOATSZ]; char hum[FLOATSZ];
dtostrfd(MIBLEsensors[i].hum, Settings->flag2.humidity_resolution, hum); dtostrfd(MIBLEsensors[i].hum, Settings->flag2.humidity_resolution, hum);
@ -2419,7 +2459,7 @@ void MI32Show(bool json)
} }
} }
if (MIBLEsensors[i].feature.lux){ if (MIBLEsensors[i].feature.lux){
if(MIBLEsensors[i].eventType.lux || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ if(MIBLEsensors[i].eventType.lux || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1){
if ((MIBLEsensors[i].lux != 0x0ffffff)) { // this is the error code -> no lux if ((MIBLEsensors[i].lux != 0x0ffffff)) { // this is the error code -> no lux
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux); ResponseAppend_P(PSTR("\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux);
@ -2427,7 +2467,7 @@ void MI32Show(bool json)
} }
} }
if (MIBLEsensors[i].feature.moist){ if (MIBLEsensors[i].feature.moist){
if(MIBLEsensors[i].eventType.moist || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ if(MIBLEsensors[i].eventType.moist || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1){
if ((MIBLEsensors[i].moisture != 0xff)) { if ((MIBLEsensors[i].moisture != 0xff)) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"" D_JSON_MOISTURE "\":%u"), MIBLEsensors[i].moisture); ResponseAppend_P(PSTR("\"" D_JSON_MOISTURE "\":%u"), MIBLEsensors[i].moisture);
@ -2435,7 +2475,7 @@ void MI32Show(bool json)
} }
} }
if (MIBLEsensors[i].feature.fert){ if (MIBLEsensors[i].feature.fert){
if(MIBLEsensors[i].eventType.fert || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ if(MIBLEsensors[i].eventType.fert || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1){
if ((MIBLEsensors[i].fertility != 0xffff)) { if ((MIBLEsensors[i].fertility != 0xffff)) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"Fertility\":%u"), MIBLEsensors[i].fertility); ResponseAppend_P(PSTR("\"Fertility\":%u"), MIBLEsensors[i].fertility);
@ -2464,7 +2504,7 @@ void MI32Show(bool json)
} }
} // minimal summary } // minimal summary
if (MIBLEsensors[i].feature.motion){ if (MIBLEsensors[i].feature.motion){
if(MIBLEsensors[i].eventType.motion || !MI32.mode.triggeredTele){ if(MIBLEsensors[i].eventType.motion || MI32.mode.triggeredTele == 0){
if(MI32.mode.triggeredTele) { if(MI32.mode.triggeredTele) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"Motion\":1")); // only real-time ResponseAppend_P(PSTR("\"Motion\":1")); // only real-time
@ -2479,7 +2519,7 @@ void MI32Show(bool json)
} }
if (MIBLEsensors[i].feature.door){ if (MIBLEsensors[i].feature.door){
if(MIBLEsensors[i].eventType.door || !MI32.mode.triggeredTele){ if(MIBLEsensors[i].eventType.door || MI32.mode.triggeredTele == 0){
if(MI32.mode.triggeredTele) { if(MI32.mode.triggeredTele) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"Door\":%u"),MIBLEsensors[i].door); ResponseAppend_P(PSTR("\"Door\":%u"),MIBLEsensors[i].door);
@ -2489,21 +2529,21 @@ void MI32Show(bool json)
} }
} }
if (MIBLEsensors[i].type == FLORA && !MI32.mode.triggeredTele) { if (MIBLEsensors[i].type == FLORA && MI32.mode.triggeredTele == 0) {
if (MIBLEsensors[i].firmware[0] != '\0') { // this is the error code -> no firmware if (MIBLEsensors[i].firmware[0] != '\0') { // this is the error code -> no firmware
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"Firmware\":\"%s\""), MIBLEsensors[i].firmware); ResponseAppend_P(PSTR("\"Firmware\":\"%s\""), MIBLEsensors[i].firmware);
} }
} }
if (MIBLEsensors[i].feature.NMT || !MI32.mode.triggeredTele){ if (MIBLEsensors[i].feature.NMT || MI32.mode.triggeredTele == 0){
if(MIBLEsensors[i].eventType.NMT){ if(MIBLEsensors[i].eventType.NMT){
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"NMT\":%u"), MIBLEsensors[i].NMT); ResponseAppend_P(PSTR("\"NMT\":%u"), MIBLEsensors[i].NMT);
} }
} }
if (MIBLEsensors[i].feature.bat){ if (MIBLEsensors[i].feature.bat){
if(MIBLEsensors[i].eventType.bat || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){ if(MIBLEsensors[i].eventType.bat || MI32.mode.triggeredTele == 0 || MI32.option.allwaysAggregate == 1){
if ((MIBLEsensors[i].bat != 0x00)) { if ((MIBLEsensors[i].bat != 0x00)) {
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"Battery\":%u"), MIBLEsensors[i].bat); ResponseAppend_P(PSTR("\"Battery\":%u"), MIBLEsensors[i].bat);
@ -2511,8 +2551,8 @@ void MI32Show(bool json)
} }
} }
MI32ShowContinuation(&commaflg); MI32ShowContinuation(&commaflg);
ResponseAppend_P(PSTR("\"RSSI\":%d"), MIBLEsensors[i].RSSI); ResponseAppend_P(PSTR("\"RSSI\":%d,"), MIBLEsensors[i].RSSI);
ResponseAppend_P(PSTR("\"MAC\":\"%02X%02X%02X%02X%02X%02X\""),MIBLEsensors[i].MAC[0],MIBLEsensors[i].MAC[1],MIBLEsensors[i].MAC[2],MIBLEsensors[i].MAC[3],MIBLEsensors[i].MAC[4],MIBLEsensors[i].MAC[5]);
ResponseJsonEnd(); ResponseJsonEnd();
MIBLEsensors[i].eventType.raw = 0; MIBLEsensors[i].eventType.raw = 0;
@ -2534,7 +2574,7 @@ void MI32Show(bool json)
MI32suspendScanTask(); MI32suspendScanTask();
WSContentSend_P(HTTP_MI32, MIBLEsensors.size()); WSContentSend_P(HTTP_MI32, MIBLEsensors.size());
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if(MI32.mode.didStartHAP){ if(MI32.mode.didStartHAP){
WSContentSend_PD(PSTR("{s}HomeKit Code{m} %s{e}"),MI32.hk_setup_code); WSContentSend_PD(PSTR("{s}HomeKit Code{m} %s{e}"),MI32.hk_setup_code);
} }
@ -2602,7 +2642,7 @@ int ExtStopBLE(){
AddLog(LOG_LEVEL_INFO,PSTR("M32: stop BLE")); AddLog(LOG_LEVEL_INFO,PSTR("M32: stop BLE"));
while (MI32.mode.runningScan) yield(); while (MI32.mode.runningScan) yield();
} }
#ifdef USE_MI_HOMEKIT #if USE_MI_HOMEKIT==1
if(MI32.mode.didStartHAP) { if(MI32.mode.didStartHAP) {
AddLog(LOG_LEVEL_INFO,PSTR("M32: stop Homebridge")); AddLog(LOG_LEVEL_INFO,PSTR("M32: stop Homebridge"));
mi_homekit_stop(); mi_homekit_stop();
@ -2671,4 +2711,4 @@ bool Xsns62(uint32_t function)
#endif // USE_MI_ESP32 #endif // USE_MI_ESP32
#endif // CONFIG_IDF_TARGET_ESP32 or CONFIG_IDF_TARGET_ESP32C3 #endif // CONFIG_IDF_TARGET_ESP32 or CONFIG_IDF_TARGET_ESP32C3
#endif // ESP32 #endif // ESP32
#endif // USE_BLE_ESP32 #endif // USE_BLE_ESP32