blob: 6615f39e0e7704a1a3d6b28e896d7e3fdb841b37 [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 {
Alex Perryb49a3fb2020-02-29 15:26:54 -08006 private readonly root_div = document.createElement('div');
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 Perryb49a3fb2020-02-29 15:26:54 -080013 document.body.appendChild(this.root_div);
Alex Perry5427c9a2020-02-15 17:43:45 -080014 const show_button = document.createElement('button');
15 show_button.addEventListener('click', () => this.toggleConfig());
16 const show_text = document.createTextNode('Show/Hide Config');
17 show_button.appendChild(show_text);
18 this.tree_div = document.createElement('div');
19 this.tree_div.hidden = true;
20 this.root_div.appendChild(show_button);
21 this.root_div.appendChild(this.tree_div);
22 }
Alex Perry5f474f22020-02-01 12:14:24 -080023
Alex Perry6249aaf2020-02-29 14:51:49 -080024 handleConfig(config: Configuration) {
25 this.config = config;
26 this.printConfig();
27 }
28
Alex Perry5f474f22020-02-01 12:14:24 -080029 printConfig() {
30 for (const i = 0; i < this.config.channelsLength(); i++) {
31 const channel_div = document.createElement('div');
32 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080033 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080034
35 const input_el = document.createElement('input');
36 input_el.setAttribute('data-index', i);
37 input_el.setAttribute('type', 'checkbox');
38 input_el.addEventListener('click', () => this.handleChange());
39 channel_div.appendChild(input_el);
40
41 const name_div = document.createElement('div');
42 const name_text = document.createTextNode(this.config.channels(i).name());
43 name_div.appendChild(name_text);
44 const type_div = document.createElement('div');
45 const type_text = document.createTextNode(this.config.channels(i).type());
46 type_div.appendChild(type_text);
47 const info_div = document.createElement('div');
48 info_div.appendChild(name_div);
49 info_div.appendChild(type_div);
50
51 channel_div.appendChild(info_div);
52 }
53 }
54
55 handleChange() {
56 const toggles = this.root_div.getElementsByTagName('input');
57 const builder = new flatbuffers.Builder(512);
58
59 const channels: flatbuffers.Offset[] = [];
60 for (const toggle of toggles) {
61 if (!toggle.checked) {
62 continue;
63 }
64 const index = toggle.getAttribute('data-index');
65 const channel = this.config.channels(index);
66 const namefb = builder.createString(channel.name());
67 const typefb = builder.createString(channel.type());
Alex Perryd5e13572020-02-22 15:15:08 -080068 Channel.startChannel(builder);
69 Channel.addName(builder, namefb);
70 Channel.addType(builder, typefb);
71 const channelfb = Channel.endChannel(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080072 channels.push(channelfb);
73 }
74
75 const channelsfb =
Alex Perryb41d5782020-02-09 17:06:40 -080076 Connect.createChannelsToTransferVector(builder, channels);
Alex Perryd5e13572020-02-22 15:15:08 -080077 Connect.startConnect(builder);
78 Connect.addChannelsToTransfer(builder, channelsfb);
79 const connect = Connect.endConnect(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080080 builder.finish(connect);
Alex Perry6249aaf2020-02-29 14:51:49 -080081 this.connection.sendConnectMessage(builder);
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}