Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios')
- const convert = require('xml2js').parseString
- const fs = require('fs')
- const moment = require('moment')
- const dotenv = require('dotenv')
- dotenv.config()
- const header = {
- 'User-Agent': 'Chrome',
- 'Content-Type': 'text/xml;charset=UTF-8',
- }
- const request_mesin = `<?xml version="1.0" encoding="utf-8"?>
- <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/">
- <soap:Body>
- <GetAttLog>
- <ArgComKey xsi:type="xsd:integer">0</ArgComKey>
- <Arg><PIN xsi:type="xsd:integer">All</PIN></Arg>
- </GetAttLog>
- </soap:Body>
- </soap:Envelope>`
- let timeout = 0
- let ip_mesin = process.env.IP_MESIN3
- let newData = []
- async function main(index) {
- try {
- let execution_time = 0
- let start_execution_time = moment()
- axios({ method: 'POST',
- url: 'http://'+ ip_mesin +'/iWsService',
- headers: header, data: request_mesin, keepAlive: true, maxBodyLength: Infinity, maxContentLength: Infinity })
- .then(function (res) {
- execution_time = moment().diff(start_execution_time, 'milliseconds')
- timeout = timeout + execution_time
- console.log('waktu timeout = ' + timeout)
- console.log('tarik dari ' + ip_mesin)
- // if(res.status = 200) {
- console.log("ambil data dari " + res.config ? res.config.url : null)
- if(res.data && res.data.length > 0) {
- convert(res.data.toString(), function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- console.log("Jumlah data yang ditarik " + arrayResponse.length)
- for(let i = 0; i < arrayResponse.length; i++) {
- if(i >= index) {
- // start get execution time
- let start_execution_time = moment()
- arrayResponse[i].IpAddress = ip_mesin
- arrayResponse[i].Type = 'rutin'
- newData.push(arrayResponse[i])
- execution_time = moment().diff(start_execution_time, 'milliseconds')
- if(execution_time <= 2) {
- if(res.status = 200) {
- timeout = timeout + execution_time
- // console.log('di bawah 2 detik')
- // console.log('waktu timeout = ' + timeout)
- // next loop
- } else {
- timeout = timeout + execution_time
- // console.log('di atas 2 detik')
- // console.log('waktu timeout = ' + timeout)
- setTimeout(() => {
- console.log('Restarting on index ' + i)
- main(i)
- }, timeout)
- }
- } else {
- execution_time = moment().diff(start_execution_time, 'miliseconds')
- timeout = timeout + execution_time
- // console.log('waktu timeout = ' + timeout)
- setTimeout(() => {
- console.log('restarting on index ' + i)
- main(i)
- }, timeout)
- }
- if(i == arrayResponse.length - 1) {
- if(arrayResponse.length == newData.length) {
- fs.writeFile(ip_mesin + '.txt', JSON.stringify(newData), function (err) {
- if (err) {
- return console.log(err);
- }
- fs.closeSync(fs.openSync(ip_mesin + '.txt', 'r'))
- console.log('saved raw')
- console.log("Jumlah data yang ditarik " + arrayResponse.length)
- console.log('jumlah data yang disimpan = ' + newData.length)
- console.log('waktu timeout = ' + timeout)
- newData = []
- timeout = 0
- setTimeout(() => {
- console.log('restarting after save')
- main(0)
- }, (parseInt(timeout) + parseInt(process.env.TIMEOUT_MESIN)))
- })
- } else {
- newData = []
- timeout = 0
- setTimeout(() => {
- console.log('restarting because not match')
- main(0)
- }, timeout)
- }
- }
- }
- }
- })
- // }
- }
- }).catch(function (error) {
- console.log(error)
- console.log('waiting')
- newData = []
- timeout = 0
- setTimeout(() => {
- console.log('restarting on error')
- main(0)
- }, timeout)
- })
- } catch(err) {
- console.log(err)
- }
- }
- main(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement