Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 1 | #ifndef Y2016_DASHBOARD_DASHBOARD_H_ |
| 2 | #define Y2016_DASHBOARD_DASHBOARD_H_ |
| 3 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 4 | #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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "aos/events/event_loop.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 18 | #include "aos/mutex/mutex.h" |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 19 | #include "aos/time/time.h" |
Austin Schuh | ed5b26d | 2019-12-05 20:51:59 -0800 | [diff] [blame^] | 20 | #include "frc971/autonomous/auto_mode_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "y2016/control_loops/superstructure/superstructure_status_generated.h" |
| 22 | #include "y2016/queues/ball_detector_generated.h" |
| 23 | #include "y2016/vision/vision_generated.h" |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 24 | |
| 25 | namespace y2016 { |
| 26 | namespace dashboard { |
| 27 | |
| 28 | // Dashboard is a webserver that opens a socket and stream data from the robot |
| 29 | // to the client. It is divided between the DataCollector, which polls |
| 30 | // RunIteration to determine what to send to the client, and an instance of a |
| 31 | // Seasocks server, which initiates a webserver on a port and opens a socket |
| 32 | // for streaming data. |
| 33 | |
| 34 | // It is an adaption of http_status, which was a 2015 project |
| 35 | // that plotted live position data from the robot queues on a webpage for |
| 36 | // debugging. |
| 37 | |
| 38 | class DataCollector { |
| 39 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 40 | DataCollector(::aos::EventLoop *event_loop); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 41 | void RunIteration(); |
| 42 | |
| 43 | // Store a datapoint. In this case, we are reading data points to determine |
| 44 | // what color to display on the webpage indicators. Traditionally, this would |
| 45 | // be used to plot live data on a graph on the page. |
| 46 | void AddPoint(const ::std::string &name, double value); |
| 47 | |
| 48 | // Method called by the websocket to get a JSON-packaged string containing, |
| 49 | // at most, a constant number of samples, starting at from_sample. |
| 50 | ::std::string Fetch(int32_t from_sample); |
| 51 | |
| 52 | void operator()(); |
| 53 | void Quit() { run_ = false; } |
| 54 | |
| 55 | private: |
| 56 | // Returns a wrapped index based on the overflow size. |
| 57 | size_t GetIndex(size_t sample_id); |
| 58 | |
| 59 | struct ItemDatapoint { |
| 60 | double value; |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 61 | ::aos::monotonic_clock::time_point time; |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct SampleItem { |
| 65 | ::std::string name; |
| 66 | ::std::vector<ItemDatapoint> datapoints; |
| 67 | }; |
| 68 | |
Austin Schuh | d32b362 | 2019-06-23 18:49:06 -0700 | [diff] [blame] | 69 | ::aos::EventLoop *event_loop_; |
| 70 | |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 71 | ::aos::Fetcher<::y2016::vision::VisionStatus> vision_status_fetcher_; |
Austin Schuh | 4b652c9 | 2019-05-27 13:22:27 -0700 | [diff] [blame] | 72 | ::aos::Fetcher<::y2016::sensors::BallDetector> ball_detector_fetcher_; |
Austin Schuh | a250b2d | 2019-05-27 16:14:02 -0700 | [diff] [blame] | 73 | ::aos::Fetcher<::frc971::autonomous::AutonomousMode> autonomous_mode_fetcher_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | ::aos::Fetcher<::y2016::control_loops::superstructure::Status> |
Austin Schuh | 9481d0d | 2019-06-29 21:56:17 -0700 | [diff] [blame] | 75 | superstructure_status_fetcher_; |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 76 | |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 77 | // Storage vector that is written and overwritten with data in a FIFO fashion. |
| 78 | ::std::vector<SampleItem> sample_items_; |
| 79 | |
| 80 | ::std::string cur_raw_data_; |
| 81 | int32_t sample_id_; // Last sample id used. |
| 82 | size_t measure_index_; // Last measure index used. |
| 83 | const int32_t overflow_id_; // Vector wrapping size. |
| 84 | |
| 85 | ::std::atomic<bool> run_{true}; |
| 86 | ::aos::Mutex mutex_; |
| 87 | }; |
| 88 | |
| 89 | class SocketHandler : public seasocks::WebSocket::Handler { |
| 90 | public: |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 91 | SocketHandler(::aos::EventLoop *event_loop); |
Comran Morshed | daf6923 | 2016-04-20 22:25:37 -0700 | [diff] [blame] | 92 | void onConnect(seasocks::WebSocket* connection) override; |
| 93 | void onData(seasocks::WebSocket* connection, const char* data) override; |
| 94 | void onDisconnect(seasocks::WebSocket* connection) override; |
| 95 | void Quit(); |
| 96 | |
| 97 | private: |
| 98 | ::std::set<seasocks::WebSocket*> connections_; |
| 99 | DataCollector data_collector_; |
| 100 | ::std::thread data_collector_thread_; |
| 101 | }; |
| 102 | |
| 103 | class SeasocksLogger : public seasocks::PrintfLogger { |
| 104 | public: |
| 105 | SeasocksLogger(Level min_level_to_log); |
| 106 | void log(Level level, const char* message) override; |
| 107 | }; |
| 108 | |
| 109 | } // namespace dashboard |
| 110 | } // namespace y2016 |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 111 | |
| 112 | #endif // Y2016_DASHBOARD_DASHBOARD_H_ |