MazakServer.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using Newtonsoft.Json;
  2. using RequestServer.HttpServer;
  3. using ResponseServer.HttpServer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.NetworkInformation;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using static MaCls;
  12. namespace HttpServer.mazak
  13. {
  14. class MazakServer
  15. {
  16. private static string ON_LINE_STATE = "在线";
  17. private static Dictionary<short, string> runModeDict = new Dictionary<short, string>();
  18. public MazakServer()
  19. {
  20. runModeDict.Add(0, "No operation mode");
  21. runModeDict.Add(1, "Automatic operation mode");
  22. runModeDict.Add(2, "Tape operation mode");
  23. runModeDict.Add(3, "Rapid traverse mode");
  24. runModeDict.Add(4, "Home return mode");
  25. runModeDict.Add(5, "Cutting feed/manual pulse mode");
  26. runModeDict.Add(6, "MDI operation mode");
  27. }
  28. static ushort h;
  29. public static ResponseBody requestHttpServer(RequestBody requestBody)
  30. {
  31. string ip = requestBody.serverUrl;
  32. string port = requestBody.port;
  33. string fun = requestBody.type;
  34. YG.Log.Instance.WriteLogAdd($"mazak-请求参数-->> " + JsonConvert.SerializeObject(requestBody));
  35. ResponseBody responseBody = new ResponseBody();
  36. responseBody.deviceState = ON_LINE_STATE;
  37. Ping pingSender = new Ping();
  38. PingReply reply = pingSender.Send(ip);
  39. if (reply.Status != IPStatus.Success)
  40. {
  41. responseBody.deviceState = "离线";
  42. responseBody.msg = "网络不通";
  43. }
  44. else
  45. {
  46. // port = 50100;
  47. int ret = MaCls.MazConnect(out h, ip, 50100, 10);
  48. YG.Log.Instance.WriteLogAdd($"mazak-连接结果-->> " + ret);
  49. RunDatasInfo runDataInfo = new RunDatasInfo();
  50. if (fun == ActionTypeEnum.Collect.ToString())
  51. {
  52. ushort status0 = 0;
  53. short status = 1;
  54. ret = MaCls.MazGetRunningSts(h, status0, ref status);
  55. YG.Log.Instance.WriteLogAdd($"mazak-status-->> " + status);
  56. MAZ_NCONTIME powerTime = default(MAZ_NCONTIME);
  57. ret = MaCls.MazGetNcPowerOnTime(h, ref powerTime);
  58. if(ret == 0)
  59. {
  60. runDataInfo.powerOnTime = powerTime.uint_3.ToString();
  61. }
  62. MAZ_NCTIME runTime = default(MAZ_NCTIME);
  63. ret = MaCls.MazGetRunningTime(h, 0, ref runTime);
  64. YG.Log.Instance.WriteLogAdd($"mazak-powerTime-->> " + powerTime.uint_3);
  65. //获取轴名称
  66. MAZ_AXISNAME axisNameInfo = default(MAZ_AXISNAME);
  67. ret = MaCls.MazGetAxisName(h, ref axisNameInfo);
  68. if(ret == 0)
  69. {
  70. foreach(OneAxisName axisName in axisNameInfo.axis)
  71. {
  72. }
  73. YG.Log.Instance.WriteLogAdd($"mazak-轴名称-->> " + axisNameInfo);
  74. //当前位置
  75. MAZ_NCPOS currentNcPos = default(MAZ_NCPOS);
  76. ret = MaCls.MazGetCurrentPos(h, ref currentNcPos);
  77. if(ret == 0)
  78. {
  79. runDataInfo.absoluteCoordinate = string.Join(",", currentNcPos.data);
  80. }
  81. //相对位置
  82. MAZ_NCPOS relativeNcPos = default(MAZ_NCPOS);
  83. ret = MaCls.MazGetRemain(h, ref relativeNcPos);
  84. if (ret == 0)
  85. {
  86. runDataInfo.relativeCoordinate = string.Join(",", relativeNcPos.data);
  87. }
  88. //机械位置
  89. MAZ_NCPOS machineNcPos = default(MAZ_NCPOS);
  90. ret = MaCls.MazGetCurrentPos(h, ref machineNcPos);
  91. if (ret == 0)
  92. {
  93. runDataInfo.machineCoordinate = string.Join(",", machineNcPos.data);
  94. }
  95. }
  96. //主轴负载
  97. ushort spindleLoad = 0;
  98. ret = MaCls.MazGetSpindleLoad(h, 0, ref spindleLoad);
  99. if (ret == 0)
  100. {
  101. runDataInfo.spindleLoad = spindleLoad.ToString();
  102. }
  103. //进给轴负载
  104. ushort feedAxisLoad = 0;
  105. ret = MaCls.MazGetSpindleLoad(h, 0, ref feedAxisLoad);
  106. if (ret == 0)
  107. {
  108. runDataInfo.feedAxisLoad = feedAxisLoad.ToString();
  109. }
  110. //伺服轴负载
  111. MAZ_AXISLOAD axisLoadInfo = default(MAZ_AXISLOAD);
  112. ret = MaCls.MazGetAxisLoad(h, ref axisLoadInfo);
  113. if (ret == 0)
  114. {
  115. runDataInfo.servoLoad = axisLoadInfo.data[0].ToString();
  116. YG.Log.Instance.WriteLogAdd($"mazak-伺服轴负载-->> " + JsonConvert.SerializeObject(axisLoadInfo));
  117. }
  118. //主程序
  119. MAZ_PROINFO proInfo = default(MAZ_PROINFO);
  120. ret = MaCls.MazGetMainPro(h, status0, ref proInfo);
  121. if (ret == 0)
  122. {
  123. runDataInfo.mainProg = proInfo.comment;
  124. }
  125. //进给速度
  126. MAZ_FEED maz_feed = default(MAZ_FEED);
  127. ret = MaCls.MazGetFeed(h, status0, ref maz_feed);
  128. if (ret == 0)
  129. {
  130. runDataInfo.actFeedSpeed = maz_feed.fmin.ToString();
  131. }
  132. //进给倍率
  133. ushort feedOver = 0;
  134. ret = MaCls.MazGetFeedOverRide(h, 0, ref feedOver);
  135. if (ret == 0)
  136. {
  137. runDataInfo.feedRateOvr = feedOver.ToString();
  138. }
  139. //主轴速度
  140. int spindleSpeed = 0;
  141. ret = MaCls.MazGetCurrentSpindleRev(h, 0, ref spindleSpeed);
  142. if (ret == 0)
  143. {
  144. runDataInfo.actSpindleSpeed = spindleSpeed.ToString();
  145. }
  146. //主轴倍率
  147. ushort spindleRateOvr = 0;
  148. ret = MaCls.MazGetSpindleOverRide(h, 0, ref spindleRateOvr);
  149. if (ret == 0)
  150. {
  151. runDataInfo.spindleRateOvr = spindleRateOvr.ToString();
  152. }
  153. //NC版本信息
  154. MAZ_NC_VERINFO nc_version = default(MAZ_NC_VERINFO);
  155. ret = MaCls.MazGetNCVersionInfo(h, ref nc_version);
  156. if (ret == 0)
  157. {
  158. runDataInfo.ncModel = nc_version.szNCModel;
  159. }
  160. //运行模式
  161. short modeNum = 0;
  162. ret = MaCls.MazGetRunMode(h, 0, ref modeNum);
  163. if (ret == 0)
  164. {
  165. runDataInfo.runMode = runModeDict[modeNum];
  166. }
  167. //运行时间
  168. MAZ_NCTIME nc_time = default(MAZ_NCTIME);
  169. ret = MaCls.MazGetRunningTime(h, 0, ref nc_time);
  170. if (ret == 0)
  171. {
  172. runDataInfo.ncRunTime = nc_time.uint_2.ToString();
  173. }
  174. //工件计数
  175. int partsCount = 0;
  176. ret = MaCls.MazGetPartsCount(h, 0, ref partsCount);
  177. if (ret == 0)
  178. {
  179. runDataInfo.partsCount = partsCount;
  180. }
  181. //当前刀号
  182. MAZ_TOOLINFO currentToolInfo = default(MAZ_TOOLINFO);
  183. ret = MaCls.MazGetCurrentTool(h, 0, ref currentToolInfo);
  184. if (ret == 0)
  185. {
  186. runDataInfo.currentToolNo = currentToolInfo.ushort_0.ToString();
  187. }
  188. //报警
  189. MAZ_ALARMALL maz_ALARMALL = default(MAZ_ALARMALL);
  190. ret = MaCls.MazGetAlarm(h, ref maz_ALARMALL);
  191. if (ret == 0)
  192. {
  193. MAZ_ALARM[] alarms = maz_ALARMALL.alarm;
  194. if (alarms != null && alarms.Count() > 0)
  195. {
  196. List<AlmInfo> errors = new List<AlmInfo>();
  197. foreach (MAZ_ALARM alarm in alarms)
  198. {
  199. AlmInfo addAlarm = new AlmInfo();
  200. addAlarm.no = alarm.short_0.ToString();
  201. addAlarm.msg = alarm.message;
  202. errors.Add(addAlarm);
  203. }
  204. responseBody.errorsInfo = JsonConvert.SerializeObject(errors);
  205. YG.Log.Instance.WriteLogAdd($"mazak-报警-->> " + JsonConvert.SerializeObject(alarms));
  206. }
  207. }
  208. //获取刀具列表,先获取刀具数量
  209. int toolNum = 0;
  210. ret = MaCls.MazGetToolDataNum(h, 0, ref toolNum);
  211. MAZ_TD[] array = new MAZ_TD[toolNum - 1 + 1];
  212. ret = MaCls.MazGetAllToolData(h, 0, toolNum, array);
  213. if (ret == 0)
  214. {
  215. if (array != null && array.Count() > 0)
  216. {
  217. List<ToolsInfo> tools = new List<ToolsInfo>();
  218. foreach (MAZ_TD mazTd in array)
  219. {
  220. ToolsInfo addToolInfo = new ToolsInfo();
  221. addToolInfo.number = mazTd.td_common.byte_0.ToString();
  222. addToolInfo.position = mazTd.td_common.pkno.ToString();
  223. addToolInfo.rateLife = mazTd.td_common.lifetime.ToString();
  224. addToolInfo.useLife = mazTd.td_common.usetime.ToString();
  225. tools.Add(addToolInfo);
  226. }
  227. responseBody.toolsData = JsonConvert.SerializeObject(tools);
  228. }
  229. YG.Log.Instance.WriteLogAdd($"mazak-获取刀具列表-->> 成功" + JsonConvert.SerializeObject(array));
  230. }
  231. else
  232. {
  233. YG.Log.Instance.WriteLogAdd($"mazak-获取刀具列表-->> 失败");
  234. }
  235. responseBody.runDatasInfo = JsonConvert.SerializeObject(runDataInfo);
  236. //responseBody.runDatasInfo = "运行时间:" + powerTime.uint_2 + ",主程序:" + proInfo.string_0 + ",进给:" + maz_feed.fmin/1000 + ",主轴负载:" + spindleLoad + ",进给负载:" + feedOver
  237. // + ",主轴速度:" + spindleSpeed + ",刀具信息:" + JsonConvert.SerializeObject(currentToolInfo);
  238. }
  239. else if (fun == ActionTypeEnum.ToolNoData.ToString())
  240. {
  241. ushort toolno = 0;
  242. MAZ_TDALL toollist = default(MAZ_TDALL);
  243. ret = MaCls.MazGetToolData(h, 0, requestBody.toolNo, ref toollist);
  244. if (ret == 0)
  245. {
  246. responseBody.toolsData = JsonConvert.SerializeObject(toollist);
  247. YG.Log.Instance.WriteLogAdd($"mazak-获取刀具-->> 成功" + JsonConvert.SerializeObject(toollist));
  248. }
  249. else
  250. {
  251. YG.Log.Instance.WriteLogAdd($"mazak-获取刀具-->> 失败");
  252. }
  253. }
  254. else if (fun == ActionTypeEnum.ToolList.ToString())
  255. {
  256. int num = 0;
  257. ret = MaCls.MazGetToolDataNum(h, 0, ref num);
  258. responseBody.runDatasInfo = "刀具编号:" + num;
  259. MAZ_TD[] array = new MAZ_TD[num - 1 + 1];
  260. ret = MaCls.MazGetAllToolData(h, 0, num, array);
  261. if (ret == 0)
  262. {
  263. responseBody.toolsData = JsonConvert.SerializeObject(array);
  264. YG.Log.Instance.WriteLogAdd($"mazak-获取刀具列表-->> 成功" + JsonConvert.SerializeObject(array));
  265. }
  266. else
  267. {
  268. YG.Log.Instance.WriteLogAdd($"mazak-获取刀具-->> 失败");
  269. }
  270. }
  271. else if (fun == ActionTypeEnum.DeleteTool.ToString())
  272. {
  273. ret = MaCls.MazDeleteToolData(h, 0, requestBody.toolNo);
  274. if (ret == 0)
  275. {
  276. YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 成功");
  277. }
  278. else
  279. {
  280. YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 失败");
  281. }
  282. }
  283. else if (fun == ActionTypeEnum.SetTool.ToString())
  284. {
  285. MAZ_TDALL toollist = default(MAZ_TDALL);
  286. ret = MaCls.MazGetToolData(h, 0, requestBody.toolNo, ref toollist);
  287. MAZ_TDALL toolData = new MAZ_TDALL();
  288. MAZ_TD[] tool1 = new MAZ_TD[22];
  289. int count = 0;
  290. foreach (MAZ_TD aZ_TDALL in toollist.tool)
  291. {
  292. MAZ_TD td11 = new MAZ_TD();
  293. td11 = aZ_TDALL;
  294. if (aZ_TDALL.td_common.ushort_0 == requestBody.toolNo)
  295. {
  296. td11.td_common.pkno = 0; //刀位号
  297. td11.td_common.byte_1 = 1; //刀具ID编号对应key
  298. td11.td_common.lengthA = 2950000; //刀具长度
  299. td11.td_common.name = 15; //刀具名称对应key
  300. td11.td_common.int_0 = 20; //公称径
  301. td11.td_common.diameter = 12300; // 刀具径刀尖角
  302. td11.td_common.string_0 = "高速钢"; //材料,机床里要有
  303. td11.td_common.lifetime = 600; //刀具寿命
  304. td11.td_common.lifenumber = 7; //刀具次数
  305. td11.td_common.usetime = 480; //刀具使用时间
  306. td11.td_common.usenumber = 6; //刀具使用次数
  307. td11.td_common.int_1 = 2; // 组号
  308. td11.td_common.compno = 1; //补偿编号
  309. td11.td_common.wearcompX = 1000;
  310. td11.td_common.wearcompY = -1000;
  311. td11.td_common.wearcompZ = 2000;
  312. td11.td_common.maxwearX = 10000;
  313. td11.td_common.maxwearY = 20000;
  314. td11.td_common.maxwearZ = 30000;
  315. td11.td_various.td_general.td_general.angle = 1600;
  316. td11.td_various.td_general.td_general.easycompX = 6541000;
  317. td11.td_various.td_general.td_general.easycompY = 2310;
  318. td11.td_various.td_general.td_general.easycompZ = 2320;
  319. td11.td_various.td_general.td_general.conscompX = 2330;
  320. td11.td_various.td_general.td_general.conscompY = 2340;
  321. td11.td_various.td_general.td_general.conscompZ = -3120;
  322. // td11.td_common.lengthB = 2;
  323. //td11.td_common.dummy5 = "1";
  324. //td11.td_common.compno = 2;
  325. }
  326. tool1[count] = td11;
  327. count++;
  328. }
  329. toollist.tool = tool1;
  330. MAZ_TD td1 = new MAZ_TD();
  331. td1.td_common.ushort_0 = requestBody.toolNo;
  332. td1.td_common.name = 33;
  333. td1.td_common.pkno = 21;
  334. td1.td_common.int_0 = 1;
  335. // td1.td_common.lengthA = 10000+ requestBody.toolNo;
  336. //td1.td_common.lengthB = 20000+ requestBody.toolNo;
  337. MAZ_TD[] tool = new MAZ_TD[20];
  338. tool[0] = td1;
  339. //toolData.tool = tool;
  340. //MAZ_TD td2 = default(MAZ_TD);
  341. //td2.td_common.ushort_0 = 2;
  342. //td2.td_common.name = 22;
  343. //toolData.tool = td2;en
  344. MAZ_TD_ERRORALL errorData = default(MAZ_TD_ERRORALL);
  345. ret = MaCls.MazSetToolData(h, 0, requestBody.toolNo, ref toollist, ref errorData);
  346. if (ret == 0)
  347. {
  348. YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 成功");
  349. }
  350. else
  351. {
  352. YG.Log.Instance.WriteLogAdd($"mazak-删除刀具-->> 失败");
  353. }
  354. }
  355. else if (!string.IsNullOrEmpty(requestBody.path))
  356. {
  357. string fileName = Path.GetFileName(requestBody.path);
  358. string filepath = Path.GetDirectoryName(requestBody.path);
  359. YG.Log.Instance.WriteLogAdd($"mazak-fileName-->> " + fileName + ",filepath=" + filepath);
  360. try
  361. {
  362. if (fun == ActionTypeEnum.Upload.ToString())
  363. {
  364. ret = MaCls.MazSendProgram(h, fileName, filepath, 1);
  365. YG.Log.Instance.WriteLogAdd($"mazak-上传响应结果-->> " + ret);
  366. if (ret == 0)
  367. {
  368. YG.Log.Instance.WriteLogAdd($"mazak-上传-->> " + requestBody.path + "成功");
  369. }
  370. else
  371. {
  372. YG.Log.Instance.WriteLogAdd($"mazak-上传-->> " + requestBody.path + "失败");
  373. }
  374. }
  375. else if (fun == ActionTypeEnum.DownLoad.ToString())
  376. {
  377. ret = MaCls.MazReceiveProgram(h, fileName, filepath, 1);
  378. if (ret == 0)
  379. {
  380. YG.Log.Instance.WriteLogAdd($"mazak-下载-->> " + requestBody.path + "成功");
  381. }
  382. else
  383. {
  384. YG.Log.Instance.WriteLogAdd($"mazak-下载-->> " + requestBody.path + "失败");
  385. }
  386. }
  387. else if (fun == ActionTypeEnum.DeleteNc.ToString())
  388. {
  389. ret = MaCls.MazDeleteProgram(h, fileName);
  390. if (ret == 0)
  391. {
  392. YG.Log.Instance.WriteLogAdd($"mazak-删除-->> " + requestBody.path + "成功");
  393. }
  394. else
  395. {
  396. YG.Log.Instance.WriteLogAdd($"mazak-删除-->> " + requestBody.path + "失败");
  397. }
  398. }
  399. else if (fun == ActionTypeEnum.SelectNcProgram.ToString())
  400. {
  401. ret = MaCls.MazSetMainPro(h, 0, fileName);
  402. if (ret == 0)
  403. {
  404. YG.Log.Instance.WriteLogAdd($"mazak-选择-->> " + requestBody.path + "成功");
  405. }
  406. else
  407. {
  408. YG.Log.Instance.WriteLogAdd($"mazak-选择-->> " + requestBody.path + "失败");
  409. }
  410. }
  411. }
  412. catch (Exception e)
  413. {
  414. YG.Log.Instance.WriteLogAdd($"mazak-异常-->> " + e.Message);
  415. ret = MaCls.MazDisconnect(h);
  416. }
  417. ret = MaCls.MazDisconnect(h);
  418. }
  419. }
  420. return responseBody;
  421. }
  422. }
  423. }