Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.40 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 === 'ONE_TIME_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 && typeof 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. function getSessionId(remoteSessionId, connectionType, callback) {
  182.  
  183. if (!remoteSessionId) throw 'remoteSessionId is required!';
  184.  
  185. if (!connectionType) throw 'connectionType is required!';
  186.  
  187. $http.get('/devices/getSessionId?remoteSessionId=' + remoteSessionId + '&connectionType=' + connectionType).then(function (resp) {
  188. if (callback) {
  189. callback(resp.data);
  190. }
  191. }, function (resp) {
  192. if (callback) {
  193. callback(resp.data);
  194. }
  195. });
  196. }
  197.  
  198. function sendInstallAgentRequest(companyId, agentId, devicesIds, callback) {
  199. if (!companyId) {
  200. throw "CompanyId is required!"
  201. }
  202. if (!agentId) {
  203. throw "agentId is required!"
  204. }
  205. if (!devicesIds) {
  206. throw "agentId is required!"
  207. }
  208.  
  209. if (!Array.isArray(devicesIds)) devicesIds = [devicesIds];
  210.  
  211. if (callback && typeof callback === "function") {
  212. for (var deviceId in devicesIds) {
  213. callbacksForRemoteInstall[deviceId] = callback;
  214. }
  215. }
  216.  
  217. var future = $http({
  218. url: '/devices/sendInstallAgentRequest?companyId=' + companyId + '&agentId=' + agentId,
  219. method: 'POST',
  220. data: devicesIds
  221. });
  222.  
  223. Utils.handleCallback(future, callback);
  224. }
  225.  
  226. function isCompanyHasUserNameAndPass(companyId, callback) {
  227. if (!companyId) {
  228. throw "CompanyId is required!"
  229. }
  230.  
  231. $http.get("/devices/isCompanyHasUserNameAndPass?companyId=" + companyId).then(function (response) {
  232. if (callback) {
  233. callback(response.data);
  234. }
  235. return response.data;
  236. });
  237. }
  238.  
  239. function getDevicesCount(scope, callback) {
  240. if (!scope) {
  241. throw "scope is required!"
  242. }
  243.  
  244. storage.callbacks[scope.$id] = {
  245. companyId: CompaniesService.getCurrentCompany().id,
  246. callback: callback
  247. };
  248.  
  249. if (storage.devices && callback) {
  250. callback(storage.devices);
  251. }
  252.  
  253. return storage.deviceCounter;
  254. }
  255.  
  256. function getAgentsByCompany(scope, companyId, callback) {
  257.  
  258. if (!scope) {
  259. throw "scope is required!"
  260. }
  261. if (!companyId) {
  262. throw "CompanyId is required!"
  263. }
  264.  
  265. if (callback) {
  266. storage.callbacks[scope.$id] = {
  267. companyId: companyId,
  268. callback: callback
  269. };
  270. }
  271.  
  272. if (storage.devices[companyId] && callback) {
  273. callback(storage.devices[companyId]);
  274. }
  275. return storage.devices[companyId];
  276.  
  277. }
  278.  
  279. function runScanDevices(scope, companyId, agentId, callback) {
  280.  
  281. if (!scope) {
  282. throw "scope is required!"
  283. }
  284.  
  285. if (!companyId) {
  286. throw "CompanyId is required!"
  287. }
  288.  
  289. if (!agentId) {
  290. throw "AgentId is required!"
  291. }
  292.  
  293. var info = Utils.customizeObj({});
  294. scope.isScanDevicesLoad = true;
  295.  
  296. $http.get('/devices/refreshNetworkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (error) {
  297. console.log(error);
  298. });
  299.  
  300. $http.get('/devices/networkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (resp) {
  301. Utils.customizeObj(resp.data, info);
  302.  
  303. if (info.networkScan) {
  304. Utils.defineReadOnlyProperty(info, "networkScan", JSON.parse(info.networkScan));
  305. }
  306.  
  307. RealTimeService.subscribe('/netScan', scope, function (newChange) {
  308. console.log(newChange);
  309. if (newChange.action === 'NETWORK_SCAN') {
  310. $http.get('/devices/networkScan?companyId=' + companyId + "&agentId=" + agentId).then(function (resp) {
  311. Utils.customizeObj(resp.data, info);
  312. Utils.defineReadOnlyProperty(info, "networkScan", JSON.parse(info.networkScan));
  313. if (newChange.data === 'Finished') {
  314. scope.isScanDevicesLoad = false;
  315. }
  316. });
  317. } else if (newChange.action === 'REMOTE_INSTALL') {
  318. var call = callbacksForRemoteInstall[newChange.data.netScanId];
  319. if (call && typeof call === "function" && newChange.data.status === 'INSTALLED') {
  320. call();
  321. }
  322.  
  323. var device = Utils.findById(info.networkScan.devices, newChange.data.netScanId);
  324. if (device) {
  325. Utils.defineReadOnlyProperty(device, "message", newChange.data.content);
  326. if (newChange.data.status === 'INSTALLED') {
  327. Utils.defineReadOnlyProperty(device, "agentStatus", 'managed');
  328. }
  329. }
  330. }
  331. if (callback) {
  332. callback(newChange.data);
  333. }
  334. });
  335.  
  336. if (callback) {
  337. callback();
  338. }
  339.  
  340. }, function (resp) {
  341. if (callback) {
  342. callback();
  343. }
  344. });
  345. return info;
  346. }
  347.  
  348. return {
  349. downloadAgent: downloadAgent,
  350. runScanDevices: runScanDevices,
  351. getDevicesCount: getDevicesCount,
  352. getAgentsByCompany: getAgentsByCompany,
  353. isCompanyHasUserNameAndPass: isCompanyHasUserNameAndPass,
  354. sendInstallAgentRequest: sendInstallAgentRequest,
  355. createOneTimeRemoteSession: createOneTimeRemoteSession,
  356. getRemoteSessions: getRemoteSessions,
  357. getSessionId: getSessionId,
  358. deleteRemoteSession: deleteRemoteSession
  359. }
  360. }
  361. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement