-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitoringController.cpp
More file actions
293 lines (246 loc) · 9.12 KB
/
MonitoringController.cpp
File metadata and controls
293 lines (246 loc) · 9.12 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "MonitoringController.h"
namespace MonitoringComponents {
void MonitoringController::Setup() {
MonitoringLogger::Start(&Serial, LogLevel::Info);
this->systemActionLatches.insert(std::pair<ActionType, bool>(ActionType::Alarm, false));
this->systemActionLatches.insert(std::pair<ActionType, bool>(ActionType::Warning, false));
this->systemActionLatches.insert(std::pair<ActionType, bool>(ActionType::SoftWarn, false));
this->systemActionLatches.insert(std::pair<ActionType, bool>(ActionType::Maintenance, false));
this->systemActionLatches.insert(std::pair<ActionType, bool>(ActionType::Okay, false));
this->OnChannelCallback([&](ChannelMessage message) {
ProcessChannelMessage(message);
});
this->Build();
for(auto action : this->actions) {
this->tracking.insert(std::pair<int, int*>(action->Id(), new int(0)));
}
MonitoringLogger::LogInfo(F("Latches"));
for(auto actionLatches : systemActionLatches) {
MonitoringLogger::LogInfo(F("ActionType: %u State: %u"), (int)actionLatches.first, actionLatches.second);
}
MonitoringLogger::LogInfo(F("Action Registrations: "));
for(auto registration : tracking) {
MonitoringLogger::LogInfo(F("Id: %u Instances: %u"), registration.first, (*registration.second));
}
}
void MonitoringController::Build() {
ConfigurationReader reader;
reader.Init();
auto discreteConfig = reader.DeserializeDigitalConfig();
auto analogConfig = reader.DeserializeAnalogConfig();
auto outputConfig = reader.DeserializeOutputConfig();
auto actionConfig = reader.DeserializeActions();
auto virtualConfig = reader.DeserializeVirtualConfig();
auto netConfig = reader.DeserializeNetConfiguration();
this->controllerRegister=netConfig.modbusAddress;
MonitoringLogger::EnableFileLogger();
ModbusService::Initialize(netConfig);
//Outputs needed for building actions
for (auto output : outputConfig) {
DiscreteOutputChannel* channel = new DiscreteOutputChannel(output);
this->outputChannels.push_back(channel);
}
MonitoringLogger::LogInfo(F("Creating Actions"));
for (int i = 0; i < actionConfig.size();i++) {
Action* action=new Action(actionConfig[i]);
if (actionConfig[i].actionType != ActionType::Custom) {
this->systemActMap[actionConfig[i].actionType] = i;
}
if (actionConfig[i].addr1) {
auto outputChannel=std::find_if(outputChannels.begin(), outputChannels.end(), [&](DiscreteOutputChannel* output) {
return actionConfig[i].addr1 == output->Address();
});
if (outputChannel != outputChannels.end()) {
ActionOutput* output = new ActionOutput((*outputChannel), actionConfig[i].onLevel1, actionConfig[i].offLevel1);
action->SetOutput(output, 1);
}
}
if (actionConfig[i].addr2) {
auto outputChannel=std::find_if(outputChannels.begin(), outputChannels.end(), [&](DiscreteOutputChannel* output) {
return actionConfig[i].addr2 == output->Address();
});
if (outputChannel != outputChannels.end()) {
ActionOutput* output = new ActionOutput((*outputChannel), actionConfig[i].onLevel2, actionConfig[i].offLevel2);
action->SetOutput(output, 2);
}
}
if (actionConfig[i].addr3) {
auto outputChannel=std::find_if(outputChannels.begin(), outputChannels.end(), [&](DiscreteOutputChannel* output) {
return actionConfig[i].addr3 == output->Address();
});
if (outputChannel != outputChannels.end()) {
ActionOutput* output = new ActionOutput((*outputChannel), actionConfig[i].onLevel3, actionConfig[i].offLevel3);
action->SetOutput(output, 3);
}
}
this->actions.push_back(action);
}
MonitoringLogger::LogInfo(F("Creating Discrete Channels"));
for (auto ch : discreteConfig) {
DiscreteInputChannel* channel = new DiscreteInputChannel(ch);
this->discreteInputs.push_back(channel);
RegisterChild(channel);
channel->OnStateChange(this->_on_channel_cbk);
}
for (auto ch : virtualConfig) {
DiscreteVirtualChannel* channel = new DiscreteVirtualChannel(ch);
this->virtualInputs.push_back(channel);
RegisterChild(channel);
channel->OnStateChange(this->_on_channel_cbk);
}
MonitoringLogger::LogInfo(F("Creating Analog Channels"));
for (auto ch : analogConfig) {
AnalogInputChannel* channel = new AnalogInputChannel(ch);
this->analogInputs.push_back(channel);
RegisterChild(channel);
channel->OnStateChange(this->_on_channel_cbk);
}
}
void MonitoringController::Initialize() {
for (auto output : outputChannels) {
output->SetOutput(State::Low);
}
for (auto action : actions) {
action->Clear();
}
for (auto dinput : discreteInputs) {
dinput->Initialize();
}
for (auto vInput : virtualInputs) {
vInput->Initialize();
}
for (auto ainput : analogInputs) {
ainput->Initialize();
}
P1.configWD(20000,TOGGLE);
P1.startWD();
ProcessStateChanges();
}
void MonitoringController::OnChannelCallback(ChannelCallback cbk) {
this->_on_channel_cbk = cbk;
}
void MonitoringController::ProcessChannelMessage(ChannelMessage message) {
Action* action;
if (message.type != ActionType::Custom) {
action = this->actions[systemActMap[message.type]];
MonitoringLogger::LogInfo(F("Action: %d Channel: %d,%d"),action->Id(),message.channel.slot,message.channel.channel);
}else {
auto act = find_if(actions.begin(), actions.end(), [&](Action* act) {
return message.actionId == act->Id();
});
if (act == actions.end()) {
MonitoringLogger::LogError(F("Invalid ActionId: %d From Channel(Slot,Channel): (%d,%d)"),
message.actionId, message.channel.slot, message.channel.channel);
return;
}
action = (*act);
}
int id = action->Id();
int* actionCount = tracking[id];
switch(message.channelAction) {
case ChannelAction::Trigger: {
if(message.type == ActionType::Custom) {
action->Invoke();
} else {
(*actionCount) += 1;
this->systemActionLatches[message.type] = true;
this->ProcessStateChanges();
}
break;
}
case ChannelAction::Clear:{
if(message.type == ActionType::Custom) {
action->Clear();
} else {
(*actionCount) -= 1;
if((*actionCount)==0) {
this->systemActionLatches[message.type] = false;
this->ProcessStateChanges();
} else if((*actionCount) < 0) {
(*actionCount) = 0;
MonitoringLogger::LogError(F("Action Id: %d is negative. Count: %d"),id,(*actionCount));
}
}
break;
}
}
}
void MonitoringController::InvokeSystemAction(ActionType actionType) {
if (actionType != ActionType::Custom) {
int index = systemActMap[actionType];
Action* action = actions[index];
action->Invoke();
}
}
void MonitoringController::ClearSystemAction(ActionType actionType) {
auto action = this->actions[this->systemActMap[actionType]];
action->Clear();
}
void MonitoringController::Print(){
String buffer;
MonitoringLogger::LogInfo(F("Latches"));
for (auto actionLatches : systemActionLatches) {
MonitoringLogger::LogInfo(F("ActionType: %u State: %u"), (int)actionLatches.first, actionLatches.second);
}
switch (controllerState) {
case ControllerState::Alarming:{
MonitoringLogger::LogInfo(F("ControllerState: Alarming"));
break;
}
case ControllerState::Maintenance:{
MonitoringLogger::LogInfo(F("ControllerState: Maintenance"));
break;
}
case ControllerState::Warning:{
MonitoringLogger::LogInfo(F("ControllerState: Warning"));
break;
}
case ControllerState::Okay:{
MonitoringLogger::LogInfo(F("ControllerState: Okay"));
break;
}
}
MonitoringLogger::LogInfo(F("Action Registrations: "));
for (auto registration : tracking) {
MonitoringLogger::LogInfo(F("Id: %u Instances: %u"), registration.first, (*registration.second));
}
}
bool MonitoringController::CheckController(){
int moduleCount=this->modules.size();
int baseErrors=P1.rollCall(&this->modules[0], moduleCount);
return baseErrors==0;
}
void MonitoringController::ProcessStateChanges() {
if (systemActionLatches[ActionType::Maintenance]) {
if (this->controllerState != ControllerState::Maintenance) {
this->controllerState = ControllerState::Maintenance;
this->InvokeSystemAction(ActionType::Maintenance);
MonitoringLogger::LogInfo("State Changed to Maintenance");
}
} else if (systemActionLatches[ActionType::Alarm]) {
if (this->controllerState != ControllerState::Alarming) {
this->controllerState = ControllerState::Alarming;
this->InvokeSystemAction(ActionType::Alarm);
MonitoringLogger::LogInfo("State Changed to Alarm");
}
} else if (systemActionLatches[ActionType::Warning]) {
if (this->controllerState != ControllerState::Warning) {
this->controllerState = ControllerState::Warning;
this->InvokeSystemAction(ActionType::Warning);
MonitoringLogger::LogInfo("State Changed to Warning");
}
} else {
if (this->controllerState != ControllerState::Okay) {
this->controllerState = ControllerState::Okay;
this->InvokeSystemAction(ActionType::Okay);
MonitoringLogger::LogInfo("State changed to Okay");
}
}
ModbusService::Update(this->controllerRegister,uint16_t(this->controllerState));
}
void MonitoringController::Run() {
this->loop();
//P1.petWD();
}
void MonitoringController::privateLoop() { }
};