1
duan
2024-08-21 f71a02229c1ba00fbecaead19256593ffb052753
提交 | 用户 | age
d7a15c 1 using Newtonsoft.Json;
D 2 using SqlSugar;
3 using System;
90c6eb 4 using System.Collections.Generic;
J 5 using System.ComponentModel;
6 using System.Data;
7 using System.Drawing;
8 using System.Drawing.Drawing2D;
9 using System.Drawing.Imaging;
10 using System.IO;
11 using System.Linq;
12 using System.Runtime.InteropServices;
13 using System.Text;
14 using System.Threading.Tasks;
15 using System.Windows.Forms;
4b8004 16 using uPLibrary.Networking.M2Mqtt;
J 17 using uPLibrary.Networking.M2Mqtt.Messages;
d7a15c 18 using WindowsFormsApp1.Common;
D 19 using WindowsFormsApp1.Controllers;
20 using WindowsFormsApp1.Entity;
21 using WindowsFormsApp1.Helper;
22 using WindowsFormsApp1.Item;
90c6eb 23
J 24 namespace WindowsFormsApp1
25 {
26     public partial class Form1 : Form
27     {
d7a15c 28         private Metafile metafile1;
D 29         private Graphics.EnumerateMetafileProc metafileDelegate;
30         private MqttClient client = null;
31
90c6eb 32         public Form1()
4b8004 33         { 
90c6eb 34             InitializeComponent();
d7a15c 35             
90c6eb 36         }
d7a15c 37
D 38
39         string _UserNo { get { return MqttconfigHelp.UserName.Trim(); } }
40         string _PW { get { return MqttconfigHelp.Password.Trim(); } }
90c6eb 41         private bool MetafileCallback(EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData)
J 42         {
43             byte[] dataArray = null;
44             if (data != IntPtr.Zero)
45             {
46
47                 dataArray = new byte[dataSize];
48                 Marshal.Copy(data, dataArray, 0, dataSize);
49                 //GdipComment
50                 metafile1.PlayRecord(recordType, flags, dataSize, dataArray);
51                 switch (recordType) //记录类型
52                 {
53
54                     case EmfPlusRecordType.Object: //对象
55                     case EmfPlusRecordType.DrawLines: //线
56                     case EmfPlusRecordType.SetPageTransform: //设置页变换
57                         break;
58                 }
59             }
60             Console.WriteLine(recordType.ToString());
61             return true;
62         }
63
64         protected override void OnPaint(PaintEventArgs e)
65         {
d7a15c 66             //PointF destPoint = new PointF(e.ClipRectangle.X, e.ClipRectangle.Y);
4b8004 67            //e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
90c6eb 68
J 69         }
70         protected override void OnClosed(EventArgs e)
71         {
72             base.OnClosed(e);
4b8004 73             //metafile1.Dispose();
90c6eb 74         }
J 75         string emf { get; set; }
76         /// <summary>
77         /// 导出为 Emf 或 Wmf 文件
78         /// </summary>
79         /// <param name="filePath">文件路径</param>
80         /// <returns>是否成功</returns>
81         private bool Export(string filePath)
82         {
83            
d7a15c 84             filePath = @"D:\CompanyCode\DIXWeb\src\WindowsFormsApp1\imgs\imgup.jpg";
D 85             //filePath = @"C:\Users\Administrator\Desktop\Debug\1.jpg";
90c6eb 86             FileInfo fi = new FileInfo(filePath);
J 87             emf = Path.GetDirectoryName(filePath) + "\\1.emf";
88             fi.CopyTo(emf,true);
89             try
90             {
91                 Bitmap bmp = new Bitmap(2220, 2220);
92                 Graphics gs = Graphics.FromImage(bmp);
93                 Metafile mf = new Metafile(emf, gs.GetHdc());
94
95                 Graphics g = Graphics.FromImage(mf);
96                 //string sf = System.IO.Path.GetDirectoryName(filePath) + "1.wmf";
97                 //mf.Save(sf, ImageFormat.Wmf);
98                 Draw(g);
99                 g.Save();
100                 
101                 g.Dispose();
102                 mf.Dispose();
103
104                 return true;
105             }
106             catch(Exception er)
107             {
108                 MessageBox.Show(er.Message);
109                 return false;
110             }
111         }
112         /// <summary>
113         /// 绘制图形
114         /// </summary>
115         /// <param name="g">用于绘图的Graphics对象</param>
116         private void Draw(Graphics g)
117         {
118             HatchBrush hb = new HatchBrush(HatchStyle.LightUpwardDiagonal, Color.Black, Color.White);
119
120             g.FillEllipse(Brushes.Gray, 10f, 10f, 200, 200);
121             g.DrawEllipse(new Pen(Color.Black, 1f), 10f, 10f, 200, 200);
122
123             g.FillEllipse(hb, 30f, 95f, 30, 30);
124             g.DrawEllipse(new Pen(Color.Black, 1f), 30f, 95f, 30, 30);
125
126             g.FillEllipse(hb, 160f, 95f, 30, 30);
127             g.DrawEllipse(new Pen(Color.Black, 1f), 160f, 95f, 30, 30);
128
129             g.FillEllipse(hb, 95f, 30f, 30, 30);
130             g.DrawEllipse(new Pen(Color.Black, 1f), 95f, 30f, 30, 30);
131
132             g.FillEllipse(hb, 95f, 160f, 30, 30);
133             g.DrawEllipse(new Pen(Color.Black, 1f), 95f, 160f, 30, 30);
134
135             g.FillEllipse(Brushes.Blue, 60f, 60f, 100, 100);
136             g.DrawEllipse(new Pen(Color.Black, 1f), 60f, 60f, 100, 100);
137
138             g.FillEllipse(Brushes.BlanchedAlmond, 95f, 95f, 30, 30);
139             g.DrawEllipse(new Pen(Color.Black, 1f), 95f, 95f, 30, 30);
140
141             g.DrawRectangle(new Pen(System.Drawing.Brushes.Blue, 0.1f), 6, 6, 208, 208);
142
143             g.DrawLine(new Pen(Color.Black, 0.1f), 110f, 110f, 220f, 25f);
144             g.DrawString("剖面图", new Font("宋体", 9f), Brushes.Green, 220f, 20f);
145         }
146         private void button1_Click(object sender, EventArgs e)
147         {
d7a15c 148
D 149            string MotifName=txtFilePath.Text.ToString();
150             var db = SqlSugarConnection.SugarClient();
151             List<Toprie_Motif> ToprieMotifList = db.SqlQueryable<Toprie_Motif>("select * from  Toprie_Motif").Where(m => m.IsVal == 0).ToList();
152
153             if (ToprieMotifList.Where(m => m.MotifName == MotifName).ToList().Count() == 0)
154             {
155                 if (!string.IsNullOrEmpty(MotifName))
156                 {
157                     Toprie_Motif ToprieMotif = new Toprie_Motif();
158                     ToprieMotif.Code = SnowFlakeSingle.Instance.NextId().ToString();
159                     ToprieMotif.MotifName = MotifName;
160                     ToprieMotif.Address = "";
161                     ToprieMotif.Remark = "";
162                     ToprieMotif.IsVal = 0;
163                     ToprieMotif.CreateCode = "5105986045";
164                     ToprieMotif.CreateTime = DateTime.Now;
165                     InsertToprieMotif(ToprieMotif);
166                     MessageBox.Show("新增成功", "提示");
167
168                 }else {
169                     MessageBox.Show("请输入订阅主题", "提示");
170                 }
171             }
172             else{
173                 MessageBox.Show("当前主题已添加,无法重复添加", "提示");
174             }
90c6eb 175         }
60f582 176
J 177         private void button2_Click(object sender, EventArgs e)
178         {
179
180         }
4b8004 181          
J 182
183         /// <summary>
184         /// 发布消息成功后调用的事件
185         /// </summary>
186         /// <param name="sender"></param>
187         /// <param name="e"></param>
188         private void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
189         {
d7a15c 190             MessageBox.Show("发布消息成功。", "信息");
4b8004 191         }
J 192
193         /// <summary>
194         /// 订阅主题成功后调用的事件
195         /// </summary>
196         /// <param name="sender"></param>
197         /// <param name="e"></param>
198         private void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
199         {
d7a15c 200            
4b8004 201            
J 202         }
203
204         /// <summary>
205         /// 取消订阅主题成功后调用的事件
206         /// </summary>
207         /// <param name="sender"></param>
208         /// <param name="e"></param>
209         private void client_MqttMsgUnsubscribed(object sender, MqttMsgUnsubscribedEventArgs e)
210         {
d7a15c 211             MessageBox.Show("取消订阅主题成功。", "信息");
4b8004 212         }
J 213
214         /// <summary>
215         /// 当接收到订阅主题的消息时调用的事件
216         /// </summary>
217         /// <param name="sender"></param>
218         /// <param name="e"></param>
219         private void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
220         {
d7a15c 221             SnowFlakeSingle.WorkId =1;
D 222
223
224             try {
225                 this.Invoke(new Action(() =>
226                 {
227                     txt_SubscribeMessage.Text += DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss") + "  Received topic: " + e.Topic + ", message: " + Encoding.UTF8.GetString(e.Message) + "\n";
228                     string Topic = e.Topic;
229                     string Message = Encoding.UTF8.GetString(e.Message);
230                     bool DupFlag = e.DupFlag;
231                     byte QosLevel = e.QosLevel;
232                     bool Retain = e.Retain;
233
234                     lbMsg.Items.Insert(0, Encoding.UTF8.GetString(e.Message));
235                     lbMsg.Items.Insert(0, "订阅主题:"+ Topic+ "*********************************************   --" + DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss"));
236
237                     if (Message!= "getParam"){
238
239                         ToreieItem ToreieItem = JsonConvert.DeserializeObject<ToreieItem>(Encoding.UTF8.GetString(e.Message));
240
241                         List<Toprie> ToprieList = new List<Toprie>();
242
243                         if (ToreieItem.sensorDatas != null && ToreieItem.sensorDatas.Count() > 0)
244                         {
245
246                             foreach (var item in ToreieItem.sensorDatas)
247                             {
248
249                                 Toprie obj = new Toprie();
250                                 obj.Code = SnowFlakeSingle.Instance.NextId().ToString();
251                                 obj.motif = Topic;
252                                 obj.times = ToreieItem.times;
253                                 obj.flag = item.flag;
254                                 obj.CreateCode = "5105986045";
255                                 obj.CreateTime = DateTime.Now;
256
257                                 switch (item.flag)
258                                 {
259                                     case FlagHelp.flag1:
260                                     case FlagHelp.flag2:
261                                     case FlagHelp.flag3:
262                                     case FlagHelp.flag4:
263                                     case FlagHelp.flag5:
264                                     case FlagHelp.flag7:
265                                         obj.val = item.value;
266                                         break;
267                                     case FlagHelp.flag6:
268                                         obj.switcher = item.switcher;
269                                         break;
270
271                                     case FlagHelp.flag8:
272                                         obj.lat = item.lat;
273                                         obj.lng = item.lng;
274                                         break;
275                                     case FlagHelp.flagICCID:
276                                         obj.str = item.str;
277                                         break;
278                                     default:
279                                         continue;
280                                 }
281                                 ToprieList.Add(obj);
282
283                             }
284                         }
285
286                         InsertListToprieList(ToprieList);
287                         
288                     }
289
290                 }));
291
292             }
293             catch (Exception ex) {
294
295                 string str = ex.ToString();
296
297             }
4b8004 298         }
J 299
300         /// <summary>
301         /// 当连接断开时调用的事件
302         /// </summary>
303         /// <param name="sender"></param>
304         /// <param name="e"></param>
305         private void client_ConnectionClosed(object sender, EventArgs e)
306         {
d7a15c 307             MessageBox.Show("连接已断开。", "信息");
4b8004 308         }
J 309
310         /// <summary>
311         /// 连接
312         /// </summary>
313         /// <param name="sender"></param>
314         /// <param name="e"></param>
315         private void btn_Connect_Click(object sender, EventArgs e)
316         {
317             try
318             {
d7a15c 319                 string brokerAddress = MqttconfigHelp.BrokerAddress;
D 320                 int brokerPort = MqttconfigHelp.BrokerPort;
4b8004 321                 client = new MqttClient(brokerAddress, brokerPort, false, null, null, MqttSslProtocols.None, null);  //实例化mqtt客户端
812400 322
J 323                 string clientId =   Guid.NewGuid().ToString("N");  //生成32位全球唯一标识符
324                 client.Connect(clientId,"","");  //建立客户端连接
4b8004 325
J 326                 client.MqttMsgPublished += client_MqttMsgPublished;  //发布消息成功后调用
327                 client.MqttMsgSubscribed += client_MqttMsgSubscribed;  //订阅主题成功后调用
328                 client.MqttMsgUnsubscribed += client_MqttMsgUnsubscribed;  //取消订阅主题成功后调用
329                 client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;  //当接收到订阅主题的消息时调用
330                 client.ConnectionClosed += client_ConnectionClosed;  //连接断开后调用
e7761b 331                 MessageBox.Show("连接成功。", "信息");// 
4b8004 332             }
J 333             catch (Exception ex)
334             {
d7a15c 335                 MessageBox.Show("连接失败!\n\r" + ex.Message.ToString(), "警告");
4b8004 336             }
J 337         }
338
339         /// <summary>
340         /// 断开连接
341         /// </summary>
342         /// <param name="sender"></param>
343         /// <param name="e"></param>
344         private void btn_DisConnect_Click(object sender, EventArgs e)
345         {
346             client.Disconnect();
347         }
348
349         /// <summary>
350         /// 发布消息
351         /// </summary>
352         /// <param name="sender"></param>
353         /// <param name="e"></param>
354         private void btn_Publish_Click(object sender, EventArgs e)
355         {
d7a15c 356             //string publishTopic = txt_PublishTopic.Text.ToString();  //发布的主题
D 357             //string publishMessage = txt_PublishMessage.Text.ToString();  //发布的消息
358             //client.Publish(publishTopic, Encoding.UTF8.GetBytes(publishMessage));  //发布消息
4b8004 359         }
J 360
361         /// <summary>
362         /// 订阅消息
363         /// </summary>
364         /// <param name="sender"></param>
365         /// <param name="e"></param>
366         private void btn_Subscribe_Click(object sender, EventArgs e)
367         {
d7a15c 368            
D 369             try {
370                 
371                 client = new MqttClient(MqttconfigHelp.BrokerAddress,MqttconfigHelp.BrokerPort, false, null, null, MqttSslProtocols.None, null);  //实例化mqtt客户端
372
373                 string clientId = Guid.NewGuid().ToString("N");  //生成32位全球唯一标识符
374                 client.Connect(clientId, MqttconfigHelp.UserName, MqttconfigHelp.Password);  //建立客户端连接
375
376                 client.MqttMsgPublished += client_MqttMsgPublished;  //发布消息成功后调用
377                 client.MqttMsgSubscribed += client_MqttMsgSubscribed;  //订阅主题成功后调用
378                 client.MqttMsgUnsubscribed += client_MqttMsgUnsubscribed;  //取消订阅主题成功后调用
379                 client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;  //当接收到订阅主题的消息时调用
e7761b 380                 client.ConnectionClosed += client_ConnectionClosed;//连接断开后调用
d7a15c 381
D 382                 string[] subscribeTopic = Toprie_MotifList().Split(',');  //可以同时订阅多个主题的消息,各主题之间用逗号分隔
383                 byte[] qosLevels = new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE };
384
e7761b 385                 foreach (var item in subscribeTopic){
d7a15c 386                     string[] MotifNameList ={ item };
D 387                     client.Subscribe(MotifNameList, qosLevels);
388                 }
389                 //this.Opacity = 0;
390                 MessageBox.Show("订阅主题成功。", "信息");
391             }
392             catch (Exception ex){
e7761b 393                 MessageBox.Show(ex.Message.ToString(),"异常");
d7a15c 394             }
D 395            
4b8004 396         }
J 397
398         /// <summary>
399         /// 取消订阅消息
400         /// </summary>
401         /// <param name="sender"></param>
402         /// <param name="e"></param>
403         private void btn_UnSubscribe_Click(object sender, EventArgs e)
404         {
d7a15c 405             string[] unSubscribeTopic =null;
4b8004 406             client.Unsubscribe(unSubscribeTopic);
J 407         }
408
409         /// <summary>
410         /// 窗口关闭事件
411         /// </summary>
412         /// <param name="sender"></param>
413         /// <param name="e"></param>
414         private void Window_Closed(object sender, EventArgs e)
415         {
416             Environment.Exit(0);  //强制退出,即使有其他的线程没有结束,因为M2Mqtt调用事件都是在单独的线程中进行的
417         }
d7a15c 418
D 419         private void txtFilePath_TextChanged(object sender, EventArgs e)
420         {
421
422
423         }
424
425         private void Form1_Load(object sender, EventArgs e)
426         {
427             //this.Opacity = 0;
428             ////窗体最小化显示
429             //this.WindowState = FormWindowState.Minimized;
430             ////不显示在任务栏中
431             //this.ShowInTaskbar = false;
432
433         }
434
435
436         /// <summary>
437         /// 
438         /// </summary>
439         public void InsertToprie(Toprie Toprie)
440         {
441             try
442             {
443                 var db = SqlSugarConnection.SugarClient();
444                 int RowIndex = db.Insertable(Toprie).ExecuteReturnIdentity();
445             }
446             catch (Exception ex)
447             {
448
449             }
450         }
451
452
453         /// <summary>
454         /// 批量添加数据
455         /// </summary>
456         public void InsertListToprieList(List<Toprie> ToprieList)
457         {
458
459             try
460             {
461
462                 var db = SqlSugarConnection.SugarClient();
463                 int RowIndex = db.Insertable(ToprieList).ExecuteReturnIdentity();
464             }
465             catch (Exception ex)
466             {
467
468             }
469         }
470
471         /// <summary>
472         /// 获取所有订阅主题
473         /// </summary>
474         public string Toprie_MotifList(){
475
476             try {
477
478                 #region 初始化
479
480                 string str = "";
481
482                 #endregion
483                 
484                 #region 获取订阅主题
485                 var db = SqlSugarConnection.SugarClient();
486                 List<Toprie_Motif> ToprieMotifList = db.SqlQueryable<Toprie_Motif>("select * from  Toprie_Motif").Where(m => m.IsVal == 0).ToList();
487
488                 #endregion
489
490                 #region 
491
492                 for (int i=0; ToprieMotifList !=null && i < ToprieMotifList.Count();i++){
493
494                     if (i == ToprieMotifList.Count() - 1)
495                     {
496                         str += ToprieMotifList[i].MotifName;
497                     }
498                     else {
499                         str += ToprieMotifList[i].MotifName + ",";
500                     }
501                 }
502
503
504                 #endregion
505
506                 return str;
507
508
509             }
510             catch (Exception ex){
511
512                 return "";
513                 
514             }
515         }
516
517
518
519         /// <summary>
520         /// 添加订阅主题
521         /// </summary>
522         public void InsertToprieMotif(Toprie_Motif ToprieMotif)
523         {
524             try
525             {
526                 var db = SqlSugarConnection.SugarClient();
527                 int RowIndex = db.Insertable(ToprieMotif).ExecuteReturnIdentity();
528             }
529             catch (Exception ex)
530             {
531
532             }
533         }
534
535         private void txt_SubscribeMessage_TextChanged(object sender, EventArgs e)
536         {
537
538         }
539
540         private void txt_BrokerAddress_TextChanged(object sender, EventArgs e)
541         {
542
543         }
90c6eb 544     }
J 545 }