Esempio n. 1
0
        private void CollectAndSaveSnesors()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start(); //  开始监视代码运行时间

            Dictionary <int, Station> dicStations = new Dictionary <int, Station>();

            #region 查询sensor表
            string    sqlSensors = @"select * from( select t.ID stationID, t.GUID as stationCode,t.Name stationName,t1.ID sensorID,t1.Name sensorName,t1.PointAddressID 
                                                 from SCADA_Station t ,SCADA_Sensor t1 where t.ID=t1.StationID and t.ReadMode='WEB-YL') x
                                                 left join PointAddressEntry x1 on x.PointAddressID=x1.ID;";
            DataTable dtSensors  = DBUtil.ExecuteDataTable(sqlSensors, out string errMsg);
            if (!string.IsNullOrWhiteSpace(errMsg))
            {
                TraceManagerForWeb.AppendErrMsg("查询sensor列表失败:" + errMsg);
                return;
            }
            foreach (DataRow dr in dtSensors.Rows)
            {
                Station station = new Station()
                {
                    _ID     = DataUtil.ToInt(dr["stationID"]),
                    _GUID   = DataUtil.ToString(dr["stationCode"]),
                    _Name   = DataUtil.ToString(dr["stationName"]),
                    sensors = new List <Sensor>()
                };
                Sensor sensor = new Sensor()
                {
                    sensorID          = DataUtil.ToString(dr["sensorID"]),
                    sensorName        = DataUtil.ToString(dr["sensorName"]),
                    _PointAddressID   = DataUtil.ToInt(dr["PointAddressID"]),
                    versionID         = DataUtil.ToInt(dr["版本ID"]),
                    dataSourceAddress = DataUtil.ToString(dr["数据源地址"]).Trim(),
                    type     = Point.ToType(DataUtil.ToString(dr["数据类型"])),
                    isActive = DataUtil.ToInt(dr["是否激活"]),
                    scale    = DataUtil.ToDouble(dr["倍率"])
                };

                if (dicStations.Keys.Contains(station._ID))
                {
                    dicStations[station._ID].sensors.Add(sensor);
                }
                else
                {
                    station.sensors.Add(sensor);
                    dicStations.Add(station._ID, station);
                }
            }
            if (dicStations.Keys.Count == 0)
            {
                TraceManagerForWeb.AppendWarning("站点表没有读取模式:WEB-YL的站点表");
                return;
            }
            #endregion

            #region 是否需求请求新token
            if (tokenCache == null || DateTime.Compare(DateTime.Now, DataUtil.ToDateTime(tokenCache.data.expireTime)) > 0)
            {
                tokenCache = GetToken();
            }
            if (tokenCache == null)
            {
                return;
            }
            if (DateTime.Compare(DateTime.Now, DataUtil.ToDateTime(tokenCache.data.expireTime)) > 0)
            {
                TraceManagerForWeb.AppendErrMsg("获取熊猫标记参数为过期标记");
                return;// 获取后还是过期秘钥
            }
            #endregion

            #region 请求监测点数据并存入
            List <YaLiDian> yaLiDians = GetWebYaLiData();
            Collect(yaLiDians, ref dicStations);
            string saveSQL = GetSaveSensorsSQL(dicStations);
            if (string.IsNullOrWhiteSpace(saveSQL))
            {
                TraceManagerForWeb.AppendWarning(string.Format(@"采集WEB-监测点数量{0}获取存入数据库SQL失败,可能原因没有在线的站点", dicStations.Keys.Count));
                return;
            }
            DBUtil.ExecuteNonQuery(saveSQL, out string err);
            stopwatch.Stop();                                   //  停止监视
            TimeSpan timespan     = stopwatch.Elapsed;          //  获取当前实例测量得出的总时间
            double   milliseconds = timespan.TotalMilliseconds; //  总毫秒数

            if (!string.IsNullOrWhiteSpace(err))
            {
                TraceManagerForWeb.AppendErrMsg("更新WEB-监测点实时数据失败" + ",耗时:" + milliseconds.ToString() + "毫秒," + err);
            }
            else
            {
                TraceManagerForWeb.AppendDebug("更新WEB-监测点实时数据成功" + ",耗时:" + milliseconds.ToString() + "毫秒");
            }
            #endregion
        }
