Example #1
0
        /// <summary>
        /// 获取极值
        /// </summary>
        /// <param name="tagName">查询条件</param>
        /// <param name="startTime">起始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="nv"></param>
        /// <param name="atc"></param>
        /// <param name="qs">极值的类型,年份,月份,天</param>
        /// <returns></returns>
        public virtual StringBuilder GetSummary(string tagName, DateTime startTime, DateTime endTime, out NamedValues nv, ArchiveSummaryTypeConstants atc, QueryStyle qs)
        {
            PointList pLst = null;

            if (this.server == null)
            {
                //log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
                //log.Debug("PIServer未初始化");
                logs.writelog("PIServer未初始化");
                throw new Exception("piserver未初始化。");
            }
            StringBuilder sb = new StringBuilder();

            try
            {
                PIData  pData  = null;
                PIValue pValue = null;
                switch (qs)
                {
                case QueryStyle.Year:
                    startTime = DateTime.Parse(startTime.Year.ToString() + "-01-01 00:00:00");
                    endTime   = DateTime.Parse(endTime.Year.ToString() + "12-31 23:59:59");
                    break;

                case QueryStyle.Month:
                    startTime = DateTime.Parse(startTime.Year.ToString() + "-" + startTime.Month.ToString() + "-01 00:00:00");
                    endTime   = DateTime.Parse(endTime.Year.ToString() + "-" + endTime.Month.ToString() + "-31 23:59:59");
                    break;

                default:
                    break;
                }
                PITime ptStart = new PITime();
                ptStart.LocalDate = startTime;
                PITime ptEnd = new PITime();
                ptEnd.LocalDate = endTime;
                if (!this.server.Connected)
                {
                    this.server.Open(this.piConnectionString);
                }
                nv   = null;
                pLst = this.server.GetPoints(tagName, null); //此时pLst中不会存在数据,也就是客户端现在还没有数据
                foreach (PIPoint point in pLst)
                {
                    pData  = point.Data;                                                                                         //取数据
                    pValue = pData.Summary(ptStart, ptEnd, atc, CalculationBasisConstants.cbTimeWeighted, new PIAsynchStatus()); //取极值数据
                    sb.AppendFormat(point.Name + "|" + pValue.TimeStamp.LocalDate.ToString() + "|" + pValue.Value.ToString() + "!");
                }
                sb.Remove(sb.Length - 1, 1);
            }
            catch (Exception ex)
            {
                //log4net.ILog log = log4net.LogManager.GetLogger(this.GetType());
                //log.Error(ex);
                logs.writelog("获取极值发生错误:" + ex);
                throw new Exception("获取极值发生错误:" + ex.Message);
            }
            return(sb);
        }
Example #2
0
        float GetCurrentValue(string tagname)
        {
            float currentvalue = 0;

            PISDK.PISDK sdk = new PISDK.PISDK();
            //PISDK.Server srv = sdk.Servers["mus-as-126.corp.pdo.om"];
            //srv.Open("UID=upoa;PWD=upoa");
            string piServer      = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["PIServer"]) ? "mus-as-126.corp.pdo.om" : ConfigurationSettings.AppSettings["PIServer"];
            string piCredentials = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["PICredentials"]) ? "UID=upoa;PWD=upoa" : ConfigurationSettings.AppSettings["PICredentials"];
            Server srv           = sdk.Servers[piServer];

            srv.Open(piCredentials);
            PISDK.PIPoints myPoints = srv.PIPoints;
            try
            {
                PIValue value = myPoints[tagname].Data.Snapshot;
                currentvalue = value.Value;
            }
            catch (Exception ex)
            { }
            return(currentvalue);
        }
Example #3
0
        private async Task <Cities> GetCitiesDataWithBatch()
        {
            //Exercise 5
            //Please refer to the following page for more information about Batch: https://pisrv01.pischool.int/piwebapi/help/controllers/batch/actions/execute
            Dictionary <string, PIBatchRequest> globalBatch = new Dictionary <string, PIBatchRequest>();

            //First internal request gets the WebId of the AF database Weather
            PIBatchRequest batchGetDbWebId = new PIBatchRequest()
            {
                Method   = "GET",
                Resource = baseUrl + @"assetdatabases?path=\\pisrv01\Weather"
            };

            //Second internal request gets the WebId from all attributes "Wikipedia Thumbnail Url" (one per each city)
            //using the WebId returned from the first internal request
            PIBatchRequest batchGetElementsWebIds = new PIBatchRequest()
            {
                Method     = "GET",
                Resource   = baseUrl + "assetdatabases/{0}/elementattributes?attributeNameFilter=*Wikipedia%20Thumbnail%20Url*",
                Parameters = new List <string>()
                {
                    "$.1.Content.WebId"
                },
                ParentIds = new List <string>()
                {
                    "1"
                }
            };

            //Third internal request gets the current values from all attributes "Wikipedia Thumbnail Url"
            //using the WebIds returned from the second internal request
            PIBatchRequest batchGetAttributesValues = new PIBatchRequest()
            {
                Method          = "GET",
                RequestTemplate = new PIRequestTemplate()
                {
                    Resource = baseUrl + "streams/{0}/value"
                },
                Parameters = new List <string>()
                {
                    "$.2.Content.Items[*].WebId"
                },
                ParentIds = new List <string>()
                {
                    "2"
                }
            };


            //Add all internal requests to the dictionary and create a JSON response for the body of the HTTP request
            globalBatch.Add("1", batchGetDbWebId);
            globalBatch.Add("2", batchGetElementsWebIds);
            globalBatch.Add("3", batchGetAttributesValues);
            string json = JsonConvert.SerializeObject(globalBatch);

            //It is important to define that the format of the body message is JSON
            HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

            string url = baseUrl + "/batch";

            //Start to make the HTTP request and wait for the response
            string response = await PostWebData(url, httpContent);

            //Deserialize the response to have access to the responses of each internal request
            Dictionary <string, PIBatchResponse> batchData = JsonConvert.DeserializeObject <Dictionary <string, PIBatchResponse> >(response);

            //Convert the string responses to PIListObject and PIValue[]
            PIListObject imageAttributes = JsonConvert.DeserializeObject <PIListObject>(batchData["2"].Content.ToString());
            JArray       items           = (JArray)(batchData["3"].Content["Items"]);

            PIValue[] piValues = new PIValue[items.Count];
            for (int i = 0; i < items.Count; i++)
            {
                piValues[i] = JsonConvert.DeserializeObject <PIValue>(items[i]["Content"].ToString());
            }

            //Using both PIListObjects as inputs, the Cities object is created.
            Cities cities = new Cities(imageAttributes, piValues);

            return(cities);
        }
 public ApiResponsePIValue(int statusCode, IDictionary <string, string> headers, PIValue data)
     : base(statusCode, headers)
 {
     this.Data = data;
 }