123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
-
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Cksoft.Unity;
- using DllHsms;
- namespace LogServer
- {
- public partial class MainFrame : Form
- {
- private long CurrCount = 0;
- private IModel CurrQueueChannel = null;
- private string CurrRecQueueName = "hello";//接收队列名称 hsmsreceive+机台编号
- public MainFrame()
- {
- InitializeComponent();
- CurrRecQueueName = System.Configuration.ConfigurationManager.AppSettings["mqname"].ToString();
- }
- //开始接收队列线程
- public int StartRecQueue(ref string errorinfo)
- {
- try
- {
- //注册通道
- CurrQueueChannel = RegeditChannel(ref errorinfo);
- if (CurrQueueChannel == null)
- {
- errorinfo = "注册队列通道发生错误,错误信息为:" + errorinfo;
- return -1;
- }
- //清空接收队列
- //int result = ClearRecQueue(ref errorinfo);
- //if (result < 0)
- //{
- // errorinfo = "清空接收队列发生错误,错误信息为:" + errorinfo;
- // return -1;
- //}
-
- CurrQueueChannel.QueueDeclare(CurrRecQueueName, false, false, false, null);
- //BasicQos函数是针对通道的,必须放在注册消费者前执行,否则注册的消费者会接受多条信息
- CurrQueueChannel.BasicQos(0, 1, false);
- var consumer = new EventingBasicConsumer(CurrQueueChannel);
- CurrQueueChannel.BasicConsume(queue: CurrRecQueueName,
- autoAck: false, //是否不要手动应答(no manual Ack),ture自动应答,自动删除处理消息;false手动应答,服务器的消息会等待应答结果才消除
- consumer: consumer);
- consumer.Received += QueueReceive;
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- //注册通道
- private IModel RegeditChannel(ref string errorinfo)
- {
- try
- {
- var factory = new ConnectionFactory();
- factory.HostName = System.Configuration.ConfigurationManager.AppSettings["mqaddress"].ToString();
- factory.UserName = System.Configuration.ConfigurationManager.AppSettings["mquser"].ToString();
- factory.Password = System.Configuration.ConfigurationManager.AppSettings["mqpassword"].ToString();
- var connection = factory.CreateConnection();
- var channel = connection.CreateModel();
- return channel;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- private int ClearRecQueue(ref string errorinfo)
- {
- try
- {
- CurrQueueChannel.QueueDelete(CurrRecQueueName, true, false);
- return 1;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private void QueueReceive(object model, BasicDeliverEventArgs ea)
- {
- CurrCount++;
- SetText2(CurrCount.ToString());
- string errorinfo = "";
- int result = WriteLog(ea.Body, ref errorinfo);
- if (result <= 0)
- {
- //写本地日志
- SetText1( "\r\n");
- SetText1( errorinfo);
- }
- CurrQueueChannel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
- }
- private delegate void SetTextDelegate1(string str);
- public void SetText1(string str)
- {
- if (textBox1.InvokeRequired)
- {
- // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以
- textBox1.Invoke(new SetTextDelegate1(SetText1), str);
- }
- else
- {
- textBox1.Text = str;
- }
- }
- private delegate void SetTextDelegate2(string str);
- public void SetText2(string str)
- {
- if (textBox2.InvokeRequired)
- {
- // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以
- textBox2.Invoke(new SetTextDelegate2(SetText2), str);
- }
- else
- {
- textBox2.Text = str;
- }
- }
- private int WriteLog(byte[] datas,ref string errorinfo)
- {
- try
- {
- HsmsLog log = EntityHelper.DeSerializeBytes<HsmsLog>(datas);
- int ftype = log.FType;// int.Parse(row["ftype"].ToString());
- int mcaid = log.MacID;// int.Parse(row["mcaid"].ToString());
- string logstr = log.Log;// row["log"].ToString();
- string fcode = log.MacCode;// row["fcode"].ToString();
- Block orgblock = log.OrgBlock;//通讯块
- DateTime occurtime = log.OccurTime;//发生时间
- string filename = GetLogFile(mcaid, fcode, ref errorinfo);
- if (filename == "")
- return -1;
- logstr = $"发生时间:{occurtime.ToString("yyyy-MM-dd HH:mm:ss.fff")} 机台ID:{mcaid} 机台编号:{fcode} 信息:{logstr}";
- FileStream fs;
- StreamWriter sw;
- if (File.Exists(filename))
- {
- //验证文件是否存在,有则追加,无则创建
- fs = new FileStream(filename, FileMode.Append, FileAccess.Write);
- }
- else
- {
- fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
- }
- sw = new StreamWriter(fs);
- sw.WriteLine(logstr);
- if(orgblock!=null)
- {
- //打印指令数据
- logstr = orgblock.GetLog(log.Log);
- sw.WriteLine(logstr);
- }
- sw.Close();
- fs.Close();
- return 1;
- }
- catch(Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return -1;
- }
- }
- private string GetLogFile(int mcaid,string code,ref string errorinfo)
- {
- try
- {
- //每天1个日志
- string sFilePath = Application.StartupPath+ "\\log" + DateTime.Now.ToString("yyyyMMdd");
- string sFileName = mcaid.ToString() + "_" + code + ".log";
- sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
- if (!Directory.Exists(sFilePath))//验证路径是否存在
- {
- Directory.CreateDirectory(sFilePath);
- //不存在则创建
- }
- return sFileName;
- }
- catch(Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return "";
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- string errorinfo = "";
- int result = StartRecQueue(ref errorinfo);
- if(result<0)
- {
- label1.Text = "启动失败:" + errorinfo;
- MessageBox.Show("启动接受失败!"+errorinfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- label1.Text = "启动成功。";
- MessageBox.Show("启动接受成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- //private delegate void SetTextDelegate(string str);
- //public void SetText(string str)
- //{
- // if (this.textBox1.InvokeRequired)
- // {
- // // this.txtreceive.BeginInvoke(new ShowDelegate(Show), strshow);//这个也可以
- // this.textBox1.Invoke(new SetTextDelegate(SetText), str);
- // }
- // else
- // {
- // this.textBox1.Text += str;
- // }
- //}
- private void button1_Click(object sender, EventArgs e)
- {
- var factory = new ConnectionFactory();
- factory.HostName = "localhost";
- factory.UserName = "guest";
- factory.Password = "guest";
- using (var connection = factory.CreateConnection())
- {
- using (var channel = connection.CreateModel())
- {
- uint result = channel.QueueDelete("hello", true,false);
- if(result>0)
- {
- }
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (MessageBox.Show("您确定要退出吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.Cancel)
- {
- e.Cancel = true;
- return;
- }
- System.Environment.Exit(0);
- }
- }
- }
|