blob: 3d20f0d311ffe56d08c284fe392f06542e2c3d20 [file] [log] [blame]
Alex Perryd5e13572020-02-22 15:15:08 -08001import {Configuration, Channel} from 'aos/configuration_generated';
2import {Connect} from 'aos/network/connect_generated';
Alex Perry6249aaf2020-02-29 14:51:49 -08003import {Connection} from './proxy';
Alex Perry5f474f22020-02-01 12:14:24 -08004
5export class ConfigHandler {
6 private readonly root_div = document.getElementById('config');
Alex Perry5427c9a2020-02-15 17:43:45 -08007 private readonly tree_div;
Alex Perry6249aaf2020-02-29 14:51:49 -08008 private config: Configuration|null = null
Alex Perry5f474f22020-02-01 12:14:24 -08009
Alex Perry6249aaf2020-02-29 14:51:49 -080010 constructor(private readonly connection: Connection) {
11 this.connection.addConfigHandler((config) => this.handleConfig(config));
12
Alex Perry5427c9a2020-02-15 17:43:45 -080013 const show_button = document.createElement('button');
14 show_button.addEventListener('click', () => this.toggleConfig());
15 const show_text = document.createTextNode('Show/Hide Config');
16 show_button.appendChild(show_text);
17 this.tree_div = document.createElement('div');
18 this.tree_div.hidden = true;
19 this.root_div.appendChild(show_button);
20 this.root_div.appendChild(this.tree_div);
21 }
Alex Perry5f474f22020-02-01 12:14:24 -080022
Alex Perry6249aaf2020-02-29 14:51:49 -080023 handleConfig(config: Configuration) {
24 this.config = config;
25 this.printConfig();
26 }
27
Alex Perry5f474f22020-02-01 12:14:24 -080028 printConfig() {
29 for (const i = 0; i < this.config.channelsLength(); i++) {
30 const channel_div = document.createElement('div');
31 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080032 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080033
34 const input_el = document.createElement('input');
35 input_el.setAttribute('data-index', i);
36 input_el.setAttribute('type', 'checkbox');
37 input_el.addEventListener('click', () => this.handleChange());
38 channel_div.appendChild(input_el);
39
40 const name_div = document.createElement('div');
41 const name_text = document.createTextNode(this.config.channels(i).name());
42 name_div.appendChild(name_text);
43 const type_div = document.createElement('div');
44 const type_text = document.createTextNode(this.config.channels(i).type());
45 type_div.appendChild(type_text);
46 const info_div = document.createElement('div');
47 info_div.appendChild(name_div);
48 info_div.appendChild(type_div);
49
50 channel_div.appendChild(info_div);
51 }
52 }
53
54 handleChange() {
55 const toggles = this.root_div.getElementsByTagName('input');
56 const builder = new flatbuffers.Builder(512);
57
58 const channels: flatbuffers.Offset[] = [];
59 for (const toggle of toggles) {
60 if (!toggle.checked) {
61 continue;
62 }
63 const index = toggle.getAttribute('data-index');
64 const channel = this.config.channels(index);
65 const namefb = builder.createString(channel.name());
66 const typefb = builder.createString(channel.type());
Alex Perryd5e13572020-02-22 15:15:08 -080067 Channel.startChannel(builder);
68 Channel.addName(builder, namefb);
69 Channel.addType(builder, typefb);
70 const channelfb = Channel.endChannel(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080071 channels.push(channelfb);
72 }
73
74 const channelsfb =
Alex Perryb41d5782020-02-09 17:06:40 -080075 Connect.createChannelsToTransferVector(builder, channels);
Alex Perryd5e13572020-02-22 15:15:08 -080076 Connect.startConnect(builder);
77 Connect.addChannelsToTransfer(builder, channelsfb);
78 const connect = Connect.endConnect(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080079 builder.finish(connect);
Alex Perry6249aaf2020-02-29 14:51:49 -080080 this.connection.sendConnectMessage(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080081 }
Alex Perry5427c9a2020-02-15 17:43:45 -080082
83 toggleConfig() {
84 this.tree_div.hidden = !this.tree_div.hidden;
85 }
Alex Perry5f474f22020-02-01 12:14:24 -080086}