Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ('DevicesService', ['$http', '$rootScope', 'RealTimeService', 'Utils', 'UsersService', 'Downloader', 'CompaniesService',
- function ($http, $rootScope, RealTimeService, Utils, UsersService, Downloader, CompaniesService) {
- var storage = {devices: {}, callbacks: {}, deviceCounter: {}},
- serviceScope = $rootScope.$new(),
- callbacksForRemoteInstall = {};
- Utils.customizeObj(storage);
- CompaniesService._addInitListener(init);
- function init() {
- $http.get("/devices?companyId=" + CompaniesService.getCurrentCompany().id).then(function (resp) {
- var devices = storage.devices;
- Utils.customizeObj(resp.data, devices);
- calculateDevicesCountersAndCallCallbacks();
- RealTimeService.subscribe('/device', serviceScope, function (newChange) {
- console.log(newChange);
- if (newChange.action === 'DEVICE_ONLINE') {
- for (var i in devices[newChange.data.clientCompanyId]) {
- if (devices[newChange.data.clientCompanyId][i].id === newChange.data.value) {
- Utils.defineReadOnlyProperty(devices[newChange.data.clientCompanyId][i], 'online', true);
- }
- }
- calculateDevicesCountersAndCallCallbacks();
- } else if (newChange.action === 'DEVICE_OFFLINE') {
- for (var i in devices[newChange.data.clientCompanyId]) {
- if (devices[newChange.data.clientCompanyId][i].id === newChange.data.value) {
- Utils.defineReadOnlyProperty(devices[newChange.data.clientCompanyId][i], 'online', false);
- }
- }
- calculateDevicesCountersAndCallCallbacks();
- } else if (newChange.action === 'DEVICE_INSTALLED') {
- if (!devices[newChange.data.clientCompanyId]) {
- var companyId = newChange.data.clientCompanyId;
- devices[companyId] = [];
- devices[companyId].push(JSON.parse(newChange.data.value).device);
- } else {
- devices[newChange.data.clientCompanyId].push(Utils.customizeObj(JSON.parse(newChange.data.value).device));
- }
- calculateDevicesCountersAndCallCallbacks();
- } else if (newChange.action === 'DEVICE_UNINSTALLED') {
- for (var i in devices[newChange.data.clientCompanyId]) {
- if (devices[newChange.data.clientCompanyId][i].id === newChange.data.value) {
- devices[newChange.data.clientCompanyId].splice(i, 1);
- }
- }
- calculateDevicesCountersAndCallCallbacks();
- }
- });
- RealTimeService.subscribe('/oneTimeConnection', $rootScope, function (newChange) {
- if (newChange.action === 'ONE_TIME_CONNECTION_CLOSED' && !newChange.data.id) {
- console.log(newChange);
- $rootScope.openCreateTicketForm(newChange.data);
- }
- })
- });
- }
- function callCallbacks() {
- for (var scopeId in storage.callbacks) {
- if (scopeId === 'staticCopy') continue;
- var callback = storage.callbacks[scopeId];
- if (callback && typeof callback === "function") {
- callback.callback(storage.devices[callback.companyId]);
- }
- }
- }
- function destroyCallbackByScope(scope) {
- for (var scopeId in storage.callbacks) {
- if (companyId === 'staticCopy') continue;
- if (scopeId === scope.$id) {
- delete storage.callbacks[scopeId]
- }
- }
- }
- function calculateDevicesCountersAndCallCallbacks() {
- for (var companyId in storage.devices) {
- if (companyId === 'staticCopy') continue;
- if (storage.devices[companyId].length == 0) {
- delete storage.devices[companyId];
- delete storage.deviceCounter[companyId];
- callCallbacks();
- return;
- }
- if (!storage.deviceCounter.companyId) {
- storage.deviceCounter[companyId] = Utils.customizeObj({all: 0, online: 0});
- }
- Utils.defineReadOnlyProperty(storage.deviceCounter[companyId], 'all', storage.devices[companyId].length);
- var online = 0;
- for (var i = 0; i < storage.devices[companyId].length; i++) {
- if (storage.devices[companyId][i].online) {
- online++;
- }
- }
- Utils.defineReadOnlyProperty(storage.deviceCounter[companyId], 'online', online);
- }
- callCallbacks();
- }
- function downloadAgent(companyId, userName, userPassword, callback) {
- if (!companyId) {
- throw "CompanyId is required!"
- }
- return $http.get("/data/prepareDownloadAgent?companyId=" + companyId + "&userName=" + userName + "&userPassword=" + userPassword).then(function (response) {
- if (callback) {
- callback(response.data);
- }
- if (response.data.code >= 400) {
- return;
- }
- Downloader.downloadByUrl(response.data.link);
- });
- }
- function createOneTimeRemoteSession(callback) {
- $http.get('/remote/createOneTimeRemoteSession').then(function (resp) {
- if (callback) {
- callback(resp.data);
- }
- }, function (resp) {
- if (callback) {
- callback(resp.data);
- }
- });
- }
- function getRemoteSessions(scope, callback) {
- if (!scope || !scope.$id) {
- throw 'First argument must be Angularjs scope!'
- }
- return Utils.getListByUrl('/remote/getRemoteSessions', callback, function (list) {
- RealTimeService.subscribe('/oneTimeConnection', scope, function (newChange) {
- console.log(newChange);
- if (newChange.action === 'ONE_TIME_CONNECTION_CLOSED' && !newChange.data.id) {
- //do nothing
- } else if (newChange.action === 'ONE_TIME_DELETED' || newChange.action === 'ONE_TIME_UNINSTALLED') {
- Utils.deleteById(list, newChange.data);
- } else {
- var session = Utils.findById(list, newChange.data.id);
- if (session) {
- Utils.customizeObj(newChange.data, session);
- } else {
- list.unshift(Utils.customizeObj(newChange.data));
- }
- }
- })
- });
- }
- function deleteRemoteSession(ids, callback) {
- if (!ids) throw 'ids is required!';
- if (!Array.isArray(ids)) ids = [ids];
- var future = $http({
- url: '/remote/deleteRemoteSession',
- method: 'POST',
- data: ids
- });
- Utils.handleCallback(future, callback);
- }
- function getSessionId(remoteSessionId, connectionType, callback) {
- if (!remoteSessionId) throw 'remoteSessionId is required!';
- if (!connectionType) throw 'connectionType is required!';
- $http.get('/devices/getSessionId?remoteSessionId=' + remoteSessionId + '&connectionType=' + connectionType).then(function (resp) {
- if (callback) {
- callback(resp.data);
- }
- }, function (resp) {
- if (callback) {
- callback(resp.data);
- }
- });
- }
- function sendInstallAgentRequest(companyId, agentId, devicesIds, callback) {
- if (!companyId) {
- throw "CompanyId is required!"
- }
- if (!agentId) {
- throw "agentId is required!"
- }
- if (!devicesIds) {
- throw "agentId is required!"
- }
- if (!Array.isArray(devicesIds)) devicesIds = [devicesIds];
- if (callback && typeof callback === "function") {
- for (var i in devicesIds) {
- callbacksForRemoteInstall[devicesIds[i]] = callback;
- }
- }
- $http({
- url: '/devices/sendInstallAgentRequest?companyId=' + companyId + '&agentId=' + agentId,
- method: 'POST',
- data: devicesIds
- });
- }
- function isCompanyHasUserNameAndPass(companyId, callback) {
- if (!companyId) {
- throw "CompanyId is required!"
- }
- $http.get("/devices/isCompanyHasUserNameAndPass?companyId=" + companyId).then(function (response) {
- if (callback) {
- callback(response.data);
- }
- return response.data;
- });
- }
- function getDevicesCount(scope, callback) {
- if (!scope) {
- throw "scope is required!"
- }
- storage.callbacks[scope.$id] = {
- companyId: CompaniesService.getCurrentCompany().id,
- callback: callback
- };
- if (storage.devices && callback) {
- callback(storage.devices);
- }
- return storage.deviceCounter;
- }
- function getAgentsByCompany(scope, companyId, callback) {
- if (!scope) {
- throw "scope is required!"
- }
- if (!companyId) {
- throw "CompanyId is required!"
- }
- if (callback) {
- storage.callbacks[scope.$id] = {
- companyId: companyId,
- callback: callback
- };
- }
- if (storage.devices[companyId] && callback) {
- callback(storage.devices[companyId]);
- }
- return storage.devices[companyId];
- }
- function runScanDevices(scope, companyId, agentId, callback) {
- if (!scope) {
- throw "scope is required!"
- }
- if (!companyId) {
- throw "CompanyId is required!"
- }
- if (!agentId) {
- throw "AgentId is required!"
- }
- var info = Utils.customizeObj({});
- scope.isScanDevicesLoad = true;
- $http.get('/devices/refreshNetworkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (error) {
- console.log(error);
- });
- $http.get('/devices/networkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (resp) {
- Utils.customizeObj(resp.data, info);
- if (info.networkScan) {
- Utils.defineReadOnlyProperty(info, "networkScan", JSON.parse(info.networkScan));
- }
- RealTimeService.subscribe('/netScan', scope, function (newChange) {
- console.log(newChange);
- if (newChange.action === 'NETWORK_SCAN') {
- $http.get('/devices/networkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (resp) {
- Utils.customizeObj(resp.data, info);
- Utils.defineReadOnlyProperty(info, "networkScan", JSON.parse(info.networkScan));
- if (newChange.data === 'Finished') {
- scope.isScanDevicesLoad = false;
- }
- });
- } else if (newChange.action === 'REMOTE_INSTALL') {
- var call = callbacksForRemoteInstall[newChange.data.netScanId];
- if (call && typeof call === "function" && newChange.data.status === 'INSTALLED') {
- call();
- }
- var device = Utils.findById(info.networkScan.devices, newChange.data.netScanId);
- if (device) {
- Utils.defineReadOnlyProperty(device, "message", newChange.data.content);
- if (newChange.data.status !== 'INIT') {
- Utils.defineReadOnlyProperty(device, "agentStatus", 'managed');
- }
- }
- }
- if (callback) {
- callback(newChange.data);
- }
- });
- if (callback) {
- callback();
- }
- }, function (resp) {
- if (callback) {
- callback();
- }
- });
- return info;
- }
- return {
- downloadAgent: downloadAgent,
- runScanDevices: runScanDevices,
- getDevicesCount: getDevicesCount,
- getAgentsByCompany: getAgentsByCompany,
- isCompanyHasUserNameAndPass: isCompanyHasUserNameAndPass,
- sendInstallAgentRequest: sendInstallAgentRequest,
- createOneTimeRemoteSession: createOneTimeRemoteSession,
- getRemoteSessions: getRemoteSessions,
- getSessionId: getSessionId,
- deleteRemoteSession: deleteRemoteSession
- }
- }
- ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement