Example #1
0
        private async void CreateJson()
        {
            for (int i = 0; i < _mqttClients.Count; i++)
            {
                try
                {
                    string         cleintNum      = "MQTT" + Convert.ToString((int.Parse(tbClientID.Text) + i));
                    string         deviceNum      = Convert.ToString((long.Parse(tbDeviceID.Text) + i));
                    IMqttClient    _mqttClient    = _mqttClients[i];
                    MqttJsonObject mqttJsonObject = new MqttJsonObject();
                    mqttJsonObject.paras  = new List <MqttJsonPara>();
                    mqttJsonObject.device = new MqttJsonDevice()
                    {
                        hard_version = "1.0.0",
                        run_time     = "",
                        soft_version = "1.0.0",
                        status       = "normal",
                        uid          = deviceNum
                    };
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "current1",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            10,
                            random.Next(-10 - 100, 100),
                            random.Next(-10 - 100, 100)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "current2",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            11,
                            random.Next(-100, 100),
                            random.Next(-100, 100)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "current3",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            12,
                            random.Next(-100, 100),
                            random.Next(-100, 100)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "relevance1",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            10,
                            random.Next(-100, 100),
                            random.Next(-100, 100)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "pulse_width",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            11,
                            random.Next(-100, 100),
                            random.Next(-100, 100)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "pulse_width2",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            1,
                            11,
                            random.Next(-100, 100),
                            random.Next(-100, 100)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "switch1",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            10,
                            random.Next(0, 1),
                            random.Next(0, 1)
                        }
                    });
                    mqttJsonObject.paras.Add(new MqttJsonPara()
                    {
                        name     = "switch2",
                        datatype = "current",
                        iotype   = "analog",
                        version  = "1.0.0",
                        data     = new List <object>()
                        {
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                            0,
                            11,
                            random.Next(0, 1),
                            random.Next(0, 1)
                        }
                    });
                    string json = ScadaHexByteOperator.ObjectToJson(mqttJsonObject);
                    if (_mqttClient != null && _mqttClient.IsConnected)
                    {
                        await _mqttClient.PublishAsync(new MqttApplicationMessage()
                        {
                            Payload = Encoding.UTF8.GetBytes(json),
                            QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce,
                            Retain = true,
                            Topic  = "Topic/" + cleintNum + "/Read/" + deviceNum
                        });
                    }
                }
                catch
                { continue; }
                Thread.Sleep(500);
            }


            AddRichText("发布时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "发布一组数据成功\r\n");
        }
