Skip to content

Commit

Permalink
Fixed reconciliation, added interfaces for controlling reconciliation…
Browse files Browse the repository at this point in the history
… process. Fixed observatory preview and added documentation generator tool.
  • Loading branch information
bcd00 committed Sep 4, 2024
1 parent 44aa3c3 commit 26e7152
Show file tree
Hide file tree
Showing 29 changed files with 1,749 additions and 831 deletions.
14 changes: 6 additions & 8 deletions apps/ove-bridge-ui/src/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
OutboundAPIChannels
} from "../../../ove-bridge/src/ipc-routes";
import type { InboundAPI } from "@ove/ove-types";
import { Toaster } from "@ove/ui-base-components";

declare global {
// noinspection JSUnusedGlobalSymbols
Expand All @@ -27,13 +28,10 @@ declare global {
* OVE Bridge App
* @constructor
*/
const App = () => {
return (
<>
<Nav />
<Router />
</>
);
};
const App = () => <>
<Nav />
<Router />
<Toaster />
</>;

export default App;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export type ConfigurationProps = {
const ConfigurationFormSchema = z.strictObject({
coreURL: z.string().optional(),
bridgeName: z.string().optional(),
calendarURL: z.string().optional()
calendarURL: z.string().optional(),
reconcile: z.boolean()
});

type ConfigurationForm = z.infer<typeof ConfigurationFormSchema>
Expand All @@ -31,21 +32,23 @@ const Configuration = ({ refreshCalendar, openDialog }: ConfigurationProps) => {
resolver: zodResolver(ConfigurationFormSchema)
});
useFormErrorHandling(errors);
const updateEnv = useEnv(setValue);
const updateEnv = useEnv(setValue as Parameters<typeof useEnv>[0]);
const connected = useSocket();

const onSubmit = async ({
calendarURL,
bridgeName,
coreURL
coreURL,
reconcile
}: ConfigurationForm, e: BaseSyntheticEvent<object> | undefined) => {
console.log("SUBMITTING")
if ((e?.nativeEvent as unknown as NativeEvent)
.submitter.name === "auto-mode") {
openDialog();
return;
}

await updateEnv({ coreURL, bridgeName, calendarURL });
await updateEnv({ coreURL, bridgeName, calendarURL, reconcile });
refreshCalendar();
};

Expand All @@ -54,7 +57,7 @@ const Configuration = ({ refreshCalendar, openDialog }: ConfigurationProps) => {
<form
method="post"
onSubmit={handleSubmit(onSubmit)}
className={styles.form}>
className={styles.form} style={{overflowY: "scroll"}}>
<label htmlFor="core-url">Core URL
- {connected ? "connected" : "disconnected"}</label>
<input {...register("coreURL")} type="text" />
Expand All @@ -65,6 +68,10 @@ const Configuration = ({ refreshCalendar, openDialog }: ConfigurationProps) => {
<button id={styles["auto-mode"]} type="submit" name="auto-mode">
Configure Auto Mode
</button>
<div style={{display: "flex", alignItems: "center", marginTop: "2rem"}}>
<label htmlFor="reconcile" style={{height: "100%", marginTop: 0}}>Reconcile</label>
<input {...register("reconcile")} type="checkbox" style={{marginLeft: "auto"}} />
</div>
<button name="update" type="submit">Update / Reconnect</button>
</form>
</section>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ type Env = {
bridgeName?: string
coreURL?: string
calendarURL?: string
reconcile: boolean
}

export const useEnv = (
setValue: (k: keyof Env, v: string | undefined) => void
setValue: <K extends keyof Env>(k: K, v: Env[K]) => void
) => {
useEffect(() => {
window.bridge.getEnv({}).then(env => {
if ("oveError" in env) return;
setValue("coreURL", env.coreURL);
setValue("bridgeName", env.bridgeName);
setValue("calendarURL", env.calendarURL);
setValue("reconcile", env.reconcile);
});
}, [setValue]);

Expand Down
19 changes: 15 additions & 4 deletions apps/ove-bridge/src/app/api/features/bridge/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import {
import { app } from "electron";
import fetch from "node-fetch";
import {
refreshReconciliation,
startReconciliation,
stopReconciliation
} from "../hardware/reconciliation";
import {
service as ReconciliationService
} from "../hardware/reconciliation-service";
import { raise } from "@ove/ove-utils";
import { execSync } from "child_process";
import { env, logger } from "../../../../env";
Expand All @@ -40,10 +42,12 @@ export const service: TBridgeService = {
env.HARDWARE : env.HARDWARE.filter(({ tags }) => tags.includes(tag)),
addDevice: ({ device }) => {
env.HARDWARE.push(device);
ReconciliationService.update();
return true;
},
removeDevice: ({ deviceId }) => {
env.HARDWARE = env.HARDWARE.filter(({ id }) => id !== deviceId);
ReconciliationService.update();
return true;
},
startStreams: () => {
Expand Down Expand Up @@ -101,13 +105,15 @@ export const service: TBridgeService = {
getEnv: () => ({
bridgeName: env.BRIDGE_NAME,
coreURL: env.CORE_URL,
calendarURL: env.CALENDAR_URL
calendarURL: env.CALENDAR_URL,
reconcile: env.RECONCILE
}),
updateEnv: ({ bridgeName, coreURL, calendarURL }) => {
updateEnv: ({ bridgeName, coreURL, calendarURL, reconcile }) => {
if (initHardware_ === null || initBridge_ === null) return;
env.CORE_URL = coreURL;
env.BRIDGE_NAME = bridgeName;
env.CALENDAR_URL = calendarURL;
env.RECONCILE = reconcile;
closeHardwareSocket();
closeSocket();
initHardware_();
Expand All @@ -133,16 +139,21 @@ export const service: TBridgeService = {
getPublicKey: () => env.PUBLIC_KEY,
getAutoSchedule: () => env.AUTO_SCHEDULE,
getGeometry: () => env.GEOMETRY,
getReconciliation: () => env.RECONCILE,
refreshReconciliation: () => {
refreshReconciliation();
if (!env.RECONCILE) return false;
stopReconciliation();
startReconciliation();
return true;
},
startReconciliation: () => {
startReconciliation();
env.RECONCILE = true;
return true;
},
stopReconciliation: () => {
stopReconciliation();
env.RECONCILE = false;
return true;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { assert } from "@ove/ove-utils";
import { env, logger } from "../../../../env";
import { io, type Socket } from "socket.io-client";
import { startReconciliation } from "./reconciliation";
import { startReconciliation, stopReconciliation } from "./reconciliation";

let socket: Socket<
THardwareServerToClientEvents,
Expand All @@ -25,7 +25,19 @@ export const closeHardwareSocket = () => {

export const initHardware = () => {
if (env.CORE_URL === undefined || env.BRIDGE_NAME === undefined) return;
startReconciliation();
if (env.RECONCILE) {
try {
startReconciliation();
} catch (e) {
logger.info(e);
}
} else {
try {
stopReconciliation();
} catch (e) {
logger.info(e);
}
}
socket = io(`${env.CORE_URL}/socket/hardware`, {
autoConnect: false,
path: env.SOCKET_PATH
Expand Down
99 changes: 80 additions & 19 deletions apps/ove-bridge/src/app/api/features/hardware/mdc-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,106 +11,162 @@ import { statusOptions } from "../../utils/status";

const reboot = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"reboot">
args: TBridgeServiceArgs<"reboot">,
ac?: () => AbortController
) => {
const rebootOptsSchema = z.object({}).strict();
const parsedOpts = rebootOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc.setPower("reboot", env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.setPower({
id: 0x01,
timeout: env.MDC_TIMEOUT,
ip,
ac: ac?.(),
port
}, "reboot");
};

const shutdown = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"shutdown">
args: TBridgeServiceArgs<"shutdown">,
ac?: () => AbortController
) => {
const shutdownOptsSchema = z.object({}).strict();
const parsedOpts = shutdownOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc.setPower("off", env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.setPower({
id: 0x01,
timeout: env.MDC_TIMEOUT,
ip,
ac: ac?.(),
port
}, "off");
};

const start = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"start">
args: TBridgeServiceArgs<"start">,
ac?: () => AbortController
) => {
const startOptsSchema = z.object({}).strict();
const parsedOpts = startOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc.setPower("on", env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.setPower({
id: 0x01,
timeout: env.MDC_TIMEOUT,
ip,
ac: ac?.(),
port
}, "on");
};

const getInfo = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"getInfo">
args: TBridgeServiceArgs<"getInfo">,
ac?: () => AbortController
) => {
const infoOptsSchema = z
.object({ type: z.literal("general").optional() }).strict();
const parsedOpts = infoOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc.getInfo(env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.getInfo({
timeout: env.MDC_TIMEOUT,
id: 0x01,
ip,
ac: ac?.(),
port
});
};

const getStatus = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"getStatus">
args: TBridgeServiceArgs<"getStatus">,
ac?: () => AbortController
) => {
const statusOptsSchema = z.object({}).strict();
const parsedOpts = statusOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return statusOptions(async () =>
mdc.getStatus(env.MDC_TIMEOUT, 0x01, ip, port), ip);
mdc.getStatus({
timeout: env.MDC_TIMEOUT,
id: 0x01,
ip,
ac: ac?.(),
port
}), ip);
};

const mute = async ({
ip,
port
}: Device, args: TBridgeServiceArgs<"mute">) => {
}: Device, args: TBridgeServiceArgs<"mute">, ac?: () => AbortController) => {
const muteOptsSchema = z.object({}).strict();
const parsedOpts = muteOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc.setIsMute(true, env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.setIsMute({
id: 0x01,
timeout: env.MDC_TIMEOUT,
ip,
ac: ac?.(),
port
}, true);
};

const unmute = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"unmute">
args: TBridgeServiceArgs<"unmute">,
ac?: () => AbortController
) => {
const unmuteOptsSchema = z.object({}).strict();
const parsedOpts = unmuteOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc.setIsMute(false, env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.setIsMute({
id: 0x01,
timeout: env.MDC_TIMEOUT,
ip,
ac: ac?.(),
port
}, false);
};

const setVolume = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"setVolume">
args: TBridgeServiceArgs<"setVolume">,
ac?: () => AbortController
) => {
const setVolumeOptsSchema = z.object({ volume: z.number() }).strict();
const parsedOpts = setVolumeOptsSchema.safeParse(args);

if (!parsedOpts.success) return undefined;

return mdc
.setVolume(parsedOpts.data.volume, env.MDC_TIMEOUT, 0x01, ip, port);
.setVolume({
id: 0x01,
timeout: env.MDC_TIMEOUT,
ip,
ac: ac?.(),
port
}, parsedOpts.data.volume);
};

const setSource = async (
{ ip, port }: Device,
args: TBridgeServiceArgs<"setSource">
args: TBridgeServiceArgs<"setSource">,
ac?: () => AbortController
) => {
const setSourceOptsSchema = z
.object({ source: MDCSourceSchema.keyof() })
Expand All @@ -119,8 +175,13 @@ const setSource = async (

if (!parsedOpts.success) return undefined;

return mdc.setSource(mdc.sources[parsedOpts.data.source],
env.MDC_TIMEOUT, 0x01, ip, port);
return mdc.setSource({
timeout: env.MDC_TIMEOUT,
id: 0x01,
ip,
ac: ac?.(),
port
}, mdc.sources[parsedOpts.data.source]);
};

const MDCService: TBridgeHardwareService = {
Expand Down
Loading

0 comments on commit 26e7152

Please sign in to comment.