예제 #1
0
        private void MqttClient_RcvDataEvent(byte[] data)
        {
            try
            {
                switch (data[0])
                {
                case 0x00:
                    byte[]  Devid  = data.SubArray(1, 16);
                    byte[]  Commid = data.SubArray(17, 4);
                    string  pwd    = data.SubArray(22, data[21]).ToASCIIString();
                    MClient mc     = Clients.GetOrAdd(Devid.ToHexString(), (k) =>
                    {
                        MClient m     = new MClient();
                        m.Devid       = Devid;
                        byte[] commid = Utils.RandomBytes(4);
                        if (Commids.ContainsKey(commid.ToHexString()))
                        {
                            commid = Utils.RandomBytes(4);
                        }
                        Commids.TryAdd(commid.ToHexString(), Devid.ToHexString());
                        m.Commid = commid;
                        if (string.IsNullOrEmpty(pwd))
                        {
                            pwd = Utils.RandomPassword();
                        }
                        m.Password = pwd;
                        return(m);
                    });
                    mc.LastRcvTime = DateTime.Now;
                    if (!string.IsNullOrEmpty(pwd) && pwd != mc.Password)
                    {
                        mc.Password = pwd;
                    }
                    MqttClient.Request(Devid.ToHexString(), Package_0x00(mc.Commid, mc.Password));
                    break;

                case 0x01:
                    string commidx = data.SubArray(5, 4).ToHexString();
                    if (Commids.TryGetValue(commidx, out string dev) && Clients.TryGetValue(dev, out MClient mx) && (DateTime.Now - mx.LastRcvTime).TotalSeconds < 5)
                    {
                        MqttClient.Request(data.SubArray(1, 4).GetInt().ToString(), Package_0x01(0));
                    }
                    else
                    {
                        MqttClient.Request(data.SubArray(1, 4).GetInt().ToString(), Package_0x01(1));
                    }
                    break;
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message + "******" + ex.StackTrace); }
        }
예제 #2
0
        public void Start()
        {
            IsRunning = true;
            Console.WriteLine("*************服务器开始启动");
            try
            {
                X509Certificate CaCert = X509Certificate.CreateFromCertFile(CaCertPath); //注意证书文件不存在会导致相关Man初始化失败
                MqttClient = new MQTTMan(ClientEui.ToHexString(), ServerEui, ServerIP, int.Parse(ServerPort), CaCert, MqttSslProtocols.TLSv1_2);
                MqttClient.Start();
                MqttClient.RcvDataEvent += MqttClient_RcvDataEvent;

                Task.Run(() =>
                {
                    while (IsRunning)
                    {
                        try
                        {
                            MqttClient?.Request(Package_0x00());
                            Thread.Sleep(1000);
                        }
                        catch { }
                    }
                });
                Task.Run(() =>
                {
                    while (IsRunning)
                    {
                        try
                        {
                            if (Status == 1 && IsSession)
                            {
                                MqttClient?.Request(OtherCommidStr, Package_0x02(GetImage()));
                            }
                            Thread.Sleep(100);
                        }
                        catch { }
                    }
                });
            }
            catch (Exception ex) { Console.WriteLine(ex + " ca:" + CaCertPath); }
            Console.WriteLine("*************服务器启动完成");
        }