ExcuteService.cs 7.86 KB
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.Implement;
using HHECS.Communication.Interfaces;
using HHECS.Communication.PLCComponent;
using HHECS.Communication.PLCComponent.HslComponent;
using HHECS.Dal;
using HHECS.Dal.Repository;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HHECS.Application.Service
{
    /// <summary>
    /// 调度程序服务类
    /// </summary>
    public class ExecuteService : BaseService
    {
        /// <summary>
        /// 初始化PLC
        /// </summary>
        /// <returns></returns>
        public BllResult<PLCCore> InitPLC(List<string> ips)
        {
            try
            {
                PLCRepository plcRepository = new PLCRepository();
                var plcs = plcRepository.Where(t => ips.Contains(t.IP) && t.Disable == false).ToList();
                if (plcs.Count == 0)
                {
                    return BllResultFactory.Error<PLCCore>($"未配置PLC、{string.Join(",", ips.ToArray())}", ErrorCodeConst.PLC_E0003.ToString());
                }
                var p = new PLCCore();
                foreach (var plc in plcs)
                {
                    if (plc.Brand == PLCType.Siemens.ToString())
                    {
                        if (Enum.TryParse(plc.Type, out SiemensPLCTypeS result))
                        {
                            p.PLCs.Add(new HslSiemensImplement(new SiemensPLCBuildModel()
                            {
                                SiemensPLCS = (SiemensPLCTypeS)Enum.Parse(typeof(SiemensPLCTypeS), plc.Type),
                                IP = plc.IP,
                                Port = 102,
                                Slot = plc.Type switch
                                {
                                    "S400" => 0,
                                    "S1200" => 0,
                                    "S300" => 0,
                                    "S1500" => 0,
                                    _ => 0
                                },
                                Rack = plc.Type switch
                                {
                                    "S400" => 3,
                                    "S1200" => 0,
                                    "S300" => 2,
                                    "S1500" => 0,
                                    _ => 0
                                },
                                Name = plc.Name
                            }));
                        }
                        else
                        {
                            return BllResultFactory.Error<PLCCore>($"此plc型号{plc.Type}未实现", ErrorCodeConst.PLC_E0003.ToString());
                        }
                    }
                    else
                    {
                        return BllResultFactory.Error<PLCCore>($"此plc类型{plc.Brand}未实现", ErrorCodeConst.PLC_E0003.ToString());
                    }
                }

                p.Connect();

                return BllResultFactory.Success(p);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<PLCCore>($"出现异常:{ex.Message}", ErrorCodeConst.DEZ0001.ToString());
            }
        }

        /// <summary>
        /// 初始化设备
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public BllResult<List<Equipment>> InitEquipment()
        {
            try
            {
                var equipments = new EquipmentRepository().Where(t => t.Disable == false).ToList();
                var equipmentProps = new EquipmentPropRepository().Where(t => true).ToList();
                var equipmentTypes = new EquipmentTypeRepository().Where(t => true).ToList();
                var equipmentTypePropTemplates = new EquipmentTypePropTemplateRepository().Where(t => true).ToList();

                //组合逻辑外键
                equipments.ForEach(t =>
                {
                    t.EquipmentType = equipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
                    t.EquipmentProps = equipmentProps.Where(i => i.EquipmentId == t.Id).ToList();
                });
                equipmentProps.ForEach(t =>
                {
                    t.Equipment = equipments.FirstOrDefault(i => i.Id == t.EquipmentId);
                    t.EquipmentTypePropTemplate = equipmentTypePropTemplates.FirstOrDefault(i => i.Id == t.EquipmentTypePropTemplateId);
                });
                //判断逻辑外键是否组合完毕
                if (equipments.Count(t => t.EquipmentType == null || t.EquipmentProps.Count == 0) > 0)
                {
                    return BllResultFactory.Error<List<Equipment>>("初始化设备信息失败,请检查基础数据", ErrorCodeConst.EQ_E0002.ToString());
                }
                return BllResultFactory.Success(equipments);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<Equipment>>("初始化设备信息出错:" + ex.Message, ErrorCodeConst.EQ_E0002.ToString());
            }
        }

        /// <summary>
        /// 初始化设备
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public BllResult<List<Equipment>> InitEquipment(string destinationArea)
        {
            try
            {
                var equipments = new EquipmentRepository().Where(t => t.DestinationArea == destinationArea && t.Disable).ToList();
                var equipmentProps = new EquipmentPropRepository().Where(t => true).ToList();
                var equipmentTypes = new EquipmentTypeRepository().Where(t => true).ToList();
                var equipmentTypePropTemplates = new EquipmentTypePropTemplateRepository().Where(t => true).ToList();

                //组合逻辑外键
                equipments.ForEach(t =>
                {
                    t.EquipmentType = equipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
                    t.EquipmentProps = equipmentProps.Where(i => i.EquipmentId == t.Id).ToList();
                });
                equipmentProps.ForEach(t =>
                {
                    t.Equipment = equipments.FirstOrDefault(i => i.Id == t.EquipmentId);
                    t.EquipmentTypePropTemplate = equipmentTypePropTemplates.FirstOrDefault(i => i.Id == t.EquipmentTypePropTemplateId);
                });
                //判断逻辑外键是否组合完毕
                if (equipments.Count(t => t.EquipmentType == null || t.EquipmentProps.Count == 0) > 0)
                {
                    return BllResultFactory.Error<List<Equipment>>("初始化设备信息失败,请检查基础数据", ErrorCodeConst.EQ_E0002.ToString());
                }
                return BllResultFactory.Success(equipments);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<Equipment>>("初始化设备信息出错:" + ex.Message, ErrorCodeConst.EQ_E0002.ToString());
            }
        }

        public void EquipmentPropUpdate(List<EquipmentProp> props)
        {
            using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
            {
                try
                {
                    DALHelper.GetFreeSql().Update<EquipmentProp>().SetSource(props).WithTransaction(uw.GetOrBeginTransaction()).ExecuteAffrows();
                    uw.Commit();
                }
                catch (Exception ex)
                {
                    uw.Rollback();
                    NoticeBus.Notice($"更新设备属性出现异常、{ex.Message}", Infrastructure.Enums.Level.Exception);
                }
            }
        }

    }
}