Skip to content

Commit

Permalink
Create or update device & deviceType
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Sep 25, 2016
1 parent b5a4027 commit c945c61
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
40 changes: 16 additions & 24 deletions api/core/device/device.create.js
Expand Up @@ -14,35 +14,27 @@ function create(param) {

if(devices.length){

// if device already exist, we don't create it again
return param;
// if device already exist, we update it
return Device.udpate({id: devices[0].id}, param.device)
.then((rows) => rows[0]);
} else {

// if not, we create the device
return createDevice(param);
return Device.create(param.device);
}
});
}
})
.then((device) => {

function createDevice(param){

// first, we create the device
return Device.create(param.device)
.then(function(device) {
// foreach deviceType, we create it if not exist
return Promise.map(param.types, function(type){
type.device = device.id;
return gladys.deviceType.create(type);
})
.then((types) => {

// we create all the types
return Promise.map(param.types, function(type) {
type.device = device.id;
return DeviceType.create(type);
})
// we return device and types
return {device: device, types: types};
});

// we return the results
.then(function(types) {
var result = {
device: device,
types: types
};
return Promise.resolve(result);
});
});
});
}
34 changes: 32 additions & 2 deletions api/core/devicetype/deviceType.create.js
@@ -1,4 +1,34 @@
const Promise = require('bluebird');
const queries = require('./deviceType.queries.js');

module.exports = function create(type){
return DeviceType.create(type);
};
return deviceTypeExist(type)
.then((exist) => {

// if exist, update
if(exist) {
return DeviceType.update({id: exist.id}, type)
.then((rows) => {
if(rows.length) return rows[0];
else return null;
});
} else {
return DeviceType.create(type);
}
});
};

/**
* Returns true if the deviceType exist
*/
function deviceTypeExist(type){
if(type.device && type.identifier){
return gladys.utils.sql(queries.getByDeviceAndIdentifier, [type.device, type.identifier])
.then((rows) => {
if(rows.length) return rows[0];
else return false;
});
} else {
return Promise.resolve(false);
}
}
1 change: 1 addition & 0 deletions api/core/devicetype/deviceType.queries.js
Expand Up @@ -38,6 +38,7 @@ module.exports = {
JOIN devicetype dt ON (d.id = dt.device)
JOIN room r ON (d.room = r.id);
`,
getByDeviceAndIdentifier: 'SELECT id FROM devicetype WHERE device = ? AND identifier = ?;',
delete : 'DELETE FROM devicetype WHERE id = ?;',
deleteDeviceStates: 'DELETE FROM devicestate WHERE devicetype = ?;'
};
20 changes: 20 additions & 0 deletions test/unit/api/core/devicetype/deviceType.create.test.js
Expand Up @@ -22,6 +22,26 @@ describe('DeviceType', function() {
});

});

it('should update deviceType', function (done) {

var obj = {
device: 1,
identifier: 'UNIQUE_IDENTIFIER',
type: 'multilevel',
min: 0,
max: 1,
sensor: false
};

gladys.deviceType.create(obj)
.then(function(type){
console.log(type);
type.type.should.equal(obj.type);
done();
}).catch(done);

});

});

Expand Down

0 comments on commit c945c61

Please sign in to comment.