wanshenmean
15 小时以前 f288ccc545f8cc32bc922c96dfb3cab9a1f92ec6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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;
        }
    }
}