Announcement

Collapse
No announcement yet.

http PUT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    http PUT

    Hi all, I am hoping someone familiar with .NET can help me out (I am a Mac person who recently migrated to HS3 on Windows 7 from Vera and I do not know which way is up in .NET or vbscript).

    I attempted to adapt a vb script from @Jon00 's post https://forums.homeseer.com/showpost...3&postcount=12 to make an http PUT to a ha-bridge server running on the same machine as HS3 so I can keep a ha-bridge device's status in sync with a HS3 device:

    Code:
    Imports System.IO
    Imports System.Net
    
    Sub Main(ByVal Parm As Object)
      Dim httpWebRequest = DirectCast(WebRequest.Create("http://<ha-bridge ip>:<port>/api/devices/lights/6/bridgeupdatestate"), HttpWebRequest)
      httpWebRequest.ContentType = "application/json"
      httpWebRequest.Method = "PUT"
    
      Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream().ToString)
        Dim json As String = "{""on"": true, ""bri"": 127 }"
        streamWriter.Write(json)
      End Using
    
      Dim httpResponse = DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
      Using streamReader = New StreamReader(httpResponse.GetResponseStream().ToString)
        Dim responseText = StreamReader.ReadToEnd()
        hs.WriteLog("Response", responseText)
      End Using
    End Sub
    But it looks like the http PUT call is never made and the response is just logged as the json body (and, embarrassingly, I don't even know enough VB script to know what I am missing! )

    Code:
    {"on": true, "bri": 127 }
    ------

    When I do the PUT using curl from my Mac:

    Code:
    curl -H "Content-Type: application/json" -X PUT -d '{ "on": true, "bri": 127  }' http://<ha-bridge ip>:<port>/api/devices/lights/6/bridgeupdatestate
    The ha-bridge device status is correctly updated and the response comes back as:

    Code:
    [{"success":{"/lights/6/state/on":true}},{"success":{"/lights/6/state/bri":127}}]
    ------

    I would really appreciate any suggestions on what I need to change in my HS3 script to get it to actually do the PUT. Thanks!

    #2
    Are there any errors in the log? Note that this is vb.net code so make sure your script file as a .vb extension and not .txt.

    Originally posted by mda View Post
    Hi all, I am hoping someone familiar with .NET can help me out (I am a Mac person who recently migrated to HS3 on Windows 7 from Vera and I do not know which way is up in .NET or vbscript).

    I attempted to adapt a vb script from @Jon00 's post https://forums.homeseer.com/showpost...3&postcount=12 to make an http PUT to a ha-bridge server running on the same machine as HS3 so I can keep a ha-bridge device's status in sync with a HS3 device:

    Code:
    Imports System.IO
    Imports System.Net
    
    Sub Main(ByVal Parm As Object)
      Dim httpWebRequest = DirectCast(WebRequest.Create("http://<ha-bridge ip>:<port>/api/devices/lights/6/bridgeupdatestate"), HttpWebRequest)
      httpWebRequest.ContentType = "application/json"
      httpWebRequest.Method = "PUT"
    
      Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream().ToString)
        Dim json As String = "{""on"": true, ""bri"": 127 }"
        streamWriter.Write(json)
      End Using
    
      Dim httpResponse = DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)
      Using streamReader = New StreamReader(httpResponse.GetResponseStream().ToString)
        Dim responseText = StreamReader.ReadToEnd()
        hs.WriteLog("Response", responseText)
      End Using
    End Sub
    But it looks like the http PUT call is never made and the response is just logged as the json body (and, embarrassingly, I don't even know enough VB script to know what I am missing! )

    Code:
    {"on": true, "bri": 127 }
    ------

    When I do the PUT using curl from my Mac:

    Code:
    curl -H "Content-Type: application/json" -X PUT -d '{ "on": true, "bri": 127  }' http://<ha-bridge ip>:<port>/api/devices/lights/6/bridgeupdatestate
    The ha-bridge device status is correctly updated and the response comes back as:

    Code:
    [{"success":{"/lights/6/state/on":true}},{"success":{"/lights/6/state/bri":127}}]
    ------

    I would really appreciate any suggestions on what I need to change in my HS3 script to get it to actually do the PUT. Thanks!
    website | buy now | support | youtube

    Comment


      #3
      Originally posted by rjh View Post
      Are there any errors in the log? Note that this is vb.net code so make sure your script file as a .vb extension and not .txt.
      nope, no errors in the log, it just seems to log the json payload as the response for some strange reason. Here is what the HS3 log looks like when I run the script:

      Code:
      Aug-28 10:26:26 PM	 	Response	{"on": true, "bri": 127 }
      Aug-28 10:26:25 PM	 	Event	Running script in background: C:/Program Files/HomeSeer HS3/scripts/sync_habridge_theater_lamps_on.vb
      I think the Response probably should be this if it were actually working:

      Code:
      [{"success":{"/lights/6/state/on":true}},{"success":{"/lights/6/state/bri":127}}]
      The script is indeed saved with a .vb extension.

      Is there perhaps some way to just use curl?

      Code:
      curl -H "Content-Type: application/json" -X PUT -d '{ "on": true, "bri": 127  }' http://<ha-bridge ip>:<port>/api/devices/lights/6/bridgeupdatestate
      Last edited by mda; August 29, 2017, 05:04 PM. Reason: Added question:

      Comment


        #4
        I installed curl.exe and that works, e.g.:

        Code:
        "C:\Program Files\cURL\curl.exe" -H "Content-Type: application/json" -X PUT http://<IP address>:<port>/api/devices/lights/6/bridgeupdatestate -d "{\"on\":true}"

        Comment

        Working...
        X