|
|
|
@ -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, |
|
|
|
export default function ImportExport($log, $translate, $q, $mdDialog, $document, $http, itembuffer, utils, types, $timeout, deviceService, |
|
|
|
dashboardUtils, entityService, dashboardService, ruleChainService, widgetService, toast, attributeService) { |
|
|
|
|
|
|
|
|
|
|
|
@ -43,7 +43,9 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
exportExtension: exportExtension, |
|
|
|
importExtension: importExtension, |
|
|
|
importDevices: importDevices, |
|
|
|
exportToPc: exportToPc |
|
|
|
convertCSVToJson: convertCSVToJson, |
|
|
|
exportToPc: exportToPc, |
|
|
|
createMultiEntity: createMultiEntity |
|
|
|
}; |
|
|
|
|
|
|
|
return service; |
|
|
|
@ -56,11 +58,11 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
var bundleAlias = widgetsBundle.alias; |
|
|
|
var isSystem = widgetsBundle.tenantId.id === types.id.nullUid; |
|
|
|
widgetService.getBundleWidgetTypes(bundleAlias, isSystem).then( |
|
|
|
function success (widgetTypes) { |
|
|
|
function success(widgetTypes) { |
|
|
|
prepareExport(widgetsBundle); |
|
|
|
var widgetsBundleItem = { |
|
|
|
widgetsBundle: prepareExport(widgetsBundle), |
|
|
|
widgetTypes: [] |
|
|
|
widgetsBundle: prepareExport(widgetsBundle), |
|
|
|
widgetTypes: [] |
|
|
|
}; |
|
|
|
for (var t in widgetTypes) { |
|
|
|
var widgetType = widgetTypes[t]; |
|
|
|
@ -70,10 +72,10 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
widgetsBundleItem.widgetTypes.push(prepareExport(widgetType)); |
|
|
|
} |
|
|
|
var name = widgetsBundle.title; |
|
|
|
name = name.toLowerCase().replace(/\W/g,"_"); |
|
|
|
name = name.toLowerCase().replace(/\W/g, "_"); |
|
|
|
exportToPc(widgetsBundleItem, name + '.json'); |
|
|
|
}, |
|
|
|
function fail (rejection) { |
|
|
|
function fail(rejection) { |
|
|
|
var message = rejection; |
|
|
|
if (!message) { |
|
|
|
message = $translate.instant('error.unknown-error'); |
|
|
|
@ -170,7 +172,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
delete widgetType.bundleAlias; |
|
|
|
} |
|
|
|
var name = widgetType.name; |
|
|
|
name = name.toLowerCase().replace(/\W/g,"_"); |
|
|
|
name = name.toLowerCase().replace(/\W/g, "_"); |
|
|
|
exportToPc(prepareExport(widgetType), name + '.json'); |
|
|
|
}, |
|
|
|
function fail(rejection) { |
|
|
|
@ -211,8 +213,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
|
|
|
|
function validateImportedWidgetType(widgetType) { |
|
|
|
if (angular.isUndefined(widgetType.name) |
|
|
|
|| angular.isUndefined(widgetType.descriptor)) |
|
|
|
{ |
|
|
|
|| angular.isUndefined(widgetType.descriptor)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
@ -230,7 +231,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
metadata: prepareRuleChainMetaData(ruleChainMetaData) |
|
|
|
}; |
|
|
|
var name = ruleChain.name; |
|
|
|
name = name.toLowerCase().replace(/\W/g,"_"); |
|
|
|
name = name.toLowerCase().replace(/\W/g, "_"); |
|
|
|
exportToPc(ruleChainExport, name + '.json'); |
|
|
|
}, |
|
|
|
(rejection) => { |
|
|
|
@ -255,7 +256,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
|
|
|
|
function prepareRuleChainMetaData(ruleChainMetaData) { |
|
|
|
delete ruleChainMetaData.ruleChainId; |
|
|
|
for (var i=0;i<ruleChainMetaData.nodes.length;i++) { |
|
|
|
for (var i = 0; i < ruleChainMetaData.nodes.length; i++) { |
|
|
|
var node = ruleChainMetaData.nodes[i]; |
|
|
|
delete node.ruleChainId; |
|
|
|
ruleChainMetaData.nodes[i] = prepareExport(node); |
|
|
|
@ -307,7 +308,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
function exportWidget(dashboard, sourceState, sourceLayout, widget) { |
|
|
|
var widgetItem = itembuffer.prepareWidgetItem(dashboard, sourceState, sourceLayout, widget); |
|
|
|
var name = widgetItem.widget.config.title; |
|
|
|
name = name.toLowerCase().replace(/\W/g,"_"); |
|
|
|
name = name.toLowerCase().replace(/\W/g, "_"); |
|
|
|
exportToPc(prepareExport(widgetItem), name + '.json'); |
|
|
|
} |
|
|
|
|
|
|
|
@ -417,10 +418,10 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
var aliasIds = Object.keys(entityAliases); |
|
|
|
if (aliasIds.length > 0) { |
|
|
|
processEntityAliases(entityAliases, aliasIds).then( |
|
|
|
function(missingEntityAliases) { |
|
|
|
function (missingEntityAliases) { |
|
|
|
if (Object.keys(missingEntityAliases).length > 0) { |
|
|
|
editMissingAliases($event, [ widget ], |
|
|
|
true, 'dashboard.widget-import-missing-aliases-title', missingEntityAliases).then( |
|
|
|
editMissingAliases($event, [widget], |
|
|
|
true, 'dashboard.widget-import-missing-aliases-title', missingEntityAliases).then( |
|
|
|
function success(updatedEntityAliases) { |
|
|
|
for (var aliasId in updatedEntityAliases) { |
|
|
|
var entityAlias = updatedEntityAliases[aliasId]; |
|
|
|
@ -485,14 +486,14 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
function success(targetLayout) { |
|
|
|
itembuffer.addWidgetToDashboard(dashboard, targetState, targetLayout, widget, |
|
|
|
aliasesInfo, onAliasesUpdateFunction, originalColumns, originalSize, -1, -1).then( |
|
|
|
function() { |
|
|
|
deferred.resolve( |
|
|
|
{ |
|
|
|
widget: widget, |
|
|
|
layoutId: targetLayout |
|
|
|
} |
|
|
|
); |
|
|
|
} |
|
|
|
function () { |
|
|
|
deferred.resolve( |
|
|
|
{ |
|
|
|
widget: widget, |
|
|
|
layoutId: targetLayout |
|
|
|
} |
|
|
|
); |
|
|
|
} |
|
|
|
); |
|
|
|
}, |
|
|
|
function fail() { |
|
|
|
@ -507,7 +508,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
dashboardService.getDashboard(dashboardId).then( |
|
|
|
function success(dashboard) { |
|
|
|
var name = dashboard.title; |
|
|
|
name = name.toLowerCase().replace(/\W/g,"_"); |
|
|
|
name = name.toLowerCase().replace(/\W/g, "_"); |
|
|
|
exportToPc(prepareDashboardExport(dashboard), name + '.json'); |
|
|
|
}, |
|
|
|
function fail(rejection) { |
|
|
|
@ -540,13 +541,13 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
dashboard = dashboardUtils.validateAndUpdateDashboard(dashboard); |
|
|
|
var entityAliases = dashboard.configuration.entityAliases; |
|
|
|
if (entityAliases) { |
|
|
|
var aliasIds = Object.keys( entityAliases ); |
|
|
|
var aliasIds = Object.keys(entityAliases); |
|
|
|
if (aliasIds.length > 0) { |
|
|
|
processEntityAliases(entityAliases, aliasIds).then( |
|
|
|
function(missingEntityAliases) { |
|
|
|
if (Object.keys( missingEntityAliases ).length > 0) { |
|
|
|
function (missingEntityAliases) { |
|
|
|
if (Object.keys(missingEntityAliases).length > 0) { |
|
|
|
editMissingAliases($event, dashboard.configuration.widgets, |
|
|
|
false, 'dashboard.dashboard-import-missing-aliases-title', missingEntityAliases).then( |
|
|
|
false, 'dashboard.dashboard-import-missing-aliases-title', missingEntityAliases).then( |
|
|
|
function success(updatedEntityAliases) { |
|
|
|
for (var aliasId in updatedEntityAliases) { |
|
|
|
entityAliases[aliasId] = updatedEntityAliases[aliasId]; |
|
|
|
@ -617,6 +618,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
// saveImportedDashboard(dashboard, deferred);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
deferred.resolve(); |
|
|
|
}, |
|
|
|
function fail() { |
|
|
|
deferred.reject(); |
|
|
|
@ -644,14 +646,13 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function exportExtension(extensionId) { |
|
|
|
|
|
|
|
getExtension(extensionId) |
|
|
|
.then( |
|
|
|
function success(extension) { |
|
|
|
var name = extension.title; |
|
|
|
name = name.toLowerCase().replace(/\W/g,"_"); |
|
|
|
name = name.toLowerCase().replace(/\W/g, "_"); |
|
|
|
exportToPc(prepareExport(extension), name + '.json'); |
|
|
|
}, |
|
|
|
function fail(rejection) { |
|
|
|
@ -711,7 +712,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
function validateImportedExtension(configuration) { |
|
|
|
if (configuration.length) { |
|
|
|
for (let i = 0; i < configuration.length; i++) { |
|
|
|
if (angular.isUndefined(configuration[i].configuration) || angular.isUndefined(configuration[i].id )|| angular.isUndefined(configuration[i].type)) { |
|
|
|
if (angular.isUndefined(configuration[i].configuration) || angular.isUndefined(configuration[i].id) || angular.isUndefined(configuration[i].type)) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -742,7 +743,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
var aliasId = aliasIds[index]; |
|
|
|
var entityAlias = entityAliases[aliasId]; |
|
|
|
entityService.checkEntityAlias(entityAlias).then( |
|
|
|
function(result) { |
|
|
|
function (result) { |
|
|
|
if (result) { |
|
|
|
checkNextEntityAliasOrComplete(index, aliasIds, entityAliases, missingEntityAliases, deferred); |
|
|
|
} else { |
|
|
|
@ -782,6 +783,91 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
return deferred.promise; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* splitCSV function (c) 2009 Brian Huisman, see http://www.greywyvern.com/?post=258
|
|
|
|
* Works by spliting on seperators first, then patching together quoted values |
|
|
|
*/ |
|
|
|
function splitCSV(str, sep) { |
|
|
|
for (var foo = str.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) { |
|
|
|
if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') { |
|
|
|
if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') { |
|
|
|
foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"'); |
|
|
|
} else if (x) { |
|
|
|
foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep)); |
|
|
|
} else foo = foo.shift().split(sep).concat(foo); |
|
|
|
} else foo[x].replace(/""/g, '"'); |
|
|
|
} |
|
|
|
return foo; |
|
|
|
} |
|
|
|
|
|
|
|
function isNumeric(str) { |
|
|
|
str = str.replace(',', '.'); |
|
|
|
return !isNaN(parseFloat(str)) && isFinite(str); |
|
|
|
} |
|
|
|
|
|
|
|
function parseStringToFormatJS(str) { |
|
|
|
if (isNumeric(str.replace(',', '.'))) { |
|
|
|
return parseFloat(str.replace(',', '.')); |
|
|
|
} |
|
|
|
if (str.search(/^(true|false)$/im) === 0) { |
|
|
|
return str.toLowerCase() === 'true'; |
|
|
|
} |
|
|
|
if (str === "") { |
|
|
|
return null; |
|
|
|
} |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
function convertCSVToJson(csvdata, config) { |
|
|
|
config = config || {}; |
|
|
|
const delim = config.delim || ","; |
|
|
|
const header = config.header || false; |
|
|
|
|
|
|
|
let csvlines = csvdata.split(/[\r\n]+/); |
|
|
|
let csvheaders = splitCSV(csvlines[0], delim); |
|
|
|
let csvrows = header ? csvlines.slice(1, csvlines.length) : csvlines; |
|
|
|
|
|
|
|
let result = {}; |
|
|
|
result.headers = csvheaders; |
|
|
|
result.rows = []; |
|
|
|
|
|
|
|
for (let r in csvrows) { |
|
|
|
if (csvrows.hasOwnProperty(r)) { |
|
|
|
let row = csvrows[r]; |
|
|
|
|
|
|
|
if (row.length === 0) |
|
|
|
break; |
|
|
|
|
|
|
|
let rowitems = splitCSV(row, delim); |
|
|
|
for (let i = 0; i < rowitems.length; i++) { |
|
|
|
rowitems[i] = parseStringToFormatJS(rowitems[i]); |
|
|
|
} |
|
|
|
result.rows.push(rowitems); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
function createMultiEntity(arrayData, entityType, update, config) { |
|
|
|
var deferred = $q.defer(); |
|
|
|
var allPromise = []; |
|
|
|
switch (entityType) { |
|
|
|
case types.entityType.device: |
|
|
|
for(var i = 0; i < arrayData.length; i++){ |
|
|
|
var promise = deviceService.saveDeviceParameters(arrayData[i], update, config); |
|
|
|
allPromise.push(promise); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
$q.all(allPromise).then(function success() { |
|
|
|
deferred.resolve(); |
|
|
|
$timeout(function () { |
|
|
|
console.log("1"); // eslint-disable-line
|
|
|
|
}, 1000); |
|
|
|
}); |
|
|
|
return deferred.promise; |
|
|
|
} |
|
|
|
|
|
|
|
// Common functions
|
|
|
|
|
|
|
|
function prepareExport(data) { |
|
|
|
@ -821,8 +907,7 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
|
|
|
|
if (window.navigator && window.navigator.msSaveOrOpenBlob) { |
|
|
|
window.navigator.msSaveOrOpenBlob(blob, filename); |
|
|
|
} |
|
|
|
else{ |
|
|
|
} else { |
|
|
|
var e = document.createEvent('MouseEvents'), |
|
|
|
a = document.createElement('a'); |
|
|
|
|
|
|
|
@ -873,7 +958,6 @@ export default function ImportExport($log, $translate, $q, $mdDialog, $document, |
|
|
|
targetEvent: $event |
|
|
|
}).then(function (importData) { |
|
|
|
deferred.resolve(importData); |
|
|
|
|
|
|
|
}, function () { |
|
|
|
deferred.reject(); |
|
|
|
}); |
|
|
|
|