blob: a65d2fa2094fd630da6c719ebbc182fa3bb7c821 [file] [log] [blame]
Philipp Schradere625ba22020-11-16 20:11:37 -08001import * as configuration from 'org_frc971/aos/configuration_generated';
2import * as connect from 'org_frc971/aos/network/connect_generated';
3import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder';
4
Alex Perry6249aaf2020-02-29 14:51:49 -08005import {Connection} from './proxy';
Alex Perry5f474f22020-02-01 12:14:24 -08006
Philipp Schradere625ba22020-11-16 20:11:37 -08007import Configuration = configuration.aos.Configuration;
8import Channel = configuration.aos.Channel;
9import Connect = connect.aos.message_bridge.Connect;
10
Alex Perry5f474f22020-02-01 12:14:24 -080011export class ConfigHandler {
Alex Perryb49a3fb2020-02-29 15:26:54 -080012 private readonly root_div = document.createElement('div');
Alex Perry5427c9a2020-02-15 17:43:45 -080013 private readonly tree_div;
Alex Perry6249aaf2020-02-29 14:51:49 -080014 private config: Configuration|null = null
Alex Perry5f474f22020-02-01 12:14:24 -080015
Alex Perry6249aaf2020-02-29 14:51:49 -080016 constructor(private readonly connection: Connection) {
17 this.connection.addConfigHandler((config) => this.handleConfig(config));
18
Alex Perryb49a3fb2020-02-29 15:26:54 -080019 document.body.appendChild(this.root_div);
Alex Perry5427c9a2020-02-15 17:43:45 -080020 const show_button = document.createElement('button');
21 show_button.addEventListener('click', () => this.toggleConfig());
22 const show_text = document.createTextNode('Show/Hide Config');
23 show_button.appendChild(show_text);
24 this.tree_div = document.createElement('div');
25 this.tree_div.hidden = true;
26 this.root_div.appendChild(show_button);
27 this.root_div.appendChild(this.tree_div);
28 }
Alex Perry5f474f22020-02-01 12:14:24 -080029
Alex Perry6249aaf2020-02-29 14:51:49 -080030 handleConfig(config: Configuration) {
31 this.config = config;
32 this.printConfig();
33 }
34
Alex Perry5f474f22020-02-01 12:14:24 -080035 printConfig() {
James Kuszmaul1ec74432020-07-30 20:26:45 -070036 for (let i = 0; i < this.config.channelsLength(); i++) {
Alex Perry5f474f22020-02-01 12:14:24 -080037 const channel_div = document.createElement('div');
38 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080039 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080040
41 const input_el = document.createElement('input');
Philipp Schrader47445a02020-11-14 17:31:04 -080042 input_el.setAttribute('data-index', i.toString());
Alex Perry5f474f22020-02-01 12:14:24 -080043 input_el.setAttribute('type', 'checkbox');
44 input_el.addEventListener('click', () => this.handleChange());
45 channel_div.appendChild(input_el);
46
47 const name_div = document.createElement('div');
48 const name_text = document.createTextNode(this.config.channels(i).name());
49 name_div.appendChild(name_text);
50 const type_div = document.createElement('div');
51 const type_text = document.createTextNode(this.config.channels(i).type());
52 type_div.appendChild(type_text);
53 const info_div = document.createElement('div');
54 info_div.appendChild(name_div);
55 info_div.appendChild(type_div);
56
57 channel_div.appendChild(info_div);
58 }
59 }
60
61 handleChange() {
62 const toggles = this.root_div.getElementsByTagName('input');
Philipp Schradere625ba22020-11-16 20:11:37 -080063 const builder =
64 new flatbuffers_builder.Builder(512) as unknown as flatbuffers.Builder;
Alex Perry5f474f22020-02-01 12:14:24 -080065
66 const channels: flatbuffers.Offset[] = [];
67 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));
Alex Perry5f474f22020-02-01 12:14:24 -080073 const namefb = builder.createString(channel.name());
74 const typefb = builder.createString(channel.type());
Alex Perryd5e13572020-02-22 15:15:08 -080075 Channel.startChannel(builder);
76 Channel.addName(builder, namefb);
77 Channel.addType(builder, typefb);
78 const channelfb = Channel.endChannel(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080079 channels.push(channelfb);
80 }
81
82 const channelsfb =
Alex Perryb41d5782020-02-09 17:06:40 -080083 Connect.createChannelsToTransferVector(builder, channels);
Alex Perryd5e13572020-02-22 15:15:08 -080084 Connect.startConnect(builder);
85 Connect.addChannelsToTransfer(builder, channelsfb);
86 const connect = Connect.endConnect(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080087 builder.finish(connect);
Alex Perry6249aaf2020-02-29 14:51:49 -080088 this.connection.sendConnectMessage(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080089 }
Alex Perry5427c9a2020-02-15 17:43:45 -080090
91 toggleConfig() {
92 this.tree_div.hidden = !this.tree_div.hidden;
93 }
Alex Perry5f474f22020-02-01 12:14:24 -080094}