Repository navigation
Expand file tree
/
Copy pathKMTronicTCP.cpp
More file actions
184 lines (161 loc) · 4 KB
/
Copy pathKMTronicTCP.cpp
File metadata and controls
184 lines (161 loc) · 4 KB
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
#include "stdafx.h"
#include "KMTronicTCP.h"
#include "../main/Logger.h"
#include "../main/Helper.h"
#include <iostream>
#include "../main/localtime_r.h"
#include "../main/mainworker.h"
#include "../httpclient/HTTPClient.h"
#include "../httpclient/UrlEncode.h"
#define KMTRONIC_POLL_INTERVAL 10
KMTronicTCP::KMTronicTCP(const int ID, const std::string &IPAddress, const unsigned short usIPPort, const std::string &username, const std::string &password) :
m_szIPAddress(IPAddress),
m_Username(CURLEncode::URLEncode(username)),
m_Password(CURLEncode::URLEncode(password))
{
m_HwdID=ID;
m_stoprequested=false;
m_usIPPort=usIPPort;
}
KMTronicTCP::~KMTronicTCP(void)
{
}
void KMTronicTCP::Init()
{
}
bool KMTronicTCP::StartHardware()
{
Init();
//Start worker thread
m_thread = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&KMTronicTCP::Do_Work, this)));
m_bIsStarted = true;
sOnConnected(this);
_log.Log(LOG_STATUS, "KMTronic: Started");
return (m_thread != NULL);
}
bool KMTronicTCP::StopHardware()
{
if (m_thread != NULL)
{
assert(m_thread);
m_stoprequested = true;
m_thread->join();
}
m_bIsStarted = false;
return true;
}
void KMTronicTCP::Do_Work()
{
int sec_counter = KMTRONIC_POLL_INTERVAL - 2;
while (!m_stoprequested)
{
sleep_seconds(1);
sec_counter++;
if (sec_counter % 12 == 0) {
m_LastHeartbeat=mytime(NULL);
}
if (sec_counter % KMTRONIC_POLL_INTERVAL == 0)
{
GetMeterDetails();
}
}
_log.Log(LOG_STATUS, "KMTronic: TCP/IP Worker stopped...");
}
bool KMTronicTCP::WriteToHardware(const char *pdata, const unsigned char length)
{
const tRBUF *pSen = reinterpret_cast<const tRBUF*>(pdata);
unsigned char packettype = pSen->ICMND.packettype;
//unsigned char subtype = pSen->ICMND.subtype;
if (packettype == pTypeLighting2)
{
//light command
int Relay = pSen->LIGHTING2.id4;
if (Relay > Max_KMTronic_Relais)
return false;
std::stringstream szURL;
if (m_Password.empty())
{
szURL << "http://" << m_szIPAddress << ":" << m_usIPPort;
}
else
{
szURL << "http://" << m_Username << ":" << m_Password << "@" << m_szIPAddress << ":" << m_usIPPort;
}
//toggle= //szURL << "/relays.cgi?relay=" << Relay;
if (pSen->LIGHTING2.cmnd == light2_sOff)
{
szURL << "/FF0" << Relay << "00";
}
else
{
szURL << "/FF0" << Relay << "01";
}
std::string sResult;
if (!HTTPClient::GET(szURL.str(), sResult))
{
_log.Log(LOG_ERROR, "KMTronic: Error sending relay command to: %s", m_szIPAddress.c_str());
return false;
}
return true;
}
return false;
}
bool KMTronicTCP::WriteInt(const unsigned char *data, const size_t len, const bool bWaitForReturn)
{
return true;
}
void KMTronicTCP::GetMeterDetails()
{
std::string sResult;
std::stringstream szURL;
if (m_Password.empty())
{
szURL << "http://" << m_szIPAddress << ":" << m_usIPPort;
}
else
{
szURL << "http://" << m_Username << ":" << m_Password << "@" << m_szIPAddress << ":" << m_usIPPort;
}
szURL << "/relays.cgi";
if (!HTTPClient::GET(szURL.str(), sResult))
{
_log.Log(LOG_ERROR, "KMTronic: Error connecting to: %s", m_szIPAddress.c_str());
return;
}
std::vector<std::string> results;
StringSplit(sResult, "\r\n", results);
if (results.size()<8)
{
_log.Log(LOG_ERROR, "KMTronic: Invalid data received");
return;
}
size_t ii,jj;
std::string tmpstr;
for (ii = 1; ii < results.size(); ii++)
{
tmpstr = results[ii];
if (tmpstr.find("Status:") != std::string::npos)
{
tmpstr = tmpstr.substr(strlen("Status:"));
std::vector<std::string> results2;
StringSplit(tmpstr, " ", results2);
if (results2.size() < 2)
{
_log.Log(LOG_ERROR, "KMTronic: Invalid data received");
return;
}
for (jj = 0; jj < results2.size(); jj++)
{
bool bIsOn = (results2[jj] != "0");
std::stringstream sstr;
int iRelay = (jj + 1);
sstr << "Relay " << iRelay;
SendSwitch(iRelay, 1, 255, bIsOn, 0, sstr.str());
if (iRelay > m_TotRelais)
m_TotRelais = iRelay;
}
return;
}
}
_log.Log(LOG_ERROR, "KMTronic: Invalid data received");
}