Advertisement
Guest User

Untitled

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