#!/usr/bin/env node

var readline = require('readline'),
    iotAgentLib = require('../lib/fiware-iotagent-lib'),
    config = require('../config'),
    separator = '\n\n\t';

var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

var commands = {
    'start': {
        parameters: [],
        description: '\tStart the IoT Agent',
        handler: startApp
    },
    'stop': {
        parameters: [],
        description: '\tStop the IoT Agent',
        handler: stopApp
    },
    'register': {
        parameters: ['id', 'type', 'attributes'],
        description: '\tRegister a new device in the IoT Agent. The attributes should be triads with the following\n' +
            '\tformat: (name type id) sepparated by commas.',
        handler: registerDevice
    },
    'unregister': {
        parameters: ['id', 'type'],
        description: '\tUnregister the selected device',
        handler: unregisterDevice
    },
    'updatevalue': {
        parameters: [],
        description: '\tUpdate a device value in the Context Broker',
        handler: updateDeviceValue
    }
};

function handleError(message) {
    return function (error) {
        if (error) {
            console.log('\n\033[31mERROR:\033[0m %s', error);
        } else {
            console.log(message)
        }

        rl.prompt();
    };
}

function startApp() {
    iotAgentLib.activate(config, handleError('Application started'));
}

function stopApp() {
    iotAgentLib.activate(handleError('Application stopped'));
}

function registerDevice() {
    console.log('Registering device');
}

function unregisterDevice() {
    console.log('Unregistering device');
}

function updateDeviceValue() {
    console.log('Updating device value');
}

function initialize() {

}

function showHelp() {
    var keyList = Object.keys(commands);

    console.log('\n');
    
    for (var i = 0; i < keyList.length; i++) {
        var parameters = '';

        for (var j = 0; j < commands[keyList[i]].parameters.length; j++) {
            parameters += '<' + commands[keyList[i]].parameters[j] + '> ';
        }

        console.log("%s %s \n\n%s\n", keyList[i], parameters, commands[keyList[i]].description);
    }

    rl.prompt();
}

function executeCommander(command) {
    if (command[0]=='help') {
        showHelp();
    } else if (commands[command[0]]) {
        commands[command[0]].handler();
    } else if (command[0].length == 0) {
        rl.prompt();
    } else {
        console.log('Unrecognized command');
        rl.prompt();
    }
}

rl.setPrompt('\033[36mIoTAgent> \033[0m');
rl.prompt();

rl.on('line', function (cmd) {
    executeCommander(cmd.split(' '));
});



@@ -138,24 +138,54 @@
 ./config'),%0A
+    async = require('async'),%0A
     separato
@@ -758,22 +758,25 @@
 at: 
-(
+%22
 name
- 
+/
 type
- id)
+/value%22
  sep
@@ -1023,32 +1023,70 @@
    parameters: %5B
+'deviceId', 'deviceType', 'attributes'
 %5D,%0A        descr
@@ -1139,16 +1139,136 @@
 t Broker
+. The attributes should be triads with the following%5Cn' +%0A            '%5Ctformat: %22name/type/value%22 sepparated by commas.
 ',%0A     
@@ -1540,24 +1540,533 @@
 ;%0A    %7D;%0A%7D%0A%0A
+function extractAttributes(attributeString, callback) %7B%0A    var attributes = attributeString.split(','),%0A        attributesResult = %5B%5D;%0A%0A    for (var i = 0; i %3C attributes.length; i++) %7B%0A        var fields = attributes%5Bi%5D.split('/'),%0A            attribute = %7B%0A                name: fields%5B0%5D,%0A                type: fields%5B1%5D%0A            %7D;%0A%0A%0A        if (fields%5B2%5D) %7B%0A            attribute.value = fields%5B2%5D;%0A        %7D%0A%0A        attributesResult.push(attribute);%0A    %7D%0A%0A    callback(null, attributesResult);%0A%7D%0A%0A
 function sta
@@ -2067,24 +2067,31 @@
 on startApp(
+command
 ) %7B%0A    iotA
@@ -2172,16 +2172,23 @@
 stopApp(
+command
 ) %7B%0A    
@@ -2264,32 +2264,39 @@
  registerDevice(
+command
 ) %7B%0A    console.
@@ -2314,32 +2314,217 @@
 ering device');%0A
+    async.waterfall(%5B%0A        async.apply(extractAttributes, command%5B2%5D),%0A        async.apply(iotAgentLib.register, command%5B0%5D, command%5B1%5D)%0A    %5D, handleError('Device value updated'));%0A
 %7D%0A%0Afunction unre
@@ -2536,16 +2536,23 @@
 rDevice(
+command
 ) %7B%0A    
@@ -2618,24 +2618,31 @@
 ceValue(
+command
 ) %7B%0A    
 console.
@@ -2637,31 +2637,163 @@
     
-console.log('Updating d
+async.waterfall(%5B%0A        async.apply(extractAttributes, command%5B2%5D),%0A        async.apply(iotAgentLib.update, command%5B0%5D, command%5B1%5D)%0A    %5D, handleError('D
 evic
@@ -2799,17 +2799,26 @@
 ce value
-'
+ updated')
 );%0A%7D%0A%0Afu
@@ -2930,20 +2930,16 @@
 ('%5Cn');%0A
-    
 %0A    for
@@ -3456,16 +3456,32 @@
 handler(
+command.slice(1)
 );%0A    %7D

ADD First version of the console client