15 namespace ba = boost::asio;
16 namespace bs = boost::system;
17 namespace dummy = ba::placeholders;
41 if (bytes_received==0 || (err && err!=ba::error::eof))
45 if (err && err!=ba::error::eof &&
46 err!=ba::error::basic_errors::not_connected &&
47 err!=ba::error::basic_errors::operation_aborted)
50 str <<
"Reading from " << URL() <<
": " << err.message() <<
" (" << err <<
")";
58 const uint16_t chk1 = uint16_t(fBuffer[2]);
63 out <<
"Checksum error (";
64 out << hex << setfill(
'0');
65 out << setw(4) << fBuffer[0] <<
":";
66 out << setw(4) << fBuffer[1] <<
"|";
67 out << setw(4) << fBuffer[2] <<
"!=";
68 out << setw(4) << chk1 <<
")";
78 data.
hum = 110*fBuffer[0]/1024.;
79 data.
temp = 110*fBuffer[1]/1024.-20;
84 msg << fixed << setprecision(1) <<
"H=" << data.
hum <<
"% T=" << data.
temp <<
"°C" ;
96 if (error && error!=ba::error::basic_errors::operation_aborted)
99 str <<
"Timeout of " << URL() <<
": " << error.message() <<
" (" << error <<
")";
110 if (fKeepAlive.expires_at() > ba::deadline_timer::traits_type::now())
120 if (error && error!=ba::error::basic_errors::operation_aborted)
123 str <<
"Read timeout of " << URL() <<
" timed out: " << error.message() <<
" (" << error <<
")";
138 string cmd =
"GET / HTTP/1.1\r\n\r\n";
145 fKeepAlive.expires_from_now(boost::posix_time::seconds(fInterval));
147 this, dummy::error));
165 fIsVerbose(true), fKeepAlive(ioservice)
204 fDim(
"PFMINI_CONTROL/DATA",
"F:1;F:1",
205 "Humidity and temperature as read out from the PFmini arduino" 206 "|Humidity[%]:Measures humidity" 207 "|Temperature[deg]:Measured temperature")
219 template <
class T,
class S>
232 msg << name <<
" - Received event has " << has <<
" bytes, but expected " << size <<
".";
240 fPFmini.PostClose(
false);
242 return T::GetCurrentState();
248 fPFmini.PostClose(
false);
252 ba::io_service::poll();
258 fPFmini.PostClose(
true);
260 return T::GetCurrentState();
265 if (!CheckEventSize(evt.
GetSize(),
"SetVerbosity", 1))
266 return T::kSM_FatalError;
268 fPFmini.SetVerbose(evt.
GetBool());
270 return T::GetCurrentState();
275 return fPFmini.GetState();
285 "No connection to web-server could be established recently");
288 "Connection established, but status still not known");
291 "Connection established, receiving reports");
295 T::AddEvent(
"SET_VERBOSE",
"B")
297 (
"set verbosity state" 298 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
301 T::AddEvent(
"DISCONNECT")
303 (
"disconnect from ethernet");
305 T::AddEvent(
"RECONNECT",
"O")
307 (
"(Re)connect ethernet connection to PFmini, a new address can be given" 308 "|[host][string]:new ethernet address in the form <host:port>");
314 fPFmini.SetVerbose(!conf.
Get<
bool>(
"quiet"));
315 fPFmini.SetDebugTx(conf.
Get<
bool>(
"debug-tx"));
316 fPFmini.SetEndpoint(conf.
Get<
string>(
"addr"));
317 fPFmini.SetInterval(conf.
Get<uint16_t>(
"interval"));
318 fPFmini.StartConnect();
329 template<
class T,
class S,
class R>
332 return Main::execute<T, StateMachinePFminiControl<S, R>>(conf);
337 po::options_description control(
"PFmini control");
338 control.add_options()
339 (
"no-dim,d",
po_switch(),
"Disable dim services")
340 (
"addr,a", var<string>(
"10.0.130.140:80"),
"Network address of the lid controling Arduino including port")
341 (
"quiet,q",
po_bool(
true),
"Disable printing contents of all received messages (except dynamic data) in clear text.")
342 (
"debug-tx",
po_bool(),
"Enable debugging of ethernet transmission.")
343 (
"interval", var<uint16_t>(15),
"Interval in seconds at which a report is requested.")
361 "The pfminictrl is an interface to the PFmini arduino.\n" 363 "The default is that the program is started without user intercation. " 364 "All actions are supposed to arrive as DimCommands. Using the -c " 365 "option, a local shell can be initialized. With h or help a short " 366 "help message about the usuage can be brought to the screen.\n" 368 "Usage: pfminictrlö [-c type] [OPTIONS]\n" 369 " or: pfminictrl [OPTIONS]\n";
395 int main(
int argc,
const char* argv[])
406 if (!conf.
Has(
"console"))
408 if (conf.
Get<
bool>(
"no-dim"))
409 return RunShell<LocalStream, StateMachine, ConnectionPFmini>(conf);
411 return RunShell<LocalStream, StateMachineDim, ConnectionDimWeather>(conf);
414 if (conf.
Get<
bool>(
"no-dim"))
416 if (conf.
Get<
int>(
"console")==0)
417 return RunShell<LocalShell, StateMachine, ConnectionPFmini>(conf);
419 return RunShell<LocalConsole, StateMachine, ConnectionPFmini>(conf);
423 if (conf.
Get<
int>(
"console")==0)
424 return RunShell<LocalShell, StateMachineDim, ConnectionDimWeather>(conf);
426 return RunShell<LocalConsole, StateMachineDim, ConnectionDimWeather>(conf);
virtual void Update(const PFmini::Data &)
int SetVerbosity(const EventImp &evt)
A general base-class describing events issues in a state machine.
void SetupConfiguration(Configuration &conf)
void HandleRequest(const bs::error_code &error)
The base implementation of a distributed messaging system.
StateMachinePFminiControl(ostream &out=cout)
Adds some functionality to boost::posix_time::ptime for our needs.
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
void Update(const PFmini::Data &data)
po::typed_value< bool > * po_switch()
static const uint16_t kMaxAddr
std::string GetString() const
int main(int argc, const char *argv[])
boost::asio::deadline_timer fKeepAlive
void SetupConfiguration(Configuration &conf)
bool Has(const std::string &var)
int Reconnect(const EventImp &evt)
void SetInterval(uint16_t i)
void AddOptions(const po::options_description &opt, bool visible=true)
void ConnectionEstablished()
bool CheckEventSize(size_t has, const char *name, size_t size)
void SetVerbose(bool b=true)
void HandleReadTimeout(const bs::error_code &error)
vector< int16_t > fBuffer
Commandline parsing, resource file parsing and database access.
void HandleReceivedData(const bs::error_code &err, size_t bytes_received, int)
virtual void HandleReadTimeout(const boost::system::error_code &)
int EvalOptions(Configuration &conf)
po::typed_value< bool > * po_bool(bool def=false)
ConnectionPFmini(ba::io_service &ioservice, MessageImp &imp)
int RunShell(Configuration &conf)
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
ConnectionDimWeather(ba::io_service &ioservice, MessageImp &imp)
virtual size_t GetSize() const