Ejemplo n.º 1
0
        public override AFAttributeList GetInputs(object context)
        {
            AFAttributeList attList = new AFAttributeList();

            attList.Add(this.Attribute.Element.Attributes["Latitude"]);
            attList.Add(this.Attribute.Element.Attributes["Longitude"]);

            _uomMeter = Attribute.PISystem.UOMDatabase.UOMs["meter"];
            _uomMPH   = Attribute.PISystem.UOMDatabase.UOMs["mile per hour"];

            return(attList);
        }
Ejemplo n.º 2
0
        //Gets the input Attributes
        public override AFAttributeList GetInputs(object context)
        {
            AFAttributeList inputs = new AFAttributeList();

            if (!String.IsNullOrEmpty(latAttribute))
            {
                inputs.Add(this.GetAttribute(latAttribute)); //Adds the Latitude Attribute to the input attribute colletion
            }

            if (!String.IsNullOrEmpty(longAttribute))
            {
                inputs.Add(this.GetAttribute(longAttribute)); //Adds the Longitude Attribute to the input attribute colletion
            }

            return(inputs);
        }
Ejemplo n.º 3
0
        private AFAttributeList LoadParameters()
        {
            if (Attribute == null || Attribute.Element == null) {
                throw new ApplicationException("Attribute and/or element is null");
            }

            var paramAttributes = new AFAttributeList();

            if (!string.IsNullOrEmpty(fPointAttribute)) {
                AFDatabase db = Attribute.Database;
                if (db == null) {
                    throw new ApplicationException("No database found");
                }

                // find Attribute's object by it name from parameters
                var ptAttr = AFAttribute.FindAttribute(fPointAttribute, Attribute);
                if (ptAttr == null) {
                    throw new ApplicationException(string.Format(Resources.ERR_AttributeHasNotBeenFound, fPointAttribute));
                }

                paramAttributes.Add(ptAttr);
            } else {
                throw new ApplicationException("Name of PI Point attribute is null or empty");
            }

            return paramAttributes;
        }
        public void Run()
        {
            PISystems piSystems = new PISystems();
            PISystem piSystem = piSystems["<AFSERVER>"];

            AFDatabase afDatabase = piSystem.Databases["NuGreen"];

            AFElementTemplate boilerTemplate = afDatabase.ElementTemplates["Boiler"];

            const int pageSize = 1000;
            int startIndex = 0;
            int totalCount;
            do
            {
                // Find a collection of elements instantiated from the Boiler tempplate.
                // Only the Elements' header information (Name, Description, Template, Type, etc.)
                // are loaded from the AF Server by this call.
                AFNamedCollection<AFElement> elements = AFElement.FindElements(
                    database: afDatabase,
                    searchRoot: null,
                    query: "Boiler",
                    field: AFSearchField.Template,
                    searchFullHierarchy: true,
                    sortField: AFSortField.Name,
                    sortOrder: AFSortOrder.Ascending,
                    startIndex: startIndex,
                    maxCount: pageSize,
                    totalCount: out totalCount);
                if (elements == null) break;

                // Partially load the element by retrieving information only for the Water Flow attribute.
                AFElement.LoadAttributes(elements, new[] { boilerTemplate.AttributeTemplates["Water Flow"] });

                Console.WriteLine("Found {0} Elements.", elements.Count);

                AFAttributeList attrList = new AFAttributeList();

                // Because we are retrieving the Water Flow attribute which was previously loaded,
                // no additional server calls are made.
                // If LoadAttributes had not been called previously, then a server call would have been made for each element
                // in the loop below.
                foreach (AFElement item in elements)
                {
                    attrList.Add(item.Attributes["Water Flow"]);
                }

                AFValues values = attrList.GetValue();

                Console.WriteLine("  Water Flow values");
                foreach (AFValue val in values)
                {
                    Console.WriteLine("  Element: {0}, Timestamp: {1}, Value: {2}",
                        val.Attribute.Element, val.Timestamp, val.Value.ToString());
                }

                startIndex += pageSize;

            } while (startIndex < totalCount);
        }
        // ===================================================================================
        // Override the Various Inputs required to calculate a value for this attribute.
        // ===================================================================================
        public override AFAttributeList GetInputs(object context)
        {
            AFAttributeList inputs = new AFAttributeList();

            //// TODO add better error handling here.
            inputs.Add(this.GetAttribute(_targetAttributeName));
            LoadParameters();
            //// Anytime someone asks for values from this data reference we tell the SDK that we also need these attributes.
            return(inputs);
        }
        private AFAttributeList CreateBrandNewInputList(object context)
        {
            // PRO TIP:
            // See comments in GetInputs where you want to avoid _cachedInputAttrs.Add or AddRange.
            // For thread safety, use a brand new list that is local to this method.

            // Start with a brand new list
            var brandNewList = new AFAttributeList();

            AFAttribute measurement = null;

            // First and foremost we need a measurement attribute, which is the centerpoint to compare against limits.
            if (UseParentAttribute)
            {
                // https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/P_OSIsoft_AF_Asset_AFDataReference_Attribute.htm
                // https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/P_OSIsoft_AF_Asset_AFAttribute_Parent.htm
                measurement = Attribute.Parent;
                if (measurement == null)
                {
                    throw new Exception("Root-level attribute does not have a parent.  You must define 'MeasAttr=something' in the ConfigString.");
                }
            }
            else
            {
                // Let's offer some bit of name substitution.
                // However, the GetInputs method lacks any timeContext, which restricts @value substitution
                // to current values only.  This restriction is fine for static attributes.
                // https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/T_OSIsoft_AF_AFNameSubstitutionType.htm
                var path = SubstituteParameters(_measAttrName, this, context, timeContext: null);
                // Note that the final fetch of the measurement attribute is *relative* to the current Attribute.
                // https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/P_OSIsoft_AF_Asset_AFAttribute_Attributes.htm
                measurement = Attribute.Attributes[path];
                if (measurement == null)
                {
                    throw new Exception($"MeasAttr '{_measAttrName}' not found.  Check your ConfigString.");
                }
            }

            if (!IsNumericType(Type.GetTypeCode(measurement.Type)))
            {
                throw new Exception($"MeasAttr does not have a numeric Type.");
            }

            // If the list will have any items, the measurement will always be at Index 0.
            brandNewList.Add(measurement);

            // Let the CDR automatically fetch the associated limits.
            // These could come back in any order, plus some or all may be missing!
            // Geez, doesn't that make it fun and challenging!
            // https://techsupport.osisoft.com/Documentation/PI-AF-SDK/html/M_OSIsoft_AF_Asset_AFAttribute_GetAttributesByTrait.htm
            brandNewList.AddRange(measurement.GetAttributesByTrait(AllowedTraits));

            return(brandNewList);
        }
