using Newtonsoft.Json;
|
using SqlSugar;
|
using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_ITaskInfoRepository;
|
using WIDESEAWCS_Model.Models;
|
|
namespace WIDESEAWCS_TaskInfoRepository
|
{
|
/// <summary>
|
/// 机械手状态 SqlSugar 仓储实现
|
/// </summary>
|
public class RobotStateRepository : RepositoryBase<Dt_RobotState>, IRobotStateRepository
|
{
|
public RobotStateRepository(IUnitOfWorkManage unitOfWork) : base(unitOfWork)
|
{
|
}
|
|
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,
|
Version = DateTime.UtcNow.Ticks,
|
RobotCraneJson = JsonConvert.SerializeObject(robotCrane),
|
CreateDate = DateTime.Now,
|
ModifyDate = DateTime.Now
|
};
|
|
Db.Insertable(newState).ExecuteCommand();
|
return newState;
|
}
|
|
public bool TryUpdate(string ipAddress, Dt_RobotState newState, long expectedVersion)
|
{
|
newState.ModifyDate = DateTime.Now;
|
|
// 乐观锁:WHERE IPAddress = @ip AND Version = @expectedVersion,版本匹配才更新
|
var affectedRows = Db.Updateable<Dt_RobotState>(newState)
|
.Where(x => x.IPAddress == ipAddress && x.Version == expectedVersion)
|
.ExecuteCommand();
|
|
return affectedRows > 0;
|
}
|
|
public RobotSocketState ToSocketState(Dt_RobotState entity)
|
{
|
var state = new RobotSocketState
|
{
|
IPAddress = entity.IPAddress,
|
Version = entity.Version,
|
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;
|
}
|
}
|
}
|