-
Notifications
You must be signed in to change notification settings - Fork 3
/
p17_cmd_battery_self_test.h
93 lines (79 loc) · 2.49 KB
/
p17_cmd_battery_self_test.h
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
#ifndef CMDBATTERYSELFTEST_H
#define CMDBATTERYSELFTEST_H
#include <QString>
#include <QByteArray>
#include <QtDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <icmdquery.h>
#include <response_double.h>
#include <response_enum.h>
#include <response_longlong.h>
#include <response_switch.h>
class P17CmdBatterySelfTest : public iCmdQuery
{
private:
bool isValidFormat(QByteArray response) {
bool result=true;
if(! response.endsWith("\r"))
result=false;
if(! response.startsWith("^"))
result=false;
Crc crc;
if(! crc.checkResponse(response))
result=false;
return result;
}
bool doSend = false;
bool awaitResponse = false;
public:
virtual QString getStateTopic() {
QString stateTopic=devName + "/switch/" + cmdName + "/state";
stateTopic.replace(QRegularExpression("\\s+"), "_");
return (stateTopic);
}
virtual QString getCommandTopic() {
QString stateTopic=devName + "/switch/" + cmdName + "/command";
stateTopic.replace(QRegularExpression("\\s+"), "_");
return (stateTopic);
}
P17CmdBatterySelfTest(QString devName) : iCmdQuery("BatteryTest", devName) {
responseList.append(new ResponseSwitch(devName, "BatteryTest"));
}
virtual QByteArray getCmd() {
if(doSend) {
qDebug() << "BatteryTest acivated.";
doSend=false;
awaitResponse = true;
return("^S004BST\r");
} else {
awaitResponse = false;
return("");
}
}
virtual QByteArray resultToJson(QByteArray response) {
if(awaitResponse) {
if(isValidFormat(response)) {
QByteArray temp = response.right(response.size() - 5);
temp.truncate(temp.size() - 3);
responseList.at(0)->setValue(temp);
QJsonObject recordObject;
foreach (auto entry, responseList) {
recordObject.insert(entry->getJsonKey(), entry->getJsonValue());
}
QJsonDocument doc(recordObject);
return(doc.toJson());
} else {
qDebug() << "BatterySelfTest: wrong Format";
return("");
}
}
return("");
}
virtual void updateMessage(const QMqttMessage &msg) {
if(msg.payload().compare("ON") == 0) {
doSend = true;
}
}
};
#endif // CMDBATTERYSELFTEST_H