コード例 #1
0
    public void run() {
      registerDriver();
      dbConnection = establishConnection();

      // #1 security EU. Plz no stealerino.
      String apiKey = "26ef2b98aa2077410020408feb29cde8";

      owmap = new OpenWeatherMap(apiKey);
      System.out.println("created OpenWeatherMap instance");

      while (true) {
        if (client != null) {
          client.requestAllData();
          try {
            client.requestWindSpeed();
          } catch (Exception e1) {
            System.out.println("Failed to request wind speed.");
            e1.printStackTrace();
          }
        }
        // Not sure if needed
        //				else {
        //					System.out.println("SensorClient unreachable.");
        //					if (!reconnectorRunning) {
        //						SCReconnector reconnector = new SCReconnector();
        //						reconnector.start();
        //					}
        //				}
        System.out.println("---------------------------------------");
        Date date = new Date();
        System.out.println(date.toString());
        CurrentWeather currentWeather = null;
        try {
          weathermapAccessible = true;
          currentWeather = getCurrentWeatherWithTimeout(currentWeather);
          System.out.println("Received current Weather from OpenWeatherMap.");
        } catch (ExecutionException e) {
          System.out.println("OpenWeatherMap appears to be inaccessible.");
          weathermapAccessible = false;
          // e.printStackTrace();
        } catch (InterruptedException e) {
          weathermapAccessible = false;
          System.out.println("Request interrupted while waiting for response.");
          // e.printStackTrace();
        } catch (TimeoutException e) {
          weathermapAccessible = false;
          System.out.println(
              "Did not receive response from openweathermap within "
                  + WEATHER_REQUEST_TIMEOUT
                  + " seconds.");
          // e.printStackTrace();
        }
        sensorServerAvailable = true;

        System.out.println("Creating WeatherData object...");

        WeatherData data =
            initializeWeatherDataObject(
                currentWeather, sensorServerAvailable, weathermapAccessible);
        System.out.println("Created WeatherData object");
        String query =
            "INSERT INTO weatherdatalog (temperature, pressure, humidity, owmtemperature, owmpressure, owmhumidity, sensorwindspeed, owmwindspeed, owmwinddegree, light, owmweathername, owmweatherdesc)"
                + " VALUES ("
                + data.getTemp()
                + ", "
                + data.getPressure()
                + ", "
                + data.getHumidity()
                + ", "
                + data.getOwmTemp()
                + ", "
                + data.getOwmPressure()
                + ", "
                + data.getOwmHumidity()
                + ", "
                + data.getSensorWindSpeed()
                + ", "
                + data.getOwmWindSpeed()
                + ", "
                + data.getOwmWindDegree()
                + ", "
                + data.getLight()
                + ", '"
                + data.getOwmName()
                + "', '"
                + data.getOwmDesc()
                + "');";
        try {
          Statement statement = dbConnection.createStatement();
          System.out.println("Executing query...");
          statement.executeUpdate(query);
          System.out.println("Inserted data");
        } catch (SQLException e) {
          e.printStackTrace();
        }

        try {
          Thread.sleep(ACQUIREDATAINTERVAL);
        } catch (InterruptedException e) {
          System.out.println("Sleep was interrupted");
          e.printStackTrace();
        }
      }
    }