<%@ LANGUAGE=VBSCRIPT %>
<% Option Explicit%>
<%
' This ASP page receives a HTTP request that contains data from a GSM-based device and stores it in a database (this code uses an Access database).
' In this example, the data is a set of measured voltage and current values with an associated time-stamp.
' The request includes the following:
' IMEI - the IMEI number of the radio sending the request
' Number - The phone number of the radio sending the request
' Data - This is a set of comma-delimited data that contains the Date & time, voltage and current. Each field is seperated by a comma. Multiple packets can be
' sent, with a semi-colon between packets. To conserve space and increase throughput, the Date & time are sent as a "Unix Epoch Time" value.
'
' Example request with 3 data points:
' IMEI=123456&Number=2125551212&data=1244994322,1.23,3.4;1244994323,1.25,3.4;1244994324,1.24,3.3;
' The database contains the following fields, of the specified types:
' ID - AutoNumber
' IMEI - Text
' Date - Long Integer
' Voltage - Single
' Current - Single
If Err.Number <> 0 then ' If we get an error display it - aids debugging
Response.Write(Err.Description)
Error.Clear
End If
' The METHOD constant determines which method (POST or GET) is being used to send the data- comment-out one of them
' Normally a POST should be used, but by setting this to 'GET' you can easily test the code using a Browser by placing the request in the URL. i.e.
' [url]www.MyServer.com/Savepowerdata.asp?IMEI=123456&Number=2125551212&data=1244994322,1.23,3.4;1244994323,1.25,3.4;1244994324,1.24,3.3;[/url]
Const METHOD = "POST"
' Const METHOD = "GET"
Const MY_DATA_BASE = "Data\PowerData.mdb" ' Path & filename of our database
Const TABLE_NAME = "PowerData" ' Name of the table we store the data in
Dim db_connection_string
Dim rsData
Dim data_string, remote_addr
Dim imei, phone_number, time, voltage, current
Dim end_field, start_field
' Extract the IMEI, Phone Number and Data string from the request
if METHOD = "POST" then
' These three lines are used if a POST request is being accepted (this is the recommended method)
data_string = Request.form("Data")
imei = Request.form("Imei")
phone_number = Request.form("Number")
else
' These three lines should be used if a GET request is being accepted
data_string = Request.QueryString("Data")
imei = Request.QueryString("Imei")
phone_number = Request.QueryString("Number")
end if
' Open the recordset that contains our data
db_connection_string = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath(MY_DATA_BASE) & ";"
Set rsData = Server.CreateObject("ADODB.Recordset")
rsData.Open "SELECT * FROM " & TABLE_NAME, db_connection_string, adOpenStatic, adLockPessimistic, adCmdText
start_field =1
' Parse each data-point from the data string and save it in the database
do
end_field = instr(start_field, data_string, ",") ' Find comma after Date/Time number
if end_field > 0 then
time = mid(data_string, start_field, end_field - start_field) ' Extract the data-point's date/time
else
Exit Do
end if
start_field = end_field+1
end_field = instr(start_field, data_string, ",") ' Find the comma after the voltage, and extract the voltage
if end_field > 0 then voltage = mid(data_string, start_field, end_field - start_field)
start_field = end_field+1
end_field = instr(start_field, data_string, ";") ' Find the semi-colon after the data-point and extract the current
if end_field > 0 then current = mid(data_string, start_field, end_field - start_field)
start_field = end_field+1
if end_field > 0 then ' If we have all of the data for the datapoint, save it in the database '
rsData.AddNew
rsData.Fields("IMEI") = imei
rsData.Fields("Date") = time
rsData.Fields("Voltage") = voltage
rsData.Fields("current") = current
rsData.Update
end if
loop while end_field > 0
rsData.Close ' Close the database and destroy the recordset object
set rsData = nothing
response.write("OK") ' Let the Client know we processed the data - we could add error checking that will report missing data, bad data, etc.
%>