Exemplo n.º 1
0
  public final void refresh() {
    if (dataManager != null) {
      try {
        groupGeofencesLock.writeLock().lock();
        deviceGeofencesLock.writeLock().lock();
        try {
          groupGeofences.clear();
          for (GroupGeofence groupGeofence : dataManager.getGroupGeofences()) {
            getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId());
          }

          deviceGeofences.clear();
          deviceGeofencesWithGroups.clear();

          for (DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) {
            getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId())
                .add(deviceGeofence.getGeofenceId());
            getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId())
                .add(deviceGeofence.getGeofenceId());
          }

          for (Device device : dataManager.getAllDevicesCached()) {
            long groupId = device.getGroupId();
            while (groupId != 0) {
              getDeviceGeofences(deviceGeofencesWithGroups, device.getId())
                  .addAll(getGroupGeofences(groupId));
              if (dataManager.getGroupById(groupId) != null) {
                groupId = dataManager.getGroupById(groupId).getGroupId();
              } else {
                groupId = 0;
              }
            }
            List<Long> deviceGeofenceIds = device.getGeofenceIds();
            if (deviceGeofenceIds == null) {
              deviceGeofenceIds = new ArrayList<>();
            } else {
              deviceGeofenceIds.clear();
            }
            Position lastPosition = Context.getConnectionManager().getLastPosition(device.getId());
            if (lastPosition != null && deviceGeofencesWithGroups.containsKey(device.getId())) {
              for (long geofenceId : deviceGeofencesWithGroups.get(device.getId())) {
                Geofence geofence = getGeofence(geofenceId);
                if (geofence != null
                    && geofence
                        .getGeometry()
                        .containsPoint(lastPosition.getLatitude(), lastPosition.getLongitude())) {
                  deviceGeofenceIds.add(geofenceId);
                }
              }
            }
            device.setGeofenceIds(deviceGeofenceIds);
          }

        } finally {
          groupGeofencesLock.writeLock().unlock();
          deviceGeofencesLock.writeLock().unlock();
        }

      } catch (SQLException error) {
        Log.warning(error);
      }
    }
  }