15 namespace ba = boost::asio;
16 namespace bs = boost::system;
17 namespace dummy = ba::placeholders;
37 const double lf = stof(l);
38 const uint32_t li = stoi(l);
40 const double min = fmod(lf, 100);
41 const double deg = li/100;
48 const double tf = stof(t);
49 const uint32_t ti = stoi(t);
51 const double h = ti/10000;
52 const double m = (ti/100)%100;
53 const double s = fmod(tf, 100);
55 return h/24 + m/1440 + s/86400;
60 if (buffer==
"Invalid command, type help")
62 Error(
"Command was ignored by GPS.");
67 if (buffer==
"veto_60" || buffer==
"veto 60 now on")
71 PostMessage(
string(
"get_nema\r\n"), 10);
74 if (buffer==
"veto_on" || buffer==
"veto now on")
77 PostMessage(
string(
"get_nema\r\n"), 10);
113 throw runtime_error(
"syntax error");
116 uint8_t c = cs[1][0];
117 for (
size_t i=1;
i<cs[1].size();
i++)
121 ss << std::hex << cs[2];
127 throw runtime_error(
"checksum error");
132 throw runtime_error(
"size mismatch");
134 throw runtime_error(
"type mismatch");
135 if (dat[5]!=
"W" && dat[5]!=
"E")
136 throw runtime_error(
"longitude type unknown");
138 throw runtime_error(
"height unit unknown");
140 throw runtime_error(
"hdop unit unknown");
141 if (!dat[13].empty())
142 throw runtime_error(
"unexpected data at position 13");
144 throw runtime_error(
"unexpected data at position 14");
147 nema.
time = ConvTm(dat[1]);
148 nema.
lat = dat[3]==
"N" ? ConvLngLat(dat[2]) : -ConvLngLat(dat[3]);
149 nema.
lng = dat[5]==
"W" ? ConvLngLat(dat[4]) : -ConvLngLat(dat[4]);
150 nema.
qos = stoi(dat[6]);
151 nema.
count = stoi(dat[7]);
152 nema.
hdop = stof(dat[8]);
153 nema.
height = stof(dat[9]);
154 nema.
geosep = stof(dat[11]);
156 if (fabs(nema.
time-fmod(
Time().Mjd(), 1))*24*3600>5)
158 Error(
"Time mismatch: GPS time deviates from PC time by more than 5s");
173 void HandleRead(
const boost::system::error_code& err,
size_t bytes_received)
176 if (bytes_received==0 || err)
178 if (err==ba::error::eof)
179 Warn(
"Connection closed by remote host.");
183 if (err && err!=ba::error::eof &&
184 err!=ba::error::basic_errors::not_connected &&
185 err!=ba::error::basic_errors::operation_aborted)
188 str <<
"Reading from " << URL() <<
": " << err.message() <<
" (" << err <<
")";
191 PostClose(err!=ba::error::basic_errors::operation_aborted);
195 istream is(&fBuffer);
198 if (!getline(is, buffer,
'\n'))
200 Error(
"Received message does not contain \\n... closing connection.");
204 buffer = buffer.substr(0, buffer.size()-1);
207 Out() << buffer << endl;
211 if (!ParseAnswer(buffer))
213 Error(
"Received: "+buffer);
218 catch (
const exception &e)
220 Error(
"Parsing NEMA message failed ["+
string(e.what())+
"]");
221 Error(
"Received: "+buffer);
226 fLastReport =
Time();
234 async_read_until(*
this, fBuffer,
'\n',
236 dummy::error, dummy::bytes_transferred));
244 if (error && error!=ba::error::basic_errors::operation_aborted)
247 str <<
"Write timeout of " << URL() <<
": " << error.message() <<
" (" << error <<
")";
266 if (fKeepAlive.expires_at() > ba::deadline_timer::traits_type::now())
269 PostMessage(
string(
"get_status\r\n"), 12);
289 fIsVerbose(true), fLastReport(
Time::none), fKeepAlive(ioservice)
305 mjd = (ceil(mjd*24*60+0.01)+0.5)/(24*60);
307 fKeepAlive.expires_at(
Time(mjd));
309 this, dummy::error));
338 fDim(
"GPS_CONTROL/NEMA",
"F:1;F:1;F:1;F:1;F:1;F:1;S:1;S:1",
339 "NEMA message from the GPS module" 340 "|time[utc]:Time of day as fraction of day (UTC)" 342 "|long[deg]:Longitude" 343 "|hdop:Horizontal delution of precision" 344 "|height[m]:Antenna altitude above mean sea level (geoid)" 345 "|geosep[m]:Geoidal sep.(Diff. between WGS-84 earth ellipsoid and mean sea lvl)" 346 "|count:Number of satellites in use (not those in view)" 347 "|quality:GPS quality indicator (see Venus manual)")
358 template <
class T,
class S>
371 msg << name <<
" - Received event has " << has <<
" bytes, but expected " << size <<
".";
379 fGPS.PostClose(
false);
381 return T::GetCurrentState();
387 fGPS.PostClose(
false);
391 ba::io_service::poll();
397 fGPS.PostClose(
true);
399 return T::GetCurrentState();
404 if (!CheckEventSize(evt.
GetSize(),
"SetVerbosity", 1))
405 return T::kSM_FatalError;
407 fGPS.SetVerbose(evt.
GetBool());
409 return T::GetCurrentState();
414 const string tx = cmd+
"\r\n";
415 fGPS.PostMessage(tx, tx.size());
416 return T::GetCurrentState();
426 return fGPS.GetState();
436 "No connection to web-server could be established recently");
439 "Connection established, but status still not known");
442 "Veto is on, no trigger will be emitted");
445 "System enabled, waiting for satellites");
448 "One trigger per second will be send, but the one at the exact minute is vetoed");
451 T::AddEvent(
"SEND_COMMAND",
"C")
453 (
"Send command to GPS");
456 T::AddEvent(
"SET_VERBOSE",
"B")
458 (
"set verbosity state" 459 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
461 T::AddEvent(
"ENABLE")
463 (
"Enable trigger signal once a second vetoed at every exact minute");
465 T::AddEvent(
"DISABLE")
467 (
"Diable trigger output");
470 T::AddEvent(
"DISCONNECT")
472 (
"disconnect from ethernet");
474 T::AddEvent(
"RECONNECT",
"O")
476 (
"(Re)connect ethernet connection to GPS, a new address can be given" 477 "|[host][string]:new ethernet address in the form <host:port>");
483 fGPS.SetVerbose(!conf.
Get<
bool>(
"quiet"));
484 fGPS.SetDebugTx(conf.
Get<
bool>(
"debug-tx"));
485 fGPS.SetEndpoint(conf.
Get<
string>(
"addr"));
497 template<
class T,
class S,
class R>
500 return Main::execute<T, StateMachineGPSControl<S, R>>(conf);
505 po::options_description control(
"GPS control");
506 control.add_options()
507 (
"no-dim,d",
po_switch(),
"Disable dim services")
508 (
"addr,a", var<string>(
"gps:23"),
"Network address of the lid controling Arduino including port")
509 (
"quiet,q",
po_bool(
true),
"Disable printing contents of all received messages (except dynamic data) in clear text.")
510 (
"debug-tx",
po_bool(),
"Enable debugging of ethernet transmission.")
528 "The gpsctrl is an interface to the GPS hardware.\n" 530 "The default is that the program is started without user intercation. " 531 "All actions are supposed to arrive as DimCommands. Using the -c " 532 "option, a local shell can be initialized. With h or help a short " 533 "help message about the usuage can be brought to the screen.\n" 535 "Usage: gpsctrl [-c type] [OPTIONS]\n" 536 " or: gpsctrl [OPTIONS]\n";
562 int main(
int argc,
const char* argv[])
573 if (!conf.
Has(
"console"))
575 if (conf.
Get<
bool>(
"no-dim"))
576 return RunShell<LocalStream, StateMachine, ConnectionGPS>(conf);
578 return RunShell<LocalStream, StateMachineDim, ConnectionDimWeather>(conf);
581 if (conf.
Get<
bool>(
"no-dim"))
583 if (conf.
Get<
int>(
"console")==0)
584 return RunShell<LocalShell, StateMachine, ConnectionGPS>(conf);
586 return RunShell<LocalConsole, StateMachine, ConnectionGPS>(conf);
590 if (conf.
Get<
int>(
"console")==0)
591 return RunShell<LocalShell, StateMachineDim, ConnectionDimWeather>(conf);
593 return RunShell<LocalConsole, StateMachineDim, ConnectionDimWeather>(conf);
float ConvLngLat(const string &l) const
ConnectionGPS(ba::io_service &ioservice, MessageImp &imp)
void Update(const GPS::NEMA &nema)
static const uint16_t kMaxAddr
boost::asio::streambuf fBuffer
A general base-class describing events issues in a state machine.
int RunShell(Configuration &conf)
int Reconnect(const EventImp &evt)
void SetupConfiguration(Configuration &conf)
The base implementation of a distributed messaging system.
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)
po::typed_value< bool > * po_switch()
void SetupConfiguration(Configuration &conf)
std::string GetString() const
int Send(const string &cmd)
void Request(bool immediate=false)
bool Has(const std::string &var)
void AddOptions(const po::options_description &opt, bool visible=true)
StateMachineGPSControl(ostream &out=cout)
bool ParseAnswer(const string &buffer)
int main(int argc, const char *argv[])
float ConvTm(const string &t) const
void SetVerbose(bool b=true)
int SetVerbosity(const EventImp &evt)
Commandline parsing, resource file parsing and database access.
int EvalOptions(Configuration &conf)
int SendCommand(const EventImp &evt)
bool CheckEventSize(size_t has, const char *name, size_t size)
Error states should be between 0x100 and 0xffff.
virtual void Update(const GPS::NEMA &)
void ConnectionEstablished()
po::typed_value< bool > * po_bool(bool def=false)
void HandleRequest(const bs::error_code &error)
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
ConnectionDimWeather(ba::io_service &ioservice, MessageImp &imp)
boost::asio::deadline_timer fKeepAlive
void HandleRead(const boost::system::error_code &err, size_t bytes_received)
virtual size_t GetSize() const