Example #2
0
        public override void SimulatorStart()
        {
            if (simulatorTimer == null)
            {
                simulatorTimer = new Timer(delegate
                {
                    Task.Run(() =>
                    {
                        for (int i = 0; i < this.IODevices.Count; i++)
                        {
                            try
                            {
                                ParaPack devicePack = new ParaPack(this.IODevices[i].IO_DEVICE_PARASTRING);
                                //构造Json模拟数据
                                MqttJsonObject mqttJsonObject = new MqttJsonObject();
                                mqttJsonObject.paras          = new List <MqttJsonPara>();
                                mqttJsonObject.device         = new MqttJsonDevice()
                                {
                                    hard_version = "1.0.0",
                                    run_time     = "",
                                    soft_version = "1.0.0",
                                    status       = "normal",
                                    uid          = devicePack.GetValue("设备识别号")
                                };
                                for (int p = 0; p < this.IODevices[i].IOParas.Count; p++)
                                {
                                    if (this.IODevices[i].IOParas[p].IO_POINTTYPE == "模拟量" || this.IODevices[i].IOParas[p].IO_POINTTYPE == "开关量")
                                    {
                                        int simulatorValue = 0;
                                        if (this.IODevices[i].IOParas[p].IO_MINVALUE != this.IODevices[i].IOParas[p].IO_MAXVALUE)
                                        {
                                            int min = -100;
                                            if (this.IODevices[i].IOParas[p].IO_MINVALUE != null && this.IODevices[i].IOParas[p].IO_MINVALUE != "")
                                            {
                                                min = int.Parse(this.IODevices[i].IOParas[p].IO_MINVALUE);
                                            }
                                            int max = 100;
                                            if (this.IODevices[i].IOParas[p].IO_MAXVALUE != null && this.IODevices[i].IOParas[p].IO_MAXVALUE != "")
                                            {
                                                min = int.Parse(this.IODevices[i].IOParas[p].IO_MAXVALUE);
                                            }
                                            simulatorValue = random.Next(min, max);
                                        }
                                        else
                                        {
                                            simulatorValue = random.Next(-100, 100);
                                        }
                                        ParaPack paraPack = new ParaPack(this.IODevices[i].IOParas[p].IO_PARASTRING);

                                        mqttJsonObject.paras.Add(new MqttJsonPara()
                                        {
                                            name     = paraPack.GetValue("JSON名称"),
                                            datatype = paraPack.GetValue("数据类型"),
                                            iotype   = "analog",
                                            version  = "1.0.0",
                                            data     = new List <object>()
                                            {
                                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                                paraPack.GetValue("版子地址"),
                                                paraPack.GetValue("端口号"),
                                                simulatorValue
                                            }
                                        });
                                    }
                                }
                                //
                                string json = ScadaHexByteOperator.ObjectToJson(mqttJsonObject);

                                if (mqttJsonObject != null)
                                {
                                    byte[] datas = ScadaHexByteOperator.StringToUTF8Byte(json);
                                    this.ReceiveData(this.IOServer, this.IOCommunication, this.IODevices[i], datas, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                                }
                                this.SimulatorAppendLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 时间初始化一条设备模拟数据!");
                            }
                            catch (Exception emx)
                            {
                                this.SimulatorAppendLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 接收数据错误! " + emx.Message);
                            }
                        }
                    });
                }, null, 1000, Internal * 1000);
            }
        }
Example #3
0
        private async void CreateJson()
        {
            try
            {
                string deviceUid = "0036001b3438511035303434";

                MqttJsonObject mqttJsonObject = new MqttJsonObject();
                mqttJsonObject.paras  = new List <MqttJsonPara>();
                mqttJsonObject.device = new MqttJsonDevice()
                {
                    hard_version = "1.0.0",
                    run_time     = "",
                    soft_version = "1.0.0",
                    status       = "normal",
                    uid          = deviceUid
                };
                for (int j = 0; j < 3; j++)
                {
                    ///写入电流
                    for (int i = 1; i <= 8; i++)
                    {
                        mqttJsonObject.paras.Add(new MqttJsonPara()
                        {
                            name     = "current_0_" + i,
                            datatype = "current",
                            iotype   = "analog",
                            version  = "1.0.0",
                            data     = new List <object>()
                            {
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                0,
                                10,
                                random.Next(-100, 100)
                            }
                        });
                    }

                    ///写入脉宽
                    for (int i = 1; i <= 8; i++)
                    {
                        mqttJsonObject.paras.Add(new MqttJsonPara()
                        {
                            name     = "pulse_width_0_" + i,
                            datatype = "pulse_width",
                            iotype   = "analog",
                            version  = "1.0.0",
                            data     = new List <object>()
                            {
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                0,
                                10,
                                random.Next(-100, 100)
                            }
                        });
                    }
                    ///写入开关量
                    for (int i = 1; i <= 8; i++)
                    {
                        mqttJsonObject.paras.Add(new MqttJsonPara()
                        {
                            name     = "switch_0_" + i,
                            datatype = "switch",
                            iotype   = "switch",
                            version  = "1.0.0",
                            data     = new List <object>()
                            {
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                0,
                                10,
                                random.Next(0, 1)
                            }
                        });
                    }
                    ///写入符合采集
                    for (int i = 1; i <= 8; i++)
                    {
                        mqttJsonObject.paras.Add(new MqttJsonPara()
                        {
                            name     = "relevance_0_" + i,
                            datatype = "relevance",
                            iotype   = "switch",
                            version  = "1.0.0",
                            data     = new List <object>()
                            {
                                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                0,
                                10,
                                random.Next(-100, 100),
                                random.Next(0, 100),
                                random.Next(0, 1)
                            }
                        });
                    }
                }
                string json = ScadaHexByteOperator.ObjectToJson(mqttJsonObject);
                if (_mqttClient != null && _mqttClient.IsConnected)
                {
                    await _mqttClient.PublishAsync(new MqttApplicationMessage()
                    {
                        Payload = Encoding.UTF8.GetBytes(json),
                        QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,
                        Retain = true,
                        Topic  = "/xc_cloud_master/cs/0036001b3438511035303434"
                    });
                }
            }
            catch
            { return; }


            AddRichText("发布时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "发布一组数据成功\r\n");
        }