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 deviceList { get; set; } = new Dictionary(); public ResponseBody requestHttpServer(RequestBody requestBody) { string ip = requestBody.serverUrl; string port = requestBody.port; string fun = requestBody.type; ResponseBody responseBody = new ResponseBody(); if (fun == ActionTypeEnum.Connect.ToString()) { Ping pingSender = new Ping(); PingReply reply = pingSender.Send(requestBody.serverUrl); if (reply.Status != IPStatus.Success) { //responseBody.result = false; responseBody.msg = "调用失败"; } } else { if (deviceList == null || (deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).Count() == 0)) { opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password); 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 addresses = new List(); 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 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 toolInfoList = new List(); //int index = 1; //List stringList = this.readVariables(opcServer, new string[6] // { // "/Tool/Catalogue/toolNo[u, ]".Replace("", this.currentToolArea.ToString()).Replace("", index.ToString()), // "/Tool/Catalogue/toolIdent[u, ]".Replace("", this.currentToolArea.ToString()).Replace("", index.ToString()), // "/Tool/Catalogue/toolInMag[u, ]".Replace("", this.currentToolArea.ToString()).Replace("", index.ToString()), // "/Tool/Catalogue/toolInPlace[u, ]".Replace("", this.currentToolArea.ToString()).Replace("", index.ToString()), // "/Tool/Catalogue/nrDuplo[u, ]".Replace("", this.currentToolArea.ToString()).Replace("", index.ToString()), // "/Tool/Catalogue/numCuttEdges[u, ]".Replace("", this.currentToolArea.ToString()).Replace("", 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; } } }