package awtweather;
import java.util.Date;
import java.io.*;
//==import javax.comm.*;
import java.net.*;
/**
* Title: Weather Monitor
* Description: This application will read weather data from an Oregon Scientific wm918 and display it in a window
* Copyright: Copyright (c) 2000
* Company: Holiwill
* @author David Rose
* @version 1.0
*/
public class wm918 extends Thread {
public static final int INPUTFILE = 0;
public static final int INPUTPORT = 1;
public static final int INPUTSOCKET = 2;
private boolean Debugging = false;
private boolean Metric = true; // true=metric units, false = imperial
private int buffer[] = new int[128]; // raw data from wm918 goes here
private int inputType; // 0=file, 1=serial, 2=metwork
private File inputFile = null; // file object
//==private SerialPort inputPort = null; // serial port object
private Socket inputSocket = null; // input socket object
private FileInputStream inF = null; // input stream object
private InputStream inP = null;
private DataInputStream inS = null;
// current readings
private Date DateTime = new Date();
private int TemperatureIn, TemperatureOut; // 0.0-50.0C
private int TemperatureFormat; // 0=C or 1=F
private int HumidityIn, HumidityOut; // 10-97%
private int BarometerLocal, BarometerSea; // 795-1050mb
private int Trend, Prediction; // 1,2,4 1,2,4,8
private int BarometerFormat; // 0=in, 1=mm, 2=mb, 3=hpa
private int DewpointIn, DewpointOut; // 0-47C
private int WindDirection, WindSpeed; // 0-359deg, 0.0-56.0ms
private int GustDirection, GustSpeed; // 0-359deg, 0.0-56.0ms
private int Windchill; // -85-60C
private int WindFormat; // 0=mph, 1=knots, 2=m/s, 3=kph
private int RainToday, RainRate; // 0-9999mm, 0-998mm/hr
private int RainYesterday; // 0-9999mm, 0-998mm/hr
private int RainTotal; // 0-9999mm, 0-998mm/hr
private int RainFormat; // 0=mm, 1=in
private int Battery; // 0=battery OK, 1=battery low
private int Power; // 0=AC, 1=DC
// minimum readings
private Date minTemperatureInDateTime = new Date();
private Date minTemperatureOutDateTime = new Date();
private Date minHumidityInDateTime = new Date();
private Date minHumidityOutDateTime = new Date();
private Date minDewpointInDateTime = new Date();
private Date minDewpointOutDateTime = new Date();
private Date minWindSpeedDateTime = new Date();
private Date minWindchillDateTime = new Date();
private int minTemperatureIn, minTemperatureOut; // 0.0-50.0C
private int minHumidityIn, minHumidityOut; // 10-97%
private int minBarometerLocal, minBarometerSea; // 795-1050mb
private int minDewpointIn, minDewpointOut; // 0-47C
private int minWindDirection, minWindSpeed; // 0-359deg, 0.0-56.0ms
private int minGustDirection, minGustSpeed; // 0-359deg, 0.0-56.0ms
private int minWindchill; // -85-60C
// maximum readings
private Date maxTemperatureInDateTime = new Date();
private Date maxTemperatureOutDateTime = new Date();
private Date maxHumidityInDateTime = new Date();
private Date maxHumidityOutDateTime = new Date();
private Date maxDewpointInDateTime = new Date();
private Date maxDewpointOutDateTime = new Date();
private Date maxWindDateTime = new Date();
private Date maxWindchillDateTime = new Date();
private int maxTemperatureIn, maxTemperatureOut; // 0.0-50.0C
private int maxHumidityIn, maxHumidityOut; // 10-97%
private int maxBarometerLocal, maxBarometerSea; // 795-1050mb
private int maxDewpointIn, maxDewpointOut; // 0-47C
private int maxWindDirection, maxWindSpeed; // 0-359deg, 0.0-56.0ms
private int maxGustDirection, maxGustSpeed; // 0-359deg, 0.0-56.0ms
private int maxWindchill; // -85-60C
private String[] Predictions = {"Sunny", "Cloudy", "Partly Cloudy", "Rain", "Unknown"}; //1,2,4,8
private String[] Trends = {"Rising", "Steady", "Falling", "Unknown"}; //1,2,4
private String[] Directions = {" N ", "NNE", "NE ", "ENE", " E ", "ESE", "SE ", "SSE",
" S ", "SSW", "SW ", "WSW", " W ", "WNW", "NW ", "NNW"};
private int[] lengths = { 0, 0, 0, 0, 0, 0, 0, 0, 35, 34, 31, 14, 27, 0, 0, 5 };
public wm918(String fileName, int fileType)
{
//DateTime = 0;
TemperatureIn = 0; TemperatureOut = 0;
HumidityIn = 0; HumidityOut = 0;
BarometerLocal = 0; BarometerSea = 0;
Trend = 0; Prediction = 0;
BarometerFormat = 0;
WindDirection = 0; WindSpeed = 0;
GustDirection = 0; GustSpeed = 0;
RainToday = 0; RainRate = 0;
inputType = fileType;
switch(inputType)
{
case INPUTFILE:
try
{
inputFile = new File(fileName);
inF = new FileInputStream(inputFile);
} catch (Exception e)
{
System.out.println("file error");
e.printStackTrace();
}
break;
case INPUTPORT:
// try
// {
// CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(fileName);
// inputPort = (SerialPort) portID.open("WeatherMonitor", 2000);
// inputPort.setSerialPortParams(9600, 8, 1, 0);
// inP = inputPort.getInputStream();
// } catch (Exception e)
// {
// System.out.println("port error");
// e.printStackTrace();
// }
// break;
case INPUTSOCKET:
try
{
InetAddress addr = InetAddress.getByName(fileName);
inputSocket = new Socket(addr, 9753);
inS = new DataInputStream(inputSocket.getInputStream());
inputSocket.setSoTimeout(20000); // Twenty-second timeout
} catch (IOException e)
{
System.out.println("socket error");
e.printStackTrace();
}
break;
}
}
public void run()
{
int c, length, pos;
while(true)
{
c = 0;
try
{
switch(inputType)
{
case INPUTFILE:
c = inF.read();
break;
case INPUTPORT:
c = inP.read();
break;
case INPUTSOCKET:
c = inS.read();
}
} catch (Exception e)
{
System.out.println("read error");
e.printStackTrace();
}
int p = c >> 4;
length = lengths[p];
buffer[0] = c & 0x0ff;
if(Debugging) System.out.print(" " + Integer.toHexString(c));
pos = 1;
while (pos < length)
{
try
{
switch(inputType)
{
case INPUTFILE:
c = inF.read();
break;
case INPUTPORT:
c = inP.read();
break;
case INPUTSOCKET:
c = inS.read();
}
} catch (Exception e)
{
System.out.println("read error");
e.printStackTrace();
}
buffer[pos++] = c & 0x0ff;
if(Debugging) System.out.print(" " + Integer.toHexString(c));
}
if(Debugging) System.out.println("");
//todo: verify checksum
switch (buffer[0]) // branch off to the appropriate handler
{
case 0x8f: // Humidity
case 0xff: // Date-Time Stamp
get8f();
break;
case 0x9f: // Temperature
get9f();
break;
case 0xaf: // Barometer, Dewpoint
getaf();
break;
case 0xbf: // Rain
getbf();
break;
case 0xcf: // Wind, Windchill
getcf();
break;
}
}
}
// public void stop()
// {
// try
// {
// switch(inputType)
// {
// case INPUTFILE:
// inF.close();
// case INPUTPORT:
// inP.close();
// case INPUTSOCKET:
// inS.close();
// }
// } catch (Exception e)
// {
// System.out.println("close error");
// e.printStackTrace();
// }
// }
public void get8f() // extract humidity
{