Applicazione SMS di query della temperatura

1. Background

ActiveXperts ha una parte del suo server che si trova in una sala server a Milano, Italia. Tra le altre cose ActiveXperts monitora la temperatura di questi server.

3. Obiettivi del nuovo sistema

Questo Case Study illustra come configurare SMS Messaging Server per interrogare un dispositivo di temperatura (The EM1 Sensatronics).

Quando un messaggio di testo viene inviato a SMS Messang Server, questo attiverà uno script che interroga il data-center a Milano per la sua temperatura attuale. Questo valore di temperatura viene inviato indietro come un SMS di risposta.

4. Soluzione ActiveXperts SMS Messaging Server

Questo è lo script di attivazione che interroga la temperatura del dispositivo Sensatronics EM1.


' // ========================================================================
' // Default Trigger.vbs
' // ------------------------------------------------------------------------
' // 
' // 
' // ========================================================================

Option Explicit

CONST STR_DEBUGFILE = "C:\Temp\QueryDemoTemperature.txt"

' Declaration of global objects
Dim g_objMessageDB, g_objDebugger, g_objConstants, g_objHttp

' Creation of global objects
Set g_objConstants      = CreateObject( "Axsms-messaging-server.Constants" )
Set g_objMessageDB      = CreateObject( "Axsms-messaging-server.MessageDB" ) 
Set g_objDebugger       = CreateObject( "ActiveXperts.VbDebugger" )
Set g_objHttp           = CreateObject( "Microsoft.XMLHTTP" )

' Set Debug file - for troubleshooting purposes
g_objDebugger.DebugFile = STR_DEBUGFILE
g_objDebugger.Enabled	= False

' // ========================================================================
' // Function: ProcessMessage
' // ------------------------------------------------------------------------
' // ProcessMessage trigger function to process incoming messages
' // ========================================================================
Function ProcessMessage( numMessageID )
   Dim objMessageIn, objMessageOut

   g_objDebugger.WriteLine ">> ProcessMessage"

   ' Open the Message Database
   g_objMessageDB.Open
   If( g_objMessageDB.LastError <> 0 ) Then
      g_objDebugger.WriteLine "<< ProcessMessage,  unable to open database"
      Exit Function
   End If

   ' Retrieve the message that has just been received. If it fails then exit script 
   Set objMessageIn   = g_objMessageDB.FindFirstMessage( "ID = " & numMessageID ) 
   If g_objMessageDB.LastError <> 0 Then
      g_objMessageDB.Close
      g_objDebugger.WriteLine "<< ProcessMessage, error: g_objMessageDB.LastError
      Exit Function
   End If

   ' Change Status to from Pending to Success. If you don't do it, the message will be 
   ' processed by subsequent triggers (if defined) because message is still pending
   objMessageIn.Status = g_objConstants.MESSAGESTATUS_SUCCESS
   g_objMessageDB.Save objMessageIn   

   g_objDebugger.WriteLine "Incoming message saved, result: [" & g_objMessageDB.LastError & "]"

   ReplyMessage ( objMessageIn )
 
   ' Close the Message Database
   g_objMessageDB.Close

   g_objDebugger.WriteLine "<< ProcessMessage"

End Function


' // ========================================================================
' // ReplyMessage
' // ------------------------------------------------------------------------
' // Auto reply to every incoming SMS message
' // ========================================================================

Function ReplyMessage( objMessageIn )

   Dim objMessageOut
   Dim strResponse
   Dim strTempCelcius
   
   If( GetTemperature( strTempCelcius ) ) Then
     strResponse = "The current temperature is: " & strTempCelcius & " degrees celsius."
   Else
     strResponse = "Failed to read temperature from device."
   End If

   g_objDebugger.WriteLine ">> ReplyMessage: " & strResponse

   Set objMessageOut = g_objMessageDB.Create
   If( g_objMessageDB.LastError = 0 ) Then
     objMessageOut.Direction = g_objConstants.MESSAGEDIRECTION_OUT
     objMessageOut.Status    = g_objConstants.MESSAGESTATUS_PENDING
     objMessageOut.Type      = objMessageIn.Type 
     objMessageOut.ToAddress = objMessageIn.FromAddress
     ' objMessageOut.ChannelID = 0
     ' explicitly set the channel ID to the SMS channel
     objMessageOut.ChannelID = 1101
     objMessageOut.BodyFormat= objMessageIn.BodyFormat
     objMessageOut.Body      = strResponse
     g_objMessageDB.Save objMessageOut
   End If

  g_objDebugger.WriteLine "<< ReplyMessage"

End Function


' // ========================================================================
' // GetTemperature
' // ------------------------------------------------------------------------
' // Get temperature
' // ========================================================================

Function GetTemperature( BYREF sTempCelcius )
  Dim sData, sTempFahr, iPos

  GetTemperature = False
  sTempCelcius = ""

  iPos         = 0
  sTempFahr    = ""
  sData        = ""
  
On Error Resume Next
  ' Get the temperature value from the Sensatronics EM1 device
  g_objHttp.open "GET", "http://em-1.activexperts-labs.com/data", false
  g_objHttp.send
  sData = g_objHttp.responseText
  iPos = InStr( InStr( sData,"|" ) + 1, sData, "|" )
On Error Goto 0
  If( Err.Number <> 0 Or sData = "" Or iPos < 3 ) Then
    Exit Function
  End If
   
  ' Return the temperature converted to celsius
  sTempFahr = Mid( sData, 3, iPos - 3 )  
  sTempCelcius = Round( ( 5 / 9 ) * ( sTempFahr - 32 ), 1 )

  GetTemperature = True
End Function