Esempio n. 1
0
  @Override
  public void run() {
    while (true) {
      LatLng currentLocation = GPS.getCurrentLocation();
      Date currentTime = new Date();

      // update the cache if the distance from the last update has equalled or exceeded CACHE_RADIUS
      // or the time from the last update is at least CACHE_TIMEOUT
      if (GPS.calculateDistance(this.lastCachedLocation, currentLocation) >= CACHE_RADIUS
          || currentTime.getTime() - this.lastCachedTime.getTime() >= CACHE_TIMEOUT) {
        try {
          HazardManager.populateHazardSet(ServerInterface.getHazards(currentLocation));
        } catch (IOException ioe) {
          ioe.printStackTrace();
        } catch (JSONException jsone) {
          jsone.printStackTrace();
        }
      }

      // remove hazards from the set of recently warned hazards if at least WARN_DELAY have passed
      for (Iterator<Date> it = this.inactiveHazards.values().iterator(); it.hasNext(); ) {
        Date nextDate = it.next();
        if (currentTime.getTime() - nextDate.getTime() >= WARN_DELAY) it.remove();
      }

      // (I) update distance for active hazards or (II) move the hazard to inactiveHazards if we are
      // more than WARN_DISTANCE away
      synchronized (this.activeHazards) {
        for (Iterator<Hazard> it = this.activeHazards.iterator(); it.hasNext(); ) {
          Hazard h = it.next();
          double distanceFromH = GPS.calculateDistance(h.getLatLong(), currentLocation);
          if (distanceFromH <= WARN_DISTANCE) // (I)
          PebbleSender.send(PebbleMessage.createUpdate(h, (int) distanceFromH));
          else { // (II)
            PebbleSender.send(PebbleMessage.createIgnore(h));
            this.inactiveHazards.put(h, currentTime);
            it.remove();
          }
        }
      }

      // copy hazards to activeHazards if we are at most WARN_DISTANCE away and the hazard is not in
      // inactiveHazards
      for (Hazard h : HazardManager.getHazardSet()) {
        double distanceFromH = GPS.calculateDistance(h.getLatLong(), currentLocation);
        if (distanceFromH <= WARN_DISTANCE && !this.inactiveHazards.keySet().contains(h)) {
          PebbleSender.send(PebbleMessage.createAlert(h, (int) distanceFromH));
          this.activeHazards.add(h);
        }
      }

      try {
        if (runState == RunState.ACTIVE) Thread.sleep(LOOP_DELAY_ACTIVE);
        else Thread.sleep(LOOP_DELAY_INACTIVE);
      } catch (InterruptedException ie) {
        break;
      }
    }
  }
Esempio n. 2
0
 public RunLoop() {
   this.runState = RunState.INACTIVE;
   LatLng currentLocation = GPS.getCurrentLocation();
   try {
     HazardManager.populateHazardSet(ServerInterface.getHazards(currentLocation));
   } catch (IOException ioe) {
     ioe.printStackTrace();
   } catch (JSONException jsone) {
     jsone.printStackTrace();
   }
   this.lastCachedLocation = currentLocation;
   this.lastCachedTime = new Date();
   this.activeHazards = Collections.synchronizedSet(new LinkedHashSet<Hazard>());
   this.inactiveHazards = new LinkedHashMap<Hazard, Date>();
 }