|
@@ -13,6 +13,7 @@ using System.Drawing;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net;
|
|
|
|
+using System.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
@@ -23,13 +24,22 @@ namespace IMCS
|
|
{
|
|
{
|
|
public partial class Form_Main : Form
|
|
public partial class Form_Main : Form
|
|
{
|
|
{
|
|
- public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/Work Pieces/TEST1/";
|
|
|
|
|
|
+ //public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/NCExtend/wks.dir/nc_program/";
|
|
|
|
+ public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/Work Pieces/TEST/";
|
|
//public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/NCExtend/";
|
|
//public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/NCExtend/";
|
|
public string CHANNEL_NO = "1";
|
|
public string CHANNEL_NO = "1";
|
|
- public string TEMP_NC_NAME = "7999.MPF";
|
|
|
|
|
|
+ public string TEMP_NC_NAME = "TEMP1.MPF"; //临时程序
|
|
private uint currentToolArea = 1;
|
|
private uint currentToolArea = 1;
|
|
|
|
+
|
|
|
|
+ string Listener_URL = "http://20.20.47.107:8010/opcua/";
|
|
Server opcServer = null;
|
|
Server opcServer = null;
|
|
- public Dictionary<string, Server> deviceList { get; set; } = new Dictionary<string, Server>();
|
|
|
|
|
|
+ //public Dictionary<string, Server> deviceList { get; set; } = new Dictionary<string, Server>();
|
|
|
|
+
|
|
|
|
+ // 作为长连接进行bean的存储, key使用ip,
|
|
|
|
+ Dictionary<string, Server> opcServerMap = new Dictionary<string, Server>();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
public Form_Main()
|
|
public Form_Main()
|
|
{
|
|
{
|
|
@@ -41,9 +51,9 @@ namespace IMCS
|
|
HttpListener httpListenner;
|
|
HttpListener httpListenner;
|
|
httpListenner = new HttpListener();
|
|
httpListenner = new HttpListener();
|
|
httpListenner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
|
|
httpListenner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
|
|
- httpListenner.Prefixes.Add("http://127.0.0.1:8010/opcua/");
|
|
|
|
|
|
+ httpListenner.Prefixes.Add(Listener_URL);
|
|
httpListenner.Start();
|
|
httpListenner.Start();
|
|
- label1.Text = "HttpServer已开启:http://127.0.0.1:8010/opcua/";
|
|
|
|
|
|
+ label1.Text = "HttpServer已开启:" + Listener_URL;
|
|
new Thread(new ThreadStart(delegate
|
|
new Thread(new ThreadStart(delegate
|
|
{
|
|
{
|
|
try
|
|
try
|
|
@@ -81,6 +91,11 @@ namespace IMCS
|
|
{
|
|
{
|
|
if (!request.Url.ToString().Contains("favicon"))
|
|
if (!request.Url.ToString().Contains("favicon"))
|
|
{
|
|
{
|
|
|
|
+
|
|
|
|
+ RequestBody opcBody = null;
|
|
|
|
+ ResponseBody responseBody = new ResponseBody();
|
|
|
|
+ Server opcServer = null;
|
|
|
|
+
|
|
try
|
|
try
|
|
{
|
|
{
|
|
Stream stream = context.Request.InputStream;
|
|
Stream stream = context.Request.InputStream;
|
|
@@ -88,13 +103,15 @@ namespace IMCS
|
|
string body = reader.ReadToEnd();
|
|
string body = reader.ReadToEnd();
|
|
|
|
|
|
YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
|
|
YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
|
|
- ResponseBody responseBody = new ResponseBody();
|
|
|
|
|
|
+ responseBody = new ResponseBody();
|
|
|
|
|
|
- RequestBody opcBody = JsonConvert.DeserializeObject<RequestBody>(body);
|
|
|
|
|
|
+
|
|
|
|
+ opcBody = JsonConvert.DeserializeObject<RequestBody>(body);
|
|
|
|
|
|
AddList(DateTime.Now.ToString(), "POST", opcBody.ServerUrl + opcBody.Type, "OK");
|
|
AddList(DateTime.Now.ToString(), "POST", opcBody.ServerUrl + opcBody.Type, "OK");
|
|
|
|
|
|
- if (deviceList == null || (deviceList.Where(m => m.Key.Equals(opcBody.ServerUrl)).Count() == 0))
|
|
|
|
|
|
+ /*
|
|
|
|
+ * if (deviceList == null || (deviceList.Where(m => m.Key.Equals(opcBody.ServerUrl)).Count() == 0))
|
|
{
|
|
{
|
|
opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
|
|
opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
|
|
if (opcServer.Session != null)
|
|
if (opcServer.Session != null)
|
|
@@ -110,18 +127,42 @@ namespace IMCS
|
|
{
|
|
{
|
|
opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
|
|
opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
|
|
}
|
|
}
|
|
|
|
+ */
|
|
//else
|
|
//else
|
|
//{
|
|
//{
|
|
// opcServer.Session.Reconnect();
|
|
// opcServer.Session.Reconnect();
|
|
//}
|
|
//}
|
|
|
|
|
|
- if (opcServer.Session.Connected)
|
|
|
|
|
|
+ // 从map中取出opcServer 判断是否存在,以及是否可用
|
|
|
|
+ opcServerMap.TryGetValue(opcBody.ServerUrl, out opcServer);
|
|
|
|
+
|
|
|
|
+ // 如果key不存在,或者存在单连接不上,则需要重新创建连接
|
|
|
|
+ if (!opcServerMap.ContainsKey(opcBody.ServerUrl) || !opcServer.Session.Connected)
|
|
|
|
+ {
|
|
|
|
+ Ping pingSender = new Ping();
|
|
|
|
+ PingReply reply = pingSender.Send(opcBody.Ip);
|
|
|
|
+ if (reply.Status == IPStatus.Success)
|
|
|
|
+ {
|
|
|
|
+ opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (opcServer != null && opcServer.Session != null && opcServer.Session.Connected)
|
|
|
|
+ {
|
|
|
|
+ // 存储Server bean
|
|
|
|
+ opcServerMap.Add(opcBody.ServerUrl, opcServer);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (opcServer != null && opcServer.Session != null && opcServer.Session.Connected)
|
|
{
|
|
{
|
|
if (opcBody.Type == ActionTypeEnum.Connect.ToString()) //连接状态
|
|
if (opcBody.Type == ActionTypeEnum.Connect.ToString()) //连接状态
|
|
{
|
|
{
|
|
responseBody.result = true;
|
|
responseBody.result = true;
|
|
}
|
|
}
|
|
- else if (opcBody.Type == ActionTypeEnum.Collect.ToString()) //采集
|
|
|
|
|
|
+ else if (opcBody.Type == ActionTypeEnum.Collect.ToString()) //数据采集
|
|
{
|
|
{
|
|
//ETH_S7Simens s7 = new ETH_S7Simens("192.168.11.164");
|
|
//ETH_S7Simens s7 = new ETH_S7Simens("192.168.11.164");
|
|
|
|
|
|
@@ -203,27 +244,29 @@ namespace IMCS
|
|
{
|
|
{
|
|
opcServer.WriteValues(opcBody.Values, opcBody.Addresses);
|
|
opcServer.WriteValues(opcBody.Values, opcBody.Addresses);
|
|
}
|
|
}
|
|
- else if (opcBody.Type == ActionTypeEnum.SelectNc.ToString())
|
|
|
|
|
|
+ else if (opcBody.Type == ActionTypeEnum.SelectNc.ToString()) // 选中程序
|
|
{
|
|
{
|
|
string sSelectedFileName = Path.GetFileName(opcBody.Path);
|
|
string sSelectedFileName = Path.GetFileName(opcBody.Path);
|
|
string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + sSelectedFileName, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + sSelectedFileName, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
if (status == "Good")
|
|
if (status == "Good")
|
|
{
|
|
{
|
|
- responseBody.msg = "选择成功";
|
|
|
|
|
|
+ responseBody.msg = "选择程序成功";
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- responseBody.msg = "选择失败";
|
|
|
|
|
|
+ responseBody.msg = "选择程序失败";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- else if (opcBody.Type == ActionTypeEnum.Upload.ToString())
|
|
|
|
|
|
+ else if (opcBody.Type == ActionTypeEnum.Upload.ToString()) //上传文件
|
|
{
|
|
{
|
|
string file = opcBody.Path;
|
|
string file = opcBody.Path;
|
|
string serverPath = "";
|
|
string serverPath = "";
|
|
//选择临时文件
|
|
//选择临时文件
|
|
- string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
|
|
|
|
+
|
|
try
|
|
try
|
|
{
|
|
{
|
|
|
|
+ string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
|
|
+
|
|
string extension = Path.GetExtension(file);
|
|
string extension = Path.GetExtension(file);
|
|
if (string.IsNullOrWhiteSpace(extension))
|
|
if (string.IsNullOrWhiteSpace(extension))
|
|
{
|
|
{
|
|
@@ -252,6 +295,8 @@ namespace IMCS
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
{
|
|
{
|
|
responseBody.msg = "上传文件失败===>" + ex.Message;
|
|
responseBody.msg = "上传文件失败===>" + ex.Message;
|
|
|
|
+ responseBody.code = 0;
|
|
|
|
+ responseBody.result = false;
|
|
YG.Log.Instance.WriteLogAdd($"{ex.Message}");
|
|
YG.Log.Instance.WriteLogAdd($"{ex.Message}");
|
|
}
|
|
}
|
|
|
|
|
|
@@ -262,9 +307,37 @@ namespace IMCS
|
|
responseBody.msg = "上传文件失败===>" + ex.Message;
|
|
responseBody.msg = "上传文件失败===>" + ex.Message;
|
|
YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
|
|
YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
|
|
}
|
|
}
|
|
- //设位主程序
|
|
|
|
- status = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
|
|
|
|
+
|
|
|
|
+ //设为主程序
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ Server.MethodCallResult methodCallResult = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO));
|
|
|
|
+
|
|
|
|
+ if (methodCallResult.status.ToUpper().Equals("GOOD"))
|
|
|
|
+ {
|
|
|
|
+ responseBody.msg += "==设为主程序成功";
|
|
|
|
+ YG.Log.Instance.WriteLogAdd(serverPath + "设为主程序成功");
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ responseBody.code = 0;
|
|
|
|
+ responseBody.result = false;
|
|
|
|
+ responseBody.msg += "==设为主程序失败";
|
|
|
|
+ YG.Log.Instance.WriteLogAdd(serverPath + "设为主程序失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ responseBody.code = 0;
|
|
|
|
+ responseBody.result = false;
|
|
|
|
+ responseBody.msg = "设为主程序失败";
|
|
|
|
+ YG.Log.Instance.WriteLogAdd($"设为主程序失败-->{ex.Message}");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|
|
else
|
|
else
|