SendDataToOPCUA.cs 7.36 KB
using HHECS.BLL.Services;
using HHECS.BllModel;
using HHECS.Dal;
using HHECS.DAL.Repository;
using HHECS.Executor;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Notice;
using HHECS.Model.ApiEntities;
using HHECS.Model.Entities;
using Opc.Ua;
using OpcUaHelper;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HHECS.BLL.EquipmentExcute.Marking
{
    public static class SendDataToOPCUA
    {
        private static OpcUaClient m_OpcUaClient;
        public  static void ConnectOPCUA()
        {
            m_OpcUaClient = new OpcUaClient();
            try
            {
                BllService bllService = new BllService();
                var dict = bllService.GetDictWithDetails(a => a.Code == "OPCUA").Data;
                if (dict==null)
                {
                    NoticeBus.Notice($"字典配置出错:opcua数据采集没有找到字典中所配置的地址", Level.Exception);
                    return;
                }
                foreach (var item in dict.DictDetails)
                {
                    //分割 连接地址、账号、密码
                    var temp = item.Value.Split(new char[] { ',' });
                    m_OpcUaClient.UserIdentity = new UserIdentity(temp[1], temp[2]);
                    var result = OpcUaExtension.ConnectServer(temp[0], m_OpcUaClient);
                    if (!result)
                    {
                        NoticeBus.Notice($"连接OPCUA服务失败,请检查{temp[0]}服务是否开启", Level.Exception);
                        continue;
                    }
                    ReadOPCUA(temp[0]);
                }
            }
            catch (Exception ex)
            {
                NoticeBus.Notice($"连接OPCUA服务出现异常,异常原因{ex.Message}", Level.Exception);
            }
        }

        public static void ReadOPCUA(string ip)
        {
            try
            {
                List<string> tags = new List<string>();
                BusCollectionCommandRepository busCollectionCommandRepository = new BusCollectionCommandRepository();
                BusCollectionResultRepository busCollectionResultRepository = new BusCollectionResultRepository();
                var commands = busCollectionCommandRepository.Where(t => t.CommunicationType == "OPCUA" && t.Ip == ip).ToList();
                foreach (var command in commands)
                {
                    tags.Add(command.CommandCode);
                }
                if (tags.Count > 0)
                {
                    List<BusCollectionResult> busCollectionResults = new List<BusCollectionResult>();
                    var dataValue = m_OpcUaClient.ReadNodes<string>(tags);
                    if (dataValue.Count != commands.Count)
                    {
                        NoticeBus.Notice($"OPCUA服务读取失败,{ip}服务读取数量和实际数量不相同", Level.Exception);
                    }
                    for (int i = 0; i < dataValue.Count; i++)
                    {
                        commands[i].Value = dataValue[i];
                        commands[i].UpdateTime = DateTime.Now;
                        BusCollectionResult busCollectionResult = new BusCollectionResult();
                        busCollectionResult.Keys = Guid.NewGuid();
                        busCollectionResult.Value = dataValue[i];
                        busCollectionResult.CommandCode = commands[i].CommandCode;
                        busCollectionResult.Status = 10;
                        busCollectionResult.ExternalEquipmentCode= commands[i].ExternalEquipmentCode+ commands[i].ExternalEquipmentName;
                        busCollectionResult.Remark = commands[i].CommandName;
                        busCollectionResult.CreateTime = DateTime.Now;
                        busCollectionResults.Add(busCollectionResult);
                    }
                    EquipmentOperation equipmentOperation=new EquipmentOperation();
                    equipmentOperation.code = commands[0].ExternalEquipmentCode;
                    equipmentOperation.status = commands.Where(t=>t.CommandName== "设备运行状态").FirstOrDefault().Value ;
                    equipmentOperation.mode = commands.Where(t => t.CommandName == "工作模式").FirstOrDefault().Value;
                    equipmentOperation.manipulator = "";
                    equipmentOperation.startTotalTime = "";
                    equipmentOperation.workTotalTime = commands.Where(t => t.CommandName == "工作总时间").FirstOrDefault().Value;
                    equipmentOperation.waitTotalTime = "";
                    equipmentOperation.startTime = null;
                    equipmentOperation.shutdownTime = null;
                    equipmentOperation.consume = "";

                    ProcessParameters processParameters = new ProcessParameters();
                    processParameters.programNumber = commands.Where(t => t.CommandName == "当前加工程序名").FirstOrDefault().Value;
                    processParameters.component = "";
                    processParameters.technological = "";
                    processParameters.workingSpeed = commands.Where(t => t.CommandName == "校平/切割/加工速度").FirstOrDefault().Value;
                    equipmentOperation.processParameters = processParameters;

                    FaultMaintenance faultMaintenance = new FaultMaintenance();
                    faultMaintenance.maintainCode = "";
                    faultMaintenance.warning = commands.Where(t => t.CommandName == "报警事件").FirstOrDefault().Value;
                    faultMaintenance.maintainMessage = "";
                    faultMaintenance.faultTotalTime = "";
                    equipmentOperation.faultMaintenance = faultMaintenance;

                    //CommonService commonService = new CommonService();
                    //var result = commonService.HttpMan1("http://127.0.0.1:12345/GetA", equipmentOperation, "数据采集上传");
                    //if (!result.Status)
                    //{
                    //    NoticeBus.Notice($"数据采集上传mom失败:{result.Message}", Level.Exception);
                    //}
                    //NoticeBus.Notice($"数据采集上传mom成功:{result.Message}", Level.Success);
                    using (var uw = DALHelper.GetFreeSql().CreateUnitOfWork())
                    {
                        try
                        {
                            busCollectionCommandRepository.UnitOfWork = uw;
                            busCollectionResultRepository.UnitOfWork = uw;

                            busCollectionCommandRepository.Update(commands);
                            //busCollectionResultRepository.Insert(busCollectionResults);
                            uw.Commit();
                            NoticeBus.Notice($"数据采集上传mom成功,IP:{commands[0].ExternalEquipmentCode}", Level.Success);
                        }
                        catch (Exception ex)
                        {
                            uw?.Rollback();
                            NoticeBus.Notice($"OPCUA服务更新或写入失败,异常原因{ex.Message}", Level.Exception);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                NoticeBus.Notice($"OPCUA服务读取写入失败,异常原因{ex.Message}", Level.Exception);
            }
        }

    }
}