Message Tags Not Seen Monitor DataSource Provider for BizTalk RFID MP

Microsoft.BizTalk.RFID.Monitor.MessageTagsNotSeen.DataSource (DataSourceModuleType)

This is the Message Tags Not Seen Monitor DataSource Provider for BizTalk RFID MP.

Element properties:

TypeDataSourceModuleType
IsolationAny
AccessibilityPublic
RunAsMicrosoft.BizTalk.RFID.DiscoveryAccount
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
DS DataSource System.CommandExecuterPropertyBagSource Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
IntervalSecondsint$Config/IntervalSeconds$Interval SecondsThis is the Interval Seconds use to run this script.
LogSuccessEventstring$Config/LogSuccessEvent$Log Success EventThis is the Log Success Event configuration use to run this script.
TimeoutSecondsint$Config/TimeoutSeconds$Timeout SecondsThis is the Timeout Seconds use to run this script.

Source Code:

<DataSourceModuleType ID="Microsoft.BizTalk.RFID.Monitor.MessageTagsNotSeen.DataSource" Accessibility="Public" RunAs="Microsoft.BizTalk.RFID.DiscoveryAccount" Batching="false">
<Configuration>
<xsd:element name="IntervalSeconds" type="xsd:int"/>
<xsd:element name="TargetComputerName" type="xsd:string"/>
<xsd:element name="TargetDeviceName" type="xsd:string"/>
<xsd:element name="LogSuccessEvent" type="xsd:boolean"/>
<xsd:element name="ManagementGroupName" type="xsd:string"/>
<xsd:element name="TimeoutSeconds" type="xsd:int"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="IntervalSeconds" ParameterType="int" Selector="$Config/IntervalSeconds$"/>
<OverrideableParameter ID="LogSuccessEvent" ParameterType="string" Selector="$Config/LogSuccessEvent$"/>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<DataSource ID="DS" TypeID="System!System.CommandExecuterPropertyBagSource">
<IntervalSeconds>$Config/IntervalSeconds$</IntervalSeconds>
<ApplicationName>%windir%\system32\cscript.exe</ApplicationName>
<WorkingDirectory/>
<CommandLine>//nologo $file/RFIDMessageTagsNotSeenMonitor.vbs$ $Config/TargetComputerName$ $Config/TargetDeviceName$ $Config/LogSuccessEvent$ $Config/ManagementGroupName$</CommandLine>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<RequireOutput>true</RequireOutput>
<Files>
<File>
<Name>RFIDMessageTagsNotSeenMonitor.vbs</Name>
<Contents><Script>
Option Explicit
SetLocale("en-us")

'Event Constants
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4
'Other constants
Const SCRIPT_NAME = "RFID Message Tag Not Seen"

' Event ID Constants
Const EVENTID_SUCCESS = 99
Const EVENTID_SCRIPT_ERROR = 1000

Const APP_DISCOVERY_CONNECT_FAILURE = -1
Const APP_DISCOVERY_QUERY_FAILURE = -2
Const REGISTRY_CONNECT_FAILURE = -3
Const REGISTRY_READ_FAILURE = -4
Const HKEY_CLASSES_ROOT = &amp;H80000000
Const HKEY_CURRENT_USER = &amp;H80000001
Const HKEY_LOCAL_MACHINE = &amp;H80000002
Const HKEY_USERS = &amp;H80000003
Const HKEY_CURRENT_CONFIG = &amp;H80000005
Const StateDataType = 3

Dim oParams, TargetComputer, TargetDeviceName, bLogSuccessEvent
Dim strName, intPrevTotalTagsRead, intCurrentTotalTagsRead
Dim oReg, oAPI, oBagState
Dim REG_Key, sStateValuePath
Dim strMessage, strErrorDetail
Dim dtStart

intPrevTotalTagsRead = 0
intCurrentTotalTagsRead = 0
dtStart = Now
oReg = Null

Set oParams = WScript.Arguments
if oParams.Count &lt; 4 then
Wscript.Quit -1
End if

Set oAPI = CreateObject("Mom.ScriptAPI")
Set oBagState = oAPI.CreateTypedPropertyBag(StateDataType)

TargetComputer = oParams(0)
TargetDeviceName = oParams(1)
bLogSuccessEvent = CBool(oParams(2))
sStateValuePath = "HKLM\" &amp; oAPI.GetScriptStateKeyPath(oParams(3))
REG_Key = sStateValuePath &amp; "\BizTalk RFID Management Pack\Message Tag Not Seen Log"

GetMonitorStatus

