MainFrame.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. 
  2. using RabbitMQ.Client;
  3. using RabbitMQ.Client.Events;
  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.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using Cksoft.Unity;
  15. using DllHsms;
  16. namespace LogServer
  17. {
  18. public partial class MainFrame : Form
  19. {
  20. private long CurrCount = 0;
  21. private IModel CurrQueueChannel = null;
  22. private string CurrRecQueueName = "hello";//接收队列名称 hsmsreceive+机台编号
  23. public MainFrame()
  24. {
  25. InitializeComponent();
  26. CurrRecQueueName = System.Configuration.ConfigurationManager.AppSettings["mqname"].ToString();
  27. }
  28. //开始接收队列线程
  29. public int StartRecQueue(ref string errorinfo)
  30. {
  31. try
  32. {
  33. //注册通道
  34. CurrQueueChannel = RegeditChannel(ref errorinfo);
  35. if (CurrQueueChannel == null)
  36. {
  37. errorinfo = "注册队列通道发生错误,错误信息为:" + errorinfo;
  38. return -1;
  39. }
  40. //清空接收队列
  41. //int result = ClearRecQueue(ref errorinfo);
  42. //if (result < 0)
  43. //{
  44. // errorinfo = "清空接收队列发生错误,错误信息为:" + errorinfo;
  45. // return -1;
  46. //}
  47. CurrQueueChannel.QueueDeclare(CurrRecQueueName, false, false, false, null);
  48. //BasicQos函数是针对通道的,必须放在注册消费者前执行,否则注册的消费者会接受多条信息
  49. CurrQueueChannel.BasicQos(0, 1, false);
  50. var consumer = new EventingBasicConsumer(CurrQueueChannel);
  51. CurrQueueChannel.BasicConsume(queue: CurrRecQueueName,
  52. autoAck: false, //是否不要手动应答(no manual Ack),ture自动应答,自动删除处理消息;false手动应答,服务器的消息会等待应答结果才消除
  53. consumer: consumer);
  54. consumer.Received += QueueReceive;
  55. return 1;
  56. }
  57. catch (Exception ex)
  58. {
  59. errorinfo = ex.Message.ToString();
  60. return -1;
  61. }
  62. }
  63. //注册通道
  64. private IModel RegeditChannel(ref string errorinfo)
  65. {
  66. try
  67. {
  68. var factory = new ConnectionFactory();
  69. factory.HostName = System.Configuration.ConfigurationManager.AppSettings["mqaddress"].ToString();
  70. factory.UserName = System.Configuration.ConfigurationManager.AppSettings["mquser"].ToString();
  71. factory.Password = System.Configuration.ConfigurationManager.AppSettings["mqpassword"].ToString();
  72. var connection = factory.CreateConnection();
  73. var channel = connection.CreateModel();
  74. return channel;
  75. }
  76. catch (Exception ex)
  77. {
  78. errorinfo = ex.Message.ToString();
  79. return null;
  80. }
  81. }
  82. private int ClearRecQueue(ref string errorinfo)
  83. {
  84. try
  85. {
  86. CurrQueueChannel.QueueDelete(CurrRecQueueName, true, false);
  87. return 1;
  88. }
  89. catch (Exception ex)
  90. {
  91. errorinfo = ex.Message.ToString();
  92. return -1;
  93. }
  94. }
  95. private void QueueReceive(object model, BasicDeliverEventArgs ea)
  96. {
  97. CurrCount++;
  98. SetText2(CurrCount.ToString());
  99. string errorinfo = "";
  100. int result = WriteLog(ea.Body, ref errorinfo);
  101. if (result <= 0)
  102. {
  103. //写本地日志
  104. SetText1( "\r\n");
  105. SetText1( errorinfo);
  106. }
  107. CurrQueueChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
  108. }
  109. private delegate void SetTextDelegate1(string str);
  110. public void SetText1(string str)
  111. {
  112. if (textBox1.InvokeRequired)
  113. {
  114. // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以
  115. textBox1.Invoke(new SetTextDelegate1(SetText1), str);
  116. }
  117. else
  118. {
  119. textBox1.Text = str;
  120. }
  121. }
  122. private delegate void SetTextDelegate2(string str);
  123. public void SetText2(string str)
  124. {
  125. if (textBox2.InvokeRequired)
  126. {
  127. // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以
  128. textBox2.Invoke(new SetTextDelegate2(SetText2), str);
  129. }
  130. else
  131. {
  132. textBox2.Text = str;
  133. }
  134. }
  135. private int WriteLog(byte[] datas,ref string errorinfo)
  136. {
  137. try
  138. {
  139. HsmsLog log = EntityHelper.DeSerializeBytes<HsmsLog>(datas);
  140. int ftype = log.FType;// int.Parse(row["ftype"].ToString());
  141. int mcaid = log.MacID;// int.Parse(row["mcaid"].ToString());
  142. string logstr = log.Log;// row["log"].ToString();
  143. string fcode = log.MacCode;// row["fcode"].ToString();
  144. Block orgblock = log.OrgBlock;//通讯块
  145. DateTime occurtime = log.OccurTime;//发生时间
  146. string filename = GetLogFile(mcaid, fcode, ref errorinfo);
  147. if (filename == "")
  148. return -1;
  149. logstr = $"发生时间:{occurtime.ToString("yyyy-MM-dd HH:mm:ss.fff")} 机台ID:{mcaid} 机台编号:{fcode} 信息:{logstr}";
  150. FileStream fs;
  151. StreamWriter sw;
  152. if (File.Exists(filename))
  153. {
  154. //验证文件是否存在,有则追加,无则创建
  155. fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
  156. }
  157. else
  158. {
  159. fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
  160. }
  161. sw = new StreamWriter(fs);
  162. sw.WriteLine(logstr);
  163. if(orgblock!=null)
  164. {
  165. //打印指令数据
  166. logstr = orgblock.GetLog(log.Log);
  167. sw.WriteLine(logstr);
  168. }
  169. sw.Close();
  170. fs.Close();
  171. return 1;
  172. }
  173. catch(Exception ex)
  174. {
  175. errorinfo = ex.Message.ToString();
  176. return -1;
  177. }
  178. }
  179. private string GetLogFile(int mcaid,string code,ref string errorinfo)
  180. {
  181. try
  182. {
  183. //每天1个日志
  184. string sFilePath = Application.StartupPath+ "\\log" + DateTime.Now.ToString("yyyyMMdd");
  185. string sFileName = mcaid.ToString() + "_" + code + ".log";
  186. sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
  187. if (!Directory.Exists(sFilePath))//验证路径是否存在
  188. {
  189. Directory.CreateDirectory(sFilePath);
  190. //不存在则创建
  191. }
  192. return sFileName;
  193. }
  194. catch(Exception ex)
  195. {
  196. errorinfo = ex.Message.ToString();
  197. return "";
  198. }
  199. }
  200. private void button2_Click(object sender, EventArgs e)
  201. {
  202. string errorinfo = "";
  203. int result = StartRecQueue(ref errorinfo);
  204. if(result<0)
  205. {
  206. label1.Text = "启动失败:" + errorinfo;
  207. MessageBox.Show("启动接受失败!"+errorinfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  208. }
  209. else
  210. {
  211. label1.Text = "启动成功。";
  212. MessageBox.Show("启动接受成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  213. }
  214. }
  215. //private delegate void SetTextDelegate(string str);
  216. //public void SetText(string str)
  217. //{
  218. // if (this.textBox1.InvokeRequired)
  219. // {
  220. // // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以
  221. // this.textBox1.Invoke(new SetTextDelegate(SetText), str);
  222. // }
  223. // else
  224. // {
  225. // this.textBox1.Text += str;
  226. // }
  227. //}
  228. private void button1_Click(object sender, EventArgs e)
  229. {
  230. var factory = new ConnectionFactory();
  231. factory.HostName = "localhost";
  232. factory.UserName = "guest";
  233. factory.Password = "guest";
  234. using (var connection = factory.CreateConnection())
  235. {
  236. using (var channel = connection.CreateModel())
  237. {
  238. uint result = channel.QueueDelete("hello", true,false);
  239. if(result>0)
  240. {
  241. }
  242. }
  243. }
  244. }
  245. private void Form1_Load(object sender, EventArgs e)
  246. {
  247. }
  248. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  249. {
  250. if (MessageBox.Show("您确定要退出吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.Cancel)
  251. {
  252. e.Cancel = true;
  253. return;
  254. }
  255. System.Environment.Exit(0);
  256. }
  257. }
  258. }