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'; |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame^] | 5 | import {Parser, Table} from './reflection' |
| 6 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 7 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 8 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 9 | import Configuration = configuration.aos.Configuration; |
| 10 | import Channel = configuration.aos.Channel; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 11 | import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest; |
| 12 | import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest; |
| 13 | import TransferMethod = web_proxy.aos.web_proxy.TransferMethod; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 14 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 15 | export class ConfigHandler { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 16 | private readonly root_div = document.createElement('div'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 17 | private readonly tree_div; |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 18 | private config: Configuration|null = null |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 19 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 20 | constructor(private readonly connection: Connection) { |
| 21 | this.connection.addConfigHandler((config) => this.handleConfig(config)); |
| 22 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 23 | document.body.appendChild(this.root_div); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 24 | const show_button = document.createElement('button'); |
| 25 | show_button.addEventListener('click', () => this.toggleConfig()); |
| 26 | const show_text = document.createTextNode('Show/Hide Config'); |
| 27 | show_button.appendChild(show_text); |
| 28 | this.tree_div = document.createElement('div'); |
| 29 | this.tree_div.hidden = true; |
| 30 | this.root_div.appendChild(show_button); |
| 31 | this.root_div.appendChild(this.tree_div); |
| 32 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 33 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 34 | handleConfig(config: Configuration) { |
| 35 | this.config = config; |
| 36 | this.printConfig(); |
| 37 | } |
| 38 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 39 | printConfig() { |
James Kuszmaul | 1ec7443 | 2020-07-30 20:26:45 -0700 | [diff] [blame] | 40 | for (let i = 0; i < this.config.channelsLength(); i++) { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 41 | const channel_div = document.createElement('div'); |
| 42 | channel_div.classList.add('channel'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 43 | this.tree_div.appendChild(channel_div); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 44 | |
| 45 | const input_el = document.createElement('input'); |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 46 | input_el.setAttribute('data-index', i.toString()); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 47 | input_el.setAttribute('type', 'checkbox'); |
| 48 | input_el.addEventListener('click', () => this.handleChange()); |
| 49 | channel_div.appendChild(input_el); |
| 50 | |
| 51 | const name_div = document.createElement('div'); |
| 52 | const name_text = document.createTextNode(this.config.channels(i).name()); |
| 53 | name_div.appendChild(name_text); |
| 54 | const type_div = document.createElement('div'); |
| 55 | const type_text = document.createTextNode(this.config.channels(i).type()); |
| 56 | type_div.appendChild(type_text); |
| 57 | const info_div = document.createElement('div'); |
| 58 | info_div.appendChild(name_div); |
| 59 | info_div.appendChild(type_div); |
| 60 | |
| 61 | channel_div.appendChild(info_div); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | handleChange() { |
| 66 | const toggles = this.root_div.getElementsByTagName('input'); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 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)); |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame^] | 73 | this.connection.addHandler( |
| 74 | channel.name(), channel.type(), (data, time) => { |
| 75 | const config = this.connection.getConfig(); |
| 76 | let schema = null; |
| 77 | for (let ii = 0; ii < config.channelsLength(); ++ii) { |
| 78 | if (config.channels(ii).type() === channel.type()) { |
| 79 | schema = config.channels(ii).schema(); |
| 80 | } |
| 81 | } |
| 82 | const parser = new Parser(schema); |
| 83 | console.log( |
| 84 | parser.toObject(Table.getRootTable(new ByteBuffer(data)))); |
| 85 | }); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 88 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 89 | |
| 90 | toggleConfig() { |
| 91 | this.tree_div.hidden = !this.tree_div.hidden; |
| 92 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 93 | } |