Пример #1
0
        /// <summary>
        /// 解析
        /// </summary>
        /// <returns></returns>
        public static AnalysisAndUpload Parse(byte[] bs, String devEUI)
        {
            AnalysisAndUpload ret     = new AnalysisAndUpload();
            receivedata       rcvdata = new receivedata();
            string            dt      = DateTime.Now.ToString();

            try
            {
                //byte[] bt = FormatBigFlotBytes(bs, 0);
                string MsgTypeandSeqnum = BitConverter.ToString(bs, 0);
                string MsgType          = MsgTypeandSeqnum.Substring(0, 1);
                string Seqnum           = MsgTypeandSeqnum.Substring(1, 1);
                if (MsgType == Convert.ToString(0))
                {//蓝牙beacon扫描数据
                    mySqlConnection = new MySqlConnection(connectionString);
                    try
                    {
                        //Open DataBase
                        //打开数据库
                        //int SeqNum;
                        string beaconmsg;
                        mySqlConnection.Open();
                        beaconmsg = MsgTypeandSeqnum.Substring(3, 71);
                        //byte[] bh = FormatBigFlotBytes(bs, 4);
                        //SeqNum = BitConverter.ToInt32(bh, 0);

                        //byte[] bm = FormatBigFlotBytes1(bs, 8);
                        //beaconmsg = BitConverter.ToString(bm, 0);
                        rcvdata.devEUI    = devEUI;
                        rcvdata.MsgType   = MsgType;
                        rcvdata.BeaconNum = Convert.ToInt32(Seqnum);
                        rcvdata.BeaconMsg = beaconmsg;
                        rcvdata.Rcvtime   = dt;

                        bool update = AddBluetooth(rcvdata);
                        ret.Ret    = update;
                        ret.beacon = "蓝牙beacon数据";
                    }
                    catch (Exception ex)
                    {
                        //Can not Open DataBase
                        //打开不成功 则连接不成功
                        ret.Ret    = false;
                        ret.beacon = "蓝牙beacon数据";
                    }
                    finally
                    {
                        //Close DataBase
                        //关闭数据库连接
                        mySqlConnection.Close();
                    }
                }
                else if (MsgType == Convert.ToString(1))
                {//sos数据
                    mySqlConnection = new MySqlConnection(connectionString);
                    try
                    {
                        //Open DataBase
                        //打开数据库
                        //int SeqNum;
                        //string beaconmsg;
                        mySqlConnection.Open();

                        //byte[] bh = FormatBigFlotBytes(bs, 4);
                        //SeqNum = BitConverter.ToInt32(bh, 0);
                        rcvdata.devEUI  = devEUI;
                        rcvdata.MsgType = MsgType;
                        rcvdata.seqnum  = Convert.ToInt32(Seqnum);
                        rcvdata.Rcvtime = dt;

                        bool update = AddSos(rcvdata);
                        ret.Ret = update;
                        ret.sos = "sos数据";
                    }
                    catch (Exception ex)
                    {
                        //Can not Open DataBase
                        //打开不成功 则连接不成功
                        ret.Ret = false;
                        ret.sos = "sos数据";
                    }
                    finally
                    {
                        //Close DataBase
                        //关闭数据库连接
                        mySqlConnection.Close();
                    }
                }
                else if (MsgType == Convert.ToString(2))
                {//终端低电量数据
                    mySqlConnection = new MySqlConnection(connectionString);
                    try
                    {
                        //Open DataBase
                        //打开数据库
                        //int SeqNum;
                        //string beaconmsg;
                        mySqlConnection.Open();

                        //byte[] bh = FormatBigFlotBytes(bs, 4);
                        //SeqNum = BitConverter.ToInt32(bh, 0);
                        rcvdata.devEUI  = devEUI;
                        rcvdata.MsgType = MsgType;
                        rcvdata.seqnum  = Convert.ToInt32(Seqnum);
                        rcvdata.Rcvtime = dt;

                        bool update = AddBattery(rcvdata);
                        ret.Ret     = update;
                        ret.battery = "终端低电量数据";
                    }
                    catch (Exception ex)
                    {
                        //Can not Open DataBase
                        //打开不成功 则连接不成功
                        //mqttdata ret = new mqttdata();
                        ret.Ret     = false;
                        ret.battery = "终端低电量数据";
                    }
                    finally
                    {
                        //Close DataBase
                        //关闭数据库连接
                        mySqlConnection.Close();
                    }
                }
                //else if (MsgType == Convert.ToString(3))
                //{//GNSS数据
                //    mySqlConnection = new MySqlConnection(connectionString);
                //    try
                //    {
                //        //Open DataBase
                //        //打开数据库
                //        mySqlConnection.Open();


                //    }
                //    catch (Exception ex)
                //    {
                //        //Can not Open DataBase
                //        //打开不成功 则连接不成功


                //    }
                //    finally
                //    {
                //        //Close DataBase
                //        //关闭数据库连接
                //        mySqlConnection.Close();

                //    }


                //}
            }
            catch
            {
                //mqttdata ret = new mqttdata();
                ret.Ret = false;
            }

            return(ret);
        }
