Exemplo n.º 1
0
        public void NoLastestDataTester()
        {
            AggResult result = new AggResult(1, 1, "20150110", AggType.Day, 1);

            result.AggDatas = new List <AggData>();

            AggData data = new AggData();

            data.SensorId = 100;
            data.Values   = new List <double>();

            for (int i = 0; i < 3; i++)
            {
                data.Values.Add((i + 1) * 10);
            }
            result.AggDatas.Add(data);

            AggDataChangeCaculate caculate = new AggDataChangeCaculate();

            caculate.ProcessAggResult(ref result);

            Assert.IsNotNull(result.AggDataChanges);
            Assert.IsTrue(result.AggDataChanges.Count == result.AggDatas.Count);

            foreach (var aggdata in result.AggDataChanges)
            {
                foreach (var val in aggdata.Values)
                {
                    Assert.IsTrue(val == Convert.ToDouble(0));
                }
            }
        }
Exemplo n.º 2
0
        protected virtual List <AggData> GetLastAggData()
        {
            if (DbHelper == null)
            {
                return(null);
            }

            //if(!rwLocker.TryEnterReadLock(TimeOut))
            //    return null;
            //BaseAggConfig configTmp = ObjectHelper.DeepCopy(this.Config);
            //rwLocker.ExitReadLock();

            int dateTimeId = DbHelper.Accessor.GetLastestDateTimeId(
                config.StructId,
                config.FactorId,
                config.Type);

            if (dateTimeId == -1)
            {
                return(null);
            }

            List <int>     sensorIds = config.GetSensorIds();
            List <AggData> aggDatas  = new List <AggData>();

            foreach (int id in sensorIds)
            {
                AggData aggData = DbHelper.Accessor.GetLastestAggData(id, config.Type, dateTimeId);
                if (aggData != null)
                {
                    aggDatas.Add(aggData);
                }
            }
            return(aggDatas);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 产生聚集数据Sql
        /// </summary>
        /// <param name="structureId"></param>
        /// <param name="factorId"></param>
        /// <param name="dateTimeId"></param>
        /// <param name="type"></param>
        /// <param name="aggData"></param>
        /// <param name="aggDataChange"></param>
        /// <returns></returns>
        private string CreateAddOrUpdateAggDataSql(
            int structureId, int factorId, int dateTimeId, AggType type, AggData aggData, AggData aggDataChange, int configId)
        {
            string sql;

            string[] aggCol       = AggDataColNames.Split(',');
            string[] aggChangeCol = AggDataChangeColNames.Split(',');

            if (this.IsAggDataExist(dateTimeId, aggData.SensorId, type))
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(this.GetUpdateAggDataString(aggCol, aggData.Values));
                sb.Append(",");
                sb.Append(this.GetUpdateAggDataString(aggChangeCol, aggDataChange.Values));
                sb.Append(",");
                sb.Append(string.Format("[AggCofigId]={0}", configId));
                sql =
                    String.Format(
                        @"update [T_DATA_AGGREGATION] set {0} where [SensorId]={1} and [DateTimeId]={2} and [AggDataTypeId] = {3}",
                        sb,
                        aggData.SensorId,
                        dateTimeId,
                        Convert.ToInt32(type));
            }
            else
            {
                StringBuilder col = new StringBuilder();

                StringBuilder val = new StringBuilder();

                for (int i = 0; i < aggData.Values.Count; i++)
                {
                    col.AppendFormat("{0},{1}", aggCol[i], aggChangeCol[i]);
                    val.AppendFormat("{0},{1}", aggData.Values[i], aggDataChange.Values[i]);
                    if (i != aggData.Values.Count - 1)
                    {
                        col.Append(",");
                        val.Append(",");
                    }
                }

                sql =
                    String.Format(
                        @"insert into [T_DATA_AGGREGATION] ({0},[DateTimeId],[StructureId],[SafeFactorId],[SensorId],[AggDataTypeId],[AggCofigId]) values({1},{2},{3},{4},{5},{6},{7})",
                        col,
                        val,
                        dateTimeId,
                        structureId,
                        factorId,
                        aggData.SensorId,
                        Convert.ToInt32(type),
                        configId);
            }
            return(sql);
        }
