/// <summary> /// Create the AF Database, Element Templates, and Attribute Templates. /// </summary> private void CreateAFTemplates() { // Check if PI System exists. PISystem targetPISystem = new PISystems()[_conf.AFServerName]; if (targetPISystem == null) { Console.WriteLine("AF server does not exist"); Environment.Exit(0); } // Check if AF Database exists. If so, delete it and recreate it to start anew. _afContext = new AFContext(); _afContext.Database = targetPISystem.Databases[_conf.AFDatabaseName]; if (_afContext.Database != null) { Console.Write(string.Format(@"AF Database {0} already exists. " + @"Press Y to remove and recreate the database, " + @"N to quit the program and specify a different database name : ", _conf.AFDatabaseName)); while (true) { ConsoleKeyInfo result = Console.ReadKey(); Console.WriteLine("\n"); if ((result.KeyChar == 'Y') || (result.KeyChar == 'y')) { targetPISystem.Databases.Remove(_afContext.Database); break; } else if ((result.KeyChar == 'N') || (result.KeyChar == 'n')) { Environment.Exit(0); } Console.Write("Invalid input, try again (Y/N) : "); } } _afContext.Database = targetPISystem.Databases.Add(_conf.AFDatabaseName); // Create the Enumeration Set. AFEnumerationSet modes = _afContext.Database.EnumerationSets.Add("Modes"); modes.Add("Manual", 0); modes.Add("Auto", 1); modes.Add("Cascade", 2); modes.Add("Program", 3); modes.Add("Prog-Auto", 4); // Create element templates for SubTree and Branch elements. AFElementTemplate subTree_ElemTmp = _afContext.Database.ElementTemplates.Add(Constants.SUBTREE); AFElementTemplate branch_ElemTmp = _afContext.Database.ElementTemplates.Add(Constants.BRANCH); AFAttributeTemplate subtree_Rollup_AttrTmp = subTree_ElemTmp.AttributeTemplates.Add(Constants.ROLLUP_SUM_ATTRIBUTE); AFAttributeTemplate branch_Rollup_AttrTmp = branch_ElemTmp.AttributeTemplates.Add(Constants.ROLLUP_SUM_ATTRIBUTE); AFAttributeTemplate threshold_AttrTmp = branch_ElemTmp.AttributeTemplates.Add(Constants.THRESHOLD_ATTRIBUTE); subtree_Rollup_AttrTmp.Type = typeof(float); branch_Rollup_AttrTmp.Type = typeof(float); threshold_AttrTmp.Type = typeof(int); AFPlugIn _piPointDR = AFDataReference.GetPIPointDataReference(targetPISystem); subtree_Rollup_AttrTmp.DataReferencePlugIn = _piPointDR; branch_Rollup_AttrTmp.DataReferencePlugIn = _piPointDR; string serverPath = @"\\" + _conf.PIServerName + @"\"; subtree_Rollup_AttrTmp.ConfigString = serverPath + @"HighAsset_%Element%_Total"; branch_Rollup_AttrTmp.ConfigString = serverPath + @"HighAsset_%Element%_Total"; threshold_AttrTmp.SetValue(_conf.ThresholdValue, null); // Create element templates for Leaf elements. _afContext.BaseLeafTemplate = _afContext.Database.ElementTemplates.Add(Constants.LEAF); _afContext.SinusoidLeafTemplate = _afContext.Database.ElementTemplates.Add(Constants.LEAF_SIN); _afContext.RandomLeafTemplate = _afContext.Database.ElementTemplates.Add(Constants.LEAF_RAND); _afContext.SinusoidLeafTemplate.BaseTemplate = _afContext.BaseLeafTemplate; _afContext.RandomLeafTemplate.BaseTemplate = _afContext.BaseLeafTemplate; // Add attribute templates for base leaf Element Templates. AFAttributeTemplate subtree_AttrTmp = _afContext.BaseLeafTemplate.AttributeTemplates.Add(Constants.SUBTREE); AFAttributeTemplate branch_AttrTmp = _afContext.BaseLeafTemplate.AttributeTemplates.Add(Constants.BRANCH); AFAttributeTemplate ID_AttrTmp = _afContext.BaseLeafTemplate.AttributeTemplates.Add(Constants.LEAF_ID); AFAttributeTemplate value_AttrTmp = _afContext.BaseLeafTemplate.AttributeTemplates.Add("Value"); AFAttributeTemplate mode_AttrTmp = _afContext.BaseLeafTemplate.AttributeTemplates.Add("Mode"); subtree_AttrTmp.Type = typeof(string); branch_AttrTmp.Type = typeof(string); ID_AttrTmp.Type = typeof(string); value_AttrTmp.Type = typeof(float); value_AttrTmp.DataReferencePlugIn = _piPointDR; mode_AttrTmp.DataReferencePlugIn = _piPointDR; mode_AttrTmp.TypeQualifier = modes; mode_AttrTmp.SetValue(modes["Manual"], null); mode_AttrTmp.ConfigString = serverPath + @"HighAsset_%Element%_Mode; ptclassname=classic; pointtype=digital; digitalset=modes; pointsource=R; location1=0; location4=3; location5=1"; // Add attribute templates for sinusoid leaf Element Templates. AFAttributeTemplate value_sinusoid_AttrTmp = _afContext.SinusoidLeafTemplate.AttributeTemplates.Add("Value"); value_sinusoid_AttrTmp.Type = typeof(double); value_sinusoid_AttrTmp.DataReferencePlugIn = _piPointDR; value_sinusoid_AttrTmp.ConfigString = serverPath + @"HighAsset_%Element%_Sinusoid; ptclassname=classic; pointtype=float32; pointsource=R; location1=0; location4=3; location5=0"; // Add attribute templates for random leaf Element Templates. AFAttributeTemplate value_random_AttrTmp = _afContext.RandomLeafTemplate.AttributeTemplates.Add("Value"); value_random_AttrTmp.Type = typeof(double); value_random_AttrTmp.DataReferencePlugIn = _piPointDR; value_random_AttrTmp.ConfigString = serverPath + @"HighAsset_%Element%_Random; ptclassname=classic; pointtype=float32; pointsource=R; location1=0; location4=3; location5=1"; // Create container element under which all leaf elements will be stored. _afContext.Database.Elements.Add("LeafElements"); // Do a bulk checkin of all changes made so far. _afContext.Database.CheckIn(AFCheckedOutMode.ObjectsCheckedOutThisThread); }