-
Notifications
You must be signed in to change notification settings - Fork 3
/
p18_query_total_energy.h
64 lines (54 loc) · 1.59 KB
/
p18_query_total_energy.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
#ifndef P18_TOTAL_ENERGY_H
#define P18_TOTAL_ENERGY_H
#include <QString>
#include <QByteArray>
#include <QtDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <icmdquery.h>
#include "response_longlong.h"
class P18QueryTotalEnergy : public iCmdQuery
{
private:
bool isValidFormat(QByteArray response) {
bool result=true;
if(! response.endsWith("\r"))
result=false;
if(! response.startsWith("^D011"))
result=false;
Crc crc;
if(! crc.checkResponse(response))
result=false;
return result;
}
public:
P18QueryTotalEnergy(QString devName) : iCmdQuery("TotalEnergy", devName) {
responseList.append(new ResposeLongLong(devName, "TotalEnergy", "Wh"));
}
virtual QByteArray getCmd() {
QByteArray cmd="^P005ET";
Crc crc;
crc.appendCrc(cmd);
cmd.append('\r');
qDebug() << cmd;
return(cmd);
}
virtual QByteArray resultToJson(QByteArray response) {
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() << "wrong Format";
return("");
}
return("");
}
};
#endif // P18_TOTAL_ENERGY_H