This js client allows you to use The Creator IoT Framework, giving the possibility to quickly implement a client, to consume the Device Server REST API.
To install dependencies, just run:
$ npm install
In order to be able to utilize this library you will have to sign up for a creator account. You can do so going through the Creator Developer Console
After signing up, you will need to create an access key and secret pair from the API keys section of the console. These will gain you access to the Creator REST API while using this library.
You can then create an instance of the library as follows:
var creator = require('creator-js-client')('CREATOR_ACCESS_KEY', 'CREATOR_ACCESS_SECRET');
Please note that you can also set the following environment variables for the library to use.
- CREATOR_ACCESS_KEY
- CREATOR_ACCESS_SECRET
and then
var creator = require('creator-js-client')();
Now you should have a working instance of the library that you can use to access your resources RESTfully.
As you may already know, creator rest api is using what is called the HATEOAS REST application structure which means that you need to follow links in order to reach to certain resources. This library will do that for you. Please keep reading!
Using this library you have two main ways of accessing resources.
- Using the raw request method
- Using the service layer functions
Please note that regrdless of the method chosen to access resources, data types of the arguments that you pass in are very important. For example if you are filtering down for the ObjectID 3201 then it needs to be passed in as a String -> '3201' rather than in the form of an integer.
The request method provide a very flexible way of accessing any resource you would like to interact with.
The structure for this method is as follows:
creator.request({
// Options
}, function(data){
// Do something with data
});
Now this method can be used to GET, POST, PUT or DELETE resources on the API.
GET example
creator.request({
steps: ['clients']
}, function(clients){
console.log(clients);
});
This will fetch you an array of your clients talking to the DeviceServer.
PUT example
creator.request({
method: 'PUT',
steps: ['clients', {Name: 'CLIENT_NAME'}, 'objecttypes', {ObjectTypeID: '3201'}, 'instances', {InstanceID: '0'}],
data: {
DigitalOutputState: true
}
}, function(cb){});
When you run this function, it will navigate until it finds the instance at the end of the steps option and set the DigitalOutputState to true.
You can set the following options when calling the request(); method
creator.request({
follow: true, //defaults to true, if this is set to true steps should be provided
method: 'PUT', // defaults to GET
steps: ['accesskeys'], // steps for navigator to follow
data: {
// data if method is set to 'PUT'
},
timeout: 31000, // defaults to 31000
headers: {}, // HTTP request headers are automatically set and we do not recommend overwriting them
relativeEntryPoint: '/clients' // this option can be set if you know where to begin within the Creator DeviceServer API, can be useful to cut down steps
}, function(cb){});
These functions provide a great way of accessing a resource very quickly without dealing with any technical details at all.
An example would be:
creator.clients.getClients();
The callback fed into the function above will be called, with an array of clients that you have talking to our Creator DeviceServer. A typical response example, containing one AwaLWM2 client - "ci40-board":
{
"PageInfo": {
"TotalCount": 1,
"ItemsCount": 1,
"StartIndex": 0
},
"Items": [
{
"Name": "ci40-board",
"Links": [
...
]
}
]
}
- Client Services - via creator.clients
getClients(cb());
getClientByName(clientName, cb());
- Metrics Services - via creator.metrics
getUserMetrics(cb());
getClientMetrics(clientName, cb());
- Object Resource Services - via creator.resources
getClientObjects(clientName, cb());
getClientObject(clientName, objectID, cb());
getClientObjectInstanceByID(clientName, objectID, instanceID, cb());
writeClientObjectInstanceByID(clientName, objectID, instanceID, data, cb());
- Subscription Services - via creator.subscriptions
getUserSubscriptions(cb());
getClientSubscriptions(clientName, cb());
Please note that the high level API is to be expanded to cover more areas of the DeviceServer.
To run creator-node tests first you need to open up the test/config.js file and put a valid pair of access key and secret in it.
Thereafter, you can run the command below and initiate the mocha tests.
$ npm test
If you have any problems installing or utilising the creator node library, please look into our Creator Forum.
Otherwise, If you have come across a nasty bug or would like to suggest new features to be added then please go ahead and open an issue or a pull request against this repo.
Please see the license file for a detailed explanation.
Any Bug fixes and/or feature enhancements are welcome. Please see the contributing file for a detailed explanation.