Announcement

Collapse
No announcement yet.

help: device status vbscript

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

  • help: device status vbscript

    i have a set-top box, inquiry
    HTML Code:
    http://192.168.0.3/web/powerstate
    shows the status below:

    HTML Code:
    <e2powerstate>
    <e2instandby>false</e2instandby>
    </e2powerstate>
    never did vbscript before. can anybody show me how to get the text <e2instandby>, and show it as Boolean(false=on/true=off) for my set-top box status?

    so far i came up with , dont know what to do next:
    PHP Code:
    Sub Main()

    Set xmlDoc CreateObject("Microsoft.XMLDOM")
    xmlDoc.Async "False"
    xmlDoc.Load("http://192.168.0.3/web/powerstate")

    Set colNodes=xmlDoc.selectNodes ("//e2powerstate/e2instandby")

    End Sub 

  • #2
    This is VB.net but probably will work to strip out the html stuff.
    I'm assuming this is for HS2 not HS3.
    I'm also assuming that your device is A1.

    The cool bit of code isn't mine, but I've used it a lot.

    PHP Code:
    Public Function RemoveTag(ByVal InpData As String) As String
        Dim intStart
    intEndintExist As Integer
        Dim strExtract
    strInput As String

        strInput 
    InpData

        
    While Instr(strInput"<") <> 0
            intStart 
    Instr(strInput"<")
            
    intEnd Instr(strInput">")

            If 
    intStart 1 Then
                strExtract 
    Mid(strInput1intStart 1) & Mid(strInputintEnd 1Len(strInput) - intEnd)
            Else
                
    strExtract Mid(strInputintEnd 1Len(strInput) - intEnd)
            
    End If
            
    strInput strExtract
        End 
    While

        Return 
    strInput
    End 
    Function 

    PHP Code:
    Dim myString as string "<e2powerstate><e2instandby>false</e2instandby></e2powerstate>"

    myString RemoveTag(myString)


    If 
    myString "false" Then
       hs
    .SetDeviceValue("A1",0)      'value to 0
       hs.SetDeviceString("A1","false",True) '
    string to "false"
       
    hs.SetDeviceStatus("A1",3)   'status to Off

    Else
       hs.SetDeviceValue("A1",1) '
    value to 0
       hs
    .SetDeviceString("A1","True",True'string to "true"
       hs.SetDeviceStatus("C1",2) '
    status to On
    End 
    if 
    If you want a boolean you can tweak the above code to do what you want. This is off the top of my head, but you get the idea. I set On to be true just
    to make it easier on my head.
    Hope this helps a bit....
    Last edited by donstephens; October 5th, 2013, 09:42 PM.
    Don

    Comment


    • #3
      Sorry Don but that will return the whole response and strip the html from it. According to me he need just the state here

      I've put together some code that does just that. I've added a few comments to make more clear what is happening. It made modulair so you just can grab pieces and reuse them. When dealing with http it's better to use error handling to prevent a crash when the url is not reachable.

      Code:
      Imports System.Net
      Imports System.IO
      
      
      Public Sub Main(ByVal Params As Object)
      '** Fill in your data
      Dim URL As String = " http://192.168.0.3/web/powerstate"
      Dim strTag As String = "<E2INSTANDBY>"
      Dim strDevice As String = "A1"
      ' ** These two are for program storage
      Dim strWebPage As String = ""
      Dim strDetails As String = ""
       
         '**********************************
         ' We are ready to get the html 
         '**********************************
         Try
           strWebPage = getHtml(URL)
         Catch ex As Exception
           hs.setdevicestring(strDevice, URL + " onbereikbaar")
           hs.setdevicevalue(strDevice, 0)
           hs.setdevicelastchange(strDevice, now)
           hs.writelog("Error","Connection to site failed due:" & ex.Message & "")
           Exit Sub
         End Try
       
         'Here we have some HTML data so now
         'its time to search for the tag
         strDetails = GetValue(strTag, strWebPage)
       
         'Now fill your device
         hs.setdevicestring(strDevice, strDetails)
      End Sub
       
      '********************************
      '********************************
      ' Additional functions and such
      '********************************
      '********************************
      ' Find a the first tag (strTag) in a string (strString) and read its value
      ' example : strTag = "<E2INSTANDBY>"
      Function GetValue(ByVal strTag As String, ByVal strString As String) As String
        Dim strTemp As String
        ' The value be found in the given HTML tag
        ' So get there first.
        strTemp = Right(strString, Len(strString) - Instr(strString , strTag) + Len(strTag) )
       
        ' We have reached the relevant block
        ' we want to grab
        strTemp = Right(strTemp, Len(strTemp) - Instr(strTemp, ">") )
        Return strTemp
      End Function
       
      ' Get the HTML code of the requested web site
      Function getHtml(ByVal url As String) As String
         Dim myWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
         myWebRequest.Method = "GET"
         ' make request for web page
         Dim myWebResponse As HttpWebResponse = DirectCast(myWebRequest.GetResponse(), HttpWebResponse)
         Dim myWebSource As New StreamReader(myWebResponse.GetResponseStream())
         Dim myPageSource As String = String.Empty
         myPageSource = myWebSource.ReadToEnd()
         myWebResponse.Close()
         return myPageSource
      End Function
      Last edited by AshaiRey; October 6th, 2013, 07:15 AM.

      Comment


      • #4
        thanks i will try that and let you know.
        yes it is HS2, i am getting this error,

        Code:
        06/10/2013 11:19:03  - Event - Running script in background: naam.vb
        06/10/2013 11:19:03  - Error - Script compile error: O tipo 'HttpWebRequest' não está definido.on line 71
        06/10/2013 11:19:03  - SCR - Option Strict Offimports Schedulerimports SystemPublic Module scriptcode11#Region "Automatically generated code, do not modify"'Automatically generated code, do not modify'Event Sources Begin	<System.ContextStaticAttribute()> Public WithEvents hs As Scheduler.hsapplication	<System.ContextStaticAttribute()> Public WithEvents hsp As scheduler.hsp	<System.ContextStaticAttribute()> Public WithEvents hssystem As scheduler.phone0'Event Sources End'End of automatically generated code#End RegionSub Main()'Set xmlDoc = CreateObject("Microsoft.XMLDOM")'xmlDoc.Async = "False"'xmlDoc.Load("http://192.168.0.3/web/powerstate")'Set colNodes=xmlDoc.selectNodes ("//e2powerstate/e2instandby")'** Fill in your dataDim URL As String = " http://192.168.0.3/web/powerstate"Dim strTag As String = ""Dim strDevice As String = "A1"' ** These two are for program storageDim strWebPage As String = ""Dim strDetails As String = ""    '**********************************   ' We are ready to get the html    '**********************************   Try     strWebPage = getHtml(URL)   Catch ex As Exception     hs.setdevicestring(strDevice, URL + " onbereikbaar")     hs.setdevicevalue(strDevice, 0)     hs.setdevicelastchange(strDevice, now)     hs.writelog("Error","Connection to site failed due:" & ex.Message & "")     Exit Sub   End Try    'Here we have some HTML data so now   'its time to search for the tag   strDetails = GetValue(strTag, strWebPage)    'Now fill your device   hs.setdevicestring(strDevice, strDetails)End Sub '********************************'********************************' Additional functions and such'********************************'********************************' Find a the first tag (strTag) in a string (strString) and read its value' example : strTag = ""Function GetValue(ByVal strTag As String, ByVal strString As String) As String  Dim strTemp As String  ' The value be found in the given HTML tag  ' So get there first.  strTemp = Right(strString, Len(strString) - Instr(strString , strTag) + Len(strTag) )   ' We have reached the relevant block  ' we want to grab  strTemp = Right(strTemp, Len(strTemp) - Instr(strTemp, ">") )  Return strTempEnd Function ' Get the HTML code of the requested web siteFunction getHtml(ByVal url As String) As String   Dim myWebRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)   myWebRequest.Method = "GET"   ' make request for web page   Dim myWebResponse As HttpWebResponse = DirectCast(myWebRequest.GetResponse(), HttpWebResponse)   Dim myWebSource As New StreamReader(myWebResponse.GetResponseStream())   Dim myPageSource As String = String.Empty   myPageSource = myWebSource.ReadToEnd()   myWebResponse.Close()   return myPageSourceEnd FunctionEnd Module
        Last edited by hjoy; October 6th, 2013, 05:15 AM.

        Comment


        • #5
          I think its not the code is the problem.

          can i just use the script as an action? and it will show in the log device status as false/true?

          Comment


          • #6
            I didn't check the code, sorry. Because i can't test it here.

            I forgot three rows at the start and i've added them in the previous posting.
            Also i get the impression you don't have any scripting experience? It's not problem but if so i need to adjust my expectations of what you know and not when explaining things.

            Yes, you can use the script as an action and it wil show whatever is filled in the response of the HTML

            Comment


            • #7
              sorry , i should have explain you better way. i have no experience of vb code, so i cant follow what is happening through the code. your comment it helps little bit. i would be better in php coding(just basic).

              I am new to hs, and i understand the scripting and how it works. i just dont understand how can i put that statement false as a status of my device, which can trigger another event.

              Comment


              • #8
                the new code return no error. but i cant see any status in log...it just shows like below
                Code:
                06/10/2013 13:34:45  - Event - Running script in background: naam.vb
                Originally posted by AshaiRey View Post
                I didn't check the code, sorry. Because i can't test it here.

                I forgot three rows at the start and i've added them in the previous posting.
                Also i get the impression you don't have any scripting experience? It's not problem but if so i need to adjust my expectations of what you know and not when explaining things.

                Yes, you can use the script as an action and it wil show whatever is filled in the response of the HTML

                Comment


                • #9
                  getting there

                  thanks again, i add a device A1 and now it only shows the status false, if the box is off it should say "true", but it dosent change, any idea?



                  ITs working now, yeeeee

                  sorry for wrecking your head.
                  Last edited by hjoy; October 6th, 2013, 08:48 AM.

                  Comment


                  • #10
                    Glad we could help. Ashai's code is way nicer. Will use some of that in other projects.
                    Don

                    Comment


                    • #11
                      just one more thing to understand the code better,

                      is it too hard, if i want to convert the string "false" to device status on and "true" to off?

                      is the strDetails hold the string?

                      if it does, i can just use if/else method or is it not that easy?

                      Comment


                      • #12
                        Originally posted by hjoy View Post
                        i add a device A1 and now it only shows the status false, if the box is off it should say "true", but it dosent change, any idea?
                        I assumed that you use a devicecode of A1 you should fill in there the devicecode you actually use.
                        (Remark: using A1 isn't a great idea because device tends to fall back to devicecode A1 when they power on after a battery change. Motion sensors like the MS13A (Hawkeye II) have this behavior.

                        Originally posted by hjoy View Post
                        just one more thing to understand the code better,

                        is it too hard, if i want to convert the string "false" to device status on and "true" to off?

                        is the strDetails hold the string?

                        if it does, i can just use if/else method or is it not that easy?
                        No, it's just as easy as that.
                        In your case you know that the only response you will get is false or true

                        Now i got the habbit to make everything uppercase when i do string comparison just to be sure that i have the right words. Uppercase and lowercase strings are different when you compare them.

                        And yes, strDetails will hold the string that is found.
                        Just change this piece
                        Code:
                         
                          'Now fill your device
                           hs.setdevicestring(strDevice, strDetails)
                        into

                        Code:
                          strDetails = UCase(strDetails)
                         
                          'Transform you result to a more suitable form
                          If strDetails = "FALSE" Then 
                             strDetail = "OFF"
                          Else 
                             strDetail = "ON"
                          End If
                         
                          'Now fill your device
                          hs.setdevicestring(strDevice, strDetails)

                        Comment


                        • #13
                          Thanks for your help. It is well explained. everything working perfect.

                          Comment

                          Working...
                          X