Announcement

Collapse
No announcement yet.

Need a little help

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

    Need a little help

    I need to parse the status of one device and copy part of it to another device.

    Example String:

    {"progress": 24, "location": "local", "path": "Shoe_Latch_Block_ABS.gcode"}


    I need to copy just the 24 in this example to another devices status.


    Is there a plug-in for that ?
    RJ_Make On YouTube

    #2
    So is this not possible?

    Sent from my ASUS_Z00AD using Tapatalk
    RJ_Make On YouTube

    Comment


      #3
      Originally posted by ServiceXp View Post
      So is this not possible?

      Sent from my ASUS_Z00AD using Tapatalk
      How are you getting that example string? You need to deserialize it, and then you can grab the key.

      In your case, something like this:

      Code:
              Dim myreq As HttpWebRequest = HttpWebRequest.Create("your web call URL")
              Dim myResp As HttpWebResponse
              Dim mywebheadercollection As WebHeaderCollection = myreq.Headers
              Dim post As JObject
              myreq.Method = "GET"
              myreq.ContentType = "application/json"
              myResp = myreq.GetResponse
              Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
              Dim myText As String
              myText = myreader.ReadToEnd() 'Serialized data
              post = Newtonsoft.Json.JsonConvert.DeserializeObject(myText)
              Return post.SelectToken("Devices[0].progress")

      Comment


        #4
        Originally posted by phuz View Post
        How are you getting that example string? You need to deserialize it, and then you can grab the key.

        In your case, something like this:

        Code:
                Dim myreq As HttpWebRequest = HttpWebRequest.Create("your web call URL")
                Dim myResp As HttpWebResponse
                Dim mywebheadercollection As WebHeaderCollection = myreq.Headers
                Dim post As JObject
                myreq.Method = "GET"
                myreq.ContentType = "application/json"
                myResp = myreq.GetResponse
                Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
                Dim myText As String
                myText = myreader.ReadToEnd() 'Serialized data
                post = Newtonsoft.Json.JsonConvert.DeserializeObject(myText)
                Return post.SelectToken("Devices[0].progress")
        Holy crap that looks dangerous. . The string is just a device status (see pic below).

        Thanks for your help, but I'm not sure what to do with that code.
        Attached Files
        RJ_Make On YouTube

        Comment


          #5
          Originally posted by ServiceXp View Post
          Holy crap that looks dangerous. . The string is just a device status (see pic below).

          Thanks for your help, but I'm not sure what to do with that code.
          Oh, in that case, you don't need all that stuff. Your serialized data is already in a string, so you can just do something like this:

          Code:
          Dim post As Newtonsoft.Json.Linq.JObject
          post = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring("A1"))
          hs.Setdevicestring(111, post.SelectToken("Devices[0].progress"), TRUE)
          Where A1 is the code of the device containing the string and 111 is the reference ID of the device you are setting.
          You may also need to set the device value, in addition to the device string, depending on how you're using it.

          To use this deserialize object, you need the Newtonsoft.Json.dll library.

          Add the code to a new script, and create an event to run that script every XX seconds.

          When you deserialize it, it looks like this, and you can reference each key:

          Code:
          {
            "progress": 24,
            "location": "local",
            "path": "Shoe_Latch_Block_ABS.gcode"
          }
          and if you use post.SelectToken("progress"), like this:
          hs.Setdevicestring(111, post.SelectToken("progress"), TRUE)

          you're new string will contain 24.

          Comment


            #6
            Originally posted by phuz View Post
            Oh, in that case, you don't need all that stuff. Your serialized data is already in a string, so you can just do something like this:

            Code:
            Dim post As Newtonsoft.Json.Linq.JObject
            post = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring("A1"))
            hs.Setdevicestring(111, post.SelectToken("Devices[0].progress"), TRUE)
            Where A1 is the code of the device containing the string and 111 is the reference ID of the device you are setting.
            You may also need to set the device value, in addition to the device string, depending on how you're using it.

            To use this deserialize object, you need the Newtonsoft.Json.dll library.

            Add the code to a new script, and create an event to run that script every XX seconds.

            When you deserialize it, it looks like this, and you can reference each key:

            Code:
            {
              "progress": 24,
              "location": "local",
              "path": "Shoe_Latch_Block_ABS.gcode"
            }
            and if you use post.SelectToken("progress"), like this:
            hs.Setdevicestring(111, post.SelectToken("progress"), TRUE)

            you're new string will contain 24.
            Thank you so much, I will attempt my first coding in HS3 tonight.

            Where do i get the .dll I need?

            Sent from my ASUS_Z00AD using Tapatalk
            RJ_Make On YouTube

            Comment


              #7
              I've attached it to this post.

              You need to put this in the C:\Program Files\HomeSeer HS3\Bin\homeseer directory.

              Then, in the C:\Program Files\HomeSeer HS3\Config\Settings.ini file, you need to add this line under the [Settings] section:

              ScriptingReferences=Newtonsoft.Json;c:\progra~1\homese~1\bin \homeseer\Newtonsoft.Json.dll
              Attached Files

              Comment


                #8
                If you get stuck at all, I can probably help you over Google chat or something.

                Comment


                  #9
                  Originally posted by phuz View Post
                  If you get stuck at all, I can probably help you over Google chat or something.
                  Started working on this and I noticed that none of the MQTT devices have codes, only Reference ID's?

                  I have a felling if I enter the Ref ID, it's going to fail?
                  RJ_Make On YouTube

                  Comment


                    #10
                    Originally posted by ServiceXp View Post
                    Started working on this and I noticed that none of the MQTT devices have codes, only Reference ID's?

                    I have a felling if I enter the Ref ID, it's going to fail?
                    Yes, so give it a code

                    Comment


                      #11
                      Here is my PassPrintProgress.vb code.

                      Code:
                      Sub Main(Optional ByVal pParms As String = "")
                      Dim MPSMV1        As Newtonsoft.Json.Linq.JObject
                      Dim MPSMV2        As Newtonsoft.Json.Linq.JObject
                      Dim FFCP        As Newtonsoft.Json.Linq.JObject
                      
                      Const iMPSMV1     As Integer = 1130
                      Const iMPSMV2     As Integer = 1152
                      Const iFFCP     As Integer = 1134
                      
                      Const sMPSMV1     As String = "A1"
                      Const sMPSMV2     As String = "A2"
                      Const sFFCP     As String = "A3"
                      
                             If pParms = "MPSMV1" Then
                                  MPSMV1 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV1))
                                  hs.Setdevicestring(iMPSMV1, MPSMV1.SelectToken("Devices[0].progress"), TRUE)
                                  hs.Setdevicestring(iMPSMV1, post.SelectToken("progress"), TRUE)
                             
                             ElseIf pParms = "MPSMV2"
                                MPSMV2 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV2))
                                hs.Setdevicestring(iMPSMV2, MPSMV2.SelectToken("Devices[0].progress"), TRUE)
                                hs.Setdevicestring(iMPSMV2, post.SelectToken("progress"), TRUE)
                             
                             ElseIf pParms = "FFCP"
                                FFCP = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sFFCP))
                                hs.Setdevicestring(iFFCP, FFCP.SelectToken("Devices[0].progress"), TRUE)
                                hs.Setdevicestring(iFFCP, post.SelectToken("progress"), TRUE)
                             End If
                             
                             
                         End Sub
                      Here is the Event:



                      Here is the Error I'm Getting:



                      I created a new folder for the .dll as there was already one present in the HomeSeer dir.

                      I also notice that the Startup.vb script was not running with a similar error. Now it's been a long time since I've restarted the server so I'm not sure If I created the new error of if something has been broke for a while.






                      Hmmmmmm, What have I done...
                      Attached Files
                      RJ_Make On YouTube

                      Comment


                        #12
                        I think I have the original problem puzzled out and this is the error I'm now receiving.


                        Running script C:\Program Files (x86)\HomeSeer HS3\scripts\PassPrintProgress.vb :Exception has been thrown by the target of an invocation.Conversion from string "A3" to type 'Integer' is not valid.
                        Here is the script

                        Code:
                        Sub Main(ByVal sParm As String)
                        Dim MPSMV1        As Newtonsoft.Json.Linq.JObject
                        Dim MPSMV2        As Newtonsoft.Json.Linq.JObject
                        Dim FFCP        As Newtonsoft.Json.Linq.JObject
                        
                        'Get Status From
                        Const sMPSMV1     As String = "A1"
                        Const sMPSMV2     As String = "A2"
                        Const sFFCP     As String = "A3"
                        
                        'Set Status To
                        Const sSMPSMV1     As String = "A4"
                        Const sSMPSMV2     As String = "A5"
                        Const sSFFCP     As String = "A6"
                        
                               If sParm = "MPSMV1" Then
                                MPSMV1 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV1))
                                    hs.Setdevicestring(sSMPSMV1, MPSMV1.SelectToken("Devices[0].progress"), TRUE)
                                    hs.Setdevicestring(sSMPSMV2, MPSMV1.SelectToken("progress"), TRUE)
                               
                               ElseIf sParm = "MPSMV2"
                                      MPSMV2 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV2))
                                  hs.Setdevicestring(sSMPSMV2, MPSMV2.SelectToken("Devices[0].progress"), TRUE)
                                  hs.Setdevicestring(sSMPSMV2, MPSMV2.SelectToken("progress"), TRUE)
                               
                               ElseIf sParm = "FFCP"
                                      FFCP = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sFFCP))
                                  hs.Setdevicestring(sSFFCP, FFCP.SelectToken("Devices[0].progress"), TRUE)
                                  hs.Setdevicestring(sSFFCP, FFCP.SelectToken("progress"), TRUE)
                               End If
                               
                               
                           End Sub
                        RJ_Make On YouTube

                        Comment


                          #13
                          Originally posted by ServiceXp View Post
                          I think I have the original problem puzzled out and this is the error I'm now receiving.


                          Here is the script

                          Code:
                          Sub Main(ByVal sParm As String)
                          Dim MPSMV1        As Newtonsoft.Json.Linq.JObject
                          Dim MPSMV2        As Newtonsoft.Json.Linq.JObject
                          Dim FFCP        As Newtonsoft.Json.Linq.JObject
                          
                          'Get Status From
                          Const sMPSMV1     As String = "A1"
                          Const sMPSMV2     As String = "A2"
                          Const sFFCP     As String = "A3"
                          
                          'Set Status To
                          Const sSMPSMV1     As String = "A4"
                          Const sSMPSMV2     As String = "A5"
                          Const sSFFCP     As String = "A6"
                          
                                 If sParm = "MPSMV1" Then
                                  MPSMV1 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV1))
                                      hs.Setdevicestring(sSMPSMV1, MPSMV1.SelectToken("Devices[0].progress"), TRUE)
                                      hs.Setdevicestring(sSMPSMV2, MPSMV1.SelectToken("progress"), TRUE)
                                 
                                 ElseIf sParm = "MPSMV2"
                                        MPSMV2 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV2))
                                    hs.Setdevicestring(sSMPSMV2, MPSMV2.SelectToken("Devices[0].progress"), TRUE)
                                    hs.Setdevicestring(sSMPSMV2, MPSMV2.SelectToken("progress"), TRUE)
                                 
                                 ElseIf sParm = "FFCP"
                                        FFCP = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sFFCP))
                                    hs.Setdevicestring(sSFFCP, FFCP.SelectToken("Devices[0].progress"), TRUE)
                                    hs.Setdevicestring(sSFFCP, FFCP.SelectToken("progress"), TRUE)
                                 End If
                                 
                                 
                             End Sub
                          Right off the bat, you don't need the Devices[0].progress part for each section of your IF statements. I was just using that as an example. Your string doesn't contain a "Devices" token, so you only need the 2nd statement in each.

                          Then, it turns out you need to use the reference ID now instead of the code.
                          Here's my working sample script using my test virtual device (ref id 540) that has your serialized data string in it:
                          Code:
                          Imports System.Text
                          Imports Newtonsoft.Json
                          Imports Newtonsoft.Json.Linq
                          
                          public Sub Main(Parms As Object)
                          dim myobject as newtonsoft.json.linq.jobject
                          dim inputstring as string
                          
                          inputstring = hs.devicestring(540) 'My test virtual device
                          myobject = newtonsoft.json.jsonconvert.deserializeobject(inputstring)
                          hs.writelog("test",myobject.selecttoken("progress").tostring)
                          End Sub
                          And the output:

                          Code:
                          Dec-03 7:02:19 AM	 	test	24
                          Dec-03 7:02:19 AM	 	Event	Running script in background: C:/Program Files/HomeSeer HS3/scripts/testscript.vb
                          Dec-03 7:02:19 AM	 	Event	Event Trigger "Manual test"
                          Dec-03 7:02:19 AM	 	Event	Event Manual test triggered by the event page 'Run' button.
                          Last edited by phuz; December 3, 2017, 07:06 AM.

                          Comment


                            #14
                            Thank you Phuz, It is now working perfectly. Here is the final code for future reference if anyone else faces this.

                            Code:
                            Sub Main(ByVal sParm As String)
                            Dim MPSMV1        As Newtonsoft.Json.Linq.JObject
                            Dim MPSMV2        As Newtonsoft.Json.Linq.JObject
                            Dim FFCP          As Newtonsoft.Json.Linq.JObject
                            
                            'Get Status From
                            Const sMPSMV1     As Integer = 1129
                            Const sMPSMV2     As Integer = 1166
                            Const sFFCP       As Integer = 1115
                            
                            'Set Status To
                            Const sSMPSMV1     As Integer = 1130
                            Const sSMPSMV2     As Integer = 1152
                            Const sSFFCP       As Integer = 1134
                            
                                   If sParm = "MPSMV1" Then
                                    MPSMV1 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV1))
                                        hs.Setdevicestring(sSMPSMV1, MPSMV1.SelectToken("progress"), TRUE)
                                   
                                   ElseIf sParm = "MPSMV2"
                                          MPSMV2 = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sMPSMV2))
                                      hs.Setdevicestring(sSMPSMV2, MPSMV2.SelectToken("progress"), TRUE)
                                   
                                   ElseIf sParm = "FFCP"
                                          FFCP = Newtonsoft.Json.JsonConvert.DeserializeObject(hs.devicestring(sFFCP))
                                      hs.Setdevicestring(sSFFCP, FFCP.SelectToken("progress"), TRUE)
                                   End If
                              
                               End Sub
                            Last edited by ServiceXp; January 17, 2018, 08:55 AM.
                            RJ_Make On YouTube

                            Comment


                              #15
                              Originally posted by ServiceXp View Post
                              Thank you Phuz, It is now working perfectly. Here is the final code for future reference if anyone else faces this.
                              Glad to hear. Enjoy!

                              Comment

                              Working...
                              X