#import <Foundation/Foundation.h>
#import "yocto_api.h"

static void usage(const char *exe)
{
    NSLog(@"usage: %s <serial or logical name> [ON/OFF]\n",exe);
    exit(1);
}


int main (int argc, const char * argv[])
{
    NSError *error;
    
    @autoreleasepool {
        // Setup the API to use local USB devices
        if(yRegisterHub(@"usb", &error) != YAPI_SUCCESS) {
            NSLog(@"RegisterHub error: %@", [error localizedDescription]);
            return 1;
        }
        if(argc < 2)
            usage(argv[0]);
        NSString *serial_or_name =[NSString stringWithUTF8String:argv[1]];
        YModule *module = yFindModule(serial_or_name);  // use serial or logical name
        if (module.isOnline) {
            if (argc > 2) {
                if (strcmp(argv[2], "ON")==0) 
                    module.beacon = Y_BEACON_ON;
                else  
                    module.beacon = Y_BEACON_OFF;
            }        
            NSLog(@"serial:       %@\n", module.serialNumber);
            NSLog(@"logical name: %@\n", module.logicalName);
            NSLog(@"luminosity:   %d\n", module.luminosity);
            NSLog(@"beacon:       ");
            if (module.beacon == Y_BEACON_ON)  
               NSLog(@"ON\n");
            else   
               NSLog(@"OFF\n");
            NSLog(@"upTime:       %d sec\n", module.upTime/1000);
            NSLog(@"USB current:  %d mA\n",  module.usbCurrent);
        } else {
            NSLog(@"%@ not connected (check identification and USB cable)\n",serial_or_name);
        }
    }
    return 0;
}
