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(); } } }
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) { ACT4238_Data dv = JsonConvert.DeserializeObject <ACT4238_Data>((string)rv); string key = dv.SensorId; ACT4238StrainConfig ptv = list[key]; if (ptv.Stamp != dv.TimeStamp) { ptv.isUpdated = true; ptv.frequency = dv.Frequency; ptv.temperature = dv.Temperature; ptv.Stamp = dv.TimeStamp; } } } foreach (string key in list.Keys) { //string str = ""; ACT4238StrainConfig ptv = list[key]; if (ptv.isUpdated) { if (ptv.temperature == 0 || ptv.frequency == 0) { this.AppendLog(stamp + " " + ptv.SensorId + "This Channel is broken"); continue; } string strainNormalKey = ptv.SensorId + "-009"; double strain = CalculateStrain(ptv); DataValue strainDv = new DataValue(); strainDv.SensorId = ptv.SensorId; strainDv.TimeStamp = ptv.Stamp; strainDv.ValueType = "009"; strainDv.Value = strain; string result = JsonConvert.SerializeObject(strainDv); pair[strainNormalKey] = result; strainDv.ValueType = "005"; strainDv.Value = ptv.temperature; string tempKey = ptv.SensorId + "-005"; result = JsonConvert.SerializeObject(strainDv); pair[tempKey] = result; Strain_Data sd = new Strain_Data(); sd.SensorId = ptv.SensorId; sd.TimeStamp = ptv.Stamp; sd.Frequency = ptv.frequency; sd.Strain = strain; 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); ptv.isUpdated = false; str += ptv.SensorId + "\r\n"; str += "R0: " + ptv.R0.ToString() + " T0: " + ptv.T0 + " G: " + ptv.G + " K: " + ptv.K + "\r\n"; str += "frequency: " + ptv.frequency + " temperature: " + ptv.temperature + "\r\n"; str += "Strain: " + strain.ToString() + "\r\n"; ////lthis.AppendLog(str); if (bgWorker.CancellationPending == true) { if (pair.Count > 0) { db_result.StringSet(pair.ToArray()); pair.Clear(); } e.Cancel = true; break; } //str += temp.SensorId + " " + temp.TimeStamp + " " + temp.ValueType + " " + temp.Value + "\r\n"; //Thread.Sleep(500); } else { //this.AppendLog(stamp + key + "is not updated"); continue; } } 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(); } } }
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); //keyCollection.Add(k + "-020"); } RedisKey[] keys = keyCollection.ToArray(); RedisValue[] vals = db_raw.StringGet(keys); foreach (RedisValue rv in vals) { if (!rv.IsNull) { BGK3475DM_Data dv = JsonConvert.DeserializeObject <BGK3475DM_Data>((string)rv); string inageKey = dv.SensorId; SettlementConfig ptv = list[inageKey]; if (ptv.Stamp != dv.TimeStamp) { ptv.IsUpdated = true; ptv.Value = dv.Innage; ptv.Stamp = dv.TimeStamp; } } } foreach (string key in list.Keys) { SettlementConfig ptv = list[key]; if (ptv.IsUpdated == false) { //this.AppendLog(stamp + " " + key + "is not updated"); continue; } str += "InitValue: " + ptv.InitValue.ToString() + "\r\n"; str += ptv.SensorId + " " + ptv.Stamp + " " + "020" + " " + ptv.Value + "\r\n"; string offsetKey = ptv.SensorId + "-007"; double offset = Math.Round(ptv.Value - ptv.InitValue, 3); DataValue temp = new DataValue(); temp.SensorId = ptv.SensorId; temp.TimeStamp = ptv.Stamp; temp.ValueType = "007"; temp.Value = offset; string result = JsonConvert.SerializeObject(temp); pair[offsetKey] = result; temp.ValueType = "020"; temp.Value = ptv.Value; string innageKey = ptv.SensorId + "-020"; result = JsonConvert.SerializeObject(temp); pair[innageKey] = result; Settlement_Data sd = new Settlement_Data(); sd.SensorId = ptv.SensorId; sd.TimeStamp = ptv.Stamp; sd.Innage = ptv.Value; sd.DeltaInnage = offset; string mq_string = ""; //db.StringSet(offsetKey, result); str += temp.SensorId + " " + temp.TimeStamp + " " + temp.ValueType + " " + temp.Value + "\r\n"; if (ptv.RefPoint == "null") { mq_string = JsonConvert.SerializeObject(sd); //send mq_string RabbitMsg rmsg = new RabbitMsg(); rmsg.RouteKey = ptv.SensorId; rmsg.Body = mq_string; dataQueue.Enqueue(rmsg); //pair[ptv.SensorId] = mq_string; //lthis.AppendLog(str); continue; } string deflectionKey = ptv.SensorId + "-010"; string refKey = ptv.RefPoint.Split('-')[0]; SettlementConfig refPoint = list[refKey]; if (!refPoint.IsUpdated) { mq_string = JsonConvert.SerializeObject(sd); //send mq_string RabbitMsg rmsg = new RabbitMsg(); rmsg.RouteKey = ptv.SensorId; rmsg.Body = mq_string; dataQueue.Enqueue(rmsg); //pair[ptv.SensorId] = mq_string; //this.AppendLog(stamp + " " + ptv.RefPoint + "ref point is not updated"); continue; } double refOffset = Math.Round(refPoint.Value - refPoint.InitValue, 3); double deflection = Math.Round(offset - refOffset, 3); //str += temp.SensorId + " " + temp.TimeStamp + " " + temp.ValueType + " " + temp.Value + "\r\n"; temp.ValueType = "010"; temp.Value = deflection; result = JsonConvert.SerializeObject(temp); pair[deflectionKey] = result; sd.Deflection = deflection; mq_string = JsonConvert.SerializeObject(sd); //send mq_string RabbitMsg msg = new RabbitMsg(); msg.RouteKey = ptv.SensorId; msg.Body = mq_string; dataQueue.Enqueue(msg); //pair[ptv.SensorId] = mq_string; if (bgWorker.CancellationPending == true) { if (pair.Count > 0) { db_result.StringSet(pair.ToArray()); pair.Clear(); } e.Cancel = true; break; } str += temp.SensorId + " " + temp.TimeStamp + " " + temp.ValueType + " " + temp.Value + "\r\n"; //lthis.AppendLog(str); } if (pair.Count > 0) { db_result.StringSet(pair.ToArray()); pair.Clear(); } foreach (string key in list.Keys) { SettlementConfig ptv = list[key]; ptv.IsUpdated = false; } } 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(); } } }
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(); } } }