Esempio n. 2
0
        private void Collect(List <YaLiDian> yaLiDians, ref Dictionary <int, Station> dicStations)
        {
            if (yaLiDians == null)
            {
                return;
            }
            if (dicStations == null)
            {
                return;
            }

            foreach (int key in dicStations.Keys)
            {
                Station station    = dicStations[key];
                int     errorTimes = 0; // 三个离线就认为其离线了
                foreach (Sensor sensor in dicStations[key].sensors)
                {
                    // 防止采集的点多了,错误消息炸了,每个都报出来了---直接让其离线
                    if (errorTimes >= 3)
                    {
                        TraceManagerForWeb.AppendErrMsg("StationName:" + station._Name + "三个条目采集失败,已跳过该站点采集,请检查点表和数据源");
                        dicStations[key].IsOnline = false;
                        break;
                    }

                    // 检查未通过
                    if (!sensor.CheckScadaWeb(out string err))
                    {
                        sensor.MakeFail(sensor.sensorName + err);
                        TraceManagerForWeb.AppendErrMsg("StationName:" + station._Name + "SensorName" + sensor.sensorName + " " + err);
                        errorTimes++;
                        continue;
                    }

                    // 拿到数据源
                    YaLiDian[] curYaLiDians = yaLiDians.Where(y => y.ID.ToUpper() == station._GUID.ToUpper()).ToArray();// 注意转换大写在比较
                    if (curYaLiDians.Length == 0)
                    {
                        sensor.MakeFail("未在WEB监测点数据源中找到配置站点信息,站点编码:" + station._GUID);
                        TraceManagerForWeb.AppendErrMsg("未在WEB监测点数据源中找到配置站点信息,站点编码:" + station._GUID);
                        errorTimes++;
                        continue;
                    }
                    object pointDataSource;
                    string tempTime     = DataUtil.ToDateString(DateTime.Now);
                    bool   tempTimeFlag = false;
                    try
                    {
                        YaLiDian curYaLiDian = curYaLiDians[0];
                        // 获取在线状态-防止sensor表没有配置在线状态
                        Type         typeOnLine         = curYaLiDian.GetType();             //获取类型
                        PropertyInfo propertyInfoOnLine = typeOnLine.GetProperty("FOnLine"); //获取采集时间的属性
                        object       curOnLine          = propertyInfoOnLine.GetValue(curYaLiDian, null);
                        if (curOnLine != null && DataUtil.ToInt(curOnLine) == 1)
                        {
                            dicStations[key].IsOnline = true;
                        }

                        // 先拿到时间
                        Type typeTempTime = curYaLiDian.GetType();                                                  //获取类型
                        System.Reflection.PropertyInfo propertyInfoTempTime = typeTempTime.GetProperty("TempTime"); //获取采集时间的属性
                        object curTempTime = propertyInfoTempTime.GetValue(curYaLiDian, null);
                        if (curTempTime != null && !string.IsNullOrWhiteSpace(curTempTime.ToString()))
                        {
                            tempTime     = DataUtil.ToDateString(curTempTime); //获取采集时间属性值
                            tempTimeFlag = true;
                        }
                        // 在拿到值
                        Type type = curYaLiDian.GetType();                                                        //获取类型
                        System.Reflection.PropertyInfo propertyInfo = type.GetProperty(sensor.dataSourceAddress); //获取指定名称的属性
                        pointDataSource = propertyInfo.GetValue(curYaLiDian, null);                               //获取属性值
                    }
                    catch (Exception e)
                    {
                        string er = string.Format("未在WEB监测点数据源中找到配置站点信息:{0}找到点地址为:{1}的点,错误原因:{2}" + station._Name, sensor.sensorName, e.Message);
                        sensor.MakeFail(er);
                        TraceManagerForWeb.AppendErrMsg(er);
                        errorTimes++;
                        continue;
                    }

                    // 根据数据源获取数据
                    sensor.ReadWEBPoint(pointDataSource);
                    sensor.LastTime = tempTime;// 使用采集时间,不要用当前时间

                    if (sensor.State == ValueState.Fail)
                    {
                        string er = string.Format("站点名称:{0},sensorName:{1},取值错误:{2}", station._Name, sensor.sensorName, sensor.Mess);
                        TraceManagerForWeb.AppendErrMsg(er);
                        errorTimes++;
                        continue;
                    }

                    // 判断采集时间是否正常
                    if (!tempTimeFlag)
                    {
                        dicStations[key].IsOnline = false;
                    }
                }
            }
        }