forked from wnasich/nodemcu-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_round.lua
113 lines (91 loc) · 3.14 KB
/
read_round.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
print('read_round ...')
-- For comparing against captureDelta
local lastValues = {}
function doReadRound()
local value
-- Detect start of node
if (appStatus.lastRoundSlot == 0) then
addToDataQueue(cfg.readerId.nodeEvent, '"start"')
end
if (appStatus.lastRoundSlot > #readerSlots) then
appStatus.lastRoundSlot = 0
end
appStatus.lastRoundSlot = appStatus.lastRoundSlot + 1
-- Node heap
if (readerSlots[appStatus.lastRoundSlot] == cfg.readerId.nodeHeap) then
value = node.heap()
if (greaterThanDelta(cfg.readerId.nodeHeap, value)) then
addToDataQueue(cfg.readerId.nodeHeap, value)
end
-- Wifi signal
elseif (readerSlots[appStatus.lastRoundSlot] == cfg.readerId.wifiSignal) then
value = wifi.sta.getrssi()
if (value and greaterThanDelta(cfg.readerId.wifiSignal, value)) then
addToDataQueue(cfg.readerId.wifiSignal, value)
end
-- External temp and humidity
--elseif (readerSlots[appStatus.lastRoundSlot] == cfg.readerId.externalTemp) then
-- This reader gets cfg.readerId.externalHum also
-- local tempValue, humValue = readTempHum()
-- if (tempValue and greaterThanDelta(cfg.readerId.externalTemp, tempValue)) then
-- addToDataQueue(cfg.readerId.externalTemp, tempValue)
-- end
-- if (humValue and greaterThanDelta(cfg.readerId.externalHum, humValue)) then
-- addToDataQueue(cfg.readerId.externalHum, humValue)
-- end
-- Other sensor
--[[
elseif (readerSlots[appStatus.lastRoundSlot] == cfg.readerId.pressureHigh) then
value = readPressure(cfg.readerId.pressureHigh)
if (valud and greaterThanDelta(cfg.readerId.pressureHigh, value)) then
addToDataQueue(cfg.readerId.pressureHigh, value)
end
]]
end
end
function addToDataQueue(measurementId, value)
local dataItem, deltaTz
if (#dataQueue == 0) then
appStatus.baseTz = rtctime.get()
deltaTz = 0
else
deltaTz = rtctime.get() - appStatus.baseTz
end
dataItem = {deltaTz, measurementId, value}
print('tz: ' .. dataItem[1], 'Reader Id: ' .. dataItem[2], 'Value: ' .. dataItem[3])
table.insert(dataQueue, dataItemToString(dataItem))
-- When last heap lower than config value then save dataQueue to file
local lastNodeHeap = lastValues[cfg.readerId.nodeHeap]
if (lastNodeHeap and lastNodeHeap <= cfg.toFileWhenHeap) then
file.open(cfg.dataFileName, 'a+')
local itemsToCopy = #dataQueue
for i = 1, itemsToCopy do
dataItem = table.remove(dataQueue)
if (dataItem) then
dataItem = stringToDataItem(dataItem)
dataItem[1] = appStatus.baseTz + dataItem[1]
file.writeline(dataItemToString(dataItem))
end
end
print('Items added to storage: ' .. itemsToCopy)
file.close()
appStatus.dataFileExists = true
end
end
function greaterThanDelta(readerId, currentValue)
local isGreater = (
lastValues[readerId] == nil or
math.abs(lastValues[readerId] - currentValue) > captureDelta[readerId]
)
if (isGreater) then
lastValues[readerId] = currentValue
end
return isGreater
end
tmr.register(
timerAllocation.readRound,
cfg.readRoundInterval,
tmr.ALARM_AUTO,
doReadRound
)
tmr.start(timerAllocation.readRound)