blob: 665d65627c76055432290da838b0469d6fbe5b9f [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"
Austin Schuh4b652c92019-05-27 13:22:27 -070020#include "y2016/queues/ball_detector.q.h"
Austin Schuh1bf8a212019-05-26 22:13:14 -070021#include "y2016/vision/vision.q.h"
Comran Morsheddaf69232016-04-20 22:25:37 -070022
23namespace y2016 {
24namespace dashboard {
25
26// Dashboard is a webserver that opens a socket and stream data from the robot
27// to the client. It is divided between the DataCollector, which polls
28// RunIteration to determine what to send to the client, and an instance of a
29// Seasocks server, which initiates a webserver on a port and opens a socket
30// for streaming data.
31
32// It is an adaption of http_status, which was a 2015 project
33// that plotted live position data from the robot queues on a webpage for
34// debugging.
35
36class DataCollector {
37 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070038 DataCollector(::aos::EventLoop *event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -070039 void RunIteration();
40
41 // Store a datapoint. In this case, we are reading data points to determine
42 // what color to display on the webpage indicators. Traditionally, this would
43 // be used to plot live data on a graph on the page.
44 void AddPoint(const ::std::string &name, double value);
45
46 // Method called by the websocket to get a JSON-packaged string containing,
47 // at most, a constant number of samples, starting at from_sample.
48 ::std::string Fetch(int32_t from_sample);
49
50 void operator()();
51 void Quit() { run_ = false; }
52
53 private:
54 // Returns a wrapped index based on the overflow size.
55 size_t GetIndex(size_t sample_id);
56
57 struct ItemDatapoint {
58 double value;
Austin Schuhf2a50ba2016-12-24 16:16:26 -080059 ::aos::monotonic_clock::time_point time;
Comran Morsheddaf69232016-04-20 22:25:37 -070060 };
61
62 struct SampleItem {
63 ::std::string name;
64 ::std::vector<ItemDatapoint> datapoints;
65 };
66
Austin Schuh1bf8a212019-05-26 22:13:14 -070067 ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_;
Austin Schuh4b652c92019-05-27 13:22:27 -070068 ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_;
Austin Schuh1bf8a212019-05-26 22:13:14 -070069
Comran Morsheddaf69232016-04-20 22:25:37 -070070 // Storage vector that is written and overwritten with data in a FIFO fashion.
71 ::std::vector<SampleItem> sample_items_;
72
73 ::std::string cur_raw_data_;
74 int32_t sample_id_; // Last sample id used.
75 size_t measure_index_; // Last measure index used.
76 const int32_t overflow_id_; // Vector wrapping size.
77
78 ::std::atomic<bool> run_{true};
79 ::aos::Mutex mutex_;
80};
81
82class SocketHandler : public seasocks::WebSocket::Handler {
83 public:
Austin Schuh1bf8a212019-05-26 22:13:14 -070084 SocketHandler(::aos::EventLoop *event_loop);
Comran Morsheddaf69232016-04-20 22:25:37 -070085 void onConnect(seasocks::WebSocket* connection) override;
86 void onData(seasocks::WebSocket* connection, const char* data) override;
87 void onDisconnect(seasocks::WebSocket* connection) override;
88 void Quit();
89
90 private:
91 ::std::set<seasocks::WebSocket*> connections_;
92 DataCollector data_collector_;
93 ::std::thread data_collector_thread_;
94};
95
96class SeasocksLogger : public seasocks::PrintfLogger {
97 public:
98 SeasocksLogger(Level min_level_to_log);
99 void log(Level level, const char* message) override;
100};
101
102} // namespace dashboard
103} // namespace y2016
Austin Schuh1bf8a212019-05-26 22:13:14 -0700104
105#endif // Y2016_DASHBOARD_DASHBOARD_H_