Simple Switch App

From SecuriWiki
Revision as of 02:44, 23 June 2015 by SecureComp (Talk | contribs)

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

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


Here is a version with all changes made to allow for command line input as well as console output of the callback info. Comments included.

// add 2 more includes to fix atoi compile error
// using atoi allows you to process command line variables
// if you are hardcoding device information you do not need atoi and can eliminate the sdtio and stdlib includes
#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>

//function callback Device Name and Device ID
void callback(Device* dev);

//function main
main(int argc, char **argv) {

    // Home Automation Device Call - initialize and execute callback
    HADevices::initialize();
    HADevices::genericCallback(&callback);

    // catch error for less than 1 or more than 2 command line parameters, argc is the actual number of command line inputs
    if(argc <= 1 || argc >3)
    {
    
	//print usage description on error catch
	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;
    }

    // Set Device ID prior to executing On/Off command
    Device *sw;

    // verify that Device ID is valid before proceessing on/off set command
    // try catch statement performs check to verify valid Device ID and errors out if not found
    try {
        sw = new Device(atoi(argv[1]));
    }catch (int exeption) {
        printf("No device found with that devid\n");
        return -1;
    }
   
    // if then else block to process Switch On or Off
    // using atoi to use command line input, you could just hardcode a Device ID here if you were working with a specific device with your app
    // if the 2nd command line input variable is 1, set switch to On else set to Off
    if(atoi(argv[2]))
    {
	sw->setSwitchOn(true);
    }
    else
    {
	sw->setSwitchOn(false);
    }
    // while true
    while (1) {
        pause();
    }
}

// The code below will provide a constant callback update to the console of any actions to the device class selected
// You will have to interrupt the output using CTRL-C to stop execution
// Comment out the below code if you do not want to see Device Status callbacks

// callback Device Name and Device ID with info from Device
void callback(Device* dev) {
    // Display name of Device that is providing callback info
    fprintf(stderr, "Device Created Callback: %s\n",dev->getDeviceName());
    // Display Last Notification Index Name and Value for Device
    fprintf(stderr, "Index: %s, Value: %s\n", dev->getLastNotificationIndexName(), dev->getLastNotificationIndexValue());
}