9 #include "controlwebsocketclient.h"
10 #include "datamessage.h"
11 #include "eventmessage.h"
12 #include "logresult.h"
14 std::vector<VehicleInfoDefine> vehicleinfolist;
15 char NOTREGISTERED[] = "NotRegistered";
17 class ControlWebsocketClientForControl : public ControlWebsocketClient {
19 bool recv(char *recvbuf, const size_t len);
24 bool ControlWebsocketClientForControl::recv(char *recvbuf, const size_t len) {
26 datamsg.decode(recvbuf, len);
27 DataOpt dopt = datamsg.getDataOpt();
29 auto itr_end = vehicleinfolist.end();
30 ClientType cltype = CLIENT_VIC;
31 CommandType cotype = CMDTYPE_GET;
32 for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
33 if (strcmp((*itr).KeyEventType, datamsg.getKeyEventType()) == 0) {
34 auto itr2_end = (*itr).status.end();
36 for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
37 switch ((*itr2).type) {
40 memcpy(&i, dopt.status + idx, sizeof(i));
41 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i, datamsg.getRecordtime());
46 memcpy(&d, dopt.status + idx, sizeof(d));
47 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, d, datamsg.getRecordtime());
52 memcpy(&c, dopt.status + idx, sizeof(c));
53 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, c, datamsg.getRecordtime());
58 memcpy(&i16, dopt.status + idx, sizeof(i16));
59 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i16, datamsg.getRecordtime());
64 memcpy(&ui16, dopt.status + idx, sizeof(ui16));
65 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui16, datamsg.getRecordtime());
70 memcpy(&ui32, dopt.status + idx, sizeof(ui32));
71 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui32, datamsg.getRecordtime());
76 memcpy(&i64, dopt.status + idx, sizeof(i64));
77 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i64, datamsg.getRecordtime());
82 memcpy(&ui64, dopt.status + idx, sizeof(ui64));
83 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui64, datamsg.getRecordtime());
88 memcpy(&b, dopt.status + idx, sizeof(b));
89 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, b, datamsg.getRecordtime());
101 template <typename T>
102 int SetDataOpt(DataOpt *opt, int idx, T value) {
103 memcpy(opt->status + idx, &value, sizeof(value));
104 return sizeof(value);
107 int main(int argc, char *argv[]) {
109 std::string configpath = "/etc/ambd/AMBformat.conf";
110 while ((ret = getopt(argc, argv, "c:")) != -1) {
113 configpath = std::string(optarg);
120 ifstream fin("standardvehicleinfo.txt");
122 std::vector<std::string> standardvehicleinfolist;
125 standardvehicleinfolist.push_back(str);
129 if (!config.readConfig(configpath)) {
130 std::cerr << "Can't read VIC-Plugin config file." << std::endl;
133 PortInfo portinfo = config.getPort();
135 ControlWebsocketClient stddataws, customdataws;
136 ControlWebsocketClientForControl stdctrlws, customctrlws;
137 if (!stddataws.initialize("ws://127.0.0.1", portinfo.standard.dataPort, "standarddatamessage-only")) {
138 std::cerr << "Can't connect standarddatamessage-only." << std::endl;
141 if (!stdctrlws.initialize("ws://127.0.0.1", portinfo.standard.controlPort, "standardcontrolmessage-only")) {
142 std::cerr << "Can't connect standarddatamessage-only." << std::endl;
145 if (!customdataws.initialize("ws://127.0.0.1", portinfo.custom.dataPort, "customdatamessage-only")) {
146 std::cerr << "Can't connect customdatamessage-only." << std::endl;
149 if (!customctrlws.initialize("ws://127.0.0.1", portinfo.custom.controlPort, "customcontrolmessage-only")) {
150 std::cerr << "Can't connect customdatamessage-only." << std::endl;
154 vehicleinfolist =config.getVehicleInfoConfig();
155 auto itr_end = vehicleinfolist.end();
159 enum ClientType cltype = CLIENT_VIC;
160 enum CommandType cotype = CMDTYPE_SET;
163 for (int i = 0; i < 10; i++) {
165 customdataws.service();
167 customctrlws.service();
171 for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
173 customdataws.service();
175 customctrlws.service();
179 dopt.common_status = SUPPORT;
181 auto itr2_end = (*itr).status.end();
182 gettimeofday(&tv, NULL);
183 for (auto itr2 = (*itr).status.begin(); itr2 != itr2_end; itr2++) {
184 switch ((*itr2).type) {
187 idx += SetDataOpt(&dopt, idx, i);
188 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i);
193 idx += SetDataOpt(&dopt, idx, d);
194 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, d);
199 idx += SetDataOpt(&dopt, idx, c);
200 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, c);
205 idx += SetDataOpt(&dopt, idx, i16);
206 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i16);
211 idx += SetDataOpt(&dopt, idx, ui16);
212 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui16);
217 idx += SetDataOpt(&dopt, idx, ui32);
218 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui32);
223 idx += SetDataOpt(&dopt, idx, i64);
224 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, i64);
229 idx += SetDataOpt(&dopt, idx, ui64);
230 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, ui64);
235 idx += SetDataOpt(&dopt, idx, b);
236 logger.PutOut(cltype, cotype, (*itr2).ambPropertyName, b);
244 if (std::find(standardvehicleinfolist.begin(), standardvehicleinfolist.end(), std::string((*itr).KeyEventType)) != standardvehicleinfolist.end()) {
245 stddataws.send(datamsg.encode((*itr).KeyEventType, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
249 customdataws.send(datamsg.encode((*itr).KeyEventType, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(int) + idx);
253 customdataws.service();
255 customctrlws.service();
259 dopt.common_status = SUPPORT;
260 SetDataOpt(&dopt, 0, (int)5);
261 stddataws.send(datamsg.encode(NOTREGISTERED, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + 2 * sizeof(int));
263 customdataws.service();
265 customctrlws.service();
267 customdataws.send(datamsg.encode(NOTREGISTERED, tv, dopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + 2 * sizeof(int));
269 customdataws.service();
271 customctrlws.service();
274 EventMessage eventmsg;
278 eopt.event_mask = -1;
280 for (auto itr = vehicleinfolist.begin(); itr != itr_end; itr++) {
282 customdataws.service();
284 customctrlws.service();
287 gettimeofday(&tv, NULL);
288 if (std::find(standardvehicleinfolist.begin(), standardvehicleinfolist.end(), std::string((*itr).KeyEventType)) != standardvehicleinfolist.end()) {
289 stdctrlws.send(eventmsg.encode((*itr).KeyEventType, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
293 customctrlws.send(eventmsg.encode((*itr).KeyEventType, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
297 customdataws.service();
299 customctrlws.service();
302 stdctrlws.send(eventmsg.encode(NOTREGISTERED, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
304 customdataws.service();
306 customctrlws.service();
308 customctrlws.send(eventmsg.encode(NOTREGISTERED, tv, eopt), StandardMessage::KEYEVENTTYPESIZE + sizeof(timeval) + sizeof(EventOpt));
310 customdataws.service();
312 customctrlws.service();
315 for (int i = 0; i < 10; i++) {
317 customdataws.service();
319 customctrlws.service();