Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple HTTP Call to Device from CQC
#11
(01-20-2020, 01:29 PM)Dean Roddey Wrote: If you do it via CML you can pass anything you want. You need to call SetAutoAuth() on the HTTPClient object and it will do authorization based on the info it was given. Call SetAuthInfo() to set the name and password, which it will use.

thanks.  I've started looking over the CML stuff for use with CML Macros.  I'll look around for some samples or some basic stuff to get an idea of the code structure and start playing with it.
Reply
#12
I've tried making some additions to the doHTTPGet macro that I found.  I'm not doing it right though.  I'm still getting a 401 error.  I ran the debug and it compiles but the info still isn't being passed.

Code:
Class=[NonFinal]
    ClassPath MEng.User.DoHTTPGet;
    ParentClass MEng.Object;
EndClass;

Imports=
    MEng.System.Runtime.HTTPClient;
    MEng.System.Runtime.URL;
EndImports;

Members=
    // An HTTP client to make server requests
    HTTPClient  m_HTTP;
EndMembers;


Methods=[Public,Final]

    Constructor()
    Begin
    EndConstructor;

   

    Method Start([In] String URLStr, [In] String User, [In] String Pass, [Out] String RetCode) Returns Int4
    Begin
       
        Locals=
            LinesList   OutHdrLines;
            LinesList   InHdrLines;
            URL         URLToGet;
            String      RepText;
            String      ContType;
            MemBuf      DataBuf;
            Card4       ContLen;
            Card4       ResCode;
            Boolean     ToSet;
        EndLocals;

        ToSet := True;
        m_HTTP.SetAutoAuth(ToSet);
        m_HTTP.SetAuthInfo(User, Pass);

        Try
            // Query a page that really is actually json content
            URLToGet.Set(URLStr, FQTypes.Full);

            ContLen := 0;
            ResCode := m_HTTP.SendGET
            (
                URLToGet
                , 5000
                , "CQSL/DoHTTPGet"
                , "*/*"
                , RepText
                , OutHdrLines
                , ContType
                , DataBuf
                , ContLen
                , False
                , InHdrLines
            );

            RetCode.Clear();
            RetCode.AppendCard4(ResCode, Radices.Dec);
        EndTry;

        Catch
            Return 0;
        EndCatch;

        Return 1;
    EndMethod;
EndMethods;
Reply
#13
What kind of authorization is is wanting? Is it basic or digest type?
Dean Roddey
Explorans limites defectum
Reply
#14
And strike 2.  Changed it around and hard coded user/pass to a default value and still getting a 401.  Hmm... but I'm finding little pieces here and there along the way.  This should be dirt simple really.

Is there anyway in the editor or somehow to "print" a value during coding/debug to check that things are actually getting set as expected?

Code:
Class=[NonFinal]
    ClassPath MEng.User.DoHTTPGet;
    ParentClass MEng.Object;
EndClass;

Imports=
    MEng.System.Runtime.HTTPClient;
    MEng.System.Runtime.URL;
    MEng.System.Runtime.KVPair;
EndImports;

Members=
    // An HTTP client to make server requests
    HTTPClient  m_HTTP;
    KVPair      m_KVPair;
EndMembers;


Methods=[Public,Final]

    Constructor()
    Begin
    EndConstructor;

   

    //Method Start([In] String URLStr, [In] String User, [In] String Pass, [Out] String RetCode) Returns Int4
    Method Start([In] String URLStr, [Out] String RetCode) Returns Int4
    Begin
       
        Locals=
            LinesList   OutHdrLines;
            LinesList   InHdrLines;
            URL         URLToGet;
            String      RepText;
            String      ContType;
            MemBuf      DataBuf;
            Card4       ContLen;
            Card4       ResCode;
            //Boolean     ToSet;
            String      AuthString;
        EndLocals;

        //ToSet := True;
        //m_HTTP.SetAutoAuth(True);
        //m_HTTP.SetAuthInfo(User, Pass);
        //m_HTTP.CreateBasicAuthStr(User, Pass, AuthString);
        m_HTTP.SetAutoAuth(True);
        m_HTTP.CreateBasicAuthStr("admin", "admin", AuthString);
        AuthString.Prepend("Basic ");

        m_KVPair.Set("Authorization", AuthString);
        OutHdrLines.AddObject(m_KVPair);

        Try
            // Query a page that really is actually json content
            URLToGet.Set(URLStr, FQTypes.Full);

            ContLen := 0;
            ResCode := m_HTTP.SendGET
            (
                URLToGet
                , 5000
                , "CQSL/DoHTTPGet"
                , "*/*"
                , RepText
                , OutHdrLines
                , ContType
                , DataBuf
                , ContLen
                , False
                , InHdrLines
            );

            RetCode.Clear();
            RetCode.AppendCard4(ResCode, Radices.Dec);
        EndTry;

        Catch
            Return 0;
        EndCatch;

        Return 1;
    EndMethod;
EndMethods;
Reply
#15
Getting closer.... I think the headers In/Out were reversed and now I'm getting a 404 (not found).   Something with the way the URL is parsed perhaps?

Passing in via a GlobalActs test run:

Code:
MacroEng::RunMacro
    P1=MEng.User.DoHTTPGet
    P2=http://192.168.1.106/rest/nodes/55066/cmd/DON, LVar:RespCode


