James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 1 | import {ByteBuffer} from 'flatbuffers'; |
| 2 | import {Configuration} from 'org_frc971/aos/configuration_generated'; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 3 | import {Connection} from 'org_frc971/aos/network/www/proxy'; |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 4 | |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 5 | import {Parser, Table} from './reflection' |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 6 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 7 | export class ConfigHandler { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 8 | private readonly root_div = document.createElement('div'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 9 | private readonly tree_div; |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 10 | private config: Configuration|null = null |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 11 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 12 | constructor(private readonly connection: Connection) { |
| 13 | this.connection.addConfigHandler((config) => this.handleConfig(config)); |
| 14 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 15 | document.body.appendChild(this.root_div); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 16 | const show_button = document.createElement('button'); |
| 17 | show_button.addEventListener('click', () => this.toggleConfig()); |
| 18 | const show_text = document.createTextNode('Show/Hide Config'); |
| 19 | show_button.appendChild(show_text); |
| 20 | this.tree_div = document.createElement('div'); |
| 21 | this.tree_div.hidden = true; |
| 22 | this.root_div.appendChild(show_button); |
| 23 | this.root_div.appendChild(this.tree_div); |
| 24 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 25 | |
Alex Perry | 6249aaf | 2020-02-29 14:51:49 -0800 | [diff] [blame] | 26 | handleConfig(config: Configuration) { |
| 27 | this.config = config; |
| 28 | this.printConfig(); |
| 29 | } |
| 30 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 31 | printConfig() { |
James Kuszmaul | 1ec7443 | 2020-07-30 20:26:45 -0700 | [diff] [blame] | 32 | for (let i = 0; i < this.config.channelsLength(); i++) { |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 33 | const channel_div = document.createElement('div'); |
| 34 | channel_div.classList.add('channel'); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 35 | this.tree_div.appendChild(channel_div); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 36 | |
| 37 | const input_el = document.createElement('input'); |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 38 | input_el.setAttribute('data-index', i.toString()); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 39 | input_el.setAttribute('type', 'checkbox'); |
| 40 | input_el.addEventListener('click', () => this.handleChange()); |
| 41 | channel_div.appendChild(input_el); |
| 42 | |
| 43 | const name_div = document.createElement('div'); |
| 44 | const name_text = document.createTextNode(this.config.channels(i).name()); |
| 45 | name_div.appendChild(name_text); |
| 46 | const type_div = document.createElement('div'); |
| 47 | const type_text = document.createTextNode(this.config.channels(i).type()); |
| 48 | type_div.appendChild(type_text); |
| 49 | const info_div = document.createElement('div'); |
| 50 | info_div.appendChild(name_div); |
| 51 | info_div.appendChild(type_div); |
| 52 | |
| 53 | channel_div.appendChild(info_div); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | handleChange() { |
| 58 | const toggles = this.root_div.getElementsByTagName('input'); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 59 | for (const toggle of toggles) { |
| 60 | if (!toggle.checked) { |
| 61 | continue; |
| 62 | } |
| 63 | const index = toggle.getAttribute('data-index'); |
Philipp Schrader | 47445a0 | 2020-11-14 17:31:04 -0800 | [diff] [blame] | 64 | const channel = this.config.channels(Number(index)); |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 65 | this.connection.addHandler( |
| 66 | channel.name(), channel.type(), (data, time) => { |
James Kuszmaul | 5f5e123 | 2020-12-22 20:58:00 -0800 | [diff] [blame] | 67 | const parser = |
| 68 | new Parser(this.connection.getSchema(channel.type())); |
James Kuszmaul | 527038a | 2020-12-21 23:40:44 -0800 | [diff] [blame] | 69 | console.log( |
| 70 | parser.toObject(Table.getRootTable(new ByteBuffer(data)))); |
| 71 | }); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 74 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 75 | |
| 76 | toggleConfig() { |
| 77 | this.tree_div.hidden = !this.tree_div.hidden; |
| 78 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 79 | } |