telefonicaid / iotagent-json

IoT Agent for a JSON based protocol (with HTTP, MQTT and AMQP transports)
https://fiware-iotagent-json.rtfd.io/
GNU Affero General Public License v3.0
49 stars 89 forks source link

Does Fiware/iotagent-json supports Commands and Encryption? #286

Open babbarkrishan opened 6 years ago

babbarkrishan commented 6 years ago

Hi All,

I am working on Smart Light App and using following Fiware GEs

I am able to send events/data on "///attrs" topic which updates the context broker and one can subscribe to the same.

Now I have below queries.

  1. Above case is for Events, means Device/Sensor sending data and it updates the same in Context Broker. I need to do other way also, means sending commands from some applications (say web application) towards Device and status of Device gets changed.

How do we work with Commands in Fiware? I tried /cmd and /cmdexe topics but it did not work. Kindly give steps and example if possible.

  1. Now, my data coming from Device is Base64 encrypted (example given below). If I send the same directly to "/attrs" topic it is not updating attributes values due to encryption. I may add extra layer for decryption and then publish it on "/attrs" topic which would update ehe values in context broker. Is it the right approach? Does Fiware supports Encryption (Mainly Base64)?

Example:

Plain Text: Payload: {"command": "ON_OFF","switchInternalId":"S01", "action":"ON",,"requestId":"123"}

Encrypted Message:

On/Off Message:

{

            "messageId": "onoffmsgId",

            "deviceId": "device-id",

            "commandId": " ON_OFF ",

            "encodingType": "Base64",

"message":"ew0KCSJjb21tYW5kIjogIkVPTyIsDQoJInN3aXRjaEludGVybmFsSWQiOiAiUzAwMDAwMyIsDQoJIMTCIsDQoJInJlcXVlc3RJZCI6ICJFT09fdGVtcFJlcUlkIg0KfQ="

}

Appreciate your help.

Thanks & Regards, Krishan Babbar

fgalan commented 6 years ago

IOTAs and CB will deal with commands sent to devices in an opaque way. As long as you can ensure that the sender (i.e. the Orion client) and the receiver (the device) share the same keys to encode-decode, I think the approach should be feasible.

The only think to pay attention is the set of forbidden chars (see https://fiware-orion.readthedocs.io/en/master/user/forbidden_characters/index.html). You should ensure that your encoded command don't have any of these (= can be problematic).

fermenreq commented 6 years ago

Hi @babbarkrishan,

Please have a look this guide that explain you how works with commands

Could you write your fiware GE versions and mosquitto ? A good way is to start it using docker compose to run mongoDB and Fiware OCB. I’m using the following version:

Image: Mongo 3.2
Image: Fiware/orion image
Mosquitto 1.4.8

Above an example:

1 Create a device:

curl -X POST -H "Fiware-Service: myHome" -H "Fiware-ServicePath: /environment" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
     "devices": [
         {
             "device_id": "sensor03",
             "entity_name": "LivingRoomSensor",
             "entity_type": "multiSensor",
             "attributes": [
                   { "object_id": "t", "name": "Temperature", "type": "celsius" },
                   { "object_id": "c", "name": "Luminosity", "type": "string" }
             ],
                 "commands": [
                                   { "object_id": "l", "name": "setOn", "type": "string", "value":"OFF" }
                        ]
         }
     ]
}' 'http://localhost:4061/iot/devices'  

2 Create a service:

curl -X POST -H "Fiware-Service: myHome" -H "Fiware-ServicePath: /environment" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"services": [
            {
          "resource": "/",
          "apikey": "TEF",
          "type": "multiSensor",
          "cbroker":"172.18.0.3:1026"
            }
            ]
}' 'http://localhost:4061/iot/services'

3 Send Measures:

mosquitto_pub -t /TEF/sensor03/attrs -m '{ "t": 35, "c":yellow}'

4 Requests for OCB

curl -X POST 'http://localhost:7896/iot/d?k=TEF&i=sensor03&getCmd=1' -H "Fiware-Service: myHome"  -H "Fiware-ServicePath: /environment" -H "Content-Type: text/plain" -H "Cache-Control: no-cache" 
-d '{ "t": 35, "c":yellow}'

Note: The apikey must be define in config.js iotAgent file: See bellow

/*
 * Copyright 2015 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-json
 *
 * iotagent-json is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-json is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-json.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[contacto@tid.es]
 */
var config = {};

config.mqtt = {
    host: 'localhost',
    port: 1883,
    thinkingThingsPlugin: true,
    qos: 0,
    retain: false
};

config.iota = {
    logLevel: 'DEBUG',
    timestamp: true,
    contextBroker: {
        host: 'localhost',
        port: '1026'
    },
    server: {
        port: 4041
    },
    deviceRegistry: {
        type: 'mongodb'
    },
    mongodb: {
        host: 'localhost',
        port: '27017',
        db: 'iotagentjson'
    },
    types: {},
    service: 'howtoService',
    subservice: '/howto',
    providerUrl: 'http://localhost:4041',
    deviceRegistrationDuration: 'P1M',
    defaultType: 'Thing',
    defaultResource: '/iot/json'
};

config.configRetrieval = false;
config.defaultKey = '2907204D';
config.defaultTransport = 'MQTT';

module.exports = config;

Please try it and let me know.

Thanks, Fernando

fgalan commented 6 years ago

As a complement to the document @fermenreq cites, this one also describes the IOTA API (including command topics) and can be useful: http://fiware-iot-stack.readthedocs.io/en/master/device_api/index.html