Add import assets and start refactorin

This commit is contained in:
Vladyslav_Prykhodko 2019-06-06 19:43:07 +03:00
parent b3f7fa8b50
commit 15e74792f0
11 changed files with 339 additions and 232 deletions

View File

@ -32,7 +32,8 @@ function AssetService($http, $q, customerService, userService) {
getCustomerAssets: getCustomerAssets,
findByQuery: findByQuery,
fetchAssetsByNameFilter: fetchAssetsByNameFilter,
getAssetTypes: getAssetTypes
getAssetTypes: getAssetTypes,
findByName: findByName
}
return service;
@ -276,4 +277,16 @@ function AssetService($http, $q, customerService, userService) {
return deferred.promise;
}
function findByName(assetName, config) {
config = config || {};
var deferred = $q.defer();
var url = '/api/tenant/assets?assetName=' + assetName;
$http.get(url, config).then(function success(response) {
deferred.resolve(response.data);
}, function fail() {
deferred.reject();
});
return deferred.promise;
}
}

View File

@ -31,7 +31,6 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
getDeviceCredentials: getDeviceCredentials,
getTenantDevices: getTenantDevices,
saveDevice: saveDevice,
saveDeviceParameters: saveDeviceParameters,
saveDeviceCredentials: saveDeviceCredentials,
unassignDeviceFromCustomer: unassignDeviceFromCustomer,
makeDevicePublic: makeDevicePublic,
@ -43,8 +42,9 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
sendOneWayRpcCommand: sendOneWayRpcCommand,
sendTwoWayRpcCommand: sendTwoWayRpcCommand,
findByQuery: findByQuery,
getDeviceTypes: getDeviceTypes
}
getDeviceTypes: getDeviceTypes,
findByName: findByName
};
return service;
@ -172,104 +172,6 @@ function DeviceService($http, $q, $window, userService, attributeService, custom
return deferred.promise;
}
function saveDeviceRelarion(deviceId, deviceRelation, config) {
const deferred = $q.defer();
let attributesType = Object.keys(types.attributesScope);
let allPromise = [];
let promise = "";
if (deviceRelation.accessToken !== "") {
promise = getDeviceCredentials(deviceId.id,null,config).then(function (response){
response.credentialsId = deviceRelation.accessToken;
response.credentialsType = "ACCESS_TOKEN";
response.credentialsValue = null;
return saveDeviceCredentials(response, config).catch(function(){
return "error";
});
});
allPromise.push(promise)
}
for (let i = 0; i < attributesType.length; i++) {
let attribute = attributesType[i];
if (deviceRelation.attributes[attribute] && deviceRelation.attributes[attribute].length !== 0) {
promise = attributeService.saveEntityAttributes(types.entityType.device, deviceId.id, types.attributesScope[attribute].value, deviceRelation.attributes[attribute], config).catch(function () {
return "error";
});
allPromise.push(promise);
}
}
if (deviceRelation.timeseries.length !== 0) {
promise = attributeService.saveEntityTimeseries(types.entityType.device, deviceId.id, "time", deviceRelation.timeseries, config).catch(function(){
return "error";
});
allPromise.push(promise);
}
$q.all(allPromise).then(function success(response) {
let isResponseHasError = false;
for(let i = 0; i < response.length; i++){
if(response[i] === "error"){
isResponseHasError = true;
break;
}
}
if (isResponseHasError){
deferred.reject();
} else {
deferred.resolve();
}
});
return deferred.promise;
}
function saveDeviceParameters(deviceParameters, update, config) {
config = config || {};
const deferred = $q.defer();
let statisticalInfo = {};
let newDevice = {
name: deviceParameters.name,
type: deviceParameters.type
};
saveDevice(newDevice, config).then(function success(response) {
saveDeviceRelarion(response.id, deviceParameters, config).then(function success() {
statisticalInfo.create = {
device: 1
};
deferred.resolve(statisticalInfo);
}, function fail() {
statisticalInfo.error = {
device: 1
};
deferred.resolve(statisticalInfo);
});
}, function fail() {
if (update) {
findByName(deviceParameters.name, config).then(function success(response) {
saveDeviceRelarion(response.id, deviceParameters, config).then(function success() {
statisticalInfo.update = {
device: 1
};
deferred.resolve(statisticalInfo);
}, function fail() {
statisticalInfo.error = {
device: 1
};
deferred.resolve(statisticalInfo);
});
}, function fail() {
statisticalInfo.error = {
device: 1
};
deferred.resolve(statisticalInfo);
});
} else {
statisticalInfo.error = {
device: 1
};
deferred.resolve(statisticalInfo);
}
});
return deferred.promise;
}
function deleteDevice(deviceId) {
var deferred = $q.defer();
var url = '/api/device/' + deviceId;

View File

@ -38,6 +38,7 @@ function EntityService($http, $q, $filter, $translate, $log, userService, device
createAlarmSourceFromSubscriptionInfo: createAlarmSourceFromSubscriptionInfo,
getRelatedEntities: getRelatedEntities,
saveRelatedEntity: saveRelatedEntity,
saveEntityParameters: saveEntityParameters,
getRelatedEntity: getRelatedEntity,
deleteRelatedEntity: deleteRelatedEntity,
moveEntity: moveEntity,
@ -1072,6 +1073,123 @@ function EntityService($http, $q, $filter, $translate, $log, userService, device
return deferred.promise;
}
function saveEntityRelarion(entityType, entityId, entityRelation, config) {
const deferred = $q.defer();
let attributesType = Object.keys(types.attributesScope);
let allPromise = [];
let promise = "";
if (entityRelation.accessToken !== "") {
promise = deviceService.getDeviceCredentials(entityId.id, null, config).then(function (response) {
response.credentialsId = entityRelation.accessToken;
response.credentialsType = "ACCESS_TOKEN";
response.credentialsValue = null;
return deviceService.saveDeviceCredentials(response, config).catch(function () {
return "error";
});
});
allPromise.push(promise)
}
for (let i = 0; i < attributesType.length; i++) {
let attribute = attributesType[i];
if (entityRelation.attributes[attribute] && entityRelation.attributes[attribute].length !== 0) {
promise = attributeService.saveEntityAttributes(entityType, entityId.id, types.attributesScope[attribute].value, entityRelation.attributes[attribute], config).catch(function () {
return "error";
});
allPromise.push(promise);
}
}
if (entityRelation.timeseries.length !== 0) {
promise = attributeService.saveEntityTimeseries(entityType, entityId.id, "time", entityRelation.timeseries, config).catch(function(){
return "error";
});
allPromise.push(promise);
}
$q.all(allPromise).then(function success(response) {
let isResponseHasError = false;
for(let i = 0; i < response.length; i++){
if(response[i] === "error"){
isResponseHasError = true;
break;
}
}
if (isResponseHasError){
deferred.reject();
} else {
deferred.resolve();
}
});
return deferred.promise;
}
function saveEntityParameters(entityType, entityParameters, update, config) {
config = config || {};
const deferred = $q.defer();
let statisticalInfo = {};
let newEntity = {
name: entityParameters.name,
type: entityParameters.type
};
let promise;
switch (entityType) {
case types.entityType.device:
promise = deviceService.saveDevice(newEntity, config);
break;
case types.entityType.asset:
promise = assetService.saveAsset(newEntity, true, config);
break;
}
promise.then(function success(response) {
saveEntityRelarion(entityType, response.id, entityParameters, config).then(function success() {
statisticalInfo.create = {
entity: 1
};
deferred.resolve(statisticalInfo);
}, function fail() {
statisticalInfo.error = {
entity: 1
};
deferred.resolve(statisticalInfo);
});
}, function fail() {
if (update) {
let findIdEntity;
switch (entityType) {
case types.entityType.device:
findIdEntity = deviceService.findByName(entityParameters.name, config);
break;
case types.entityType.asset:
findIdEntity = assetService.findByName(entityParameters.name, config);
break;
}
findIdEntity.then(function success(response) {
saveEntityRelarion(response.id, entityType, entityParameters, config).then(function success() {
statisticalInfo.update = {
entity: 1
};
deferred.resolve(statisticalInfo);
}, function fail() {
statisticalInfo.error = {
entity: 1
};
deferred.resolve(statisticalInfo);
});
}, function fail() {
statisticalInfo.error = {
entity: 1
};
deferred.resolve(statisticalInfo);
});
} else {
statisticalInfo.error = {
entity: 1
};
deferred.resolve(statisticalInfo);
}
});
return deferred.promise;
}
function getRelatedEntity(entityId, keys, typeTranslatePrefix) {
var deferred = $q.defer();
getEntityPromise(entityId.entityType, entityId.id, {ignoreLoading: true}).then(

View File

@ -48,7 +48,7 @@ export function AssetCardController(types) {
/*@ngInject*/
export function AssetController($rootScope, userService, assetService, customerService, $state, $stateParams,
$document, $mdDialog, $q, $translate, types) {
$document, $mdDialog, $q, $translate, types, importExport) {
var customerId = $stateParams.customerId;
@ -56,6 +56,29 @@ export function AssetController($rootScope, userService, assetService, customerS
var assetGroupActionsList = [];
var assetAddItemActionsList = [
{
onAction: function ($event) {
vm.grid.addItem($event);
},
name: function() { return $translate.instant('action.add') },
details: function() { return $translate.instant('asset.add-asset-text') },
icon: "insert_drive_file"
},
{
onAction: function ($event) {
importExport.importEntities($event, types.entityType.asset).then(
function() {
vm.grid.refreshList();
}
);
},
name: function() { return $translate.instant('action.import') },
details: function() { return $translate.instant('asset.import') },
icon: "file_upload"
}
];
var vm = this;
vm.types = types;
@ -77,6 +100,7 @@ export function AssetController($rootScope, userService, assetService, customerS
actionsList: assetActionsList,
groupActionsList: assetGroupActionsList,
addItemActions: assetAddItemActionsList,
onGridInited: gridInited,
@ -294,6 +318,8 @@ export function AssetController($rootScope, userService, assetService, customerS
} else if (vm.assetsScope === 'customer_user') {
vm.assetGridConfig.addItemAction = {};
}
vm.assetGridConfig.addItemActions = [];
}
vm.assetGridConfig.refreshParamsFunc = refreshAssetsParamsFunction;

View File

@ -350,8 +350,15 @@ export default angular.module('thingsboard.types', [])
rulenode: "RULE_NODE",
entityView: "ENTITY_VIEW"
},
entityGroup: {
columnType: {
importEntityColumnType: {
name: {
name: 'entity-group.entity-field.name',
value: 'name'
},
type: {
name: 'entity-group.entity-field.type',
value: 'type'
},
clientAttribute: {
name: 'entity-group.column-type.client-attribute',
value: 'CLIENT_ATTRIBUTE'
@ -377,21 +384,48 @@ export default angular.module('thingsboard.types', [])
value: 'ACCESS_TOKEN'
}
},
entityField: {
name: {
name: 'entity-group.entity-field.name',
value: 'name'
},
type: {
name: 'entity-group.entity-field.type',
value: 'type'
},
assigned_customer: {
name: 'entity-group.entity-field.assigned_customer',
value: 'assigned_customer'
}
}
},
// entityGroup: {
// columnType: {
// clientAttribute: {
// name: 'entity-group.column-type.client-attribute',
// value: 'CLIENT_ATTRIBUTE'
// },
// sharedAttribute: {
// name: 'entity-group.column-type.shared-attribute',
// value: 'SHARED_ATTRIBUTE'
// },
// serverAttribute: {
// name: 'entity-group.column-type.server-attribute',
// value: 'SERVER_ATTRIBUTE'
// },
// timeseries: {
// name: 'entity-group.column-type.timeseries',
// value: 'TIMESERIES'
// },
// entityField: {
// name: 'entity-group.column-type.entity-field',
// value: 'ENTITY_FIELD'
// },
// accessToken: {
// name: 'entity-group.column-type.access-token',
// value: 'ACCESS_TOKEN'
// }
// },
// entityField: {
// name: {
// name: 'entity-group.entity-field.name',
// value: 'name'
// },
// type: {
// name: 'entity-group.entity-field.type',
// value: 'type'
// },
// assigned_customer: {
// name: 'entity-group.entity-field.assigned_customer',
// value: 'assigned_customer'
// }
// }
// },
aliasEntityType: {
current_customer: "CURRENT_CUSTOMER"
},

View File

@ -68,7 +68,7 @@ export function DeviceController($rootScope, userService, deviceService, custome
},
{
onAction: function ($event) {
importExport.importDevices($event, types.entityType.device).then(
importExport.importEntities($event, types.entityType.device).then(
function() {
vm.grid.refreshList();
}
@ -337,8 +337,6 @@ export function DeviceController($rootScope, userService, deviceService, custome
icon: "add"
};
vm.deviceGridConfig.addItemActions = [];
} else if (vm.devicesScope === 'customer_user') {
deviceActionsList.push(
{
@ -352,8 +350,9 @@ export function DeviceController($rootScope, userService, deviceService, custome
);
vm.deviceGridConfig.addItemAction = {};
vm.deviceGridConfig.addItemActions = [];
}
vm.deviceGridConfig.addItemActions = [];
}
vm.deviceGridConfig.refreshParamsFunc = refreshDevicesParamsFunction;

View File

@ -101,13 +101,13 @@ export default function ImportDialogCsvController($scope, $mdDialog, toast, impo
for (var i = 0; i < parseData.headers.length; i++) {
if (vm.importParameters.isHeader && parseData.headers[i].search(/^(name|type)$/im) === 0) {
columnParam = {
type: types.entityGroup.columnType.entityField.value,
type: types.importEntityColumnType.entityField.value,
key: parseData.headers[i].toLowerCase(),
sampleData: parseData.rows[0][i]
};
} else {
columnParam = {
type: types.entityGroup.columnType.serverAttribute.value,
type: types.importEntityColumnType.serverAttribute.value,
key: vm.importParameters.isHeader ? parseData.headers[i] : "",
sampleData: parseData.rows[0][i]
};
@ -137,38 +137,34 @@ export default function ImportDialogCsvController($scope, $mdDialog, toast, impo
};
for (var j = 0; j < parameterColumns.length; j++) {
switch (parameterColumns[j].type) {
case types.entityGroup.columnType.serverAttribute.value:
case types.importEntityColumnType.serverAttribute.value:
entityData.attributes.server.push({
key: parameterColumns[j].key,
value: importData.rows[i][j]
});
break;
case types.entityGroup.columnType.sharedAttribute.value:
case types.importEntityColumnType.sharedAttribute.value:
entityData.attributes.shared.push({
key: parameterColumns[j].key,
value: importData.rows[i][j]
});
break;
case types.entityGroup.columnType.timeseries.value:
case types.importEntityColumnType.timeseries.value:
entityData.timeseries.push({
key: parameterColumns[j].key,
value: importData.rows[i][j]
});
break;
case types.entityGroup.columnType.accessToken.value:
case types.importEntityColumnType.accessToken.value:
entityData.accessToken = importData.rows[i][j];
break;
case types.entityGroup.columnType.entityField.value:
switch (parameterColumns[j].key) {
case types.entityGroup.entityField.name.value:
case types.importEntityColumnType.name.value:
entityData.name = importData.rows[i][j];
break;
case types.entityGroup.entityField.type.value:
case types.importEntityColumnType.type.value:
entityData.type = importData.rows[i][j];
break;
}
break;
}
}
entitysData.push(entityData);
}

View File

@ -147,16 +147,13 @@
<md-step md-label="Done">
<md-step-body layout="column">
<div>
<p class="md-body-1" ng-if="vm.statistical.create && vm.statistical.create.device">{{vm.statistical.create.device}} new devices were successfully created.</p>
<p class="md-body-1" ng-if="vm.statistical.update && vm.statistical.update.device">{{vm.statistical.update.device}} devices were successfully updated.</p>
<p class="md-body-1" ng-if="vm.statistical.error && vm.statistical.error.device">There was an error creating {{vm.statistical.error.device}} devices.</p>
<p class="md-body-1" ng-if="vm.statistical.create && vm.statistical.create.device">{{vm.statistical.create.entity}} new entities were successfully created.</p>
<p class="md-body-1" ng-if="vm.statistical.update && vm.statistical.update.device">{{vm.statistical.update.entity}} entities were successfully updated.</p>
<p class="md-body-1" ng-if="vm.statistical.error && vm.statistical.error.device">There was an error creating {{vm.statistical.error.entity}} entities.</p>
</div>
</md-step-body>
<md-step-actions layout="row">
<span flex></span>
<!--<md-button ng-disabled="$root.loading" ng-click="vm.cancel()">-->
<!--{{ 'action.cancel' | translate }}-->
<!--</md-button>-->
<md-button class="md-primary md-raised" ng-disabled="$root.loading" ng-click="vm.finishExport();">
Ok
</md-button>

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable import/no-unresolved, import/default */
/* eslint-disable import/no-unresolved, import/default*/
import importDialogTemplate from './import-dialog.tpl.html';
import importDialogCSVTemplate from './import-dialog-csv.tpl.html';
@ -25,7 +25,7 @@ import entityAliasesTemplate from '../entity/alias/entity-aliases.tpl.html';
/* eslint-disable no-undef, angular/window-service, angular/document-service */
/*@ngInject*/
export default function ImportExport($log, $translate, $q, $mdDialog, $document, $http, itembuffer, utils, types, $timeout, deviceService, $rootScope,
export default function ImportExport($log, $translate, $q, $mdDialog, $document, $http, itembuffer, utils, types, $rootScope,
dashboardUtils, entityService, dashboardService, ruleChainService, widgetService, toast, attributeService) {
@ -42,7 +42,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document,
importWidgetsBundle: importWidgetsBundle,
exportExtension: exportExtension,
importExtension: importExtension,
importDevices: importDevices,
importEntities: importEntities,
convertCSVToJson: convertCSVToJson,
exportToPc: exportToPc,
createMultiEntity: createMultiEntity
@ -578,9 +578,12 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document,
return deferred.promise;
}
function importDevices($event, entityType) {
function importEntities($event, entityType) {
var deferred = $q.defer();
openImportDialogCSV($event, entityType,'device.import', 'device.device-file').then(
switch (entityType) {
case types.entityType.device:
openImportDialogCSV($event, entityType, 'device.import', 'device.device-file').then(
function success() {
deferred.resolve();
},
@ -589,6 +592,18 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document,
}
);
return deferred.promise;
case types.entityType.asset:
openImportDialogCSV($event, entityType, 'asset.import', 'asset.asset-file').then(
function success() {
deferred.resolve();
},
function fail() {
deferred.reject();
}
);
return deferred.promise;
}
}
function saveImportedDashboard(dashboard, deferred) {
@ -843,14 +858,12 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document,
let allPromise = [];
let statisticalInfo = {};
let deferred = $q.defer();
switch (entityType) {
case types.entityType.device:
for(let i = 0; i < partSize; i++){
const promise = deviceService.saveDeviceParameters(arrayData[i], updateData, config);
const promise = entityService.saveEntityParameters(entityType, arrayData[i], updateData, config);
allPromise.push(promise);
}
break;
}
qAllWithProgress(allPromise).then(function success(response) {
for (let i = 0; i < response.length; i++){
statisticalInfo = sumObject(statisticalInfo, response[i]);

View File

@ -38,59 +38,66 @@ export default function TableColumnsAssignment() {
}
/*@ngInject*/
function TableColumnsAssignmentController($scope, types, $timeout) {
function TableColumnsAssignmentController($scope, types) {
var vm = this;
vm.columnTypes = {};
vm.entityField = {};
vm.columnTypes.name = types.importEntityColumnType.name;
vm.columnTypes.type = types.importEntityColumnType.type;
switch (vm.entityType) {
case types.entityType.device:
vm.columnTypes.sharedAttribute = types.entityGroup.columnType.sharedAttribute;
vm.columnTypes.serverAttribute = types.entityGroup.columnType.serverAttribute;
vm.columnTypes.timeseries = types.entityGroup.columnType.timeseries;
vm.columnTypes.entityField = types.entityGroup.columnType.entityField;
vm.columnTypes.accessToken = types.entityGroup.columnType.accessToken;
vm.columnTypes.sharedAttribute = types.importEntityColumnType.sharedAttribute;
vm.columnTypes.serverAttribute = types.importEntityColumnType.serverAttribute;
vm.columnTypes.timeseries = types.importEntityColumnType.timeseries;
vm.columnTypes.accessToken = types.importEntityColumnType.accessToken;
break;
case types.entityType.asset:
vm.columnTypes.serverAttribute = types.importEntityColumnType.serverAttribute;
vm.columnTypes.timeseries = types.importEntityColumnType.timeseries;
break;
}
vm.entityField.name = types.entityGroup.entityField.name;
// switch (vm.entityType) {
// case types.entityType.device:
// vm.entityField.type = types.entityGroup.entityField.type;
// // vm.entityField.assigned_customer = types.entityGroup.entityField.assigned_customer;
// break;
// case types.entityType.asset:
// vm.entityField.type = types.entityGroup.entityField.type;
// // vm.entityField.assigned_customer = types.entityGroup.entityField.assigned_customer;
// break;
// }
switch (vm.entityType) {
case types.entityType.device:
vm.entityField.type = types.entityGroup.entityField.type;
// vm.entityField.assigned_customer = types.entityGroup.entityField.assigned_customer;
break;
}
$scope.$watch('vm.columns', function(newVal){
if (newVal) {
var isSelectName = false;
var isSelectType = false;
var isSelectCredentials = false;
for (var i = 0; i < newVal.length; i++) {
if (newVal[i].type === types.entityGroup.columnType.entityField.value &&
newVal[i].key === types.entityGroup.entityField.name.value) {
isSelectName = true;
}
if (newVal[i].type === types.entityGroup.columnType.entityField.value &&
newVal[i].key === types.entityGroup.entityField.type.value) {
isSelectType = true;
}
if (newVal[i].type === types.entityGroup.columnType.accessToken.value) {
isSelectCredentials = true;
}
}
$timeout(function () {
vm.entityField.name.disable = isSelectName;
vm.entityField.type.disable = isSelectType;
vm.columnTypes.accessToken.disable = isSelectCredentials;
});
if(isSelectName && isSelectType) {
vm.theForm.$setDirty();
} else {
vm.theForm.$setPristine();
}
}
}, true);
// $scope.$watch('vm.columns', function(newVal){
// if (newVal) {
// var isSelectName = false;
// var isSelectType = false;
// // var isSelectCredentials = false;
// for (var i = 0; i < newVal.length; i++) {
// if (newVal[i].type === types.importEntityColumnType.entityField.value &&
// newVal[i].key === types.entityGroup.entityField.name.value) {
// isSelectName = true;
// }
// if (newVal[i].type === types.importEntityColumnType.entityField.value &&
// newVal[i].key === types.entityGroup.entityField.type.value) {
// isSelectType = true;
// }
// // if (newVal[i].type === types.entityGroup.columnType.accessToken.value) {
// // isSelectCredentials = true;
// // }
// }
// $timeout(function () {
// vm.entityField.name.disable = isSelectName;
// vm.entityField.type.disable = isSelectType;
// // vm.columnTypes.accessToken.disable = isSelectCredentials;
// });
// if(isSelectName && isSelectType) {
// vm.theForm.$setDirty();
// } else {
// vm.theForm.$setPristine();
// }
// }
// }, true);
}

View File

@ -246,7 +246,9 @@
"select-asset": "Select asset",
"no-assets-matching": "No assets matching '{{entity}}' were found.",
"asset-required": "Asset is required",
"name-starts-with": "Asset name starts with"
"name-starts-with": "Asset name starts with",
"import": "Import assets",
"asset-file": "Asset file"
},
"attribute": {
"attributes": "Attributes",