Sub GetMonitorStatus()
Dim strDevXML
Dim objDevs
Dim intDevs, itmDev
Dim strDeviceName, strDeviceIP, strDeviceState, strMonitorStatus
Dim intPrevCntVal, intCurrCntVal

strDevXML = GetDeviceStatus()
Set objDevs = GetXMLObject(strDevXML)

intDevs = objDevs.selectNodes("//Device").length
strMonitorStatus = "0"

For itmDev=0 to intDevs-1
strDeviceName = objDevs.selectNodes("//Device/Name").item(itmDev).text
strDeviceIP = objDevs.selectNodes("//Device/IP").item(itmDev).text
strDeviceState = objDevs.selectNodes("//Device/State").item(itmDev).text
intPrevCntVal = GetPrevCounterValue(strDeviceName)
intCurrCntVal = GetCurrCounterValue()
call SaveCounterValue(strDeviceName, intCurrCntVal)

If strDeviceState = "Open" Then
If (intPrevCntVal = 0) and (intCurrCntVal = 0) Then
strMonitorStatus = "1"
strErrorDetail = ""
Else
If intPrevCntVal = intCurrCntVal Then
strMonitorStatus = "0"
strErrorDetail = "Monitor Message Tag Not Seen: For Device Name " &amp; strDeviceName &amp; " with Device IP " &amp; strDeviceIP &amp; " - Total Tags Read: " &amp; CStr(intCurrCntVal)
Else
strMonitorStatus = "1"
strErrorDetail = ""
End If
End If
Else
strMonitorStatus = "1"
strErrorDetail = ""
End If
Next

If bLogSuccessEvent Then
strMessage = "The script '" &amp; SCRIPT_NAME &amp; "' For Device Name '" &amp; strDeviceName &amp; "' - Previous Value: " &amp; CStr(intPrevCntVal) &amp; " - Current Value: " &amp; CStr(intCurrCntVal) &amp; " - Monitor Status ='" &amp; strMonitorStatus &amp; "' Error Detail: " &amp; strErrorDetail
CreateEvent EVENTID_SUCCESS, EVENT_TYPE_INFORMATION, strMessage

strMessage = "The script '" &amp; SCRIPT_NAME &amp; "' For Device Name '" &amp; strDeviceName &amp; "' completed successfully in " &amp; _
DateDiff("s", dtStart, Now) &amp; " seconds."
CreateEvent EVENTID_SUCCESS, EVENT_TYPE_INFORMATION, strMessage
End If

oBagState.AddValue "State", strMonitorStatus
oBagState.AddValue "ErrorDetail", strErrorDetail
oAPI.AddItem oBagState
Call oAPI.ReturnItems
End Sub

Sub CreateEvent(lEventID, lEventType, strMessage)
oAPI.LogScriptEvent SCRIPT_NAME,lEventID, lEventType, strMessage
End Sub

Function GetCurrCounterValue()
Dim winmgmt1, intTotalTagsRead
Dim wbemServices, wbemObject, wbemObjectSet
intTotalTagsRead = 0
winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//" &amp; TargetComputer &amp; ""
Set wbemServices = GetObject(winmgmt1)
Set wbemObjectSet = wbemServices.InstancesOf("Win32_PerfFormattedData_RFIDDevices_RFIDDevices")

For Each wbemObject In wbemObjectSet
strName = wbemObject.Name
intTotalTagsRead = wbemObject.TotalTagsRead

If LCase(strName) = LCase(TargetDeviceName) Then
intCurrentTotalTagsRead = CInt(intTotalTagsRead)
End If
Next
GetCurrCounterValue = intCurrentTotalTagsRead
End Function

