Simple Switch App

From SecuriWiki
Revision as of 14:32, 21 September 2014 by matt (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The code in SDK_Setup_Guide_-_Almond+_2014 for the sample switch app does not quite work as is, but the adjustment is fairly simple. Replace the main function with the following (note ~2 differences):

switch.cpp:

main(int argc, char **argv) {

  HADevices::initialize();
  HADevices::genericCallback(&callback);

  if(argc <= 1 || argc >3)
  {
    //print usage
    fprintf(stderr, "usage: ./almond/switch  Devid  state\n");
    fprintf(stderr, "       ./almond/switch <DevID> <1/0>\n");
    fprintf(stderr, "* example to turn on switch with DevId 2 : ./almond/switch 2 1\n");
    fprintf(stderr, "* go to device manager to see switch DevID ex: sensor #devid\n");
    fprintf(stderr, "* execute binary from / directory\n");
    return -1;
  }

  HADevices * ha = new HADevices;
  Device *sw;

  try {
    sw = ha->getDeviceByID(1);
    printf("Device %d found with name %s\n", sw->getID(), sw->getDeviceName());
  }catch (int exeption) {
    printf("No device found with that devid\n");
    return -1;
  }
  if(atoi(argv[2]))
  {
    fprintf(stderr, "Toggling switch on");
    sw->setSwitchOn(true);
  }
  else
  {
    fprintf(stderr, "Toggling switch off");
    sw->setSwitchOn(false);
  }

  while (1) {
    pause();
  }
}