Пример #1
0
        private void LoadBgkTemperatureConfig(SqlConnection connection)
        {
            string     sqlStatement = "select SensorId,Description from BgkTemperatureConfig";
            SqlCommand command      = new SqlCommand(sqlStatement, connection);

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

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

                    BgkTemperatureConfig ssv = new BgkTemperatureConfig(sensorId);
                    configCollection.Add(sensorId, ssv);

                    string[]     viewItem = { sensorId, sensorId, desc.ToString() };
                    ListViewItem listItem = new ListViewItem(viewItem);
                    this.listView1.Items.Add(listItem);
                }
                BgkTemperatureSolve actSolver = new BgkTemperatureSolve(configCollection, 300, redis, redisDbIndex, textBoxLog, dataQueue);
                solverCollection.Add(actSolver);
            }
        }
Пример #2
0
        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>();

            //string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            try
            {
                IDatabase db_raw    = this.redis.GetDatabase(0);
                IDatabase db_result = this.redis.GetDatabase(redisDbIndex);

                //RedisKey[] keys = list.Keys.Select(key => (RedisKey)key).ToArray();
                List <RedisKey> keyCollection = new List <RedisKey>();
                foreach (string k in list.Keys)
                {
                    keyCollection.Add(k);
                    //keyCollection.Add(k + "-005");
                    //keyCollection.Add(k + "-012");
                }

                RedisKey[] keys = keyCollection.ToArray();

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

                Dictionary <string, DataValue> dvs = new Dictionary <string, DataValue>();

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

                        string key = dv.SensorId;

                        double temp = ResistanceToTemperature(dv.Value2);

                        BgkTemperatureConfig ptv = list[key];

                        if (ptv.Stamp != dv.TimeStamp)
                        {
                            ptv.temperature = temp;
                            ptv.Stamp       = dv.TimeStamp;

                            DataValue strainDv = new DataValue();
                            strainDv.SensorId  = ptv.SensorId;
                            strainDv.TimeStamp = ptv.Stamp;
                            strainDv.ValueType = "005";
                            strainDv.Value     = ptv.temperature;
                            string tempKey = ptv.SensorId + "-005";
                            string result  = JsonConvert.SerializeObject(strainDv);
                            pair[tempKey] = result;

                            Temperature_Data sd = new Temperature_Data();
                            sd.SensorId    = ptv.SensorId;
                            sd.TimeStamp   = ptv.Stamp;
                            sd.Temperature = ptv.temperature;

                            string    mq_string = JsonConvert.SerializeObject(sd);
                            RabbitMsg msg       = new RabbitMsg();
                            msg.RouteKey = ptv.SensorId;
                            msg.Body     = mq_string;
                            dataQueue.Enqueue(msg);

                            str += ptv.SensorId + "\r\n";
                            str += " temperature: " + ptv.temperature + "\r\n";
                        }
                    }
                }

                if (pair.Count > 0)
                {
                    db_result.StringSet(pair.ToArray());
                    pair.Clear();
                }
                //lthis.AppendLog(str);
            }
            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();
                }
            }
        }