Esempio n. 1
0
  private void startSyncSequence() {
    //
    // Initialize sensor device with current
    // goal data and current time.
    //

    parent.gattSchedule.add(new BlueTooth.GattAction(getSetUserSettingWithGoal()));

    //
    // Read todays stuff.
    //

    BlueTooth.GattAction ga = new BlueTooth.GattAction();

    ga.mode = BlueTooth.GattAction.MODE_READ;
    ga.characteristic = parent.currentSecondary;

    parent.gattSchedule.add(ga);

    parent.fireNext(false);

    //
    // Get sync status.
    //

    syncStatus = HealthData.getStatus("sensor");

    //
    // Compute time stuff for step day history records.
    //

    long lastSavedDay = 0;

    if (syncStatus.has("lastSavedDay")) {
      lastSavedDay = Simple.getTimeStamp(Json.getString(syncStatus, "lastSavedDay"));
      lastSavedDay /= 1000L;
    }

    //
    // Get no more than ten days back.
    //

    Calendar calendar = Calendar.getInstance();

    calendar.setTime(new Date());
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    long today = calendar.getTimeInMillis();
    today /= 1000L;

    long tendays = 86400 * 10;

    long todoSavedDay = ((today - lastSavedDay) > tendays) ? (today - tendays) : lastSavedDay;

    //
    // Schedule activity days.
    //

    while (todoSavedDay < today) {
      todoSavedDay += 86400;

      int day = (int) ((today - todoSavedDay) / 86400);

      Log.d(LOGTAG, "startSyncSequence: schedule day:" + day);

      parent.gattSchedule.add(new BlueTooth.GattAction(getStepHistoryData(day)));
    }

    //
    // Schedule sleep data positions.
    //

    long lastSavedAct = 0;

    if (syncStatus.has("lastSavedAct")) {
      lastSavedAct = Simple.getTimeStamp(Json.getString(syncStatus, "lastSavedAct"));
      lastSavedAct /= 1000L;
    }

    long now = new Date().getTime();

    calendar.setTimeInMillis(now);
    calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE) / 10) * 10);
    calendar.set(Calendar.SECOND, 0);

    long nowtenminutes = calendar.getTimeInMillis() / 1000L;

    calendar.set(Calendar.HOUR_OF_DAY, (calendar.get(Calendar.HOUR_OF_DAY) / 3) * 3);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    long nowthreehours = calendar.getTimeInMillis() / 1000L;

    int startposition = (int) ((nowtenminutes - nowthreehours) / (10 * 60));
    int lastposition = startposition + (10 * 8 * 18);

    while (lastposition >= startposition) {
      long positiontime = nowtenminutes - (lastposition * 10 * 60);

      if (positiontime >= lastSavedAct) {
        Log.d(LOGTAG, "startSyncSequence: schedule position:" + lastposition);

        parent.gattSchedule.add(new BlueTooth.GattAction(getSleepHistoryData(lastposition)));
      }

      lastposition -= 18;
    }

    //
    // Disconnect after sync.
    //

    parent.gattSchedule.add(new BlueTooth.GattAction(BlueTooth.GattAction.MODE_DISCONNECT));

    parent.fireNext(false);
  }