Пример #1
0
        /// <summary>
        /// 循环发送数据
        /// </summary>
        private static void WhilePublish()
        {
            DateTime tempTime = DateTime.Now;
            DateTime logTime  = DateTime.Now;

            while (true)
            {
                try
                {
                    string         strSql    = "select * from param_temp";
                    string         strSqlLog = string.Format("select * from log where logtime>'{0}'", logTime.ToString("yyyy/MM/dd HH:mm:ss"));
                    DataSet        dataSet   = _dbHelperMySQL.Quene(strSql);
                    DataSet        dsLog     = _dbHelperMySQL.Quene(strSqlLog);
                    Model.MQTTData mQTTData  = new Model.MQTTData();
                    mQTTData.repeaterID = PurVar.Repeater.repeaterId.ToString();
                    //添加数据信息
                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Count == 1)
                        {
                            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                            {
                                Model.ParamData paramData = new Model.ParamData();
                                paramData.currentValue = Convert.ToDouble(dataRow["currentvalue"]);
                                paramData.paramID      = Convert.ToInt64(dataRow["paramid"]);
                                paramData.saveTime     = Convert.ToDateTime(dataRow["savetime"]);
                                paramData.paramTemp    = Convert.ToInt32(dataRow["param_temp"]);
                                mQTTData.paramDatas.Add(paramData);
                                if (tempTime < Convert.ToDateTime(dataRow["savetime"]))
                                {
                                    tempTime = Convert.ToDateTime(dataRow["savetime"]);
                                }
                            }
                        }
                    }
                    //添加日志信息
                    if (dsLog != null)
                    {
                        if (dsLog.Tables.Count == 1)
                        {
                            foreach (DataRow drLog in dsLog.Tables[0].Rows)
                            {
                                Model.Log log = new Model.Log();
                                log.content = drLog["content"].ToString();
                                log.logTime = Convert.ToDateTime(drLog["logtime"]);
                                log.logType = drLog["logtype"].ToString();
                                if (logTime < Convert.ToDateTime(drLog["logtime"]))
                                {
                                    logTime = Convert.ToDateTime(drLog["logtime"]);
                                }
                                mQTTData.logs.Add(log);
                            }
                        }
                    }
                    string strJsonData = JsonConvert.SerializeObject(mQTTData);

                    PublishRepeater("service/" + PurVar.Repeater.repeaterId.ToString() + "/data", strJsonData);
                }
                catch (Exception em)
                { PurVar.PurStatic.WriteToStatus(em.Message); }
                Thread.Sleep(5000);
            }
        }
Пример #2
0
        /// <summary>
        /// 循环发送数据 和数据库联系 OK
        /// </summary>
        private static void WhilePublish()
        {
            DateTime tempTime = DateTime.Now;
            DateTime logTime  = DateTime.Now;

            while (true)

            { //读取temp数据库
                //局部变量,便于总体打包11111111111111采集数据表格
                Model.MQTTData mQTTData = new Model.MQTTData();
                try
                {
                    string  strSql    = "select * from param_temp";
                    string  strSqlLog = string.Format("select * from log where logtime>'{0}'", logTime.ToString("yyyy/MM/dd HH:mm:ss"));
                    DataSet dataSet   = _dbHelperMySQL.Quene(strSql);
                    DataSet dsLog     = _dbHelperMySQL.Quene(strSqlLog);
                    //  Model.MQTTData mQTTData = new Model.MQTTData();
                    mQTTData.repeaterID = PurVar.Repeater.repeaterId.ToString();
                    //添加数据信息
                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Count == 1)
                        {
                            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                            {
                                Model.ParamData paramData = new Model.ParamData();
                                paramData.currentValue = Convert.ToDouble(dataRow["currentvalue"]);
                                paramData.paramID      = Convert.ToInt64(dataRow["paramid"]);
                                paramData.paramName    = Convert.ToString(dataRow["paramName"]);
                                paramData.saveTime     = Convert.ToDateTime(dataRow["savetime"]);
                                paramData.paramTemp    = Convert.ToInt32(dataRow["param_temp"]);
                                mQTTData.paramDatas.Add(paramData);
                                if (tempTime < Convert.ToDateTime(dataRow["savetime"]))
                                {
                                    tempTime = Convert.ToDateTime(dataRow["savetime"]);
                                }
                            }
                        }
                    }
                    //只发本部分
                    string strJsonDatatemp = JsonConvert.SerializeObject(mQTTData.paramDatas);
                    PublishRepeater("service/" + PurVar.Repeater.repeaterId.ToString() + "/data", strJsonDatatemp);
                }
                catch (Exception em)
                { PurVar.PurStatic.WriteToStatus(em.Message); }
                Thread.Sleep(2000);


                //读取2222222222222222222222定时器表格
                try
                {
                    string  strSql    = "select * from param_timer";
                    string  strSqlLog = string.Format("select * from log where logtime>'{0}'", logTime.ToString("yyyy/MM/dd HH:mm:ss"));
                    DataSet dataSet   = _dbHelperMySQL.Quene(strSql);
                    DataSet dsLog     = _dbHelperMySQL.Quene(strSqlLog);
                    //   Model.MQTTData mQTTData = new Model.MQTTData();
                    mQTTData.repeaterID = PurVar.Repeater.repeaterId.ToString();
                    //添加数据信息
                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Count == 1)
                        {
                            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                            {
                                Model.ParamTimer paramTimer = new Model.ParamTimer();
                                paramTimer.StartTime = Convert.ToDouble(dataRow["StartTime"]);
                                paramTimer.StopTime  = Convert.ToDouble(dataRow["StopTime"]);
                                paramTimer.Enable    = Convert.ToInt32(dataRow["Enable"]);
                                paramTimer.TimerName = Convert.ToString(dataRow["TimerName"]);
                                paramTimer.SaveTime  = Convert.ToDateTime(dataRow["SaveTime"]);
                                paramTimer.GPIONum   = Convert.ToInt32(dataRow["GPIONum"]);
                                mQTTData.paramTimer.Add(paramTimer);
                                if (tempTime < Convert.ToDateTime(dataRow["SaveTime"]))
                                {
                                    tempTime = Convert.ToDateTime(dataRow["SaveTime"]);
                                }
                            }
                        }
                    }
                    string strJsonDataTimer = JsonConvert.SerializeObject(mQTTData.paramTimer);
                    PublishRepeater("service/" + PurVar.Repeater.repeaterId.ToString() + "/timer", strJsonDataTimer);
                }
                catch (Exception em)
                { PurVar.PurStatic.WriteToStatus(em.Message); }
                Thread.Sleep(2000);


                //读取GPIO数据库333333333333333333333端口表格
                try
                {
                    string  strSql    = "select * from param_GPIO";
                    string  strSqlLog = string.Format("select * from log where logtime>'{0}'", logTime.ToString("yyyy/MM/dd HH:mm:ss"));
                    DataSet dataSet   = _dbHelperMySQL.Quene(strSql);
                    DataSet dsLog     = _dbHelperMySQL.Quene(strSqlLog);
                    //    Model.MQTTData mQTTData = new Model.MQTTData();
                    mQTTData.repeaterID = PurVar.Repeater.repeaterId.ToString();
                    //添加数据信息
                    if (dataSet != null)
                    {
                        if (dataSet.Tables.Count == 1)
                        {
                            foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                            {
                                Model.ParamGPIO paramGPIO = new Model.ParamGPIO();
                                paramGPIO.StartTemp = Convert.ToDouble(dataRow["StartTemp"]);
                                paramGPIO.StopTemp  = Convert.ToDouble(dataRow["StopTemp"]);
                                paramGPIO.StartHum  = Convert.ToDouble(dataRow["StartHum"]);
                                paramGPIO.StopHum   = Convert.ToDouble(dataRow["StopHum"]);
                                paramGPIO.GPIOName  = Convert.ToString(dataRow["GPIOName"]);
                                paramGPIO.Enable    = Convert.ToInt32(dataRow["Enable"]);

                                paramGPIO.SaveTime     = Convert.ToDateTime(dataRow["SaveTime"]);
                                paramGPIO.GPIONum      = Convert.ToInt32(dataRow["GPIONum"]);
                                paramGPIO.GPIOTemp     = Convert.ToInt32(dataRow["GPIOTemp"]);
                                paramGPIO.DelayTime    = Convert.ToInt32(dataRow["DelayTime"]);
                                paramGPIO.ControlStyle = Convert.ToInt32(dataRow["ControlStyle"]);
                                paramGPIO.SetValue     = Convert.ToInt32(dataRow["SetValue"]);
                                mQTTData.paramGPIO.Add(paramGPIO);
                                if (tempTime < Convert.ToDateTime(dataRow["SaveTime"]))
                                {
                                    tempTime = Convert.ToDateTime(dataRow["SaveTime"]);
                                }
                            }
                        }
                    }
                    string strJsonDataGPIO = JsonConvert.SerializeObject(mQTTData.paramGPIO);
                    PublishRepeater("service/" + PurVar.Repeater.repeaterId.ToString() + "/GPIO", strJsonDataGPIO);
                }
                catch (Exception em)
                { PurVar.PurStatic.WriteToStatus(em.Message); }
                Thread.Sleep(2000);

                string strJsonData = JsonConvert.SerializeObject(mQTTData);
                /////////////////////4444444444444打包所有的表格
                PublishRepeater("service/" + PurVar.Repeater.repeaterId.ToString() + "/ALL", strJsonData);
            }
        }