SMS project to query temperature and receive an SMS response

This case study is included in the evaluation version of the SMS Messaging Server

1. Background

ActiveXperts has a some of it's server located in a server room in Milan, Italy. Among other things ActiveXperts would like to monitor the temperature of these servers.

2. Goals of the new System

This case study demonstrates how to configure the SMS Messaging server to query a temperature device (The Sensatronics EM1).

When a text message is send to the SMS Messaging Server it will trigger a script which queries the data-center in Milan for its current temperature. This temperature value is sent back as an SMS reply.

3. ActiveXperts SMS Messaging Server Solution

This is the trigger script which queries the Sensatronics EM1 temperature device.

' // ========================================================================
' // 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