Form1.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using HeidenhainDNCLib;
  2. using IMCS.HeidenHain;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using HEIDENHAIN.body;
  18. using System.Net.NetworkInformation;
  19. namespace HEIDENHAIN
  20. {
  21. public partial class Form1 : Form
  22. {
  23. private int iChannel = 0;
  24. //private string RemotePath = "TNC:\\nc_prog\\ATUO";//ConfigurationManager.AppSettings["RemotePath"];
  25. private string RemotePath = "TNC:\\SMG80SKCX";//ConfigurationManager.AppSettings["RemotePath"];
  26. //连接设备列表
  27. public Dictionary<string, DNC_STATE> deviceList { get; set; } = new Dictionary<string, DNC_STATE>();
  28. private DNC_STATE m_ControlState;
  29. public Dictionary<string, JHMachineInProcess> machineList { get; set; } = new Dictionary<string, JHMachineInProcess>();
  30. //private JHMachineInProcess Machine = new JHMachineInProcess();
  31. // private JHAutomatic m_Automatic = null;
  32. // private JHFileSystem m_FileSystem = null;
  33. //private JHProcessData m_ProcessData = null;
  34. //private JHError m_Error = null;
  35. string Http_Request_Url = "http://127.0.0.1:8011/heidenhain/";
  36. bool _contine = true;//用于线程循环
  37. private AutoResetEvent autoConnectEvent = new AutoResetEvent(false);//此处需要调用System.Threading;用于触发等待的线程已发生的事件(连接)
  38. public delegate void RecvAndSendHandler(HttpListenerContext s);//此处需要调用System.Net用于请求和响应HttpListener类
  39. public event RecvAndSendHandler RecvAndSend;
  40. AsyncCallback callback;
  41. HttpListenerContext context = null;
  42. public Form1()
  43. {
  44. InitializeComponent();
  45. }
  46. private void Form1_Load(object sender, EventArgs e)
  47. {
  48. this.RecvAndSend += new RecvAndSendHandler(HttpListen_RecvAndSend);
  49. #region 添加监听的信息线程添加到线程池
  50. WaitCallback wc = new WaitCallback(http_Listen);
  51. ThreadPool.QueueUserWorkItem(wc);
  52. label1.Text = "HttpServer已开启:" + Http_Request_Url;
  53. #endregion
  54. }
  55. /// <summary>
  56. /// 监听的线程
  57. /// </summary>
  58. /// <param name="ob"></param>
  59. private void http_Listen(object ob)
  60. {
  61. callback = new AsyncCallback(acceptCallback);
  62. HttpListener httpListenner;
  63. httpListenner = new HttpListener();
  64. httpListenner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
  65. httpListenner.Prefixes.Add(Http_Request_Url);
  66. httpListenner.Start();
  67. while (_contine)
  68. {
  69. try
  70. {
  71. httpListenner.BeginGetContext(callback, httpListenner);
  72. autoConnectEvent.WaitOne();
  73. }
  74. catch (Exception)
  75. {
  76. }
  77. Thread.Sleep(10);
  78. }
  79. }
  80. /// <summary>
  81. /// 回调函数
  82. /// </summary>
  83. /// <param name="ar"></param>
  84. private void acceptCallback(IAsyncResult ar)
  85. {
  86. try
  87. {
  88. context = ((HttpListener)ar.AsyncState).EndGetContext(ar);
  89. }
  90. catch (Exception)
  91. {
  92. autoConnectEvent.Set();
  93. }
  94. if (context != null)
  95. {
  96. RecvAndSend(context);//触发我们一开始声明的事件
  97. autoConnectEvent.Set();
  98. }
  99. }
  100. /// <summary>
  101. /// 接听到消息的方法
  102. /// </summary>
  103. /// <param name="cont"></param>
  104. private void HttpListen_RecvAndSend(HttpListenerContext cont)
  105. {
  106. HttpListenerRequest request = cont.Request;
  107. HttpListenerResponse response = context.Response;
  108. Servlet servlet = new MyServlet();
  109. servlet.onCreate();
  110. if (request.HttpMethod == "POST")
  111. {
  112. if (!request.Url.ToString().Contains("favicon"))
  113. {
  114. ResponseBody responseBody = new ResponseBody();
  115. string body = null;
  116. RequestBody hdhBody =null;
  117. try
  118. {
  119. Stream stream = context.Request.InputStream;
  120. StreamReader reader = new StreamReader(stream, Encoding.UTF8);
  121. body = reader.ReadToEnd();
  122. //YG.Log.Instance.WriteLogAdd(">>>===收到POST数据 : >>>>===" + body);
  123. hdhBody = JsonConvert.DeserializeObject<RequestBody>(body);
  124. AddList(DateTime.Now.ToString(), "POST", hdhBody.ServerUrl + ":设备:" + hdhBody.MachineName, "OK:数据接受");
  125. if (hdhBody.Type == ActionTypeEnum.Connect.ToString())
  126. {
  127. Ping pingSender = new Ping();
  128. PingReply reply = pingSender.Send(hdhBody.ServerUrl);
  129. if (reply.Status != IPStatus.Success)
  130. {
  131. responseBody.result = false;
  132. }
  133. }
  134. else
  135. {
  136. //第一次连接加入数组,以支持多台设备
  137. if (deviceList == null || (deviceList.Where(m => m.Key == hdhBody.MachineName).Count() == 0))
  138. {
  139. m_ControlState = connect(hdhBody.MachineName);
  140. //DNC连接正常,加入数组
  141. if (m_ControlState.ToString() == "DNC_STATE_MACHINE_IS_AVAILABLE")
  142. {
  143. deviceList.Add(hdhBody.MachineName, m_ControlState);
  144. }
  145. Thread.Sleep(500);
  146. }
  147. else
  148. {
  149. //取设备对应的状态
  150. m_ControlState = deviceList.Where(m => m.Key == hdhBody.MachineName).FirstOrDefault().Value;
  151. }
  152. if (m_ControlState != null && m_ControlState.ToString() == "DNC_STATE_MACHINE_IS_AVAILABLE")
  153. {
  154. JHMachineInProcess Machine = machineList.Where(m => m.Key == hdhBody.MachineName).FirstOrDefault().Value;
  155. JHError m_Error = Machine.GetInterface(HeidenhainDNCLib.DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHERROR);
  156. JHErrorEntry2List errorsList = m_Error.GetErrorList();
  157. IJHErrorEntry2 pErrorEntry = null;
  158. for (int i = 0; i < errorsList.Count; i++)
  159. {
  160. pErrorEntry = errorsList[i];
  161. if (pErrorEntry != null && pErrorEntry.Text != null)
  162. {
  163. //Console.WriteLine("===" + pErrorEntry.Text.ToString());
  164. responseBody.errorsInfo += pErrorEntry.Text.ToString() + " ";
  165. }
  166. }
  167. if (hdhBody.Type == ActionTypeEnum.Collect.ToString())
  168. {
  169. JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
  170. JHProcessData m_ProcessData = Machine.GetInterface(HeidenhainDNCLib.DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHPROCESSDATA);
  171. object pFeed = new object();
  172. object pSpeed = new object();
  173. object pRapid = new object();
  174. object proStatus = new object();
  175. //进出倍率 主轴倍率
  176. m_Automatic.GetOverrideInfo(ref pFeed, ref pSpeed, ref pRapid);
  177. m_Automatic.GetExecutionMode();
  178. DNC_STS_PROGRAM dncProgram = m_Automatic.GetProgramStatus();
  179. RunDatasInfo runDatasInfo = new RunDatasInfo();
  180. runDatasInfo.feedRate = pFeed.ToString();
  181. runDatasInfo.spindleMagnification = pSpeed.ToString();
  182. runDatasInfo.spindleSpeed = pRapid.ToString();
  183. responseBody.runDatasInfo = JsonConvert.SerializeObject(runDatasInfo);
  184. object oHours = new object();
  185. object oMinutes = new object();
  186. // --- NC uptime --------------------------------------------------------------------------
  187. m_ProcessData.GetNcUpTime(ref oHours, ref oMinutes);
  188. string ncUpTime = oHours.ToString() + ":" + (Convert.ToInt32(oMinutes) > 9 ? oMinutes.ToString() : ("0" + oMinutes.ToString()));
  189. // --- Machine uptime ---------------------------------------------------------------------
  190. m_ProcessData.GetMachineUpTime(ref oHours, ref oMinutes);
  191. string machineUpTime = oHours.ToString() + ":" + (Convert.ToInt32(oMinutes) > 9 ? oMinutes.ToString() : ("0" + oMinutes.ToString()));
  192. // --- Machine running time ---------------------------------------------------------------
  193. m_ProcessData.GetMachineRunningTime(ref oHours, ref oMinutes);
  194. string runningTimes = oHours.ToString() + ":" + (Convert.ToInt32(oMinutes) > 9 ? oMinutes.ToString() : ("0" + oMinutes.ToString()));
  195. }
  196. else if (hdhBody.Type == ActionTypeEnum.Upload.ToString())
  197. {
  198. JHFileSystem m_FileSystem = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHFILESYSTEM);
  199. JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
  200. string sSelectedFile = Path.GetFileName(hdhBody.Path);
  201. string dncPath = null;
  202. string tempDncPath = RemotePath + "\\2.h";
  203. if (hdhBody.Address != "")
  204. {
  205. RemotePath = RemotePath + "\\" + hdhBody.Address;
  206. }
  207. //上传
  208. dncPath = GenPath(RemotePath, sSelectedFile);
  209. //设置临时程序为主程序
  210. m_Automatic.SelectProgram(iChannel, tempDncPath);
  211. try
  212. { //删除上传文件,try异常防止文件不存在
  213. //m_FileSystem.DeleteFile(dncPath);
  214. }
  215. catch (Exception edel)
  216. {
  217. }
  218. //上传
  219. m_FileSystem.TransmitFile(hdhBody.Path, dncPath);
  220. //设当前上传程序为主程序
  221. m_Automatic.SelectProgram(iChannel, dncPath);
  222. }
  223. else if (hdhBody.Type == ActionTypeEnum.DeleteNc.ToString())
  224. {
  225. JHFileSystem m_FileSystem = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHFILESYSTEM);
  226. string dncPath = GenPath(RemotePath, hdhBody.Path);
  227. m_FileSystem.DeleteFile(dncPath);
  228. }
  229. else if (hdhBody.Type == ActionTypeEnum.SelectNcProgram.ToString())//选中程序
  230. {
  231. JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
  232. string dncPath = GenPath(RemotePath, hdhBody.Path);
  233. m_Automatic.SelectProgram(iChannel, dncPath);
  234. }
  235. else if (hdhBody.Type == ActionTypeEnum.StartNcProgram.ToString())//启动程序备用
  236. {
  237. JHAutomatic m_Automatic = Machine.GetInterface(DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
  238. //m_Automatic.SelectProgram(iChannel, GenPath(RemotePath, hdhBody.Path));
  239. //Thread.Sleep(1000);
  240. m_Automatic.StartProgram(GenPath(RemotePath, hdhBody.Path));
  241. }
  242. else if (hdhBody.Type == ActionTypeEnum.Read.ToString())
  243. {
  244. }
  245. else if (hdhBody.Type == ActionTypeEnum.Write.ToString())
  246. {
  247. }
  248. else if (hdhBody.Type == ActionTypeEnum.ToolList.ToString())
  249. {
  250. IJHDataEntry2 ToolLine = null;
  251. IJHDataEntry2List ToolCells = null;
  252. //IJHDataEntry2 ToolCell = null;
  253. List<ToolsInfo> toolsList = new List<ToolsInfo>();
  254. JHDataAccess dataAccess = Machine.GetInterface(HeidenhainDNCLib.DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHDATAACCESS);
  255. dataAccess.SetAccessMode(DNC_ACCESS_MODE.DNC_ACCESS_MODE_TABLEDATAACCESS, "");
  256. //string ToolColumnNamesAccessor = @"\TABLE\TOOL\T\('1'-'50')"; // see Init()
  257. string ToolColumnNamesAccessor = @"\TABLE\TOOL_P\T\('1'-'50')";
  258. IJHDataEntry2 ToolTable = dataAccess.GetDataEntry2(ToolColumnNamesAccessor, DNC_DATA_UNIT_SELECT.DNC_DATA_UNIT_SELECT_METRIC, false);
  259. IJHDataEntry2List ToolLines = ToolTable.GetChildList();
  260. int ToolLinesCount = ToolLines.Count >= 50 ? 50 : ToolLines.Count;
  261. //int ToolLinesCount = ToolLines.Count;
  262. for (int i = 0; i < ToolLinesCount; i++)
  263. {
  264. ToolLine = ToolLines[i];
  265. ToolCells = ToolLine.GetChildList();
  266. // get child list from server
  267. ToolsInfo toolsInfo = new ToolsInfo();
  268. //刀位编码
  269. int[] pCode = ToolCells[0].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA);
  270. toolsInfo.position = string.Join(".", pCode);
  271. toolsInfo.number = ToolCells[1].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
  272. toolsInfo.name = ToolCells[2].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
  273. if (!String.IsNullOrEmpty(toolsInfo.name) && !String.IsNullOrEmpty(toolsInfo.number) && pCode.Length > 0 && pCode[1] > 0)
  274. {
  275. string ToolNumberAccessor = @"\TABLE\TOOL\T\" + toolsInfo.number.ToString();
  276. IJHDataEntry2List ToolList = dataAccess.GetDataEntry2(ToolNumberAccessor, DNC_DATA_UNIT_SELECT.DNC_DATA_UNIT_SELECT_METRIC, false).GetChildList();
  277. //报警期限
  278. toolsInfo.warnLife = ToolList[10].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
  279. //刀具寿命目标值
  280. toolsInfo.targetLife = ToolList[11].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
  281. //Cur_Time使用时间
  282. toolsInfo.curTime = ToolList[12].GetPropertyValue(DNC_DATAENTRY_PROPKIND.DNC_DATAENTRY_PROPKIND_DATA).ToString();
  283. toolsList.Add(toolsInfo);
  284. }
  285. }
  286. //获取海德汉的刀具寿命信息
  287. responseBody.toolsInfo = JsonConvert.SerializeObject(toolsList.Distinct().ToList());
  288. }
  289. }
  290. else
  291. {
  292. responseBody.msg = m_ControlState.ToString();
  293. responseBody.result = false;
  294. deviceList.Remove(hdhBody.MachineName);
  295. machineList.Remove(hdhBody.MachineName);
  296. }
  297. }
  298. }
  299. catch (Exception opcex)
  300. {
  301. YG.Log.Instance.WriteLogAdd($"海德汉响应异常--->>" + opcex.Message);
  302. AddList(DateTime.Now.ToString(), "POST", request.Url.ToString(), opcex.Message);
  303. //发生异常,清空数组,重新连接
  304. deviceList = new Dictionary<string, DNC_STATE>();
  305. machineList = new Dictionary<string, JHMachineInProcess>();
  306. }
  307. finally {
  308. AddList(DateTime.Now.ToString(), "POST", hdhBody.ServerUrl + ":响应数据:" + responseBody.toolsInfo, responseBody.result ? "OK" : "失败:" + m_ControlState != null ? m_ControlState.ToString() : "");
  309. response.ContentType = "application/json;charset=UTF-8";
  310. response.ContentEncoding = Encoding.UTF8;
  311. response.AppendHeader("Content-Type", "application/json;charset=UTF-8");
  312. string retJsonData = JsonConvert.SerializeObject(responseBody);
  313. using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
  314. {
  315. YG.Log.Instance.WriteLogAdd($"海德汉响应结果--->>{JsonConvert.SerializeObject(JsonConvert.SerializeObject(responseBody))}--->>{body}\r\n");
  316. writer.Write(JsonConvert.SerializeObject(responseBody));
  317. writer.Close();
  318. response.Close();
  319. }
  320. }
  321. }
  322. }
  323. else if (request.HttpMethod == "GET")
  324. {
  325. if (!request.Url.ToString().Contains("favicon"))
  326. {
  327. string ip = request.QueryString["ip"];
  328. string port = request.QueryString["port"];
  329. string fun = request.QueryString["fun"];
  330. AddList(DateTime.Now.ToString(), "GET", ip + port + fun, "OK");
  331. }
  332. response.Close();
  333. }
  334. }
  335. private DNC_STATE connect(string connectName)
  336. {
  337. DNC_CNC_TYPE CncType ;
  338. IJHConnectionList connectionList = null;
  339. IJHConnection connection = null;
  340. try
  341. {
  342. JHMachineInProcess Machine = null;
  343. //第一次连接加入数组,以支持多台设备
  344. if (machineList == null || (machineList.Where(m => m.Key == connectName).Count() == 0))
  345. {
  346. Machine = new JHMachineInProcess();
  347. //DNC连接正常,加入数组
  348. machineList.Add(connectName, Machine);
  349. Thread.Sleep(20);
  350. }
  351. else
  352. {
  353. //取对应设备
  354. Machine = machineList.Where(m => m.Key == connectName).FirstOrDefault().Value;
  355. }
  356. Machine.ConnectRequest(connectName);
  357. string sCurrentMachine = Machine.currentMachine;
  358. // Find out control type
  359. connectionList = Machine.ListConnections();
  360. for (int i = 0; i < connectionList.Count; i++)
  361. {
  362. connection = connectionList[i];
  363. if (connection.name == sCurrentMachine)
  364. {
  365. CncType = connection.cncType;
  366. }
  367. if (connection != null)
  368. Marshal.ReleaseComObject(connection);
  369. }
  370. return Machine.GetState();
  371. }
  372. catch (COMException cex)
  373. {
  374. return DNC_STATE.DNC_STATE_NOT_INITIALIZED;
  375. }
  376. catch (Exception ex)
  377. {
  378. return DNC_STATE.DNC_STATE_NOT_INITIALIZED;
  379. }
  380. finally
  381. {
  382. if (connectionList != null)
  383. Marshal.ReleaseComObject(connectionList);
  384. if (connection != null)
  385. Marshal.ReleaseComObject(connection);
  386. }
  387. }
  388. private string GenPath(string part1, string part2)
  389. {
  390. string sFullPath = part1;
  391. switch (part2)
  392. {
  393. case ".":
  394. break;
  395. case "..":
  396. if (part1.EndsWith(@"\") && part1.Length > 5)
  397. part1 = part1.Substring(0, part1.Length - 3);
  398. int iLastFolderPos = part1.LastIndexOf(@"\");
  399. if (iLastFolderPos >= 0)
  400. sFullPath = part1.Substring(0, iLastFolderPos + 1);
  401. break;
  402. default:
  403. if (part1.EndsWith(@"\"))
  404. sFullPath = part1 + part2;
  405. else
  406. sFullPath = part1 + @"\" + part2;
  407. break;
  408. }
  409. return sFullPath;
  410. }
  411. public class Servlet
  412. {
  413. public virtual void onGet(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response, string info) { }
  414. public virtual void onPost(System.Net.HttpListenerRequest request, System.Net.HttpListenerResponse response) { }
  415. public virtual void onCreate()
  416. {
  417. }
  418. }
  419. public void AddList(string dtime, string type, string url, string res)
  420. {
  421. this.Invoke(new Action(delegate ()
  422. {
  423. listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
  424. ListViewItem lvi = new ListViewItem();
  425. lvi.Text = dtime;
  426. lvi.SubItems.Add(type);
  427. lvi.SubItems.Add(url);
  428. lvi.SubItems.Add(res);
  429. this.listView1.Items.Insert(0, lvi);
  430. if (this.listView1.Items.Count > 100)
  431. {
  432. this.listView1.Items.Clear();
  433. }
  434. this.listView1.EndUpdate(); //结束数据处理,UI界面一次性绘制。}
  435. }));
  436. }
  437. public class MyServlet : Servlet
  438. {
  439. public override void onCreate()
  440. {
  441. base.onCreate();
  442. }
  443. public override void onGet(HttpListenerRequest request, HttpListenerResponse response, string info)
  444. {
  445. Console.WriteLine("GET:" + request.Url);
  446. byte[] buffer = Encoding.UTF8.GetBytes(info);
  447. //string sss = request.QueryString["ty"];
  448. System.IO.Stream output = response.OutputStream;
  449. output.Write(buffer, 0, buffer.Length);
  450. // You must close the output stream.
  451. output.Close();
  452. //listener.Stop();
  453. }
  454. public override void onPost(HttpListenerRequest request, HttpListenerResponse response)
  455. {
  456. Console.WriteLine("POST:" + request.Url);
  457. byte[] res = Encoding.UTF8.GetBytes("OK");
  458. response.OutputStream.Write(res, 0, res.Length);
  459. }
  460. }
  461. }
  462. }