public void testGetData2() { OpentsdbClient client = new OpentsdbClient(opentsdburl); try { Dictionary <string, string> tagMap = new Dictionary <string, string>(); tagMap.Add("chl", "hqdApp"); IDictionary <string, Dictionary <string, object> > tagsValuesMap = (IDictionary <string, Dictionary <string, object> >)client.getData("metric-t", tagMap, OpentsdbClient.AGGREGATOR_SUM, "1h", "2016-06-27 10:00:00", "2016-06-30 11:00:00", "yyyyMMdd hh"); foreach (var it in tagsValuesMap) { string tags = it.Key.ToString(); Console.WriteLine(">> tags: " + tags); IDictionary <string, Object> tvMap = tagsValuesMap[tags]; foreach (var it2 in tvMap) { string time = it2.Key.ToString(); Console.WriteLine(" >> " + time + " <-> " + tvMap[time]); } } } catch (Exception e) { Console.WriteLine(e.Message); } }
public void testPutData() { OpentsdbClient client = new OpentsdbClient(opentsdburl); try { Dictionary <string, string> tagMap = new Dictionary <string, string>(); tagMap.Add("chl", "hqdApp"); client.putData("metric-t", DateTimeUtil.parse("20160627 12:15", "yyyyMMdd HH:mm"), 210L, tagMap); client.putData("metric-t", DateTimeUtil.parse("20160627 12:17", "yyyyMMdd HH:mm"), 180L, tagMap); client.putData("metric-t", DateTimeUtil.parse("20160627 13:20", "yyyyMMdd HH:mm"), 180L, tagMap); } catch (Exception e) { Console.WriteLine(e.Message); } }
public static void PutDataToTSDB(RootObject dp) { OpentsdbClient client = new OpentsdbClient(opentsdburl); string datatime = DateTime.Now.ToString("yyyyMMdd HH:mm:ss"); var realDatatime = DateTimeUtil.parse(datatime, "yyyyMMdd HH:mm:ss"); try { Dictionary <string, string> tagMap = new Dictionary <string, string>(); tagMap.Add("deviceId", dp.dp_value.ID); if (dp.dp_value.power.Equals("0")) { client.putData("power", realDatatime, dp.dp_value.power, tagMap); } else { client.putData("fanspeed", realDatatime, dp.dp_value.fanspeed, tagMap); client.putData("fresh", realDatatime, dp.dp_value.fresh, tagMap); client.putData("heat", realDatatime, dp.dp_value.heat, tagMap); client.putData("power", realDatatime, dp.dp_value.power, tagMap); client.putData("sleep", realDatatime, dp.dp_value.sleep, tagMap); client.putData("auto", realDatatime, dp.dp_value.auto, tagMap); client.putData("childlock", realDatatime, dp.dp_value.childlock, tagMap); client.putData("lcd", realDatatime, dp.dp_value.lcd, tagMap); client.putData("tempin", realDatatime, dp.dp_value.tempin, tagMap); client.putData("tempout", realDatatime, dp.dp_value.tempout, tagMap); int s; int t; s = int.Parse(dp.dp_value.sur1); t = int.Parse(dp.dp_value.total1); client.putData("sur1", realDatatime, (s / t).ToString(), tagMap); s = int.Parse(dp.dp_value.sur2); t = int.Parse(dp.dp_value.total2); client.putData("sur1", realDatatime, (s / t).ToString(), tagMap); s = int.Parse(dp.dp_value.sur3); t = int.Parse(dp.dp_value.total3); client.putData("sur1", realDatatime, (s / t).ToString(), tagMap); } } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// 将实时数据记录到TSDB /// </summary> /// <param name="deviceInfo"></param> /// <param name="itemInfo"></param> private void RecordToTsdb(RetDeviceInfo deviceInfo, RetDeviceItemInfo itemInfo, string curValue, DateTime now) { decimal value; try { value = Convert.ToDecimal(curValue); // 如果不能转成数字,则不存储到TSDB中。 } catch (Exception e) { log.ErrorFormat("内部错误:{0},{1}", e.Message, e.StackTrace); return; } DeviceMonitoringApi deviceApi = new DeviceMonitoringApi(); IoTHubConfigurationModel parameter = new IoTHubConfigurationModel(); parameter.ID = long.Parse(deviceInfo.DataConnectID); var retDataConnect = deviceApi.GetDataConnect(parameter); if (retDataConnect.Code == -1) { log.Error("获取数据连接数据出错,ID:" + deviceInfo.DataConnectID); return; } RetDataConnectConfiguration dataConnect = retDataConnect.Data; Dictionary <string, string> tagMap = new Dictionary <string, string>(); foreach (var tag in deviceInfo.TagList) { tagMap.Add(tag.Key, tag.Value); } try { OpentsdbClient client = new OpentsdbClient("http://" + dataConnect.ServerAddress + ":" + dataConnect.ServerPort); client.putData(itemInfo.PropertyLabel, now, value, tagMap); } catch (Exception) { return; } }
public static List <string> readFileToList(string fileName) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); List <string> list = new List <string>(); StreamReader m_streamReader = new StreamReader(fs);//中文乱码加上System.Text.Encoding.Default,或则 System.Text.Encoding.GetEncoding("GB2312") //使用StreamReader类来读取文件 m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin); // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容 OpentsdbClient client = new OpentsdbClient(opentsdburl); string strLine = m_streamReader.ReadLine(); int i = 1; while (strLine != null) { //list.Add(strLine); Array temp = strLine.Split('\t'); Dictionary <string, string> tagMap = new Dictionary <string, string>(); //DateTime dt = DateTimeUtil.parse(temp.GetValue(1).ToString(), "yyyy/MM/dd HH:mm"); DateTime dt1 = Convert.ToDateTime(temp.GetValue(1)); tagMap.Add("room", "room" + temp.GetValue(0)); //nq_cu_heat nq_instant_energy nq_cu_flow nq_instant_flowrate nq_entrance_temp nq_exit_temp nq_diff_tmp bool bool1 = client.putData("nq_cu_heat", dt1, Convert.ToDouble(temp.GetValue(2)), tagMap); bool bool2 = client.putData("nq_instant_energy", dt1, Convert.ToDouble(temp.GetValue(3)), tagMap); bool bool3 = client.putData("nq_cu_flow", dt1, Convert.ToDouble(temp.GetValue(4)), tagMap); bool bool4 = client.putData("nq_instant_flowrate", dt1, Convert.ToDouble(temp.GetValue(5)), tagMap); bool bool5 = client.putData("nq_entrance_temp", dt1, Convert.ToDouble(temp.GetValue(6)), tagMap); bool bool6 = client.putData("nq_exit_temp", dt1, Convert.ToDouble(temp.GetValue(7)), tagMap); bool bool7 = client.putData("nq_diff_tmp", dt1, Convert.ToDouble(temp.GetValue(8)), tagMap); Console.WriteLine(i.ToString() + ":" + bool1 + " " + bool2 + " " + bool3 + " " + bool4 + " " + bool5 + " " + bool6 + " " + bool7); strLine = m_streamReader.ReadLine(); i++; } //关闭此StreamReader对象 m_streamReader.Close(); return(list); }
public static void testGetData() { OpentsdbClient client = new OpentsdbClient(opentsdburl); try { Dictionary <string, string> tagMap = new Dictionary <string, string>(); tagMap.Add("waterpump", "waterpump4"); string resContent = client.getData("current", tagMap, OpentsdbClient.AGGREGATOR_AVG, "1m", "2018-03-27 10:20:00", "2018-03-27 10:25:00"); Console.WriteLine(resContent); //log.info(">>>" + resContent); //Dictionary<string, string> tagMap = new Dictionary<string, string>(); //tagMap.Add("host", "szawd"); //OpenTSDB.Client.opentsdb.client.response.QueryLastResponse res = client.queryLastData("sys.batch.test6", tagMap, 500); //if (null != res) //{ // Console.WriteLine(res.value); //} } catch (Exception e) { Console.WriteLine(e.Message); } }
public static int nowInterval = 0; //目前已经来了几次 public static void PutAirData() { OpentsdbClient client = new OpentsdbClient(opentsdburl); string phone = "15064113079"; string password = "******"; try { //获取空气数据的列表 List <airData> airDataList = GetSensorDataByApiBLL.getAirData(phone, password); int numPutToTSDB = 0;//这一次for循环存的设备数 for (int i = 0; i < airDataList.Count; i++) { //以下if用于判断i设备的数据时间戳是否变化,没变化不再执行写入 if (lastTimeStamp[i] == null) { lastTimeStamp[i] = airDataList[i].data[0].timestamp; numPutToTSDB++; } else { if (lastTimeStamp[i].Equals(airDataList[i].data[0].timestamp)) { continue; } else { lastTimeStamp[i] = airDataList[i].data[0].timestamp; numPutToTSDB++; } } Dictionary <string, string> tagMap = new Dictionary <string, string>(); tagMap.Add("deviceId", airDataList[i].deviceId); string datatime = ConvertToTime(airDataList[i].data[0].timestamp).ToString("yyyyMMdd HH:mm:ss"); var realDatatime = DateTimeUtil.parse(datatime, "yyyyMMdd HH:mm:ss"); //public bool putData(string metric, DateTime timestamp, string value, Dictionary<string, string> tagMap) // AsyncHandleAlarmPolicies1(string deviceId, string air, string curValue, DateTime now) client.putData("pm2d5", realDatatime, airDataList[i].data[0].data.pm2d5, tagMap); client.putData("pm10", realDatatime, airDataList[i].data[0].data.pm10, tagMap); client.putData("temperature", realDatatime, airDataList[i].data[0].data.temperature, tagMap); client.putData("humidity", realDatatime, airDataList[i].data[0].data.humidity, tagMap); client.putData("pm1d0", realDatatime, airDataList[i].data[0].data.pm1d0, tagMap); client.putData("co2", realDatatime, airDataList[i].data[0].data.co2, tagMap); double air = double.Parse(airDataList[i].data[0].data.tvoc); air = air * 92.14 / 22400;//单位ppb转mg/m3的方法 client.putData("tvoc", realDatatime, air.ToString(), tagMap); client.putData("ch2o", realDatatime, airDataList[i].data[0].data.ch2o, tagMap); client.putData("rssi", realDatatime, airDataList[i].data[0].data.rssi, tagMap); if (nowInterval >= intervalToAlert) { alert al = new alert(); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "pm2d5", airDataList[i].data[0].data.pm2d5, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "pm10", airDataList[i].data[0].data.pm10, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "temperature", airDataList[i].data[0].data.temperature, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "humidity", airDataList[i].data[0].data.humidity, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "pm1d0", airDataList[i].data[0].data.pm1d0, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "co2", airDataList[i].data[0].data.co2, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "tvoc", air.ToString(), realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "ch2o", airDataList[i].data[0].data.ch2o, realDatatime); al.AsyncHandleAlarmPolicies1(airDataList[i].deviceId, "rssi", airDataList[i].data[0].data.rssi, realDatatime); } } log.InfoFormat("{0}devices put to TSDB this time!", numPutToTSDB); nowInterval++;//执行完一次for循环就算存了一次数据 if (nowInterval > intervalToAlert) { nowInterval = 0; } } catch (Exception e) { log.Info("采集程序出异常啦!" + e.Message); // storeairdata2opentsdb.getdataservicestart(); } }