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 revision
Previous revision
useful_programs [2023/10/05 12:39] – [Debug printing] emozolyakuseful_programs [2024/03/19 09:39] (current) – [Detection of change of state] emozolyak
Line 397: Line 397:
     DEBUG(table.concat(arg, ' '))      DEBUG(table.concat(arg, ' ')) 
 end     end    
 +--[[ add extra spaces on both sides of string 
 +    @ s - string 
 +      n - number of spaces 
 +    --> string wrapped in spaces 
 +--]] 
 +function wrapToSpaces(s, n) 
 +    local spaces = string.rep(' ', math.floor(n / 2))
 +    return  spaces .. s 
 +            .. ((n % 2 == 0) and spaces or spaces .. ' ')
 +end 
 </code> </code>
 Then the print output can be enhanced like this: Then the print output can be enhanced like this:
Line 455: Line 465:
      
 end end
 +</code>
 +
 +<code lua>
 +-- converting utc timestamp in human readable string
 +function getDateString(utc)
 +    return os.date("%c", utc)
 +end 
 </code> </code>
  
Line 505: Line 522:
 ==== Simple TON timer ==== ==== Simple TON timer ====
 <code lua> <code lua>
--------------------------------- TON simple timer ----------------------------- +-- Timer constructor  
-TonClass = {} +Timer = {} ; setmetatable(Timer, {__call = function(self)  
-function TonClass :new() +                                       local obj = {stamp = os.time(), out = false
-    self.__index self  +                                       return setmetatable(obj{__index = self}
-    return setmetatable({stamp = os.time(), out = false,}, self)+                                   end }) 
 +-- Timer loop  
 +function Timer : run(in_delay)  
 +   local now = os.time()  
 +   if (not in_) then  
 +       self.out = false ; self.stamp = now  
 +   else  
 +       if (not self.out and (now - self.stamp>= delay) then  
 +           self.out = true  
 +       end  
 +   end  
 +   return self.out 
 end  end 
-  +function Timer reset() self.out = false ; self.stamp = os.time() end 
-function TonClass :__call(inputCondition, onDelay) +
-    local now = os.time() +
-    if (inputCondition ~= self.out) then  +
-        if (not inputCondition) then  +
-            self.out = false                               -- immediate off   +
-        else                 +
-            local timeDifference = now - self.stamp        -- check the time passed for the true condition  +
-            if (timeDifference < onDelay) then  +
-                DEBUG("going to sw on in " .. onDelay - timeDifference)   +
-                return self.out              +
-            else  +
-                self.out = true DEBUG("Switched on!"                       -- sw self on!   +
-            end  +
-        end  +
-    end  +
-  +
-    self.stamp = now  +
-    return self.out  +
-end -------------------------------- end of timer ------------------------------ +
- +
-function main (userId) +
-  local REQUIRED_DELAY = 15 -- seconds  +
-  if not TonTimer then  +
-      TonTimer = TonClass :new() +
-  end  +
-   +
-  DEBUG("ton state now = " .. tostring(TonTimer(true, REQUIRED_DELAY))) +
-   +
-end+
 </code> </code>
  
Line 661: Line 660:
    end     end 
        
 +end 
 +
 +</code>
 +
 +This one uses simper timer: 
 +<code lua>
 +include "timers"
 +CHAT_ID = 569335646
 +MSG_DELAY = 5 * 60 
 +
 +function main (userId)
 +  
 +  reportConnError(2, MSG_DELAY, CHAT_ID, 'CONN STUCK ON NODE https://node-name.webhmicloud.com')
 +  
 +end
 +
 +function reportConnError(connId, delay, chatId, msg)
 +    
 +    if (not tmr) then 
 +      tmr = Timer()
 +    end     
 +    
 +    if tmr : run(GetConnectionErrors(connId) > 0, delay) then 
 +        SendTelegramMessage(chatId, msg)    
 +        tmr : reset() 
 +    end 
 end  end 
  
Line 894: Line 919:
 Another way of creating objects:  Another way of creating objects: 
 <code lua> <code lua>
-ObjConstructor {} +TimerConstructor function ()  
-setmetatable(ObjConstructor, { +return setmetatable({}, { 
-             __call = function(self, delay)  -- init method  +        __call = function(self, delay)  
-                     local { +                local now os.time()  
-                     delay = delay   -- put init data here  +                if (not self.stamp or now - self.stamp >= delay) then  
-                    } +                    self.stamp = now  
-                     -- now may add methods to the prototype  +                    return true  
-                     return setmetatable(o, {__index = self})  +                else  
-                     end +                    return false  
-})+                end  
 +             end  
 +}) end   
  
-function ObjConstructor : oneShot() +-- usage   
-  + 
- local now = os.time() +function main (userId
-  +    --  creating timer    
- if (not self.stamp or (now - self.stamp >= self.delay)  then  +    if (not timer) then  
-     self.stamp now  +        timer TimerConstructor()
-     return true  +
-    else  +
-        return false +
     end      end 
          
 +    if timer (60) then 
 +        INFO('timer!')
 +        for i = 1, 8 do 
 +            toggle('synapse.do' .. i)
 +        end 
 +    end
 end end
  
-function main (userId+function toggle(v
-   +    W(v, 1 - R(v)) 
-  if (not tmr) then  +end 
-      local ONE_SHOT_DELAY = 10  +
-      tmr = ObjConstructor(ONE_SHOT_DELAY) +
-  end  +
-   +
-  DEBUG( tostring(tmr : oneShot() )  ) +
-   +
-end +
- +
 </code> </code>
  
Line 1493: 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  
 +                                              if cb then cb() end  
 +                                          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 )
 +    ------- Detecting changes of a set of registers ----------------
 +  local regSet = {{110, function() 
 +                            INFO("I'm a callback for reg 110")
 +                    end },
 +                   {1, function() 
 +                       INFO("I'm a callback for reg 1")
 +                    end }, 
 +                    {200, function() 
 +                        INFO("I'm a callback for reg 200")
 +                    end }, 
 +                 
 +   }
 +  
 +  local handlers = {} 
 +  
 +  -- registereing handlers 
 +  if (not handlers[regSet[1][1]]) then 
 +      for _, s in ipairs(regSet) do
 +          handlers[s[1]] = OnChange()
 +      end 
 +  end 
 +   
 +  -- using  handlers 
 +  for _, s in ipairs(regSet) do
 +      local reg, func   = s[1], s[2]
 +      handlers[reg](R(reg), func())    
 +  end 
 end end
 </code> </code>
Line 1564: Line 1585:
 INVALID_VALUE                = -100000 -- invalid value sign  INVALID_VALUE                = -100000 -- invalid value sign 
    
-filter = { {reg = 1, safeCopy = 2, errCount = 0},  -- what to filter  +filter = { {reg = 1, safeCopy = 2, errCount = 0},  -- reg - source register  
-           {reg = 3, safeCopy = 4, errCount = 0} } -- and where to store +           {reg = 3, safeCopy = 4, errCount = 0} } -- safeCopy - last read ok value 
    
 function main (userId) function main (userId)
useful_programs.1696509542.txt.gz · Last modified: 2023/10/05 12:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki