// ########################################################################################################
// Polleninfo auswerten heute und die nächsten Tage
// Angepasste Version von Spike - Vielen Dank an steinwedel, pix, robsdobs, und wer noch so dran war :)
// basierend auf Original Version 1.52 -> angepasste Version 1.1 - 15.03.2019
// umgestellt von request auf axios 12.04.2022 von MeinzIsAs
// ########################################################################################################
// @ts-check
'use strict';
const axios_intern = require('axios').default;
//import request = require("request");
///< script> var exports = {}; < / script>
/**************************************************************************************************
* Initiale Optionen
**************************************************************************************************/
//Welcher Ort soll abgefragt werden?
var AbfrageOrt: string = 'https://www.wetteronline.de/pollen/trofaiach';
//Wo sollen die Daten unter javascript.0... abgelegt werden?
var baseObjPath: string = 'Allgemeine_Programme.Pollenvorhersage';
/**************************************************************************************************
* Globale TypeScript Definitionen
**************************************************************************************************/
interface iRequestOptions {
url: string;
headers: any;
}
/**
* Function for check if a object is a primitive object (string, number, etc)
* @param obj any object to check if it is a primitive object
* @returns boolean which is true if object in parameter is a primitive object
*/
function isPrimitive(obj:any):boolean {
return (obj !== Object(obj));
}
/**
* write any objects given as parameter to log as debug
* @param param a nist of different objects
*/
function logDebug(...param: any[]):void {
if (arguments.length > 0) {
var text:string[] = [];
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'string' || arguments[i] instanceof String) {
text.push(arguments[i]);
} else if(arguments[i] === null) {
text.push('null');
} else if(arguments[i] === undefined) {
text.push('undefined');
} else if (isPrimitive(arguments[i])) {
text.push(arguments[i]);
} else {
text.push(JSON.stringify(arguments[i]));
}
//enum close bracket cannot be found in "channel[state.id=*.TEMPERATURE](rooms=WZ|Wohnung|Wohnzimmer"
}
log(text.join(' '), 'debug');
}
}
/**
* write any objects given as parameter to log as Information
* @param param a nist of different objects
*/
function logInfo(...param: any[]):void {
if (arguments.length > 0) {
var text:string[] = [];
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'string' || arguments[i] instanceof String) {
text.push(arguments[i]);
} else if(arguments[i] === null) {
text.push('null');
} else if(arguments[i] === undefined) {
text.push('undefined');
} else if (isPrimitive(arguments[i])) {
text.push(arguments[i]);
} else {
text.push(JSON.stringify(arguments[i]));
}
//enum close bracket cannot be found in "channel[state.id=*.TEMPERATURE](rooms=WZ|Wohnung|Wohnzimmer"
}
log(text.join(' '), 'info');
setState('javascript.' + instance + '.Ereignisliste.event','INFO|'+text.join(' '));
}
//logEnhanced(logtext, 'info');
//console.debug(p);
//logs(logtext,loglevels.debug);
}
/**
* write any objects given as parameter to log as warning
* @param param a nist of different objects
*/
function logWarning(...param: any[]):void {
if (arguments.length > 0) {
var text:string[] = [];
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'string' || arguments[i] instanceof String) {
text.push(arguments[i]);
} else if(arguments[i] === null) {
text.push('null');
} else if(arguments[i] === undefined) {
text.push('undefined');
} else if (isPrimitive(arguments[i])) {
text.push(arguments[i]);
} else {
text.push(JSON.stringify(arguments[i]));
}
//enum close bracket cannot be found in "channel[state.id=*.TEMPERATURE](rooms=WZ|Wohnung|Wohnzimmer"
}
log(text.join(' '), 'warn');
setState('javascript.' + instance + '.Ereignisliste.event','WARN|'+text.join(' '));
}
//logEnhanced(logtext, 'warn');
//console.warn(p);
//logs(logtext,loglevels.warn);
}
/**
* write any objects given as parameter to log as error
* @param param a nist of different objects
*/
function logError(...param: any[]):void {
if (arguments.length > 0) {
var text:string[] = [];
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'string' || arguments[i] instanceof String) {
text.push(arguments[i]);
} else if(arguments[i] === null) {
text.push('null');
} else if(arguments[i] === undefined) {
text.push('undefined');
} else if (isPrimitive(arguments[i])) {
text.push(arguments[i]);
} else {
text.push(JSON.stringify(arguments[i]));
}
//enum close bracket cannot be found in "channel[state.id=*.TEMPERATURE](rooms=WZ|Wohnung|Wohnzimmer"
}
log(text.join(' '), 'error');
setState('javascript.' + instance + '.Ereignisliste.event','ERROR|'+text.join(' '));
}
}
/**************************************************************************************************
* Lokale TypeScript Definitionen
**************************************************************************************************/
//import request = require("request");
var Pollenarten:string[] = ['Ambrosia','Ampfer','Beifuß','Birke','Buche','Eiche','Erle','Gräser','Hasel','Pappel','Roggen','Ulme','Wegerich','Weide']; //'Erle','Hasel','Pappel','Weide','Ulme','Birke','Buche','Ampfer','Roggen','Gräser','Eiche','Wegerich','Beifuß','Ambrosia'
var tageVorhersage = ['d0','d1','d2','d3','d4','d5', 'd6'];
var tageVorhersageName = ['heute','morgen','heute + 2','heute + 3','heute + 4','heute + 5', 'heute + 6)']; // ,'heute + 6'
function stripTags(data:string):string {
return data.replace(/( |<([^>]+)>)/ig, "");
}
function getDate(days: number): string { // Eingabe 0, 1, 2, ...
var jetzt = new Date();
jetzt.setHours(0);
jetzt.setMinutes(0);
jetzt.setSeconds(0);
jetzt.setMilliseconds(0);
var newdate = new Date();
newdate.setTime(jetzt.getTime() + (24 * 60 * 60 * 1000 * days) ); // heute + anzahl tage ins Millisekunden
var date :string = newdate.getDate().toString();
var month: string = (newdate.getMonth()+1).toString();
var year: string = newdate.getFullYear().toString();
date = (date.length < 2) ? '0' + date : date;
month = (month.length < 2) ? '0' + month : month;
var result:string = year+month+date;
logDebug('Datum in Link: ', result);
return (result); // Ausgabe z.B. 20160506
}
function GetPollenIndex(pflanze:string, html:string, day:number):number {
var code ={'noburden':0, 'weakburden':1, 'moderateburden':2, 'strongburden':3, '':0};
var result:string= "";
var start:number =html.indexOf(pflanze)+pflanze.length;
html=html.slice(start);
start = html.indexOf('data-day' + day);
html=html.slice(start);
var ende:number =html.indexOf("burden") + 6;
result=html.substring(0,ende).replace("data-day" + day + '="',"");
result=result.trim();
// log("Pflanze: " + pflanze + ", Tag: " + day + ": " + result + " > " + code[result]);
return code[result];
}
function initializePollenwetter(baseObj:string, name:string):void {
createState(baseObj, '', false, {
name: name,
type: "string",
role: 'json',
desc: 'Pollenflug',
read: true,
write: true});
for(let i=0;i<Pollenarten.length;i++) {
createState(baseObj + '.' + Pollenarten[i].toLowerCase(), 0, false, {
name: 'Pollenart '+ Pollenarten[i],
type: "number",
role: 'value',
desc: 'Pollenart ' + Pollenarten[i],
read: true,
write: true});
}
createState(baseObj+'.count.gt0', 0, false, {
name: 'Anzahl Pollen größer 0',
type: "number",
role: 'value',
desc: 'Anzahl Pollen',
read: true,
write: true});
for(let i=0;i<4;i++) {
createState(baseObj+'.text.'+i.toString(), '', false, {
name: 'Liste Pollen größern ' + String(i),
type: "string",
role: 'text',
desc: 'Pollenübersicht',
read: true,
write: true});
createState(baseObj+'.html.'+i.toString(), '', false, {
name: 'Liste Pollen größern ' + String(i) + ' (html)',
type: "string",
role: 'html',
desc: 'Pollenübersicht',
read: true,
write: true});
createState(baseObj+'.count.'+i.toString(), 0, false, {
name: 'Anzahl Pollen mit ' + String(i),
type: "number",
role: 'value',
desc: 'Anzahl Pollen',
read: true,
write: true});
}
}
function readPollenwetterForAllDays():void {
axios_intern({
method: 'get',
baseURL: AbfrageOrt,
})
.then(function (response) {
if (response.status == 200) { // kein Fehler, Inhalt in body
var body=response.data.replace(/\t/g, ' ');
var body=response.data.replace(/(\n|\r)/g, '');
var start:number =body.indexOf('text">Ambrosia</div>'); // Erle 'hog_text' 'text">Ambrosia</div>'
var ende:number =body.indexOf('id="burden_icons_legend_headline">'); // burden_icons_legend_headline
if (start>-1) { body=body.substring(start, ende); }
let pobj:any = {};
let pot = ['','','',''];
let poh = ['','','',''];
let poc = [0,0,0,0];
let cnt =0;
for(let day=0;day<tageVorhersage.length;day++) {
let baseObj: string = baseObjPath + '.' + tageVorhersage[day];
initializePollenwetter(baseObj, tageVorhersageName[day]);
for(let i=0;i<Pollenarten.length;i++) {
let pi:number = GetPollenIndex(Pollenarten[i],body, day);
// log(body);
pobj[Pollenarten[i].toLowerCase()] = pi;
if (pi>0) { cnt++; }
poc[pi]++;
pot[pi] = pot[pi] + Pollenarten[i] + '\r\n';
poh[pi] = poh[pi] + Pollenarten[i] + '<br/>';
setState(baseObj+'.'+Pollenarten[i].toLowerCase(),pi);
}
setState(baseObj+'.count.gt0',cnt);
for(let i=0;i<4;i++) {
setState(baseObj+'.text.'+i.toString(),pot[i]);
setState(baseObj+'.html.'+i.toString(),poh[i]);
setState(baseObj+'.count.'+i.toString(),poc[i]);
}
setState(baseObj,JSON.stringify(pobj));
}
} else {
logInfo("StatusCode="+response.statusCode);
// logError(error); // Error beim Einlesen
}
});
}
function readPollenwetter() {
log('Pollen gelesen');
readPollenwetterForAllDays();
}
/*
# * * * * * command to execute
# ¦ ¦ ¦ ¦ ¦
# ¦ ¦ ¦ ¦ ¦
# ¦ ¦ ¦ ¦ +----- day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# ¦ ¦ ¦ +---------- month (1 - 12)
# ¦ ¦ +--------------- day of month (1 - 31)
# ¦ +-------------------- hour (0 - 23)
# +------------------------- min (0 - 59)
*/
//schedule('0 3 * * *', readPollenwetter); // "5 0 * * *"
schedule('*/60 * * * *', readPollenwetter);