-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWriteValuesUsingAFExample.cs
More file actions
60 lines (49 loc) · 2.53 KB
/
WriteValuesUsingAFExample.cs
File metadata and controls
60 lines (49 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using OSIsoft.AF;
using OSIsoft.AF.Asset;
using OSIsoft.AF.Data;
using OSIsoft.AF.Time;
namespace ExamplesLibrary
{
/// <summary>
/// This example demonstrates how to write values to the PI Data Archive via an AF Server.
/// An example for numeric and digital tags is given.
/// </summary>
/// <prerequisite-examples>
/// BuildSimpleDatabaseExample
/// </prerequisite-examples>
public class WriteValuesUsingAFExample : IExample
{
public void Run()
{
PISystems piSystems = new PISystems();
PISystem piSystem = piSystems["<AFSERVER>"];
AFDatabase afDatabase = piSystem.Databases["Basic-AFSDK-Sample"];
AFElement boilerA = afDatabase.Elements["Region_0"].Elements["BoilerA"];
AFElementTemplate elementTemplate = afDatabase.ElementTemplates["BasicBoilerTemplate"];
AFAttributeTemplate temperatureAttrTemplate = elementTemplate.AttributeTemplates["Temperature"];
AFAttributeTemplate modeAttrTemplate = elementTemplate.AttributeTemplates["Mode"];
AFElement.LoadAttributes(new[] { boilerA }, new[] { temperatureAttrTemplate, modeAttrTemplate });
AFEnumerationSet digSet = afDatabase.EnumerationSets["Modes"];
IList<AFValue> valuesToWrite = new List<AFValue>();
for (int i = 0; i < 10; i++)
{
AFTime time = new AFTime(new DateTime(2015, 1, 1, i, 0, 0, DateTimeKind.Local));
AFValue afValueFloat = new AFValue(i, time);
// Associate the AFValue to an attribute so we know where to write to.
afValueFloat.Attribute = boilerA.Attributes["Temperature"];
AFEnumerationValue digSetValue = i % 2 == 0 ? digSet["Auto"] : digSet["Manual"];
AFValue afValueDigital = new AFValue(digSetValue, time);
afValueDigital.Attribute = boilerA.Attributes["Mode"];
valuesToWrite.Add(afValueFloat);
valuesToWrite.Add(afValueDigital);
}
// Perform a bulk write. Use a single local call to PI Buffer Subsystem if possible.
// Otherwise, make a single call to the PI Data Archive.
// We use no compression just so we can check all the values are written.
// AFListData is the class that provides the bulk write method.
AFListData.UpdateValues(valuesToWrite, AFUpdateOption.InsertNoCompression, AFBufferOption.BufferIfPossible);
}
}
}