Advertisement
leandrobpedro

CreateElektroDatabase

Jan 15th, 2019
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub xo_CustomConfig()
  2.     Const xlCellTypeLastCell = 11
  3.     Const xlValues = -4163
  4.    
  5.     Set objExcel = CreateObject("Excel.Application")
  6.     Set objWorkbook = objExcel.Workbooks.Open( "c:\temp\TB0806-BD-001.xlsx" )
  7.     objExcel.Visible = True
  8.    
  9.     Set list = CreateObject("System.Collections.ArrayList")
  10.    
  11.     Dim i
  12.     Dim enabled
  13.     Dim lastRow
  14.     Dim sheet
  15.     Dim offset
  16.    
  17.     For Each sheet In objExcel.ActiveWorkbook.Sheets
  18.         lastRow = sheet.UsedRange.SpecialCells(xlCellTypeLastCell).row
  19.         Set r = sheet.UsedRange.Find("Incluído")
  20.         offset = 0
  21.         If Not r Is Nothing Then
  22.             For i = 2 To lastRow
  23.                 ' TODO: desabilitar quando X estiver taxado.
  24.                
  25.                 enabled = sheet.Cells(i, r.Column).Value = "X" Or _
  26.                         sheet.Cells(i, r.Column).Value = "x"
  27.                
  28.                 If enabled Then
  29.                     a = sheet.Cells(i, "F").Value   ' Name
  30.                     b = sheet.Cells(i, "G").Value   ' DocString
  31.                     c = GetTagKind( sheet.Name )
  32.                     d = sheet.Cells(i, sheet.UsedRange.Find("IEC-61850").Column).Value  ' IEC61850
  33.                                    
  34.                     Set m_tag = New Tag.Init(a, b, c, d)
  35.                    
  36.                     IEC61850 m_tag
  37.                     DNPMaster m_tag, offset
  38.                     DNPSlave m_tag, offset
  39.  
  40.                     offset = offset + 1
  41.                     ' list.Add m_tag
  42.                 End If
  43.             Next
  44.         End If
  45.     Next
  46.     objExcel.Application.Quit
  47.    
  48.     MsgBox "Configuracao concluida"
  49. End Sub
  50.  
  51. '*********************************** class ************************************'
  52. Class Tag
  53.     Public AdviseType,AllowRead,AllowWrite,EnableDriverEvent
  54.     Public DocString,Name
  55.     Public N1,N2,N3,N4,ParamDevice,ParamItem
  56.     Public Kind, Params
  57.     Public Folder
  58.    
  59.     Private arr
  60.    
  61.     Public Function Init(m_Name, m_DocString, m_Kind, m_Params)
  62.         Set Init = Me
  63.         Name = m_Name
  64.         DocString = m_DocString
  65.         Kind = m_Kind
  66.         Params = m_Params
  67.         Folder = GetFolder( m_Kind )
  68.         GetParams()
  69.        
  70.         Select Case m_Kind
  71.             Case "DD"   : AllowRead = True : AllowWrite = False : N1 = 1 : N2 = 1 : N3 = 402
  72.             Case "DS"   : AllowRead = True : AllowWrite = False : N1 = 1 : N2 = 1 : N3 = 202
  73.             Case "MA"   : AllowRead = True : AllowWrite = False : N1 = 1 : N2 = 1 : N3 = 3205
  74.             Case "CD"   : AllowRead = False: AllowWrite = True  : N1 = 1 : N2 = 5 : N3 = 1201
  75.             Case "SP"   : AllowRead = False: AllowWrite = True  : N1 = 1 : N2 = 5 : N3 = 4103
  76.         End Select
  77.     End Function
  78.    
  79.     Private Function GetFolder( kind )
  80.         Select Case kind
  81.             Case "DD", "DS" : GetFolder = "Digitais"
  82.             Case "MA"       : GetFolder = "Analogicos"
  83.             Case "CD"       : GetFolder = "Comandos"
  84.             Case "SP"       : GetFolder = "Setpoints"
  85.         End Select
  86.     End Function
  87.    
  88.     Private Sub GetParams()
  89.         arr = Split(Replace(Params & ".#.#", "/", "."), ".") '
  90.         Select Case m_Kind
  91.             Case "MA"
  92.                 If UBound(arr) = 7 Then
  93.                     If (arr(3) = "OpCnt") Then
  94.                         ParamItem = arr(2) & "$ST$OpCnt$stVal"
  95.                     Else
  96.                         ParamItem = arr(2) & "$" & arr(5) & "$" & arr(3) & "$" & arr(4) & "$cVal$mag$f"
  97.                     End If
  98.                 ElseIf UBound(arr) = 6 Then
  99.                     ParamItem = arr(2) & "$" & arr(4) & "$" & arr(3) & "$mag$f"
  100.                 End If
  101.             Case Else
  102.                 If arr(4) = "#" Then
  103.                     arr(4) = "ST"
  104.                 ElseIf arr(5) = "#" Then
  105.                     arr(5) = GetAttribute(arr(3))
  106.                 End If
  107.                 ParamItem = arr(2) & "$" & arr(4) & "$" & arr(3) & "$" & arr(5)
  108.         End Select
  109.        
  110.         ParamDevice = arr(0) & ":" & arr(0) & arr(1)
  111.     End Sub
  112.    
  113.        
  114.    
  115.    
  116.     Private Function GetAttribute(ByVal str)
  117.         Dim regex
  118.         Set regex = CreateObject("VBScript.RegExp")
  119.        
  120.         With regex
  121.             .Global = True
  122.             .MultiLine = False
  123.             .IgnoreCase = False
  124.             .Pattern = "^(Op|TrDev.*[0-9]|Tr|OpCls|RecOK|RestST|RecCyc.*[0-9]|DefTrip)$"
  125.         End With
  126.    
  127.         GetAttribute = IIf(regex.test(str), "general", "stVal")
  128.     End Function
  129. End Class
  130. '*********************************** /class ***********************************'
  131.  
  132. Function GetTagKind( name )
  133.     ' TODO:
  134.     Select Case True
  135.         Case InStr(name, "Duplos") > 0  : GetTagKind = "DD"
  136.         Case InStr(name, "Simples") > 0 : GetTagKind = "DS"
  137.         Case InStr(name, "Ana") > 0     : GetTagKind = "MA"
  138.         Case InStr(name, "Comand") > 0  : GetTagKind = "CD"
  139.         Case InStr(name, "metros") > 0  : GetTagKind = "SP"
  140.         Case Else                       : GetTagKind = ""
  141.     End Select
  142. End Function
  143. Sub IEC61850( tag )
  144.     Set driver = Application.GetObject("Aquisicao.[61850].IEC61850")
  145.    
  146.     On Error Resume Next
  147.     Set folder = driver.Item( tag.Folder )
  148.     If Err Then Set folder = driver.AddObject( "IOFolder", True, tag.Folder )
  149.     On Error Goto 0
  150.    
  151.     Set bind = folder.AddObject("IOTag", False)
  152.     With bind
  153.         ' .AdviseType = 0
  154.         .AllowRead = tag.AllowRead
  155.         .AllowWrite = tag.AllowWrite
  156.         ' .EnableDeadBand = tag.EnableDeadBand
  157.         ' .EnableDriverEvent = tag.EnableDriverEvent
  158.         .DocString = tag.DocString
  159.         .Name = tag.Name
  160.         .ParamDevice = tag.ParamDevice
  161.         .ParamItem = tag.ParamItem
  162.     End With
  163.    
  164. End Sub
  165. Sub DNPMaster( tag , offset)
  166.     Set driver = Application.GetObject(Parent.Parent.Name & "_IO_01")
  167.    
  168.     On Error Resume Next
  169.     Set folder = driver.Item( tag.Folder )
  170.     If Err Then Set folder = driver.AddObject( "IOFolder", True, tag.Folder )
  171.     On Error Goto 0
  172.    
  173.     Set bind = folder.AddObject("IOTag", False)
  174.     With bind
  175.         ' .AdviseType = 0
  176.         .AllowRead = tag.AllowRead
  177.         .AllowWrite = tag.AllowWrite
  178.         ' .EnableDeadBand = tag.EnableDeadBand
  179.         ' .EnableDriverEvent = tag.EnableDriverEvent
  180.         .DocString = tag.DocString
  181.         .Name = tag.Name
  182.         .N1 = tag.N1
  183.         .N2 = tag.N2
  184.         .N3 = tag.N3
  185.         .N4 = offset
  186.     End With
  187. End Sub
  188. Sub DNPSlave( tag , offset)
  189.     Set driver = Application.GetObject("Distribuicao.DNP3Slave")
  190.    
  191.     On Error Resume Next
  192.     Set folder = driver.Item( tag.Folder )
  193.     If Err Then Set folder = driver.AddObject( "IOFolder", True, tag.Folder )
  194.     On Error Goto 0
  195.    
  196.    
  197.     ' TODO
  198.     Select Case tag.Kind
  199.         Case "DS", "DD" : tag.N1 = 21
  200.         Case Else       : tag.N1 = 0
  201.     End Select
  202.        
  203.     '
  204.     Set bind = folder.AddObject("IOTag", False)
  205.     With bind
  206.         ' .AdviseType = 0
  207.         .AllowRead = Not tag.AllowRead
  208.         .AllowWrite = Not tag.AllowWrite
  209.         ' .EnableDeadBand = tag.EnableDeadBand
  210.         ' .EnableDriverEvent = tag.EnableDriverEvent
  211.         .DocString = tag.DocString
  212.         .Name = tag.Name
  213.         .N1 = tag.N1
  214.         .N2 = tag.N2
  215.         .N3 = tag.N3
  216.         .N4 = offset
  217.         If tag.Kind <> "CD" Then
  218.             .Links.CreateLink "Value", "Aquisicao.IEC61850." & tag.Folder & "." & tag.Name & ".Value", 0
  219.         End If
  220.     End With
  221. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement