source: trunk/FACT++/src/tngweather.cc@ 19808

Last change on this file since 19808 was 19711, checked in by tbretz, 5 years ago
Removed an obsolete line from the request.
File size: 20.1 KB
Line 
1#include <boost/array.hpp>
2
3#include <string> // std::string
4#include <algorithm> // std::transform
5#include <cctype> // std::tolower
6
7#include "FACT.h"
8#include "Dim.h"
9#include "Event.h"
10#include "Shell.h"
11#include "StateMachineDim.h"
12#include "StateMachineAsio.h"
13#include "ConnectionSSL.h"
14#include "LocalControl.h"
15#include "Configuration.h"
16#include "Timers.h"
17#include "Console.h"
18
19#include "tools.h"
20
21#include "HeadersTNGWeather.h"
22
23#include <QtXml/QDomDocument>
24
25namespace ba = boost::asio;
26namespace bs = boost::system;
27namespace dummy = ba::placeholders;
28
29using namespace std;
30using namespace TNGWeather;
31
32
33class ConnectionWeather : public ConnectionSSL
34{
35 uint16_t fInterval;
36
37 bool fIsVerbose;
38
39 string fSite;
40
41 virtual void UpdateWeather(const Time &, const DimWeather &)
42 {
43 }
44
45 virtual void UpdateSeeing(const Time &, const DimSeeing &)
46 {
47 }
48
49 virtual void UpdateDust(const Time &, const float &)
50 {
51 }
52
53 string fRdfData;
54 float fDust;
55
56protected:
57
58 boost::array<char, 4096> fArray;
59
60 Time fLastReport;
61 Time fLastReception;
62
63 Time fLastSeeing;
64
65 void HandleRead(const boost::system::error_code& err, size_t bytes_received)
66 {
67 // Do not schedule a new read if the connection failed.
68 if (bytes_received==0 || err)
69 {
70 if (err==ba::error::eof)
71 Warn("Connection closed by remote host.");
72
73 // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
74 // 125: Operation canceled
75 if (err && err!=ba::error::eof && // Connection closed by remote host
76 err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
77 err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
78 {
79 ostringstream str;
80 str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
81 Error(str);
82 }
83 PostClose(err!=ba::error::basic_errors::operation_aborted);
84
85 fRdfData = "";
86 return;
87 }
88
89 fRdfData += string(fArray.data(), bytes_received);
90
91 const size_t end = fRdfData.find("\r\n\r\n");
92 if (end==string::npos)
93 {
94 Out() << "Received data corrupted [1]." << endl;
95 Out() << fRdfData << endl;
96 return;
97 }
98
99 string data(fRdfData);
100 data.erase(0, end+4);
101
102 size_t pos = 0;
103 while (1)
104 {
105 const size_t chunk = data.find("\r\n", pos);
106 if (chunk==0 || chunk==string::npos)
107 {
108 StartReadReport();
109 return;
110 }
111
112 size_t len = 0;
113 stringstream val(data.substr(pos, chunk-pos));
114 val >> hex >> len;
115
116 data.erase(pos, chunk-pos+2);
117 if (len==0)
118 break;
119
120 pos += len+2; // Count trailing \r\n of chunk
121 }
122
123
124 fLastReception = Time();
125 fRdfData = "";
126 PostClose(false);
127
128 if (fIsVerbose)
129 {
130 Out() << "------------------------------------------------------" << endl;
131 Out() << data << endl;
132 Out() << "------------------------------------------------------" << endl;
133 }
134
135 QDomDocument doc;
136 if (!doc.setContent(QString(data.data()), false))
137 {
138 Warn("Parsing of xml failed [0].");
139 PostClose(false);
140 return;
141 }
142
143 if (fIsVerbose)
144 Out() << "Parsed:\n-------\n" << doc.toString().toStdString() << endl;
145
146 const QDomElement root = doc.documentElement();
147 const QDomElement channel = root.firstChildElement("channel");
148 const QDomElement item = channel.firstChildElement("item");
149
150 const QDomElement see = item.firstChildElement("tngw:dimmSeeing");
151 const QDomElement mjd = item.firstChildElement("tngw:dimmSeeing.date");
152 const QDomElement med = item.firstChildElement("tngw:dimmSeeing.median");
153 const QDomElement sdev = item.firstChildElement("tngw:dimmSeeing.stdev");
154 const QDomElement dust = item.firstChildElement("tngw:dustTotal");
155 const QDomElement trend = item.firstChildElement("tngw:trend");
156 const QDomElement pres = item.firstChildElement("tngw:airPressure");
157 const QDomElement dew = item.firstChildElement("tngw:dewPoint");
158 const QDomElement wdir = item.firstChildElement("tngw:windDirection");
159 const QDomElement speed = item.firstChildElement("tngw:windSpeed");
160 const QDomElement hum = item.firstChildElement("tngw:hum");
161 const QDomElement tmp = item.firstChildElement("tngw:temperature");
162 const QDomElement solar = item.firstChildElement("tngw:solarimeter");
163 const QDomElement date = item.firstChildElement("tngw:date");
164
165 if (see.isNull() || mjd.isNull() || med.isNull() || sdev.isNull() ||
166 dust.isNull() || trend.isNull() || pres.isNull() || dew.isNull() ||
167 wdir.isNull() || speed.isNull() || hum.isNull() || tmp.isNull() ||
168 solar.isNull()|| date.isNull())
169 {
170 Warn("Parsing of xml failed [1].");
171 PostClose(false);
172 return;
173 }
174
175 DimWeather w;
176 w.fDustTotal = dust .text().toFloat();
177 w.fTempTrend = trend.text().toFloat();
178 w.fAirPressure = pres .text().toFloat();
179 w.fDewPoint = dew .text().toFloat();
180 w.fWindDirection = wdir .text().toFloat();
181 w.fWindSpeed = speed.text().toFloat()*3.6;
182 w.fHumidity = hum .text().toFloat();
183 w.fTemperature = tmp .text().toFloat();
184 w.fSolarimeter = solar.text().toFloat();
185
186 DimSeeing s;
187 s.fSeeing = see .text().toFloat();
188 s.fSeeingMed = med .text().toFloat();
189 s.fSeeingStdev = sdev .text().toFloat();
190
191 const string dateObj = date.text().toStdString();
192 const string dateSee = mjd .text().toStdString();
193
194 Time timeObj(dateObj);
195 Time timeSee(dateSee);
196 if (!timeObj.IsValid())
197 {
198 struct tm tm;
199
200 vector<char> buf(255);
201 if (strptime(dateObj.c_str(), "%c", &tm))
202 timeObj = Time(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
203 tm.tm_hour, tm.tm_min, tm.tm_sec);
204 }
205
206 if (!timeSee.IsValid())
207 {
208 struct tm tm;
209
210 vector<char> buf(255);
211 if (strptime(dateSee.c_str(), "%c", &tm))
212 timeSee = Time(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
213 tm.tm_hour, tm.tm_min, tm.tm_sec);
214
215 Warn("Seeing time invalid ["+dateObj+"]");
216 }
217
218 if (!timeObj.IsValid())
219 throw runtime_error("object time invalid");
220
221 if (timeObj!=fLastReport && fIsVerbose)
222 {
223 Out() << endl;
224 Out() << "Date: " << timeObj << endl;
225 Out() << "DustTotal: " << w.fDustTotal << " ugr/m^2" << endl;
226 Out() << "AirPressure: " << w.fAirPressure << " mbar" << endl;
227 Out() << "DewPoint: " << w.fDewPoint << " deg C" << endl;
228 Out() << "WindDirection: " << w.fWindDirection << " deg" << endl;
229 Out() << "WindSpeed: " << w.fWindSpeed << " m/s" << endl;
230 Out() << "Humidity: " << w.fHumidity << "%" << endl;
231 Out() << "Temperature: " << w.fTemperature << " deg C" << endl;
232 Out() << "TempTrend 24h: " << w.fTempTrend << " deg C" << endl;
233 Out() << "Solarimeter: " << w.fSolarimeter << " W/m^2" << endl;
234 Out() << endl;
235 Out() << "Seeing: " << s.fSeeing << " arcsec [" << timeSee << "]" << endl;
236 Out() << "Seeing: " << s.fSeeingMed << " +- " << s.fSeeingStdev << endl;
237 Out() << endl;
238 }
239
240 fLastReport = timeObj;
241
242 UpdateWeather(timeObj, w);
243
244 if (timeSee.IsValid() && fLastSeeing!=timeSee)
245 {
246 UpdateSeeing(timeSee, s);
247 fLastSeeing = timeSee;
248 }
249
250 if (fDust==w.fDustTotal)
251 return;
252
253 UpdateDust(timeObj, w.fDustTotal);
254 fDust = w.fDustTotal;
255
256 ostringstream out;
257 out << setprecision(3) << "Dust: " << fDust << "ug/m^3 [" << timeObj << "]";
258 Message(out);
259 }
260
261 void StartReadReport()
262 {
263 async_read_some(ba::buffer(fArray),
264 boost::bind(&ConnectionWeather::HandleRead, this,
265 dummy::error, dummy::bytes_transferred));
266 }
267
268 boost::asio::deadline_timer fKeepAlive;
269
270 void PostRequest()
271 {
272 const string cmd =
273 "GET "+fSite+" HTTP/1.1\r\n"
274 "User-Agent: FACT tngweather\r\n"
275 "Accept: */*\r\n"
276 "Host: "+URL()+"\r\n"
277 "Connection: close\r\n"//Keep-Alive\r\n"
278 "Content-Type: application/rss+xml\r\n"
279 "Pragma: no-cache\r\n"
280 "Cache-Control: no-cache\r\n"
281 "Expires: 0\r\n"
282 "Cache-Control: max-age=0\r\n"
283 "\r\n";
284
285 PostMessage(cmd);
286 }
287
288 void Request()
289 {
290 PostRequest();
291
292 fKeepAlive.expires_from_now(boost::posix_time::seconds(fInterval));
293 fKeepAlive.async_wait(boost::bind(&ConnectionWeather::HandleRequest,
294 this, dummy::error));
295 }
296
297 void HandleRequest(const bs::error_code &error)
298 {
299 // 125: Operation canceled (bs::error_code(125, bs::system_category))
300 if (error && error!=ba::error::basic_errors::operation_aborted)
301 {
302 ostringstream str;
303 str << "Write timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
304 Error(str);
305
306 PostClose(false);
307 return;
308 }
309
310 if (IsClosed())
311 {
312 // For example: Here we could schedule a new accept if we
313 // would not want to allow two connections at the same time.
314 PostClose(true);
315 return;
316 }
317
318 // Check whether the deadline has passed. We compare the deadline
319 // against the current time since a new asynchronous operation
320 // may have moved the deadline before this actor had a chance
321 // to run.
322 if (fKeepAlive.expires_at() > ba::deadline_timer::traits_type::now())
323 return;
324
325 Request();
326 }
327
328
329private:
330 // This is called when a connection was established
331 void ConnectionEstablished()
332 {
333 Request();
334 StartReadReport();
335 }
336
337public:
338
339 static const uint16_t kMaxAddr;
340
341public:
342 ConnectionWeather(ba::io_service& ioservice, MessageImp &imp) : ConnectionSSL(ioservice, imp()),
343 fIsVerbose(true), fDust(-1),
344 fLastReport(Time::none), fLastReception(Time::none), fLastSeeing(Time::none),
345 fKeepAlive(ioservice)
346 {
347 SetLogStream(&imp);
348 }
349
350 void SetVerbose(bool b)
351 {
352 fIsVerbose = b;
353 ConnectionSSL::SetVerbose(b);
354 }
355
356 void SetInterval(uint16_t i)
357 {
358 fInterval = i;
359 }
360
361 void SetSite(const string &site)
362 {
363 fSite = site;
364 }
365
366 int GetState() const
367 {
368 if (fLastReport.IsValid() && fLastReport+boost::posix_time::seconds(fInterval*2)>Time())
369 return 3; // receiving
370
371 if (fLastReception.IsValid() && fLastReception+boost::posix_time::seconds(fInterval*2)>Time())
372 return 2; // connected
373
374 return 1; // Disconnected
375 }
376};
377
378const uint16_t ConnectionWeather::kMaxAddr = 0xfff;
379
380// ------------------------------------------------------------------------
381
382#include "DimDescriptionService.h"
383
384class ConnectionDimWeather : public ConnectionWeather
385{
386private:
387 DimDescribedService fDimWeather;
388 DimDescribedService fDimAtmosphere;
389 DimDescribedService fDimSeeing;
390
391 virtual void UpdateWeather(const Time &t, const DimWeather &data)
392 {
393 fDimWeather.setData(&data, sizeof(DimWeather));
394 fDimWeather.Update(t);
395 }
396
397 virtual void UpdateDust(const Time &t, const float &dust)
398 {
399 fDimAtmosphere.setData(&dust, sizeof(float));
400 fDimAtmosphere.Update(t);
401 }
402
403 virtual void UpdateSeeing(const Time &t, const DimSeeing &see)
404 {
405 fDimSeeing.setData(&see, sizeof(DimSeeing));
406 fDimSeeing.Update(t);
407 }
408
409public:
410 ConnectionDimWeather(ba::io_service& ioservice, MessageImp &imp) :
411 ConnectionWeather(ioservice, imp),
412 fDimWeather("TNG_WEATHER/DATA", "F:1;F:1;F:1;F:1;F:1;F:1;F:1;F:1;F:1",
413 "|T[deg C]:Temperature"
414 "|DeltaT[deg C]:Temperature trend 24h"
415 "|T_dew[deg C]:Dew point"
416 "|H[%]:Humidity"
417 "|P[mbar]:Air pressure"
418 "|v[km/h]:Wind speed"
419 "|d[deg]:Wind direction (N-E)"
420 "|Dust[ug/m^3]:Dust (total)"
421 "|Solarimeter[W/m^2]:Solarimeter"),
422 fDimAtmosphere("TNG_WEATHER/DUST", "F:1",
423 "|Dust[ug/m^3]:Dust (total)"),
424 fDimSeeing("TNG_WEATHER/SEEING", "F:1;F:1;F:1",
425 "|Seeing[arcsec]:Seeing"
426 "|SeeingMed[arcsec]:Seeing Median"
427 "|SeeingStdev[arcsec]:Seeing Stdev")
428 {
429 }
430};
431
432// ------------------------------------------------------------------------
433
434template <class T, class S>
435class StateMachineWeather : public StateMachineAsio<T>
436{
437private:
438 S fWeather;
439
440 bool CheckEventSize(size_t has, const char *name, size_t size)
441 {
442 if (has==size)
443 return true;
444
445 ostringstream msg;
446 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
447 T::Fatal(msg);
448 return false;
449 }
450
451 int SetVerbosity(const EventImp &evt)
452 {
453 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
454 return T::kSM_FatalError;
455
456 fWeather.SetVerbose(evt.GetBool());
457
458 return T::GetCurrentState();
459 }
460/*
461 int Disconnect()
462 {
463 // Close all connections
464 fWeather.PostClose(false);
465
466 return T::GetCurrentState();
467 }
468
469 int Reconnect(const EventImp &evt)
470 {
471 // Close all connections to supress the warning in SetEndpoint
472 fWeather.PostClose(false);
473
474 // Now wait until all connection have been closed and
475 // all pending handlers have been processed
476 poll();
477
478 if (evt.GetBool())
479 fWeather.SetEndpoint(evt.GetString());
480
481 // Now we can reopen the connection
482 fWeather.PostClose(true);
483
484 return T::GetCurrentState();
485 }
486*/
487 int Execute()
488 {
489 return fWeather.GetState();
490 }
491
492
493public:
494 StateMachineWeather(ostream &out=cout) :
495 StateMachineAsio<T>(out, "TNG_WEATHER"), fWeather(*this, *this)
496 {
497 // State names
498 T::AddStateName(State::kDisconnected, "NoConnection",
499 "No connection to web-server could be established recently");
500
501 T::AddStateName(State::kConnected, "Invalid",
502 "Connection to webserver can be established, but received data is not recent or invalid");
503
504 T::AddStateName(State::kReceiving, "Valid",
505 "Connection to webserver can be established, receint data received");
506
507 // Verbosity commands
508 T::AddEvent("SET_VERBOSE", "B")
509 (bind(&StateMachineWeather::SetVerbosity, this, placeholders::_1))
510 ("set verbosity state"
511 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
512/*
513 // Conenction commands
514 AddEvent("DISCONNECT")
515 (bind(&StateMachineWeather::Disconnect, this))
516 ("disconnect from ethernet");
517
518 AddEvent("RECONNECT", "O")
519 (bind(&StateMachineWeather::Reconnect, this, placeholders::_1))
520 ("(Re)connect ethernet connection to FTM, a new address can be given"
521 "|[host][string]:new ethernet address in the form <host:port>");
522*/
523 }
524
525 int EvalOptions(Configuration &conf)
526 {
527 fWeather.SetVerbose(!conf.Get<bool>("quiet"));
528 fWeather.SetInterval(conf.Get<uint16_t>("interval"));
529 fWeather.SetDebugTx(conf.Get<bool>("debug-tx"));
530 fWeather.SetSite(conf.Get<string>("url"));
531 fWeather.SetEndpoint(conf.Get<string>("addr"));
532 fWeather.StartConnect();
533
534 return -1;
535 }
536};
537
538// ------------------------------------------------------------------------
539
540#include "Main.h"
541
542
543template<class T, class S, class R>
544int RunShell(Configuration &conf)
545{
546 return Main::execute<T, StateMachineWeather<S, R>>(conf);
547}
548
549void SetupConfiguration(Configuration &conf)
550{
551 po::options_description control("TNG weather control options");
552 control.add_options()
553 ("no-dim,d", po_switch(), "Disable dim services")
554 ("addr,a", var<string>("tngweb.tng.iac.es:443"), "Network address of Cosy")
555 ("url,u", var<string>("/api/meteo/weather/feed.xml"), "File name and path to load")
556 ("quiet,q", po_bool(true), "Disable printing contents of all received messages (except dynamic data) in clear text.")
557 ("interval,i", var<uint16_t>(300), "Interval between two updates on the server in seconds")
558 ("debug-tx", po_bool(), "Enable debugging of ethernet transmission.")
559 ;
560
561 conf.AddOptions(control);
562}
563
564/*
565 Extract usage clause(s) [if any] for SYNOPSIS.
566 Translators: "Usage" and "or" here are patterns (regular expressions) which
567 are used to match the usage synopsis in program output. An example from cp
568 (GNU coreutils) which contains both strings:
569 Usage: cp [OPTION]... [-T] SOURCE DEST
570 or: cp [OPTION]... SOURCE... DIRECTORY
571 or: cp [OPTION]... -t DIRECTORY SOURCE...
572 */
573void PrintUsage()
574{
575 cout <<
576 "The tngweather is an interface to the TNG weather data.\n"
577 "\n"
578 "The default is that the program is started without user intercation. "
579 "All actions are supposed to arrive as DimCommands. Using the -c "
580 "option, a local shell can be initialized. With h or help a short "
581 "help message about the usuage can be brought to the screen.\n"
582 "\n"
583 "Usage: tngweather [-c type] [OPTIONS]\n"
584 " or: tngweather [OPTIONS]\n";
585 cout << endl;
586}
587
588void PrintHelp()
589{
590// Main::PrintHelp<StateMachineFTM<StateMachine, ConnectionFTM>>();
591
592 /* Additional help text which is printed after the configuration
593 options goes here */
594
595 /*
596 cout << "bla bla bla" << endl << endl;
597 cout << endl;
598 cout << "Environment:" << endl;
599 cout << "environment" << endl;
600 cout << endl;
601 cout << "Examples:" << endl;
602 cout << "test exam" << endl;
603 cout << endl;
604 cout << "Files:" << endl;
605 cout << "files" << endl;
606 cout << endl;
607 */
608}
609
610int main(int argc, const char* argv[])
611{
612 Configuration conf(argv[0]);
613 conf.SetPrintUsage(PrintUsage);
614 Main::SetupConfiguration(conf);
615 SetupConfiguration(conf);
616
617 if (!conf.DoParse(argc, argv, PrintHelp))
618 return 127;
619
620 //try
621 {
622 // No console access at all
623 if (!conf.Has("console"))
624 {
625 if (conf.Get<bool>("no-dim"))
626 return RunShell<LocalStream, StateMachine, ConnectionWeather>(conf);
627 else
628 return RunShell<LocalStream, StateMachineDim, ConnectionDimWeather>(conf);
629 }
630 // Cosole access w/ and w/o Dim
631 if (conf.Get<bool>("no-dim"))
632 {
633 if (conf.Get<int>("console")==0)
634 return RunShell<LocalShell, StateMachine, ConnectionWeather>(conf);
635 else
636 return RunShell<LocalConsole, StateMachine, ConnectionWeather>(conf);
637 }
638 else
639 {
640 if (conf.Get<int>("console")==0)
641 return RunShell<LocalShell, StateMachineDim, ConnectionDimWeather>(conf);
642 else
643 return RunShell<LocalConsole, StateMachineDim, ConnectionDimWeather>(conf);
644 }
645 }
646 /*catch (std::exception& e)
647 {
648 cerr << "Exception: " << e.what() << endl;
649 return -1;
650 }*/
651
652 return 0;
653}
Note: See TracBrowser for help on using the repository browser.