volkerrichert / ioBroker.mymuell

Simple script to get "die nächsten Müllabfuhren"
1 stars 0 forks source link

Feature request: Switch to axios please #2

Open RP70DP opened 1 year ago

RP70DP commented 1 year ago

Because request is deprecated, it will be removed from javascript adapter in the near future (as far as i understand). Can you switch to axios please?

volkerrichert commented 1 year ago

request is an external module, but you're right. It's deprecated. I'll take a look.

PRs are also wellcome ;-)

RP70DP commented 3 months ago

Hier eine an die neue Funktion "httpGet" angepasste Version. Vielen Dank an @TT-Tom, der die wesentlichen Arbeiten geleistet hat.

const city_id = 66005;
const area_id = 2800;
const logging = false;
const deviceName = 'Müllabfuhr [Stadt]';
const numberOfEntries = 9;

const instanz = '0_userdata.' + instance + '.muell';

if (logging) log('starting muell.' + instanz);

extendObject(instanz, {
       type: "device",
       common: {
           name: deviceName
       }
   }, function (err) {
       if (err) {
           log('could not create device: ', 'warn');
           return;
       }

   }
);
let baseData = {};

// query names first

httpGet('https://mymuell.jumomind.com/mmapp/api.php?r=trash&city_id=' + city_id + '&area_id=' + area_id, { timeout: 2000 }, (error, response) => {
    if (logging) log(`first query statusCode ${response.statusCode}`);
    if (logging) log(`first query data ${response.data}`);
    if (!error && response.statusCode == 200) {
        let data = JSON.parse(response.data);  // info ist ein Objekt
        data.forEach((v, i) => {
            baseData[v._name] = v;
        })
        updateMuell();
    } else {
        log(error, 'error');
    }
});

schedule('{"time":{"exactTime":true,"start":"12:17"},"period":{"days":1}}', updateMuell);

function updateMuell() {
    const options = 'https://mymuell.jumomind.com/webservice.php?idx=termins&city_id=' + city_id + '&area_id=' + area_id + '&ws=3'

    httpGet(options, { timeout: 2000 }, (error, response) => {
        if (logging) log(`update query statusCode ${response.statusCode}`);
        if (logging) log(`update query data ${response.data}`);
        if (!error && response.statusCode == 200) {
            let info = JSON.parse(response.data);  // info ist ein Objekt
            if (info[0].Ack === 'Success') {
                let data = info[0]._data;  // xy ist eine Eigenschaft des Objektes info
                let counter = 0;
                const date = (new Date())
                const todayStr = date.getFullYear() + "-" + ('00' + (date.getMonth() + 1)).slice(-2) + "-" + ('00' + date.getDate()).slice(-2)
                log(todayStr);
                data.forEach((v, i) => {
                    if (todayStr < v.cal_date && counter <= numberOfEntries) {

                        const basePath = instanz + '.' + ('000' + counter).slice(-3);
                        if (logging) log(v.cal_date + ' -> ' + basePath);
                        // States erstellen
                        extendObject(basePath, {
                            type: "channel",
                            common: {
                                name: v.cal_date_normal
                            }
                        }, function (err) {
                            if (err) {
                                log('could not create device: ', 'warn');
                                return;
                            }

                        }
                        );
                        createState(basePath + '.date', v.cal_date_normal, {
                            name: 'Datum',
                            desc: 'Datum der Abholung',
                            type: 'string',
                            read: true,
                            write: false
                        }, () => {
                            setState(basePath + '.date', v.cal_date_normal, true);
                        });

                        createState(basePath + '.desc', baseData[v.cal_garbage_type].title, {
                            name: 'Beschreibung',
                            desc: 'Beschreibung der Abholung',
                            type: 'string',
                            read: true,
                            write: false
                        }, () => {
                            setState(basePath + '.desc', baseData[v.cal_garbage_type].title, true);
                        });

                        createState(basePath + '.color',baseData[v.cal_garbage_type].color, {
                            name: 'Farbe',
                            desc: 'Farbe',
                            type: 'string',
                            read: true,
                            write: false
                        }, () => {
                            setState(basePath + '.color', baseData[v.cal_garbage_type].color, true);
                        });

                        counter++;
                    }
                });
            }
        }
    });
}