EquipmentStatusHelper.cs 16.3 KB
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
using HHECS.BllModel;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Models;

namespace HHECS.DAQShared.Common.Utils
{
    /// <summary>
    /// 设备状态解析帮助类
    /// </summary>
    public class EquipmentStatusHelper
    {
        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <param name="equipmentType"></param>
        /// <param name="equipmentProps"></param>
        /// <param name="tags"></param>
        /// <remarks>无手动状态,手动模式记录空闲</remarks>
        /// <returns></returns>
        public static BllResult<EquipmentStatus> GetEquipmentStatus(EquipmentTypeConst equipmentType, IEnumerable<EquipmentPropExtend> equipmentProps, IEnumerable<TagItemDto> tags)
        {
            var result = GetEquipmentStatusV2(equipmentType, equipmentProps, tags);
            //手动状态转空闲
            if (result.Data == EquipmentStatus.Manual)
            {
                return BllResultFactory.Success(EquipmentStatus.Free);
            }

            return result;
        }

        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <param name="equipmentType"></param>
        /// <param name="equipmentProps"></param>
        /// <param name="tags"></param>
        /// <remarks>包含手动状态</remarks>
        /// <returns></returns>
        public static BllResult<EquipmentStatus> GetEquipmentStatusV2(EquipmentTypeConst equipmentType, IEnumerable<EquipmentPropExtend> equipmentProps, IEnumerable<TagItemDto> tags)
        {
            try
            {
                var valuePairs = new Dictionary<string, string>();
                foreach (var item in equipmentProps.DistinctBy(x => x.EquipmentTypePropTemplateCode))
                {
                    var tagCodes = new List<string>
                    {
                        item.EquipmentTypePropTemplateCode,
                        item.Address
                    };
                    var temp = tags.Where(x => tagCodes.Contains(x.Tag, StringComparer.OrdinalIgnoreCase)).FirstOrDefault();
                    if (temp == null)
                    {
                        continue;
                    }
                    valuePairs.Add(item.EquipmentTypePropTemplateCode, temp.Value);
                }

                var status = equipmentType switch
                {
                    EquipmentTypeConst.SingleForkSRM or
                    EquipmentTypeConst.SingleForkSRMV2 or
                    EquipmentTypeConst.DoubleForkSRM or
                    EquipmentTypeConst.DoubleForkSRMV2 or
                    EquipmentTypeConst.SingleForkSSRM or
                    EquipmentTypeConst.DoubleForkSRMV2 or
                    EquipmentTypeConst.SingleForkSSRMV132 => GetSRMStatus(valuePairs),
                    EquipmentTypeConst.WeldRobot => GetWeldRobotStatus(valuePairs),
                    EquipmentTypeConst.WeldRobotV2 => GetWeldRobotV2Status(valuePairs),
                    EquipmentTypeConst.AGVForklift => GetAGVForkliftStatus(valuePairs),
                    EquipmentTypeConst.RGVStation => GetRGVStationStatus(valuePairs),
                    EquipmentTypeConst.StationMonitor => GetStationMonitorStatus(valuePairs),
                    EquipmentTypeConst.Hoist => GetHoistStatus(valuePairs),
                    EquipmentTypeConst.CNC => GetCNCStatus(valuePairs),
                    EquipmentTypeConst.SingleForkRGV or
                    EquipmentTypeConst.SingleForkRGV_LDHW_V1 or
                    EquipmentTypeConst.SingleForkRGV_LDHW_V2 or
                    EquipmentTypeConst.SingleForkRGV_LDHW_V3
                    => GetSingleForkRGVStatus(valuePairs),
                    _ => EquipmentStatus.None,
                };
                return BllResultFactory.Success(status);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<EquipmentStatus>(ex.Message);
            }
        }

        /// <summary>
        /// 验证是否存在空值
        /// </summary>
        /// <param name="vals"></param>
        /// <returns></returns>
        private static bool ValidationIsNullOrWhiteSpace(params string[] vals) => !vals.Where(string.IsNullOrWhiteSpace).Any();

