lixiaojun
2022-10-24 29b9d55e85a1c412d1367335caf3e046aef8afae
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
using IStation.Untity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DataDockingSocket
{
    /// <summary>
    /// 发送控制指令辅助类
    /// </summary>
    public class SendControlJobHelper 
    {
        private static RabbitMqExChangeHelper _queueHelper = null;
 
        /// <summary>
        /// 开始任务
        /// </summary>
        public static Task StartJob(Model.IMonitorDataDockingSession session)
        {
            //已经开启就关闭
            if (_queueHelper != null)
            {
                _queueHelper.Close();
                _queueHelper = null;
            }
 
            //会话结束后关闭
            session.SessionClosedEvent += () =>
            {
                if (_queueHelper != null)
                {
                    _queueHelper.Close();
                    _queueHelper = null;
                }
            };
 
            _queueHelper = new RabbitMqExChangeHelper();
            return Task.Run(() =>
            {
                _queueHelper.Receive<Model.MonitorTestControlParas>("NTTEST", (paras) =>
                {
                    try
                    {
                        if(paras==null)
                        {
                            LogHelper.Info("南通控制命令,数据序列化失败");
                            return true;
                        }
                        byte[] bts = null;
                        switch (paras.Type)
                        {
                            case 0:bts = null; break;//从辅助类中获取  关闭
                            case 1: bts = null; break;//从辅助类中获取 开启
                            case -1:bts = null;break;//从辅助类中获取 变频
                            default: break;
                        }
                        if (bts != null)
                        {
                            if (session != null)
                            {
                                if (session.IsConnected)
                                {
                                    session.Send(bts, 0, bts.Length);
                                    LogHelper.Info(session.SessionName + ":" + BitTransfer.ToString(bts) + ", 发送一条请求控制指令");
                                }
                            }
                        }
                        return true;
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error("南通控制命令,数据消息队列出错,自动跳过", ex);
                        return true;
                    }
                });
            });
 
        }
 
    }
}