From e57938fbda9bff702004038f76d009422968bff5 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 06 十一月 2026 18:05:07 +0800
Subject: [PATCH] 提交代码
---
DeviceService/DeviceManager/DeviceManager.cs | 126 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 126 insertions(+), 0 deletions(-)
diff --git a/DeviceService/DeviceManager/DeviceManager.cs b/DeviceService/DeviceManager/DeviceManager.cs
new file mode 100644
index 0000000..392aba8
--- /dev/null
+++ b/DeviceService/DeviceManager/DeviceManager.cs
@@ -0,0 +1,126 @@
+锘縰sing DeviceService.Core;
+using DeviceService.Service;
+using System.Text.Encodings.Web;
+using System.Text.Json;
+using System.Text.Unicode;
+
+namespace DeviceService.Device
+{
+ /// <summary>
+ /// 鏈嶅姟姝e紡鍚姩鍚庣殑璁惧绠$悊锛氳澶囧疄渚嬬殑鍐呭瓨闆嗗悎銆佺被鍨嬫壂鎻忎笌娉ㄥ唽锛圧egister锛夈��
+ /// 璁惧淇℃伅鐨勬暟鎹簱璇诲啓鐢� <see cref="DeviceHelper"/> 璐熻矗锛屾湰绫讳笉鍋氫换浣曟寔涔呭寲銆�
+ /// </summary>
+ public class DeviceManager
+ {
+
+ private readonly static object _sync = new object();
+ private readonly static Dictionary<string, IDevice> _deviceCollection = new Dictionary<string, IDevice>();
+
+ public static IReadOnlyList<IDevice> Devices
+ {
+ get
+ {
+ lock (_sync)
+ {
+ return _deviceCollection.Values.ToList();
+ }
+ }
+ }
+
+ // ==================== 璁惧娉ㄥ唽 ====================
+
+ /// <summary>鏈嶅姟鍚姩鏃舵敞鍐岃澶囷紙鍐呴儴浼氳皟鐢� Init锛夛紝鏃犺鍒濆鍖栨槸鍚︽垚鍔熼兘浼氬姞鍏ラ泦鍚堛��</summary>
+ public static bool Register(IDevice device, out string msg)
+ {
+ if (device == null) throw new ArgumentNullException(nameof(device));
+
+ bool initOk = device.Init(out msg);
+ if (initOk)
+ {
+ lock (_sync)
+ {
+ _deviceCollection[device.DeviceId] = device;
+ }
+ }
+ return initOk;
+ }
+
+ public static void UnRegisterAll()
+ {
+ lock (_sync)
+ {
+ _deviceCollection.Clear();
+ }
+ }
+
+ /// <summary>
+ /// 鐢ㄦ暟鎹簱璇诲彇鍒扮殑璁惧鍒锋柊鍐呭瓨闆嗗悎锛堜笉璋冪敤 Init锛夈��
+ /// 宸插瓨鍦ㄤ簬鍐呭瓨涓殑瀹炰緥淇濇寔涓嶅彉锛岄伩鍏嶄涪澶卞凡娉ㄥ唽/宸叉墦寮�鐨勮繍琛岀姸鎬侊紱
+ /// 鏂板璁板綍浣跨敤浼犲叆鐨勶紙浠庢暟鎹簱杩樺師鐨勶級瀹炰緥銆�
+ /// </summary>
+ public static void Refresh(IEnumerable<IDevice> devices)
+ {
+ if (devices == null) throw new ArgumentNullException(nameof(devices));
+
+ lock (_sync)
+ {
+ var refreshed = new Dictionary<string, IDevice>(StringComparer.Ordinal);
+ foreach (IDevice device in devices)
+ {
+ if (device == null || string.IsNullOrWhiteSpace(device.DeviceId))
+ {
+ continue;
+ }
+
+ refreshed[device.DeviceId] = _deviceCollection.TryGetValue(device.DeviceId, out IDevice? existing)
+ ? existing
+ : device;
+ }
+
+ _deviceCollection.Clear();
+ foreach (KeyValuePair<string, IDevice> pair in refreshed)
+ {
+ _deviceCollection[pair.Key] = pair.Value;
+ }
+ }
+ }
+
+ /// <summary>鎸� DeviceId 浠庡唴瀛橀泦鍚堢Щ闄よ澶囷紙鏁版嵁搴撹褰曠殑鍒犻櫎鐢� <see cref="DeviceHelper.RemoveDevice"/> 璐熻矗锛夈��</summary>
+ public static bool Remove(string id)
+ {
+ lock (_sync)
+ {
+ return _deviceCollection.Remove(id);
+ }
+ }
+
+ private static readonly JsonSerializerOptions SafeOptions = new()
+ {
+ Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
+ };
+
+ public static void HandleClientMessage(RequestArg arg)
+ {
+ var data = arg.Data;
+ string deviceId = data.DeviceId;
+ if (_deviceCollection.TryGetValue(deviceId, out IDevice device))
+ {
+ var res = device.HandleRequest(data);
+ arg.Client.Send(JsonSerializer.Serialize(res, SafeOptions), out _);
+ }
+ else
+ {
+ var res = new ResponseData
+ {
+ Success = false,
+ Message = "鏃犳晥鐨凞eviceId"
+ };
+ arg.Client.Send(JsonSerializer.Serialize(res, SafeOptions), out _);
+ }
+ }
+
+ public static void ReleaseClient(IClient client) {
+
+ }
+ }
+}
--
Gitblit v1.9.1