misc_functions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
misc_functions [2019/05/14 10:28] – akuzmuk | misc_functions [2021/04/14 08:42] (current) – atolstov | ||
---|---|---|---|
Line 2: | Line 2: | ||
==== GetTime ==== | ==== GetTime ==== | ||
- | The GetTime function returns table with local time information. **sec** field contains seconds in Unixtime format. **ns** field contains nanoseconds passed since current second. | + | The GetTime function returns table with local time information. **sec** field contains seconds in Unixtime format. **ns** field contains nanoseconds passed since begin of current second. |
This function is available since WebHMI version 3.6 | This function is available since WebHMI version 3.6 | ||
Example: | Example: | ||
- | <code lua> | + | <code lua GetTime> |
function main (userId) | function main (userId) | ||
local time = GetTime(); | local time = GetTime(); | ||
Line 13: | Line 13: | ||
INFO(" | INFO(" | ||
end | end | ||
+ | </ | ||
+ | |||
+ | ==== Delay ==== | ||
+ | |||
+ | <code lua Delay> | ||
+ | function main (userId) | ||
+ | | ||
+ | -- Uncomment this part to measure the time spent | ||
+ | -- local start_time = GetTime(); | ||
+ | -- | ||
+ | Delay(100.25, | ||
+ | | ||
+ | | ||
+ | -- Uncomment this part to measure the time spent | ||
+ | -- local cur_time = GetTime(); | ||
+ | -- local time_spent_sec, | ||
+ | -- local millseconds_spent = time_spent_sec * 1000 + time_spent_ns / 1000000 | ||
+ | -- ERROR(" | ||
+ | -- | ||
+ | | ||
+ | end | ||
+ | -- Additional functions can be defined after or before main() loop | ||
+ | function GetTimePrecise() | ||
+ | local start_time = GetTime(); | ||
+ | return | ||
+ | end | ||
+ | |||
+ | function Delay (milliseconds, | ||
+ | repeat | ||
+ | delay_ns = milliseconds * 1000000 | ||
+ | local cur_time = GetTime(); | ||
+ | time_over = GetTimePrecise() > (precise_start_time + delay_ns) | ||
+ | until time_over | ||
+ | end | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== GetReg filter nil (if nil then zero) ==== | ||
+ | <code lua GetReg filtering> | ||
+ | function main (userId) | ||
+ | | ||
+ | val1 = GetReg(1) or 0 -- if GetReg is nil, then it will be equal to 0 | ||
+ | TRACE(" | ||
+ | val2 = GetReg(" | ||
+ | TRACE(" | ||
+ | | ||
+ | |||
+ | |||
+ | val2_filtered = (val2 or 0) -- if val2 is nil, then it will be equal to 0 too. | ||
+ | TRACE(" | ||
+ | sum = val1 + val2_filtered | ||
+ | |||
+ | TRACE(sum) | ||
+ | | ||
+ | end | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | ==== IfInRangeHHMM ==== | ||
+ | <code lua IfInRangeHHMM> | ||
+ | function main (userId) | ||
+ | now = os.time() | ||
+ | | ||
+ | | ||
+ | working_time = ifInRangeHHMM(now, | ||
+ | | ||
+ | | ||
+ | TRACE(" | ||
+ | if working_time then | ||
+ | -- Add your code here | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function ifInRangeHHMM(now, | ||
+ | -- проверяет текущее время на попадание в диапазон час минута | ||
+ | out = false | ||
+ | local cur_HH, cur_MM = tonumber(os.date(" | ||
+ | tonumber(os.date(" | ||
+ | |||
+ | -- TRACE(" | ||
+ | not_splitted_midnight_shift = start_hour < end_hour or start_hour == end_hour and start_min < end_min | ||
+ | if not_splitted_midnight_shift then | ||
+ | |||
+ | local after_start | ||
+ | local before_end = | ||
+ | -- TRACE(" | ||
+ | -- TRACE(" | ||
+ | | ||
+ | out = after_start and before_end | ||
+ | else | ||
+ | TRACE(" | ||
+ | out = ifInRangeHHMM(now, | ||
+ | end | ||
+ | | ||
+ | | ||
+ | return out | ||
+ | |||
+ | end | ||
+ | |||
+ | |||
</ | </ |
misc_functions.1557829696.txt.gz · Last modified: 2019/05/14 10:28 by akuzmuk