Mercurial > code > home > repos > light-bridge
annotate src/main.ts @ 13:1c865af058e7
start make* funcs and add links to light addresses
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jan 2024 20:03:20 -0800 |
parents | 7cc004eafb82 |
children | 412d37f707c9 |
rev | line source |
---|---|
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
1 import { LitElement, TemplateResult, css, html } from "lit"; |
0 | 2 import { customElement, state } from "lit/decorators.js"; |
3 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
4 interface Light { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
5 name: string; |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
6 address: { url?: string; label: string }; |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
7 requestingColor: string; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
8 requestingDeviceColor: object; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
9 emittingColor: string; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
10 online: boolean; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
11 latencyMs: number; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
12 } |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
13 |
0 | 14 @customElement("lb-page") |
15 export class LbPage extends LitElement { | |
16 static styles = [ | |
17 css` | |
18 :host { | |
19 display: flex; | |
20 flex-direction: column; | |
21 height: 100vh; | |
22 } | |
23 table { | |
24 border-collapse: collapse; | |
25 } | |
26 td, | |
27 th { | |
28 border: 1px solid #aaa; | |
29 text-align: center; | |
30 } | |
31 .color { | |
32 display: inline-block; | |
33 width: 30px; | |
34 height: 30px; | |
35 border-radius: 50%; | |
36 margin: 3px; | |
37 vertical-align: middle; | |
38 } | |
9 | 39 .col-group-1 { |
40 background: #e0ecf4; | |
41 } | |
42 .col-group-2 { | |
43 background: #e0f3db; | |
44 } | |
45 .col-group-3 { | |
46 background: #fee8c8; | |
47 } | |
0 | 48 `, |
49 ]; | |
3 | 50 |
51 // bug https://github.com/lit/lit/issues/4305 | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
52 @((state as any)()) lights: Light[] = []; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
53 @((state as any)()) lightByName: Map<string, Light> = new Map(); |
9 | 54 @((state as any)()) reportTime: Date = new Date(0); |
0 | 55 |
56 connectedCallback(): void { | |
57 super.connectedCallback(); | |
58 const es = new EventSource("api/table"); | |
59 es.onmessage = (ev) => { | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
60 console.log("got table update"); |
1 | 61 const body = JSON.parse(ev.data); |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
62 this.lights = body.lights.map((wrappedLight: any) => wrappedLight.light as Light); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
63 this.rebuildLightByName(); |
1 | 64 this.reportTime = new Date(body.now * 1000); |
0 | 65 }; |
66 } | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
67 private rebuildLightByName() { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
68 this.lightByName = new Map(); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
69 this.lights.forEach((light: any) => { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
70 this.lightByName.set(light.name, light); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
71 }); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
72 } |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
73 |
0 | 74 render() { |
75 return html` | |
76 <h1>Light-bridge</h1> | |
77 | |
78 <table> | |
12 | 79 ${this.renderLightHeaders()} ${this.lights.map(this.renderLightRow.bind(this))} |
0 | 80 </table> |
1 | 81 <p>Updated ${this.reportTime.toLocaleString("sv")}</p> |
0 | 82 <p> |
83 <a href="metrics">metrics</a> | | |
84 <a href="api/graph">graph</a> | |
85 </p> | |
86 <bigast-loginbar></bigast-loginbar> | |
87 `; | |
88 } | |
12 | 89 |
90 renderLightHeaders() { | |
91 return html`<tr> | |
92 <th class="col-group-1">light</th> | |
93 <th class="col-group-1">address</th> | |
94 <th class="col-group-2">requested color</th> | |
95 <th class="col-group-2">requested device color</th> | |
96 <th class="col-group-3">emitting color</th> | |
97 <th class="col-group-3">online</th> | |
98 <th class="col-group-3">latency</th> | |
99 </tr>`; | |
100 } | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
101 |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
102 renderLightRow(d: Light) { |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
103 return html` |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
104 <tr> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
105 <td class="col-group-1">${d.name}</td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
106 <td class="col-group-1"><code>${this.renderLinked(d.address)}</code></td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
107 <td class="col-group-2"> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
108 <code>${d.requestingColor}</code> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
109 <input type="color" @input=${this.onRequestingColor.bind(this, d.name)} .value="${d.requestingColor}" /> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
110 </td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
111 <td class="col-group-2"><code>${JSON.stringify(d.requestingDeviceColor)}</code></td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
112 <td class="col-group-3">${d.emittingColor} <span class="color" style="background: ${d.emittingColor}"></span></td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
113 <td class="col-group-3">${d.online ? "✔" : ""}</td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
114 <td class="col-group-3">${d.latencyMs} ms</td> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
115 </tr> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
116 `; |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
117 } |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
118 |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
119 private renderLinked(link: { url?: string; label: string }): TemplateResult { |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
120 var addr = html`${link.label}`; |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
121 if (link.url) { |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
122 addr = html`<a href="${link.url}">${addr}</a>`; |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
123 } |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
124 return addr; |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
125 } |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
126 |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
127 async onRequestingColor(lightName: string, ev: InputEvent) { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
128 const currentRequest = this.lightByName.get(lightName)!.requestingColor; |
0 | 129 const value = (ev.target as HTMLInputElement).value; |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
130 console.log("LbPage ~ onRequestingColor ~ value === currentRequest:", value, currentRequest); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
131 if (value === currentRequest) { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
132 return; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
133 } |
0 | 134 const url = new URL("api/output", location as any); |
135 url.searchParams.append("light", lightName); | |
136 fetch(url, { method: "PUT", body: value }); // todo: only run one fetch at a time, per light | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
137 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
138 // sometime after this, the SSE table will send us back the change we made (probably) |
0 | 139 } |
140 } |