misc_functions
Table of Contents
Miscellaneous functions
GetTime
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
Example:
- GetTime
function main (userId) local time = GetTime(); INFO("Seconds: " .. time.sec); INFO("Nanoseconds:" .. time.ns); end
Delay
- Delay
function main (userId) -- Uncomment this part to measure the time spent -- local start_time = GetTime(); -- Delay(100.25, GetTimePrecise()) -- Uncomment this part to measure the time spent -- local cur_time = GetTime(); local cur_sec, cur_ns = cur_time.sec, cur_time.ns -- local time_spent_sec, time_spent_ns = cur_sec - start_time.sec, cur_ns - start_time.ns -- local millseconds_spent = time_spent_sec * 1000 + time_spent_ns / 1000000 -- ERROR("Time spent ms = " .. millseconds_spent) -- end -- Additional functions can be defined after or before main() loop function GetTimePrecise() local start_time = GetTime(); return start_time.sec * 1000^3 + start_time.ns end function Delay (milliseconds, precise_start_time) 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)
- GetReg filtering
function main (userId) val1 = GetReg(1) or 0 -- if GetReg is nil, then it will be equal to 0 TRACE("val1 " ..tostring(val1)) val2 = GetReg("temperature") TRACE("val2 " ..tostring(val2)) val2_filtered = (val2 or 0) -- if val2 is nil, then it will be equal to 0 too. TRACE("val2_filtered " ..tostring(val2_filtered)) sum = val1 + val2_filtered TRACE(sum) end
IfInRangeHHMM
- IfInRangeHHMM
function main (userId) now = os.time() working_time = ifInRangeHHMM(now, 20,0,8,0) TRACE("working_time = " .. tostring(working_time)) if working_time then -- Add your code here end end function ifInRangeHHMM(now, start_hour, start_min, end_hour, end_min) -- проверяет текущее время на попадание в диапазон час минута out = false local cur_HH, cur_MM = tonumber(os.date("%H", now)), tonumber(os.date("%M", now)) -- TRACE("now = " .. os.date("%X %x", now) .. " " .. cur_HH .. ":" .. cur_MM) 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 = (cur_HH > start_hour) or (cur_HH == start_hour and cur_MM >= start_min) local before_end = (cur_HH < end_hour) or (cur_HH == end_hour and cur_MM <= end_min) -- TRACE("after_start =" .. tostring(after_start)) -- TRACE("before_end =" .. tostring(before_end)) out = after_start and before_end ; --TRACE("going to return " .. tostring(out)) else TRACE("ifInRangeHHMM: Range is midnight splitted") out = ifInRangeHHMM(now, start_hour, start_min, 23, 59) or ifInRangeHHMM(now, 0, 0, end_hour, end_min) end return out end
misc_functions.txt · Last modified: 2021/04/14 08:42 by atolstov