blob: 81f61b458970d482c05f44a1523ccb05fabe3e41 [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';
Philipp Schradere625ba22020-11-16 20:11:37 -08005
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;
James Kuszmaul71a81932020-12-15 21:08:01 -08009import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest;
10import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest;
11import TransferMethod = web_proxy.aos.web_proxy.TransferMethod;
Philipp Schradere625ba22020-11-16 20:11:37 -080012
Alex Perry5f474f22020-02-01 12:14:24 -080013export class ConfigHandler {
Alex Perryb49a3fb2020-02-29 15:26:54 -080014 private readonly root_div = document.createElement('div');
Alex Perry5427c9a2020-02-15 17:43:45 -080015 private readonly tree_div;
Alex Perry6249aaf2020-02-29 14:51:49 -080016 private config: Configuration|null = null
Alex Perry5f474f22020-02-01 12:14:24 -080017
Alex Perry6249aaf2020-02-29 14:51:49 -080018 constructor(private readonly connection: Connection) {
19 this.connection.addConfigHandler((config) => this.handleConfig(config));
20
Alex Perryb49a3fb2020-02-29 15:26:54 -080021 document.body.appendChild(this.root_div);
Alex Perry5427c9a2020-02-15 17:43:45 -080022 const show_button = document.createElement('button');
23 show_button.addEventListener('click', () => this.toggleConfig());
24 const show_text = document.createTextNode('Show/Hide Config');
25 show_button.appendChild(show_text);
26 this.tree_div = document.createElement('div');
27 this.tree_div.hidden = true;
28 this.root_div.appendChild(show_button);
29 this.root_div.appendChild(this.tree_div);
30 }
Alex Perry5f474f22020-02-01 12:14:24 -080031
Alex Perry6249aaf2020-02-29 14:51:49 -080032 handleConfig(config: Configuration) {
33 this.config = config;
34 this.printConfig();
35 }
36
Alex Perry5f474f22020-02-01 12:14:24 -080037 printConfig() {
James Kuszmaul1ec74432020-07-30 20:26:45 -070038 for (let i = 0; i < this.config.channelsLength(); i++) {
Alex Perry5f474f22020-02-01 12:14:24 -080039 const channel_div = document.createElement('div');
40 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080041 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080042
43 const input_el = document.createElement('input');
Philipp Schrader47445a02020-11-14 17:31:04 -080044 input_el.setAttribute('data-index', i.toString());
Alex Perry5f474f22020-02-01 12:14:24 -080045 input_el.setAttribute('type', 'checkbox');
46 input_el.addEventListener('click', () => this.handleChange());
47 channel_div.appendChild(input_el);
48
49 const name_div = document.createElement('div');
50 const name_text = document.createTextNode(this.config.channels(i).name());
51 name_div.appendChild(name_text);
52 const type_div = document.createElement('div');
53 const type_text = document.createTextNode(this.config.channels(i).type());
54 type_div.appendChild(type_text);
55 const info_div = document.createElement('div');
56 info_div.appendChild(name_div);
57 info_div.appendChild(type_div);
58
59 channel_div.appendChild(info_div);
60 }
61 }
62
63 handleChange() {
64 const toggles = this.root_div.getElementsByTagName('input');
Philipp Schradere625ba22020-11-16 20:11:37 -080065 const builder =
66 new flatbuffers_builder.Builder(512) as unknown as flatbuffers.Builder;
Alex Perry5f474f22020-02-01 12:14:24 -080067
68 const channels: flatbuffers.Offset[] = [];
69 for (const toggle of toggles) {
70 if (!toggle.checked) {
71 continue;
72 }
73 const index = toggle.getAttribute('data-index');
Philipp Schrader47445a02020-11-14 17:31:04 -080074 const channel = this.config.channels(Number(index));
Alex Perry5f474f22020-02-01 12:14:24 -080075 const namefb = builder.createString(channel.name());
76 const typefb = builder.createString(channel.type());
Alex Perryd5e13572020-02-22 15:15:08 -080077 Channel.startChannel(builder);
78 Channel.addName(builder, namefb);
79 Channel.addType(builder, typefb);
80 const channelfb = Channel.endChannel(builder);
James Kuszmaul71a81932020-12-15 21:08:01 -080081 ChannelRequest.startChannelRequest(builder);
82 ChannelRequest.addChannel(builder, channelfb);
83 ChannelRequest.addMethod(builder, TransferMethod.SUBSAMPLE);
84 channels.push(ChannelRequest.endChannelRequest(builder));
Alex Perry5f474f22020-02-01 12:14:24 -080085 }
86
87 const channelsfb =
James Kuszmaul71a81932020-12-15 21:08:01 -080088 SubscriberRequest.createChannelsToTransferVector(builder, channels);
89 SubscriberRequest.startSubscriberRequest(builder);
90 SubscriberRequest.addChannelsToTransfer(builder, channelsfb);
91 const request = SubscriberRequest.endSubscriberRequest(builder);
92 builder.finish(request);
Alex Perry6249aaf2020-02-29 14:51:49 -080093 this.connection.sendConnectMessage(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080094 }
Alex Perry5427c9a2020-02-15 17:43:45 -080095
96 toggleConfig() {
97 this.tree_div.hidden = !this.tree_div.hidden;
98 }
Alex Perry5f474f22020-02-01 12:14:24 -080099}