Difference between revisions of "Simple Switch App"

From SecuriWiki
Jump to: navigation, search
m
m (added 2 more includes and complete switch.cpp file)
Line 45: Line 45:
 
   }
 
   }
 
}
 
}
 +
</pre></code>
 +
 +
In order to resolve an "atoi" error during compile, add two more includes, stdio.h and stdlib.h.
 +
The complete modified switch.cpp file is shown below.
 +
 +
<code><pre>
 +
// add these 2 includes to avoid atoi error
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
 +
#include <stdarg.h>
 +
#include <iostream>
 +
#include <string>
 +
#include <string.h>
 +
#include <fstream>
 +
#include <sstream>
 +
#include <sys/file.h>
 +
#include <errno.h>
 +
#include <fcntl.h>
 +
#include <unistd.h>
 +
#include <libAlmondHA.h>
 +
 +
void callback(Device* dev);
 +
//replace main proc with this new main proc from securifi wiki
 +
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();
 +
  }
 +
}
 +
 +
void callback(Device* dev) {
 +
    fprintf(stderr, "Device Created Callback: %s\n",dev->getDeviceName());
 +
    fprintf(stderr, "Index: %s, Value: %s\n", dev->getLastNotificationIndexName(), dev->getLastNotificationIndexValue());
 +
}
 +
 +
 
</pre></code>
 
</pre></code>

Revision as of 23:10, 19 June 2015

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();
  }
}

In order to resolve an "atoi" error during compile, add two more includes, stdio.h and stdlib.h. The complete modified switch.cpp file is shown below.

// add these 2 includes to avoid atoi error
#include <stdio.h>
#include <stdlib.h>

#include <stdarg.h>
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <sstream>
#include <sys/file.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <libAlmondHA.h>

void callback(Device* dev);
//replace main proc with this new main proc from securifi wiki
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();
  }
}

void callback(Device* dev) {
    fprintf(stderr, "Device Created Callback: %s\n",dev->getDeviceName());
    fprintf(stderr, "Index: %s, Value: %s\n", dev->getLastNotificationIndexName(), dev->getLastNotificationIndexValue());
}