blob: a7c2149467c020503f455429acef49ea81b3cd63 [file] [log] [blame]
James Kuszmauldac091f2022-03-22 09:35:06 -07001import {ByteBuffer} from 'flatbuffers';
2import {Configuration} from 'org_frc971/aos/configuration_generated';
James Kuszmaul71a81932020-12-15 21:08:01 -08003import {Connection} from 'org_frc971/aos/network/www/proxy';
James Kuszmauldac091f2022-03-22 09:35:06 -07004
James Kuszmaul527038a2020-12-21 23:40:44 -08005import {Parser, Table} from './reflection'
Philipp Schradere625ba22020-11-16 20:11:37 -08006
Alex Perry5f474f22020-02-01 12:14:24 -08007export class ConfigHandler {
Alex Perryb49a3fb2020-02-29 15:26:54 -08008 private readonly root_div = document.createElement('div');
Alex Perry5427c9a2020-02-15 17:43:45 -08009 private readonly tree_div;
Alex Perry6249aaf2020-02-29 14:51:49 -080010 private config: Configuration|null = null
Alex Perry5f474f22020-02-01 12:14:24 -080011
Alex Perry6249aaf2020-02-29 14:51:49 -080012 constructor(private readonly connection: Connection) {
13 this.connection.addConfigHandler((config) => this.handleConfig(config));
14
Alex Perryb49a3fb2020-02-29 15:26:54 -080015 document.body.appendChild(this.root_div);
Alex Perry5427c9a2020-02-15 17:43:45 -080016 const show_button = document.createElement('button');
17 show_button.addEventListener('click', () => this.toggleConfig());
18 const show_text = document.createTextNode('Show/Hide Config');
19 show_button.appendChild(show_text);
20 this.tree_div = document.createElement('div');
21 this.tree_div.hidden = true;
22 this.root_div.appendChild(show_button);
23 this.root_div.appendChild(this.tree_div);
24 }
Alex Perry5f474f22020-02-01 12:14:24 -080025
Alex Perry6249aaf2020-02-29 14:51:49 -080026 handleConfig(config: Configuration) {
27 this.config = config;
28 this.printConfig();
29 }
30
Alex Perry5f474f22020-02-01 12:14:24 -080031 printConfig() {
James Kuszmaul1ec74432020-07-30 20:26:45 -070032 for (let i = 0; i < this.config.channelsLength(); i++) {
Alex Perry5f474f22020-02-01 12:14:24 -080033 const channel_div = document.createElement('div');
34 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080035 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080036
37 const input_el = document.createElement('input');
Philipp Schrader47445a02020-11-14 17:31:04 -080038 input_el.setAttribute('data-index', i.toString());
Alex Perry5f474f22020-02-01 12:14:24 -080039 input_el.setAttribute('type', 'checkbox');
40 input_el.addEventListener('click', () => this.handleChange());
41 channel_div.appendChild(input_el);
42
43 const name_div = document.createElement('div');
44 const name_text = document.createTextNode(this.config.channels(i).name());
45 name_div.appendChild(name_text);
46 const type_div = document.createElement('div');
47 const type_text = document.createTextNode(this.config.channels(i).type());
48 type_div.appendChild(type_text);
49 const info_div = document.createElement('div');
50 info_div.appendChild(name_div);
51 info_div.appendChild(type_div);
52
53 channel_div.appendChild(info_div);
54 }
55 }
56
57 handleChange() {
58 const toggles = this.root_div.getElementsByTagName('input');
Alex Perry5f474f22020-02-01 12:14:24 -080059 for (const toggle of toggles) {
60 if (!toggle.checked) {
61 continue;
62 }
63 const index = toggle.getAttribute('data-index');
Philipp Schrader47445a02020-11-14 17:31:04 -080064 const channel = this.config.channels(Number(index));
James Kuszmaul527038a2020-12-21 23:40:44 -080065 this.connection.addHandler(
66 channel.name(), channel.type(), (data, time) => {
James Kuszmaul5f5e1232020-12-22 20:58:00 -080067 const parser =
68 new Parser(this.connection.getSchema(channel.type()));
James Kuszmaul527038a2020-12-21 23:40:44 -080069 console.log(
70 parser.toObject(Table.getRootTable(new ByteBuffer(data))));
71 });
Alex Perry5f474f22020-02-01 12:14:24 -080072 }
73
Alex Perry5f474f22020-02-01 12:14:24 -080074 }
Alex Perry5427c9a2020-02-15 17:43:45 -080075
76 toggleConfig() {
77 this.tree_div.hidden = !this.tree_div.hidden;
78 }
Alex Perry5f474f22020-02-01 12:14:24 -080079}