Ejemplo n.º 7
0
        //bring in PIPoint attributes
        public override AFAttributeList GetInputs(object context)
        {
            AFAttributeList returnList = new AFAttributeList();

            string[] splitConfig    = ConfigString.Split(';');
            string   selectedOption = splitConfig[2];

            if (selectedOption == "convertRadio")
            {
                //config string is "initial date;name of attribute"
                AFAttribute minSinceAttributeObj = GetAttribute(splitConfig[1]);
                returnList.Add(minSinceAttributeObj);
            }
            else //selectedOption == "formatRadio"
            {
                //config string is "name of date attribute;name of time attribute"
                AFAttribute dateAttributeObj = GetAttribute(splitConfig[0]);
                AFAttribute timeAttributeObj = GetAttribute(splitConfig[1]);
                returnList.Add(dateAttributeObj);
                returnList.Add(timeAttributeObj);
            }

            return(returnList);
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            NetworkCredential credential = new NetworkCredential(connectionInfo.user, connectionInfo.password);
            var piSystem = (new PISystems())[connectionInfo.AFServerName];

            Console.WriteLine($"connecting to : {connectionInfo.AFServerName} - {connectionInfo.AFDatabaseName}");
            piSystem.Connect(credential);
            var afdb = piSystem.Databases[connectionInfo.AFDatabaseName];

            Console.WriteLine("connected");

            //element search
            var query  = "Template:'Antimatter Relay'";
            var search = new AFElementSearch(afdb, "Relay Search", query);

            var results  = search.FindElements(0, true, 1000);
            var attrList = new AFAttributeList();

            foreach (var element in results)
            {
                Console.WriteLine($"{element.Name}");
                foreach (var attribute in element.Attributes)
                {
                    //not optimized
                    //var snapShot = attribute.GetValue();
                    //Console.WriteLine($"{attribute.Name}: {snapShot.Value.ToString()} {snapShot.UOM}");

                    //optimized
                    attrList.Add(attribute);
                }
            }

            //one call to get values
            var snapShots = attrList.GetValue();

            foreach (var snapShot in snapShots)
            {
                Console.WriteLine($"Element: {snapShot.Attribute.Element.Name} - {snapShot.Attribute.Name}: {snapShot.Value.ToString()} {snapShot.UOM}");
            }

            Console.WriteLine("completed execution");
            //Console.ReadKey();
        }
