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