Announcement

Collapse
No announcement yet.

how to send a http post command by homesser

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

  • #31
    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.

    Comment


    • #32
      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

      Comment


      • #33
        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

        Comment


        • #34
          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

          Comment


          • #35
            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.

            Comment


            • #36
              Great!

              Comment

              Working...
              X