From 2ad758117d18e7a91bd0706020f4aaac9602589d Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Thu, 16 Jul 2020 17:15:12 +0200 Subject: [PATCH] scripter add event result --- lib/LinkedList-1.2.3/LinkedList.h | 98 ++++++++++++++++++++++++++++++- tasmota/xdrv_10_scripter.ino | 26 ++++++-- 2 files changed, 118 insertions(+), 6 deletions(-) mode change 100644 => 100755 lib/LinkedList-1.2.3/LinkedList.h diff --git a/lib/LinkedList-1.2.3/LinkedList.h b/lib/LinkedList-1.2.3/LinkedList.h old mode 100644 new mode 100755 index 371b14ac7..64321cf01 --- a/lib/LinkedList-1.2.3/LinkedList.h +++ b/lib/LinkedList-1.2.3/LinkedList.h @@ -39,8 +39,11 @@ protected: ListNode* getNode(int index); + ListNode* findEndOfSortedString(ListNode *p, int (*cmp)(T &, T &)); + public: LinkedList(); + LinkedList(int sizeIndex, T _t); //initiate list size and default value ~LinkedList(); /* @@ -65,7 +68,6 @@ public: virtual bool unshift(T); /* Set the object at index, with T; - Increment _size; */ virtual bool set(int index, T); /* @@ -94,6 +96,16 @@ public: */ virtual void clear(); + /* + Sort the list, given a comparison function + */ + virtual void sort(int (*cmp)(T &, T &)); + + // add support to array brakets [] operator + inline T& operator[](int index); + inline T& operator[](size_t& i) { return this->get(i); } + inline const T& operator[](const size_t& i) const { return this->get(i); } + }; // Initialize LinkedList with false values @@ -157,7 +169,7 @@ ListNode* LinkedList::getNode(int index){ return current; } - return false; + return NULL; } template @@ -165,6 +177,13 @@ int LinkedList::size(){ return _size; } +template +LinkedList::LinkedList(int sizeIndex, T _t){ + for (int i = 0; i < sizeIndex; i++){ + add(_t); + } +} + template bool LinkedList::add(int index, T _t){ @@ -226,6 +245,12 @@ bool LinkedList::unshift(T _t){ return true; } + +template +T& LinkedList::operator[](int index) { + return getNode(index)->data; +} + template bool LinkedList::set(int index, T _t){ // Check if index position is in bounds @@ -322,4 +347,73 @@ void LinkedList::clear(){ shift(); } +template +void LinkedList::sort(int (*cmp)(T &, T &)){ + if(_size < 2) return; // trivial case; + + for(;;) { + + ListNode **joinPoint = &root; + + while(*joinPoint) { + ListNode *a = *joinPoint; + ListNode *a_end = findEndOfSortedString(a, cmp); + + if(!a_end->next ) { + if(joinPoint == &root) { + last = a_end; + isCached = false; + return; + } + else { + break; + } + } + + ListNode *b = a_end->next; + ListNode *b_end = findEndOfSortedString(b, cmp); + + ListNode *tail = b_end->next; + + a_end->next = NULL; + b_end->next = NULL; + + while(a && b) { + if(cmp(a->data, b->data) <= 0) { + *joinPoint = a; + joinPoint = &a->next; + a = a->next; + } + else { + *joinPoint = b; + joinPoint = &b->next; + b = b->next; + } + } + + if(a) { + *joinPoint = a; + while(a->next) a = a->next; + a->next = tail; + joinPoint = &a->next; + } + else { + *joinPoint = b; + while(b->next) b = b->next; + b->next = tail; + joinPoint = &b->next; + } + } + } +} + +template +ListNode* LinkedList::findEndOfSortedString(ListNode *p, int (*cmp)(T &, T &)) { + while(p->next && cmp(p->data, p->next->data) <= 0) { + p = p->next; + } + + return p; +} + #endif diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index 06e482a4d..b885feadf 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -194,7 +194,7 @@ void LoadFile(const char *name,uint8_t *buf,uint32_t len) { #define EPOCH_OFFSET 1546300800 enum {OPER_EQU=1,OPER_PLS,OPER_MIN,OPER_MUL,OPER_DIV,OPER_PLSEQU,OPER_MINEQU,OPER_MULEQU,OPER_DIVEQU,OPER_EQUEQU,OPER_NOTEQU,OPER_GRTEQU,OPER_LOWEQU,OPER_GRT,OPER_LOW,OPER_PERC,OPER_XOR,OPER_AND,OPER_OR,OPER_ANDEQU,OPER_OREQU,OPER_XOREQU,OPER_PERCEQU}; -enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD}; +enum {SCRIPT_LOGLEVEL=1,SCRIPT_TELEPERIOD,SCRIPT_EVENT_HANDLED}; #ifdef USE_SCRIPT_FATFS @@ -379,6 +379,7 @@ struct SCRIPT_MEM { #endif } glob_script_mem; +bool event_handeled = false; #ifdef USE_SCRIPT_GLOBVARS @@ -1573,6 +1574,11 @@ chknext: fvar=UtcTime()-(uint32_t)EPOCH_OFFSET; goto exit; } + if (!strncmp(vname,"eres",4)) { + fvar=event_handeled; + tind->index=SCRIPT_EVENT_HANDLED; + goto exit_settable; + } #ifdef USE_ENERGY_SENSOR if (!strncmp(vname,"enrg[",5)) { lp+=5; @@ -3901,6 +3907,9 @@ int16_t Run_script_sub(const char *type, int8_t tlen, JsonObject *jo) { if (*dfvar>300) *dfvar=300; Settings.tele_period=*dfvar; break; + case SCRIPT_EVENT_HANDLED: + event_handeled=*dfvar; + break; } sysv_type=0; } @@ -4063,7 +4072,9 @@ void ScripterEvery100ms(void) { Run_Scripter(">T",2, mqtt_data); } } - if (fast_script==99) Run_Scripter(">F",2,0); + if (Settings.rule_enabled) { + if (fast_script==99) Run_Scripter(">F",2,0); + } } //mems[5] is 50 bytes in 6.5 @@ -6619,6 +6630,7 @@ void cpy2lf(char *dst,uint32_t dstlen, char *src) { bool Xdrv10(uint8_t function) { bool result = false; + event_handeled = false; char *sprt; switch (function) { @@ -6778,11 +6790,17 @@ bool Xdrv10(uint8_t function) #ifdef SCRIPT_POWER_SECTION if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">P",2,0); #else - if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">E",2,0); + if (bitRead(Settings.rule_enabled, 0)) { + Run_Scripter(">E",2,0); + result=event_handeled; + } #endif break; case FUNC_RULES_PROCESS: - if (bitRead(Settings.rule_enabled, 0)) Run_Scripter(">E",2,mqtt_data); + if (bitRead(Settings.rule_enabled, 0)) { + Run_Scripter(">E",2,mqtt_data); + result=event_handeled; + } break; #ifdef USE_WEBSERVER case FUNC_WEB_ADD_BUTTON: