Exemple #1
0
        private void LoadTemperatureHumidityConfig(SqlConnection connection)
        {
            string     sqlStatement = "select SensorId,Description from TemperatureHumidityConfig";
            SqlCommand command      = new SqlCommand(sqlStatement, connection);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                Dictionary <string, TemperatureHumidityConfig> configCollection = new Dictionary <string, TemperatureHumidityConfig>();

                while (reader.Read())
                {
                    string sensorId = reader.GetString(0);
                    object desc     = reader.GetValue(1);

                    TemperatureHumidityConfig ssv = new TemperatureHumidityConfig();
                    ssv.SensorId = sensorId;
                    configCollection.Add(sensorId, ssv);

                    string[]     viewItem = { sensorId, sensorId, desc.ToString() };
                    ListViewItem listItem = new ListViewItem(viewItem);
                    this.listView1.Items.Add(listItem);
                }
                TemperatureHumiditySolve actSolver = new TemperatureHumiditySolve(configCollection, 300, redis, redisDbIndex, textBoxLog, dataQueue);
                solverCollection.Add(actSolver);
            }
        }
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bgWorker = sender as BackgroundWorker;
            string           stamp    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string           str      = "";

            str += stamp + " ";
            if (redis.IsConnected)
            {
            }
            else
            {
                str += "redis server is not connected";
                //lthis.AppendLog(str);
                return;
            }
            Dictionary <RedisKey, RedisValue> pair = new Dictionary <RedisKey, RedisValue>();

            try
            {
                IDatabase db_raw    = this.redis.GetDatabase(0);
                IDatabase db_result = this.redis.GetDatabase(redisDbIndex);

                List <RedisKey> keyCollection = new List <RedisKey>();
                foreach (string k in list.Keys)
                {
                    keyCollection.Add(k);
                }

                RedisKey[] keys = keyCollection.ToArray();

                RedisValue[] vals = db_raw.StringGet(keys);

                foreach (RedisValue rv in vals)
                {
                    if (!rv.IsNull)
                    {
                        TemperatureHumidity_Data data = JsonConvert.DeserializeObject <TemperatureHumidity_Data>((string)rv);

                        string key = data.SensorId;

                        TemperatureHumidityConfig ptv = list[key];

                        if (ptv.Stamp != data.TimeStamp)
                        {
                            ptv.Stamp = data.TimeStamp;

                            string mq_string = JsonConvert.SerializeObject(data);
                            //mq_string to mq
                            RabbitMsg msg = new RabbitMsg();
                            msg.RouteKey = ptv.SensorId;
                            msg.Body     = mq_string;
                            dataQueue.Enqueue(msg);

                            string    redisKey = data.SensorId + "-005";
                            DataValue temp     = new DataValue();
                            temp.SensorId  = data.SensorId;
                            temp.TimeStamp = data.TimeStamp;
                            temp.ValueType = "005";
                            temp.Value     = data.Temperature;
                            string result = JsonConvert.SerializeObject(temp);
                            pair[redisKey] = result;

                            redisKey       = data.SensorId + "-006";
                            temp           = new DataValue();
                            temp.SensorId  = data.SensorId;
                            temp.TimeStamp = data.TimeStamp;
                            temp.ValueType = "006";
                            temp.Value     = data.Humidity;
                            result         = JsonConvert.SerializeObject(temp);
                            pair[redisKey] = result;
                        }
                    }
                }

                if (pair.Count > 0)
                {
                    db_result.StringSet(pair.ToArray());
                    pair.Clear();
                }
            }
            catch (Exception ex)
            {
                this.AppendLog(ex.Message);
                using (StreamWriter sw = new StreamWriter(@"ErrLog.txt", true))
                {
                    sw.WriteLine(stamp + " " + ex.Message + " \r\n" + ex.StackTrace.ToString());
                    sw.WriteLine("---------------------------------------------------------");
                    sw.Close();
                }
            }
        }