-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1Monitor.cpp
160 lines (130 loc) · 3.47 KB
/
P1Monitor.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
#include "P1Monitor.h"
#include "EnergyMonitorConfig.h"
#include <iostream>
#include <ctime>
#include <unistd.h>
using namespace std;
P1Monitor::P1Monitor(const EnergyMonitorConfig& config) :
myIsTestmode(config.isTestMode())
{
}
P1Monitor::~P1Monitor()
{
deinit();
}
bool P1Monitor::init()
{
if (!myIsTestmode)
{
if (ftdi_init(&myFtdiContext) < 0)
{
cout << "ftdi_init failed" << endl;
return false;
}
if (ftdi_usb_open(&myFtdiContext, 0x0403, 0x6001) < 0)
{
cout << "ftdi_usb_open failed" << endl;
return false;
}
if (ftdi_set_baudrate(&myFtdiContext, 115200) < 0)
{
cout << "ftdi_set_baudrate failed" << endl;
return false;
}
if (ftdi_set_line_property2(&myFtdiContext, BITS_7, STOP_BIT_1, EVEN, BREAK_OFF) < 0)
{
cout << "ftdi_set_line_property2 failed" << endl;
return false;
}
myFtdiContext.usb_read_timeout = 50000;
ftdi_setdtr(&myFtdiContext, 0);
}
return true;
}
bool P1Monitor::deinit()
{
if (!myIsTestmode)
{
ftdi_setdtr(&myFtdiContext, 1);
if (ftdi_usb_close(&myFtdiContext) < 0)
{
cout << "ftdi_usb_close failed" << endl;
return false;
}
ftdi_deinit(&myFtdiContext);
}
return true;
}
bool P1Monitor::update(int timeout)
{
bool isUpdated = false;
if (!myIsTestmode)
{
time_t endTime = time(NULL) + timeout;
// Skip message in progress
time_t lastChar = time(NULL);
unsigned char buf[1];
while (time(NULL) - lastChar < 2 && time(NULL) < endTime)
{
if (ftdi_read_data(&myFtdiContext, buf, 1) > 0)
{
lastChar = time(NULL);
}
else
{
usleep(100);
}
}
// Wait for the message
while (time(NULL) < endTime && ftdi_read_data(&myFtdiContext, buf, 1) <= 0)
{
usleep(1000);
}
// Process the message
if (time(NULL) < endTime)
{
// Avoid timing out in the middle of the message
endTime += 5;
string line;
line += buf[0];
lastChar = time(NULL);
while (time(NULL) < endTime && time(NULL) - lastChar < 2)
{
if (ftdi_read_data(&myFtdiContext, buf, 1) > 0)
{
lastChar = time(NULL);
if (buf[0] >= 32 && buf[0] <= 126)
{
line += buf[0];
}
else if (!line.empty())
{
processLine(line);
line = "";
isUpdated = true;
}
}
else
{
usleep(10);
}
}
}
}
return isUpdated;
}
string P1Monitor::getValue(const string& id) const
{
TCurrentValueMap::const_iterator iter = myCurrentValueMap.find(id);
return (iter != myCurrentValueMap.end()) ? iter->second : string();
}
void P1Monitor::processLine(const string& line)
{
size_t pos = line.find_first_of('(');
if (pos != string::npos)
{
string id = line.substr(0, pos);
string value = line.substr(pos);
myCurrentValueMap[id] = value;
}
}