feat: 完成前后端旋转编码器的数字孪生
This commit is contained in:
92
src/stores/Peripherals/RotaryEncoder.ts
Normal file
92
src/stores/Peripherals/RotaryEncoder.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { AuthManager } from "@/utils/AuthManager";
|
||||
import type { RotaryEncoderDirection } 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();
|
||||
});
|
||||
|
||||
function initHub() {
|
||||
if (rotaryEncoderHub.value) return;
|
||||
const connection = AuthManager.createHubConnection("RotaryEncoderHub");
|
||||
const proxy =
|
||||
getHubProxyFactory("IRotaryEncoderHub").createHubProxy(connection);
|
||||
getReceiverRegister("IRotaryEncoderReceiver").register(
|
||||
connection,
|
||||
rotaryEncoderReceiver,
|
||||
);
|
||||
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) 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 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,
|
||||
enableCycleRotate,
|
||||
disableCycleRotate,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user