exports.id = 'sokolovodchodyspojov'; exports.title = 'Sokolov odchody spojov'; exports.version = '1.0.0'; exports.group = 'Worksys'; exports.color = '#2134B0'; exports.input = 1; exports.output = ["blue", "white", "yellow", "red"]; exports.click = false; exports.author = 'Rastislav Kovac'; exports.options = { edge: "undefined" }; exports.readme = `Get bus departures from bus stop in Sokolov`; const instanceSendTo = { debug: 0, departures: 1, delays: 2 }; exports.install = function(instance) { const parseString = require('xml2js').parseString; const fetch = require('node-fetch'); const { execSync } = require('child_process'); class FetchRequest { async getDepartures() { // use simpleDate to get bus departures for current day, otherwise use date to get departures for next day // it must be in format 2022-12-05 console.log(new Date()) //let date = new Date().toLocaleString().split('/'); //[ '12', '6', '2022, 11:47:32 PM' ] // console.log(date) // console.log(new Date().toLocaleString('zh', { hour12: false })) // console.log(new Date().toLocaleString('sk', { hour12: false })) const date = new Date(new Date().toLocaleString("en-US", {timeZone: "Europe/Bratislava" })) const year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); console.log(year, month, day) if(('' + month).length == 1) month = '0' + month; if(('' + day).length == 1) day = '0' + day; // if(date[0].length == 1) date[0] = '0' + date[0]; // if(date[1].length == 1) date[1] = '0' + date[1]; // const year = date[2].slice(0,4); // "2022" // const month = date[0]; // const day = date[1]; const simpleDate = `${year}-${month}-${day}`; console.log('simple date ~~~~~~', simpleDate) const sr = '' + ' ' + '' + '' + '411000469' + '0' + `${simpleDate}` + // '2022-12-05' + `${simpleDate}` + // '2022-12-05' + '411' + '' + '' + ''; let response = await fetch('http://62.141.28.141:5533/CPWebSvc2/service.asmx', { method : "POST", body: sr, headers: {'Content-Type':'application/soap+xml'} }) //console.log('bus departures``````````', response) console.log('response ok ^^^^^^ dep',response.ok); if (!response.ok) { const message = `An error has occured: ${response.status}`; throw new Error(message); } response = await response.text(); //console.log('bus departures``````````', response) const result = this.parseDepartures(response); return result; } parseDepartures(data) { const allRegex = [/tn="[a-z]*/g, /lnt="[a-z]*/g, /td="[a-z]*/g, /ebn="[a-z]*/g]; let index = 0; //we receive all departures for current day const allDepartures = []; //we filter just departures which are in future const futureDepartures = []; let value = null; //for (const regexp of allRegex) for (let i = 0; i < allRegex.length; i++) { index = 0; const matches = data.matchAll(allRegex[i]); // console.log('-----', matches) for (const match of matches) { if(!allDepartures[index]) allDepartures[index] = []; //console.log(`Found ${match[0]} start=${match.index} end=${match.index + match[0].length}.`); const substr = match.input.slice(match.index + match[0].length, ); const endOfSearchedString = substr.indexOf('"'); if(i == 1) value = substr.slice(4, endOfSearchedString); // bus number e.g. '7' else if (i == 3) value = substr.slice(13, capitalizeFirstLetter(endOfSearchedString)); //name of buss stop e.g. "Sidlisko Michal skola" else if (i == 2) { // in case date, or time is just 1 number long (5.7.22 4:5), we need to change it to (05.07.22 04:05) value = substr.slice(0, endOfSearchedString); // "5.11.22 4:5" let date = value.slice(0, value.indexOf(' ')); // "5.11.22" date = date.split('.') // ['5', '11', '22'] if(date[0].length == 1) date[0] = '0' + date[0]; if(date[1].length == 1) date[1] = '0' + date[1]; date = date.join('.') let time = value.slice(value.indexOf(' ') + 1, value.length); // "4:5" ==> '04:05' time = time.split(':'); if(time[0].length == 1) time[0] = '0' + time[0]; if(time[1].length == 1) time[1] = '0' + time[1]; time = time.join(':') value = date + ' ' + time; } else value = substr.slice(0, endOfSearchedString); allDepartures[index].push(value); index++; } } //return allDepartures; // console.log('---',allDepartures); //instance.send(instanceSendTo.allDepartures, allDepartures); //we filter just departures, that are in future let date = new Date(); date = date.toISOString(); console.log('******date', date) allDepartures.map(item => { let busDeparture = item[2]; let temp = [...busDeparture.matchAll(/\d\d/g)].map(a => a[0]); //[ '24', '11', '22', '04', '05' ] busDeparture = new Date(`20${temp[2]}-${temp[1]}-${temp[0]} ${temp[3]}:${temp[4]}`) //console.log(busDeparture.toISOString(), date.toISOString()) //date je UTC time, preto sa zmenia allDepartures az po 01:00 //console.log('***********',busDeparture, date) if(busDeparture.toISOString() > date) { item[2] = `${temp[3]}:${temp[4]}` futureDepartures.push(item); } }) return futureDepartures; } async getDelays() { const delays = []; const d = new Date(); const date = d.toISOString(); const sr = '' + ' ' + '' + '' + '411000469' + `${date}` + '10' + '0' + '411' + '' + '' + ''; let response = await fetch('http://62.141.28.141:5533/CPWebSvc2/service.asmx', { method : "POST", body: sr, headers: {'Content-Type':'application/soap+xml'} }) //console.log('delays ------', response); //console.log('response ok ^^^^^^ dep',response.ok); if (!response.ok) { const message = `An error has occured: ${response.status}`; throw new Error(message); } response = await response.text(); //console.log('delays ~~~~~', response); parseString(response, function (err, result) { //console.log(result) //console.log('asd',result['soap:Envelope']['soap:Body'][0].FindAVLDepartureFromBusStopTableResponse[0].departureCollection[0].Departure); result = result['soap:Envelope']['soap:Body'][0].FindAVLDepartureFromBusStopTableResponse[0].departureCollection[0].Departure; //console.log(checkNested(result,'soap:Envelope.soap:Body[0].FindAVLDepartureFromBusStopTableResponse[0].departureCollection[0].Departure' )); //console.log(checkNested(result,'soapEnvelope' )); // result = result['soap:Envelope']['soap:Body'][0].FindAVLDepartureFromBusStopTableResponse[0].departureCollection[0]; // console.log('====', result) if(!result) return; for (const item of result) { let a = null; //console.log(item) if(item.Delay[0].Departure[0].startsWith('0001-01')) a = [item.TripNumber[0], 0]; else a = [item.TripNumber[0], item.Delay[0].Departure[0], item.Delay[0].Arrival[0]]; delays.push(a); } }); return delays; } } const departureRequest = new FetchRequest(); const delaysRequest = new FetchRequest(); const getDepartures = () => { departureRequest.getDepartures() .then(futureDepartures => { let responseObj = {}; responseObj["departures"] = futureDepartures; console.log('future +++++++', responseObj) instance.send(instanceSendTo.departures, responseObj); }).catch(error => { console.log('Error fetching bus departures - repeating request in 1 minute', error); setTimeout(() => getDepartures(), 60000) }); } function getDelays(){ delaysRequest.getDelays() .then(delays => { let responseObj = {}; responseObj["delays"] = delays; console.log('delays -------', responseObj) instance.send(instanceSendTo.delays, responseObj); }) .catch(error => { console.log('Error fetching bus delays data', error); }); } setTimeout(getDepartures, 30000); setInterval(getDelays, 60000); // let startDay = new Date().getDay(); let date = new Date().toLocaleString().split('/'); //[ '12', '6', '2022, 11:47:32 PM' ] let startDay = date[1]; //'6' function checkIfNewDay() { let currentDay = new Date().toLocaleString().split('/')[1]; console.log('new day check -----',startDay, currentDay); if(currentDay == startDay) return; getDepartures(); startDay = currentDay; } //we check if day changed, if yes, we get bus departures. we check every 15 minutes setInterval(checkIfNewDay, 900000); function capitalizeFirstLetter(string) { return string[0].toUpperCase() + string.slice(1); } }; const aodchody = [ [ "2", "3", "16.11.22 4:5", "Závodu míru" ], [ "290", "3", "16.11.22 4:25", "Závodu míru" ], [ "292", "3", "16.11.22 4:48", "Závodu míru" ], [ "296", "3", "16.11.22 4:58", "Závodu míru" ], [ "6", "3", "16.11.22 5:5", "Závodu míru" ], [ "300", "3", "16.11.22 5:18", "Závodu míru" ], [ "8", "3", "16.11.22 5:55", "Závodu míru" ], [ "16", "3", "16.11.22 6:30", "Závodu míru" ], [ "1", "33", "16.11.22 6:45", "sídl. Michal škola" ], [ "18", "3", "16.11.22 6:52", "Hrušková" ], [ "310", "3", "16.11.22 7:0", "Závodu míru" ], [ "22", "3", "16.11.22 7:10", "Závodu míru" ], [ "5", "33", "16.11.22 7:50", "sídl. Michal škola" ], [ "32", "3", "16.11.22 8:10", "Závodu míru" ], [ "7", "33", "16.11.22 8:25", "sídl. Michal škola" ], [ "100", "3", "16.11.22 8:30", "Závodu míru" ], [ "9", "33", "16.11.22 8:50", "sídl. Michal škola" ], [ "34", "3", "16.11.22 9:0", "Závodu míru" ], [ "38", "3", "16.11.22 9:20", "Závodu míru" ], [ "11", "33", "16.11.22 9:30", "sídl. Michal škola" ], [ "298", "3", "16.11.22 9:55", "Závodu míru" ], [ "2", "7", "16.11.22 10:10", "Březová, aut. st." ], [ "13", "33", "16.11.22 10:20", "sídl. Michal škola" ], [ "48", "3", "16.11.22 10:50", "Závodu míru" ], [ "50", "3", "16.11.22 11:13", "Závodu míru" ], [ "15", "33", "16.11.22 11:25", "sídl. Michal škola" ], [ "52", "3", "16.11.22 11:40", "Závodu míru" ], [ "17", "33", "16.11.22 11:52", "sídl. Michal škola" ], [ "56", "3", "16.11.22 12:20", "Závodu míru" ], [ "62", "3", "16.11.22 12:40", "Závodu míru" ], [ "64", "3", "16.11.22 12:55", "Závodu míru" ], [ "19", "33", "16.11.22 13:0", "sídl. Michal škola" ], [ "66", "3", "16.11.22 13:20", "Stará ovčárna" ], [ "21", "33", "16.11.22 13:35", "sídl. Michal škola" ], [ "23", "33", "16.11.22 14:0", "sídl. Michal škola" ], [ "72", "3", "16.11.22 14:5", "Závodu míru" ], [ "25", "33", "16.11.22 14:42", "sídl. Michal škola" ], [ "94", "3", "16.11.22 14:48", "Závodu míru" ], [ "27", "33", "16.11.22 15:5", "sídl. Michal škola" ], [ "106", "3", "16.11.22 15:9", "Závodu míru" ], [ "84", "3", "16.11.22 15:35", "Závodu míru" ], [ "29", "33", "16.11.22 15:35", "sídl. Michal škola" ], [ "6", "7", "16.11.22 15:50", "Březová, aut. st." ], [ "96", "3", "16.11.22 16:10", "Závodu míru" ], [ "31", "33", "16.11.22 16:10", "sídl. Michal škola" ], [ "4", "7", "16.11.22 16:25", "Březová, aut. st." ], [ "102", "3", "16.11.22 16:30", "Závodu míru" ], [ "302", "3", "16.11.22 16:44", "Závodu míru" ], [ "8", "7", "16.11.22 16:55", "Březová, aut. st." ], [ "108", "3", "16.11.22 17:5", "Stará ovčárna" ], [ "112", "3", "16.11.22 17:30", "Závodu míru" ], [ "114", "3", "16.11.22 17:53", "Závodu míru" ], [ "118", "3", "16.11.22 18:10", "Závodu míru" ], [ "120", "3", "16.11.22 18:30", "Závodu míru" ], [ "122", "3", "16.11.22 18:50", "Závodu míru" ], [ "124", "3", "16.11.22 19:15", "Závodu míru" ], [ "126", "3", "16.11.22 19:30", "Závodu míru" ], [ "130", "3", "16.11.22 19:50", "Závodu míru" ], [ "132", "3", "16.11.22 20:15", "Závodu míru" ], [ "134", "3", "16.11.22 20:45", "Závodu míru" ], [ "136", "3", "16.11.22 21:5", "Závodu míru" ], [ "256", "3", "16.11.22 21:20", "Stará ovčárna" ], [ "140", "3", "16.11.22 22:8", "Stará ovčárna" ] ]