-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlowMeter.cpp
161 lines (138 loc) · 4.74 KB
/
FlowMeter.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 "FlowMeter.hpp"
#include <syslog.h>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <time.h>
using namespace std;
FlowMeter::FlowMeter(): ios(), meter_port(ios){
running = false;
}
FlowMeter::~FlowMeter(){
this->Disconnect(); // Run this in case the sensor is left connected
}
void FlowMeter::setRunning(bool value){
runMutex.lock();
this->running = value;
runMutex.unlock();
}
bool FlowMeter::isRunning(){
bool retVal;
runMutex.lock();
retVal = running;
runMutex.unlock();
return retVal;
}
bool FlowMeter::Connect(){
if(!this->isRunning()){
syslog(LOG_DAEMON|LOG_INFO,"Connecting flow meter");
try{
meter_port.open("/dev/ttyUSB0");
meter_port.set_option(boost::asio::serial_port_base::baud_rate(9600));
meter_port.set_option(boost::asio::serial_port_base::character_size(8));
boost::asio::async_read_until(meter_port,buf,"\r\n",boost::bind(&FlowMeter::parseLine,this,boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
readThread = new boost::thread(boost::ref(*this));
this->setRunning(true);
syslog(LOG_DAEMON|LOG_INFO,"Flow Meter Connected");
} catch (std::exception& e){
syslog(LOG_DAEMON|LOG_ERR,"Unable to connect to flow meter. Error: %s",e.what());
}
} else {
syslog(LOG_DAEMON|LOG_INFO,"Flow meter already running");
}
}
void FlowMeter::parseLine(const boost::system::error_code& ec, size_t bytes_transferred){
time_t timestamp = time(NULL);
if(!ec){
runMutex.lock();
if(running){
// Parse Line Here
istream is(&buf);
string line;
getline(is,line);
unsigned int i=0;
boost::char_separator<char> sep(",", "", boost::drop_empty_tokens);
boost::tokenizer< boost::char_separator<char> > tokens(line, sep);
int counts = 0;
double flowRate = 0;
bool error = false;
BOOST_FOREACH(string t, tokens){
boost::algorithm::trim(t);
if(i == 0){
try{
counts = boost::lexical_cast<int>(t);
} catch( boost::bad_lexical_cast const &){
syslog(LOG_DAEMON|LOG_ERR, "Unable to convert flow meter counts");
error = true;
}
} else if (i == 1){
try{
flowRate = boost::lexical_cast<double>(t);
} catch( boost::bad_lexical_cast const &){
syslog(LOG_DAEMON|LOG_ERR, "Unable to convert flow rate");
error = true;
}
}
i++;
}
// Pass Reading to Handlers
if(!error){
listenersMutex.lock();
BOOST_FOREACH(IFlowMeterListener* l, listeners){
l->processFlowReading(timestamp, counts, flowRate);
}
listenersMutex.unlock();
}
// Schedule next request
boost::asio::async_read_until(meter_port,buf,"\r\n",boost::bind(&FlowMeter::parseLine,this,boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
} else {
ios.stop();
}
runMutex.unlock();
} else {
syslog(LOG_DAEMON|LOG_ERR, "Error communicating with flow meter");
}
}
bool FlowMeter::Disconnect(){
if(this->isRunning()){
this->setRunning(false);
try{
meter_port.close();
} catch (std::exception& e){
syslog(LOG_DAEMON|LOG_ERR,"Unable to disconnect flow meter. Error: %s",e.what());
}
if(readThread) readThread->join();
syslog(LOG_DAEMON|LOG_INFO,"Flow meter disconnected");
}
}
void FlowMeter::addListener(IFlowMeterListener *l){
listenersMutex.lock();
listeners.push_back(l);
listenersMutex.unlock();
}
void FlowMeter::clearListeners(){
listenersMutex.lock();
BOOST_FOREACH(IFlowMeterListener* l, listeners){
l->flowMeterStopping();
}
listeners.clear();
listenersMutex.unlock();
}
void FlowMeter::operator() (){
listenersMutex.lock();
BOOST_FOREACH(IFlowMeterListener* l, listeners){
l->flowMeterStarting();
}
listenersMutex.unlock();
ios.reset();
ios.run();
listenersMutex.lock();
BOOST_FOREACH(IFlowMeterListener* l, listeners){
l->flowMeterStopping();
}
listenersMutex.unlock();
}