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'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame^] | 6 | private readonly tree_div; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 7 | |
| 8 | constructor( |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 9 | private readonly config: Configuration, |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame^] | 10 | 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 Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 20 | |
| 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 Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame^] | 25 | this.tree_div.appendChild(channel_div); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 26 | |
| 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 Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 60 | Channel.startChannel(builder); |
| 61 | Channel.addName(builder, namefb); |
| 62 | Channel.addType(builder, typefb); |
| 63 | const channelfb = Channel.endChannel(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 64 | channels.push(channelfb); |
| 65 | } |
| 66 | |
| 67 | const channelsfb = |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 68 | Connect.createChannelsToTransferVector(builder, channels); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 69 | Connect.startConnect(builder); |
| 70 | Connect.addChannelsToTransfer(builder, channelsfb); |
| 71 | const connect = Connect.endConnect(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 72 | builder.finish(connect); |
| 73 | const array = builder.asUint8Array(); |
| 74 | console.log('connect', array); |
| 75 | this.dataChannel.send(array.buffer.slice(array.byteOffset)); |
| 76 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame^] | 77 | |
| 78 | toggleConfig() { |
| 79 | this.tree_div.hidden = !this.tree_div.hidden; |
| 80 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 81 | } |