Пример #2
0
        /// <summary>
        /// 订阅消息到达事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            //处理接收到的消息
            string msg = System.Text.Encoding.Default.GetString(e.Message);
            string dt  = DateTime.Now.ToString();

            if (e.Topic.EndsWith("rx"))   // 上行数据消息接收
            {
                // 收到数据
                JObject message = JObject.Parse(msg);                // json 解析
                String  devEUI  = message.Value <String>("devEUI");  // 数据来自终端的devEUI
                String  payload = message.Value <String>("data");    // 数据部分,base64 编码
                byte[]  data    = Convert.FromBase64String(payload); // 原始数据字节数组

                // 按数据格式协议解析
                AnalysisAndUpload tah = AnalysisAndUpload.Parse(data, devEUI);
                if (tah.Ret == true)
                {
                    if (tah.beacon == "蓝牙beacon数据")
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            textBox1.Text = devEUI + "-" + tah.beacon + "上传数据成功!" + dt + "\r\n" + textBox1.Text;
                        }));
                    }
                    else if (tah.sos == "sos数据")
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            textBox1.Text = devEUI + "-" + tah.sos + "上传数据成功!" + dt + "\r\n" + textBox1.Text;
                        }));
                    }
                    else if (tah.battery == "终端低电量数据")
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            textBox1.Text = devEUI + "-" + tah.battery + "上传数据成功!" + dt + "\r\n" + textBox1.Text;
                        }));
                    }
                }
                else
                {
                    if (tah.beacon == "蓝牙beacon数据")
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            textBox1.Text = devEUI + "-" + tah.beacon + "上传数据失败!" + dt + "\r\n" + textBox1.Text;
                        }));
                    }
                    else if (tah.sos == "sos数据")
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            textBox1.Text = devEUI + "-" + tah.sos + "上传数据失败!" + dt + "\r\n" + textBox1.Text;
                        }));
                    }
                    else if (tah.battery == "终端低电量数据")
                    {
                        Invoke(new MethodInvoker(delegate
                        {
                            textBox1.Text = devEUI + "-" + tah.battery + "上传数据失败!" + dt + "\r\n" + textBox1.Text;
                        }));
                    }
                }

                //if (tah != null)
                //{
                //    Invoke(new MethodInvoker(delegate
                //        {

                //            lblWd.Text = tah.Temperature.ToString();
                //            lblSd.Text = DateTime.Now.ToString();

                //      //      string data1 = lblWd.Text.TrimEnd((char[])"/n/r".ToCharArray());


                //            FileStream fs = new FileStream(@"D:\test.txt", FileMode.Append, FileAccess.Write);
                //            StreamWriter sw = new StreamWriter(fs);
                //            sw.WriteLine("日期:" + lblSd.Text+"     data: "+ lblWd.Text );
                //            sw.Close();
                //            fs.Close();

                //        }));
                //    // 回复序数
                //    DownlinkAckToken();

                //}
            }
        }