Exemple #1
0
        /// <summary>
        /// 写入单个标签点的历史数据
        /// 这个方法主要用于从csv文件导入历史数据到golden数据库中。从csv读取的数据是字符串型二维数组,因此这里的历史数据入参是字符串型二维数组,而不是List<PValue>
        /// </summary>
        /// <param name="tagname"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public int PutArchivedValues(string tagname, string[][] data)
        {
            //注意,只能向数据库第一个有效数据的时间之后,插入历史数据。
            //——如果插入的值得历史时刻在第一个有效值前,则插入不返回错误,但是数据写不进去。
            //准备数据
            PISDK.PIValues pivalues = new PISDK.PIValues(); //新建PIValues,ReadOnly属性为true
            pivalues.ReadOnly = false;                      //只有将PIValues的readonly属性置为false,才能写入。

            //数据点的描述,是一个NamedValues类型。该类型是一个集合。可以添加不同的描述项,并给项设定值
            NamedValues nvatts = new NamedValues();

            nvatts.Add("questionable", true);

            //将数据写入pivalues
            //——只使用第1个值即实时值,第2个值时间。
            int i = 0;

            //try
            //{
            for (i = 0; i < data.Length; i++)
            {
                try
                {
                    //在PI导出的数据中,value有可能是字符串。有时候表示服务器启停,有时候表示“IO timeout”等,这些值都无法转换成数值型,必须跳过。
                    if (data[i] != null && data[i].Length != 0)
                    {
                        pivalues.Add(Convert.ToDateTime(data[i][2]), Convert.ToDouble(data[i][1]), nvatts);
                    }
                }
                catch
                {
                    continue;
                }
            }
            //}
            //catch (Exception ex)
            //{
            //    //将pi的异常PgimDataException记录在_exception中,并以PgimDataException的名称继续抛出异常
            //    this._exception = "写历史数据时,转换数据格式错误!错误行数"+i.ToString();
            //    throw ex;     //将pgim的异常继续向外传递。
            //}

            //使用piPoint.Data.UpdateValues更新点历史数据
            //this._piPoint = new PISDK.PIPoint();
            try
            {
                this._piPoint = this._piServer.PIPoints[tagname];
                PISDKCommon.PIErrors pierror = this._piPoint.Data.UpdateValues(pivalues);
                //处理写入结果
                return(pierror.Count);
            }
            catch (Exception ex)
            {
                //将pi的异常PgimDataException记录在_exception中,并以PgimDataException的名称继续抛出异常
                this._exception = "PI其他未知错误。通常是服务器故障、标签中对应的服务器名称不正确导致!";
                throw ex;     //将pgim的异常继续向外传递。
                return(-1);
            }
        }
        public pointData getPointList(string tagName, string start, string end)
        {
            PISDK.PIValues PIconnect = new PISDK.PIValues();  //Create new instance of PI value
            PIconnect = PI_SERVER.PIPoints[tagName].Data.RecordedValues(start, end);
            pointData temp = new pointData();

            foreach (PIValue val in PIconnect)
            {
                if (val.Value as DigitalState == null) // if point has data, Digital state means point has no data
                {
                    object objtemp = val.Value;
                    temp.value = objtemp.ToString();
                    objtemp = val.TimeStamp.LocalDate;
                    temp.timestamp = objtemp.ToString();
                }
            }
            lastValueTime = end;
            return temp;
        }
        public pointData updatePointValue(string tagName, string start, string end)
        {
            PISDK.PIValues PIconnect = new PISDK.PIValues();  //Create new instance of PI value
            PIconnect = PI_SERVER.PIPoints[tagName].Data.RecordedValues(start, end);
            pointData temp = new pointData();

            foreach (PIValue val in PIconnect)
            {
                if (val.Value as DigitalState == null) // if point has data, Digital state means point has no data
                {
                    object objtemp = val.Value;
                    temp.value     = objtemp.ToString();
                    objtemp        = val.TimeStamp.LocalDate;
                    temp.timestamp = objtemp.ToString();
                }
            }
            lastValueTime = end;
            return(temp);
        }
 public int[] getPointList(string tagName, string start, string end)
 {
     PISDK.PIValues PIconnect = new PISDK.PIValues();  //Create new instance of PI value
     PIconnect = PI_SERVER.PIPoints[tagName].Data.RecordedValues(start, end);
     pointData temp = new pointData();
     int counter = 0;
     int[] PIlist = new int[60];
     
     foreach (PIValue val in PIconnect)
     {
         if (val.Value as DigitalState == null) // if point has data, Digital state means point has no data
         {
             object objtemp = val.Value;
             temp.value = objtemp.ToString();
             objtemp = val.TimeStamp.LocalDate;
             temp.timestamp = objtemp.ToString();
             PIlist[counter] = int.Parse(temp.value); // add each PI point to the list
             counter++;
         }
     }
     lastValueTime = end;
     return PIlist;
 }
        public int[] getPointList(string tagName, string start, string end)
        {
            PISDK.PIValues PIconnect = new PISDK.PIValues();  //Create new instance of PI value
            PIconnect = PI_SERVER.PIPoints[tagName].Data.RecordedValues(start, end);
            pointData temp = new pointData();
            int counter = 0;
            int[] PIlist = new int[60];

            foreach (PIValue val in PIconnect)
            {
                if (val.Value as DigitalState == null) // if point has data, Digital state means point has no data
                {
                    object objtemp = val.Value;
                    temp.value = objtemp.ToString();
                    objtemp = val.TimeStamp.LocalDate;
                    temp.timestamp = objtemp.ToString();
                    PIlist[counter] = int.Parse(temp.value); // add each PI point to the list
                    counter++;
                }
            }
            lastValueTime = end;
            return PIlist;
        }
Exemple #6
0
        /// <summary>
        /// 查询间隔数据
        /// </summary>
        /// <param name="tagName">查询条件</param>
        /// <param name="startTime">起始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="sampleInterVal">间隔时间:1h,2h,3h,4h</param>
        /// <param name="fvc">过滤方式</param>
        /// <param name="filter">过滤</param>
        /// <returns></returns>
        public virtual DataTable GetInterpolatedValues(string[] tagName, DateTime startTime, DateTime endTime, object sampleInterVal, string filter)
        {
            if (this.server == null)
            {
                //log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
                //log.Debug("PIServer未初始化");
                logs.writelog("PIServer未初始化");
                throw new Exception("PIserver未初始化。");
            }
            //PIValues pivalues = null;
            DataSet ds = new DataSet();
            //声明一个数据表,表名为LoadValue
            DataTable dt = new DataTable("LoadValue");

            //该表有3列,分别为piID,ptValue,ptTime
            dt.Columns.Add("ptID", typeof(System.String));
            dt.Columns.Add("ptValue", typeof(System.String));
            dt.Columns.Add("ptTime", typeof(DateTime));

            //StringBuilder sb = new StringBuilder();
            //由于历史数据可能过多,所以选择用datatable返回
            try
            {
                if (!this.server.Connected)
                {
                    this.server.Open(this.piConnectionString);
                }
                //分别对pointID取数
                for (int i = 0; i < tagName.Length; i++)
                {
                    //pisdk的pipoint为pointID对应的原子点名
                    PISDK.PIPoint piPoint = this.server.PIPoints[tagName[i].ToString()];
                    //取2min内的2个点
                    PISDK.PIValues piValues = piPoint.Data.InterpolatedValues(
                        System.DateTime.Now.AddMinutes(-2),
                        System.DateTime.Now.AddMinutes(-1),
                        3,
                        "1=1",
                        PISDK.FilteredViewConstants.fvShowFilteredState,
                        new PISDKCommon.PIAsynchStatus());
                    if (piValues.Count > 0)
                    {
                        //将每个点对应的数据和事件加入到数据表LoadValue中
                        for (int j = 1; j <= piValues.Count; j++)
                        {
                            DataRow dr = dt.NewRow();
                            dr["ptID"]    = tagName[i];
                            dr["ptValue"] = piValues[j].Value.ToString();
                            dr["ptTime"]  = piValues[j].TimeStamp.LocalDate;
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
                //log.Error(ex);
                logs.writelog("按照时间间隔查询数据发生错误:" + ex);
                throw new Exception("按照时间间隔查询数据发生错误:" + ex.Message);
            }
            return(dt);
        }