WeldRobotAnalysis.cs 4.61 KB
using HHECS.BllModel;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.Models;
using HHECS.EquipmentModel;

namespace HHECS.DAQHandle.EquipmentHandle
{
    /// <summary>
    /// 焊接机器人数据解析
    /// </summary>
    internal class WeldRobotAnalysis : BaseAnalysis
    {
        public WeldRobotAnalysis(EquipmentType equipmentType) : base(equipmentType) { }

        public override BllResult Execute(IEnumerable<EquipmentDataRecord> records)
        {
            try
            {
                //按设备分组
                foreach (var item in records.GroupBy(x => x.EquipmentCode))
                {
                    var equipmentRecords = item.ToList();
                    var setStatusResult = SetEquipmentStatus(item.Key, equipmentRecords);
                    if (!setStatusResult.Success)
                    {
                        SystemLog.PrintError(setStatusResult.Msg);
                    }
                    var setAlarmResult = SetEquipmentAlarm(item.Key, equipmentRecords);
                    if (!setAlarmResult.Success)
                    {
                        SystemLog.PrintError(setStatusResult.Msg);
                    }
                    var setValueResult = SetEquipmentPropValues(item.Key, item.LastOrDefault());
                    if (!setValueResult.Success)
                    {
                        SystemLog.PrintError(setStatusResult.Msg);
                    }
                }
                //解析完成后清空记录
                Context.EquipmentDataRecord.RemoveRange(records);
                Context.SaveChanges();
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"[{nameof(WeldRobotAnalysis)}]解析出现异常:{ex}");
            }
        }

        /// <summary>
        /// 记录设备状态
        /// </summary>
        /// <param name="equipmentSN"></param>
        /// <param name="equipmentDataRecords"></param>
        /// <returns></returns>
        protected BllResult SetEquipmentStatus(string equipmentSN, IEnumerable<EquipmentDataRecord> equipmentDataRecords)
        {
            try
            {
                //待完善
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"更新设备[{equipmentSN}]状态失败.{ex.Message}");
            }
        }

        protected BllResult SetEquipmentAlarm(string equipmentSN, IEnumerable<EquipmentDataRecord> equipmentDataRecords)
        {
            try
            {
                //待完善
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"更新设备[{equipmentSN}]报警失败.{ex.Message}");
            }
        }

        protected BllResult SetEquipmentPropValues(string equipmentSN, EquipmentDataRecord record)
        {
            try
            {
                var equipmentId = Context.Equipment.Where(x => x.Code == equipmentSN).First(x => x.Id);
                var equipmentProps = Context.EquipmentProp.Where(x => x.EquipmentId == equipmentId).ToList();
                if (equipmentProps.Count == 0)
                {
                    return BllResultFactory.Error($"设备编号[{record.EquipmentCode}]的属性配置为空!");
                }

                var jsonResult = JsonConvertToTagList(record.Tags);
                if (!jsonResult.Success)
                {
                    return BllResultFactory.Error($"设备编号[{record.EquipmentCode}]的Json数据转换失败.{jsonResult.Msg}");
                }

                var tags = jsonResult.Data;
                var temps = new List<EquipmentProp>();
                foreach (var prop in equipmentProps)
                {
                    var tag = tags.Where(x => x.Tag == prop.Address).FirstOrDefault();
                    if (tag == null) continue;
                    Context.EquipmentProp.Attach(prop);
                    prop.Value = tag.Value;
                    prop.Updated = ConvertToDateTime(record.Timestamp).Data;
                    temps.Add(prop);
                }
                if (temps.Count != 0)
                {
                    Context.UpdateRange(temps);
                    Context.SaveChanges();
                }
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error($"设备[{equipmentSN}]属性值更新失败.{ex.Message}");
            }
        }
    }
}