Exemplo n.º 4
0
        public virtual AggResult AggProcess(AggRawData datas)
        {
            if (datas == null || datas.Datas.Count == 0 || datas.Datas[0].Values.Count == 0)
            {
                return(null);
            }

            AggResult result = new AggResult(datas.StructId, datas.FactorId, datas.TimeTag, datas.Type, datas.ConfigId);

            result.AggDatas = new List <AggData>();
            int ColNum = datas.Datas[0].Values[0].Count; // 监测数据项个数
            int id     = 0;

            List <double> temp = new List <double>();

            foreach (var aggRawData in datas.Datas)
            {
                if (aggRawData.Values == null)
                {
                    continue;
                }

                id = aggRawData.SensorId;
                AggData aggData = new AggData();
                aggData.SensorId = aggRawData.SensorId;
                for (int i = 0; i < ColNum; i++)
                {
                    if (temp.Count > 0)
                    {
                        temp.Clear();
                    }
                    foreach (var value in aggRawData.Values)
                    {
                        if (i < aggRawData.Values[i].Count)
                        {
                            temp.Add(value[i]);
                        }
                    }
                    if (temp.Count > 0)
                    {
                        aggData.Values.Add(this.GetAggValue(temp));
                    }
                }
                if (aggData.Values.Count > 0)
                {
                    result.AggDatas.Add(aggData);
                }
            }

            result.LastAggDatas = datas.LastAggDatas;
            return(result);
        }
Exemplo n.º 5
0
        public void MissSomeLastAggDataTester()
        {
            AggResult result = new AggResult(1, 1, "20150110", AggType.Day, 1);

            result.AggDatas     = new List <AggData>();
            result.LastAggDatas = new List <AggData>();
            AggData data     = new AggData();
            AggData lastData = new AggData();

            data.SensorId     = 100;
            data.Values       = new List <double>();
            lastData.SensorId = 100;
            lastData.Values   = new List <double>();

            for (int i = 0; i < 3; i++)
            {
                data.Values.Add((i + 1) * 10);
            }
            for (int i = 0; i < 2; i++)
            {
                lastData.Values.Add((i + 1) * 20);
            }
            result.AggDatas.Add(data);
            result.LastAggDatas.Add(lastData);

            AggDataChangeCaculate caculate = new AggDataChangeCaculate();

            caculate.ProcessAggResult(ref result);

            Assert.IsNotNull(result.AggDataChanges);
            Assert.IsTrue(result.AggDataChanges.Count == result.AggDatas.Count);

            // int num = 1;
            foreach (var aggdata in result.AggDataChanges)
            {
                for (int i = 0; i < aggdata.Values.Count; i++)
                {
                    if (i != aggdata.Values.Count - 1)
                    {
                        Assert.IsTrue(aggdata.Values[i] == Convert.ToDouble(-10 * (i + 1)));
                    }
                    else
                    {
                        Assert.IsTrue(aggdata.Values[i] == Convert.ToDouble(0));
                    }
                }
            }
        }
