Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.49 KB | None | 0 0
  1. ('DevicesService', ['$http', '$rootScope', 'RealTimeService', 'Utils', 'UsersService', 'Downloader', 'CompaniesService',
  2. function ($http, $rootScope, RealTimeService, Utils, UsersService, Downloader, CompaniesService) {
  3.  
  4. var storage = {devices: {}, callbacks: {}, deviceCounter: {}},
  5. serviceScope = $rootScope.$new(),
  6. callbacksForRemoteInstall = {};
  7.  
  8. Utils.customizeObj(storage);
  9.  
  10. CompaniesService._addInitListener(init);
  11.  
  12. function init() {
  13. $http.get("/devices?companyId=" + CompaniesService.getCurrentCompany().id).then(function (resp) {
  14. var devices = storage.devices;
  15.  
  16. Utils.customizeObj(resp.data, devices);
  17. calculateDevicesCountersAndCallCallbacks();
  18.  
  19. RealTimeService.subscribe('/device', serviceScope, function (newChange) {
  20.  
  21. console.log(newChange);
  22.  
  23. if (newChange.action === 'DEVICE_ONLINE') {
  24. for (var i in devices[newChange.data.clientCompanyId]) {
  25. if (devices[newChange.data.clientCompanyId][i].id === newChange.data.value) {
  26. Utils.defineReadOnlyProperty(devices[newChange.data.clientCompanyId][i], 'online', true);
  27. }
  28. }
  29. calculateDevicesCountersAndCallCallbacks();
  30. } else if (newChange.action === 'DEVICE_OFFLINE') {
  31. for (var i in devices[newChange.data.clientCompanyId]) {
  32. if (devices[newChange.data.clientCompanyId][i].id === newChange.data.value) {
  33. Utils.defineReadOnlyProperty(devices[newChange.data.clientCompanyId][i], 'online', false);
  34. }
  35. }
  36. calculateDevicesCountersAndCallCallbacks();
  37. } else if (newChange.action === 'DEVICE_INSTALLED') {
  38. if (!devices[newChange.data.clientCompanyId]) {
  39. var companyId = newChange.data.clientCompanyId;
  40. devices[companyId] = [];
  41. devices[companyId].push(JSON.parse(newChange.data.value).device);
  42. } else {
  43. devices[newChange.data.clientCompanyId].push(Utils.customizeObj(JSON.parse(newChange.data.value).device));
  44. }
  45. calculateDevicesCountersAndCallCallbacks();
  46. } else if (newChange.action === 'DEVICE_UNINSTALLED') {
  47. for (var i in devices[newChange.data.clientCompanyId]) {
  48. if (devices[newChange.data.clientCompanyId][i].id === newChange.data.value) {
  49. devices[newChange.data.clientCompanyId].splice(i, 1);
  50. }
  51. }
  52. calculateDevicesCountersAndCallCallbacks();
  53. }
  54. });
  55.  
  56. RealTimeService.subscribe('/oneTimeConnection', $rootScope, function (newChange) {
  57. if (newChange.action === 'REMOTE_CONNECTION_CLOSED' && !newChange.data.id) {
  58. console.log(newChange);
  59. $rootScope.openCreateTicketForm(newChange.data);
  60. }
  61. })
  62. });
  63. }
  64.  
  65. function callCallbacks() {
  66. for (var scopeId in storage.callbacks) {
  67. if (scopeId === 'staticCopy') continue;
  68. var callback = storage.callbacks[scopeId];
  69. if (callback && callback.callback && typeof callback.callback === "function") {
  70. callback.callback(storage.devices[callback.companyId]);
  71. }
  72. }
  73. }
  74.  
  75. function destroyCallbackByScope(scope) {
  76. for (var scopeId in storage.callbacks) {
  77. if (companyId === 'staticCopy') continue;
  78. if (scopeId === scope.$id) {
  79. delete storage.callbacks[scopeId]
  80. }
  81. }
  82. }
  83.  
  84. function calculateDevicesCountersAndCallCallbacks() {
  85. for (var companyId in storage.devices) {
  86. if (companyId === 'staticCopy') continue;
  87.  
  88. if (storage.devices[companyId].length == 0) {
  89. delete storage.devices[companyId];
  90. delete storage.deviceCounter[companyId];
  91. callCallbacks();
  92. return;
  93. }
  94.  
  95. if (!storage.deviceCounter.companyId) {
  96. storage.deviceCounter[companyId] = Utils.customizeObj({all: 0, online: 0});
  97. }
  98.  
  99. Utils.defineReadOnlyProperty(storage.deviceCounter[companyId], 'all', storage.devices[companyId].length);
  100. var online = 0;
  101.  
  102. for (var i = 0; i < storage.devices[companyId].length; i++) {
  103. if (storage.devices[companyId][i].online) {
  104. online++;
  105. }
  106. }
  107. Utils.defineReadOnlyProperty(storage.deviceCounter[companyId], 'online', online);
  108.  
  109. }
  110. callCallbacks();
  111. }
  112.  
  113. function downloadAgent(companyId, userName, userPassword, callback) {
  114. if (!companyId) {
  115. throw "CompanyId is required!"
  116. }
  117.  
  118. return $http.get("/data/prepareDownloadAgent?companyId=" + companyId + "&userName=" + userName + "&userPassword=" + userPassword).then(function (response) {
  119. if (callback) {
  120. callback(response.data);
  121. }
  122. if (response.data.code >= 400) {
  123. return;
  124. }
  125. Downloader.downloadByUrl(response.data.link);
  126. });
  127. }
  128.  
  129. function createOneTimeRemoteSession(callback) {
  130. $http.get('/remote/createOneTimeRemoteSession').then(function (resp) {
  131. if (callback) {
  132. callback(resp.data);
  133. }
  134. }, function (resp) {
  135. if (callback) {
  136. callback(resp.data);
  137. }
  138. });
  139. }
  140.  
  141. function getRemoteSessions(scope, callback) {
  142.  
  143. if (!scope || !scope.$id) {
  144. throw 'First argument must be Angularjs scope!'
  145. }
  146.  
  147. return Utils.getListByUrl('/remote/getRemoteSessions', callback, function (list) {
  148. RealTimeService.subscribe('/oneTimeConnection', scope, function (newChange) {
  149. console.log(newChange);
  150.  
  151. if (newChange.action === 'ONE_TIME_CONNECTION_CLOSED' && !newChange.data.id) {
  152. //do nothing
  153. } else if (newChange.action === 'ONE_TIME_DELETED' || newChange.action === 'ONE_TIME_UNINSTALLED') {
  154. Utils.deleteById(list, newChange.data);
  155. } else {
  156. var session = Utils.findById(list, newChange.data.id);
  157. if (session) {
  158. Utils.customizeObj(newChange.data, session);
  159. } else {
  160. list.unshift(Utils.customizeObj(newChange.data));
  161. }
  162. }
  163. })
  164. });
  165. }
  166.  
  167. function deleteRemoteSession(ids, callback) {
  168. if (!ids) throw 'ids is required!';
  169.  
  170. if (!Array.isArray(ids)) ids = [ids];
  171.  
  172. var future = $http({
  173. url: '/remote/deleteRemoteSession',
  174. method: 'POST',
  175. data: ids
  176. });
  177.  
  178. Utils.handleCallback(future, callback);
  179. }
  180.  
  181. //one time connection
  182. function getSessionId(remoteSessionId, connectionType, callback) {
  183.  
  184. if (!remoteSessionId) throw 'remoteSessionId is required!';
  185.  
  186. if (!connectionType) throw 'connectionType is required!';
  187.  
  188. $http.get('/devices/getSessionId?remoteSessionId=' + remoteSessionId + '&connectionType=' + connectionType).then(function (resp) {
  189. if (callback) {
  190. callback(resp.data);
  191. }
  192. }, function (resp) {
  193. if (callback) {
  194. callback(resp.data);
  195. }
  196. });
  197. }
  198.  
  199. //regular connection
  200. function createRemoteSession(deviceId, companyId, connectionType, callback) {
  201.  
  202. if (!deviceId) throw 'deviceId is required!';
  203.  
  204. if (!companyId) throw 'companyId is required!';
  205.  
  206. if (!connectionType) throw 'connectionType is required!';
  207.  
  208. $http.get('/remote/createRemoteSession?deviceId=' + deviceId + '&connectionType=' + connectionType + '&companyId=' + companyId).then(function (resp) {
  209. if (callback) {
  210. callback(resp.data);
  211. }
  212. }, function (resp) {
  213. if (callback) {
  214. callback(resp.data);
  215. }
  216. });
  217. }
  218.  
  219. function sendInstallAgentRequest(companyId, agentId, devicesIds, callback) {
  220. if (!companyId) {
  221. throw "CompanyId is required!"
  222. }
  223. if (!agentId) {
  224. throw "agentId is required!"
  225. }
  226. if (!devicesIds) {
  227. throw "agentId is required!"
  228. }
  229.  
  230. if (!Array.isArray(devicesIds)) devicesIds = [devicesIds];
  231.  
  232. if (callback && typeof callback === "function") {
  233. for (var i in devicesIds) {
  234. callbacksForRemoteInstall[devicesIds[i]] = callback;
  235. }
  236. }
  237.  
  238. $http({
  239. url: '/devices/sendInstallAgentRequest?companyId=' + companyId + '&agentId=' + agentId,
  240. method: 'POST',
  241. data: devicesIds
  242. });
  243. }
  244.  
  245. function sendUninstallAgentRequest(companyId, agentId, devicesIds, callback) {
  246. if (!companyId) {
  247. throw "CompanyId is required!"
  248. }
  249. if (!agentId) {
  250. throw "agentId is required!"
  251. }
  252. if (!devicesIds) {
  253. throw "agentId is required!"
  254. }
  255.  
  256. if (!Array.isArray(devicesIds)) devicesIds = [devicesIds];
  257.  
  258. if (callback && typeof callback === "function") {
  259. for (var i in devicesIds) {
  260. callbacksForRemoteInstall[devicesIds[i]] = callback;
  261. }
  262. }
  263.  
  264. $http({
  265. url: '/devices/sendUninstallAgentRequest?companyId=' + companyId + '&agentId=' + agentId,
  266. method: 'POST',
  267. data: devicesIds
  268. });
  269. }
  270.  
  271. function isCompanyHasUserNameAndPass(companyId, callback) {
  272. if (!companyId) {
  273. throw "CompanyId is required!"
  274. }
  275.  
  276. $http.get("/devices/isCompanyHasUserNameAndPass?companyId=" + companyId).then(function (response) {
  277. if (callback) {
  278. callback(response.data);
  279. }
  280. return response.data;
  281. });
  282. }
  283.  
  284. function getDevicesCount(scope, callback) {
  285. if (!scope) {
  286. throw "scope is required!"
  287. }
  288.  
  289. storage.callbacks[scope.$id] = {
  290. companyId: CompaniesService.getCurrentCompany().id,
  291. callback: callback
  292. };
  293.  
  294. if (storage.devices && callback) {
  295. callback(storage.devices);
  296. }
  297.  
  298. return storage.deviceCounter;
  299. }
  300.  
  301. function getAgentsByCompany(scope, companyId, callback) {
  302.  
  303. if (!scope) {
  304. throw "scope is required!"
  305. }
  306. if (!companyId) {
  307. throw "CompanyId is required!"
  308. }
  309.  
  310. if (callback) {
  311. storage.callbacks[scope.$id] = {
  312. companyId: companyId,
  313. callback: callback
  314. };
  315. }
  316.  
  317. if (storage.devices[companyId] && callback) {
  318. callback(storage.devices[companyId]);
  319. }
  320. return storage.devices[companyId];
  321.  
  322. }
  323.  
  324. function runScanDevices(scope, companyId, agentId, callback) {
  325.  
  326. if (!scope) {
  327. throw "scope is required!"
  328. }
  329.  
  330. if (!companyId) {
  331. throw "CompanyId is required!"
  332. }
  333.  
  334. if (!agentId) {
  335. throw "AgentId is required!"
  336. }
  337.  
  338. var info = Utils.customizeObj({});
  339. scope.isScanDevicesLoad = true;
  340.  
  341. $http.get('/devices/refreshNetworkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (error) {
  342. console.log(error);
  343. });
  344.  
  345. $http.get('/devices/networkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (resp) {
  346. Utils.customizeObj(resp.data, info);
  347.  
  348. if (info.networkScan) {
  349. Utils.defineReadOnlyProperty(info, "networkScan", JSON.parse(info.networkScan));
  350. }
  351.  
  352. RealTimeService.subscribe('/netScan', scope, function (newChange) {
  353. console.log(newChange);
  354. if (newChange.action === 'NETWORK_SCAN') {
  355. $http.get('/devices/networkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (resp) {
  356. Utils.customizeObj(resp.data, info);
  357. Utils.defineReadOnlyProperty(info, "networkScan", JSON.parse(info.networkScan));
  358. if (newChange.data === 'Finished') {
  359. scope.isScanDevicesLoad = false;
  360. }
  361. });
  362. } else if (newChange.action === 'REMOTE_INSTALL') {
  363.  
  364. var device = (Utils.findById(info.networkScan.devices, newChange.data.netScanId)) || (Utils.findById(info.networkScan.devices, newChange.data.deviceId));
  365.  
  366. if (device) {
  367. if (newChange.data.status === 'MANAGED') {
  368. Utils.defineReadOnlyProperty(device, "agentStatus", 'managed');
  369. } else if (newChange.data.status === 'INSTALLED') {
  370. Utils.defineReadOnlyProperty(device, "agentStatus", 'installed');
  371. } else if (newChange.data.status === 'ERROR') {
  372. Utils.defineReadOnlyProperty(device, "agentStatus", 'error');
  373. } else if (newChange.data.status === 'LOGIN_ERROR') {
  374. Utils.defineReadOnlyProperty(device, "agentStatus", 'login error');
  375. } else if (newChange.data.status === 'UNKNOWN') {
  376. Utils.defineReadOnlyProperty(device, "agentStatus", 'unknown');
  377. } else if (newChange.data.status === 'INIT') {
  378. Utils.defineReadOnlyProperty(device, "agentStatus", 'init');
  379. } else if (newChange.data.status === 'IN_PROGRESS') {
  380. Utils.defineReadOnlyProperty(device, "agentStatus", 'inProgress');
  381. }
  382. Utils.defineReadOnlyProperty(device, "message", newChange.data.content);
  383. }
  384.  
  385. var call = callbacksForRemoteInstall[newChange.data.netScanId] || callbacksForRemoteInstall[newChange.data.deviceId];
  386. if (call && typeof call === "function" && (newChange.data.status !== 'INIT' || newChange.data.status !== 'IN_PROGRESS')) {
  387. call();
  388. }
  389. }
  390. if (callback) {
  391. callback(newChange.data);
  392. }
  393. });
  394.  
  395. if (callback) {
  396. callback();
  397. }
  398.  
  399. }, function (resp) {
  400. if (callback) {
  401. callback();
  402. }
  403. });
  404. return info;
  405. }
  406.  
  407. return {
  408. downloadAgent: downloadAgent,
  409. runScanDevices: runScanDevices,
  410. getDevicesCount: getDevicesCount,
  411. getAgentsByCompany: getAgentsByCompany,
  412. isCompanyHasUserNameAndPass: isCompanyHasUserNameAndPass,
  413. sendInstallAgentRequest: sendInstallAgentRequest,
  414. sendUninstallAgentRequest: sendUninstallAgentRequest,
  415. createOneTimeRemoteSession: createOneTimeRemoteSession,
  416. getRemoteSessions: getRemoteSessions,
  417. getSessionId: getSessionId,
  418. createRemoteSession: createRemoteSession,
  419. deleteRemoteSession: deleteRemoteSession
  420. }
  421. }
  422. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement