import java.util.*; import java.io.*; import java.net.*; import java.text.NumberFormat; import WeatherStation; import com.ibutton.tmex.*; import com.ibutton.tmex.adapter.*; import com.ibutton.tmex.container.*; import com.ibutton.tmex.utils.*; public class Console { public static void main(String args[]) { Socket sock = null; ServerSocket servsock = null; PrintStream out = null; DataInputStream in = null; try { servsock = new ServerSocket(5555, 600); } catch (IOException ioe) {} DSPortAdapter adapter = findFirstValidAdapter(); if (adapter != null) { // construct the weather station object on this adapter WeatherStation weather_station = new WeatherStation(adapter); // find weather station devices on adapter if (weather_station.findDevices()) { while (true) { System.out.println("Waiting for client connection..."); try { sock = servsock.accept(); } catch (IOException ioe) {} try { out = new PrintStream(sock.getOutputStream()); in = new DataInputStream(sock.getInputStream()); } catch (IOException ioe) {} // make reading of weather station and check result if (weather_station.makeReading()) { // success so print the reading float temp = weather_station.getTemperature(); float speed = weather_station.getWindSpeed(); out.println(temp); out.println(speed); out.flush(); System.out.println("data: " + temp + ", " + speed); } } } else System.out.println(weather_station.getLastErrorAsString()); // free the port used by the adapter try { System.out.println("Releasing adapter port"); adapter.freePort(); } catch(Exception e) { System.out.println("Exception on freeport: " + e); } } else System.out.println("No valid 1-Wire adapter found"); System.out.println(); System.exit(0); } //------------------------------------------------------------------------- /** Find the first valid ibutton adapter by using the iButton Access * Provider and enumerating all adapter types and ports. Provide * output to standard out during the search. * * @parameter adapter adapter reference to return the valid adapter in * * @return 'true' if a valid adapter was found else 'false' */ public static DSPortAdapter findFirstValidAdapter() { // create an iButton Access Provider iButtonAccessProvider ibap = new iButtonAccessProvider(); // enumerate through each of the adapter classes for(Enumeration adapter_enum = ibap.enumerateAllAdapters(); adapter_enum.hasMoreElements(); ) { // cast the enum as a DSPortAdapter DSPortAdapter adapter = (DSPortAdapter)adapter_enum.nextElement(); String port_name = "COM1"; try { // select the port if (adapter.selectPort(port_name)) System.out.println("Port " + port_name + " selected"); else { System.out.println("Port " + port_name + " could not be selected"); continue; } if (adapter.adapterDetected()) return adapter; } catch(Exception e) { System.out.println("Exception on selectport: " + e); } } // adapter enumeration // valid adapter not found return null; } }