Announcement

Collapse
No announcement yet.

how to send a http post command by homesser

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

  • zwolfpack
    replied
    Great!

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack you are awesome!!
    It worked perfectly, again I can not thank you.
    You really are good at what you do.
    thank you very much.

    Leave a comment:


  • zwolfpack
    replied
    The device name isn't quite correct. I used << and >> to highlight what needed to be changed, those need to be removed. So change the line

    Const devName = "<<Multroom>> slidersom1-M8"

    to

    Const devName = "Multroom SLIDERSOM1"

    or whatever the exact name of the device is. You might need to play with that. In your original you had slidersom1-M8, but in the log entry above it says SLIDERSOM1

    Don't know where that error is coming from. The script appears to be running. Are you triggering it on a change of the slider? That could be causing it to run several times very quickly.

    Also, try changing this line

    If dvRef = 0 Then

    to this

    If dvRef <= 0 Then

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack the script is working with error, move the slider else is not changing the values in the "dvref" variable. is only showing the value (-1) independent of the position that I place the slider.

    Otherwise the script is sending to the machine correctly.
    Sometimes the following error appears in the log.

    follow the script with the room changed, if possible check where I'm going wrong.
    nov-19 11:46:36 Error 3 Running script testeslider.vb :Uma exceção foi acionada pelo destino de uma chamada.->Does entry point Main exist in script? em System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) em System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Obj ect obj, Object[] parameters, Object[] arguments) em System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) em Scheduler.clsRunVBNetScript.ExecuteScript()
    nov-19 11:46:36 Lexicon-DD8P POST data: {"volume":"-0,1"}
    nov-19 11:46:36 Lexicon-DD8P device value = -1
    nov-19 11:46:36 Device Control Device: Multroom SLIDERSOM1 to Dim 92% (92) by/from: CAPI Control Handler
    nov-19 11:46:36 Device Control Device: Multroom SLIDERSOM1 to Dim 1% (92) by/from: CAPI Control Handler






    ' Lexicon-DD8P control script
    ' ref. https://www.lexicon.com/tl_files/cat...l-Protocol.pdf
    ' valid parameters for URI /output:
    ' name=OUTPUT1
    ' mute=0
    ' dim=1
    ' volume=0.000000
    ' turn_on_volume=0.000000
    ' max_volume=0.000000
    ' limiter=0
    ' output_mix=[1.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.0000 00,0.000000]


    ' set debug to true for additional logging or false for normal operation
    Const debug = true

    Imports System.IO
    Imports System.Net
    Imports System.Text
    Sub Main(p as String)
    Const logName = "Lexicon-DD8P"

    Const server = "192.168.0.175"

    ' Name of slider device - include location
    Const devName = "<<Multroom>> slidersom1-M8"

    ' read device value
    Dim dvRef = hs.GetDeviceRefByName(devName)
    If dvRef = 0 Then
    hs.WriteLogEx(logName, "ERROR: can't find reference for device " & devName, "#ff0000")
    Return
    End If
    Dim dv As Integer = hs.DeviceValue(dvRef)
    If debug Then hs.WriteLog(logName, "device value = " & dv.toString)

    ' scale slider value to volume in dB
    Const scaleTodB = 0.1
    Dim volume As Double = dv * scaleTodB

    ' URI as specified in manual
    Dim uri = "/output/5"
    ' ensure uri has leading "/"
    If uri.Length > 0 Then If uri.Substring(0, 1) <> "/" Then uri = "/" & uri
    ' Create a request using a URL that can receive a post.
    Dim server_url = "http://" & server & uri

    ' set up parameters to control
    Dim nParams = 1
    Dim pj(nParams - 1) As String

    ' volume
    pj(0) = jPar("volume", volume.toString)

    ' add additional parameters if desired


    ' join JSON param into the POST data content
    Dim postData = "{" & String.Join(",", pj) & "}"
    If debug Then hs.WriteLog(logName, "POST data: " & postData)
    Dim request As WebRequest = WebRequest.Create(server_url)
    ' Set the Method property of the request to POST.
    request.Method = "POST"
    ' Create POST data and convert it to a byte array.
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    ' Set the ContentType property of the WebRequest.
    request.ContentType = "application/json"
    ' Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length
    ' Get the request stream.
    Dim dataStream As Stream = request.GetRequestStream()
    ' Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length)
    ' Close the Stream object.
    dataStream.Close()
    ' Get the response.
    Dim response As WebResponse = request.GetResponse()
    ' Display the status.
    If debug Then hs.WriteLog(logName, "POST status: " & CType(response, HttpWebResponse).StatusDescription)
    ' Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream()
    ' Open the stream using a StreamReader for easy access.
    Dim reader As New StreamReader(dataStream)
    ' Read the content.
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.
    If debug Then hs.WriteLog(logName, "POST response data: " & responseFromServer)
    ' Clean up the streams.
    reader.Close()
    dataStream.Close()
    response.Close()
    End Sub

    ' format a JSON name:value parameter, with appropriate quoting
    Function jPar(pname as String, pvalue as String) As String
    return jQuote(pname) & ":" & jQuote(pvalue)
    End Function

    Function jQuote(value as String) As String
    Dim vqq = """"
    If value.Length > 0 Then If InStr("0123456789[{", value.Substring(0,1)) Then vqq = ""
    return vqq & value & vqq
    End Function

    Leave a comment:


  • zwolfpack
    replied
    Give this a try. Couple of things that need to be adjusted

    - in the line
    Code:
    Const devName = "<<location>> slidersom1-M8"
    change <<room>> to the location property of your slider device

    - you likely will need to play with the scaling from the device (0-100) to the scaling needed for the volume. Adjust the line
    Code:
    Const scaleTodB = 0.1
    as needed

    Code:
    ' Lexicon-DD8P control script
    ' ref. https://www.lexicon.com/tl_files/catalog//LuxuryCars/Lexicon/Active/DD8PLUS/Lexicon-DD8P-Control-Protocol.pdf
    ' valid parameters for URI /output:
    ' name=OUTPUT1
    ' mute=0
    ' dim=1
    ' volume=0.000000
    ' turn_on_volume=0.000000
    ' max_volume=0.000000
    ' limiter=0
    ' output_mix=[1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000]
    
    
    ' set debug to true for additional logging or false for normal operation
    Const debug = true
    
    Imports System.IO  
    Imports System.Net  
    Imports System.Text  
    Sub Main(p as String)
        Const logName = "Lexicon-DD8P"
    
        Const server = "192.168.0.175"
    
        ' Name of slider device - include location
        Const devName = "<<location>> slidersom1-M8"
    
        ' read device value
        Dim dvRef = hs.GetDeviceRefByName(devName)
        If dvRef = 0 Then
            hs.WriteLogEx(logName, "ERROR: can't find reference for device " & devName, "#ff0000")
            Return
        End If
        Dim dv As Integer = hs.DeviceValue(dvRef)
        If debug Then hs.WriteLog(logName, "device value = " & dv.toString)
    
        ' scale slider value to volume in dB
        Const scaleTodB = 0.1
        Dim volume As Double = dv * scaleTodB
    
        ' URI as specified in manual
        Dim uri = "/output/5"
        ' ensure uri has leading "/"
        If uri.Length > 0 Then If uri.Substring(0, 1) <> "/" Then uri = "/" & uri
        ' Create a request using a URL that can receive a post.   
        Dim server_url = "http://" & server & uri
    
        ' set up parameters to control
        Dim nParams = 1
        Dim pj(nParams - 1) As String
    
        ' volume
        pj(0) = jPar("volume", volume.toString)
    
        ' add additional parameters if desired
    
    
        ' join JSON param into the POST data content
        Dim postData = "{" & String.Join(",", pj) & "}"
        If debug Then hs.WriteLog(logName, "POST data: " & postData)
        Dim request As WebRequest = WebRequest.Create(server_url)
        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/json"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.
        If debug Then hs.WriteLog(logName, "POST status: " & CType(response, HttpWebResponse).StatusDescription)  
        ' Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        If debug Then hs.WriteLog(logName, "POST response data: " & responseFromServer)
        ' Clean up the streams.
        reader.Close()
        dataStream.Close()
        response.Close()
    End Sub
    
    ' format a JSON name:value parameter, with appropriate quoting
    Function jPar(pname as String, pvalue as String) As String
        return jQuote(pname) & ":" & jQuote(pvalue)
    End Function
    
    Function jQuote(value as String) As String
        Dim vqq = """"
        If value.Length > 0 Then If InStr("0123456789[{", value.Substring(0,1)) Then vqq = ""
        return vqq & value & vqq
    End Function

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack I was able to create the slider in the homeseer and the same is working the problem that I can not use the value of the slider in the post to be sending to the lexicon equipment. I'm putting down the script you've done for me, I've tried changing the number to a variable and it's displaying the following errors.
    I am counting on your help.

    Imports System.IO
    Imports System.Net
    Imports System.Text
    Sub Main(parm as object)


    Dim DevTemp
    Dim DevRef
    Dim DevAddress


    DevAddress = "slidersom1-M8"

    DevRef = hs.GetDeviceRef(DevAddress)
    DevTemp = hs.DeviceValue(DevRef)



    const debug = false
    const server = "192.168.0.175"
    const postData = "{""output_id"":5,""name"": ""Piscina"",""mute"":0,""dim"":1,""volume"":-(DevTemp).000002,""turn_on_volume"":-30.000002,""max_volume"":0.000000,""limiter"":0,""output_mix "":[1.000000,1.000000,1.000000,1.000000,1.000000,1.000000,1.0000 00,1.000000]}"
    const server_url = "http://" & server & "/output/5"
    Dim request As WebRequest = WebRequest.Create(server_url)
    request.Method = "POST"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    request.ContentType = "application/json" .
    request.ContentLength = byteArray.Length
    Dim dataStream As Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim response As WebResponse = request.GetResponse()
    If debug Then hs.writelog("POST request", CType(response, HttpWebResponse).StatusDescription)
    dataStream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    If debug Then hs.writelog("POST response", responseFromServer)
    reader.Close()
    dataStream.Close()
    response.Close()
    End Sub




    ERRORS SUBMITTED:

    nov-18 22:25:23 Error Compiling script testeslider.vb: 'request' não é membro de 'String'.
    nov-18 22:25:23 Error Compiling script testeslider.vb: O namespace ou o tipo especificado em Imports 'System.Core' não contém nenhum membro público ou não pode ser encontrado. Verifique se o namespace ou o tipo está definido e contém pelo menos um membro público. Verifique se o nome do elemento importado não usa aliases.
    nov-18 22:25:23 Error Compiling script testeslider.vb: 'request' não é membro de 'String'.
    nov-18 22:25:23 Error Compiling script testeslider.vb: O namespace ou o tipo especificado em Imports 'System.Core' não contém nenhum membro público ou não pode ser encontrado. Verifique se o namespace ou o tipo está definido e contém pelo menos um membro público. Verifique se o nome do elemento importado não usa aliases.

    Leave a comment:


  • JoshKitson
    replied
    Originally posted by zwolfpack View Post

    Looks like a forum thing. Does it for me as well until I put CODE tags around it.

    Your GetURL call has multiple issues. See http://help.homeseer.com/help/HS3/st...nternet_geturl

    Try
    Code:
    &hs.GetURL("https://connected2.homeseer.com", "/JSON?user=***&pass=***&request=controldevicebyvalue&ref=9&value=0", TRUE, 443)
    Port 443 is https. This may not work though, I don't know if it will get sent out encrypted.

    Edit: it does work. Recommend checking "do not log this event", otherwise your password will be in the log every time it runs.
    Perfect, Thank you!

    Leave a comment:


  • zwolfpack
    replied
    Originally posted by JoshKitson View Post

    Not sure why the space is appearing, it's definitely not there until i paste it in here.. In-fact it doesn't appear until I actually post the message. I've rewritten the command multiple times and keep getting the same outcome, any other thoughts?

    Running script statement immediately:
    &hs.GetURL("https://connected2.homeseer.com/JSON?user=***&pass=***&request=controldevicebyvalue&ref=9&va lue=0","",TRUE,8080)

    Literally every-time I edit the post and remove the space from the line above and hit save, the space re-appears
    Looks like a forum thing. Does it for me as well until I put CODE tags around it.

    Your GetURL call has multiple issues. See http://help.homeseer.com/help/HS3/st...nternet_geturl

    Try
    Code:
    &hs.GetURL("https://connected2.homeseer.com", "/JSON?user=***&pass=***&request=controldevicebyvalue&ref=9&value=0", TRUE, 443)
    Port 443 is https. This may not work though, I don't know if it will get sent out encrypted.

    Edit: it does work. Recommend checking "do not log this event", otherwise your password will be in the log every time it runs.
    Last edited by zwolfpack; November 10th, 2018, 03:30 PM.

    Leave a comment:


  • JoshKitson
    replied
    Originally posted by zwolfpack View Post
    Looks like you have an out-of-place space embedded in the word "value" - you have "...&va lue=0".
    Not sure why the space is appearing, it's definitely not there until i paste it in here.. In-fact it doesn't appear until I actually post the message. I've rewritten the command multiple times and keep getting the same outcome, any other thoughts?

    Running script statement immediately:
    &hs.GetURL("https://connected2.homeseer.com/JSON?user=***&pass=***&request=controldevicebyvalue&ref=9&va lue=0","",TRUE,8080)

    Literally every-time I edit the post and remove the space from the line above and hit save, the space re-appears

    Leave a comment:


  • zwolfpack
    replied
    Congrats on making it work! Sorry I can't help with the HSTouch Designer, I've never used that. Guessing that you could create a device that's controlled by a slider. Then you could read the value of that device into the script and put that value into the POST message.

    For help in creating the slider, perhaps ask in one of the HSTouch forums. Once you get the slider that controls a virtual device I can help using that in the POST script.

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack I would like to thank you again for your commitment to help me, I was able to make it work. I placed examente as it was showing up in google browser and it worked.

    Thank you and if you have any other way of thanking you that you are within my reach, you can count on me.

    The world needs more people like you zwolfpack.

    my only doubt now is how I create a slider in the hstouch that can increase and decrease the volume of these outputs. If you can help with this too I will be very grateful


    Imports System.IO
    Imports System.Net
    Imports System.Text
    Sub Main(parm as object)
    const debug = false
    const server = "192.168.0.175"
    const postData = "{""output_id"":0,""name"": ""Piscina-L"",""mute"":1,""dim"":1,""volume"":0.000002,""turn_on_vo lum e"":-30.000002,""max_volume"":0.000000,""limiter"":0,""output_mix "":[1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.0000 00,1.000000]}"
    ' Create a request using a URL that can receive a post.
    const server_url = "http://" & server & "/output/0"
    Dim request As WebRequest = WebRequest.Create(server_url)
    ' Set the Method property of the request to POST.
    request.Method = "POST"
    ' Create POST data and convert it to a byte array.
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    ' Set the ContentType property of the WebRequest.
    request.ContentType = "application/json"
    ' Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length
    ' Get the request stream.
    Dim dataStream As Stream = request.GetRequestStream()
    ' Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length)
    ' Close the Stream object.
    dataStream.Close()
    ' Get the response.
    Dim response As WebResponse = request.GetResponse()
    ' Display the status.
    If debug Then hs.writelog("POST request", CType(response, HttpWebResponse).StatusDescription)
    ' Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream()
    ' Open the stream using a StreamReader for easy access.
    Dim reader As New StreamReader(dataStream)
    ' Read the content.
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.
    If debug Then hs.writelog("POST response", responseFromServer)
    ' Clean up the streams.
    reader.Close()
    dataStream.Close()
    response.Close()
    End Sub
    Last edited by Jean carlos; November 8th, 2018, 06:52 PM.

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack I tried to do what you requested it sends the script without error but does not change anything in the equipment.

    Imports System.IO
    Imports System.Net
    Imports System.Text
    Sub Main(parm as object)
    const debug = false
    const server = "192.168.0.175"
    const postData = "{""output_id"":1,""name"": ""Piscina-R"",""mute"":1,""dim"":1,""volume"":0.000000,""turn_on_vo lum e"":0.000000,""max_volume"" :0.000000,""limiter"":0,""output_mix"":[1.000000,0.000000,1.000000,0.000000,1.00 0000,0.000000,1.000000,0.000000]}"
    ' Create a request using a URL that can receive a post.
    const server_url = "http://" & server & "/output/1"
    Dim request As WebRequest = WebRequest.Create(server_url)
    ' Set the Method property of the request to POST.
    request.Method = "POST"
    ' Create POST data and convert it to a byte array.
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    ' Set the ContentType property of the WebRequest.
    request.ContentType = "application/json"
    ' Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length
    ' Get the request stream.
    Dim dataStream As Stream = request.GetRequestStream()
    ' Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length)
    ' Close the Stream object.
    dataStream.Close()
    ' Get the response.
    Dim response As WebResponse = request.GetResponse()
    ' Display the status.
    If debug Then hs.writelog("POST request", CType(response, HttpWebResponse).StatusDescription)
    ' Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream()
    ' Open the stream using a StreamReader for easy access.
    Dim reader As New StreamReader(dataStream)
    ' Read the content.
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.
    If debug Then hs.writelog("POST response", responseFromServer)
    ' Clean up the streams.
    reader.Close()
    dataStream.Close()
    response.Close()
    End Sub

    Leave a comment:


  • zwolfpack
    replied
    I took a look at it last night, I'll try to post something later this evening.

    One thing, the term OUTPUT_IDX in the manual actually stands for a number 0 - 7. Try changing the line

    const server_url = "http://" & server & "/output/OUTPUT_IDX"

    to

    const server_url = "http://" & server & "/output/0"

    or 1, 2, etc...

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack you or anyone can help me with the problem quoted above?
    I tried to get the whole code and replace it in the command line, until it sends the command without error but it does not work.

    I had to put "" example "" quotes because the homesser was giving syntax error.

    please can not find what's missing or what I'm doing wrong.

    follow the code below


    Imports System.IO
    Imports System.Net
    Imports System.Text
    Sub Main(parm as object)
    const debug = false
    const server = "192.168.0.175"
    const postData = "{""output_id"":0,""name"": ""OUTPUT 1"",""mute"":0,""dim"":1,""volume"":0.000000,""turn_on_volum e"":0.000000,""max_volume"" :0.000000,""limiter"":0,""output_mix"":[1.000000,0.000000,0.000000,0.000000,0.00 0000,0.000000,0.000000,0.000000]}"
    ' Create a request using a URL that can receive a post.
    const server_url = "http://" & server & "/output/OUTPUT_IDX"
    Dim request As WebRequest = WebRequest.Create(server_url)
    ' Set the Method property of the request to POST.
    request.Method = "POST"
    ' Create POST data and convert it to a byte array.
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
    ' Set the ContentType property of the WebRequest.
    request.ContentType = "application/json"
    ' Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length
    ' Get the request stream.
    Dim dataStream As Stream = request.GetRequestStream()
    ' Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length)
    ' Close the Stream object.
    dataStream.Close()
    ' Get the response.
    Dim response As WebResponse = request.GetResponse()
    ' Display the status.
    If debug Then hs.writelog("POST request", CType(response, HttpWebResponse).StatusDescription)
    ' Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream()
    ' Open the stream using a StreamReader for easy access.
    Dim reader As New StreamReader(dataStream)
    ' Read the content.
    Dim responseFromServer As String = reader.ReadToEnd()
    ' Display the content.
    If debug Then hs.writelog("POST response", responseFromServer)
    ' Clean up the streams.
    reader.Close()
    dataStream.Close()
    response.Close()
    End Sub

    Leave a comment:


  • Jean carlos
    replied
    zwolfpack returning to the subject of communication with the lexicon equipment, I'm having difficulties controlling the outputs of the equipment .. for being new and having changed the commands a lot I can not correctly enter the information in the script.
    Can you please help me mount these parameters in that script you set for me?
    Attached Files

    Leave a comment:

Working...
X