-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoad.cpp
More file actions
197 lines (170 loc) · 6.87 KB
/
Load.cpp
File metadata and controls
197 lines (170 loc) · 6.87 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
#include "PowerDevice.h"
#include "PowerSimTypeSupportImpl.h"
#include "common/DataReaderListenerBase.h"
#include "common/Utils.h"
#include <dds/DCPS/Marked_Default_Qos.h>
#include <ace/Get_Opt.h>
class LoadDevice;
class ElectricCurrentDataReaderListenerImpl : public DataReaderListenerBase {
public:
explicit ElectricCurrentDataReaderListenerImpl(LoadDevice& load_dev)
: DataReaderListenerBase("powersim::ElectricCurrent - DataReaderListenerImpl")
, load_dev_(load_dev)
{
}
virtual ~ElectricCurrentDataReaderListenerImpl() = default;
void on_data_available(DDS::DataReader_ptr reader) final;
private:
LoadDevice& load_dev_;
};
class LoadDevice : public PowerDevice {
public:
explicit LoadDevice(const tms::Identity& id, bool verbose = false)
: PowerDevice(id, tms::DeviceRole::ROLE_LOAD, verbose)
{
}
DDS::ReturnCode_t init(DDS::DomainId_t domain_id, int argc = 0, char* argv[] = nullptr)
{
DDS::ReturnCode_t rc = PowerDevice::init(domain_id, argc, argv);
if (rc != DDS::RETCODE_OK) {
return rc;
}
// Subscribe to powersim::ElectricCurrent topic
powersim::ElectricCurrentTypeSupport_var ec_ts = new powersim::ElectricCurrentTypeSupportImpl;
if (DDS::RETCODE_OK != ec_ts->register_type(sim_participant_, "")) {
ACE_ERROR((LM_ERROR, "(%P|%t) ERROR: LoadDevice::init: register_type ElectricCurrent failed\n"));
return DDS::RETCODE_ERROR;
}
CORBA::String_var ec_type_name = ec_ts->get_type_name();
DDS::Topic_var ec_topic = sim_participant_->create_topic(powersim::TOPIC_ELECTRIC_CURRENT.c_str(),
ec_type_name,
TOPIC_QOS_DEFAULT,
nullptr,
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!ec_topic) {
ACE_ERROR((LM_ERROR, "(%P|%t) ERROR: LoadDevice::init: create_topic \"%C\" failed\n",
powersim::TOPIC_ELECTRIC_CURRENT.c_str()));
return DDS::RETCODE_ERROR;
}
DDS::Subscriber_var sim_sub = sim_participant_->create_subscriber(SUBSCRIBER_QOS_DEFAULT,
nullptr,
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!sim_sub) {
ACE_ERROR((LM_ERROR, "(%P|%t) ERROR: LoadDevice::init: create_subscriber failed\n"));
return DDS::RETCODE_ERROR;
}
DDS::DataReaderListener_var ec_listener(new ElectricCurrentDataReaderListenerImpl(*this));
DDS::DataReader_var ec_dr_base = sim_sub->create_datareader(ec_topic,
DATAREADER_QOS_DEFAULT,
ec_listener.in(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (!ec_dr_base) {
ACE_ERROR((LM_ERROR, "(%P|%t) ERROR: LoadDevice::init: create_datareader for topic \"%C\" failed\n",
powersim::TOPIC_ELECTRIC_CURRENT.c_str()));
return DDS::RETCODE_ERROR;
}
return DDS::RETCODE_OK;
}
tms::Identity connected_dev_id() const
{
const powersim::ConnectedDeviceSeq connected_devs = connected_devices_in();
if (connected_devs.empty()) {
return tms::Identity();
}
return connected_devs[0].id();
}
private:
tms::DeviceInfo populate_device_info() const override
{
auto device_info = get_device_info();
device_info.role() = tms::DeviceRole::ROLE_LOAD;
device_info.product() = Utils::get_ProductInfo();
device_info.topics() = Utils::get_TopicInfo({}, {}, {});
tms::PowerDeviceInfo pdi;
{
pdi.powerPorts() = { tms::PowerPortInfo() };
tms::LoadInfo load_info;
{
// Pick a feature for the demo purpose.
load_info.features() = { tms::LoadFeature::LOADF_DEMAND_RESPONSE };
}
pdi.load() = load_info;
}
device_info.powerDevice() = pdi;
return device_info;
}
tms::EnergyStartStopLevel essl_ = tms::EnergyStartStopLevel::ESSL_OPERATIONAL;
};
void ElectricCurrentDataReaderListenerImpl::on_data_available(DDS::DataReader_ptr reader)
{
powersim::ElectricCurrentSeq data;
DDS::SampleInfoSeq info_seq;
powersim::ElectricCurrentDataReader_var typed_reader = powersim::ElectricCurrentDataReader::_narrow(reader);
const DDS::ReturnCode_t rc = typed_reader->take(data, info_seq, DDS::LENGTH_UNLIMITED,
DDS::ANY_SAMPLE_STATE, DDS::ANY_VIEW_STATE, DDS::ANY_INSTANCE_STATE);
if (rc != DDS::RETCODE_OK) {
ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: ElectricCurrentDataReaderListenerImpl::on_data_available: "
"take data failed: %C\n", OpenDDS::DCPS::retcode_to_string(rc)));
return;
}
// Simulate the non-operational mode by ignoring the simulated current messages
if (load_dev_.energy_level() != tms::EnergyStartStopLevel::ESSL_OPERATIONAL) {
return;
}
for (CORBA::ULong i = 0; i < data.length(); ++i) {
if (info_seq[i].valid_data) {
const powersim::ElectricCurrent& ec = data[i];
const auto& power_path = ec.power_path();
const int path_length = power_path.size();
if (path_length < 2) {
ACE_ERROR((LM_WARNING, "(%P|%t) WARNING: ElectricCurrentDataReaderListenerImpl::on_data_available: invalid power path\n"));
continue;
}
const tms::Identity& from = power_path[path_length - 2];
const tms::Identity& to = power_path[path_length - 1];
if (from == load_dev_.connected_dev_id() && to == load_dev_.get_device_id()) {
if (load_dev_.verbose()) {
ACE_DEBUG((LM_INFO, "=== (%T) Receiving power from \"%C\" -- %f Amps...\n", from.c_str(), ec.amperage()));
}
break;
}
}
}
}
int main(int argc, char* argv[])
{
DDS::DomainId_t domain_id = OpenDDS::DOMAIN_UNKNOWN;
const char* load_id = nullptr;
bool verbose = false;
ACE_Get_Opt get_opt(argc, argv, "d:i:v");
if (get_opt.long_option("domain", 'd', ACE_Get_Opt::ARG_REQUIRED) != 0 ||
get_opt.long_option("id", 'i', ACE_Get_Opt::ARG_REQUIRED) != 0 ||
get_opt.long_option("verbose", 'v', ACE_Get_Opt::NO_ARG) != 0) {
return 1;
}
int c;
while ((c = get_opt()) != -1) {
switch (c) {
case 'i':
load_id = get_opt.opt_arg();
break;
case 'd':
domain_id = static_cast<DDS::DomainId_t>(ACE_OS::atoi(get_opt.opt_arg()));
break;
case 'v':
verbose = true;
break;
default:
break;
}
}
if (domain_id == OpenDDS::DOMAIN_UNKNOWN || load_id == nullptr) {
ACE_ERROR((LM_ERROR, "Usage: %C -d DDS_Domain_Id -i Load_Device_Id [-v]\n", argv[0]));
return 1;
}
LoadDevice load_dev(load_id, verbose);
if (load_dev.init(domain_id, argc, argv) != DDS::RETCODE_OK) {
return 1;
}
return load_dev.run();
}