123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- using HslCommunication.Enthernet;
- using IMCS.Device;
- using IMCS_CCS.Utils.DeviceProtocol;
- using Newtonsoft.Json;
- using Opc.Ua;
- using Opc.Ua.Client;
- using SinumerikOpcUaAPI;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using YG;
- using YG.Device;
- using static SinumerikOpcUaAPI.Server;
- using System.Collections;
- using System.Runtime.Remoting.Messaging;
- using System.Reflection.Emit;
- using System.Runtime.Remoting.Channels;
- using System.Net.NetworkInformation;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
- namespace IMCS
- {
- public partial class Form_Main : Form
- {
- //public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/Work Pieces/";
- public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/NCExtend/wks.dir/nc_program/";
- public string UPLOAD_TOOL_PATH = "Sinumerik/FileSystem/NCExtend/wks.dir/tool_device/";
- //public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/Work Pieces/WORK_NC/";
- public string CHANNEL_NO = "1";
- public string TEMP_NC_NAME = "7999.MPF";
- private uint currentToolArea = 1;
- private ushort mNAMESPACE_INDEX = 2;
- // 作为长连接进行bean的存储, key使用ip,
- Dictionary<string, Server> opcServerMap = new Dictionary<string, Server>();
-
- public Dictionary<string, Server> deviceList { get; set; } = new Dictionary<string, Server>();
- public Form_Main()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- HttpListener httpListenner;
- httpListenner = new HttpListener();
- httpListenner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
- httpListenner.Prefixes.Add("http://127.0.0.1:8010/opcua/");
- httpListenner.Start();
- label1.Text = "HttpServer已开启:http://127.0.0.1:8010/opcua/";
- new Thread(new ThreadStart(delegate
- {
- try
- {
- loop(httpListenner);
- }
- catch (Exception ex)
- {
- string ss = ex.ToString();
- httpListenner.Stop();
- /* Application.ExitThread();
- Application.Exit();
- Application.Restart();
- Process.GetCurrentProcess().Kill();*/
- }
- })).Start();
- }
- private void loop(HttpListener httpListenner)
- {
- while (true)
- {
- HttpListenerContext context = httpListenner.GetContext();
- context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
- HttpListenerRequest request = context.Request;
- HttpListenerResponse response = context.Response;
- Servlet servlet = new MyServlet();
- servlet.onCreate();
- if (request.HttpMethod == "POST")
- {
- if (!request.Url.ToString().Contains("favicon"))
- {
- RequestBody opcBody = null;
- ResponseBody responseBody = new ResponseBody();
- Server opcServer = null;
- try
- {
- Stream stream = context.Request.InputStream;
- StreamReader reader = new StreamReader(stream, Encoding.UTF8);
- string body = reader.ReadToEnd();
- YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
-
- opcBody = JsonConvert.DeserializeObject<RequestBody>(body);
- AddList(DateTime.Now.ToString(), "POST", opcBody.ServerUrl + opcBody.Type, "OK");
- /*if (deviceList == null || (deviceList.Where(m => m.Key.Equals(opcBody.ServerUrl)).Count() == 0))
- {
- opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
- if (opcServer.Session != null)
- {
- deviceList.Add(opcBody.ServerUrl, opcServer);
- }
- }
- else
- {
- opcServer = deviceList.Where(m => m.Key.Equals(opcBody.ServerUrl)).FirstOrDefault().Value;
- }*/
- /*if (opcServer == null || !opcServer.Session.Connected)
- {
- opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
- }*/
-
- // 从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);
- }
- }
- //opcServer = DeviceOPCUat.OpcUa_Connection1(opcBody.ServerUrl, opcBody.UserName, opcBody.Password);
- //opcServer.IsOn
- //else
- //{
- // opcServer.Session.Reconnect();
- //}
- if (opcServer!=null && opcServer.Session!=null && opcServer.Session.Connected)
- {
- if (opcBody.Type == ActionTypeEnum.Connect.ToString()) //连接状态
- {
- responseBody.result = true;
- }
- else if (opcBody.Type == ActionTypeEnum.Collect.ToString()) //采集
- {
- //ETH_S7Simens s7 = new ETH_S7Simens("192.168.11.164");
- //responseBody.runDatasInfo = JsonConvert.SerializeObject(s7.deviceInfo);
- if(opcBody.check == null)
- {
- // 封装需要采集字段的数据
- this.addAddress(opcBody);
- }
- else
- {
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.6");//千机质检结果
- }
- List<string> values = opcServer.ReadValues(opcBody.Addresses);
- responseBody.values = values;
- /*RunDatasInfo runDatasInfo = new RunDatasInfo();
- for (int i = 0; i < values.Count; i++)
- {
- if (i == 0)
- {
- runDatasInfo.feedRate = values[i];
- }
- else if (i == 1)
- {
- runDatasInfo.spindleMagnification = values[i];
- }
- else if (i == 2)
- {
- runDatasInfo.spindleSpeed = values[i];
- }
- else if (i == 3)
- {
- runDatasInfo.spindleLoad = values[i];
- }
- else if (i == 4)
- {
- runDatasInfo.mainPro = values[i];
- }
- else if (i == 5)
- {
- runDatasInfo.content = values[i];
- }
- }
- responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);*/
- /*//报警
- SinumerikOpcUaAPI.FilterDefinition m_filter = new SinumerikOpcUaAPI.FilterDefinition();
- m_filter.AreaId = new NodeId("Sinumerik", (ushort)2);
- m_filter.Severity = EventSeverity.Min;
- m_filter.IgnoreSuppressedOrShelved = true;
- m_filter.EventTypes = (IList<NodeId>)new NodeId[1]
- {
- ObjectTypeIds.ConditionType
- };
- m_filter.SelectClauses = m_filter.ConstructSelectClauses(opcServer.Session, ObjectTypeIds.ConditionType);
- for (int index = 0; index < m_filter.SelectClauses.Count<SimpleAttributeOperand>(); ++index)
- {
- responseBody.errorsInfo += m_filter.SelectClauses[index].ToString() + (object)index + " ";
- }*/
- }
- else if (opcBody.Type == ActionTypeEnum.ToolList.ToString())
- {
- List<string> nums = this.readVariables(opcServer, new string[1]
- {
- "/Tool/Catalogue/numTools[u<Area index>]".Replace("<Area index>", this.currentToolArea.ToString())
- }
- );
- int num = int.Parse(nums[0]);
- List<ToolsInfo> toolInfoList = new List<ToolsInfo>();
- for (int index = 1; index <= num; index++)
- {
- try
- {
- List<string> stringList = this.readVariables(opcServer, new string[10]
- {
- "/Tool/Catalogue/toolNo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
- "/Tool/Catalogue/toolIdent[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
- "/Tool/Catalogue/toolInMag[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
- "/Tool/Catalogue/toolInPlace[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
- "/Tool/Catalogue/nrDuplo[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
- "/Tool/Catalogue/numCuttEdges[u<Area index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Row index>", index.ToString()),
- "/Tool/Compensation/edgeData[u<Area index>,c<Column index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Column index>",index.ToString()).Replace("<Row index>", "3"),
- "/Tool/Compensation/edgeData[u<Area index>,c<Column index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Column index>",index.ToString()).Replace("<Row index>", "6"),
- "/Tool/Compensation/edgeData[u<Area index>,c<Column index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Column index>",index.ToString()).Replace("<Row index>", "12"),
- "/Tool/Compensation/edgeData[u<Area index>,c<Column index>, <Row index>]".Replace("<Area index>", this.currentToolArea.ToString()).Replace("<Column index>",index.ToString()).Replace("<Row index>", "15")
- }
- );
- ToolsInfo toolInfo = new ToolsInfo(); ;
- toolInfo.number = Convert.ToUInt16(stringList[0]) + "";
- toolInfo.name = stringList[1];
- toolInfo.length = stringList[6];
- toolInfo.radius = stringList[7];
- toolInfo.wearLength = stringList[8];
- toolInfo.wearRadius = stringList[9];
- //toolInfo.toolInMag = Convert.ToUInt16(stringList[2]);
- //toolInfo.toolInPlace = Convert.ToUInt16(stringList[3]);
- //toolInfo.nrDuplo = Convert.ToUInt16(stringList[4]);
- //toolInfo.numCuttEdges = Convert.ToUInt16(stringList[5]);
- toolInfoList.Add(toolInfo);
- }
- catch (Exception ex)
- {
- YG.Log.Instance.WriteLogAdd($"{ex.Message}"+":"+ index);
- }
-
- }
- responseBody.toolsInfo = JsonConvert.SerializeObject(toolInfoList);
- }
- else if (opcBody.Type == ActionTypeEnum.Read.ToString())
- {
- responseBody.values = opcServer.ReadValues(opcBody.Addresses);
- }
- else if (opcBody.Type == ActionTypeEnum.Write.ToString())
- {
- opcServer.WriteValues(opcBody.Values, opcBody.Addresses);
- }
- else if (opcBody.Type == ActionTypeEnum.SelectNc.ToString())
- {
- string sSelectedFileName = Path.GetFileName(opcBody.Path);
- string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + sSelectedFileName, Convert.ToUInt32(CHANNEL_NO)).status;
- if (status == "Good")
- {
- responseBody.msg = "选择成功";
- }
- else
- {
- responseBody.msg = "选择失败";
- }
- }
- else if (opcBody.Type == ActionTypeEnum.Upload.ToString())
- {
- string file = opcBody.Path;
- string serverPath = "";
- string status = "";
- //选择临时文件
- status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
- try
- {
- string extension = Path.GetExtension(file);
- if (string.IsNullOrWhiteSpace(extension))
- {
- responseBody.msg = "文件名不合法";
- }
- try
- {
- byte[] data = opcServer.ReadFile(file);
- serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
- Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
- {
- responseBody.msg = "上传文件成功";
- YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
- }
- else
- {
- responseBody.msg = "上传文件失败";
- YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
- }
- // }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"{ex.Message}");
- }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
- }
- //设位主程序
- status = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO)).status;
- }
- else if (opcBody.Type == ActionTypeEnum.UploadFileList.ToString())
- {
- //选择临时文件
- string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
- for (int i = 0; i < opcBody.subFileList.Count; i++)
- {
- string file = opcBody.subFileList[i];
- string serverPath = "";
- try
- {
- string extension = Path.GetExtension(file);
- if (string.IsNullOrWhiteSpace(extension))
- {
- responseBody.result = false;
- responseBody.msg = file + "==文件名不合法";
- break;
- }
- try
- {
- byte[] data = opcServer.ReadFile(file);
- serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
- Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
- {
- responseBody.msg = "上传文件成功";
- YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
- }
- else
- {
- responseBody.msg = "上传文件失败";
- responseBody.result = false;
- YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
- break;
- }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"{ex.Message}");
- break;
- }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- responseBody.result = false;
- YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
- }
- }
- //设为主程序
- status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + opcBody.prgName, Convert.ToUInt32(CHANNEL_NO)).status;
- if (status != "Good")
- {
- responseBody.msg = "选择失败";
- responseBody.result = false;
- }
- for (int i = 0; i < opcBody.toolFileList.Count; i++)
- {
- string file = opcBody.toolFileList[i];
- string serverPath = "";
- try
- {
- string extension = Path.GetExtension(file);
- if (string.IsNullOrWhiteSpace(extension))
- {
- responseBody.result = false;
- responseBody.msg = file + "==文件名不合法";
- break;
- }
- try
- {
- byte[] data = opcServer.ReadFile(file);
- serverPath = UPLOAD_TOOL_PATH + Path.GetFileName(file);
- Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
- {
- YG.Log.Instance.WriteLogAdd(file + "对刀仪上传文件成功");
- }
- else
- {
-
- YG.Log.Instance.WriteLogAdd(file + "对刀仪上传文件失败");
- break;
- }
- }
- catch (Exception ex)
- {
-
- YG.Log.Instance.WriteLogAdd($"{ex.Message}");
- break;
- }
- }
- catch (Exception ex)
- {
- YG.Log.Instance.WriteLogAdd($"对刀仪493-->{ex.Message}");
- }
- }
-
- }
- else if (opcBody.Type == ActionTypeEnum.DownLoad.ToString())
- {
- string file = opcBody.Path;
- string serverPath = "";
- try
- {
- string extension = Path.GetExtension(file);
- if (string.IsNullOrWhiteSpace(extension))
- {
- responseBody.msg = "文件名不合法";
- }
- try
- {
- //byte[] data = opcServer.ReadFile(file);
- serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
- // Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileFromServer("/Methods/CopyFileFromServer", serverPath);
- //
- byte[] op = opcServer.ReadFile(serverPath);
- DiagnosticInfoCollection diagnosticInfos = null;
- CallMethodRequestCollection callMethodRequestCollection = new CallMethodRequestCollection();
- CallMethodResultCollection results = new CallMethodResultCollection();
- CallMethodRequest callMethodRequest = new CallMethodRequest();
- callMethodRequest.MethodId = new NodeId("/Methods/CopyFileFromServer", mNAMESPACE_INDEX);
- callMethodRequest.ObjectId = new NodeId("/Methods", mNAMESPACE_INDEX);
- callMethodRequest.InputArguments.Add(serverPath);
- //callMethodRequest.InputArguments.Add(data);
- callMethodRequestCollection.Add(callMethodRequest);
- RequestHeader requestHeader = new RequestHeader();
- ResponseHeader responseHeader = opcServer.Session.Call(null, callMethodRequestCollection, out results, out diagnosticInfos);
- Server.MethodCallResult methodCallResult = new MethodCallResult(results);
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
- {
- responseBody.msg = "上传文件成功";
- YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
- Variant result3 = methodCallResult.results;
- byte[] buff;
- using (MemoryStream ms = new MemoryStream())
- {
- IFormatter iFormatter = new BinaryFormatter();
- iFormatter.Serialize(ms, result3.Value);
- buff = ms.GetBuffer();
- }
- // 将byte[]写入文件
- File.WriteAllBytes(file, buff);
- // 读取文件内容到字符串
- string fileContent = File.ReadAllText(file);
- fileContent = fileContent.Trim();
- // 去除字符串中的空格
- //string trimmedContent = fileContent.Trim(); // 使用Trim去除字符串首尾的空格
- // 或者使用Replace去除所有空格
- string trimmedContent = fileContent.Replace(" ", "");
- // 将处理过的字符串写回文件
- File.WriteAllText(file, trimmedContent);
- string aa = result3.ToString();
- }
- else
- {
- responseBody.msg = "上传文件失败";
- YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
- }
- // }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"{ex.Message}");
- }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
- }
- }
- else if (opcBody.Type == ActionTypeEnum.Delete.ToString())
- {
- string file = opcBody.Path;
- string serverPath = "";
- try
- {
- string extension = Path.GetExtension(file);
- if (string.IsNullOrWhiteSpace(extension))
- {
- responseBody.msg = "文件名不合法";
- }
- else
- {
- try
- {
- //byte[] data = opcServer.ReadFile(file);
- serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
- Server.MethodCallResult methodCallResult = opcServer.MethodCallDeleteFile(serverPath);
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
- {
- responseBody.msg = "删除文件成功";
- YG.Log.Instance.WriteLogAdd(file + "删除文件成功");
- }
- else
- {
- responseBody.msg = "上传文件失败";
- YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
- }
- // }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"{ex.Message}");
- }
- }
- }
- catch (Exception ex)
- {
- responseBody.msg = "上传文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"493-->{ex.Message}");
- }
- }
- else if (opcBody.Type == ActionTypeEnum.SynFile.ToString())
- {
- try
- {
- Server.MethodCallResult methodCallResult = opcServer.MethodCall("GetAllFiles", UPLOAD_NC_PATH);
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
- {
- responseBody.msg = "同步文件成功";
- }
- else
- {
- responseBody.msg = "同步文件失败";
- }
- // }
- }
- catch (Exception ex)
- {
- responseBody.msg = "同步文件失败===>" + ex.Message;
- YG.Log.Instance.WriteLogAdd($"{ex.Message}");
- }
- }
- }
- else
- {
- responseBody.code = 0;
- responseBody.msg = "服务器离线,连不上opcUa";
- responseBody.result = false;
- }
- YG.Log.Instance.WriteLogAdd("Disconnect!");
- response.ContentType = "application/json;charset=UTF-8";
- response.ContentEncoding = Encoding.UTF8;
- response.AppendHeader("Content-Type", "application/json;charset=UTF-8");
- string retJsonData = JsonConvert.SerializeObject(responseBody);
- YG.Log.Instance.WriteLogAdd($"WMS--->>{responseBody.Json_SerializeObject()}--->>{body}\r\n");
- }
- catch (Exception opcex)
- {
- // 删除map中对应的bean
- opcServerMap.Remove(opcBody.ServerUrl);
- // 断开连接
- if (opcServer!=null && opcServer.Session != null)
- {
- opcServer.Disconnect();
- }
- responseBody.msg = opcex.Message;
- responseBody.result = false;
- AddList(DateTime.Now.ToString(), "POST", request.Url.ToString(), opcex.Message);
- YG.Log.Instance.WriteLogAdd(opcex.StackTrace);
- }
- finally
- {
- using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
- {
-
- writer.Write(responseBody.Json_SerializeObject());
- writer.Close();
- //response.Close();
- }
- }
- }
- }
- else if (request.HttpMethod == "GET")
- {
- if (!request.Url.ToString().Contains("favicon"))
- {
- string ip = request.QueryString["ip"];
- string port = request.QueryString["port"];
- string fun = request.QueryString["fun"];
- AddList(DateTime.Now.ToString(), "GET", ip + port + fun, "OK");
- }
- }
- response.Close();
- }
- }
- private List<string> readVariables(Server server, string[] variables)
- {
- List<string> nodeIdStrings = new List<string>();
- foreach (string variable in variables)
- {
- NodeId nodeId = new NodeId(variable, (ushort)2);
- nodeIdStrings.Add(nodeId.ToString());
- }
- return server.ReadValues(nodeIdStrings);
- }
- private void addAddress(RequestBody opcBody)
- {
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX310.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX311.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX311.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX311.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX312.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX312.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX313.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX314.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX314.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX314.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX316.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX326.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX326.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX326.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX326.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX491.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX493.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX493.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX495.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX497.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX497.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX499.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX501.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX501.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.5");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.6");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX554.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.5");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.6");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX555.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX556.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX556.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX556.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX556.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX556.6");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX556.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX557.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX557.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX557.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX557.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX557.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX557.5");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX558.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX558.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX559.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX559.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX559.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX559.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX560.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX560.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX560.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX560.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX560.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX560.5");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX570.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.5");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.6");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX574.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX575.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX575.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX578.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX578.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX578.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX579.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX579.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX579.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX579.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX590.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX590.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX590.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX624.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX624.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.0");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.1");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.2");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.3");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.4");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.5");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.6");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB2.DBX626.7");
- opcBody.Addresses.Add("ns=2;s=/Plc/DB10.DBX106.1");
-
-
- /*opcBody.Addresses.Add("ns=2;s=/Nck/SequencedAlarms/timeBCD");
- opcBody.Addresses.Add("ns=2;s=/Nck/SequencedAlarms/textIndex");*/
- opcBody.Addresses.Add("ns=2;s=/Channel/GeometricAxis/feedRateOvr");
- opcBody.Addresses.Add("ns=2;s=/Channel/Spindle/speedOvr");
- opcBody.Addresses.Add("ns=2;s=/Channel/Spindle/actSpeed");
- opcBody.Addresses.Add("ns=2;s=/Channel/MachineAxis/vaLoad");
- opcBody.Addresses.Add("ns=2;s=/Nck/ChannelDiagnose/setupTime");
- opcBody.Addresses.Add("ns=2;s=/Channel/ChannelDiagnose/operatingTime");
- opcBody.Addresses.Add("ns=2;s=/Channel/ProgramInfo/selectedWorkPProg");
- }
- public class DeviceInfo
- {
- public string ConnectMsg
- { get; set; }
- public string ConnectErrorMsg
- { get; set; }
- public string CncSoftVersion
- { get; set; }
- public string CncType
- { get; set; }
- public string CncId
- { get; set; }
- public string CncMode
- { get; set; }
- public string RunStatus
- { get; set; }
- public string Products
- { get; set; }
- public string CycleTime
- { get; set; }
- public string RemainTime
- { get; set; }
- public string MainProg
- { get; set; }
- public string SetFeedSpeed
- { get; set; }
- public string ActFeedSpeed
- { get; set; }
- public string SetSpSpeed
- { get; set; }
- public string ActSpSpeed
- { get; set; }
- public string AxisName
- { get; set; }
- public string FeedRate
- { get; set; }
- public string SpRate
- { get; set; }
- public string MaxPos1
- { get; set; }
- public string MaxPos2
- { get; set; }
- public string MaxPos3
- { get; set; }
- public string RelPos1
- { get; set; }
- public string RelPos2
- { get; set; }
- public string RelPos3
- { get; set; }
- public string RemainPos1
- { get; set; }
- public string RemainPos2
- { get; set; }
- public string RemainPos3
- { get; set; }
- public string ToolNo
- { get; set; }
- public string LenthX
- { get; set; }
- public string LenthZ
- { get; set; }
- public string RadiusCompenNum
- { get; set; }
- public string LengthCompenNum
- { get; set; }
- public string ToolAbradeRadius
- { get; set; }
- public string ToolEdgePos
- { get; set; }
- public string IsAlarm
- { get; set; }
- public List<AlmInfo> AlmMsg { get; set; } = new List<AlmInfo>();
- public string ToolLife
- { get; set; }
- }
- public class AlmInfo
- {
- public string no { get; set; }
- public string msg { get; set; }
- }
- public class Servlet
- {
- public virtual void onGet(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response, string info) { }
- public virtual void onPost(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response) { }
- public virtual void onCreate()
- {
- }
- }
- public void AddList(string dtime, string type, string url, string res)
- {
- this.Invoke(new Action(delegate ()
- {
- listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
- ListViewItem lvi = new ListViewItem();
- lvi.Text = dtime;
- lvi.SubItems.Add(type);
- lvi.SubItems.Add(url);
- lvi.SubItems.Add(res);
- this.listView1.Items.Insert(0, lvi);
- if (this.listView1.Items.Count > 100)
- {
- this.listView1.Items.Clear();
- }
- this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。}
- }));
- }
- public class MyServlet : Servlet
- {
- public override void onCreate()
- {
- base.onCreate();
- }
- public override void onGet(HttpListenerRequest request, HttpListenerResponse response, string info)
- {
- Console.WriteLine("GET:" + request.Url);
- byte[] buffer = Encoding.UTF8.GetBytes(info);
- //string sss = request.QueryString["ty"];
- System.IO.Stream output = response.OutputStream;
- output.Write(buffer, 0, buffer.Length);
- // You must close the output stream.
- output.Close();
- //listener.Stop();
- }
- public override void onPost(HttpListenerRequest request, HttpListenerResponse response)
- {
- Console.WriteLine("POST:" + request.Url);
- byte[] res = Encoding.UTF8.GetBytes("OK");
- response.OutputStream.Write(res, 0, res.Length);
- }
- }
- }
- }
|