-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interactor: an extremely simple webapp for /
- Loading branch information
Showing
7 changed files
with
291 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import attrs | ||
from interactor.commander.store import Command, reg, store | ||
from photons_web_server import commander | ||
|
||
|
||
@attrs.define | ||
class SIOBody: | ||
path: str | ||
method: str = "GET" | ||
body: dict[str, object] = attrs.field(factory=dict) | ||
params: dict[str, object] = attrs.field(factory=dict) | ||
|
||
|
||
@attrs.define | ||
class InvalidRoute(Exception): | ||
pass | ||
|
||
|
||
@store.command | ||
class SIOCommands(Command): | ||
@classmethod | ||
def add_routes(kls, routes: commander.RouteTransformer) -> None: | ||
routes.sio("command", kls.respond) | ||
|
||
async def respond( | ||
self, | ||
respond: commander.Responder, | ||
message: commander.Message, | ||
) -> None: | ||
route_transformer = self.meta.retrieve_one( | ||
commander.RouteTransformer, type_cache=reg.type_cache | ||
) | ||
body = self.create(SIOBody, message.body) | ||
route = route_transformer.app.router.resolve(path=body.path, method=body.method) | ||
if not route: | ||
await respond(InvalidRoute()) | ||
|
||
handler = route[0].handler | ||
if not route or not (cmd := getattr(handler, "__commander_class__", None)): | ||
await respond(InvalidRoute()) | ||
return | ||
|
||
with route_transformer.instantiate_route(message.request, cmd, handler) as route: | ||
route_args = self.store.determine_http_args_and_kwargs( | ||
self.meta, | ||
route, | ||
respond.progress, | ||
message.request, | ||
(), | ||
{"_body_raw": body.body, "_params_raw": body.params}, | ||
) | ||
response = await route(*route_args) | ||
await respond(response.raw_body) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
apps/interactor/interactor_webapp/interactor/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
apps/interactor/interactor_webapp/interactor/src/lib/commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Socket } from 'socket.io-client'; | ||
import { ulid } from "ulidx"; | ||
import { writable, type Writable } from 'svelte/store'; | ||
|
||
export class Device { | ||
constructor(public serial: string) { | ||
} | ||
} | ||
|
||
type MM = { | ||
[key: string]: unknown | ||
} | ||
|
||
type WaitingMap = { | ||
[key: string]: ((body: MM) => void) | null | ||
}; | ||
|
||
export class Commands { | ||
socket: Socket; | ||
waiting: WaitingMap; | ||
devices: Writable<Array<Device>> | ||
connecting: Writable<boolean> | ||
|
||
constructor(socket: Socket) { | ||
this.socket = socket | ||
this.devices = writable([]) | ||
this.connecting = writable(true) | ||
this.waiting = {}; | ||
|
||
socket.on('connect', this.connect) | ||
socket.on('disconnect', this.disconnect) | ||
socket.on('progress', this.reply); | ||
socket.on('reply', this.reply); | ||
socket.on('error', this.reply); | ||
} | ||
|
||
connect = async (): Promise<void> => { | ||
const serials = await this.refresh_serials(); | ||
this.connecting.set(false) | ||
this.devices.set(serials.map(serial => new Device(serial))) | ||
} | ||
|
||
disconnect = async (): Promise<void> => { | ||
this.connecting.set(true) | ||
} | ||
|
||
reply = (body: MM): void => { | ||
const message_id = body.message_id | ||
if (typeof message_id == "string") { | ||
const handle = this.waiting[message_id] | ||
if (handle) { | ||
handle(body) | ||
} | ||
} | ||
} | ||
|
||
send<T_Ret>(path: string, options: MM, handler: (data: MM) => T_Ret): Promise<T_Ret> { | ||
const socket = this.socket; | ||
if (!socket) { | ||
return Promise.reject("No active socket") | ||
} | ||
return new Promise(resolve => { | ||
const handle = (data: MM): void => { | ||
resolve(handler(data)) | ||
}; | ||
const command = { path: path, message_id: ulid(), ...options }; | ||
this.waiting[command.message_id] = handle | ||
socket.emit('command', command) | ||
}) | ||
} | ||
|
||
refresh_serials(): Promise<Array<string>> { | ||
return new Promise(resolve => { | ||
this.send("/v2/discover/serials", {}, (data: MM) => { | ||
if (data.reply instanceof Object && "error" in data.reply) { | ||
throw new Error(String(data.reply.error)) | ||
} | ||
if (!Array.isArray(data.reply)) { | ||
throw new Error("Reply wasn't an array") | ||
} | ||
resolve(data.reply); | ||
}) | ||
}); | ||
} | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
apps/interactor/interactor_webapp/interactor/src/routes/+page.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
<script> | ||
import { io } from 'socket.io-client'; | ||
import { Commands } from '$lib/commands'; | ||
const socketio = io({ path: '/webapp' }); | ||
const commands = new Commands(socketio); | ||
$: connecting = commands.connecting; | ||
$: devices = commands.devices; | ||
</script> | ||
|
||
<h1>Interactor</h1> | ||
|
||
{#if $connecting} | ||
<p>Connecting...</p> | ||
{:else} | ||
<ul> | ||
{#each $devices as device} | ||
<li>{device.serial}</li> | ||
{/each} | ||
</ul> | ||
{/if} |