Function GetPrevCounterValue(strKey)
On Error Resume Next
If IsNull(oReg) Then
Set oReg = CreateObject("WScript.Shell")
End If
Dim regData
regData = oReg.RegRead(REG_Key &amp; "\" &amp; strKey )
If IsNull(regData) or IsEmpty(regData) or regData = "" Then
GetPrevCounterValue = 0
Else
GetPrevCounterValue = CInt(regData)
End If
Err.Clear
End Function

Function SaveCounterValue(strKey, strValue)
If IsNull(oReg) Then
Set oReg = CreateObject("WScript.Shell")
End If
Call oReg.RegWrite(REG_Key &amp; "\" &amp; strKey , CStr(strValue))
Err.Clear
End Function

Function GetDeviceStatus()
Dim strAllOutPut, intDGCnt
Dim strXMLAns, strXMLDet
Dim ArrDevGrps, itmArrDG
Dim intEmpty
strAllOutPut = ExecuteRFIDCmd(TargetDeviceName, true)

strXMLAns = "&lt;Devices&gt;"
strXMLAns = strXMLAns &amp; GetRegExpText("(Name .*|&lt;host&gt;.*&lt;/host&gt;|DeviceConnectionState.*)",strAllOutPut)
strXMLAns = strXMLAns &amp; "&lt;/Devices&gt;"

GetDeviceStatus = strXMLAns
End Function

Function ExecuteRFIDCmd(strDeviceNameToUse, boolReadOutput)
Dim ncControlcommand
Dim oShell
Dim curDir
Dim strOptionToUse
Dim strExecOut

strOptionToUse = "GetDeviceStatus"

Set oShell = CreateObject("WScript.Shell")
curDir = oShell.CurrentDirectory
ncControlcommand = "cmd.exe /C """ &amp; QuoteWrap(GetRFIDConsolExePath()) &amp; " " &amp; QuoteWrap(strOptionToUse) &amp; " " &amp; QuoteWrap(strDeviceNameToUse) &amp;""""

IF boolReadOutput Then
strExecOut = RunCmd(ncControlcommand,true)
Else
strExecOut = RunCmd(ncControlcommand,false)
End If
ExecuteRFIDCmd = strExecOut
End Function

Function RunCmd(CmdString, boolGetOutPut)
Dim wshshell
Dim oExec
Dim output
Dim strOutPut

Set wshshell = CreateObject("WScript.Shell")
Set oExec = wshshell.Exec(CmdString)
Set output = oExec.StdOut
Do While oExec.Status = 0
WScript.Sleep 100
if output.AtEndOfStream = false then
IF boolGetOutPut Then
strOutPut = strOutPut &amp; output.ReadAll
End IF
else
exit Do
end if
Loop
IF boolGetOutPut Then
strOutPut = strOutPut &amp; oExec.Stderr.ReadAll
Else
strOutPut = "1"
End IF

If oExec.ExitCode &lt;&gt; 0 Then
strOutPut = "0"
End If
Set wshshell = Nothing
RunCmd = strOutPut
End Function

Function GetRFIDConsolExePath()
Dim nsCtrExeName
nsCtrExeName="rfidclientconsole.exe"
GetRFIDConsolExePath = nsCtrExeName
end Function

Function QuoteWrap(myString)
If (myString &lt;&gt; "") And (left(mySTring,1) &lt;&gt; Chr(34)) And (Right(myString,1) &lt;&gt; Chr(34)) Then
QuoteWrap = Chr(34) &amp; myString &amp; Chr(34)
Else
QuoteWrap = myString
End If
End Function

Function GetRegExpText(strMatchPattern, strSearchText)
Dim objRegEx, Match, Matches, StrReturnStr
Dim intCol : intCol = 1
Dim strHost
Set objRegEx = New RegExp
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = strMatchPattern
Set Matches = objRegEx.Execute(strSearchText)

For Each Match in Matches
Select Case intCol
Case 1
strReturnStr = strReturnStr &amp; "&lt;Device&gt;"
strReturnStr = strReturnStr &amp; "&lt;Name&gt;" &amp; Replace(Match.value,"Name ","") &amp; "&lt;/Name&gt;"
intCol = intCol +1
Case 2
strHost = Replace(Match.value,"&lt;host&gt;","")
strHost = Replace(strHost,"&lt;/host&gt;","")
strReturnStr = strReturnStr &amp; "&lt;IP&gt;" &amp; strHost &amp; "&lt;/IP&gt;"
intCol = intCol +1
Case 3
strReturnStr = strReturnStr &amp; "&lt;State&gt;" &amp; Replace(Match.value,"DeviceConnectionState ","") &amp; "&lt;/State&gt;"
strReturnStr = strReturnStr &amp; "&lt;/Device&gt;"
intCol = 1
End Select
Next
GetRegExpText = strReturnStr
End Function

Function GetXMLObject(strXMLToUse)
Dim objXMLToUse
Set objXMLToUse = CreateObject("MSXML2.domdocument")
objXMLToUse.async = false
objXMLToUse.loadXML(strXMLToUse)
Set GetXMLObject = objXMLToUse
End Function

Function MomCreateObject(ByVal sProgramId)
Dim ObjError
Set ObjError = New Error

On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
ObjError.Save
On Error Goto 0

If ObjError.Number &lt;&gt; 0 Then WScript.Quit
End Function

Class Error
Private m_lNumber
Private m_sSource
Private m_sDescription
Private m_sHelpContext
Private m_sHelpFile
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class

</Script></Contents>
<Unicode>1</Unicode>
</File>
</Files>
</DataSource>
</MemberModules>
<Composition>
<Node ID="DS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
</DataSourceModuleType>