blob: 0af78b6706b135b4d59ebc7fbc92067e38ad8814 [file] [log] [blame]
Alex Perry5f474f22020-02-01 12:14:24 -08001import {aos} from 'aos/network/web_proxy_generated';
2
3export class ConfigHandler {
4 private readonly root_div = document.getElementById('config');
5
6 constructor(
7 private readonly config: aos.Configuration,
8 private readonly dataChannel: RTCDataChannel) {}
9
10 printConfig() {
11 for (const i = 0; i < this.config.channelsLength(); i++) {
12 const channel_div = document.createElement('div');
13 channel_div.classList.add('channel');
14 this.root_div.appendChild(channel_div);
15
16 const input_el = document.createElement('input');
17 input_el.setAttribute('data-index', i);
18 input_el.setAttribute('type', 'checkbox');
19 input_el.addEventListener('click', () => this.handleChange());
20 channel_div.appendChild(input_el);
21
22 const name_div = document.createElement('div');
23 const name_text = document.createTextNode(this.config.channels(i).name());
24 name_div.appendChild(name_text);
25 const type_div = document.createElement('div');
26 const type_text = document.createTextNode(this.config.channels(i).type());
27 type_div.appendChild(type_text);
28 const info_div = document.createElement('div');
29 info_div.appendChild(name_div);
30 info_div.appendChild(type_div);
31
32 channel_div.appendChild(info_div);
33 }
34 }
35
36 handleChange() {
37 const toggles = this.root_div.getElementsByTagName('input');
38 const builder = new flatbuffers.Builder(512);
39
40 const channels: flatbuffers.Offset[] = [];
41 for (const toggle of toggles) {
42 if (!toggle.checked) {
43 continue;
44 }
45 const index = toggle.getAttribute('data-index');
46 const channel = this.config.channels(index);
47 const namefb = builder.createString(channel.name());
48 const typefb = builder.createString(channel.type());
49 aos.Channel.startChannel(builder);
50 aos.Channel.addName(builder, namefb);
51 aos.Channel.addType(builder, typefb);
52 const channelfb = aos.Channel.endChannel(builder);
53 channels.push(channelfb);
54 }
55
56 const channelsfb =
57 aos.message_bridge.Connect.createChannelsToTransferVector(
58 builder, channels);
59 aos.message_bridge.Connect.startConnect(builder);
60 aos.message_bridge.Connect.addChannelsToTransfer(builder, channelsfb);
61 const connect = aos.message_bridge.Connect.endConnect(builder);
62 builder.finish(connect);
63 const array = builder.asUint8Array();
64 console.log('connect', array);
65 this.dataChannel.send(array.buffer.slice(array.byteOffset));
66 }
67}