SendDataToOPCUA.cs
7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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);
}
}
}
}