|
@@ -30,7 +30,10 @@ namespace Fanuc_HttpServer.opcuaserver
|
|
|
string fun = requestBody.type;
|
|
|
|
|
|
ResponseBody responseBody = new ResponseBody();
|
|
|
- if (fun == ActionTypeEnum.Connect.ToString())
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (fun == ActionTypeEnum.Collect.ToString())
|
|
|
{
|
|
|
Ping pingSender = new Ping();
|
|
|
PingReply reply = pingSender.Send(requestBody.serverUrl);
|
|
@@ -39,178 +42,191 @@ namespace Fanuc_HttpServer.opcuaserver
|
|
|
//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()) //连接状态
|
|
|
+ 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))
|
|
|
{
|
|
|
- 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/acFZ"); //进给速度
|
|
|
- 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++)
|
|
|
+ YG.Log.Instance.WriteLogAdd(">>>===come in deviceList>>>>===");
|
|
|
+ try
|
|
|
{
|
|
|
- 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];
|
|
|
-
|
|
|
- }
|
|
|
+ opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ YG.Log.Instance.WriteLogAdd(">>>===come in deviceList1>>>>===" + e.Message);
|
|
|
}
|
|
|
- // responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);
|
|
|
|
|
|
+ YG.Log.Instance.WriteLogAdd(">>>===come in deviceList2>>>>===");
|
|
|
+ if (opcServer.Session != null)
|
|
|
+ {
|
|
|
+ deviceList.Add(requestBody.serverUrl, opcServer);
|
|
|
+ }
|
|
|
}
|
|
|
- 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
|
|
|
{
|
|
|
-
|
|
|
+ opcServer = deviceList.Where(m => m.Key.Equals(requestBody.serverUrl)).FirstOrDefault().Value;
|
|
|
}
|
|
|
- else if (fun == ActionTypeEnum.Write.ToString())
|
|
|
+ if (opcServer == null || !opcServer.Session.Connected)
|
|
|
{
|
|
|
-
|
|
|
+ opcServer = DeviceOPCUat.OpcUa_Connection1(requestBody.serverUrl, requestBody.userName, requestBody.password);
|
|
|
}
|
|
|
+ //else
|
|
|
+ //{
|
|
|
+ // opcServer.Session.Reconnect();
|
|
|
+ //}
|
|
|
|
|
|
- else if (fun == ActionTypeEnum.Upload.ToString())
|
|
|
+ if (opcServer.Session.Connected)
|
|
|
{
|
|
|
- string file = requestBody.path;
|
|
|
- string serverPath = "";
|
|
|
- //选择临时文件
|
|
|
- string status = opcServer.MethodCallSelectProgram(UPLOAD_NC_PATH + TEMP_NC_NAME, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
|
- try
|
|
|
+ responseBody.deviceState = ON_LINE_STATE;
|
|
|
+ if (fun == ActionTypeEnum.Connect.ToString()) //连接状态
|
|
|
{
|
|
|
- string extension = Path.GetExtension(file);
|
|
|
- if (string.IsNullOrWhiteSpace(extension))
|
|
|
+ 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++)
|
|
|
{
|
|
|
- responseBody.msg = "文件名不合法";
|
|
|
+ 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];
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
- try
|
|
|
- {
|
|
|
+ // 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())
|
|
|
+ {
|
|
|
|
|
|
- byte[] data = opcServer.ReadFile(file);
|
|
|
+ }
|
|
|
+ else if (fun == ActionTypeEnum.Write.ToString())
|
|
|
+ {
|
|
|
|
|
|
- serverPath = UPLOAD_NC_PATH + Path.GetFileName(file);
|
|
|
- Server.MethodCallResult methodCallResult = opcServer.MethodCallCopyFileToServer("/Methods/CopyFileToServer", serverPath, data, true);
|
|
|
+ }
|
|
|
|
|
|
- if (methodCallResult.status.ToUpper().Equals("GOOD"))
|
|
|
+ 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 = "上传文件成功";
|
|
|
- YG.Log.Instance.WriteLogAdd(file + "上传文件成功");
|
|
|
+ responseBody.msg = "文件名不合法";
|
|
|
}
|
|
|
- else
|
|
|
+ try
|
|
|
{
|
|
|
- responseBody.msg = "上传文件失败";
|
|
|
- YG.Log.Instance.WriteLogAdd(file + "上传文件失败");
|
|
|
+
|
|
|
+ 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($"{ex.Message}");
|
|
|
+ YG.Log.Instance.WriteLogAdd($"493-->{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;
|
|
|
}
|
|
|
- //设位主程序
|
|
|
- status = opcServer.MethodCallSelectProgram(serverPath, Convert.ToUInt32(CHANNEL_NO)).status;
|
|
|
- }
|
|
|
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- responseBody.code = 0;
|
|
|
- responseBody.msg = "服务器离线,连不上opcUa";
|
|
|
- responseBody.result = false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ responseBody.code = 0;
|
|
|
+ responseBody.msg = "服务器离线,连不上opcUa";
|
|
|
+ responseBody.result = false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return responseBody;
|
|
|
}
|
|
|
}
|