ExcuteService.cs 6.23 KB
using HHECS.Application.Error;
using HHECS.BllModel;
using HHECS.Communication;
using HHECS.Communication.Implement;
using HHECS.Communication.PLC;
using HHECS.Communication.PLC.PLCComponent;
using HHECS.Communication.PLC.PLCComponent.HslComponent;
using HHECS.Dal.Repository;
using HHECS.DAL.Repository;
using HHECS.Model.Entities;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HHECS.Application.Service
{
    /// <summary>
    /// 调度程序服务类
    /// </summary>
    public class ExecuteService : BaseService
    {
        //public static List<Equipment> EquipmentList { get; set; } = new List<Equipment>();

        /// <summary>
        /// 初始化PLC
        /// </summary>
        /// <returns></returns>
        public BllResult<PLCCore> InitPLC(List<string> LineCodes)
        {
            try
            {
                PLCRepository plcRepository = new PLCRepository();
                var plcs = plcRepository.Where(t => t.Disable == false && LineCodes.Contains(t.LineCode)).ToList();
                var p = new PLCCore();
                foreach (var plc in plcs)
                {
                    if (plc.Brand == PLCBrand.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());
                    }
                }
                string msg = string.Empty;
                //切换长连接,对于非禁用PLC采取切换长连接(如果这里开启长连接失败,后面处理类用Read或是Write就会开启短连接,所以这里失败,后面照样可以处理。就一直返回成功)
                Task.Run(() =>
                {
                    //切换长连接,对于非禁用PLC采取切换长连接
                    p.PLCs.AsParallel().ForAll(t =>
                    {
                        if (!plcs.Find(a => a.IP == t.IP).Disable)
                        {
                            p.Connect(t.IP);
                        }
                    });
                });
                return BllResultFactory.Success(p, msg);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Exception<PLCCore>(ex, $"出现异常:{ex.Message}", ErrorCodeConst.PLC_E0005.ToString());
            }
        }

        /// <summary>
        /// 初始化设备
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public BllResult<List<Equipment>> InitEquipment(List<string> LineCodes)
        {
            try
            {
                var equipments = new EquipmentRepository().Where(t => t.Disable == false && LineCodes.Contains(t.LineCode)).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();
                var workStations = new BaseWorkStationRepository().Where(t => LineCodes.Contains(t.LineCode)).ToList();

                //组合逻辑外键
                equipments.ForEach(t =>
                {
                    t.EquipmentType = equipmentTypes.FirstOrDefault(i => i.Id == t.EquipmentTypeId);
                    t.EquipmentProps = equipmentProps.Where(i => i.EquipmentId == t.Id).ToList();
                    t.WorkStationList = workStations;
                    t.WorkStation = workStations.FirstOrDefault(i => i.Id == t.WorkStationId);
                });
                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());
                }
                //EquipmentList = equipments;
                return BllResultFactory.Success(equipments);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Exception<List<Equipment>>(ex, "初始化设备信息出错:" + ex.Message, ErrorCodeConst.EQ_E0002.ToString());
            }
        }
    }
}