Exemplo n.º 1
0
        @Override
        public void onBridgeConnected(PHBridge b) {
          Log.w(TAG, "Connected to the Bridge.");
          phHueSDK.setSelectedBridge(b);
          phHueSDK.enableHeartbeat(b, PHHueSDK.HB_INTERVAL);
          phHueSDK
              .getLastHeartbeat()
              .put(
                  b.getResourceCache().getBridgeConfiguration().getIpAddress(),
                  System.currentTimeMillis());

          PlatformConfig cfg =
              new PlatformConfig(
                  getBaseContext(),
                  ServiceType.IN_PROC,
                  ModeType.CLIENT_SERVER,
                  "0.0.0.0",
                  0,
                  QualityOfService.LOW);

          OcPlatform.Configure(cfg);

          EntityHandlerHue entitycb = new EntityHandlerHue();
          try {
            resourceHandle =
                OcPlatform.registerResource(
                    "/a/huebulb",
                    "device.light",
                    OcPlatform.DEFAULT_INTERFACE,
                    entitycb,
                    EnumSet.of(ResourceProperty.DISCOVERABLE));
          } catch (OcException e) {
            e.printStackTrace();
          }
        }
Exemplo n.º 2
0
  public void randomLights() {
    PHBridge bridge = phHueSDK.getSelectedBridge();

    List<PHLight> allLights = bridge.getResourceCache().getAllLights();
    Random rand = new Random();

    for (PHLight light : allLights) {
      PHLightState lightState = new PHLightState();
      lightState.setHue(rand.nextInt(MAX_HUE));
      // To validate your lightstate is valid (before sending to the bridge) you can use:
      // String validState = lightState.validateState();
      bridge.updateLightState(light, lightState, listener);
      // bridge.updateLightState(light, lightState); // If no bridge response is required then use
      // this simpler form.
    }
  }
Exemplo n.º 3
0
 @Override
 public void onBridgeConnected(PHBridge b, String username) {
   phHueSDK.setSelectedBridge(b);
   phHueSDK.enableHeartbeat(b, PHHueSDK.HB_INTERVAL);
   phHueSDK
       .getLastHeartbeat()
       .put(
           b.getResourceCache().getBridgeConfiguration().getIpAddress(),
           System.currentTimeMillis());
 }
Exemplo n.º 4
0
 @Override
 public void onConnectionResumed(PHBridge bridge) {
   // Log.v(TAG, "onConnectionResumed "
   // +
   // bridge.getResourceCache().getBridgeConfiguration().getIpAddress());
   phHueSDK
       .getLastHeartbeat()
       .put(
           bridge.getResourceCache().getBridgeConfiguration().getIpAddress(),
           System.currentTimeMillis());
   for (int i = 0; i < phHueSDK.getDisconnectedAccessPoint().size(); i++) {
     if (phHueSDK
         .getDisconnectedAccessPoint()
         .get(i)
         .getIpAddress()
         .equals(bridge.getResourceCache().getBridgeConfiguration().getIpAddress())) {
       phHueSDK.getDisconnectedAccessPoint().remove(i);
     }
   }
 }
Exemplo n.º 5
0
 @Override
 public void onBridgeConnected(final PHBridge b) {
   PHHueSDK phHueSDK = PHHueSDK.getInstance();
   phHueSDK.setSelectedBridge(b);
   phHueSDK.enableHeartbeat(b, PHHueSDK.HB_INTERVAL);
   phHueSDK
       .getLastHeartbeat()
       .put(
           b.getResourceCache().getBridgeConfiguration().getIpAddress(),
           System.currentTimeMillis());
 }
Exemplo n.º 6
0
 @Override
 public void onConnectionResumed(final PHBridge bridge) {
   PHHueSDK phHueSDK = PHHueSDK.getInstance();
   for (int i = 0; i < phHueSDK.getDisconnectedAccessPoint().size(); i++) {
     if (phHueSDK
         .getDisconnectedAccessPoint()
         .get(i)
         .getIpAddress()
         .equals(bridge.getResourceCache().getBridgeConfiguration().getIpAddress())) {
       phHueSDK.getDisconnectedAccessPoint().remove(i);
     }
   }
 }
Exemplo n.º 7
0
        @Override
        public void onAccessPointsFound(List<PHAccessPoint> accessPoint) {
          Log.w(TAG, "Access Points Found. " + accessPoint.size());
          if (accessPoint != null && accessPoint.size() > 0) {
            phHueSDK.getAccessPointsFound().clear();
            phHueSDK.getAccessPointsFound().addAll(accessPoint);
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    adapter.updateData(phHueSDK.getAccessPointsFound());
                  }
                });

            HueSharedPreferences prefs2 = HueSharedPreferences.getInstance(AppContext);
            PHAccessPoint accessPoint2 = (PHAccessPoint) adapter.getItem(0);
            accessPoint2.setUsername(prefs2.getUsername());

            PHBridge connectedBridge = phHueSDK.getSelectedBridge();

            if (connectedBridge != null) {
              String connectedIP =
                  connectedBridge.getResourceCache().getBridgeConfiguration().getIpAddress();
              Log.w(TAG, "Connected IP: " + connectedIP);
              if (connectedIP != null) { // We
                // are
                // already
                // connected
                // here:-
                Log.w(TAG, "Connected IP != null");
                phHueSDK.disableHeartbeat(connectedBridge);
                phHueSDK.disconnect(connectedBridge);
              }
            }
            phHueSDK.connect(accessPoint2);
          }
        }
  /**
   * Called when the activity will start interacting with the user.
   *
   * @param savedInstanceState the bundle object.
   */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createschedule);
    initComponents();
    String lightArray[];
    String groupArray[];

    phHueSDK = PHHueSDK.getInstance(getApplicationContext());
    bridge = phHueSDK.getSelectedBridge();

    recurringSchedules = bridge.getResourceCache().getAllSchedules(true);
    getRecurringSchedulesNames();

    ArrayAdapter<String> scheduleSpinnerAdapter =
        new ArrayAdapter<String>(this, R.layout.light_spinner_item, recurringSchedulesArray);
    scheduleSpinner.setAdapter(scheduleSpinnerAdapter);

    scheduleSpinner.setVisibility(View.VISIBLE);

    lights = bridge.getResourceCache().getAllLights();
    lightArray = phHueSDK.getLightNames(lights);
    if (lightArray.length == 0) {
      rbLightForSchedule.setEnabled(false);
    }

    groups = bridge.getResourceCache().getAllGroups();
    groupArray = phHueSDK.getGroupNames(groups);
    if (groupArray.length == 0) {
      rbGroupForSchedule.setEnabled(false);
    }

    ArrayAdapter<String> lightSpinnerAdapter =
        new ArrayAdapter<String>(this, R.layout.inner_spinner_item, lightArray);
    lightSpinner.setAdapter(lightSpinnerAdapter);
    lightSpinner.setEnabled(false);

    ArrayAdapter<String> groupSpinnerAdapter =
        new ArrayAdapter<String>(this, R.layout.inner_spinner_item, groupArray);
    groupSpinner.setAdapter(groupSpinnerAdapter);
    groupSpinner.setEnabled(false);

    final Calendar c = Calendar.getInstance();
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);

    btnScheduleTime.setOnClickListener(
        new View.OnClickListener() {

          @Override
          public void onClick(View v) {
            TimePickerDialog timePicker =
                new TimePickerDialog(
                    PHUpdateRecurringScheduleActivity.this, mTimeSetListener, mHour, mMinute, true);

            timePicker.show();
          }
        });

    rbLightForSchedule.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            rbLightForSchedule.setChecked(true);
            rbGroupForSchedule.setChecked(false);
            lightSpinner.setEnabled(true);
            groupSpinner.setEnabled(false);
          }
        });

    rbGroupForSchedule.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            rbLightForSchedule.setChecked(false);
            rbGroupForSchedule.setChecked(true);
            lightSpinner.setEnabled(false);
            groupSpinner.setEnabled(true);
          }
        });

    btnScheduleLightState.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            Intent intent =
                new Intent(
                    PHUpdateRecurringScheduleActivity.this,
                    PHUpdateScheduleLightStateActivity.class);
            if (intent != null) {
              startActivity(intent);
            }
          }
        });

    scheduleSpinner.setOnItemSelectedListener(
        new OnItemSelectedListener() {

          @Override
          public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            PHSchedule timer = recurringSchedules.get(position);
            editTvScheduleName.setText(timer.getName());
            if (timer.getLightIdentifier() != null) {
              rbLightForSchedule.setChecked(true);
              lightSpinner.setEnabled(true);
              String lightIdentifier = timer.getLightIdentifier();
              PHLight light = null;
              for (int i = 0; i < lights.size(); i++) {
                if (lightIdentifier.equals(lights.get(i).getIdentifier())) {
                  light = lights.get(i);
                  break;
                }
              }
              if (light != null) {
                lightSpinner.setSelection(PHHelper.getIndex(lightSpinner, light.getName()));
                phHueSDK.setCurrentLightState(timer.getLightState());
                rbGroupForSchedule.setChecked(false);
                groupSpinner.setEnabled(false);
              }
            } else if (timer.getGroupIdentifier() != null) {
              rbGroupForSchedule.setChecked(true);
              groupSpinner.setEnabled(true);
              String groupIdentifier = timer.getGroupIdentifier();

              PHGroup group = null;
              for (int i = 0; i < groups.size(); i++) {
                if (groupIdentifier.equals(groups.get(i).getIdentifier())) {
                  group = groups.get(i);
                  break;
                }
              }
              if (group != null) {
                groupSpinner.setSelection(PHHelper.getIndex(groupSpinner, group.getName()));
                phHueSDK.setCurrentLightState(timer.getLightState());
                rbLightForSchedule.setChecked(false);
                lightSpinner.setEnabled(false);
              }
            }

            Date lastScheduleTime = timer.getDate();
            if (lastScheduleTime != null) {
              mHour = lastScheduleTime.getHours();
              mMinute = lastScheduleTime.getMinutes();
              timeToSend = Calendar.getInstance().getTime();
              timeToSend.setHours(mHour);
              timeToSend.setMinutes(mMinute);
              updateDisplay();
            }

            editTvScheduleDescriptor.setText(timer.getDescription());
            editTvRandomTime.setText(Integer.toString(timer.getRandomTime()));

            recurringDays = timer.getRecurringDays();
            recurringDaysBitStr =
                String.format("%07d", new BigInteger(Integer.toBinaryString(recurringDays)));

            btnSun.setChecked(false);
            btnSat.setChecked(false);
            btnFri.setChecked(false);
            btnThur.setChecked(false);
            btnThur.setChecked(false);
            btnWed.setChecked(false);
            btnTue.setChecked(false);
            btnMon.setChecked(false);

            for (int i = 0; i < recurringDaysBitStr.length(); i++) {
              switch (i) {
                case 0:
                  if (recurringDaysBitStr.charAt(0) == '1') {

                    recurringDays = (recurringDays | RecurringDay.RECURRING_MONDAY.getValue());
                    btnMon.setChecked(true);
                  }

                  break;
                case 1:
                  if (recurringDaysBitStr.charAt(1) == '1') {
                    recurringDays = (recurringDays | RecurringDay.RECURRING_TUESDAY.getValue());
                    btnTue.setChecked(true);
                  }
                  break;
                case 2:
                  if (recurringDaysBitStr.charAt(2) == '1') {
                    recurringDays = (recurringDays | RecurringDay.RECURRING_WEDNESDAY.getValue());
                    btnWed.setChecked(true);
                  }
                  break;
                case 3:
                  if (recurringDaysBitStr.charAt(3) == '1') {
                    recurringDays = (recurringDays | RecurringDay.RECURRING_THURSDAY.getValue());
                    btnThur.setChecked(true);
                  }
                  break;
                case 4:
                  if (recurringDaysBitStr.charAt(4) == '1') {
                    recurringDays = (recurringDays | RecurringDay.RECURRING_FRIDAY.getValue());
                    btnFri.setChecked(true);
                  }
                  break;
                case 5:
                  if (recurringDaysBitStr.charAt(5) == '1') {
                    recurringDays = (recurringDays | RecurringDay.RECURRING_SATURDAY.getValue());
                    btnSat.setChecked(true);
                  }
                  break;
                case 6:
                  if (recurringDaysBitStr.charAt(6) == '1') {
                    recurringDays = (recurringDays | RecurringDay.RECURRING_SUNDAY.getValue());
                    btnSun.setChecked(true);
                  }
                  break;
                default:
                  break;
              }
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
          }
        });
  }
  /** Updates recurring schedule. */
  private void updateRecurringScheudle() {
    int pos = scheduleSpinner.getSelectedItemPosition();
    PHSchedule schedule = recurringSchedules.get(pos);

    String name = editTvScheduleName.getText().toString().trim();
    if (name.length() != 0) {
      schedule.setName(name);
    }

    if (timeToSend == null || recurringDays == 0) {
      PHWizardAlertDialog.showErrorDialog(
          PHUpdateRecurringScheduleActivity.this,
          getResources().getString(R.string.txt_empty_time),
          R.string.btn_ok);
      return;
    } else {
      schedule.setDate(timeToSend);
      schedule.setRecurringDays(recurringDays);
    }

    String lightIdentifier = null;
    String groupIdentifier = null;
    if (rbLightForSchedule.isChecked()) {
      int lightPos = lightSpinner.getSelectedItemPosition();
      PHLight light = lights.get(lightPos);
      lightIdentifier = light.getIdentifier();
      schedule.setLightIdentifier(lightIdentifier);
      schedule.setGroupIdentifier(null);

    } else if (rbGroupForSchedule.isChecked()) {
      int groupPos = groupSpinner.getSelectedItemPosition();
      PHGroup group = groups.get(groupPos);
      groupIdentifier = group.getIdentifier();
      schedule.setGroupIdentifier(groupIdentifier);
      schedule.setLightIdentifier(null);
    }

    if (stateToSend != null) {
      schedule.setLightState(stateToSend);
    }

    String description = editTvScheduleDescriptor.getText().toString().trim();

    if (description.length() != 0) {
      schedule.setDescription(description);
    }

    String timerRandomTime = editTvRandomTime.getText().toString().trim();
    if (timerRandomTime.length() != 0) {
      schedule.setRandomTime(Integer.parseInt(timerRandomTime));
    }

    final PHWizardAlertDialog dialogManager = PHWizardAlertDialog.getInstance();
    dialogManager.showProgressDialog(
        R.string.sending_progress, PHUpdateRecurringScheduleActivity.this);

    bridge.updateSchedule(
        schedule,
        new PHScheduleListener() {

          @Override
          public void onSuccess() {

            dialogManager.closeProgressDialog();
            PHWizardAlertDialog.showResultDialog(
                PHUpdateRecurringScheduleActivity.this,
                getString(R.string.txt_timer_updated),
                R.string.btn_ok,
                R.string.txt_result);
          }

          @Override
          public void onStateUpdate(
              Hashtable<String, String> successAttribute, List<PHHueError> errorAttribute) {
            // TODO Auto-generated method stub
          }

          @Override
          public void onError(int code, String msg) {
            dialogManager.closeProgressDialog();
            Log.v(TAG, "onError : " + code + " : " + msg);
            PHWizardAlertDialog.showErrorDialog(
                PHUpdateRecurringScheduleActivity.this, msg, R.string.btn_ok);
          }
        });
  }