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