/* * TINIWebServer.java */ /* Copyright (C) 1999 Dallas Semiconductor Corporation. * All rights Reserved. Printed in U.S.A. * This software is protected by copyright laws of * the United States and of foreign countries. * This material may also be protected by patent laws of the United States * and of foreign countries. * This software is furnished under a license agreement and/or a * nondisclosure agreement and may only be used or copied in accordance * with the terms of those agreements. * The mere transfer of this software does not imply any licenses * of trade secrets, proprietary technology, copyrights, patents, * trademarks, maskwork rights, or any other form of intellectual * property whatsoever. Dallas Semiconductor retains all ownership rights. */ /* $Workfile: TINIWebServer.java $ $Revision: 7 $ $Date: 2009/03/05 10:15AM $ $Author: Ganbold $ $Modtime: 2009/03/05 6:15PM $ */ import java.net.*; import java.io.*; import java.util.*; import com.dalsemi.system.*; import com.dalsemi.system.TINIOS.*; /** This class implements a web server using the HTTPServer class. Logging is implemented using * the HTTPServer default logging option. Applications may leave default logging disabled and * implement their own logging methods. */ public class TINIWebServer { WebWorker webWorker; TemperatureWorker temperatureWorker; String[] prevTemp = new String[4]; int prev100s, prev10s, prev1s; byte[] copyBuffer; String webRoot, webIndex; boolean lastButtonFound = true; boolean localPages = false; static Object lock = new Object();// lock for file access Clock clock = new Clock(); static String indexTop = "" + "" + "TINIWebServer" + "" + "" + "" + "" + "" + "" + "" + "" + "

TINIWebServer

" + "

TINIWebServer is running
On TINI


" + "

" + "The TINIWebServer application uses the HTTPServer class to implement a simple web server.
" + "

" + "

Current temperature of devices:

"; static String indexBottom = "


" + "
" + "" + ""; /** Constructor */ public TINIWebServer() throws IOException { copyBuffer = new byte[1024]; } /** Update the temperature on the web page. */ void updateTemperature(String[] temperature) { prevTemp = temperature; if (!localPages) createPage(temperature); else createPageLocal(temperature); } /** Concatenate top and bottom of index page around temperature * using local index array info. */ public void createPageLocal(String[] temp) { System.out.println("Creating page using createPageLocal"); try { clock.getRTC(); int year = clock.getYear(); int month = clock.getMonth(); int day = clock.getDate(); int hour = clock.getHour(); int minute = clock.getMinute(); int second = clock.getSecond(); String dString= new String("
Current date: "); if (month < 10) dString += "0"; dString += Integer.toString(month); dString += "/"; if (day < 10) dString += "0"; dString += Integer.toString(day); dString += "/"; if (year < 10) dString += "0"; dString += Integer.toString(year); String timeString = new String("
Current time: " + hour + ":"); if (minute < 10) timeString += "0"; timeString += Integer.toString(minute); timeString += ":"; if (second < 10) timeString += "0"; timeString += Integer.toString(second); if (clock.getPm()==true) timeString += " pm"; else timeString += " am"; synchronized(lock) { FileOutputStream index = new FileOutputStream(new File(webRoot, webIndex)); index.write(indexTop.getBytes(), 0, indexTop.length()); String br = "
"; for (int i = 0; i < 4; i++) { index.write(temp[i].getBytes()); index.write(br.getBytes()); } index.write(dString.getBytes()); index.write(timeString.getBytes()); index.write(indexBottom.getBytes(), 0, indexBottom.length()); index.close(); } } catch(Exception e) { ByteArrayOutputStream out = new ByteArrayOutputStream (); e.printStackTrace (new PrintStream (out)); System.out.println("createPageLocal -" + e.toString()); } } /** Concatenate top and bottom of index page around temperature. */ public void createPage(String[] temp) { System.out.println("Creating page using createPage"); try { File indexPage = new File(webRoot + "indextop.html"); if(!indexPage.exists()) { localPages = true; createPageLocal(temp); return; } clock.getRTC(); int year = clock.getYear(); int month = clock.getMonth(); int day = clock.getDate(); int hour = clock.getHour(); int minute = clock.getMinute(); int second = clock.getSecond(); String dString= new String("
Current date: "); if (month < 10) dString += "0"; dString += Integer.toString(month); dString += "/"; if (day < 10) dString += "0"; dString += Integer.toString(day); dString += "/"; if (year < 10) dString += "0"; dString += Integer.toString(year); String timeString = new String("
Current time: " + hour + ":"); if (minute < 10) timeString += "0"; timeString += Integer.toString(minute); timeString += ":"; if (second < 10) timeString += "0"; timeString += Integer.toString(second); if (clock.getPm()==true) timeString += " pm"; else timeString += " am"; synchronized(lock) { FileOutputStream index = new FileOutputStream(new File(webRoot, webIndex)); FileInputStream tempFile = null; int bytesRead = (tempFile = new FileInputStream(new File(webRoot, "indextop.html"))).read(copyBuffer); index.write(copyBuffer, 0, bytesRead); tempFile.close(); String br = "
"; for(int i = 0; i < 4; i++){ index.write(temp[i].getBytes()); index.write(br.getBytes()); } index.write(dString.getBytes()); index.write(timeString.getBytes()); bytesRead = (tempFile = new FileInputStream(new File(webRoot, "indexbottom.html"))).read(copyBuffer); index.write(copyBuffer, 0, bytesRead); tempFile.close(); index.close(); } } catch(Exception e) { ByteArrayOutputStream out = new ByteArrayOutputStream (); e.printStackTrace (new PrintStream (out)); System.out.println("createPage -" + e.toString()); } } /** Start the various servers. Then check the temperature and * update the web page. */ public void drive() { int delay = 0; Clock clock1 = new Clock(); String HWSN = TINIOS.getTINISerialNumber(); System.out.println("HWSN: " + HWSN); System.out.println(); try { // create the web server Thread webServer = new Thread(webWorker); webServer.setName("Web Server"); // create the temperature server Thread temperatureServer = new Thread(temperatureWorker); temperatureServer.setName("Temperature Server"); webRoot = webWorker.getWebRoot(); webIndex = webWorker.getWebPage(); // start the web server webServer.start(); // start the temperature server temperatureServer.start(); // periodically check the temperature and update the web page while (true) { // Thread.sleep(5000); System.gc(); clock1.getRTC(); System.out.println("Date: " + clock1.getMonth() + "/" + clock1.getDate() + "/" + clock1.getYear() + " " + clock1.getHour() + ":" + clock1.getMinute() + ":" + clock1.getSecond() + "." + clock1.getHundredth() + (clock1.getPm() ? " PM" : " AM")); delay = (60 - clock1.getSecond()) * 1000; // System.out.println("clock1.getSecond() = " + clock1.getSecond()); Thread.sleep(delay); System.out.println("delay was " + delay); updateTemperature(temperatureWorker.getCurrentTemperature()); } } catch(Throwable t) { ByteArrayOutputStream out = new ByteArrayOutputStream (); t.printStackTrace (new PrintStream (out)); System.out.println("createPageLocal -"+t.toString()); // why kill the server if the exception is not fatal? //System.out.println(t); } } /** Create a TINIWebServer. */ public static void main(String[] args) { System.out.println("Starting TINI WebServer version 2.0 ..."); /* com.dalsemi.system.Clock C = new com.dalsemi.system.Clock(); long time = C.getTickCount(); System.out.println(new java.util.Date(time)); C.setYear(2001); C.setMonth(10); C.setDate(13); C.set12Hour(false); C.setHour(6); C.setMinute(29); C.setRTC(); System.out.println(new java.util.Date(C.getTickCount())); */ try { int currentArg = 0; TINIWebServer tiniWebServer = new TINIWebServer(); tiniWebServer.webWorker = new WebWorker(tiniWebServer.lock); tiniWebServer.temperatureWorker = new TemperatureWorker(); tiniWebServer.drive(); } catch(Throwable t) { ByteArrayOutputStream out = new ByteArrayOutputStream (); t.printStackTrace (new PrintStream (out)); System.out.println(t); System.out.println(t.getMessage()); } finally { System.out.println("\nTINIWebserver exiting..."); } } }