AIOAIManager.cs
4.9 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
148
149
150
151
152
153
using Asa.IOModule;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class AIOAIManager:AIManager
{
private object AILock = "";
private List<int> AIValList = null;
private int AILength = 4;
private Asa.IOModule.AIOBOX AIBox = null;
private System.Timers.Timer conTimer = null;
private List<string> needConIp = new List<string>();
public override void StartConnect(params string[] ipList)
{
if (conTimer == null)
{
conTimer = new System.Timers.Timer();
conTimer.AutoReset = true;
conTimer.Interval = 60000;
conTimer.Elapsed += ConTimer_Elapsed;
}
conTimer.Enabled = false;
needConIp = new List<string>(ipList);
foreach (string ip in ipList)
{
bool result = ConnectionIP(ip);
}
if (needConIp.Count > 0)
{
//启动定时器,1一分钟重连一次
conTimer.Start();
}
}
private bool ConnectionIP(string ioIp)
{
int autoMS = 150;
try
{
AIValList = new List<int>();
AIBox = new Asa.IOModule.AIOBOX();
AIBox.IP = ioIp;
// bool rtn = AIBox.AutoIP(ioIp);
AIBox.SetInput(Asa.IOModule.Box_Type.AI, AILength);
AIBox.SetOutput(Asa.IOModule.Box_Type.DO, 0);
AIBox.AutoReadInput(true, autoMS);
AIBox.AI_Changed_Event += Box_AI_Changed_Event;
LogUtil.debug("开始连接AI模块[" + ioIp + "][" + autoMS + "],尝试重连三次");
for (int i = 1; i <= 3; i++)
{
bool result = AIBox.Connect();
if (result)
{
if (needConIp.Contains(ioIp))
{
needConIp.Remove(ioIp);
}
LogUtil.info("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]成功:" + AIBox.ErrInfo);
return true;
}
else
{
LogUtil.error("第【" + i + "】次连接IO模块[" + ioIp + "][" + autoMS + "]失败:" + AIBox.ErrInfo + "");
}
Thread.Sleep(10);
}
}
catch (Exception error)
{
LogUtil.error("连接IO模块[" + ioIp + "][" + autoMS + "]出错:" + error.ToString());
}
return false;
}
private void ConTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
List<string> list = new List<string>(needConIp);
if (list.Count > 0)
{
foreach (string ip in list)
{
LogUtil.info("重连AOI AI 模块 :" + ip);
ConnectionIP(ip);
}
}
}
catch (Exception ex)
{
LogUtil.error("AOI AI ConTimer_Elapsed 出错: " + ex.ToString());
}
}
public override void CloseConnect()
{
try
{
if (AIBox != null)
{
AIBox.Close();
}
}
catch (Exception ex)
{
LogUtil.error("断开AI连接出错:" + ex.ToString());
}
}
private void Box_AI_Changed_Event(AIOBOX box, int[] val)
{
try
{
if (val != null && val.Length >= AILength)
{
lock (AILock)
{
AIValList = new List<int>();
AIValList.AddRange(val);
}
}
}
catch (Exception ex)
{
LogUtil.error("Box_AI_Changed_Event出错:" + ex.ToString());
}
}
public override double GetAIValue(string ioiP,int index)
{
if (AIValList != null && AIValList.Count > index)
{
return AIValList[index];
}
return -1;
}
//public override double ConvertAI(double aiValue, double defaultValue)
//{
// double xishu = (double)ConfigAppSettings.GetNumValue(Setting_Init.AI_ConvertPosition);
// double result = Math.Round((aiValue - defaultValue) / xishu, 2);
// return result;
//}
}
}