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