Mercurial > code > home > repos > light-bridge
annotate src/main.ts @ 25:cee43f550577
add /lightNames
author | drewp@bigasterisk.com |
---|---|
date | Fri, 02 Feb 2024 20:52:09 -0800 |
parents | 178e020289c1 |
children |
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 } | |
18 | 48 |
49 @media (max-width: 1200px) { | |
50 .opt { | |
51 display: none; | |
52 } | |
53 } | |
0 | 54 `, |
55 ]; | |
3 | 56 |
57 // 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
|
58 @((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
|
59 @((state as any)()) lightByName: Map<string, Light> = new Map(); |
9 | 60 @((state as any)()) reportTime: Date = new Date(0); |
0 | 61 |
62 connectedCallback(): void { | |
63 super.connectedCallback(); | |
64 const es = new EventSource("api/table"); | |
65 es.onmessage = (ev) => { | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
66 console.log("got table update"); |
1 | 67 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
|
68 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
|
69 this.rebuildLightByName(); |
1 | 70 this.reportTime = new Date(body.now * 1000); |
0 | 71 }; |
72 } | |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
73 private rebuildLightByName() { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
74 this.lightByName = new Map(); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
75 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
|
76 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
|
77 }); |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
78 } |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
79 |
0 | 80 render() { |
81 return html` | |
82 <h1>Light-bridge</h1> | |
83 | |
84 <table> | |
12 | 85 ${this.renderLightHeaders()} ${this.lights.map(this.renderLightRow.bind(this))} |
0 | 86 </table> |
1 | 87 <p>Updated ${this.reportTime.toLocaleString("sv")}</p> |
0 | 88 <p> |
22 | 89 <a href="metrics">metrics</a> | <a href="https://bigasterisk.com/code/light-bridge/files/tip/">code</a> | |
0 | 90 <a href="api/graph">graph</a> |
91 </p> | |
92 <bigast-loginbar></bigast-loginbar> | |
93 `; | |
94 } | |
12 | 95 |
96 renderLightHeaders() { | |
97 return html`<tr> | |
98 <th class="col-group-1">light</th> | |
99 <th class="col-group-1">address</th> | |
100 <th class="col-group-2">requested color</th> | |
18 | 101 <th class="col-group-2 opt">requested device color</th> |
12 | 102 <th class="col-group-3">emitting color</th> |
18 | 103 <th class="col-group-3 opt">online</th> |
104 <th class="col-group-3 opt">latency</th> | |
12 | 105 </tr>`; |
106 } | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
107 |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
108 renderLightRow(d: Light) { |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
109 return html` |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
110 <tr> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
111 <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
|
112 <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
|
113 <td class="col-group-2"> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
114 <code>${d.requestingColor}</code> |
22 | 115 <input type="color" @input=${this.onRequestingColor.bind(this, d.name, null)} .value="${d.requestingColor}" /> |
116 <button @click=${this.onRequestingColor.bind(this, d.name, "#000000")}>off</button> | |
117 <button @click=${this.onRequestingColor.bind(this, d.name, "#ffffff")}>full</button> | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
118 </td> |
18 | 119 <td class="col-group-2 opt"><code>${JSON.stringify(d.requestingDeviceColor)}</code></td> |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
120 <td class="col-group-3">${d.emittingColor} <span class="color" style="background: ${d.emittingColor}"></span></td> |
18 | 121 <td class="col-group-3 opt">${d.online ? "✔" : ""}</td> |
122 <td class="col-group-3 opt">${d.latencyMs} ms</td> | |
13
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
123 </tr> |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
124 `; |
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 |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
127 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
|
128 var addr = html`${link.label}`; |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
129 if (link.url) { |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
130 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
|
131 } |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
132 return addr; |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
133 } |
1c865af058e7
start make* funcs and add links to light addresses
drewp@bigasterisk.com
parents:
12
diff
changeset
|
134 |
22 | 135 async onRequestingColor(lightName: string, preset: string | null, ev: InputEvent) { |
11
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
136 const currentRequest = this.lightByName.get(lightName)!.requestingColor; |
22 | 137 const value = preset || (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
|
138 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
|
139 if (value === currentRequest) { |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
140 return; |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
141 } |
0 | 142 const url = new URL("api/output", location as any); |
143 url.searchParams.append("light", lightName); | |
144 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
|
145 |
028ed39aa78f
more ts types; attempted multiplayer but the change events are managed wrong
drewp@bigasterisk.com
parents:
9
diff
changeset
|
146 // sometime after this, the SSE table will send us back the change we made (probably) |
0 | 147 } |
148 } |