-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscreteVirtualChannel.h
More file actions
102 lines (91 loc) · 2.81 KB
/
DiscreteVirtualChannel.h
File metadata and controls
102 lines (91 loc) · 2.81 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
#pragma once
#include <ArduinoSTL.h>
#include "ModbusService.h"
#include "Function.h"
#include "IO.h"
#include "Callbacks.h"
#include "MonitoringComponent.h"
#include "Configuration.h"
namespace MonitoringComponents {
class DiscreteVirtualChannel : public MonitoringComponent {
public:
DiscreteVirtualChannel(VirtualDigitalConfiguration config, Ref<MonitoringComponent> parent = nullptr):MonitoringComponent(parent),
configuration(config),
modbusAddress(config.modbusAddress),
alertAddress(config.alertAddress),
triggerOn(config.triggerOn),
enabled(config.enabled),
_on_state_change([](ChannelMessage) {}),
alert(config.alert) {
this->alert.activated=false;
this->triggered=false;
}
DiscreteVirtualChannel():MonitoringComponent(nullptr), _on_state_change([](ChannelMessage){ }) { };
void Initialize() {
bool state = this->isTriggered();
if (state) {
ChannelMessage message;
message.actionId = this->alert.actionId;
message.channel = ChannelAddress();
message.type = this->alert.actionType;
message.channelAction = ChannelAction::Trigger;
ModbusService::Update(this->alertAddress,uint16_t(this->alert.actionType));
this->alert.activated = true;
this->_on_state_change(message);
}else{
ModbusService::Update(this->alertAddress,uint16_t(ActionType::Okay));
}
this->triggered = state;
}
bool isTriggered() {
if(!this->enabled) return (this->triggerOn==TriggerOn::Low) ? true:false;
bool value = ModbusService::ReadCoil(this->modbusAddress.address);
return (this->triggerOn==TriggerOn::Low) ? !value:value;
/* switch (this->triggerOn) {
case TriggerOn::High: {
return value;
}
case TriggerOn::Low: {
return !value;
}
default: {
return false;
}
} */
}
void OnStateChange(ChannelCallback cbk) {
this->_on_state_change = cbk;
}
private:
VirtualDigitalConfiguration configuration;
TriggerOn triggerOn;
ModbusAddress modbusAddress;
ModbusAddress alertAddress;
DigitalAlert alert;
bool triggered;
bool enabled;
ChannelCallback _on_state_change;
void privateLoop() {
bool state = this->isTriggered();
if (state != this->triggered) {
if (this->alert.enabled) {
ChannelMessage message;
message.actionId = this->alert.actionId;
message.channel = ChannelAddress();
message.type = this->alert.actionType;
if (state) {
message.channelAction = ChannelAction::Trigger;
this->alert.activated = true;
ModbusService::Update(this->alertAddress,uint16_t(this->alert.actionType));
}else {
message.channelAction = ChannelAction::Clear;
this->alert.activated = false;
ModbusService::Update(this->alertAddress,uint16_t(ActionType::Okay));
}
this->_on_state_change(message);
}
this->triggered = state;
}
}
};
}