Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame^] | 1 | import {Configuration, Channel} from 'aos/configuration_generated'; |
| 2 | import {Connect} from 'aos/network/connect_generated'; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 3 | |
| 4 | export class ConfigHandler { |
| 5 | private readonly root_div = document.getElementById('config'); |
| 6 | |
| 7 | constructor( |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame^] | 8 | private readonly config: Configuration, |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 9 | 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 Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame^] | 50 | Channel.startChannel(builder); |
| 51 | Channel.addName(builder, namefb); |
| 52 | Channel.addType(builder, typefb); |
| 53 | const channelfb = Channel.endChannel(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 54 | channels.push(channelfb); |
| 55 | } |
| 56 | |
| 57 | const channelsfb = |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame^] | 58 | Connect.createChannelsToTransferVector( |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 59 | builder, channels); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame^] | 60 | Connect.startConnect(builder); |
| 61 | Connect.addChannelsToTransfer(builder, channelsfb); |
| 62 | const connect = Connect.endConnect(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 63 | builder.finish(connect); |
| 64 | const array = builder.asUint8Array(); |
| 65 | console.log('connect', array); |
| 66 | this.dataChannel.send(array.buffer.slice(array.byteOffset)); |
| 67 | } |
| 68 | } |