From 9fb0938c15bdc4577025e217622df17e8afeff08 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期日, 19 四月 2026 19:24:42 +0800
Subject: [PATCH] feat(RobotState): 实现 RobotStateRepository,封装 RowVersion 乐观锁
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoRepository/RobotStateRepository.cs | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 174 insertions(+), 0 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoRepository/RobotStateRepository.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoRepository/RobotStateRepository.cs
new file mode 100644
index 0000000..d282af4
--- /dev/null
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoRepository/RobotStateRepository.cs
@@ -0,0 +1,174 @@
+using Newtonsoft.Json;
+using SqlSugar;
+using WIDESEAWCS_Core.BaseRepository;
+using WIDESEAWCS_Core.UnitOfWork;
+using WIDESEAWCS_ITaskInfoRepository;
+using WIDESEAWCS_Model.Models;
+
+namespace WIDESEAWCS_TaskInfoRepository
+{
+ /// <summary>
+ /// 鏈烘鎵嬬姸鎬� SqlSugar 浠撳偍瀹炵幇
+ /// </summary>
+ public class RobotStateRepository : IUnitOfWork, IRobotStateRepository
+ {
+ private readonly IUnitOfWorkManage _unitOfWork;
+ private readonly SqlSugarClient _db;
+
+ public RobotStateRepository(IUnitOfWorkManage unitOfWork)
+ {
+ _unitOfWork = unitOfWork;
+ _db = unitOfWork.GetDbClient();
+ }
+
+ public Dt_RobotState? GetByIp(string ipAddress)
+ {
+ return _db.Queryable<Dt_RobotState>()
+ .Where(x => x.IPAddress == ipAddress)
+ .First();
+ }
+
+ public Dt_RobotState GetOrCreate(string ipAddress, RobotCraneDevice robotCrane)
+ {
+ var existing = GetByIp(ipAddress);
+ if (existing != null)
+ {
+ return existing;
+ }
+
+ var newState = new Dt_RobotState
+ {
+ IPAddress = ipAddress,
+ RobotCraneJson = JsonConvert.SerializeObject(robotCrane),
+ CreateTime = DateTime.Now,
+ UpdateTime = DateTime.Now
+ };
+
+ _db.Insertable(newState).ExecuteCommand();
+ return newState;
+ }
+
+ public bool TryUpdate(string ipAddress, Dt_RobotState newState, byte[] expectedRowVersion)
+ {
+ newState.UpdateTime = DateTime.Now;
+
+ var affectedRows = _db.Updateable<Dt_RobotState>(newState)
+ .Where(x => x.IPAddress == ipAddress)
+ .WhereRowVersion(x => x.RowVersion, expectedRowVersion)
+ .ExecuteCommand();
+
+ return affectedRows > 0;
+ }
+
+ public RobotSocketState ToSocketState(Dt_RobotState entity)
+ {
+ var state = new RobotSocketState
+ {
+ IPAddress = entity.IPAddress,
+ Version = BitConverter.ToInt64(entity.RowVersion.Length >= 8 ? entity.RowVersion.Take(8).ToArray() : new byte[8], 0),
+ IsEventSubscribed = entity.IsEventSubscribed,
+ RobotRunMode = entity.RobotRunMode,
+ RobotControlMode = entity.RobotControlMode,
+ RobotArmObject = entity.RobotArmObject,
+ Homed = entity.Homed,
+ CurrentAction = entity.CurrentAction,
+ OperStatus = entity.OperStatus,
+ IsSplitPallet = entity.IsSplitPallet,
+ IsGroupPallet = entity.IsGroupPallet,
+ RobotTaskTotalNum = entity.RobotTaskTotalNum,
+ IsInFakeBatteryMode = entity.IsInFakeBatteryMode,
+ CurrentBatchIndex = entity.CurrentBatchIndex,
+ ChangePalletPhase = entity.ChangePalletPhase,
+ IsScanNG = entity.IsScanNG,
+ BatteryArrived = entity.BatteryArrived
+ };
+
+ // 鍙嶅簭鍒楀寲澶嶆潅 JSON 瀛楁
+ if (!string.IsNullOrEmpty(entity.RobotCraneJson))
+ {
+ state.RobotCrane = JsonConvert.DeserializeObject<RobotCraneDevice>(entity.RobotCraneJson);
+ }
+
+ if (!string.IsNullOrEmpty(entity.CurrentTaskJson))
+ {
+ state.CurrentTask = JsonConvert.DeserializeObject<Dt_RobotTask>(entity.CurrentTaskJson);
+ }
+
+ if (!string.IsNullOrEmpty(entity.LastPickPositionsJson))
+ {
+ state.LastPickPositions = JsonConvert.DeserializeObject<int[]>(entity.LastPickPositionsJson);
+ }
+
+ if (!string.IsNullOrEmpty(entity.LastPutPositionsJson))
+ {
+ state.LastPutPositions = JsonConvert.DeserializeObject<int[]>(entity.LastPutPositionsJson);
+ }
+
+ if (!string.IsNullOrEmpty(entity.CellBarcodeJson))
+ {
+ state.CellBarcode = JsonConvert.DeserializeObject<List<string>>(entity.CellBarcodeJson) ?? new List<string>();
+ }
+
+ return state;
+ }
+
+ public Dt_RobotState ToEntity(RobotSocketState state)
+ {
+ var entity = new Dt_RobotState
+ {
+ IPAddress = state.IPAddress,
+ IsEventSubscribed = state.IsEventSubscribed,
+ RobotRunMode = state.RobotRunMode,
+ RobotControlMode = state.RobotControlMode,
+ RobotArmObject = state.RobotArmObject,
+ Homed = state.Homed,
+ CurrentAction = state.CurrentAction,
+ OperStatus = state.OperStatus,
+ IsSplitPallet = state.IsSplitPallet,
+ IsGroupPallet = state.IsGroupPallet,
+ RobotTaskTotalNum = state.RobotTaskTotalNum,
+ IsInFakeBatteryMode = state.IsInFakeBatteryMode,
+ CurrentBatchIndex = state.CurrentBatchIndex,
+ ChangePalletPhase = state.ChangePalletPhase,
+ IsScanNG = state.IsScanNG,
+ BatteryArrived = state.BatteryArrived
+ };
+
+ // 搴忓垪鍖栧鏉傚璞′负 JSON
+ if (state.RobotCrane != null)
+ {
+ entity.RobotCraneJson = JsonConvert.SerializeObject(state.RobotCrane);
+ }
+
+ if (state.CurrentTask != null)
+ {
+ entity.CurrentTaskJson = JsonConvert.SerializeObject(state.CurrentTask);
+ }
+
+ if (state.LastPickPositions != null)
+ {
+ entity.LastPickPositionsJson = JsonConvert.SerializeObject(state.LastPickPositions);
+ }
+
+ if (state.LastPutPositions != null)
+ {
+ entity.LastPutPositionsJson = JsonConvert.SerializeObject(state.LastPutPositions);
+ }
+
+ if (state.CellBarcode != null && state.CellBarcode.Count > 0)
+ {
+ entity.CellBarcodeJson = JsonConvert.SerializeObject(state.CellBarcode);
+ }
+
+ return entity;
+ }
+
+ public SqlSugarClient GetDbClient() => _db;
+
+ public void BeginTran() => _unitOfWork.BeginTran();
+
+ public void CommitTran() => _unitOfWork.CommitTran();
+
+ public void RollbackTran() => _unitOfWork.RollbackTran();
+ }
+}
--
Gitblit v1.9.3