Code:
Class=[NonFinal]
    ClassPath MEng.User.DoHTTPGet;
    ParentClass MEng.Object;
EndClass;

Imports=
    MEng.System.Runtime.HTTPClient;
    MEng.System.Runtime.URL;
    MEng.System.Runtime.KVPair;
EndImports;

Members=
    // An HTTP client to make server requests
    HTTPClient  m_HTTP;
EndMembers;


Methods=[Public,Final]

    Constructor()
    Begin
    EndConstructor;

   
    Method Start([In] String URLStr, [Out] String RetCode) Returns Int4
    Begin
       
        Locals=
            LinesList   OutHdrLines;
            LinesList   InHdrLines;
            URL         URLToGet;
            String      RepText;
            String      ContType;
            MemBuf      DataBuf;
            Card4       ContLen;
            Card4       ResCode;
            KVPair      HdrPair;
            String      AuthString;
        EndLocals;

        Try
            // Load up some standard ingoing header lines
            InHdrLines.RemoveAll();
            HdrPair.Set("Host", "192.168.1.10");
            InHdrLines.AddObject(HdrPair);

            m_HTTP.CreateBasicAuthStr("admin", "admin", AuthString);
            AuthString.Prepend("Basic ");
            HdrPair.Set("Authorization", AuthString);
            InHdrLines.AddObject(HdrPair);

            //Set the URL to query
            URLToGet.Set(URLStr, FQTypes.Full);

            ContLen := 0;
            ResCode := m_HTTP.SendGET
            (
                URLToGet
                , 5000
                , "CQSL/DoHTTPGet"
                , "text/xml"
                , RepText
                , OutHdrLines
                , ContType
                , DataBuf
                , ContLen
                , False
                , InHdrLines
            );


            RetCode.Clear();
            RetCode.AppendCard4(ResCode, Radices.Dec);
        EndTry;

        Catch
            Return 0;
        EndCatch;

        Return 1;
    EndMethod;
EndMethods;
Reply
#16
Now that was a fun learning adventure.  After lots of head banging I finally found the CQCLogging routine and after adding that the answer was very clear.

The CML Macro was expecting 2 inputs which I knew.  In the test I was comma separating them as had been typical with other action editor entries.  BUT the CML example I was basing this from didn't expect comma separation only spaces so the comma was actually being included as part of the URL string!!!!

Step one done.  Now I can use the CML macro to control my ISY Scenes and every other missing device in the ISY driver.

Step two is to make a PUT version to control Philips Hue Groups!
Reply
#17
I assume you would be able to submit user name and password as part of a PUT using this method?
Reply
#18
(01-21-2020, 03:44 PM)gReatAutomation Wrote: I assume you would be able to submit user name and password as part of a PUT using this method?
This macro is specific to a HTTP GET and it does use a username/password in the GET.  I posted another thread where I was fighting with the PUT.  However that macro did not deal with authentication because I was writing it to send PUT commands to the Hue bridge to turn on groups of lights.
Reply
#19
Thanks. I was able to re-use what you did and make it a PUT with authentication and it works great  Big Grin I needed something like this  to interface with my NVR API such that I could reboot the NVR when I detected a camera that had gone offline for any reason, among other cool things.
Reply
#20
(01-23-2020, 01:28 PM)gReatAutomation Wrote: Thanks. I was able to re-use what you did and make it a PUT with authentication and it works great  Big Grin I needed something like this  to interface with my NVR API such that I could reboot the NVR when I detected a camera that had gone offline for any reason, among other cool things.

Cool glad it was put to use.  I have a PUT version cobbled for turning on/off Hue Groups which was my driver for it.  I was testing the HTTP-Trigger with my camera system (Xeoma) for a motion trigger in my garage.

I'm waiting, hoping the ISY driver will be updated soon.... I need that before I can really do much more.  I'm giving thought on replacing Insteon with Lutron though.  I can get Lutron dimmers pretty cheap so... maybe.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending HTTP PUT Commands gReatAutomation 2 1,635 09-17-2019, 01:11 PM
Last Post: gReatAutomation
  Device varible in email greyjeep 3 2,276 01-09-2018, 05:21 PM
Last Post: Dean Roddey
  Train HTTP Trigger Driver jkmonroe 24 14,765 10-25-2016, 11:55 AM
Last Post: karenlee
  Harmony Hub Device Control jkmonroe 6 4,846 07-03-2016, 02:36 PM
Last Post: potts.mike
  zwave device configuration jkmonroe 75 26,759 12-31-2015, 05:21 PM
Last Post: Deane Johnson
  Eventghost able to Ping device out of network EST 10 5,630 09-24-2015, 05:18 PM
Last Post: EST
  Help setting up GeoFancy & HTTP Trigger driver Sendero 10 5,841 04-20-2015, 11:08 AM
Last Post: bbrendon
  Simple Clarification Questions Deane Johnson 5 3,127 01-03-2015, 03:20 PM
Last Post: Deane Johnson
  Android based device Voice control of CQC How To Discussion Thread batwater 5 4,616 12-07-2013, 10:01 PM
Last Post: RichardU
  HTTP Trigger Driver EST 5 3,946 08-01-2013, 05:59 AM
Last Post: EST

Forum Jump:


Users browsing this thread: 1 Guest(s)