Exemplo n.º 6
0
        public bool ProcessAggResult(ref AggResult result)
        {
            if (result == null)
            {
                Log.Info("agg data change caculate failed, para is null!");
                return(false);
            }

            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, statrt AggDataChangeCaculate...", result.StructId, result.SafeFactorId, result.AggType);

            List <AggData> lastAggDatas = result.LastAggDatas;

            result.AggDataChanges = new List <AggData>();
            foreach (AggData aggData in result.AggDatas)
            {
                AggData lasAggRawData;
                AggData dataChange = new AggData();
                dataChange.SensorId = aggData.SensorId;
                ///存在上次聚集数据
                if (GetLastAggData(aggData.SensorId, lastAggDatas, out lasAggRawData))
                {
                    for (int i = 0; i < aggData.Values.Count; i++)
                    {
                        if (i < lasAggRawData.Values.Count)
                        {
                            dataChange.Values.Add(aggData.Values[i] - lasAggRawData.Values[i]);
                        }
                        else
                        {
                            dataChange.Values.Add(defalultValue);
                        }
                    }
                }
                else /// 无上次聚集数据
                {
                    for (int i = 0; i < aggData.Values.Count; i++)
                    {
                        dataChange.Values.Add(defalultValue);
                    }
                    Log.InfoFormat("sensorid:{0},timetag:{1},type:{2}, has no last agg data", aggData.SensorId, result.TimeTag, result.AggType);
                }
                result.AggDataChanges.Add(dataChange);
            }
            Log.InfoFormat("struct:{0},factorId:{1},type:{2}, end AggDataChangeCaculate...", result.StructId, result.SafeFactorId, result.AggType);
            return(true);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 获取最近一次聚集数据
        /// </summary>
        /// <param name="sensorId"></param>
        /// <param name="lastDatas"></param>
        /// <param name="lastAggData"></param>
        /// <returns></returns>
        private bool GetLastAggData(int sensorId, List <AggData> lastDatas, out AggData lastAggData)
        {
            lastAggData = null;
            if (lastDatas == null)
            {
                return(false);
            }

            try
            {
                lastAggData = (from data in lastDatas where data.SensorId == sensorId select data).ToList().First();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 获取最新的聚集数据
        /// </summary>
        /// <param name="sensorId">传感器Id</param>
        /// <param name="type">聚集类型</param>
        /// <param name="dateTimeId">日期Id</param>
        /// <returns></returns>
        public AggData GetLastestAggData(int sensorId, AggType type, int dateTimeId)
        {
            string sql =
                String.Format(
                    @"select {0} from {1} where [SensorId] = {2} and [AggDataTypeId] = {3} and [DateTimeId] = {4}",
                    AggDataColNames,
                    AggDataTableName,
                    sensorId,
                    Convert.ToInt32(type),
                    dateTimeId);

            try
            {
                DataSet ds = this.helper.Query(sql);
                if (ds == null || ds.Tables.Count != 1 || ds.Tables[0].Rows.Count != 1)
                {
                    return(null);
                }
                AggData  aggData  = new AggData();
                string[] colNames = AggDataColNames.Split(',');
                aggData.SensorId = sensorId;
                DataRow row = ds.Tables[0].Rows[0];
                foreach (var colName in colNames)
                {
                    if (row[colName].ToString() != string.Empty)
                    {
                        aggData.Values.Add(Convert.ToDouble(row[colName]));
                    }
                }
                return(aggData);
            }
            catch (Exception e)
            {
                Log.ErrorFormat(
                    "传感器{0},{1}的{2}聚集数据获取失败,sql:{3}, error:{4},trace{5}",
                    sensorId,
                    dateTimeId,
                    type,
                    sql,
                    e.Message,
                    e.StackTrace);
                return(null);
            }
        }
Exemplo n.º 9
0
        public static AggResult CreateData(string timeTag, AggType type)
        {
            AggResult result = new AggResult(90, 42, timeTag, type, 1);

            result.AggDatas     = new List <AggData>();
            result.LastAggDatas = new List <AggData>();
            AggData data     = new AggData();
            AggData lastData = new AggData();

            data.SensorId     = 1758;
            data.Values       = new List <double>();
            lastData.SensorId = 1758;
            lastData.Values   = new List <double>();

            for (int i = 0; i < 2; i++)
            {
                data.Values.Add((i + 1) * 10);
                lastData.Values.Add((i + 1) * 20);
            }
            result.AggDatas.Add(data);
            result.LastAggDatas.Add(lastData);
            return(result);
        }