Beispiel #1
0
        public string GetValue(DateTime dt)
        {
            string sql = string.Format("SELECT {0} FROM {1}_result WHERE nodeid = {2} and time < '{3}' order by time desc limit 1 ", Name + ",time",
                                       CurrentSensor.Model.Sname, CurrentSensor.NodeId,
                                       dt.ToString("yyyy-MM-dd HH:mm"));
            object o = null;

            try
            {
                o = SqlLiteHelper.ExecuteScalar(ConfigurationManager.AppSettings["dbPath"], sql);
            }
            catch (Exception ex)
            { }

            string result = o == null ? "" : o.ToString();

            if (!string.IsNullOrWhiteSpace(result))
            {
                try
                {
                    result = double.Parse(result).ToString("0.##");
                }
                catch (Exception ex)
                {
                }
            }
            return(result);
        }
Beispiel #2
0
        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);
                    }
                }
            }
        }