Application development

Since we are running the Hyperledger environment locally, the application being developed here has to run on the same network as Hyperledger. Given that we're not running it in IBM Cloud/Bluemix, the configuration will be stored in a JSON file, in the same directory that the main .js file of the application will run in.

The content structure of the configuration JSON file is listed here and must be updated with the details that were defined in Chapter 2, Creating Your First IoT Solution:

{
"org": "<your IoT organization id>",
"id": "sample-app",
"auth-key": "<application authentication key>",
"auth-token": "<application authentication token>"
}

The application code receives all of the events that were published by the device and updates the FoodBoxes in the pallet with the temperature gathered:

// Composer Rest Server definitions
var request = require('request');
var UPDATE_BOX_URL = "http://<composer rest server url>:3004/api/UpdateFoodBoxTransportationData"
var UPDATE_PALLET_URL = "http://<composer rest server url>:3004/api/UpdateTransportationData"

// Watson IoT definitions
var Client = require("ibmiotf");
var appClientConfig = require("./application.json");
var appClient = new Client.IotfApplication(appClientConfig);

appClient.connect();]

appClient.on("connect", function () {
appClient.subscribeToDeviceEvents();
});

appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) {
// update food box
// updateFoodBox(payload.temperature);
// update pallet
// updatePallet(payload.temperature);
});

The following code calls the defined transaction in the blockchain network through the Composer REST server:

var updateFoodBox = function (temperature) {
var options = {
uri: UPDATE_BOX_URL,
method: 'POST',
json: {
"$class": "com.packtpublishing.businessnetwork.foodsafety.UpdateFoodBoxTransportationData",
"asset": "resource:com.packtpublishing.businessnetwork.foodsafety.FoodBox#<YOUR FOODBOX ID>",
"locationInformation": {
"$class": "com.packtpublishing.businessnetwork.foodsafety.Location",
"date": "2018-12-24T15:08:27.912Z",
"location": "<LOCATION TYPE>",
"locationIdentifier": "<LOCATION ID>",
"status": "<LOCATION STATUS>"
},
"measurementInformation": {
"$class": "com.packtpublishing.businessnetwork.foodsafety.Measurement",
"date": "2018-12-24T15:08:27.912Z",
"value": 0
}
}
};
}

var updatePallet = function (temperature) {
var options = {
uri: UPDATE_BOX_URL,
method: 'POST',
json: {
"$class": "com.packtpublishing.businessnetwork.foodsafety.UpdateTransportationData",
"asset": "resource:com.packtpublishing.businessnetwork.foodsafety.FoodBoxPallet#<YOUR PALLET ID>",
"locationInformation": {
"$class": "com.packtpublishing.businessnetwork.foodsafety.Location",
"date": "2018-12-24T15:09:02.944Z",
"location": "<LOCATION TYPE>",
"locationIdentifier": "<LOCATION ID>",
"status": "<STATUS>"
},
"measurementInformation": {
"$class": "com.packtpublishing.businessnetwork.foodsafety.Measurement",
"date": "2018-12-24T15:09:02.944Z",
"value": 0
}
}
};
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.198.174