Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Packages
- const axios = require('axios')
- const soapRequest = require('easy-soap-request')
- const convert = require('xml2js').parseString
- const startDate = new Date()
- const endDate = new Date()
- const fs = require('fs')
- require('dotenv').config()
- // set periode pengiriman data
- const periodic = parseInt(process.env.periodic)
- function sleep(millis) {
- return new Promise(resolve => setTimeout(resolve, millis));
- }
- function sleep(millis) {
- return new Promise(resolve => setTimeout(resolve, millis));
- }
- async function getRequestMachine() {
- let ip_address = process.env.ip_address
- let ip_address1 = process.env.ip_address1
- let ip_address2 = process.env.ip_address2
- let ip_address3 = process.env.ip_address3
- let ip_address4 = process.env.ip_address4
- let ip_address5 = process.env.ip_address5
- let ip_address6 = process.env.ip_address6
- let ip_address7 = process.env.ip_address7
- let ip_address8 = process.env.ip_address8 // kabel rusak
- let ip_address9 = process.env.ip_address9
- let ip_address10 = process.env.ip_address10 // kabel rusak
- let ip_address11 = process.env.ip_address11
- /**
- * Sementara, buat data tembak
- */
- // const startDate = moment("2022-12-31 00:00:00")
- // const endDate = moment("2023-02-04 00:00:00")
- console.log("Machine IP:" + ip_address)
- console.log("Machine IP:" + ip_address1)
- console.log("Machine IP:" + ip_address2)
- console.log("Machine IP:" + ip_address3)
- console.log("Machine IP:" + ip_address4)
- console.log("Machine IP:" + ip_address5)
- console.log("Machine IP:" + ip_address6)
- console.log("Machine IP:" + ip_address7)
- console.log("Machine IP:" + ip_address8)
- console.log("Machine IP:" + ip_address9)
- console.log("Machine IP:" + ip_address10)
- console.log("Machine IP:" + ip_address11)
- // penampung data
- let newData = []
- // tanggal
- const newStartDate = startDate.getTime()
- const newEndDate = endDate.getTime()
- const url = 'http://'+ ip_address +'/iWsService'
- const url1 = 'http://'+ ip_address1 +'/iWsService'
- const url2 = 'http://'+ ip_address2 +'/iWsService'
- const url3 = 'http://'+ ip_address3 +'/iWsService'
- const url4 = 'http://'+ ip_address4 +'/iWsService'
- const url5 = 'http://'+ ip_address5 +'/iWsService'
- const url6 = 'http://'+ ip_address6 +'/iWsService'
- const url7 = 'http://'+ ip_address7 +'/iWsService'
- const url8 = 'http://'+ ip_address8 +'/iWsService'
- const url9 = 'http://'+ ip_address9 +'/iWsService'
- const url10 = 'http://'+ ip_address10 +'/iWsService'
- const url11 = 'http://'+ ip_address11 +'/iWsService'
- let response = null
- let response1 = null
- let response2 = null
- let response3 = null
- let response4 = null
- let response5 = null
- let response6 = null
- let response7 = null
- let response8 = null
- let response9 = null
- let response10 = null
- let response11 = null
- const destination_url = process.env.server_api
- const header = {
- 'user-agent': 'chrome',
- 'Content-Type': 'text/xml;charset=UTF-8',
- }
- // request data
- const request_attendance = `<?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>`;
- try {
- response = await soapRequest({ url: url, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response1 = await soapRequest({ url: url1, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response2 = await soapRequest({ url: url2, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response3 = await soapRequest({ url: url3, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response4 = await soapRequest({ url: url4, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response5 = await soapRequest({ url: url5, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response6 = await soapRequest({ url: url6, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response7 = await soapRequest({ url: url7, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response8 = await soapRequest({ url: url8, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response9 = await soapRequest({ url: url9, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response10 = await soapRequest({ url: url10, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- try {
- response11 = await soapRequest({ url: url11, headers: header, xml: request_attendance, timeout: periodic })
- } catch(e) {
- console.log("Error Mesin : " + e.message)
- }
- if(response != null && response.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address)
- /**
- * Convert manual sistem
- */
- convert(response.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- // const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- // const newJsonDate = jsonDate.getTime()
- // if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- // }
- }
- })
- }
- if(response1 != null && response1.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address1)
- /**
- * Convert manual sistem
- */
- convert(response1.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- // const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- // const newJsonDate = jsonDate.getTime()
- // if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- // }
- }
- })
- }
- if(response2 != null && response2.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address2)
- /**
- * Convert manual sistem
- */
- convert(response2.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response3 != null && response3.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address3)
- /**
- * Convert manual sistem
- */
- convert(response3.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response4 != null && response4.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address4)
- /**
- * Convert manual sistem
- */
- convert(response4.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response5 != null && response5.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address5)
- /**
- * Convert manual sistem
- */
- convert(response5.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response6 != null && response6.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address6)
- /**
- * Convert manual sistem
- */
- convert(response6.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response7 != null && response7.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address7)
- /**
- * Convert manual sistem
- */
- convert(response7.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response8 != null && response8.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address8)
- /**
- * Convert manual sistem
- */
- convert(response8.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response9 != null && response9.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address9)
- /**
- * Convert manual sistem
- */
- convert(response9.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response10 != null && response10.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address10)
- /**
- * Convert manual sistem
- */
- convert(response10.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- if(response11 != null && response11.response.statusCode == 200) {
- console.log("Sukses koneksi ke mesin " + ip_address11)
- /**
- * Convert manual sistem
- */
- convert(response11.response.body.toString(), async function (err, msg) {
- let arrayResponse = msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetAttLogResponse'][0]['Row']
- for(i = 0; i < arrayResponse.length; i++) {
- const jsonDate = new Date(arrayResponse[i]['DateTime'][0])
- const newJsonDate = jsonDate.getTime()
- if(newJsonDate > newStartDate && newJsonDate <= newEndDate) {
- newData.push(arrayResponse[i])
- }
- }
- })
- }
- // console.log(newData)
- try {
- /**
- * set JSON without IP Address information
- * hanya body (message) saja yang dibawa ke controller
- */
- let json = {
- "message": newData
- }
- // fs.writeFile('rawdata.'+ip+'.txt', body.toString(), function (err) {
- // if (err) return console.log(err);
- // console.log('saved log file xml');
- // })
- fs.writeFile('machinedata_all_machine.txt', JSON.stringify(json), function (err) {
- if (err) {
- return console.log(err);
- }
- console.log('saved log file json');
- })
- /**
- * ngirim data jgn di compress
- */
- axios.post(destination_url, json).then(function (post) {
- console.log("Status Kirim Ke Server = " + post.status)
- })
- } catch(e) {
- console.log("Error Server = " + e.message)
- }
- }
- async function main() {
- getRequestMachine()
- await sleep(periodic)
- }
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement