modbus_rtu_device.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { APIClient } from "@d8d-appcontainer/api"
  2. import type { VariablesContext } from "../middlewares.ts";
  3. import type { DeviceInstance } from "../../client/share/monitorTypes.ts";
  4. import { DeviceProtocolType } from "../../client/share/monitorTypes.ts";
  5. import debug from "debug";
  6. import { modbusRTUConnection } from "../utils/modbus_rtu.ts";
  7. const log = debug("d8d:device:controller");
  8. interface ModbusRTUDevice extends DeviceInstance {
  9. config?: {
  10. deviceAddress: number;
  11. ipAddress?: string;
  12. port?: number;
  13. baudRate?: number;
  14. };
  15. }
  16. export async function createModbusRTUDevice(c: VariablesContext) {
  17. try {
  18. const apiClient = c.get('apiClient');
  19. const deviceData: ModbusRTUDevice = await c.req.json();
  20. // 验证协议参数
  21. if (deviceData.protocol === DeviceProtocolType.MODBUS) {
  22. }
  23. // 创建设备记录
  24. await apiClient.database
  25. .table("device_instances")
  26. .insert({
  27. id: deviceData.id,
  28. type_id: deviceData.type_id,
  29. protocol: deviceData.protocol,
  30. address: deviceData.address,
  31. config: JSON.stringify(deviceData.config || {}),
  32. collect_interval: deviceData.collect_interval || 60,
  33. remark: deviceData.remark,
  34. is_enabled: deviceData.is_enabled ?? 1,
  35. is_deleted: 0,
  36. });
  37. // 查询刚创建的设备
  38. const [createdDevice] = await apiClient.database
  39. .table("device_instances")
  40. .where({ id: deviceData.id });
  41. return c.json({ data: createdDevice });
  42. } catch (error: unknown) {
  43. log("创建设备失败:", error);
  44. const message = error instanceof Error ? error.message : "未知错误";
  45. return c.json({ error: `创建设备失败: ${message}` }, 500);
  46. }
  47. }
  48. export async function collectModbusRTUData(c: VariablesContext) {
  49. try {
  50. const apiClient = c.get('apiClient');
  51. const { deviceId } = c.req.param();
  52. const [device] = await apiClient.database
  53. .table("device_instances")
  54. .where({ id: deviceId, is_deleted: 0 });
  55. if (!device) {
  56. return c.json({ error: "设备不存在" }, 404);
  57. }
  58. if (device.protocol !== DeviceProtocolType.MODBUS) {
  59. return c.json({ error: "不支持的协议类型" }, 400);
  60. }
  61. return c.json({ data: null });
  62. } catch (error: unknown) {
  63. log("采集数据失败:", error);
  64. const message = error instanceof Error ? error.message : "未知错误";
  65. return c.json({ error: `采集数据失败: ${message}` }, 500);
  66. }
  67. }
  68. export async function testModbusRTUConnection(c: VariablesContext) {
  69. try {
  70. const { ip, port, frameHexStrings } = await c.req.json();
  71. if (!ip || !port) {
  72. return c.json({ error: "缺少IP或端口参数" }, 400);
  73. }
  74. const result = await modbusRTUConnection({ ip, port, frameHexStrings });
  75. return c.json({ result }, 200);
  76. } catch (error: unknown) {
  77. log("Modbus RTU测试连接错误:", error);
  78. const message = error instanceof Error ? error.message : "未知错误";
  79. return c.json({ error: `测试连接失败: ${message}` }, 500);
  80. }
  81. }