Advertisement
vitareinforce

worker-multimachine-v2

Mar 8th, 2023
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.18 KB | None | 0 0
  1. // Packages
  2. const axios = require('axios')
  3. const soapRequest = require('easy-soap-request')
  4. const convert = require('xml2js').parseString
  5. const startDate = new Date()
  6. const endDate = new Date()
  7. const fs = require('fs')
  8. require('dotenv').config()
  9.  
  10. // set periode pengiriman data
  11. const periodic = parseInt(process.env.periodic)
  12.  
  13. function sleep(millis) {
  14. return new Promise(resolve => setTimeout(resolve, millis));
  15. }
  16.  
  17. function sleep(millis) {
  18. return new Promise(resolve => setTimeout(resolve, millis));
  19. }
  20.  
  21. async function getRequestMachine() {
  22.  
  23. let ip_address = process.env.ip_address
  24. let ip_address1 = process.env.ip_address1
  25. let ip_address2 = process.env.ip_address2
  26. let ip_address3 = process.env.ip_address3
  27. let ip_address4 = process.env.ip_address4
  28. let ip_address5 = process.env.ip_address5
  29. let ip_address6 = process.env.ip_address6
  30. let ip_address7 = process.env.ip_address7
  31. let ip_address8 = process.env.ip_address8 // kabel rusak
  32. let ip_address9 = process.env.ip_address9
  33. let ip_address10 = process.env.ip_address10 // kabel rusak
  34. let ip_address11 = process.env.ip_address11
  35.  
  36. /**
  37. * Sementara, buat data tembak
  38. */
  39. // const startDate = moment("2022-12-31 00:00:00")
  40. // const endDate = moment("2023-02-04 00:00:00")
  41.  
  42. console.log("Machine IP:" + ip_address)
  43. console.log("Machine IP:" + ip_address1)
  44. console.log("Machine IP:" + ip_address2)
  45. console.log("Machine IP:" + ip_address3)
  46. console.log("Machine IP:" + ip_address4)
  47. console.log("Machine IP:" + ip_address5)
  48. console.log("Machine IP:" + ip_address6)
  49. console.log("Machine IP:" + ip_address7)
  50. console.log("Machine IP:" + ip_address8)
  51. console.log("Machine IP:" + ip_address9)
  52. console.log("Machine IP:" + ip_address10)
  53. console.log("Machine IP:" + ip_address11)
  54.  
  55. // penampung data
  56. let newData = []
  57.  
  58. // tanggal
  59. const newStartDate = startDate.getTime()
  60. const newEndDate = endDate.getTime()
  61.  
  62. const url = 'http://'+ ip_address +'/iWsService'
  63. const url1 = 'http://'+ ip_address1 +'/iWsService'
  64. const url2 = 'http://'+ ip_address2 +'/iWsService'
  65. const url3 = 'http://'+ ip_address3 +'/iWsService'
  66. const url4 = 'http://'+ ip_address4 +'/iWsService'
  67. const url5 = 'http://'+ ip_address5 +'/iWsService'
  68. const url6 = 'http://'+ ip_address6 +'/iWsService'
  69. const url7 = 'http://'+ ip_address7 +'/iWsService'
  70. const url8 = 'http://'+ ip_address8 +'/iWsService'
  71. const url9 = 'http://'+ ip_address9 +'/iWsService'
  72. const url10 = 'http://'+ ip_address10 +'/iWsService'
  73. const url11 = 'http://'+ ip_address11 +'/iWsService'
  74.  
  75. let response = null
  76. let response1 = null
  77. let response2 = null
  78. let response3 = null
  79. let response4 = null
  80. let response5 = null
  81. let response6 = null
  82. let response7 = null
  83. let response8 = null
  84. let response9 = null
  85. let response10 = null
  86. let response11 = null
  87.  
  88.  
  89.  
  90. const destination_url = process.env.server_api
  91. const header = {
  92. 'user-agent': 'chrome',
  93. 'Content-Type': 'text/xml;charset=UTF-8',
  94. }
  95.  
  96. // request data
  97. const request_attendance = `<?xml version="1.0" encoding="utf-8"?>
  98. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  99. <soap:Body>
  100. <GetAttLog>
  101. <ArgComKey xsi:type="xsd:integer">0</ArgComKey>
  102. <Arg><PIN xsi:type="xsd:integer">All</PIN></Arg>
  103. </GetAttLog>
  104. </soap:Body>
  105. </soap:Envelope>`;
  106.  
  107. try {
  108. response = await soapRequest({ url: url, headers: header, xml: request_attendance, timeout: periodic })
  109. } catch(e) {
  110. console.log("Error Mesin : " + e.message)
  111. }
  112. try {
  113. response1 = await soapRequest({ url: url1, headers: header, xml: request_attendance, timeout: periodic })
  114. } catch(e) {
  115. console.log("Error Mesin : " + e.message)
  116. }
  117. try {
  118. response2 = await soapRequest({ url: url2, headers: header, xml: request_attendance, timeout: periodic })
  119. } catch(e) {
  120. console.log("Error Mesin : " + e.message)
  121. }
  122. try {
  123. response3 = await soapRequest({ url: url3, headers: header, xml: request_attendance, timeout: periodic })
  124. } catch(e) {
  125. console.log("Error Mesin : " + e.message)
  126. }
  127. try {
  128. response4 = await soapRequest({ url: url4, headers: header, xml: request_attendance, timeout: periodic })
  129. } catch(e) {
  130. console.log("Error Mesin : " + e.message)
  131. }
  132. try {
  133. response5 = await soapRequest({ url: url5, headers: header, xml: request_attendance, timeout: periodic })
  134. } catch(e) {
  135. console.log("Error Mesin : " + e.message)
  136. }
  137. try {
  138. response6 = await soapRequest({ url: url6, headers: header, xml: request_attendance, timeout: periodic })
  139. } catch(e) {
  140. console.log("Error Mesin : " + e.message)
  141. }
  142. try {
  143. response7 = await soapRequest({ url: url7, headers: header, xml: request_attendance, timeout: periodic })
  144. } catch(e) {
  145. console.log("Error Mesin : " + e.message)
  146. }
  147. try {
  148. response8 = await soapRequest({ url: url8, headers: header, xml: request_attendance, timeout: periodic })
  149. } catch(e) {
  150. console.log("Error Mesin : " + e.message)
  151. }
  152. try {
  153. response9 = await soapRequest({ url: url9, headers: header, xml: request_attendance, timeout: periodic })
  154. } catch(e) {
  155. console.log("Error Mesin : " + e.message)
  156. }
  157. try {
  158. response10 = await soapRequest({ url: url10, headers: header, xml: request_attendance, timeout: periodic })
  159. } catch(e) {
  160. console.log("Error Mesin : " + e.message)
  161. }
  162. try {
  163. response11 = await soapRequest({ url: url11, headers: header, xml: request_attendance, timeout: periodic })
  164. } catch(e) {
  165. console.log("Error Mesin : " + e.message)
  166. }
  167.  
  168. if(response != null && response.response.statusCode == 200) {
  169. console.log("Sukses koneksi ke mesin " + ip_address)
  170. /**
  171. * Convert manual sistem
  172. */
  173. convert(response.response.body.toString(), async function (err, msg) {
  174. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  175. for(i = 0; i < arrayResponse.length; i++) {
  176. // const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  177. // const newJsonDate = jsonDate.getTime()
  178.  
  179. // if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  180. newData.push(arrayResponse[i])
  181. // }
  182. }
  183. })
  184. }
  185. if(response1 != null && response1.response.statusCode == 200) {
  186. console.log("Sukses koneksi ke mesin " + ip_address1)
  187. /**
  188. * Convert manual sistem
  189. */
  190. convert(response1.response.body.toString(), async function (err, msg) {
  191. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  192. for(i = 0; i < arrayResponse.length; i++) {
  193. // const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  194. // const newJsonDate = jsonDate.getTime()
  195.  
  196. // if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  197. newData.push(arrayResponse[i])
  198. // }
  199. }
  200. })
  201. }
  202. if(response2 != null && response2.response.statusCode == 200) {
  203. console.log("Sukses koneksi ke mesin " + ip_address2)
  204. /**
  205. * Convert manual sistem
  206. */
  207. convert(response2.response.body.toString(), async function (err, msg) {
  208. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  209. for(i = 0; i < arrayResponse.length; i++) {
  210. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  211. const newJsonDate = jsonDate.getTime()
  212.  
  213. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  214. newData.push(arrayResponse[i])
  215. }
  216. }
  217. })
  218. }
  219. if(response3 != null && response3.response.statusCode == 200) {
  220. console.log("Sukses koneksi ke mesin " + ip_address3)
  221. /**
  222. * Convert manual sistem
  223. */
  224. convert(response3.response.body.toString(), async function (err, msg) {
  225. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  226. for(i = 0; i < arrayResponse.length; i++) {
  227. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  228. const newJsonDate = jsonDate.getTime()
  229.  
  230. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  231. newData.push(arrayResponse[i])
  232. }
  233. }
  234. })
  235. }
  236. if(response4 != null && response4.response.statusCode == 200) {
  237. console.log("Sukses koneksi ke mesin " + ip_address4)
  238. /**
  239. * Convert manual sistem
  240. */
  241. convert(response4.response.body.toString(), async function (err, msg) {
  242. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  243. for(i = 0; i < arrayResponse.length; i++) {
  244. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  245. const newJsonDate = jsonDate.getTime()
  246.  
  247. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  248. newData.push(arrayResponse[i])
  249. }
  250. }
  251. })
  252. }
  253. if(response5 != null && response5.response.statusCode == 200) {
  254. console.log("Sukses koneksi ke mesin " + ip_address5)
  255. /**
  256. * Convert manual sistem
  257. */
  258. convert(response5.response.body.toString(), async function (err, msg) {
  259. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  260. for(i = 0; i < arrayResponse.length; i++) {
  261. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  262. const newJsonDate = jsonDate.getTime()
  263.  
  264. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  265. newData.push(arrayResponse[i])
  266. }
  267. }
  268. })
  269. }
  270. if(response6 != null && response6.response.statusCode == 200) {
  271. console.log("Sukses koneksi ke mesin " + ip_address6)
  272. /**
  273. * Convert manual sistem
  274. */
  275. convert(response6.response.body.toString(), async function (err, msg) {
  276. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  277. for(i = 0; i < arrayResponse.length; i++) {
  278. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  279. const newJsonDate = jsonDate.getTime()
  280.  
  281. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  282. newData.push(arrayResponse[i])
  283. }
  284. }
  285. })
  286. }
  287. if(response7 != null && response7.response.statusCode == 200) {
  288. console.log("Sukses koneksi ke mesin " + ip_address7)
  289. /**
  290. * Convert manual sistem
  291. */
  292. convert(response7.response.body.toString(), async function (err, msg) {
  293. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  294. for(i = 0; i < arrayResponse.length; i++) {
  295. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  296. const newJsonDate = jsonDate.getTime()
  297.  
  298. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  299. newData.push(arrayResponse[i])
  300. }
  301. }
  302. })
  303. }
  304. if(response8 != null && response8.response.statusCode == 200) {
  305. console.log("Sukses koneksi ke mesin " + ip_address8)
  306. /**
  307. * Convert manual sistem
  308. */
  309. convert(response8.response.body.toString(), async function (err, msg) {
  310. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  311. for(i = 0; i < arrayResponse.length; i++) {
  312. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  313. const newJsonDate = jsonDate.getTime()
  314.  
  315. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  316. newData.push(arrayResponse[i])
  317. }
  318. }
  319. })
  320. }
  321. if(response9 != null && response9.response.statusCode == 200) {
  322. console.log("Sukses koneksi ke mesin " + ip_address9)
  323. /**
  324. * Convert manual sistem
  325. */
  326. convert(response9.response.body.toString(), async function (err, msg) {
  327. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  328. for(i = 0; i < arrayResponse.length; i++) {
  329. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  330. const newJsonDate = jsonDate.getTime()
  331.  
  332. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  333. newData.push(arrayResponse[i])
  334. }
  335. }
  336. })
  337. }
  338. if(response10 != null && response10.response.statusCode == 200) {
  339. console.log("Sukses koneksi ke mesin " + ip_address10)
  340. /**
  341. * Convert manual sistem
  342. */
  343. convert(response10.response.body.toString(), async function (err, msg) {
  344. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  345. for(i = 0; i < arrayResponse.length; i++) {
  346. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  347. const newJsonDate = jsonDate.getTime()
  348.  
  349. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  350. newData.push(arrayResponse[i])
  351. }
  352. }
  353. })
  354. }
  355. if(response11 != null && response11.response.statusCode == 200) {
  356. console.log("Sukses koneksi ke mesin " + ip_address11)
  357. /**
  358. * Convert manual sistem
  359. */
  360. convert(response11.response.body.toString(), async function (err, msg) {
  361. let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
  362. for(i = 0; i < arrayResponse.length; i++) {
  363. const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
  364. const newJsonDate = jsonDate.getTime()
  365.  
  366. if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
  367. newData.push(arrayResponse[i])
  368. }
  369. }
  370. })
  371. }
  372.  
  373. // console.log(newData)
  374.  
  375. try {
  376. /**
  377. * set JSON without IP Address information
  378. * hanya body (message) saja yang dibawa ke controller
  379. */
  380. let json = {
  381. "message": newData
  382. }
  383.  
  384. // fs.writeFile('rawdata.'+ip+'.txt', body.toString(), function (err) {
  385. // if (err) return console.log(err);
  386. // console.log('saved log file xml');
  387. // })
  388.  
  389. fs.writeFile('machinedata_all_machine.txt', JSON.stringify(json), function (err) {
  390. if (err) {
  391. return console.log(err);
  392. }
  393. console.log('saved log file json');
  394. })
  395.  
  396. /**
  397. * ngirim data jgn di compress
  398. */
  399. axios.post(destination_url, json).then(function (post) {
  400. console.log("Status Kirim Ke Server = " + post.status)
  401. })
  402. } catch(e) {
  403. console.log("Error Server = " + e.message)
  404. }
  405. }
  406. async function main() {
  407. getRequestMachine()
  408. await sleep(periodic)
  409. }
  410.  
  411. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement