Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Dahua Camera Snapshot mit axios

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Dahua Camera Snapshot mit axios

    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      JojoS last edited by JojoS

      Hallo,
      ich verzweifele daran einen Snapshot einer Dahua/Alhua Kamera per axios oder httpGet zu bekommen. Mit request hatte es funktioniert, aber jetzt bekomme ich nur Fehler 401, also Anmeldefehler.
      Habe es mit httpGet und auch direkt mit axios.get probiert, user/pw in der url oder als auth Parameter, geht alles nicht. Hat jemand eine Idee?

      const axios = require('axios');
      
      function sendImage() {
          console.info('Test SendImage');
              // httpGet('http://viewer:123456@192.168.10.200/cgi-bin/snapshot.cgi',  // encoding null for write to buffer
              httpGet('http://192.168.10.200/cgi-bin/snapshot.cgi',  // encoding null for write to buffer
              { 
                  responseType: 'arraybuffer',
                  basicAuth: {
                      user: 'viewer',
                      password: '123456'
                  }
              },
              (error, response)  => {
                  console.warn('response statuscode: ' + response.statusCode);
                  console.warn('response data length: ' + response.data.length);
                  console.log(response.headers);
                  console.log(error);
                  if (!error) {
                      console.log('sendTo Telegram.0');
                      writeFile('0_userdata.0', 'test-sendimage.jpg', response.data, (err) => {
                          if (err) {
                              console.error(err);
                          }
                      });
                      // sendTo('telegram.0', {text: response.data, type: 'photo', caption: 'Test Test'});
                  } else {
                      console.log(error);
                  }
              }
          );
      }
      
      function sendImage2() {
          // read image
          // axios.get('http://viewer:123456@192.168.10.200/cgi-bin/snapshot.cgi', {
          axios.get('http://192.168.10.200/cgi-bin/snapshot.cgi', {
              auth: {
                  username: 'viewer',
                  password: '123456'
              }
            })
          .then(function (response) {
              // handle success
              console.log(response.statusCode);
          })
          .catch(function (error) {
              // handle error
              console.log(error);
          })
          .finally(function () {
              // always executed
          });
      
      }
      
      sendImage2();
      

      Fehler von der httpGet Version:

      javascript.0	00:37:13.402	info	Stopping script script.js.common.TestSendImage
      javascript.0	00:37:13.489	info	Start JavaScript script.js.common.TestSendImage (Javascript/js)
      javascript.0	00:37:13.502	info	script.js.common.TestSendImage: Test SendImage
      javascript.0	00:37:13.503	info	script.js.common.TestSendImage: httpGet(config={"method":"get","url":"http://192.168.10.200/cgi-bin/snapshot.cgi","responseType":"arraybuffer","responseEncoding":"utf8","timeout":2000,"auth":{"username":"viewer","password":"123456"},"headers":{"User-Agent":"Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0"}})
      javascript.0	00:37:13.504	info	script.js.common.TestSendImage: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions
      javascript.0	00:37:13.534	info	script.js.common.TestSendImage: httpGet(url=http://192.168.10.200/cgi-bin/snapshot.cgi, responseTime=31ms)
      javascript.0	00:37:13.534	warn	script.js.common.TestSendImage: response statuscode: 401
      javascript.0	00:37:13.534	warn	script.js.common.TestSendImage: response data length: 0
      javascript.0	00:37:13.534	info	script.js.common.TestSendImage: Object [AxiosHeaders] { 'www-authenticate': 'Digest realm="Login to fa37801de3adf33db0a8c0fb69a45df5", qop="auth", nonce="1355095432", opaque="da3a7775ef776651c4b36d707fb55fcc1a4335d8"', connection: 'close', 'content-length': '0' }
      javascript.0	00:37:13.534	info	script.js.common.TestSendImage: null
      javascript.0	00:37:13.534	info	script.js.common.TestSendImage: sendTo Telegram.0
      javascript.0	00:37:13.535	info	script.js.common.TestSendImage: writeFile(adapter=0_userdata.0, fileName=test-sendimage.jpg)
      

      mit curl bekomme ich ein image:

      curl -o image.jpg --digest --user viewer:123456-O "http://192.168.10.200/cgi-bin/snapshot.cgi"
      

      edit:
      nach weiterem suchen finde ich das axios wohl kein 'digest auth' kann, wurde in den issues vielfach gewünscht. Request kann es mit sendImmediately=false.
      Kann ich ein npm Modul wie axios-digest-auth benutzen? JavaScript ist nicht meine Muttersprache...

      J 1 Reply Last reply Reply Quote 0
      • J
        JojoS @JojoS last edited by JojoS

        ich habe es endlich hinbekommen.
        Mit dem npm Modul mhoc/axios-digest-auth ging es:

        const axios = require('axios');
        const AxiosDigestAuth = require('@mhoc/axios-digest-auth').default;
        
        
        const options = {
            axios,        
            username: "viewer",
            password: "123456",
        };
        
        const axiosDigestAuthInst = new AxiosDigestAuth(options);
        
        const ExecuteMyRequest = async () => {
            const requestOptions =  {
                responseType: 'arraybuffer',
                method: "GET",
                url: "http://192.168.10.200/cgi-bin/snapshot.cgi"
            }
        
            const response = await axiosDigestAuthInst.request(requestOptions);
            console.log(response.status);
            sendTo('telegram.0', {text: response.data, type: 'photo', caption: 'Test Test'});
            writeFile('0_userdata.0', 'test-sendimage.jpg', response.data, (err) => {
                if (err) {
                    console.error(err);
                }
            });
            
            console.log('request done.');
        };
        
        function sendImage3() {
            ExecuteMyRequest();
        }
        
        sendImage3();
        

        Das Modul axios-digest-auth ist nicht in der ioBroker Installation drin, muss also noch in den Javascript Adaptereinstellungen hinzugefügt werden.

        vowill 1 Reply Last reply Reply Quote 0
        • vowill
          vowill @JojoS last edited by vowill

          @jojos Gute Lösung - muss ich gelegentlich mal ausprobieren!
          Ich hatte ebenfalls verschiedene Ansätze probiert und schließlich als Lösung 'motioneye' auf meinem NAS in einem Docker-Container installiert. Aus diesem Programm lassen sich die Snapshots (und das Live-Video für die vis) ebenfalls abholen.

          1 Reply Last reply Reply Quote 1
          • First post
            Last post

          Support us

          ioBroker
          Community Adapters
          Donate

          415
          Online

          31.8k
          Users

          80.0k
          Topics

          1.3m
          Posts

          2
          3
          306
          Loading More Posts
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes
          Reply
          • Reply as topic
          Log in to reply
          Community
          Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
          The ioBroker Community 2014-2023
          logo