Ejemplo n.º 9
0
        public void GetPIData(string[] paths, string startTime, string endTime, string interval)
        {
            string[] correctPaths = paths.Select(m => m.Substring(3)).ToArray();
            AFKeyedResults <string, AFAttribute> results = AFAttribute.FindAttributesByPath(correctPaths, null);
            AFAttributeList attributeList = new AFAttributeList();

            foreach (AFAttribute attribute in results)
            {
                attributeList.Add(attribute);
            }

            AFTime                 start        = new AFTime(startTime);
            AFTime                 end          = new AFTime(endTime);
            AFTimeRange            timeRange    = new AFTimeRange(start, end);
            AFTimeSpan             timeSpan     = AFTimeSpan.Parse(interval);
            IEnumerable <AFValues> valueResults = attributeList.Data.InterpolatedValues(timeRange, timeSpan, string.Empty, false, new PIPagingConfiguration(PIPageType.TagCount, 100));

            piValuesList = new PIValuesList(valueResults);
        }
Ejemplo n.º 10
0
        private AFAttributeList LoadParameters()
        {
            if (Attribute == null || Attribute.Element == null)
            {
                throw new ApplicationException("Attribute and/or element is null");
            }

            AFDatabase afDB    = base.Database;
            AFTable    afTable = afDB.Tables[fTableName];

            if (afTable == null)
            {
                throw new ArgumentException("Table not found");
            }
            fDataRows = afTable.Table.Select();

            var paramAttributes = new AFAttributeList();

            if (!string.IsNullOrEmpty(fAttributeName))
            {
                // find Attribute's object by it name from parameters (this attribute contains key values)
                var refAttr = AFAttribute.FindAttribute(fAttributeName, Attribute);
                if (refAttr == null)
                {
                    throw new ApplicationException(string.Format(Resources.ERR_AttributeHasNotBeenFound, fAttributeName));
                }

                if (!Extensions.IsNumericType(refAttr.Type))
                {
                    throw new ApplicationException(string.Format("The attribute `{0}` has no numeric type ", fAttributeName));
                }

                paramAttributes.Add(refAttr);
            }
            else
            {
                throw new ApplicationException("Name of lookup attribute is null or empty");
            }

            return(paramAttributes);
        }
Ejemplo n.º 11
0
        // Helper method used in PrintEnergyUsageAtTime() and PrintDailyAverageEnergyUseage
        // Note that this is an optional method, it is used in the solutions, but it is possible
        // to get a valid solution without using this method
        static AFAttributeList GetAttributes(AFDatabase database, string templateName, string attributeName)
        {
            AFAttributeList attrList = new AFAttributeList();

            using (AFElementSearch elementQuery = new AFElementSearch(database, "AttributeSearch", string.Format("template:\"{0}\"", templateName)))
            {
                elementQuery.CacheTimeout = TimeSpan.FromMinutes(5);
                foreach (AFElement element in elementQuery.FindElements())
                {
                    foreach (AFAttribute attr in element.Attributes)
                    {
                        if (attr.Name.Equals(attributeName))
                        {
                            attrList.Add(attr);
                        }
                    }
                }
            }

            return(attrList);
        }
Ejemplo n.º 12
0
        private void LoadParameters()
        {
            if (Attribute == null || Attribute.Element == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(fSourceAttributeName) && fParamAttributes == null)
            {
                fParamAttributes = new AFAttributeList();

                var attr = AFAttribute.FindAttribute(fSourceAttributeName, Attribute);
                if (attr == null)
                {
                    throw new ArgumentException(string.Format(Resources.ERR_AttributeHasNotBeenFound, SourceAttributeName));
                }
                else
                {
                    fParamAttributes.Add(attr);
                }

                fLastLoadAttribute = DateTime.UtcNow;
            }
        }
Ejemplo n.º 13
0
        private AFAttributeList LoadParameters()
        {
            if (Attribute == null || Attribute.Element == null)
            {
                throw new ApplicationException("Attribute and/or element is null");
            }

            var paramAttributes = new AFAttributeList();

            if (!string.IsNullOrEmpty(fAttributeName))
            {
                var attr = AFAttribute.FindAttribute(fAttributeName, Attribute);
                if (attr == null)
                {
                    throw new ArgumentException(string.Format(Resources.ERR_AttributeHasNotBeenFound, fAttributeName));
                }
                else
                {
                    paramAttributes.Add(attr);
                }
            }

            return(paramAttributes);
        }
Ejemplo n.º 14
0
        public override AFAttributeList GetInputs(object context)
        {
            AFAttributeList attList = new AFAttributeList();

            attList.Add(this.Attribute.Element.Attributes["Latitude"]);
            attList.Add(this.Attribute.Element.Attributes["Longitude"]);

            _uomMeter = Attribute.PISystem.UOMDatabase.UOMs["meter"];
            _uomMPH = Attribute.PISystem.UOMDatabase.UOMs["mile per hour"];

            return attList;
        }
        public void Run()
        {
            PISystems piSystems = new PISystems();
            PISystem  piSystem  = piSystems["<AFSERVER>"];

            AFDatabase afDatabase = piSystem.Databases["NuGreen"];

            AFElementTemplate boilerTemplate = afDatabase.ElementTemplates["Boiler"];

            const int pageSize   = 1000;
            int       startIndex = 0;
            int       totalCount;

            do
            {
                // Find a collection of elements instantiated from the Boiler tempplate.
                // Only the Elements' header information (Name, Description, Template, Type, etc.)
                // are loaded from the AF Server by this call.
                AFNamedCollection <AFElement> elements = AFElement.FindElements(
                    database: afDatabase,
                    searchRoot: null,
                    query: "Boiler",
                    field: AFSearchField.Template,
                    searchFullHierarchy: true,
                    sortField: AFSortField.Name,
                    sortOrder: AFSortOrder.Ascending,
                    startIndex: startIndex,
                    maxCount: pageSize,
                    totalCount: out totalCount);
                if (elements == null)
                {
                    break;
                }

                // Partially load the element by retrieving information only for the Water Flow attribute.
                AFElement.LoadAttributes(elements, new[] { boilerTemplate.AttributeTemplates["Water Flow"] });

                Console.WriteLine("Found {0} Elements.", elements.Count);

                AFAttributeList attrList = new AFAttributeList();

                // Because we are retrieving the Water Flow attribute which was previously loaded,
                // no additional server calls are made.
                // If LoadAttributes had not been called previously, then a server call would have been made for each element
                // in the loop below.
                foreach (AFElement item in elements)
                {
                    attrList.Add(item.Attributes["Water Flow"]);
                }

                AFValues values = attrList.GetValue();

                Console.WriteLine("  Water Flow values");
                foreach (AFValue val in values)
                {
                    Console.WriteLine("  Element: {0}, Timestamp: {1}, Value: {2}",
                                      val.Attribute.Element, val.Timestamp, val.Value.ToString());
                }

                startIndex += pageSize;
            } while (startIndex < totalCount);
        }
Ejemplo n.º 16
0
        private AFAttributeList LoadParameters(object context)
        {
            if (Attribute == null || Attribute.Element == null)
            {
                throw new ApplicationException("Attribute and/or element is null");
            }

            var paramAttributes = new AFAttributeList();

            if (!string.IsNullOrEmpty(AttributeName))
            {
                AFDatabase db = Attribute.Database;
                if (db == null)
                {
                    throw new ApplicationException("No database found");
                }

                // find Attribute's object by it name from parameters (this attribute contains link to other attribute with real values)
                string name;
                if (IsAttributeSubstituted(AttributeName))
                {
                    string value = SubstituteParameters(AttributeName, this, null, null);
                    name = SubstituteAttributeValues(context, null, value);
                }
                else
                {
                    name = AttributeName;
                }

                var refAttr = AFAttribute.FindAttribute(name, Attribute);
                if (refAttr == null)
                {
                    throw new ApplicationException(string.Format(Resources.ERR_AttributeHasNotBeenFound, name));
                }

                if (MethodType == RefAttributeType.DynamicPath)
                {
                    AFValue inVal  = refAttr.GetValue();
                    object  objVal = inVal.Value;
                    if (inVal.IsGood && objVal != null)
                    {
                        if (Extensions.IsStringVal(objVal))
                        {
                            string attrName;
                            try {
                                attrName = objVal.ToString();
                            } catch (Exception ex) {
                                throw new ArgumentException(string.Format(Resources.ERR_UnrecognizedRefValue, inVal.ToString(), name));
                            }

                            var lookupAttr = AFAttribute.FindAttribute(attrName, db);
                            if (lookupAttr == null)
                            {
                                throw new ArgumentException(string.Format(Resources.ERR_AttributeLookupHasNotBeenFound, attrName));
                            }
                            else
                            {
                                paramAttributes.Add(lookupAttr);
                            }
                        }
                        else
                        {
                            throw new ApplicationException(Resources.ERR_SourceAttributeMustBeStringType);
                        }
                    }
                    else
                    {
                        throw new ApplicationException(string.Format(Resources.ERR_UnrecognizedRefValue, "null or bad", name));
                    }
                }
                else
                {
                    paramAttributes.Add(refAttr);
                }

                if (TimestampType == "Attribute")
                {
                    var tsAttr = AFAttribute.FindAttribute(TimestampSource, Attribute);
                    if (tsAttr == null)
                    {
                        throw new ApplicationException(string.Format(Resources.ERR_AttributeHasNotBeenFound, TimestampSource));
                    }
                    paramAttributes.Add(tsAttr);
                }
            }
            else
            {
                throw new ApplicationException("Name of lookup attribute is null or empty");
            }

            return(paramAttributes);
        }