105 lines
2.7 KiB
TypeScript
105 lines
2.7 KiB
TypeScript
import { AuthManager } from "@/utils/AuthManager";
|
|
import type {
|
|
RotaryEncoderDirection,
|
|
RotaryEncoderPressStatus,
|
|
} from "@/utils/signalR/Peripherals.RotaryEncoderClient";
|
|
import {
|
|
getHubProxyFactory,
|
|
getReceiverRegister,
|
|
} from "@/utils/signalR/TypedSignalR.Client";
|
|
import type {
|
|
IRotaryEncoderHub,
|
|
IRotaryEncoderReceiver,
|
|
} from "@/utils/signalR/TypedSignalR.Client/server.Hubs";
|
|
import { HubConnectionState, type HubConnection } from "@microsoft/signalr";
|
|
import { isUndefined } from "mathjs";
|
|
import { defineStore } from "pinia";
|
|
import { onMounted, onUnmounted, ref, shallowRef } from "vue";
|
|
|
|
export const useRotaryEncoder = defineStore("RotaryEncoder", () => {
|
|
const rotaryEncoderHub = shallowRef<{
|
|
connection: HubConnection;
|
|
proxy: IRotaryEncoderHub;
|
|
} | null>(null);
|
|
const rotaryEncoderReceiver: IRotaryEncoderReceiver = {
|
|
onReceiveRotate: async (data) => {},
|
|
};
|
|
|
|
onMounted(() => {
|
|
initHub();
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
clearHub();
|
|
});
|
|
|
|
async function initHub() {
|
|
if (rotaryEncoderHub.value) return;
|
|
const connection = AuthManager.createHubConnection("RotaryEncoderHub");
|
|
const proxy =
|
|
getHubProxyFactory("IRotaryEncoderHub").createHubProxy(connection);
|
|
getReceiverRegister("IRotaryEncoderReceiver").register(
|
|
connection,
|
|
rotaryEncoderReceiver,
|
|
);
|
|
await connection.start();
|
|
rotaryEncoderHub.value = { connection, proxy };
|
|
}
|
|
|
|
function clearHub() {
|
|
if (!rotaryEncoderHub.value) return;
|
|
rotaryEncoderHub.value.connection.stop();
|
|
rotaryEncoderHub.value = null;
|
|
}
|
|
|
|
function reinitializeHub() {
|
|
clearHub();
|
|
initHub();
|
|
}
|
|
|
|
function getHubProxy() {
|
|
if (!rotaryEncoderHub.value) {
|
|
reinitializeHub();
|
|
throw new Error("Hub not initialized");
|
|
}
|
|
return rotaryEncoderHub.value.proxy;
|
|
}
|
|
|
|
async function setEnable(enabled: boolean) {
|
|
const proxy = getHubProxy();
|
|
return await proxy.setEnable(enabled);
|
|
}
|
|
|
|
async function rotateOnce(num: number, direction: RotaryEncoderDirection) {
|
|
const proxy = getHubProxy();
|
|
return await proxy.rotateEncoderOnce(num, direction);
|
|
}
|
|
|
|
async function pressOnce(num: number, pressStatus: RotaryEncoderPressStatus) {
|
|
const proxy = getHubProxy();
|
|
return await proxy.pressEncoderOnce(num, pressStatus);
|
|
}
|
|
|
|
async function enableCycleRotate(
|
|
num: number,
|
|
direction: RotaryEncoderDirection,
|
|
freq: number,
|
|
) {
|
|
const proxy = getHubProxy();
|
|
return await proxy.enableCycleRotateEncoder(num, direction, freq);
|
|
}
|
|
|
|
async function disableCycleRotate() {
|
|
const proxy = getHubProxy();
|
|
return await proxy.disableCycleRotateEncoder();
|
|
}
|
|
|
|
return {
|
|
setEnable,
|
|
rotateOnce,
|
|
pressOnce,
|
|
enableCycleRotate,
|
|
disableCycleRotate,
|
|
};
|
|
});
|