-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomematic.cpp
119 lines (99 loc) · 2.51 KB
/
homematic.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
#include "homematic.h"
#include "sensorlogger.h"
#include "logger.h"
homematic::homematic(logger*root)
{
_root = root;
}
homematic::~homematic()
{
}
void homematic::setXMLAPI_URL(const std::string &xml_api_url)
{
_xmlAPI_URL = xml_api_url;
}
std::string homematic::getXMLAPI_URL() const
{
return _xmlAPI_URL;
}
void homematic::publish(const std::string &iseID, const std::string &payload) const
{
if(_xmlAPI_URL.size() > 0)
{
std::string publishURL = _xmlAPI_URL + "/statechange.cgi?ise_id=" + iseID + "&new_value=" + payload;
try
{
_root->httpRequest(publishURL);
}
catch(int e)
{
_root->error("Cannot publish to Homematic ISE " + iseID + ".");
}
}
}
std::string homematic::getValue(const std::string &iseID) const
{
if(_xmlAPI_URL.size() > 0)
{
std::string getterURL = _xmlAPI_URL + "/state.cgi?datapoint_id=" + iseID;
try
{
std::string sysvarText = _root->httpRequest(getterURL);
std::string valueKey = "value=";
size_t valuePos = sysvarText.find(valueKey, 0);
if(valuePos != std::string::npos)
{
size_t stopPos = 0;
valuePos += valueKey.size();
if(sysvarText.size() > valuePos)
{
char quotationMark = sysvarText.at(valuePos);
valuePos++;
if(sysvarText.size() > valuePos)
{
// Find next true quotation mark:
stopPos = valuePos;
bool skippedEscaped = false;
for(size_t i=valuePos; i<sysvarText.size(); ++i)
{
if((sysvarText.at(i) == '\\') && !skippedEscaped)
{
skippedEscaped = true;
i+=1;
continue;
}
skippedEscaped = false;
if(sysvarText.at(i) == quotationMark)
{
stopPos = i;
break;
}
}
}
}
if(stopPos > valuePos)
{
return sysvarText.substr(valuePos, stopPos-valuePos);
}
else if(stopPos == valuePos)
{
// Empty result will be converted to 0 by the sensor.
return "";
}
}
else
{
_root->error("Cannot measure HomeMatic system variable with ISE ID " + iseID + ". Reason: no value=\'\' key in the string returned from the HomeMatic XML API. Requested URL: " + getterURL);
throw E_CANNOT_READ_HM_SYSVAR;
}
}
catch(int e)
{
if(e != E_CANNOT_READ_HM_SYSVAR)
_root->error("Cannot measure HomeMatic system variable with ISE ID " + iseID + ". Requested URL: " + getterURL);
throw E_CANNOT_READ_HM_SYSVAR;
}
}
_root->error("Cannot measure HomeMatic system variable with ISE ID " + iseID + ". XML API URL: " + _xmlAPI_URL);
throw E_CANNOT_READ_HM_SYSVAR;
}