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 | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 3 | import {Connection} from './proxy'; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 4 | |
| 5 | export class ConfigHandler { |
| 6 | private readonly root_div = document.getElementById('config'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 7 | private readonly tree_div; |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 8 | private config: Configuration|null = null |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 9 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 10 | constructor(private readonly connection: Connection) { |
| 11 | this.connection.addConfigHandler((config) => this.handleConfig(config)); |
| 12 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 13 | const show_button = document.createElement('button'); |
| 14 | show_button.addEventListener('click', () => this.toggleConfig()); |
| 15 | const show_text = document.createTextNode('Show/Hide Config'); |
| 16 | show_button.appendChild(show_text); |
| 17 | this.tree_div = document.createElement('div'); |
| 18 | this.tree_div.hidden = true; |
| 19 | this.root_div.appendChild(show_button); |
| 20 | this.root_div.appendChild(this.tree_div); |
| 21 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 22 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 23 | handleConfig(config: Configuration) { |
| 24 | this.config = config; |
| 25 | this.printConfig(); |
| 26 | } |
| 27 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 28 | printConfig() { |
| 29 | for (const i = 0; i < this.config.channelsLength(); i++) { |
| 30 | const channel_div = document.createElement('div'); |
| 31 | channel_div.classList.add('channel'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 32 | this.tree_div.appendChild(channel_div); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 33 | |
| 34 | const input_el = document.createElement('input'); |
| 35 | input_el.setAttribute('data-index', i); |
| 36 | input_el.setAttribute('type', 'checkbox'); |
| 37 | input_el.addEventListener('click', () => this.handleChange()); |
| 38 | channel_div.appendChild(input_el); |
| 39 | |
| 40 | const name_div = document.createElement('div'); |
| 41 | const name_text = document.createTextNode(this.config.channels(i).name()); |
| 42 | name_div.appendChild(name_text); |
| 43 | const type_div = document.createElement('div'); |
| 44 | const type_text = document.createTextNode(this.config.channels(i).type()); |
| 45 | type_div.appendChild(type_text); |
| 46 | const info_div = document.createElement('div'); |
| 47 | info_div.appendChild(name_div); |
| 48 | info_div.appendChild(type_div); |
| 49 | |
| 50 | channel_div.appendChild(info_div); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | handleChange() { |
| 55 | const toggles = this.root_div.getElementsByTagName('input'); |
| 56 | const builder = new flatbuffers.Builder(512); |
| 57 | |
| 58 | const channels: flatbuffers.Offset[] = []; |
| 59 | for (const toggle of toggles) { |
| 60 | if (!toggle.checked) { |
| 61 | continue; |
| 62 | } |
| 63 | const index = toggle.getAttribute('data-index'); |
| 64 | const channel = this.config.channels(index); |
| 65 | const namefb = builder.createString(channel.name()); |
| 66 | const typefb = builder.createString(channel.type()); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 67 | Channel.startChannel(builder); |
| 68 | Channel.addName(builder, namefb); |
| 69 | Channel.addType(builder, typefb); |
| 70 | const channelfb = Channel.endChannel(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 71 | channels.push(channelfb); |
| 72 | } |
| 73 | |
| 74 | const channelsfb = |
Alex Perry | b41d578 | 2020-02-09 17:06:40 -0800 | [diff] [blame] | 75 | Connect.createChannelsToTransferVector(builder, channels); |
Alex Perry | d5e1357 | 2020-02-22 15:15:08 -0800 | [diff] [blame] | 76 | Connect.startConnect(builder); |
| 77 | Connect.addChannelsToTransfer(builder, channelsfb); |
| 78 | const connect = Connect.endConnect(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 79 | builder.finish(connect); |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 80 | this.connection.sendConnectMessage(builder); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 81 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 82 | |
| 83 | toggleConfig() { |
| 84 | this.tree_div.hidden = !this.tree_div.hidden; |
| 85 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 86 | } |