blob: 73b6b8955dd2d64c0d7e8dc9274df8ee76f2d323 [file] [log] [blame]
Philipp Schradere625ba22020-11-16 20:11:37 -08001import * as configuration from 'org_frc971/aos/configuration_generated';
James Kuszmaul71a81932020-12-15 21:08:01 -08002import {Connection} from 'org_frc971/aos/network/www/proxy';
Philipp Schradere625ba22020-11-16 20:11:37 -08003import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
James Kuszmaul71a81932020-12-15 21:08:01 -08004import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated';
James Kuszmaul527038a2020-12-21 23:40:44 -08005import {Parser, Table} from './reflection'
6import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer';
Philipp Schradere625ba22020-11-16 20:11:37 -08007
Alex Perry5f474f22020-02-01 12:14:24 -08008
Philipp Schradere625ba22020-11-16 20:11:37 -08009import Configuration = configuration.aos.Configuration;
10import Channel = configuration.aos.Channel;
James Kuszmaul71a81932020-12-15 21:08:01 -080011import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
12import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
13import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
Philipp Schradere625ba22020-11-16 20:11:37 -080014
Alex Perry5f474f22020-02-01 12:14:24 -080015export class ConfigHandler {
Alex Perryb49a3fb2020-02-29 15:26:54 -080016 private readonly root_div = document.createElement('div');
Alex Perry5427c9a2020-02-15 17:43:45 -080017 private readonly tree_div;
Alex Perry6249aaf2020-02-29 14:51:49 -080018 private config: Configuration|null = null
Alex Perry5f474f22020-02-01 12:14:24 -080019
Alex Perry6249aaf2020-02-29 14:51:49 -080020 constructor(private readonly connection: Connection) {
21 this.connection.addConfigHandler((config) => this.handleConfig(config));
22
Alex Perryb49a3fb2020-02-29 15:26:54 -080023 document.body.appendChild(this.root_div);
Alex Perry5427c9a2020-02-15 17:43:45 -080024 const show_button = document.createElement('button');
25 show_button.addEventListener('click', () => this.toggleConfig());
26 const show_text = document.createTextNode('Show/Hide Config');
27 show_button.appendChild(show_text);
28 this.tree_div = document.createElement('div');
29 this.tree_div.hidden = true;
30 this.root_div.appendChild(show_button);
31 this.root_div.appendChild(this.tree_div);
32 }
Alex Perry5f474f22020-02-01 12:14:24 -080033
Alex Perry6249aaf2020-02-29 14:51:49 -080034 handleConfig(config: Configuration) {
35 this.config = config;
36 this.printConfig();
37 }
38
Alex Perry5f474f22020-02-01 12:14:24 -080039 printConfig() {
James Kuszmaul1ec74432020-07-30 20:26:45 -070040 for (let i = 0; i < this.config.channelsLength(); i++) {
Alex Perry5f474f22020-02-01 12:14:24 -080041 const channel_div = document.createElement('div');
42 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080043 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080044
45 const input_el = document.createElement('input');
Philipp Schrader47445a02020-11-14 17:31:04 -080046 input_el.setAttribute('data-index', i.toString());
Alex Perry5f474f22020-02-01 12:14:24 -080047 input_el.setAttribute('type', 'checkbox');
48 input_el.addEventListener('click', () => this.handleChange());
49 channel_div.appendChild(input_el);
50
51 const name_div = document.createElement('div');
52 const name_text = document.createTextNode(this.config.channels(i).name());
53 name_div.appendChild(name_text);
54 const type_div = document.createElement('div');
55 const type_text = document.createTextNode(this.config.channels(i).type());
56 type_div.appendChild(type_text);
57 const info_div = document.createElement('div');
58 info_div.appendChild(name_div);
59 info_div.appendChild(type_div);
60
61 channel_div.appendChild(info_div);
62 }
63 }
64
65 handleChange() {
66 const toggles = this.root_div.getElementsByTagName('input');
Alex Perry5f474f22020-02-01 12:14:24 -080067 for (const toggle of toggles) {
68 if (!toggle.checked) {
69 continue;
70 }
71 const index = toggle.getAttribute('data-index');
Philipp Schrader47445a02020-11-14 17:31:04 -080072 const channel = this.config.channels(Number(index));
James Kuszmaul527038a2020-12-21 23:40:44 -080073 this.connection.addHandler(
74 channel.name(), channel.type(), (data, time) => {
James Kuszmaul5f5e1232020-12-22 20:58:00 -080075 const parser =
76 new Parser(this.connection.getSchema(channel.type()));
James Kuszmaul527038a2020-12-21 23:40:44 -080077 console.log(
78 parser.toObject(Table.getRootTable(new ByteBuffer(data))));
79 });
Alex Perry5f474f22020-02-01 12:14:24 -080080 }
81
Alex Perry5f474f22020-02-01 12:14:24 -080082 }
Alex Perry5427c9a2020-02-15 17:43:45 -080083
84 toggleConfig() {
85 this.tree_div.hidden = !this.tree_div.hidden;
86 }
Alex Perry5f474f22020-02-01 12:14:24 -080087}