-
Notifications
You must be signed in to change notification settings - Fork 3
/
ohs_th_sensor.h
96 lines (88 loc) · 3.52 KB
/
ohs_th_sensor.h
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
/*
* ohs_th_sensor.h
*
* Created on: 22. 2. 2020
* Author: adam
*/
#ifndef OHS_TH_SENSOR_H_
#define OHS_TH_SENSOR_H_
#ifndef SENSOR_DEBUG
#define SENSOR_DEBUG 0
#endif
#if SENSOR_DEBUG
#define DBG_SENSOR(...) {chprintf(console, __VA_ARGS__);}
#else
#define DBG_SENSOR(...)
#endif
/*
* Sensor thread
*/
static THD_WORKING_AREA(waSensorThread, 320);
static THD_FUNCTION(SensorThread, arg) {
chRegSetThreadName(arg);
msg_t msg;
sensorEvent_t *inMsg;
triggerEvent_t *outMsgTrig;
uint8_t nodeIndex;
uint8_t lastNode = DUMMY_NO_VALUE;
uint32_t lastNodeTime = 0;
time_t timeNow;
while (true) {
msg = chMBFetchTimeout(&sensor_mb, (msg_t*)&inMsg, TIME_INFINITE);
if (msg == MSG_OK) {
// Get current time
timeNow = getTimeUnixSec();
// Lookup sensor
nodeIndex = getNodeIndex(inMsg->address, 'S', inMsg->function, inMsg->number);
if (nodeIndex != DUMMY_NO_VALUE) {
DBG_SENSOR("Sensor data for node: %u:%c:%u\r\n", inMsg->address, inMsg->function, inMsg->number);
// node enabled
if (GET_NODE_ENABLED(node[nodeIndex].setting)) {
node[nodeIndex].value = inMsg->value;
node[nodeIndex].lastOK = timeNow; // Get timestamp
// MQTT
if (GET_NODE_MQTT(node[nodeIndex].setting)) pushToMqtt(typeSensor, nodeIndex, functionValue);
// Global battery check
if ((node[nodeIndex].function == 'B') && !(GET_NODE_BATT_LOW(node[nodeIndex].setting)) && (node[nodeIndex].value < 3.6)){
SET_NODE_BATT_LOW(node[nodeIndex].setting); // switch ON battery low flag
tmpLog[0] = 'R'; tmpLog[1] = 'A'; tmpLog[2] = DUMMY_NO_VALUE; tmpLog[3] = nodeIndex; pushToLog(tmpLog, 4);
}
if ((node[nodeIndex].function == 'B') && (GET_NODE_BATT_LOW(node[nodeIndex].setting)) && (node[nodeIndex].value > 4.16)){
tmpLog[0] = 'R'; tmpLog[1] = 'D'; tmpLog[2] = DUMMY_NO_VALUE; tmpLog[3] = nodeIndex; pushToLog(tmpLog, 4);
CLEAR_NODE_BATT_LOW(node[nodeIndex].setting); // switch OFF battery low flag
}
// Triggers
outMsgTrig = chPoolAlloc(&trigger_pool);
if (outMsgTrig != NULL) {
//memcpy(outMsg, inMsg, sizeof(sensorEvent_t));
outMsgTrig->address = inMsg->address;
outMsgTrig->function = inMsg->function;
outMsgTrig->number = inMsg->number;
outMsgTrig->type = 'S';
outMsgTrig->value = inMsg->value;
msg = chMBPostTimeout(&trigger_mb, (msg_t)outMsgTrig, TIME_IMMEDIATE);
if (msg != MSG_OK) {
//chprintf(console, "S-MB full %d\r\n", temp);
}
} else {
pushToLogText("FT"); // Trigger queue is full
}
} // node enabled
} else {
// Let's call same unknown node for re-registration only once a while,
// or we send many packets if multiple sensor data come in
if ((lastNode != inMsg->address) || (timeNow > lastNodeTime)) {
DBG_SENSOR("Unregistered sensor: %u:%c:%u\r\n", inMsg->address, inMsg->function, inMsg->number);
chThdSleepMilliseconds(5); // This is needed for sleeping battery nodes, or they wont see reg. command.
nodeIndex = sendCmd(inMsg->address, NODE_CMD_REGISTRATION); // call this address to register
lastNode = inMsg->address;
lastNodeTime = timeNow + 1; // add 1-2 second(s)
}
}
} else {
DBG_SENSOR("Sensor ERROR\r\n");
}
chPoolFree(&sensor_pool, inMsg);
}
}
#endif /* OHS_TH_SENSOR_H_ */