User Tools

Site Tools


useful_programs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
useful_programs [2024/02/26 09:06] – [Filters] emozolyakuseful_programs [2024/03/19 09:24] – [Detection of change of state] emozolyak
Line 1515: Line 1515:
 ===== Detection of change of state ===== ===== Detection of change of state =====
  
-Sometimes you may need take actions upon changing any of the registers in a set. The following script's idea is to use one function to track changes of the respective registers, looking at their prevous value stored in a global structure (tablewhich is keeping between script execution+Sometimes you may need take actions upon changing any of the registers in a set. The following function returns true at the moment (scanthe value differs from previous value or false if no changes. Also, it can call callback function to make your code more readable.
  
 <code lua> <code lua>
-include "lib.lib"+OnChange = {} ; setmetatable(OnChange, {__call = function(self)  
 +                                    return function(v, cb)  
 +                                          local out = false  
 +                                          if (not self.prev) then 
 +                                              self.prev = v  
 +                                          end  
 +                                          if (self.prev and v ~= self.prev ) then  
 +                                              out = true  
 +                                              cb() 
 +                                          end  
 +                                          self.prev = v  
 +                                          return out  
 +                                    end  
 +end  
 +})
  
 function main (userId) function main (userId)
------------------------- INIT (create globals) ---------------------   +  if (not onchange1) then  
-  if (not startedFlag) then   +      onchange1 OnChange()
-  +
-        detect = { curMinute = {regId = 1, prevValue = 0},  -- register's context  +
-                   stateReg  = {regId = 2, prevValue = 0}   -- which registes to track  +
-                 } +
-        mt = {__call = function(self, row)                  -- one method can detect as many registes as set in  +
-                           local me = self[row]             -- the detect table above  +
-                           local curValue = R(me.regId) +
- +
-                           if (not curValue) then return false end  +
-                            +
-                           local result  +
-                           if (not startedFlag) then  +
-                               me.prevValue = curValue ; result = false  +
-                           else  +
-                               if (R(me.regId) ~= me.prevValue) then  +
-                                   DEBUG("change of state detected for " .. row) +
-                                   me.prevValue = curValue ; result = true  +
-                               else  +
-                                   me.prevValue = curValue ; result =  false  +
-                               end  +
-                            end  +
-                            return result +
-                   end +
-           } +
-      setmetatable(detect, mt) +
-      prevAlerts #GetCurrentAlerts()   +
-      startedFlag = true  -- avoid this block further+
   end    end 
- ----------------------- MAIN ----------------------- 
-  
-     local alerts = GetCurrentAlerts()  
-     local alertCount = #alerts 
-      
-     if (alertCount > prevAlerts) or detect("curMinute") or detect("stateReg") then -- run on any of changes  
-         -- YOUR CODE FOR ACTIONS MIGHT BE HERE --  
-         local jsonToSend = cjson.encode({stateRegister = R(2), activeAlerts = (#alerts > 0) and 1 or 0}) 
-         AddInfoMessage(jsonToSend) 
-          
-         W(4, jsonToSend) -- to external register  
-         W(6, jsonToSend) -- to custom protocol 
-          
-     end  
-     
-     prevAlerts = alertCount 
      
 +  onchange1(R(110), function() INFO("true!") end )
 end end
 </code> </code>
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki