-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferEngine.bas
More file actions
151 lines (136 loc) · 5.6 KB
/
TransferEngine.bas
File metadata and controls
151 lines (136 loc) · 5.6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Attribute VB_Name = "TransferEngine"
Option Explicit
Public Sub TransferData()
Dim rangeTagName As Range
On Error GoTo errorHandler
Application.Cursor = xlWait
If ConnectServers() Then
Set rangeTagName = ActiveSheet.Range("rangeTagsStart").Offset(1, 0)
Do While Trim(rangeTagName.Value) <> ""
' If the target tag name is blank, then assume that the target
' name is the same as the source name.
If Trim(rangeTagName.Offset(0, 1).Value) = "" Then
ProcessTagValues rangeTagName.Value, rangeTagName.Value
Else
ProcessTagValues rangeTagName.Value, rangeTagName.Offset(0, 1).Value
End If
Set rangeTagName = rangeTagName.Offset(1, 0)
Loop
Else
Application.Cursor = xlDefault
MsgBox "Unable to connect to Source and Target PI servers.", vbExclamation, APP_NAME
End If
Application.Cursor = xlDefault
Exit Sub
errorHandler:
Application.Cursor = xlDefault
End Sub
Private Sub ProcessTagValues(ByVal SourceTagName As String, ByVal TargetTagName As String)
Dim ReturnCode As Long
Dim Times() As Long
Dim Values() As Single
Dim DigStates() As Long
Dim SourcePointNum As Long
Dim TargetPointNum As Long
Dim Count As Long
Dim Index As Long
Dim ErrorCount As Integer
Dim IsSourceTagDigital As Boolean
Dim ItemCount As Long
On Error GoTo errorHandler
ItemCount = 0
ErrorCount = 0
SetActiveServer SOURCE_SERVER
SourcePointNum = GetPointNumber(SourceTagName)
If SourcePointNum > 0 Then
IsSourceTagDigital = IsDigitalTag(SourceTagName)
Count = ActiveSheet.Range("rangeMaxDataPoints").Value
ReDim Values(Count)
ReDim DigStates(Count)
ReDim Times(Count)
Times(0) = GetPITime(ActiveSheet.Range("rangeStartTime").Value)
Times(Count - 1) = GetPITime(ActiveSheet.Range("rangeEndTime").Value)
ReturnCode = piar_compvalues(SourcePointNum, Count, Times(0), Values(0), DigStates(0), 0)
If ReturnCode = SUCCESS Then
Application.StatusBar = Count & " archive values found for tag " & SourceTagName
SetActiveServer TARGET_SERVER
TargetPointNum = GetPointNumber(TargetTagName)
If TargetPointNum > 0 Then
If IsDigitalTag(TargetTagName) Then
' Make sure the source tag is also digital. The flag is set above when
' the source PI server is the active connection.
If IsSourceTagDigital Then
ConvertDigStates SourceTagName, TargetTagName, DigStates
Count = Count - 1
For Index = 0 To Count
Application.StatusBar = "Writing value " & Index + 1 & " of " & Count & " to target PI server..."
ReturnCode = piar_putvalue(TargetPointNum, 0, DigStates(Index), Times(Index), 0)
If ReturnCode <> SUCCESS Then
ErrorCount = ErrorCount + 1
If ErrorCount > ActiveSheet.Range("rangeMaxErrors").Value Then
Exit Sub
End If
Else
ItemCount = ItemCount + 1
If ItemCount > ActiveSheet.Range("rangeMaxItems").Value Then
ItemCount = 0
Sleep
End If
End If
Next Index
End If
Else
Count = Count - 1
For Index = 0 To Count
Application.StatusBar = "Writing value " & Index + 1 & " of " & Count + 1 & " to target PI server..."
ReturnCode = piar_putvalue(TargetPointNum, Values(Index), 0, Times(Index), 0)
If ReturnCode <> SUCCESS Then
ErrorCount = ErrorCount + 1
If ErrorCount > ActiveSheet.Range("rangeMaxErrors").Value Then
Exit Sub
End If
Else
ItemCount = ItemCount + 1
If ItemCount > ActiveSheet.Range("rangeMaxItems").Value Then
ItemCount = 0
Sleep
End If
End If
Next Index
End If
End If
End If
End If
Exit Sub
errorHandler:
MsgBox "Error occurred in the ProcessTagValues routine: " & Err.Description, vbCritical, APP_NAME
End Sub
Private Sub Sleep()
Const TIME_CONV_FACTOR As Double = 1 / 24 / 60 / 60
Dim SleepStart As Date
Dim SleepEnd As Date
SleepStart = Now
SleepEnd = SleepStart + (ActiveSheet.Range("rangeRestDuration").Value * TIME_CONV_FACTOR)
Do While Now < SleepEnd
Application.StatusBar = "Sleeping... " & Format(Now, "hh:mm:ss")
DoEvents
Loop
End Sub
Private Sub ConvertDigStates(ByVal SourceTagName As String, _
ByVal TargetTagName As String, _
ByRef DigStates() As Long)
Dim numStates As Long
Dim Index As Long
Dim StateString As String
Dim StateStrings() As String
numStates = UBound(DigStates)
ReDim StateStrings(numStates)
SetActiveServer SOURCE_SERVER
For Index = 0 To numStates
StateStrings(Index) = GetDigitalStateString(DigStates(Index))
Next
SetActiveServer TARGET_SERVER
For Index = 0 To numStates
DigStates(Index) = GetDigitalStateCodeForTag(TargetTagName, StateStrings(Index))
Next Index
End Sub