Skip to content

Commit

Permalink
deviceState createByDeviceTypeIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Sep 25, 2016
1 parent 4b36ce6 commit b5a4027
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/core/devicestate/deviceState.createByDeviceTypeIdentifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var queries = require('./deviceState.queries.js');

module.exports = function(identifier, service, state){

// we get the devicetype
return gladys.utils.sqlUnique(queries.getDeviceTypeByIdentifierAndService, [identifier, service])
.then(function(deviceType){

// we create the deviceState
state.devicetype = deviceType.id;
return gladys.deviceState.create(state);
});
};
6 changes: 6 additions & 0 deletions api/core/devicestate/deviceState.queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module.exports = {
FROM device
JOIN devicetype ON (device.id = devicetype.device)
WHERE device.identifier = ? AND device.service = ? AND devicetype.type = ?;
`,
getDeviceTypeByIdentifierAndService: `
SELECT devicetype.id
FROM device
JOIN devicetype ON (device.id = devicetype.device)
WHERE devicetype.identifier = ? AND device.service = ?;
`

};
1 change: 1 addition & 0 deletions api/core/devicestate/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

module.exports.create = require('./deviceState.create.js');
module.exports.createByIdentifier = require('./deviceState.createByIdentifier.js');
module.exports.createByDeviceTypeIdentifier = require('./deviceState.createByDeviceTypeIdentifier.js');
module.exports.get = require('./deviceState.get.js');
4 changes: 4 additions & 0 deletions test/fixtures/devicetype.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[
{
"id": 1,
"name": "Switch",
"identifier": "UNIQUE_IDENTIFIER",
"type": "binary",
"tag": "light",
"sensor": false,
Expand All @@ -10,6 +12,7 @@
},
{
"id": 2,
"name": "name",
"type":"multilevel",
"sensor": false,
"tag": "light",
Expand All @@ -20,6 +23,7 @@
},
{
"id": 3,
"name": "other name",
"type": "binary",
"sensor": false,
"device": 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var should = require('should');
var validateDeviceState = require('../../validator/deviceStateValidator.js');

describe('DeviceState', function() {

describe('createByDeviceTypeIdentifier', function() {

it('should return correct deviceState created', function (done) {

var state = {
value: 12.1
};

gladys.deviceState.createByDeviceTypeIdentifier('UNIQUE_IDENTIFIER', 'test', state)
.then(function(result){
validateDeviceState(result);
result.value.should.equal(state.value);
result.devicetype.should.equal(1);
done();
}).catch(function(err){
done(err);
});

});


});

});

0 comments on commit b5a4027

Please sign in to comment.