        /// <summary>
        /// 获取堆垛机状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <returns></returns>
        private static EquipmentStatus GetSRMStatus(Dictionary<string, string> valuePairs)
        {
            var totalError = valuePairs[SRMProps.TotalError.ToString()];
            var operationModel = valuePairs[SRMProps.OperationModel.ToString()];
            var fork1TaskExcuteStatus = valuePairs[SRMProps.Fork1TaskExcuteStatus.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(totalError, operationModel, fork1TaskExcuteStatus);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            //手动状态
            var manualEnumValues = new List<SRMOperationModel>
            {
                SRMOperationModel.Maintain,
                SRMOperationModel.Manual,
                SRMOperationModel.Airborne,
                SRMOperationModel.StandAlone,
            }.Select(x => x.ToString()).ToList();

            //手动
            if (manualEnumValues.Contains(operationModel))
            {
                return EquipmentStatus.Manual;
            }

            //故障
            if (totalError == bool.TrueString)
            {
                return EquipmentStatus.Failure;
            }

            //运行
            if (operationModel == SRMOperationModel.Online.GetIndexString() && fork1TaskExcuteStatus == SRMTaskExcuteStatus.TaskExecuting.GetIndexString())
            {
                return EquipmentStatus.Running;
            }

            //空闲
            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取焊接机器人设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <remarks>长沙版本</remarks>
        /// <returns></returns>
        private static EquipmentStatus GetWeldRobotStatus(Dictionary<string, string> valuePairs)
        {
            var bootFlag = valuePairs[RobotProp.BootFlag.ToString()];
            var totalError = valuePairs[RobotProp.Alarm.ToString()];
            var workMode = valuePairs[RobotProp.Work_Mode.ToString()];
            var workTime = valuePairs[RobotProp.Work_Time.ToString()];
            var workFlag = valuePairs[RobotProp.WorkFlag.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(bootFlag, totalError, workMode, workTime, workFlag);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            if (bootFlag != bool.TrueString)
            {
                return EquipmentStatus.Offline;
            }

            //故障,T1、T2模式不记录报警
            if (totalError == bool.TrueString && workTime != "1" && workTime != "2")
            {
                return EquipmentStatus.Failure;
            }

            //运行
            if (workMode == "3" && (workFlag == bool.TrueString || workTime == bool.TrueString))
            {
                return EquipmentStatus.Running;
            }

            //待机
            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取焊接机器人设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <remarks>徐州、昆山版本</remarks>
        /// <returns></returns>
        private static EquipmentStatus GetWeldRobotV2Status(Dictionary<string, string> valuePairs)
        {
            var nstatus = valuePairs[RobotV2Prop.Nstatus.ToString()];
            var totalError = valuePairs[RobotV2Prop.Alarm.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(nstatus, totalError);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            if (nstatus == "4")
            {
                return EquipmentStatus.Offline;
            }

            //故障,T1、T2模式不记录报警
            if (totalError == "1" || nstatus == "3")
            {
                return EquipmentStatus.Failure;
            }

            //运行
            if (nstatus == "1")
            {
                return EquipmentStatus.Running;
            }

            //待机
            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取AGV叉车设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <remarks>RCS/ACS系统设备</remarks>
        /// <returns></returns>
        private static EquipmentStatus GetAGVForkliftStatus(Dictionary<string, string> valuePairs)
        {
            var travelStatus = valuePairs[AGVForkliftProp.Status.ToString()];
            var isOnline = valuePairs[AGVForkliftProp.IsOnline.ToString()];
            var error = valuePairs[AGVForkliftProp.Error.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(travelStatus, isOnline);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            if (isOnline == "0")
            {
                return EquipmentStatus.Offline;
            }

            if (travelStatus == "20")
            {
                //手动状态
                return EquipmentStatus.Manual;
            }

            //故障,部分设备没有故障状态
            if (travelStatus == "30")
            {
                return EquipmentStatus.Failure;
            }

            //非手动模式,存在报警信息,则认为是报警
            if (!string.IsNullOrWhiteSpace(error))
            {
                return EquipmentStatus.Failure;
            }

            //运行
            if (travelStatus == "13")
            {
                return EquipmentStatus.Running;
            }
            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取RGV站台设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <remarks>ECS系统</remarks>
        /// <returns></returns>
        private static EquipmentStatus GetRGVStationStatus(Dictionary<string, string> valuePairs)
        {
            var totalError = valuePairs[RGVStationProps.Fault.ToString()];
            var autoMode = valuePairs[RGVStationProps.AutoMode.ToString()];
            var running = valuePairs[RGVStationProps.Running.ToString()];
            var stop = valuePairs[RGVStationProps.Stop.ToString()];
            var manual = valuePairs[RGVStationProps.Manual.ToString()];
            var airborne = valuePairs[RGVStationProps.Airborne.ToString()];
            var maintain = valuePairs[RGVStationProps.Maintain.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(totalError, autoMode, running, stop, manual, airborne, maintain);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            //手动、机载模式、维修状态下,认为是手动
            if (manual == bool.TrueString || airborne == bool.TrueString || maintain == bool.TrueString)
            {
                return EquipmentStatus.Manual;
            }

            //故障
            if (totalError == bool.TrueString)
            {
                return EquipmentStatus.Failure;
            }

            //运行
            if (autoMode == bool.TrueString && running == bool.TrueString)
            {
                return EquipmentStatus.Running;
            }

            //待机
            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取RGV设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <remarks>ECS系统设备</remarks>
        /// <returns></returns>
        private static EquipmentStatus GetSingleForkRGVStatus(Dictionary<string, string> valuePairs)
        {
            var totalError = valuePairs[SingleForkRGVProp.TotalError.ToString()];
            var operationModel = valuePairs[SingleForkRGVProp.OperationModel.ToString()];
            var fork1TaskNo = valuePairs[SingleForkRGVProp.Fork1TaskNo.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(totalError, operationModel, fork1TaskNo);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            _ = int.TryParse(operationModel, out var operationModelValue);

            //非联机状态,认为是手动
            if (operationModelValue > 0 && operationModelValue < 5)
            {
                return EquipmentStatus.Manual;
            }

            if (totalError == bool.TrueString)
            {
                return EquipmentStatus.Failure;
            }

            //联机状态,且有任务号,则认为是运行状态
            if (operationModel == "5" && fork1TaskNo != "0")
            {
                return EquipmentStatus.Running;
            }

            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取站台监控设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <returns></returns>
        private static EquipmentStatus GetStationMonitorStatus(Dictionary<string, string> valuePairs)
        {
            var totalError = valuePairs[StationProps.StationError.ToString()];
            var stationCorotation = false;
            var stationReverse = false;
            if (valuePairs.ContainsKey(StationProps.StationCorotation.ToString()))
            {
                _ = bool.TryParse(valuePairs[StationProps.StationCorotation.ToString()], out stationCorotation);
            }

            if (valuePairs.ContainsKey(StationProps.StationReverse.ToString()))
            {
                _ = bool.TryParse(valuePairs[StationProps.StationReverse.ToString()], out stationReverse);
            }

            var validationResult = ValidationIsNullOrWhiteSpace(totalError);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            //故障
            if (totalError == bool.TrueString)
            {
                return EquipmentStatus.Failure;
            }

            //运行
            if (stationCorotation || stationReverse)
            {
                return EquipmentStatus.Running;
            }

            //待机
            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取提升机设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <returns></returns>
        private static EquipmentStatus GetHoistStatus(Dictionary<string, string> valuePairs)
        {
            var hoistStatus = valuePairs[HoistProps.HoistStatus.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(hoistStatus);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            //故障
            if (hoistStatus == "1")
            {
                return EquipmentStatus.Failure;
            }
            if (hoistStatus == "6")
            {
                return EquipmentStatus.Running;
            }

            return EquipmentStatus.Free;
        }

        /// <summary>
        /// 获取切割机设备状态
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <returns></returns>
        private static EquipmentStatus GetCNCStatus(Dictionary<string, string> valuePairs)
        {
            var redLight = valuePairs[CNCProp.RedLight.ToString()];
            var yellowLight = valuePairs[CNCProp.YellowLight.ToString()];
            var greenLight = valuePairs[CNCProp.GreenLight.ToString()];

            var validationResult = ValidationIsNullOrWhiteSpace(redLight, yellowLight, greenLight);
            if (!validationResult)
            {
                return EquipmentStatus.None;
            }

            if (greenLight == bool.TrueString)
            {
                return EquipmentStatus.Running;
            }

            //绿灯不亮,红灯亮,属于故障
            if (greenLight == bool.FalseString && redLight == bool.TrueString)
            {
                return EquipmentStatus.Free;
            }

            return EquipmentStatus.Free;
        }
    }
}