// ################################################
// ParcelApp ical v0.01 / 24.11.2024
// by stenmic
// ################################################
// ################################################
ical_Table = 'ical.1.data.table';
Paketdienst_abkuerzen_ab_Zeichen = 19;
Aufbewahrungszeit_History_Tage = 14;
History_in_Tagen = true;
Farbe_fuer_Heute = '#3bb273';
Buttontext_Unterwegs = 'Unterwegs';
Buttonfarbe_Unterwegs = '#a0a0a0';
Buttontext_History = 'Histrory';
Buttonfarbe_History = '#e55a52';
// ################################################
if ( !existsState('0_userdata.0.ParcelApp.VIS_json')) {
createState('0_userdata.0.ParcelApp.unterwegs_json','[]',{name: 'unterwegs_json',role: 'value',type: 'string'});
createState('0_userdata.0.ParcelApp.VIS_json','[]',{name: 'VIS_json',role: 'value',type: 'string'});
createState('0_userdata.0.ParcelApp.history_json_temp','[]',{name: 'history_json_temp',role: 'value',type: 'string'});
createState('0_userdata.0.ParcelApp.Button_unterwegs_history',true,{name: 'Button_unterwegs_history',role: 'button',type: 'boolean'});
createState('0_userdata.0.ParcelApp.Button',true,{name: 'Button',role: 'switch',type: 'boolean'});
createState('0_userdata.0.ParcelApp.Button_Text','History',{name: 'Button_Text',role: 'value',type: 'string'});
createState('0_userdata.0.ParcelApp.Button_Farbe','',{name: 'Button_Farbe',role: 'value',type: 'string'});
await wait(2000);
}
function subsequenceFirstFromEnd(sequence, at2) {
var start = 0;
var end = sequence.length - 1 - at2 + 1;
return sequence.slice(start, end);
}
function subsequenceFromStartLast(sequence, at1) {
var start = at1;
var end = sequence.length - 1 + 1;
return sequence.slice(start, end);
}
// Beschreibe diese Funktion …
async function makeJson() {
HistoryAlt = (() => { try { return JSON.parse(getState('0_userdata.0.ParcelApp.history_json_temp').val); } catch (e) { return {}; }})();
TabelleHistoryAlt = [];
ical = getState(ical_Table).val;
Y = 0;
while (!(getAttr(HistoryAlt, (String(Y))) == null)) {
timestamp = getAttr(HistoryAlt, (String(Y) + '.timestamp')) + Aufbewahrungszeit_History_Tage * 86400000;
if ((timestamp >= (new Date().getTime())) && (JSON.stringify(ical).indexOf(getAttr(HistoryAlt, (String(Y) + '.Sendungsnummer')).slice(((getAttr(HistoryAlt, (String(Y) + '.Sendungsnummer')).indexOf(';">') + 1 + 3) - 1), (getAttr(HistoryAlt, (String(Y) + '.Sendungsnummer')).indexOf('</a>') + 1) - 1)) + 1 == 0)) {
if (History_in_Tagen == true) {
Tage = Math.round((Math.floor((new Date().getTime()) / 86400000) * 86400000 - Math.floor(getDateObject(getAttr(HistoryAlt, (String(Y) + '.timestamp'))).getTime() / 86400000) * 86400000) / 86400000);
if (Tage == 0) {
Datum_History = 'Heute geliefert';
} else if (Tage == 1) {
Datum_History = 'Gestern geliefert';
} else if (Tage > 1) {
Datum_History = ['vor ',Tage,' Tagen geliefert'].join('');
} else {
Datum_History = formatDate(getDateObject(getAttr(HistoryAlt, (String(Y) + '.timestamp'))), 'T.M.');
}
} else {
Datum_History = formatDate(getDateObject(getAttr(HistoryAlt, (String(Y) + '.timestamp'))), 'T.M.');
}
cleanHistoryTemp = { 'Datum': Datum_History, 'Sendung': getAttr(HistoryAlt, (String(Y) + '.Sendung')), 'Paketdienst': getAttr(HistoryAlt, (String(Y) + '.Paketdienst')), 'Sendungsnummer': getAttr(HistoryAlt, (String(Y) + '.Sendungsnummer')), 'timestamp': getAttr(HistoryAlt, (String(Y) + '.timestamp')) };
TabelleHistoryAlt.push(cleanHistoryTemp);
}
if (Y > 500) {
break;
}
Y = (typeof Y === 'number' ? Y : 0) + 1;
}
History2 = TabelleHistoryAlt;
Tabelle = [];
Heute = formatDate(new Date(), 'T.M.');
Morgen = formatDate(getDateObject(((new Date().getTime()) + 86400000)), 'T.M.');
X = 0;
while (!(getAttr(ical, (String(X))) == null)) {
Lieferdatum = formatDate(getDateObject(getAttr(ical, (String(X) + '._object.start'))), 'T.M.');
if (Lieferdatum == Heute) {
spanInHEUTE = ['<span style="color:',Farbe_fuer_Heute,';">'].join('');
Lieferdatum = 'Heute';
spanOutHEUTE = '</span>';
} else {
spanInHEUTE = '';
spanOutHEUTE = '';
}
if (Lieferdatum == Morgen) {
Lieferdatum = 'Morgen';
}
Sendung = getAttr(ical, (String(X) + '.event')).slice(0, getAttr(ical, (String(X) + '.event')).lastIndexOf(' - ') + 1).trim();
Sendungsnummer = subsequenceFirstFromEnd(getAttr(ical, (String(X) + '._IDID')), 2);
Link = subsequenceFromStartLast(getAttr(ical, (String(X) + '._section')), ((getAttr(ical, (String(X) + '._section')).indexOf('https') + 1) - 1));
if (Paketdienst_abkuerzen_ab_Zeichen == 0) {
Paketdienst = subsequenceFromStartLast(subsequenceFromStartLast(getAttr(ical, (String(X) + '.event')), ((getAttr(ical, (String(X) + '.event')).lastIndexOf(' - ') + 1) - 1)).trim(), 2);
} else {
if (subsequenceFromStartLast(subsequenceFromStartLast(getAttr(ical, (String(X) + '.event')), ((getAttr(ical, (String(X) + '.event')).lastIndexOf(' - ') + 1) - 1)).trim(), 2).length > Paketdienst_abkuerzen_ab_Zeichen) {
Paketdienst = String(subsequenceFromStartLast(subsequenceFromStartLast(getAttr(ical, (String(X) + '.event')), ((getAttr(ical, (String(X) + '.event')).lastIndexOf(' - ') + 1) - 1)).trim(), 2).slice(0, Paketdienst_abkuerzen_ab_Zeichen)) + '.';
} else {
Paketdienst = subsequenceFromStartLast(subsequenceFromStartLast(getAttr(ical, (String(X) + '.event')), ((getAttr(ical, (String(X) + '.event')).lastIndexOf(' - ') + 1) - 1)).trim(), 2);
}
}
Json = { 'Datum': [spanInHEUTE,Lieferdatum,spanOutHEUTE].join(''), 'Sendung': [spanInHEUTE,Sendung,spanOutHEUTE].join(''), 'Paketdienst': [spanInHEUTE,Paketdienst,spanOutHEUTE].join(''), 'Sendungsnummer': [spanInHEUTE,[' <a href="',Link,'" target="_blank" style="text-decoration: none; color: inherit;">',Sendungsnummer,'</a>'].join(''),spanOutHEUTE].join('') };
Tabelle.push(Json);
if (JSON.stringify(History2).indexOf(Sendungsnummer) + 1 == 0) {
JsonHistory = { 'Datum': formatDate(getDateObject(getAttr(ical, (String(X) + '._object.start'))), 'T.M.'), 'Sendung': Sendung, 'Paketdienst': Paketdienst, 'Sendungsnummer': [' <a href="',Link,'" target="_blank" style="text-decoration: none; color: inherit;">',Sendungsnummer,'</a>'].join(''), 'timestamp': (new Date().getTime()) };
History2.unshift(JsonHistory);
}
if (X > 500) {
break;
}
X = (typeof X === 'number' ? X : 0) + 1;
}
setState('0_userdata.0.ParcelApp.unterwegs_json', JSON.stringify(Tabelle), true);
setState('0_userdata.0.ParcelApp.history_json_temp', JSON.stringify(History2), true);
}
on({ id: [].concat(ical_Table), change: 'any' }, async (obj) => {
let value = obj.state.val;
let oldValue = obj.oldState.val;
await makeJson();
});
await makeJson();
if (getState('0_userdata.0.ParcelApp.Button').val == false) {
setState('0_userdata.0.ParcelApp.Button_Text', Buttontext_History, true);
setState('0_userdata.0.ParcelApp.Button_Farbe', Buttonfarbe_History, true);
} else {
setState('0_userdata.0.ParcelApp.Button_Text', Buttontext_Unterwegs, true);
setState('0_userdata.0.ParcelApp.Button_Farbe', Buttonfarbe_Unterwegs, true);
}
on({ id: '0_userdata.0.ParcelApp.Button_unterwegs_history', change: 'any' }, async (obj) => {
let value = obj.state.val;
let oldValue = obj.oldState.val;
if (getState('0_userdata.0.ParcelApp.Button').val == false) {
setState('0_userdata.0.ParcelApp.Button', true, true);
} else {
setState('0_userdata.0.ParcelApp.Button', false, true);
}
});
on({ id: '0_userdata.0.ParcelApp.Button', change: 'ne' }, async (obj) => {
let value = obj.state.val;
let oldValue = obj.oldState.val;
if ((obj.state ? obj.state.val : '') == true) {
setState('0_userdata.0.ParcelApp.VIS_json', getState('0_userdata.0.ParcelApp.unterwegs_json').val, true);
setState('0_userdata.0.ParcelApp.Button_Text', Buttontext_Unterwegs, true);
setState('0_userdata.0.ParcelApp.Button_Farbe', Buttonfarbe_Unterwegs, true);
} else {
setState('0_userdata.0.ParcelApp.Button_Text', Buttontext_History, true);
setState('0_userdata.0.ParcelApp.Button_Farbe', Buttonfarbe_History, true);
History_temp = (() => { try { return JSON.parse(getState('0_userdata.0.ParcelApp.history_json_temp').val); } catch (e) { return {}; }})();
History_Neu = [];
Z = 0;
ical = getState(ical_Table).val;
while (!(getAttr(History_temp, (String(Z))) == null)) {
if (JSON.stringify(ical).indexOf(getAttr(History_temp, (String(Z) + '.Sendungsnummer')).slice(((getAttr(History_temp, (String(Z) + '.Sendungsnummer')).indexOf(';">') + 1 + 3) - 1), (getAttr(History_temp, (String(Z) + '.Sendungsnummer')).indexOf('</a>') + 1) - 1)) + 1 == 0) {
History_Json = { 'Datum': getAttr(History_temp, (String(Z) + '.Datum')), 'Sendung': getAttr(History_temp, (String(Z) + '.Sendung')), 'Paketdienst': getAttr(History_temp, (String(Z) + '.Paketdienst')), 'Sendungsnummer': getAttr(History_temp, (String(Z) + '.Sendungsnummer')), 'timestamp': getAttr(History_temp, (String(Z) + '.timestamp')) };
History_Neu.push(History_Json);
}
if (Z > 500) {
break;
}
Z = (typeof Z === 'number' ? Z : 0) + 1;
}
setState('0_userdata.0.ParcelApp.VIS_json', JSON.stringify(History_Neu), true);
}
});
on({ id: '0_userdata.0.ParcelApp.unterwegs_json', change: 'ne' }, async (obj) => {
let value = obj.state.val;
let oldValue = obj.oldState.val;
if (getState('0_userdata.0.ParcelApp.Button').val == false) {
setState('0_userdata.0.ParcelApp.Button', true, true);
} else {
setState('0_userdata.0.ParcelApp.VIS_json', (obj.state ? obj.state.val : ''), true);
}
});