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