private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBox.SelectedIndex) { case 0: Chart.BringToFront(); ComboBox.BringToFront(); break; case 1: ChartAll.Series[0].Points.Clear(); string sqlAll = string.Format("SELECT {0} FROM {1}_result WHERE nodeid = {2} ", Name + ",time", CurrentSensor.Model.Sname, CurrentSensor.NodeId); DataTable tableAll = null; try { tableAll = SqlLiteHelper.ExecuteReader(ConfigurationManager.AppSettings["dbPath"], sqlAll); foreach (DataRow row in tableAll.Rows) { int index = ChartAll.Series[0].Points.AddXY(row["time"].ToString(), row[Name]); ChartAll.Series[0].Points[index].ToolTip = string.Format("时间:{0} {1}:{2}{3}", row["time"], Alias, row[Name], Unit); } ChartAll.BringToFront(); ComboBox.BringToFront(); } catch (Exception ex) { MessageBox.Show("查询数据失败"); ComboBox.SelectedIndex = 0; } break; case 2: DateTimePickerForm dtpf = new DateTimePickerForm(); if (dtpf.ShowDialog() == DialogResult.OK) { ChartHis.Series[0].Points.Clear(); string sqlHis = string.Format("SELECT {0} FROM {1}_result WHERE nodeid = {2} and time < '{3}' and time >'{4}' ", Name + ",time", CurrentSensor.Model.Sname, CurrentSensor.NodeId, dtpf.dateTimePickerRetrieveEnd.Value.ToString("yyyy-MM-dd HH:mm"), dtpf.dateTimePickerRetrieveBegin.Value.ToString("yyyy-MM-dd HH:mm")); DataTable tableHis = null; try { tableHis = SqlLiteHelper.ExecuteReader(ConfigurationManager.AppSettings["dbPath"], sqlHis); foreach (DataRow row in tableHis.Rows) { int idx = ChartHis.Series[0].Points.AddXY(row["time"].ToString(), row[Name]); ChartHis.Series[0].Points[idx].ToolTip = string.Format("时间:{0} {1}:{2}{3}", row["time"], Alias, row[Name], Unit); } ChartHis.BringToFront(); ComboBox.BringToFront(); } catch (Exception ex) { MessageBox.Show("查询数据失败"); ComboBox.SelectedIndex = 0; } } else { ComboBox.SelectedIndex = 0; } break; } }
void InitAlarm() { //初始化策略 . 1.首先查询数据库,查询出所有的Strategy及其对应关系 //ConfigData.AllStrategy string stA = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'T_ALARM'"; string stM = "SELECT COUNT(*) FROM sqlite_master where type = 'table' and name = 'T_ALARM_SENSOR_MAP'"; object objA = SqlLiteHelper.ExecuteScalar(db, stA); if (Convert.ToInt32(objA) == 0) { int res = SqlLiteHelper.ExecuteNonQuery(db, createAlarmSql); } object objM = SqlLiteHelper.ExecuteScalar(db, stM); if (Convert.ToInt32(objM) == 0) { int res = SqlLiteHelper.ExecuteNonQuery(db, createMapSql); } DataTable dtA = SqlLiteHelper.ExecuteReader(db, selectAlarmSql); DataTable dtM = SqlLiteHelper.ExecuteReader(db, selectMapSql); //处理查询下来的数据表 foreach (DataRow r in dtA.Rows) { AlarmStrategy als; if (ConfigData.AllStrategy.Exists(ast => ast.Name == r["name"].ToString())) { als = ConfigData.AllStrategy.Find(ast => ast.Name == r["name"].ToString()); } else { als = new AlarmStrategy(); als.Name = r["name"].ToString(); ConfigData.AllStrategy.Add(als); } Alarm24 a24 = new Alarm24(); als.A24s.Add(a24); a24.Field = r["field"].ToString(); int warnTest = 0; if (int.TryParse(r["warn"].ToString(), out warnTest)) { a24.Warn = warnTest; } for (int i = 0; i < a24.Hs.Length; i++) { string ht = string.Format("h{0}t", i); string hl = string.Format("h{0}l", i); double tTest = 0; double lTest = 0; if (double.TryParse(r[ht].ToString(), out tTest)) { a24.Hs[i].Top = tTest; } if (double.TryParse(r[hl].ToString(), out lTest)) { a24.Hs[i].Low = lTest; } } } //处理AlarmMap foreach (DataRow r in dtM.Rows) { //aname varchar(50), date varchar(50), uid varchar(50), node varchar(50), port varchar(50) string aname = r["aname"].ToString(); string date = r["date"].ToString(); string uid = r["uid"].ToString(); string node = r["node"].ToString(); string port = r["port"].ToString(); Sensor sensor = ConfigData.allSensors.Find(s => s.Uid == uid && s.NodeId == node && s.PortId == port); AlarmStrategy ast = ConfigData.AllStrategy.Find(o => o.Name == aname); if (sensor != null && ast != null) { if (!sensor.AlarmDic.ContainsKey(date)) { sensor.AlarmDic.Add(date, ast); } } } }