VTI aus Programm Picoc beschreiben

Einklappen
X
 
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge
  • Tico
    Lox Guru
    • 31.08.2016
    • 1035

    #16
    It works!
    Klicke auf die Grafik für eine vergrößerte Ansicht

Name: Picture.png
Ansichten: 209
Größe: 33,1 KB
ID: 405517

    I needed to make a change to strl length to 7200 on account of the file size. The program also complained about a couple of things -

    Code:
    const char IdToken[20] = "\"IdToken\":\"";
    I removed the term const and it doesn't complain. I'm not sure what other issues that might present?

    Code:
    [len] = 0;  // add trailing 0 to token string
    I commented out the entire line with //. This stopped the error text. It probably causes the next issue...

    The token generated is correct except that it is missing the first letter e. The beginning should be -

    eyJraWQiQiJsM3....


    The program is now -
    Code:
    FILE *fp1;
    char str1[7200];
    char *tokenStart;
    char *tokenEnd;
    char token[4095];
    char IdToken[20] = "\"IdToken\":\"";
    int len;
    int nEvents;
    
    while(TRUE)
    {
        nEvents = getinputevent();
        if (nEvents & 0x08) // check for change on analogue input 1 (Bit 4) - this is only used as a trigger to run following code
        {
            fp1 = fopen("/user/common/zodiac_token.json", "r");
    // open file with token read
            fgets(str1, 7200 , fp1); // copy content of file to string, max. 4094 bytes plus trailing 0
    
            tokenStart = strstr(str1,"IdToken");
    // search for IDtoken in string
            if (tokenStart != NULL) {
                tokenStart += strlen(IdToken); // if found then add length of token itself
                tokenEnd = strstr(tokenStart, "\"");
    // search for final quotation symbol
                if (tokenEnd != NULL) {
                    len = tokenEnd - tokenStart; // if found then calculate length of token
                    strncpy(token, tokenStart, len); // copy n bytes from string to token starting from begin of token
                    //[len] = 0; // add trailing 0 to token string
                    setoutput(0,len); // set analogue output 1 to length of token
                    setoutputtext(0,token); // set text output 1 to token
                }
            } else {
                setoutputtext(0,"");
    // clear output if no token is found              
                setoutput(0,0);
            }
        }
            fclose(fp1);
        }
        sleep(500); // sleep for half a second  
    }​
    Ich spreche kein Deutsch. Gib Google Translate die Schuld, wenn ich unverständlich bin.

    Kommentar

    • Jan W.
      Lox Guru
      • 30.08.2015
      • 1398

      #17
      Congratulations! The "const" may be removed without any consequences. The line
      Code:
      tokenStart += strlen(IdToken); // if found then add length of token itself
      can be changed to
      Code:
      tokenStart = tokenStart + strlen(IdToken) -1; // if found then add length of token itself
      to subtract one character. Im not sure why you got an error at
      Code:
      token[len] = 0; // add trailing 0 to token string
      The word "token" was missing in your post, but was included in my example. Maybe that's the reason for the error? The function "strncpy" copies the entire string, but strings in C needs to be terminated by a byte with 0 (zero). The string "str1" might be initialized with 0 (zeros) automatically at the first start of the program, but if the length of the token may change (gets shorter), it could result in an invalid token containing the rest of the previous one. For safety reasons I would try to add that line of code. If you still get errors, try
      Code:
      token[len] = (char) 0; // add trailing 0 to token string
      Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
      Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
      Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
      Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
      Node-RED: IKEA Tradfri

      Kommentar

      • Tico
        Lox Guru
        • 31.08.2016
        • 1035

        #18
        HelloJan W. I'm happy to report all those fixes worked. I've reproduced the code below. The program is very useful for doing OAuth 2.0 on the Miniserver in a standalone capacity. The full steps require a bit of a work-around in saving to the user/common directory, but at least the option exists for standalone authentication.

        Code:
        FILE *fp1;
        char str1[7200];
        char *tokenStart;
        char *tokenEnd;
        char token[4095];
        char IdToken[20] = "\"IdToken\":\"";
        int len;
        int nEvents;
        
        while(TRUE)
        {
            nEvents = getinputevent();
            if (nEvents & 0x08) // check for change on analogue input 1 ( Bit 4) - this is only used as a trigger to run following code
            {
                fp1 = fopen("/user/common/zodiac_token.json", "r");
        // open file with token read
                fgets(str1, 7200 , fp1); // copy content of file to string, plus trailing 0
        
                tokenStart = strstr(str1,"IdToken");
        // search for IDtoken in string
                if (tokenStart != NULL) {
                    //tokenStart += strlen(IdToken); // if found then add length of token itself
                    tokenStart = tokenStart + strlen(IdToken) -1; // if found then add length of token itself
                    tokenEnd = strstr(tokenStart, "\"");
        // search for final quotation symbol
                    if (tokenEnd != NULL) {
                        len = tokenEnd - tokenStart; // if found then calculate length of tokens
                        strncpy(token, tokenStart, len); // copy n bytes from string to token starting from begin of token
                        token[len] = 0; // add trailing 0 to token string
                        setoutput(0,len); // set analogue output 1 to length of token
                        setoutputtext(0,token); // set text output 1 to token
                    }
                } else {
                    setoutputtext(0,"");
        // clear output if no token is found              
                    setoutput(0,0);
                }
            }
                fclose(fp1);
            }
            sleep(500); // sleep for half a second  
        }​
        Zuletzt geändert von Tico; 02.10.2023, 13:19.
        Ich spreche kein Deutsch. Gib Google Translate die Schuld, wenn ich unverständlich bin.

        Kommentar


        • Tico
          Tico kommentierte
          Kommentar bearbeiten
          P.S. Be careful if using Google Translate when viewing the code. If viewing in other than German, the code will change slightly.
      • Rick_Pa_87
        Dumb Home'r
        • 04.08.2022
        • 17

        #19
        Also den Token auslesen das geht jetzt. Nur einen jason in folgendem Format

        [

        {

        "cmd" : "Login",

        "code" : 0,

        "value" : {

        "Token" : {

        "leaseTime" : 3600,

        "name" : "84f0963abb6bcf2"

        }

        }

        }

        ]


        geht nicht. da steigt das Programm aus. Vermutlich duch die Zeilenumbrüche

        Kommentar

        • Jan W.
          Lox Guru
          • 30.08.2015
          • 1398

          #20
          Rick_Pa_87 : An den Zeilenumbrüchen liegt es nicht. Der o.a. Pico C Code ist kein vollständiger JSON Parser, sondern sucht lediglich ein definierten String, wie z.B. "IDToken":" inkl. :" und nimmt dann den Teil bis zum nächsten " als Token. Bei Deinem JSON File sind Leerzeichen enthalten. Man müsste zunächst nach "Token" suchen, dann nach "name", dann nach " und den Teil bis zum abschließenden " als Token. Das würde funktionieren, auch wenn die Einhaltung des JSON Formates, z.B. "name" nur innerhalb der Klammern von "Token" zu suchen. Vereinfacht könnte man auch direkt nach "name" suchen, aber dann darf das vollständige JSON das Wort "name" nicht mehrmals enthalten.

          Hier ein ungetesteter Codeschnipsel für die o.a. Suche und ohne Prüfung, ob die Suchen jeweils erfolgreich waren:
          Code:
          ​...
          tokenStart = strstr(str1,"\"Token\"");       // search for token ID
          tokenStart = tokenStart + 7;   // move pointer forward to text following the token that was found
          tokenStart = strstr(tokenStart, "\"name\"");       // search for Token name
          tokenStart = tokenStart + 6;   // move pointer forward to text following "name"
          tokenStart = strstr(tokenStart, "\"");  // search for beginning quotation mark of token text
          tokenStart = tokenStart + 1;   // move pointer to text following quotation mark
          ​​tokenEnd = strstr(tokenStart, "\""); // search for next quotation at the end of the token text
          len = tokenEnd - tokenStart; // if found then calculate length of tokens
          strncpy(token, tokenStart, len); // copy n bytes from string to token starting from begin of token
          token[len] = 0; // add trailing 0 to token string
          setoutput(0,len); // set analogue output 1 to length of token
          setoutputtext(0,token); // set text output 1 to token
          ​
          Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
          Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
          Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
          Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
          Node-RED: IKEA Tradfri

          Kommentar

          • Tico
            Lox Guru
            • 31.08.2016
            • 1035

            #21
            Hello Jan W. I have a problem with an application when sending GET commands. I have reached the limit of what the Virtual Output command will support. The Loxone Monitor reports 'Virtual Output command too long'. The Virtual Output character limit appears to be ~2000 bytes and is probably an arbitrary limit similar to the Program Block 'setoutputtext' limit (1023 bytes increased to 4096 bytes).

            The majority of the command length is an OAuth token.

            I wanted to find out if it is viable to send a GET command directly from the Program Block and save the response to the Miniserver /user/common directory?

            The CURL command for the request is -

            Code:
            curl -X GET -H "Host:prod.zodiac-io.com" -H "accept:application/json" -H "authorization:OAuth_Token_here" -H "accept-encoding:identity" -H "user-agent:okhttp/3.12.0" "https://prod.zodiac-io.com/devices/v1/JT19999999/shadow"
            ​
            Ich spreche kein Deutsch. Gib Google Translate die Schuld, wenn ich unverständlich bin.

            Kommentar

            • Jan W.
              Lox Guru
              • 30.08.2015
              • 1398

              #22
              Hi Tico,
              there are many examples on the Internet for sending a http get request via c code, but the easiest might be example 2 from Loxone: http://updatefiles.loxone.com/Knowle...ing.pdf#page15
              In a nushell you create a string with the content of the get request, open a stream, write the request and read the answer. The example has a write to file at the end as well, but is missing the typical „while(true)“ loop.

              Best Regards,
              Jan
              Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
              Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
              Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
              Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
              Node-RED: IKEA Tradfri

              Kommentar


              • Tico
                Tico kommentierte
                Kommentar bearbeiten
                Thanks, I'll check it out.
            Lädt...