Skip to content

Commit

Permalink
Add possibilities to create device and devicetype in web view
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Sep 24, 2016
1 parent 67f7f81 commit f42348f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 8 deletions.
41 changes: 41 additions & 0 deletions assets/js/app/device/device.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
vm.changeValue = changeValue;
vm.updateDeviceType = updateDeviceType;
vm.changeTypeDisplay = changeTypeDisplay;
vm.createDevice = createDevice;
vm.deleteDevice = deleteDevice;
vm.createDeviceType = createDeviceType;
vm.deleteDeviceType = deleteDeviceType;

vm.saving = false;
vm.devices = [];
vm.rooms = [];
Expand Down Expand Up @@ -65,6 +70,21 @@
vm.rooms = data.data;
});
}

function createDevice(device){
return deviceService.create(device)
.then(function(data){
vm.devices.push(data.data.device);
vm.newDevice = {};
});
}

function deleteDevice(index, id){
return deviceService.deleteDevice(id)
.then(function(){
vm.devices.splice(index, 1);
});
}

function updateDevice(device){
console.log('Updating device ' + device.name);
Expand All @@ -85,6 +105,9 @@

function selectDevice(device){
vm.selectedDevice = device;
vm.newType = {
device: device.id
};
return deviceService.getDeviceTypesDevice(device.id)
.then(function(data){
vm.selectedDevice.types = data.data;
Expand All @@ -104,6 +127,24 @@

});
}

function createDeviceType(deviceType){
deviceType.sensor = deviceType.sensor || false;
return deviceService.createDeviceType(deviceType)
.then(function(data){
vm.selectedDevice.types.push(data.data);
vm.newType = {
device: vm.selectedDevice.id
};
});
}

function deleteDeviceType(index, id){
return deviceService.deleteDeviceType(id)
.then(function(data){
vm.selectedDevice.types.splice(index, 1);
});
}

function changeTypeDisplay(type){
if(type.display) type.display = 0;
Expand Down
20 changes: 12 additions & 8 deletions assets/js/app/device/device.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
updateDevice: updateDevice,
deleteDevice: deleteDevice,
getDeviceTypeByRoom: getDeviceTypeByRoom,
createType: createType,
createDeviceType: createDeviceType,
getDeviceTypesDevice: getDeviceTypesDevice,
getTypes: getTypes,
updateDeviceType: updateDeviceType,
Expand All @@ -41,16 +41,20 @@
return $http({method: 'GET', url: '/device'});
}

function create(device){
return $http({method: 'POST', url: '/device', data: device});
function create(device, types){
var data = {
device: device,
types: types || []
};
return $http({method: 'POST', url: '/device', data: data});
}

function updateDevice(device){
return $http({method: 'PATCH', url: '/device/' + device.id, data: device});
}

function deleteDevice(device){
return $http({method: 'DELETE', url: '/device/' + device.id, data: device});
function deleteDevice(id){
return $http({method: 'DELETE', url: '/device/' + id});
}

function getDeviceTypesDevice(id){
Expand All @@ -63,16 +67,16 @@
return $http({method: 'GET', url: '/devicetype'});
}

function createType(deviceType){
function createDeviceType(deviceType){
return $http({method: 'POST', url: '/devicetype', data: deviceType});
}

function updateDeviceType(deviceType){
return $http({method: 'PATCH', url: '/devicetype/' + deviceType.id, data: deviceType});
}

function deleteDeviceType(deviceType){
return $http({method: 'DELETE', url: '/devicetype/' + deviceType.id, data: deviceType});
function deleteDeviceType(id){
return $http({method: 'DELETE', url: '/devicetype/' + id});
}

function exec(deviceType, value){
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
"devices-control": "Control my devices",
"device-name": "Name",
"device-action": "Action",
"device-create-name": "Name",
"device-create-identifier": "Identifier",
"device-create-protocol": "Protocol",
"device-create-service": "Service",
"device-create-button": "Create",
"notification-update-available-title": "Update available !",
"notification-update-available-text": "New version of Gladys :",
"installation-step": "Step",
Expand Down
5 changes: 5 additions & 0 deletions config/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@
"devices-types-display": "Afficher",
"devices-types-display-no": "Non",
"devices-types-display-yes": "Oui",
"device-create-name": "Nom",
"device-create-identifier": "Identifier",
"device-create-protocol": "Protocol",
"device-create-service": "Service",
"device-create-button": "Créer",
"notification-update-available-title": "Mise à jour disponible !",
"notification-update-available-text": "Nouvelle version de Gladys :",
"installation-step": "Etape",
Expand Down
33 changes: 33 additions & 0 deletions views/device/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,19 @@
<tr>
<th><%= __('ID') %></th>
<th><%= __('Name') %></th>
<th><%= __('Identifier') %></th>
<th><%= __('Protocol') %></th>
<th><%= __('Service') %></th>
<th><%= __('Room') %></th>
<th><%= __('Action') %></th>
<th><%= __('Delete') %></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="device in vm.devices" class="ng-cloak">
<td>{{device.id}}</td>
<td><input type="text" class="form-control" ng-model="device.name" ng-model-options='{ debounce: 500 }' ng-change="vm.updateDevice(device);" /></td>
<td>{{device.identifier}}</td>
<td>{{device.protocol}}</td>
<td>{{device.service}}</td>
<td><!--<select name="selectRoom" id="selectRoom" class="form-control" ng-model="device.room">
Expand All @@ -129,6 +132,22 @@
</select>
</td>
<td><button class="btn btn-primary btn-sm" data-toggle="modal" data-target=".bs-example-modal-lg" ng-click="vm.selectDevice(device);" ><%= __('Edit') %></button></td>
<td><button class="btn btn-danger btn-sm" ng-click="vm.deleteDevice($index, device.id);" ><%= __('Delete') %></button></td>
</tr>
<tr>
<td></td>
<td><input type="text" class="form-control" ng-model="vm.newDevice.name" placeholder="<%= __('device-create-name') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newDevice.identifier" placeholder="<%= __('device-create-identifier') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newDevice.protocol" placeholder="<%= __('device-create-protocol') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newDevice.service" placeholder="<%= __('device-create-service') %>"/></td>
<td>
<select name="selectRoom" id="selectRoom" class="form-control"
ng-options="room.name for room in vm.rooms track by room.id"
ng-model="vm.newDevice.room" >
</select>
</td>
<td></td>
<td><button class="btn btn-success btn-sm" data-toggle="modal" data-target=".create-device-modal" ng-click="vm.createDevice(vm.newDevice);" ><%= __('device-create-button') %></button></td>
</tr>
</tbody>
</table>
Expand Down Expand Up @@ -212,6 +231,7 @@
<th><%= __('devices-types-isensor') %></th>
<th><%= __('devices-types-display') %></th>
<th><%= __('devices-types-lastvalue') %></th>
<th><%= __('Action') %></th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -253,6 +273,19 @@
{{type.lastValue && '<%= __('devices-types-off') %>' || '<%= __('devices-types-on') %>' }}
</button>
</td>
<td><button class="btn btn-sm btn-danger" ng-click="vm.deleteDeviceType($index, type.id);"><%= __('Delete') %></button></td>
</tr>
<tr>
<td></td>
<td><input type="text" class="form-control" ng-model="vm.newType.type" placeholder="<%= __('devices-types-type') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newType.tag" placeholder="<%= __('devices-types-tag') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newType.unit" placeholder="<%= __('devices-types-unit') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newType.min" placeholder="<%= __('devices-types-min') %>" /></td>
<td><input type="text" class="form-control" ng-model="vm.newType.max" placeholder="<%= __('devices-types-max') %>" /></td>
<td><input type="checkbox" class="form-control" ng-model="vm.newType.sensor" /></td>
<td></td>
<td></td>
<td><button class="btn btn-sm btn-success" ng-click="vm.createDeviceType(vm.newType);"><%= __('Create') %></button></td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit f42348f

Please sign in to comment.