blob: ae9896ced80d179016e1c9b55fa6783f1f66667e [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 Perry5f474f22020-02-01 12:14:24 -08003
4export class ConfigHandler {
5 private readonly root_div = document.getElementById('config');
Alex Perry5427c9a2020-02-15 17:43:45 -08006 private readonly tree_div;
Alex Perry5f474f22020-02-01 12:14:24 -08007
8 constructor(
Alex Perryd5e13572020-02-22 15:15:08 -08009 private readonly config: Configuration,
Alex Perry5427c9a2020-02-15 17:43:45 -080010 private readonly dataChannel: RTCDataChannel) {
11 const show_button = document.createElement('button');
12 show_button.addEventListener('click', () => this.toggleConfig());
13 const show_text = document.createTextNode('Show/Hide Config');
14 show_button.appendChild(show_text);
15 this.tree_div = document.createElement('div');
16 this.tree_div.hidden = true;
17 this.root_div.appendChild(show_button);
18 this.root_div.appendChild(this.tree_div);
19 }
Alex Perry5f474f22020-02-01 12:14:24 -080020
21 printConfig() {
22 for (const i = 0; i < this.config.channelsLength(); i++) {
23 const channel_div = document.createElement('div');
24 channel_div.classList.add('channel');
Alex Perry5427c9a2020-02-15 17:43:45 -080025 this.tree_div.appendChild(channel_div);
Alex Perry5f474f22020-02-01 12:14:24 -080026
27 const input_el = document.createElement('input');
28 input_el.setAttribute('data-index', i);
29 input_el.setAttribute('type', 'checkbox');
30 input_el.addEventListener('click', () => this.handleChange());
31 channel_div.appendChild(input_el);
32
33 const name_div = document.createElement('div');
34 const name_text = document.createTextNode(this.config.channels(i).name());
35 name_div.appendChild(name_text);
36 const type_div = document.createElement('div');
37 const type_text = document.createTextNode(this.config.channels(i).type());
38 type_div.appendChild(type_text);
39 const info_div = document.createElement('div');
40 info_div.appendChild(name_div);
41 info_div.appendChild(type_div);
42
43 channel_div.appendChild(info_div);
44 }
45 }
46
47 handleChange() {
48 const toggles = this.root_div.getElementsByTagName('input');
49 const builder = new flatbuffers.Builder(512);
50
51 const channels: flatbuffers.Offset[] = [];
52 for (const toggle of toggles) {
53 if (!toggle.checked) {
54 continue;
55 }
56 const index = toggle.getAttribute('data-index');
57 const channel = this.config.channels(index);
58 const namefb = builder.createString(channel.name());
59 const typefb = builder.createString(channel.type());
Alex Perryd5e13572020-02-22 15:15:08 -080060 Channel.startChannel(builder);
61 Channel.addName(builder, namefb);
62 Channel.addType(builder, typefb);
63 const channelfb = Channel.endChannel(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080064 channels.push(channelfb);
65 }
66
67 const channelsfb =
Alex Perryb41d5782020-02-09 17:06:40 -080068 Connect.createChannelsToTransferVector(builder, channels);
Alex Perryd5e13572020-02-22 15:15:08 -080069 Connect.startConnect(builder);
70 Connect.addChannelsToTransfer(builder, channelsfb);
71 const connect = Connect.endConnect(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080072 builder.finish(connect);
73 const array = builder.asUint8Array();
74 console.log('connect', array);
75 this.dataChannel.send(array.buffer.slice(array.byteOffset));
76 }
Alex Perry5427c9a2020-02-15 17:43:45 -080077
78 toggleConfig() {
79 this.tree_div.hidden = !this.tree_div.hidden;
80 }
Alex Perry5f474f22020-02-01 12:14:24 -080081}