blob: 72b496ec4eff7e6bd06edf26fa471922ee7468ce [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');
6
7 constructor(
Alex Perryd5e13572020-02-22 15:15:08 -08008 private readonly config: Configuration,
Alex Perry5f474f22020-02-01 12:14:24 -08009 private readonly dataChannel: RTCDataChannel) {}
10
11 printConfig() {
12 for (const i = 0; i < this.config.channelsLength(); i++) {
13 const channel_div = document.createElement('div');
14 channel_div.classList.add('channel');
15 this.root_div.appendChild(channel_div);
16
17 const input_el = document.createElement('input');
18 input_el.setAttribute('data-index', i);
19 input_el.setAttribute('type', 'checkbox');
20 input_el.addEventListener('click', () => this.handleChange());
21 channel_div.appendChild(input_el);
22
23 const name_div = document.createElement('div');
24 const name_text = document.createTextNode(this.config.channels(i).name());
25 name_div.appendChild(name_text);
26 const type_div = document.createElement('div');
27 const type_text = document.createTextNode(this.config.channels(i).type());
28 type_div.appendChild(type_text);
29 const info_div = document.createElement('div');
30 info_div.appendChild(name_div);
31 info_div.appendChild(type_div);
32
33 channel_div.appendChild(info_div);
34 }
35 }
36
37 handleChange() {
38 const toggles = this.root_div.getElementsByTagName('input');
39 const builder = new flatbuffers.Builder(512);
40
41 const channels: flatbuffers.Offset[] = [];
42 for (const toggle of toggles) {
43 if (!toggle.checked) {
44 continue;
45 }
46 const index = toggle.getAttribute('data-index');
47 const channel = this.config.channels(index);
48 const namefb = builder.createString(channel.name());
49 const typefb = builder.createString(channel.type());
Alex Perryd5e13572020-02-22 15:15:08 -080050 Channel.startChannel(builder);
51 Channel.addName(builder, namefb);
52 Channel.addType(builder, typefb);
53 const channelfb = Channel.endChannel(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080054 channels.push(channelfb);
55 }
56
57 const channelsfb =
Alex Perryb41d5782020-02-09 17:06:40 -080058 Connect.createChannelsToTransferVector(builder, channels);
Alex Perryd5e13572020-02-22 15:15:08 -080059 Connect.startConnect(builder);
60 Connect.addChannelsToTransfer(builder, channelsfb);
61 const connect = Connect.endConnect(builder);
Alex Perry5f474f22020-02-01 12:14:24 -080062 builder.finish(connect);
63 const array = builder.asUint8Array();
64 console.log('connect', array);
65 this.dataChannel.send(array.buffer.slice(array.byteOffset));
66 }
67}