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