useful_programs
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
useful_programs [2023/10/26 07:28] – [Generating alert messages with time - delay] emozolyak | useful_programs [2024/03/19 09:39] (current) – [Detection of change of state] emozolyak | ||
---|---|---|---|
Line 465: | Line 465: | ||
| | ||
end | end | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- converting utc timestamp in human readable string | ||
+ | function getDateString(utc) | ||
+ | return os.date(" | ||
+ | end | ||
</ | </ | ||
Line 912: | Line 919: | ||
Another way of creating objects: | Another way of creating objects: | ||
<code lua> | <code lua> | ||
- | ObjConstructor | + | TimerConstructor |
- | setmetatable(ObjConstructor, { | + | return |
- | __call = function(self, | + | __call = function(self, |
- | local o = { | + | local now = os.time() |
- | | + | if (not self.stamp or now - self.stamp >= delay) then |
- | } | + | |
- | | + | return |
- | return | + | else |
- | end | + | return false |
- | }) | + | end |
+ | end | ||
+ | }) end | ||
- | function ObjConstructor : oneShot() | + | -- usage |
- | + | ||
- | local now = os.time() | + | function main (userId) |
- | + | | |
- | if (not self.stamp or (now - self.stamp >= self.delay) ) | + | if (not timer) then |
- | self.stamp | + | |
- | return true | + | |
- | else | + | |
- | return false | + | |
end | end | ||
| | ||
+ | if timer (60) then | ||
+ | INFO(' | ||
+ | for i = 1, 8 do | ||
+ | toggle(' | ||
+ | end | ||
+ | end | ||
end | end | ||
- | function | + | function |
- | + | 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 | + | |
- | + | ||
</ | </ | ||
Line 1511: | 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 | + | Sometimes you may need take actions upon changing any of the registers in a set. The following function |
<code lua> | <code lua> | ||
- | include "lib.lib" | + | OnChange = {} ; setmetatable(OnChange, |
+ | 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 | + | |
- | + | ||
- | detect = { curMinute = {regId = 1, prevValue = 0}, -- register' | + | |
- | | + | |
- | } | + | |
- | mt = {__call = function(self, | + | |
- | local me = self[row] | + | |
- | local curValue = R(me.regId) | + | |
- | + | ||
- | if (not curValue) then return false end | + | |
- | + | ||
- | local result | + | |
- | if (not startedFlag) then | + | |
- | | + | |
- | else | + | |
- | if (R(me.regId) ~= me.prevValue) then | + | |
- | | + | |
- | | + | |
- | else | + | |
- | | + | |
- | end | + | |
- | end | + | |
- | return result | + | |
- | end | + | |
- | } | + | |
- | setmetatable(detect, | + | |
- | | + | |
- | startedFlag = true -- avoid this block further | + | |
end | end | ||
- | | ||
- | |||
- | local alerts = GetCurrentAlerts() | ||
- | local alertCount = #alerts | ||
- | |||
- | if (alertCount > prevAlerts) or detect(" | ||
- | -- YOUR CODE FOR ACTIONS MIGHT BE HERE -- | ||
- | local jsonToSend = cjson.encode({stateRegister = R(2), activeAlerts = (#alerts > 0) and 1 or 0}) | ||
- | | ||
- | |||
- | W(4, jsonToSend) -- to external register | ||
- | W(6, jsonToSend) -- to custom protocol | ||
- | |||
- | | ||
- | | ||
- | | ||
| | ||
+ | onchange1(R(110), | ||
+ | ------- Detecting changes of a set of registers ---------------- | ||
+ | local regSet = {{110, function() | ||
+ | INFO(" | ||
+ | end }, | ||
+ | {1, function() | ||
+ | | ||
+ | end }, | ||
+ | {200, function() | ||
+ | INFO(" | ||
+ | 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), | ||
+ | end | ||
end | end | ||
</ | </ | ||
Line 1582: | Line 1585: | ||
INVALID_VALUE | INVALID_VALUE | ||
- | 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.1698305327.txt.gz · Last modified: 2023/10/26 07:28 by emozolyak