private void LoadSKD100Config(SqlConnection connection) { string sqlStatement = "select SensorId,InitVal,Description from SKD100Config"; SqlCommand command = new SqlCommand(sqlStatement, connection); using (SqlDataReader reader = command.ExecuteReader()) { Dictionary <string, SKD100Config> configCollection = new Dictionary <string, SKD100Config>(); while (reader.Read()) { string sensorId = reader.GetString(0); double init = reader.GetDouble(1); object desc = reader.GetValue(2); SKD100Config ssv = new SKD100Config(); ssv.SensorId = sensorId; ssv.InitVal = init; configCollection.Add(sensorId, ssv); string[] viewItem = { sensorId, sensorId, desc.ToString() }; ListViewItem listItem = new ListViewItem(viewItem); this.listView1.Items.Add(listItem); } SKD100Solve actSolver = new SKD100Solve(configCollection, 400, 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) { SKD100_Data dv = JsonConvert.DeserializeObject <SKD100_Data>((string)rv); string key = dv.SensorId; SKD100Config ptv = list[key]; if (ptv.TimeStamp != dv.TimeStamp) { ptv.IsUpdated = true; ptv.SensorId = dv.SensorId; ptv.TimeStamp = dv.TimeStamp; LaserRange_Data data = new LaserRange_Data(); data.SensorId = dv.SensorId; data.TimeStamp = dv.TimeStamp; data.Distance = dv.Distance; data.DeltaDistance = Math.Round(dv.Distance - ptv.InitVal, 3); 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 = ptv.SensorId + "-021"; DataValue temp = new DataValue(); temp.SensorId = ptv.SensorId; temp.TimeStamp = ptv.TimeStamp; temp.ValueType = "021"; temp.Value = dv.Distance; string result = JsonConvert.SerializeObject(temp); pair[redisKey] = result; redisKey = ptv.SensorId + "-028"; temp.ValueType = "028"; temp.Value = dv.Distance - ptv.InitVal; 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(); } } }