/*
 add comment: What is the purpose of this class?
*/

import java.io.*;
import java.net.*;
import java.util.*;

public class SensorClient {

  /* add comments */
  public static void main(String[] args) throws IOException {
   
    /* add comments */
    if (args.length != 2) {
      System.err.println("Usage: java SensorClient <host name> <port number>");
      System.exit(1);
    }

    /* add comments */
    String hostName = args[0];
    int portNumber = Integer.parseInt(args[1]);
   
    /* add comments */
    Random r = new Random();
    char c = (char)(r.nextInt(26) + 'A');
    int i = r.nextInt(10000);

    /* add comments */
    Sensor mySensor = new Sensor("" + c + i);

    try (
      /* add comments */
      Socket socket = new Socket(hostName, portNumber);

      /* add comments */
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    ) {

      Scanner scan = new Scanner(System.in);

      /* add comments */
      while (true) {

        /* add comments */
        System.out.print("Press Return...");
        scan.nextLine();
       
        /* add comments */
        mySensor.measure();
        mySensor.print();
       
        /* add comments */
        out.println(mySensor.adhocJson());
      }

    } catch (UnknownHostException e) { /* add comments */
      System.err.println("Don't know about host " + hostName);
      System.exit(1);
    } catch (IOException e) { /* add comments */
      System.err.println("Couldn't get I/O for the connection to " + hostName);
      System.exit(1);
    } catch(NoSuchElementException e) {
      System.exit(1);
    }   
  }
}
