CenterService.cs 4.01 KB
using HHECS.Communication;
using HHECS.Communication.Robot;
using HHECS.RobotTool.Common;
using HHECS.RobotTool.Model;

namespace HHECS.RobotTool.Service
{
    public class CenterService
    {
        public List<EquipmentExtend> Equipments = new List<EquipmentExtend>();

        private readonly IFreeSql _freeSql;
        private readonly SystemLog _logger = SystemLog.GetInstance();

        private List<IEquipmentCommunication> _equipmentCommunications = new List<IEquipmentCommunication>();

        public CenterService(IFreeSql freeSql)
        {
            _freeSql = freeSql;
        }

        internal void Start()
        {
            try
            {
                var equipments = _freeSql.Queryable<EquipmentExtend>().ToList();
                var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
                var equipmentTypes = _freeSql.Queryable<EquipmentTypeExtend>().Where(x => equipmentTypeIds.Contains(x.Id)).ToList();
                var equipmentIds = equipments.Select(x => x.Id).ToList();
                var equipmentProps = _freeSql.Queryable<EquipmentPropExtend>().Where(x => equipmentIds.Contains(x.EquipmentId)).ToList();
                var equipmentTypeTemps = _freeSql.Queryable<EquipmentTypePropTemplateExtend>().Where(x => equipmentTypeIds.Contains(x.EquipmentTypeId)).ToList();

                foreach (var item in equipments)
                {
                    item.EquipmentType = equipmentTypes.FirstOrDefault(x => x.Id == item.EquipmentTypeId)!;
                    var props = equipmentProps.Where(x => x.EquipmentId == item.Id).ToList();
                    props.ForEach(prop =>
                    {
                        prop.Equipment = item;
                        prop.EquipmentTypePropTemplate = equipmentTypeTemps.FirstOrDefault(x => x.Id == prop.EquipmentTypePropTemplateId)!;
                    });
                    item.EquipmentProps = props;
                }

                var communicationConfig = _freeSql.Queryable<CommunicationConfig>().Where(x => x.IsEnable).ToList();
                if (communicationConfig.Count == 0)
                {
                    _logger.LogWarning($"设备通讯配置列表为空,请配置数据后再试!");
                    return;
                }
                _equipmentCommunications = InitialCommunication(communicationConfig);
                if (_equipmentCommunications.Count == 0)
                {
                    _logger.LogWarning($"通讯配置数量:{communicationConfig.Count},初始化成功数量:{_equipmentCommunications.Count},请检查基础数据配置是否正确!");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"启动失败:{ex.Message}");
            }
        }

        internal void Stop()
        {
            _equipmentCommunications.ForEach(x => x.DisConnect());
            _equipmentCommunications.Clear();
            Equipments.Clear();
        }

        /// <summary>
        /// 初始化设备通讯
        /// </summary>
        /// <param name="communicationConfigs"></param>
        /// <returns></returns>
        private static List<IEquipmentCommunication> InitialCommunication(IEnumerable<CommunicationConfig> communicationConfigs)
        {
            var temps = new List<IEquipmentCommunication>();
            foreach (var item in communicationConfigs)
            {
                IEquipmentCommunication? communication = item.CommunicationType switch
                {
                    CommunicationTypeConst.Fanuc => new HslFanucImplement(new FanucBuildModel
                    {
                        Name = item.Name,
                        IP = item.IpAddress,
                        Port = item.Port,
                    }),
                    _ => null
                };
                if (communication == null)
                {
                    continue;
                }
                temps.Add(communication);
            }
            return temps;
        }
    }
}