123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using OPCUA.Device;
- using RequestServer.HttpServer;
- using ResponseServer.HttpServer;
- using SinumerikOpcUaAPI;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Text;
- using System.Threading.Tasks;
- namespace Fanuc_HttpServer.opcuaserver
- {
- class OpcUaServer
- {
- public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/Work Pieces/TEST1/";
- //public string UPLOAD_NC_PATH = "Sinumerik/FileSystem/NCExtend/";
- public string CHANNEL_NO = "1";
- public string TEMP_NC_NAME = "7999.MPF";
- private uint currentToolArea = 1;
- private static string ON_LINE_STATE = "在线";
- Server opcServer = null;
- public static Dictionary<string, Server> deviceList { get; set; } = new Dictionary<string, Server>();
- public ResponseBody requestHttpServer(RequestBody requestBody)
- {
- string ip = requestBody.serverUrl;
- string port = requestBody.port;
- string fun = requestBody.type;
- ResponseBody responseBody = new ResponseBody();
- if (fun == ActionTypeEnum.Collect.ToString())
- {
- Ping pingSender = new Ping();
- PingReply reply = pingSender.Send(requestBody.serverUrl);
- if (reply.Status != IPStatus.Success)
- {
- //responseBody.result = false;
- responseBody.msg = "调用失败";
- }
- else
- {
- requestBody.serverUrl = "opc.tcp://" + requestBody.serverUrl + ":4840";
- YG.Log.Instance.WriteLogAdd(">>>===come in >>>>===");
- if (deviceList == null || (deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).Count() == 0))
- {
- YG.Log.Instance.WriteLogAdd(">>>===come in deviceList>>>>===");
- try
- {
- opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
- }
- catch (Exception e)
- {
- YG.Log.Instance.WriteLogAdd(">>>===come in deviceList1>>>>===" + e.Message);
- }
- YG.Log.Instance.WriteLogAdd(">>>===come in deviceList2>>>>===");
- if (opcServer.Session != null)
- {
- deviceList.Add(requestBody.serverUrl, opcServer);
- }
- }
- else
- {
- opcServer = deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).FirstOrDefault().Value;
- }
- if (opcServer == null || !opcServer.Session.Connected)
- {
- opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
- }
- //else
- //{
- // opcServer.Session.Reconnect();
- //}
- if (opcServer.Session.Connected)
- {
- responseBody.deviceState = ON_LINE_STATE;
- if (fun == ActionTypeEnum.Connect.ToString()) //连接状态
- {
- responseBody.result = true;
- }
- else if (fun == ActionTypeEnum.Collect.ToString()) //采集
- {
- List<string> addresses = new List<string>();
- addresses.Add("ns=2;s=/Channel/GeometricAxis/feedRateOvr"); //进给倍率
- addresses.Add("ns=2;s=/Channel/State/actFeedRateIpo"); //进给速度
- addresses.Add("ns=2;s=/Channel/Spindle/speedOvr");//主轴倍率
- addresses.Add("ns=2;s=/Channel/Spindle/actSpeed");//主轴实际速度
- //addresses.Add("ns=2;s=/Channel/Spindle/driveLoad");//主轴负载
- addresses.Add("ns=2;s=/Channel/ProgramInfo/selectedWorkPProg");//主程序
- addresses.Add("ns=2;s=/Nck/ChannelDiagnose/poweronTime");
- List<string> values = opcServer.ReadValues(addresses);
- RunDatasInfo runDatasInfo = new RunDatasInfo();
- for (int i = 0; i < values.Count; i++)
- {
- if (i == 0)
- {
- responseBody.feedRateOvr = values[i];
- responseBody.actFeed = values[i];
- }
- else if (i == 1)
- {
- responseBody.actFeed = values[i];
- }
- else if (i == 2)
- {
- responseBody.spindleMagnification = values[i];
- }
- else if (i == 3)
- {
- responseBody.actSpindle = values[i];
- }
- else if (i == 4)
- {
- responseBody.mainProg = values[i];
- }
- else if (i == 5)
- {
- responseBody.powerOnTime = values[i];
- }
- }
- // responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);
- }
- else if (fun == ActionTypeEnum.ToolList.ToString())
- {
- //List<ToolsInfo> toolInfoList = new List<ToolsInfo>();
- //int index = 1;
- //List<string> stringList = this.readVariables(opcServer, new string[6]
- // {
- // "/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())
- // }
- // );
- //ToolsInfo toolInfo = new ToolsInfo(); ;
- //toolInfo.number = Convert.ToUInt16(stringList[0]) + "";
- ////toolInfo.toolIdent = stringList[1];
- ////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);
- // responseBody.toolsInfo = toolInfoList;
- }
- else if (fun == ActionTypeEnum.Read.ToString())
- {
- }
- else if (fun == ActionTypeEnum.Write.ToString())
- {
- }
- else if (fun == ActionTypeEnum.Upload.ToString())
- {
- string file = requestBody.path;
- string serverPath = "";
- //选择临时文件
- string 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
- {
- responseBody.code = 0;
- responseBody.msg = "服务器离线,连不上opcUa";
- responseBody.result = false;
- }
- }
- }
-
- return responseBody;
- }
- }
- }
|