@re-ai/hd-api
Advanced tools
Comparing version 0.0.2 to 0.0.4
export * from './types/TOptions'; | ||
export * from './types/TDevice'; | ||
export * from './types/THDMessage'; | ||
export { DeviceAPIService as ReAIDeviceAPI } from './services/deviceService'; | ||
export { HDMessage as ReAIDeviceMessage } from './services/HDMessage'; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./types/TDevice"), exports); | ||
__exportStar(require("./types/THDMessage"), exports); | ||
var deviceService_1 = require("./services/deviceService"); | ||
@@ -22,0 +23,0 @@ Object.defineProperty(exports, "ReAIDeviceAPI", { enumerable: true, get: function () { return deviceService_1.DeviceAPIService; } }); |
@@ -0,6 +1,7 @@ | ||
import { IWSMessageHDHandler } from "../interfaces/IWSMessageHDHandler"; | ||
import { IWSMessageHDSend } from "../interfaces/IWSMessageHDSend"; | ||
import { HDInfoTypes } from "../types/THDMessage"; | ||
export declare class HDMessage implements IWSMessageHDSend { | ||
client: WebSocket; | ||
constructor(ws: WebSocket); | ||
import { HDInfoTypes, HDWSMessageRecieveKeys, HDWSMessageRecieveResultData } from "../types/THDMessage"; | ||
export declare class HDMessage implements IWSMessageHDSend, IWSMessageHDHandler { | ||
client?: WebSocket; | ||
constructor(ws?: WebSocket); | ||
private send; | ||
@@ -13,2 +14,3 @@ sendForInfoGet(infoType: HDInfoTypes): Promise<void>; | ||
sendForScanQRCode(): void; | ||
handle(message: string, onResult: (msgId: string, key: HDWSMessageRecieveKeys, data: HDWSMessageRecieveResultData) => Promise<any>): Promise<boolean | any>; | ||
} |
@@ -5,2 +5,3 @@ "use strict"; | ||
const THDMessage_1 = require("../types/THDMessage"); | ||
const Logger_1 = require("../utils/Logger"); | ||
class HDMessage { | ||
@@ -12,2 +13,4 @@ client; | ||
send(name, data) { | ||
if (!this.client) | ||
return; | ||
const message = JSON.stringify({ | ||
@@ -22,3 +25,3 @@ jsonrpc: "2.0", | ||
}); | ||
this.client.send(message); | ||
this.client?.send(message); | ||
} | ||
@@ -53,2 +56,21 @@ sendForInfoGet(infoType) { | ||
} | ||
async handle(message, onResult) { | ||
try { | ||
if (!message || typeof message !== "string") { | ||
return false; | ||
} | ||
const json = JSON.parse(message); | ||
if (json.method !== "messages.tool") { | ||
Logger_1.Logger.debug("message handle method", json.method); | ||
return false; | ||
} | ||
const { msgId, content } = json.result; | ||
const { key, result } = content; | ||
return await onResult(msgId, key, result); | ||
} | ||
catch (error) { | ||
Logger_1.Logger.error("message handle err", error); | ||
return Promise.reject(error); | ||
} | ||
} | ||
} | ||
@@ -55,0 +77,0 @@ exports.HDMessage = HDMessage; |
@@ -15,1 +15,46 @@ export declare enum HDInfoTypes { | ||
} | ||
export type HDWSMessageRecieve = { | ||
jsonrpc: string; | ||
method: string; | ||
id: number; | ||
result: HDWSMessageRecieveResult; | ||
}; | ||
export type HDWSMessageRecieveResult = { | ||
msgId: string; | ||
content: { | ||
key: HDWSMessageRecieveKeys; | ||
result: HDWSMessageRecieveResultData; | ||
}; | ||
tool_call_id?: string; | ||
}; | ||
export declare enum HDWSMessageRecieveKeys { | ||
infoWifiList = "wifiList", | ||
infoWifiListSync = "wifiListSync", | ||
infoBluetoothList = "bluetoothList", | ||
infoBluetoothListSync = "bluetoothListSync", | ||
infoGps = "gps", | ||
infoSensor = "sensor" | ||
} | ||
export type HDWSMessageRecieveResultData = { | ||
code: number; | ||
message: "success" | "fail"; | ||
} | HDWifiInfo | HDWifiInfo[] | HDBluetoothInfo | HDBluetoothInfo[] | HDGPSInfo | HDSensorInfo; | ||
export type HDWifiInfo = { | ||
ssid: string; | ||
bssid: string; | ||
signalStrength?: number; | ||
isConnected?: boolean; | ||
}; | ||
export type HDBluetoothInfo = { | ||
name: string; | ||
address: string; | ||
bondStae?: any; | ||
connectionState?: any; | ||
}; | ||
export type HDGPSInfo = { | ||
latitude: number; | ||
longitude: number; | ||
}; | ||
export type HDSensorInfo = { | ||
[key: string]: any; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HDActionNames = exports.HDInfoTypes = void 0; | ||
exports.HDWSMessageRecieveKeys = exports.HDActionNames = exports.HDInfoTypes = void 0; | ||
var HDInfoTypes; | ||
@@ -20,1 +20,10 @@ (function (HDInfoTypes) { | ||
})(HDActionNames || (exports.HDActionNames = HDActionNames = {})); | ||
var HDWSMessageRecieveKeys; | ||
(function (HDWSMessageRecieveKeys) { | ||
HDWSMessageRecieveKeys["infoWifiList"] = "wifiList"; | ||
HDWSMessageRecieveKeys["infoWifiListSync"] = "wifiListSync"; | ||
HDWSMessageRecieveKeys["infoBluetoothList"] = "bluetoothList"; | ||
HDWSMessageRecieveKeys["infoBluetoothListSync"] = "bluetoothListSync"; | ||
HDWSMessageRecieveKeys["infoGps"] = "gps"; | ||
HDWSMessageRecieveKeys["infoSensor"] = "sensor"; | ||
})(HDWSMessageRecieveKeys || (exports.HDWSMessageRecieveKeys = HDWSMessageRecieveKeys = {})); |
@@ -6,2 +6,22 @@ | ||
## 使用方式 | ||
```typescript | ||
import { HDInfoTypes, ReAIDeviceMessage } from "@re-ai/hd-api"; | ||
const wss: WebSocket = [你的websocket对象] | ||
const deviceMessageClient = new ReAIDeviceMessage(wss); | ||
// 发送消息 | ||
deviceMessageClient.sendForInfoGet(HDInfoTypes.wifiList) | ||
deviceMessageClient.sendForWifiSet("your wifi ssid", "your wifi password") | ||
// 消息处理 | ||
const deviceMessageHandler = new ReAIDeviceMessage(wss); | ||
deviceMessageHandler.handle("your message str", (msgId, key, data) => { | ||
console.log(msgId, key, data) | ||
return Promise.resolve(true) | ||
}) | ||
``` | ||
## 枚举类型 | ||
@@ -8,0 +28,0 @@ |
{ | ||
"name": "@re-ai/hd-api", | ||
"version": "0.0.2", | ||
"version": "0.0.4", | ||
"description": "ReAI 硬件接口api", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -12,2 +12,3 @@ | ||
+ [设备api接口](./docs/api.md) | ||
+ [发送ws消息](./docs/ws.md) | ||
+ [发送ws消息](./docs/ws.send.md) | ||
+ [接收ws消息]('./docs/ws.recieve.md') |
export * from './types/TOptions' | ||
export * from './types/TDevice' | ||
export * from './types/THDMessage' | ||
export { DeviceAPIService as ReAIDeviceAPI } from './services/deviceService' | ||
export { HDMessage as ReAIDeviceMessage } from './services/HDMessage' |
@@ -0,8 +1,10 @@ | ||
import { IWSMessageHDHandler } from "../interfaces/IWSMessageHDHandler"; | ||
import { IWSMessageHDSend } from "../interfaces/IWSMessageHDSend"; | ||
import { HDActionNames, HDInfoTypes } from "../types/THDMessage"; | ||
import { HDActionNames, HDInfoTypes, HDWSMessageRecieve, HDWSMessageRecieveKeys, HDWSMessageRecieveResultData } from "../types/THDMessage"; | ||
import { Logger } from "../utils/Logger"; | ||
export class HDMessage implements IWSMessageHDSend { | ||
export class HDMessage implements IWSMessageHDSend, IWSMessageHDHandler { | ||
client: WebSocket | ||
constructor(ws: WebSocket) { | ||
client?: WebSocket | ||
constructor(ws?: WebSocket) { | ||
this.client = ws | ||
@@ -12,2 +14,3 @@ } | ||
private send<T>(name: string, data?: T) { | ||
if (!this.client) return | ||
@@ -23,11 +26,11 @@ const message = JSON.stringify({ | ||
}) | ||
this.client.send(message) | ||
this.client?.send(message) | ||
} | ||
sendForInfoGet(infoType: HDInfoTypes): Promise<void> { | ||
this.send(HDActionNames.INFO_GET, { | ||
key: infoType | ||
}) | ||
return Promise.reject() | ||
@@ -42,3 +45,3 @@ } | ||
} | ||
sendForBluetoothConnect(mac: string): void { | ||
@@ -49,3 +52,3 @@ this.send(HDActionNames.BLUETOOTH_CONNECT, { | ||
} | ||
sendForVolumeSet(volume: number): void { | ||
@@ -64,2 +67,29 @@ this.send(HDActionNames.VOLUME_SET, { | ||
} | ||
async handle(message: string, onResult: ( | ||
msgId: string, | ||
key: HDWSMessageRecieveKeys, | ||
data: HDWSMessageRecieveResultData | ||
) => Promise<any>): Promise<boolean | any> { | ||
try { | ||
if (!message || typeof message !== "string") { | ||
return false | ||
} | ||
const json: HDWSMessageRecieve = JSON.parse(message) | ||
if (json.method !== "messages.tool") { | ||
Logger.debug("message handle method", json.method) | ||
return false | ||
} | ||
const { msgId, content } = json.result | ||
const { key, result } = content | ||
return await onResult(msgId, key, result) | ||
} catch (error) { | ||
Logger.error("message handle err", error) | ||
return Promise.reject(error) | ||
} | ||
} | ||
} | ||
@@ -69,9 +99,9 @@ | ||
function generateRandomString(length: number) { | ||
let result = ''; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
const charactersLength = characters.length; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
return result; | ||
} | ||
let result = ''; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
const charactersLength = characters.length; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
return result; | ||
} |
@@ -15,2 +15,55 @@ export enum HDInfoTypes { | ||
SCAN_QRCODE = "scanQrcode" | ||
} | ||
export type HDWSMessageRecieve = { | ||
jsonrpc: string; | ||
method: string; | ||
id: number; | ||
result: HDWSMessageRecieveResult | ||
} | ||
export type HDWSMessageRecieveResult = { | ||
msgId: string; | ||
content: { | ||
key: HDWSMessageRecieveKeys, | ||
result: HDWSMessageRecieveResultData | ||
}, | ||
tool_call_id?: string | ||
} | ||
export enum HDWSMessageRecieveKeys { | ||
infoWifiList = "wifiList", | ||
infoWifiListSync = "wifiListSync", | ||
infoBluetoothList = "bluetoothList", | ||
infoBluetoothListSync = "bluetoothListSync", | ||
infoGps = "gps", | ||
infoSensor = "sensor" | ||
} | ||
export type HDWSMessageRecieveResultData = { | ||
code: number, | ||
message: "success" | "fail" | ||
} | HDWifiInfo | HDWifiInfo[] | HDBluetoothInfo | HDBluetoothInfo[] | HDGPSInfo | HDSensorInfo | ||
export type HDWifiInfo = { | ||
ssid: string, | ||
bssid: string, | ||
signalStrength?: number, | ||
isConnected?: boolean | ||
} | ||
export type HDBluetoothInfo = { | ||
name: string, | ||
address: string, | ||
bondStae?: any, | ||
connectionState?: any | ||
} | ||
export type HDGPSInfo = { | ||
latitude: number, | ||
longitude: number | ||
} | ||
export type HDSensorInfo = { | ||
[key: string] : any | ||
} |
41760
55
1041
13