-
Notifications
You must be signed in to change notification settings - Fork 3
/
response_longlong.h
69 lines (54 loc) · 1.99 KB
/
response_longlong.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
#ifndef RESPOSE_LONGLONG_H
#define RESPOSE_LONGLONG_H
#include "iResponse.h"
class ResposeLongLong :public iResponse {
private:
QString devName;
QString valueName;
qlonglong value=0;
QString unit;
public:
ResposeLongLong(const QString devName, const QString valueName, QString unit) :
devName(devName),
valueName(valueName),
unit(unit) {
}
virtual void setValue(QByteArray newValue) {
value = newValue.toLongLong();
}
virtual QString getJsonKey() {
return devName + "_" + valueName;
}
virtual QJsonValue getJsonValue() {
return value;
}
virtual QString getAutodetectTopic() {
QString returnStr="homeassistant/sensor/" + devName + "/" + getJsonKey() + "/config";
returnStr.replace(QRegularExpression("\\s+"), "_");
return returnStr;
}
virtual QByteArray getAutodetectPalyoad(QString stateTopic, QString availTopic, QString commandTopic) {
QJsonObject recordObject;
recordObject.insert("unit_of_measurement", unit);
recordObject.insert("name", devName + " " + valueName);
recordObject.insert("unique_id", devName + "_" + valueName);
recordObject.insert("state_topic", stateTopic);
if(availTopic.length()>0) {
recordObject.insert("availability_topic", availTopic);
}
QString valueTemplate="{{ value_json.";
valueTemplate.append(getJsonKey());
valueTemplate.append("}}");
recordObject.insert("value_template", valueTemplate);
QJsonObject deviceObject;
deviceObject.insert("name", devName);
deviceObject.insert("identifiers", devName);
deviceObject.insert("sw_version", "0.0");
deviceObject.insert("model", "Infini Solar");
deviceObject.insert("manufacturer", "S/W Daniel Schramm");
recordObject.insert("device", deviceObject);
QJsonDocument doc(recordObject);
return(doc.toJson());
}
};
#endif // RESPOSE_LONGLONG_H