|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
export type ManualAction =
|
|
|
|
'init' | 'release' | 'write' | 'report' | 'lock' | 'unlock' |
|
|
|
|
'export' | 'cancel' | 'import' | 'upgrade'
|
|
|
|
|
|
|
|
export type ZmqStatus = 'disconnected' | 'connected'
|
|
|
|
|
|
|
|
export enum WorkerCMD {
|
|
|
|
START,
|
|
|
|
SUBSCRIBE,
|
|
|
|
UNSUBSCRIBE,
|
|
|
|
PUBLISH,
|
|
|
|
STOP,
|
|
|
|
SET_TIMEOUT
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum ZmqMsgResultType {
|
|
|
|
SUCCESS = 200,
|
|
|
|
PROGRESS = 1002,
|
|
|
|
ERROR = 1003,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export enum ZmqCMD {
|
|
|
|
MSG,
|
|
|
|
JSON_MSG,
|
|
|
|
STATUS,
|
|
|
|
TIMEOUT
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TimeoutMsg {
|
|
|
|
timeoutId: string
|
|
|
|
timeoutTopic: string
|
|
|
|
}
|
|
|
|
export interface ZmqMessage {
|
|
|
|
cmd: ZmqCMD
|
|
|
|
msg: string
|
|
|
|
community: boolean
|
|
|
|
topic: string
|
|
|
|
}
|
|
|
|
type TopicType = 'event' | 'status'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// web端发布消息类型
|
|
|
|
export interface PublishMsg<A> {
|
|
|
|
id: string // 每条消息的唯一标识
|
|
|
|
action: A // 消息的动作
|
|
|
|
reply: 'yes' | 'no' // 是否需要回复
|
|
|
|
params: any // 消息的参数
|
|
|
|
}
|
|
|
|
// web端发布消息服务端返回的数据类型
|
|
|
|
export interface PubMsgData {
|
|
|
|
code: number
|
|
|
|
feedback: any,
|
|
|
|
id: string
|
|
|
|
result: 'success' | 'progress' | 'failure' | 'refuse' | 'error'
|
|
|
|
}
|
|
|
|
|
|
|
|
// 订阅消息服务端返回的数据类型
|
|
|
|
export interface SubMsgData {
|
|
|
|
id: string
|
|
|
|
action: 'report'
|
|
|
|
feedback: any,
|
|
|
|
reply: 'yes' | 'no' // 是否需要回复
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @Description: 获取发布的主题
|
|
|
|
* type: 主题类型
|
|
|
|
* module: 模块名称
|
|
|
|
* userid: 用户唯一标识
|
|
|
|
*/
|
|
|
|
|
|
|
|
export function getPubTopic(type: TopicType, module: string,) {
|
|
|
|
return `web/${type}/${module}/`
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSubTopic(server: string, type: TopicType, module: string,) {
|
|
|
|
return `${server}/${type}/${module}/`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取随机id
|
|
|
|
|
|
|
|
export function getRandomId() {
|
|
|
|
const uniqueId = `${Date.now()}-${uuidv4()}`;
|
|
|
|
return uniqueId
|
|
|
|
}
|
|
|
|
|
|
|
|
export function generatePubMsg<A>(massage: Omit<PublishMsg<A>, 'id'>): PublishMsg<A> {
|
|
|
|
return {
|
|
|
|
id: getRandomId(),
|
|
|
|
action: massage.action,
|
|
|
|
reply: massage.reply,
|
|
|
|
params: massage.params
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getLockPubMsg(action: 'lock' | 'unlock'): PublishMsg<'lock' | 'unlock'> {
|
|
|
|
return {
|
|
|
|
id: getRandomId(),
|
|
|
|
action: action,
|
|
|
|
params: [],
|
|
|
|
reply: 'yes'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pollingWithCallback(intervalInSeconds: number, callback: Function) {
|
|
|
|
let counter = 0
|
|
|
|
const fps = 60
|
|
|
|
|
|
|
|
function poll() {
|
|
|
|
counter++
|
|
|
|
if (counter >= intervalInSeconds * fps) {
|
|
|
|
callback()
|
|
|
|
counter = 0
|
|
|
|
}
|
|
|
|
requestAnimationFrame(poll)
|
|
|
|
}
|
|
|
|
|
|
|
|
poll()
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getPubInitData<T extends ManualAction>(action: T, params: any, reply: 'yes' | 'no' = 'yes') {
|
|
|
|
return generatePubMsg<T>({
|
|
|
|
action,
|
|
|
|
reply,
|
|
|
|
params
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export function isHexadecimal(text: string) {
|
|
|
|
const hexRegex = /^[0-9A-Fa-f]+$/
|
|
|
|
return hexRegex.test(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stringToHex(str: string) {
|
|
|
|
return Array.from(str)
|
|
|
|
.map(char => char.charCodeAt(0).toString(16).toUpperCase())
|
|
|
|
.join('')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function stringToDecimalismNumbers(str: string): number[] {
|
|
|
|
return Array.from(str).map(char => char.charCodeAt(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hexToArray(num: string) {
|
|
|
|
const numStr = num.toString()
|
|
|
|
let paddedNumStr = numStr
|
|
|
|
if (numStr.length % 2 !== 0) {
|
|
|
|
paddedNumStr = numStr.slice(0, -1) + '0' + numStr.slice(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = []
|
|
|
|
|
|
|
|
for (let i = 0; i < paddedNumStr.length; i += 2) {
|
|
|
|
result.push(paddedNumStr.slice(i, i + 2))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hexToDecimal(hex: string[]) {
|
|
|
|
return hex.map(item => parseInt(item, 16))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decimalToHexArray(decimalArray: number[]) {
|
|
|
|
return decimalArray.map(num => num.toString(16).toUpperCase()).join('')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decimalToString(arr: number[]) {
|
|
|
|
return arr.map(num => String.fromCharCode(num)).join('')
|
|
|
|
}
|