-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOmnikGetStats.cpp
161 lines (130 loc) · 3.58 KB
/
OmnikGetStats.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
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
#include "OmnikGetStats.h"
#include "EnergyMonitorConfig.h"
#include <cstring>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <arpa/inet.h>
#include <errno.h>
using namespace std;
namespace
{
const string cOmnikSerialNumberConfigName = "OmnikSN";
const string cOmnikIPConfigName = "OmnikIP";
const int cOmnikPortNumber = 8899;
const int cUpdateInterval = 300;
}
OmnikGetStats::OmnikGetStats(const EnergyMonitorConfig& config) :
myOmnikTemperature(.0),
myOmnikPower(.0),
myOmnikGeneratedToday(.0),
myOmnikGeneratedTotal(.0),
myOmnikTotalHours(.0),
myLastUpdateTime(0)
{
myOmnikSerialNumber = atol(config.getValue(cOmnikSerialNumberConfigName).c_str());
myOmnikIPAddress = config.getValue(cOmnikIPConfigName);
}
bool OmnikGetStats::updateStats()
{
bool result = false;
if (time(NULL) > (myLastUpdateTime + cUpdateInterval))
{
result = getStats();
if (result)
{
parseStats();
}
myLastUpdateTime = time(NULL);
}
return result;
}
bool OmnikGetStats::getStats()
{
// generate the magic message
// first 4 bytes are fixed x68 x02 x40 x30
// next 8 bytes are the reversed serial number twice(hex)
// next 2 bytes are fixed x01 x00
// next byte is a checksum (2x each binary number form the serial number + 115)
// last byte is fixed x16
char magicMessage[] = {0x68, 0x02, 0x40, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x16};
int checksum = 0;
for (int i = 0; i < 4; ++i)
{
magicMessage[i + 4] = magicMessage[i + 8] = ((myOmnikSerialNumber >> (8 * i)) & 0xff);
checksum += magicMessage[i + 4];
}
checksum *= 2;
checksum += 115;
checksum &= 0xff;
magicMessage[14] = checksum;
// Now create a TCP socket for communication with the Omnik
int socket_desc;
if ((socket_desc = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
cout << "Could not create TCP socket" << endl;
return false;
}
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr(myOmnikIPAddress.c_str());
server.sin_family = AF_INET;
server.sin_port = htons(cOmnikPortNumber);
//Connect to remote server
if (connect(socket_desc, (struct sockaddr*)&server, sizeof(server)) < 0)
{
cout << "TCP connect error: " << errno << endl;
return false;
}
//Send the magic message
if (send(socket_desc, magicMessage, 16, 0) < 0)
{
cout << "Send failed" << endl;
return false;
}
//Receive a reply from the server
memset(myOmnikReply, 0, sizeof(myOmnikReply));
if (recv(socket_desc, myOmnikReply, sizeof(myOmnikReply), 0) < 0)
{
cout << "recv failed" << endl;
return false;
}
return true;
}
void OmnikGetStats::parseStats()
{
myOmnikTemperature = ctonr(&myOmnikReply[31], 2, 10);
myOmnikGeneratedToday = ctonr(&myOmnikReply[69], 2, 100);
myOmnikGeneratedTotal = ctonr(&myOmnikReply[71], 4, 10);
myOmnikTotalHours = ctonr(&myOmnikReply[75], 4, 1);
myOmnikPower = .0;
for (int i = 0; i < 3; ++i)
{
if (ctonr(&myOmnikReply[33 + 2 * i], 2, 1) != 0)
{
myOmnikPower += ctonr(&myOmnikReply[59 + 2 * i], 2, 1);
}
}
}
double OmnikGetStats::ctonr(unsigned char* src, int nrofbytes, int div)
{
if (nrofbytes <= 0 || nrofbytes > 4)
{
return -1;
}
double result = .0;
int flag = 0;
for (int i = nrofbytes; i > 0; --i)
{
result += (double)(src[i - 1] * pow(256, nrofbytes - i));
if (src[i - 1] == 0xff)
{
flag++;
}
}
if (flag == nrofbytes) // all oxff
{
result = 0;
}
result /= (double)div;
return result;
}