forked from Dashark/dam0888socket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cpp
More file actions
134 lines (120 loc) · 3.13 KB
/
Copy pathDevice.cpp
File metadata and controls
134 lines (120 loc) · 3.13 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
#include "Device.h"
#include <string>
#include <syslog.h>
#include <fstream>
#include <sstream>
#include <string>
#include <cassert>
#include "json.hpp"
#include <iostream>
using json = nlohmann::json;
Device::Device(const char ip[], const char id[]):ip_(ip), id_(id) {
}
Device::~Device() {
clearOpers();
}
void Device::update(int sid, const std::vector<char> &stats) {
int idx = 0;
bool newst = false;
for(char s : stats) {
for(Operation *oper : opers_) {
if(oper->equalPort(sid) && oper->equalAddr(idx)) {
newst |= oper->execute(s);
break; //TODO one state for one operation
}
}
idx += 1;
}
if(newst) {
for(Messager *mes : messagers_) {
if(mes != nullptr) {
stateStr(mes);
for(Broker *bk : brokers_) {
if(bk != nullptr) {
mes->send(bk);
}
}
}
}
}
}
std::string Device::stateStr() {
std::string all = "{\"kafkaType\":\"x\",\"data\":{\"mpos\":\"" + id_ + "\"";
for(Operation* oper : opers_) {
all += ",";
assert(oper != nullptr);
all += oper->stateStr();
}
gchar *time_str = NULL;
//GTimeVal time_val;
GDateTime *time = NULL;
time = g_date_time_new_now_local();
time_str = g_date_time_format(time, "%Y/%m/%d %H:%M:%S");
all += ",\"time\":\"" + std::string(time_str) + "\"}}";
g_free(time_str);
g_date_time_unref(time);
syslog(LOG_INFO, "Device String : %s", all.c_str());
return all;
}
std::string Device::stateStr(Messager *mes) {
mes->setID(id_);
for(Operation* oper : opers_) {
assert(oper != nullptr);
oper->stateStr(mes);
}
gchar *time_str = NULL;
GDateTime *time = NULL;
time = g_date_time_new_now_local();
time_str = g_date_time_format(time, "%Y/%m/%d %H:%M:%S");
mes->setTime(time_str);
g_free(time_str);
g_date_time_unref(time);
mes->dump();
return "";
}
void Device::clearOpers() {
for(Operation* oper : opers_)
delete oper;
opers_.clear();
}
///////////////////////////////////////////////////////////////////////
DeviceFactory::DeviceFactory() {
kafDef_ = new KafkaDefine();
}
DeviceFactory::~DeviceFactory() {
delete kafDef_;
}
std::vector<Device*> DeviceFactory::createDevices() {
kafDef_->load();
std::vector<Device*> devs;
std::ifstream i("config.json");
json js_;
i >> js_;
if(js_["devices"]==""){
syslog(LOG_CRIT, "Device configure fail!");
return devs; //TODO sth will do
}
else
{
syslog(LOG_CRIT, "Device configure success!");
}
OperationDefine opdef;
for (auto& element : js_["devices"]){
std::string str_id=element["id"];
char* id=(char*)str_id.c_str();
syslog(LOG_CRIT,"Device has loded !!! id: %s",id);
std::string str_ip=element["ip"];
char* ip=(char*)str_ip.c_str();
std::string str_type=element["type"];
char* type=(char*)str_type.c_str();
Device *dev = new Device(ip, id);
Broker *bro = kafDef_->getBroker(id);
dev->attach(bro);
Messager *mes = kafDef_->getMessager(id);
dev->attach(mes);
devs.push_back(dev);
std::vector<Operation*> ops = opdef.create(element["operate"], type);
dev->setOpers(ops);
}
return devs;
}