Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Merge branch 'feature/auto_hub_creation' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrokyrychuk committed Sep 18, 2015
2 parents 0f64305 + 5518f61 commit 94e81f9
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
69 changes: 57 additions & 12 deletions app/scripts/controllers/device-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

(function() {
'use strict';
/* global URI */

angular
.module('siteApp')
.controller('DeviceBusController', DeviceBusController);

function DeviceBusController($window, $log, $scope, $filter, $modalInstance,
notifyUser, extractNumberFromName, getBoardPins, initialState, deviceInfo,
editMode, serialPortsList, transportsList, wizardMode, boardInfo) {
function DeviceBusController($log, $scope, $filter, $modalInstance,
dataService, notifyUser, extractNumberFromName, getBoardPins, initialState,
deviceInfo, editMode, serialPortsList, transportsList, wizardMode,
boardInfo) {

var vm = this;

Expand All @@ -35,10 +37,11 @@
vm.bus = initialState;
vm.busOptions = {};
vm.serialInputType = 'serial';
vm.watchCancelers = {};
vm.wizardMode = wizardMode;
vm.editMode = editMode;

vm.willBeConnectedDirectlyToCore = false;

$scope.$watch('vm.transport', function (newValue) {
if (angular.isUndefined(newValue)) {
return;
Expand All @@ -63,6 +66,13 @@
}
});

$scope.$watch('vm.selectedSerialPort', function (newValue) {
if (angular.isUndefined(newValue)) {
return;
}
vm.serialPort = newValue.port;
});

vm.transport = getTransportById('serial');

// Assigning default option values
Expand All @@ -74,6 +84,7 @@
// handlers
vm.save = save;
vm.cancel = cancel;
vm.hubMayBeAdded = hubMayBeAdded;

////////////

Expand All @@ -83,20 +94,50 @@
vm.busOptions[spec.name].value : vm.busOptions[spec.name];
});

$modalInstance.close(vm.bus);
// Add hub
if (vm.willBeConnectedDirectlyToCore && vm.hubMayBeAdded()) {
var hubConnection = new URI({protocol: 'serial', path: vm.serialPort})
.addQuery('baudrate', vm.bus.options['baudrate'])
.toString();
dataService.hubs().get().$promise
.then(function(hubsList) {
var hubIsUnique = true;
angular.forEach(hubsList.items, function(hub) {
hubIsUnique = hubIsUnique && hubConnection !== hub.connection;
});
if (hubIsUnique) {
hubsList.items.push({
'connection': hubConnection,
'enabled': true,
});
return hubsList.$save()
.then(function() {
notifyUser('success', 'Hub for ' + vm.bus.name +
' bus has been successfully added.');
closeModalWindow();
}, function(data) {
notifyUser('error', ('An unexpected error occurred when ' +
'updating hubs list (' + data.data + ')'));
});
} else {
notifyUser('warning', 'Hub with same connection attribute is ' +
'already exists.');
}
});

} else {
closeModalWindow();
}

function closeModalWindow() {
$modalInstance.close(vm.bus);
}
}

function cancel() {
$modalInstance.dismiss('cancel');
}

function onSelectedSerialPortChanged(newValue) {
if (angular.isUndefined(newValue)) {
return;
}
vm.path = newValue.port;
}

function getTransportById(id) {
for (var i = 0; i < vm.transports.length; i++) {
if (vm.transports[i].id === id) {
Expand All @@ -120,6 +161,10 @@
return optionValue;
}

function hubMayBeAdded() {
return !vm.editMode && vm.transport.id === 'serial';
}

}

})();
44 changes: 43 additions & 1 deletion app/views/device-bus.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,54 @@ <h3 class="modal-title">Communication Interface</h3>
</select>
</div>

<div ng-show="vm.hubMayBeAdded()">
<hr>
<div class="form-group" show-form-errors>
<!-- TODO: link to docs describing what this option means -->
<label class="control-label">
Will this device be connected to the machine running SmartAnthill
directly via Connection Interface you are currently creating (used as
SmartAnthill Hub)?
</label>
<select name="enabled" class="form-control" ng-model="vm.willBeConnectedDirectlyToCore"
ng-options="option.value as option.name for option in [{name: 'Yes', value: true}, {name: 'No', value: false}]">
</select>
</div>

<div ng-show="vm.willBeConnectedDirectlyToCore">
<p>Please, assign the serial port current device will be connected to.</p>
<div class="form-group">
<label>Port: </label>
<div class="btn-group">
<label class="btn btn-default" ng-model="vm.serialInputType" btn-radio="'serial'">Available Serial Ports</label>
<label class="btn btn-default" ng-model="vm.serialInputType" btn-radio="'manual'">Manual</label>
</div>
</div>

<div class="form-group" ng-show="vm.serialInputType === 'serial'">
<label class="control-label">Select available serial port</label>
<ui-select ng-model="vm.selectedSerialPort" theme="bootstrap">
<ui-select-match required placeholder="Select or search a serial port in the list...">{{ $select.selected.port }}</ui-select-match>
<ui-select-choices repeat="item in vm.serialports | filter:$select.search">
<div ng-bind-html="item.port | highlight: $select.search"></div>
<small ng-bind-html="item.description | highlight: $select.search"></small>
</ui-select-choices>
</ui-select>
</div>

<div class="form-group" ng-show="vm.serialInputType === 'manual'">
<label class="control-label">Manual port configuration:</label>
<input type="text" name="manual" class="form-control" maxlength="100" ng-model="vm.serialPort">
</div>
</div>
</div>

</form>

</div>

<div class="modal-footer">
<button class="btn btn-link" ng-click="vm.cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="vm.save()"
ng-disabled="hubForm.$invalid">Save</button>
ng-disabled="hubForm.$invalid || (vm.willBeConnectedDirectlyToCore && !vm.serialPort)">Save</button>
</div>

0 comments on commit 94e81f9

Please sign in to comment.