Author Topic: A simple application data monitor using HTML & Browser  (Read 1353 times)

SteveDee

  • Newbie
  • *
  • Posts: 33
A simple application data monitor using HTML & Browser
« on: December 18, 2009, 10:37:34 AM »
There are times where it might be useful to have a simple way to monitor basic data from your application.

Two examples that I have faced in industry are:-
1. Running a "headless" data server application. With no user interface, it is useful to have an independant means of checking that your app is running, how long it has been "up" and to provide a list of recent errors.
2. A data management system where you wish to provide a simple data summary to production line staff, anywhere in the plant, without giving them access to the full and relatively complex main application.

A solution I favor is to pump summary data out to an HTML file, with read only access from anywhere on the network.

You can't assume that everybody is computer savvy, but the chances are that anyone who has used a computer will be familiar with a web browser, even if they have never used a wordprocessor or spreadsheet app. So by providing them with a link, operators at almost any level should be able to view your data.

Since we want the browser to show current data, we simply code a refresh command (to the browser) in the html file with a suitable update period.

If you want to explore this, start a new Gambas project and add a timer to the main form. Then add this code to the main form:-

Code: [Select]
PUBLIC lngCount AS Long
PUBLIC dteStartTime AS Date

PUBLIC SUB Form_Open()

  dteStartTime = Now
  WITH Timer1
    .Delay = 5000
    .Start
  END WITH
 
END

PUBLIC SUB Timer1_Timer()
DIM strTemp AS String
DIM strRunTime AS String
 
  'Simulate a "process" temperature
  INC lngCount
  strRunTime = CStr(DateDiff(dteStartTime, Now, gb.Minute))
  strTemp = CStr(Round((Rnd(25)), -2))
  HTML.SaveReport("Current Temperature", strTemp, strRunTime, lngCount)

END

Now add a new module to your project and name it "HTML". Paste the code below into your HTML module.

Code: [Select]
' Gambas module file

PRIVATE sngLastReading AS Single

PUBLIC FUNCTION SaveReport(strHeading AS String, strTempC AS String, strRun AS String, lngReadings AS Long) AS Boolean

DIM strReportFile AS String
DIM strReport AS String
DIM strTempDiff AS String
DIM hFile AS File

  strReportFile = User.Home & "/Report.html"
  strTempDiff = CStr(Round(CSng(strTempC) - sngLastReading, -2))
   
    'Build Report
    strReport = "<html><head>" & Chr(10)
    strReport = strReport & "<title>"
    strReport = strReport & strHeading
    strReport = strReport & "</title>" & Chr(10)
    'auto refresh every 5 secs
    strReport = strReport & "<meta http-equiv=" "refresh" " content=5>"
    strReport = strReport & "</head>"
    'back colour
    strReport = strReport & "<body bgcolor=" "#E0E0FF" ">"
    strReport = strReport & "<body>"
    'bold text
    strReport = strReport & "<p>" & "<B>" & "Real-Time HTML Report - " & strHeading & "</B>" & "</p>"
    'Normal
    strReport = strReport & "<p>" & "A simple example of an HTML monitor screen" & "</p>"
    'horiz line & blank line
    strReport = strReport & "<HR>" & "<BR>"
    'table
    strReport = strReport & "<table border=" "1" ">"
    strReport = strReport & "<tr>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Time: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">" & Format(Time, "hh:nn:ss") & "</font></td>"
    strReport = strReport & "</tr>"
    strReport = strReport & "<tr>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Date: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">" & Format(Date, "dd-mmmm-yyyy") & "</font></td>"
    strReport = strReport & "</tr>"
    strReport = strReport & "<tr>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Temperature: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" " color=" "blue" ">" & strTempC & " C" & "</font></td>"
    strReport = strReport & "</tr>"
    strReport = strReport & "<tr>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Temp change: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">" & strTempDiff & " C" & "</font></td>"
    strReport = strReport & "</tr>"
    strReport = strReport & "<tr>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Number of Readings: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">" & lngReadings & "</font></td>"
    strReport = strReport & "</tr>"
    strReport = strReport & "<tr>"   
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Browser Refresh Rate: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">5 seconds</font></td>"   
    strReport = strReport & "</tr>"
    strReport = strReport & "<tr>"   
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">Server up time: </font></td>"
    strReport = strReport & "<td><font size=" "2" " face=" "Verdana" ">" & strRun & " mins</font></td>"   
    strReport = strReport & "</tr>"   
    strReport = strReport & "</table><p><br></p>"
    strReport = strReport & "<HR>"
    'embed a graphic
    strReport = strReport & "<EMBED SRC=" & Application.Path & "/logo.jpg" " HEIGHT=" "75" " WIDTH=" "250" ">"
    strReport = strReport & "<BR>" & "<HR>" & "<BR>"
    'play a sound
    strReport = strReport & "<EMBED SRC=" & Application.Path & "/bell.wav" " HEIGHT=" "0" " WIDTH=" "0" ">"
    'add a Marquee
    strReport = strReport & "<MARQUEE>Here we go again!</MARQUEE>"
   
    strReport = strReport & "</body>"
    strReport = strReport & "</html>"
    sngLastReading = CSng(strTempC)
   
  TRY KILL strReportFile
  hFile = OPEN strReportFile FOR APPEND 
  WRITE #hFile, strReport, Len(strReport)
  CLOSE #hFile 
 
END


Run the project and point your web browser at the Report.html file in your Home directory.

I couldn't see an option to attached my sound & graphic files to this post, so if you wish you can provide your own and add these to the Gambas project folder. However, this wont stop the code above from running.

Linux Basic

A simple application data monitor using HTML & Browser
« on: December 18, 2009, 10:37:34 AM »