-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbmscollector.cpp
350 lines (314 loc) · 10.6 KB
/
bmscollector.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "bmscollector.h"
#include "bms_def.h"
#include <QtNetwork>
#include "secs.h"
#include "bms_bmudevice.h"
#include "bms_bcudevice.h"
#include "bms_svidevice.h"
#include "bms_system.h"
#include <QSysInfo>
BMSCollector::BMSCollector(QObject *parent) : QObject(parent)
{
}
bool BMSCollector::loadConfig(QString path)
{
bool ret = false;
QFile f(path);
if(!f.open(QIODevice::ReadOnly)){
f.close();
return false;
}
QJsonParseError e;
QJsonDocument d = QJsonDocument::fromJson(f.readAll(),&e);
f.close();
if(d.isNull()){
return false;
}
QJsonObject obj = d.object();
if(obj.contains("bms")){
QJsonArray lst = obj["bms"].toArray();
for(auto v:lst){
QJsonObject o = v.toObject();
RemoteSystem *sys = new RemoteSystem();
sys->connection = o["ip"].toString();
sys->alias = o["alias"].toString();
sys->port = o["port"].toInt();
sys->autoConnect = o["autoconnect"].toBool();
m_servers.append(sys);
if(QSysInfo::productType().contains("win")){
sys->logPath = o["log_path"].toString()+"/"+sys->connection;
}
else{
sys->logPath = QCoreApplication::applicationDirPath()+ o["log_path"].toString()+"/"+sys->connection;
}
//sys->system->logPath(sys->logPath);
// create log path
if(!QDir(sys->logPath).exists()){
QDir().mkpath(sys->logPath);
}
if(o.contains("enable_log")){
//sys->system->enableLog(o["enable_log"].toBool());
sys->enableLog = o["enable_log"].toBool();
}
else{
//sys->system->enableLog(false);
}
if(o.contains("log_days")){
//sys->system->logDays(o["log_days"].toInt());
sys->logDays = o["log_days"].toInt();
}
else{
//sys->system->logDays(-1);
}
if(o.contains("log_records")){
//sys->system->logRecords(o["log_records"].toInt());
sys->logRecords = o["log_records"].toInt();
}
else{
//sys->system->logRecords(1000);
}
}
}
if(obj.contains("loginpromote")){
m_loginPromote = obj["loginpromote"].toBool();
}
}
bool BMSCollector::loginPromote()
{
return m_loginPromote;
}
bool BMSCollector::connectServer(int id)
{
bool success = false;
if(id == -1){ // connect each system if autoconnect
foreach (RemoteSystem *s, m_servers) {
if(!s->autoConnect) continue;
if(s->socket == nullptr){
QTcpSocket *socket = new QTcpSocket();
socket->connectToHost(s->connection,s->port);
if(socket->isValid()){
connect(socket,&QTcpSocket::readyRead,this,&BMSCollector::handleServerData);
if(m_currentSystemIndex<0) m_currentSystemIndex = 0;
s->socket = socket;
s->data.clear();
s->configReady = false;
//s->configReady =true;
readAllConfig();
success = true;
}
}
}
}
else if(id < m_servers.size()){
RemoteSystem *s = m_servers[id];
if(s->socket == nullptr){
QTcpSocket *socket = new QTcpSocket();
//qDebug()<<"Connect to "<<s->connection <<" Port: " <<s->port;
socket->connectToHost(s->connection,s->port);
bool connected = socket->waitForConnected(5000);
if(connected){
qDebug()<<"Socket connected";
connect(socket,&QTcpSocket::readyRead,this,&BMSCollector::handleServerData);
if(m_currentSystemIndex<0) m_currentSystemIndex = 0;
s->socket = socket;
connect(socket,&QTcpSocket::disconnected,this,&BMSCollector::handleSocketDisconnect);
s->configReady = false;
s->data.clear();
s->system = new BMS_System();
s->system->setController(false);
//s->configReady = true;
readAllConfig();
success = true;
}
else{
//qDebug()<<"Connect fail";
}
}
}
return success;
}
bool BMSCollector::disconnectServer(int id)
{
qDebug()<<Q_FUNC_INFO;
if(id < m_servers.size() && m_servers[id]->socket != nullptr){
if(m_servers[id]->socket->isValid()){
m_servers[id]->socket->close();
//disconnect(m_servers[id]->socket,&QTcpSocket::readyRead);
// m_servers[id]->socket->deleteLater();
// m_servers[id]->socket = nullptr;
m_servers[id]->configReady = false;
//m_servers[id]->system->deleteLater();
//delete m_servers[id]->socket;
//delete m_servers[id]->system;
return true;
}
}
return false;
}
bool BMSCollector::addConnection(QString ipAddress)
{
// QTcpSocket *socket = new QTcpSocket();
// socket->connectToHost(ipAddress,5001);
// if(socket->isValid()){
// RemoteSystem *sys = new RemoteSystem();
// sys->connection=ipAddress;
// sys->socket = socket;
// sys->system = new BMS_System();
// sys->system->connectionString = ipAddress;
// m_servers.append(sys);
// connect(socket,&QTcpSocket::readyRead,this,&BMSCollector::handleServerData);
// if(m_currentSystemIndex<0) m_currentSystemIndex = 0;
// return true;
// }
return false;
}
bool BMSCollector::deleteConnection(QTcpSocket *socket)
{
foreach (RemoteSystem *s, m_servers) {
if(s->socket == socket){
m_servers.removeOne(s);
}
}
}
bool BMSCollector::readConfig(QString connection)
{
QByteArray b = "SYS:CFGFR";
//b.insert(0,hsmsParser::genHeader(hsmsParser::BMS_CONFIG,b.size()));
foreach(RemoteSystem *s, m_servers) {
if(s->connection == connection){
qDebug()<<"Write Command"<<b;
s->socket->write(b);
// s->socket->flush();
}
}
}
void BMSCollector::readAllConfig()
{
QByteArray b = "SYS:CFGFR";
//b.insert(0,hsmsParser::genHeader(hsmsParser::BMS_CONFIG,b.size()));
foreach(RemoteSystem *s, m_servers) {
//qDebug()<<"Write Command"<<b;
s->socket->write(b);
}
}
void BMSCollector::readInitTime()
{
QByteArray b = "SYS:INIT_TIME";
foreach (RemoteSystem *s, m_servers) {
s->socket->write(b);
}
}
void BMSCollector::handleSocketDisconnect()
{
//qDebug()<<Q_FUNC_INFO;
QTcpSocket *sock = static_cast<QTcpSocket*>(sender());
foreach (RemoteSystem *sys , m_servers) {
if(sys->socket == sock){
sys->socket->deleteLater();
sys->socket = nullptr;
sys->configReady = false;
// delete sys->system;
// delete sys->socket;
emit controllerOffline();
}
}
}
// todo: move data handling process to timed function
// handle every system periodically
void BMSCollector::handleServerData()
{
QTcpSocket *socket = (QTcpSocket*)sender();
//qDebug()<<Q_FUNC_INFO<<socket;
//RemoteSystem *sys;
foreach (RemoteSystem *sys, m_servers) {
if(sys->socket == socket){
//QByteArray data = sys->socket->readAll();
sys->data.append(sys->socket->readAll());
sys->lastSeen = QDateTime::currentDateTime();
int len;
quint8 hlen;
if(sys->configReady){
//qDebug()<<"Config ready";
quint8 ret = hsmsParser::getHeader(sys->data,&len,&hlen);
if(ret == hsmsParser::BMS_STACK && sys->data.size() >= (len+hlen)){
//if(sys->data.size() >= len){
//qDebug()<<"Receive stack packet";
sys->data.remove(0,hlen);
QDataStream d(&sys->data,QIODevice::ReadOnly);
//qDebug()<<"Feed start";
d >> sys->system;
//qDebug()<<"Feed 1";
emit dataReceived();
sys->data.remove(0,len);
//qDebug()<<"Feed 2";
// if(sys->system->enableLog()){
// QByteArray b;
// QDataStream d2(&b,QIODevice::ReadWrite);
// d2 << sys->system;
// sys->system->log(b);
// }
// show alarm information
//bool set = false;
//qDebug()<<"Feed end";
//}
}
else if(ret == hsmsParser::BMS_WRONG_HEADER){
sys->data.clear();
}
else if(ret == hsmsParser::BMS_SYS_DATETIME){
sys->data.remove(0,hlen);
QDataStream ds(&sys->data,QIODevice::ReadWrite);
qint64 epoch;
ds >> epoch;
sys->system->startTime(epoch);
}
}
else{
qDebug()<<"Not Ready";
quint8 ret = hsmsParser::getHeader(sys->data,&len,&hlen);
if(ret == hsmsParser::BMS_CONFIG){
qDebug()<<"Receive config packet";
sys->data.remove(0,hlen);
sys->system->Configuration2(sys->data);
sys->configReady = true;
emit configReady();
sys->data.remove(0,len);
}
else if(ret == hsmsParser::BMS_WRONG_HEADER){
sys->data.clear();
}
}
}
}
}
RemoteSystem* BMSCollector::currentSystem()
{
if(m_currentSystemIndex < 0){
return nullptr;
}
else{
return m_servers.at(m_currentSystemIndex);
}
}
RemoteSystem* BMSCollector::nextSystem()
{
if(m_currentSystemIndex < 0) return nullptr;
else{
m_currentSystemIndex++;
if(m_currentSystemIndex == m_servers.size()){
m_currentSystemIndex = 0;
}
return m_servers.at(m_currentSystemIndex);
}
}
RemoteSystem* BMSCollector::prevSystem()
{
if(m_currentSystemIndex < 0) return nullptr;
else{
m_currentSystemIndex--;
if(m_currentSystemIndex<0){
m_currentSystemIndex = m_servers.size()-1;
}
return m_servers.at(m_currentSystemIndex);
}
}