Advertisement
Guest User

Untitled

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