blob: e093cb9c46327d259be3b261c51ac9f25b648019 [file] [log] [blame]
Comran Morsheddaf69232016-04-20 22:25:37 -07001#include <iostream>
2#include <memory>
3#include <sstream>
4#include <string>
5#include <thread>
6#include <atomic>
7#include <vector>
8
9#include "seasocks/PageHandler.h"
10#include "seasocks/PrintfLogger.h"
11#include "seasocks/StringUtil.h"
12#include "seasocks/WebSocket.h"
13
14#include "aos/linux_code/init.h"
15#include "aos/common/time.h"
16#include "aos/common/util/phased_loop.h"
17#include "aos/common/mutex.h"
18
19namespace y2016 {
20namespace dashboard {
21
22// Dashboard is a webserver that opens a socket and stream data from the robot
23// to the client. It is divided between the DataCollector, which polls
24// RunIteration to determine what to send to the client, and an instance of a
25// Seasocks server, which initiates a webserver on a port and opens a socket
26// for streaming data.
27
28// It is an adaption of http_status, which was a 2015 project
29// that plotted live position data from the robot queues on a webpage for
30// debugging.
31
32class DataCollector {
33 public:
34 DataCollector();
35 void RunIteration();
36
37 // Store a datapoint. In this case, we are reading data points to determine
38 // what color to display on the webpage indicators. Traditionally, this would
39 // be used to plot live data on a graph on the page.
40 void AddPoint(const ::std::string &name, double value);
41
42 // Method called by the websocket to get a JSON-packaged string containing,
43 // at most, a constant number of samples, starting at from_sample.
44 ::std::string Fetch(int32_t from_sample);
45
46 void operator()();
47 void Quit() { run_ = false; }
48
49 private:
50 // Returns a wrapped index based on the overflow size.
51 size_t GetIndex(size_t sample_id);
52
53 struct ItemDatapoint {
54 double value;
Austin Schuhf2a50ba2016-12-24 16:16:26 -080055 ::aos::monotonic_clock::time_point time;
Comran Morsheddaf69232016-04-20 22:25:37 -070056 };
57
58 struct SampleItem {
59 ::std::string name;
60 ::std::vector<ItemDatapoint> datapoints;
61 };
62
63 // Storage vector that is written and overwritten with data in a FIFO fashion.
64 ::std::vector<SampleItem> sample_items_;
65
66 ::std::string cur_raw_data_;
67 int32_t sample_id_; // Last sample id used.
68 size_t measure_index_; // Last measure index used.
69 const int32_t overflow_id_; // Vector wrapping size.
70
71 ::std::atomic<bool> run_{true};
72 ::aos::Mutex mutex_;
73};
74
75class SocketHandler : public seasocks::WebSocket::Handler {
76 public:
77 SocketHandler();
78 void onConnect(seasocks::WebSocket* connection) override;
79 void onData(seasocks::WebSocket* connection, const char* data) override;
80 void onDisconnect(seasocks::WebSocket* connection) override;
81 void Quit();
82
83 private:
84 ::std::set<seasocks::WebSocket*> connections_;
85 DataCollector data_collector_;
86 ::std::thread data_collector_thread_;
87};
88
89class SeasocksLogger : public seasocks::PrintfLogger {
90 public:
91 SeasocksLogger(Level min_level_to_log);
92 void log(Level level, const char* message) override;
93};
94
95} // namespace dashboard
96} // namespace y2016