Announcement

Collapse
No announcement yet.

Import Returned Text from cmd line to HS

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

    Import Returned Text from cmd line to HS

    Ok, so how do I 'grab' the text line which is returned when I run a command line statement?

    For example I have a program which when I type:

    CLAMP /STATUS

    it returns

    DATA


    how do I get the DATA into HS?

    #2
    When you say command line you mean via a dos prompt? If so try something like this.

    Sub Main

    Dim objShell, boolCode
    Set objShell = CreateObject("WScript.Shell")
    sReturnVal = objShell.Run("command line statement")

    End Sub
    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

    Comment


      #3
      you will need to output your dos cmd to a text file and then read the file into hs. something like this

      clamp /status>output.txt

      then use an hs script to open the output.txt file an read the contents


      then modify the following script to open and read the output.txt file

      Const ForReading = 1
      Const ForWriting = 2

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)

      strText = objFile.ReadAll
      objFile.Close
      Mark

      HS3 Pro 4.2.19.5
      Hardware: Insteon Serial PLM | AD2USB for Vista Alarm | HAI Omnistat2 | 1-Wire HA7E | RFXrec433 | Dahua Cameras | LiftMaster Internet Gateway | Tuya Smart Plugs
      Plugins: Insteon (mine) | Vista Alarm (mine) | Omnistat 3 | Ultra1Wire3 | RFXCOM | HS MyQ | BLRadar | BLDenon | Tuya | Jon00 Charting | Jon00 Links
      Platform: Windows Server 2022 Standard, i5-12600K/3.7GHz/10 core, 16GB RAM, 500GB SSD

      Comment


        #4
        Here is a VB.Net class that allows you to direct STDOUT and STDERR without having to use a temporary file. HomeSeer Tech could use this for their upgrade routines so we don't have to see the command line line popups (hint, hint).

        PHP Code:
            Dim MyCommand As New CmdShell

            Dim Results
        () As String MyCommand.DoCommand("ping""www.homeseer.com -n 1""c:\windows\system32")

            For 
        Each Line As String In Results
              Call hs
        .WriteLog("Debug"Line.ToString)
            
        Next 
        PHP Code:

        '
        Routines To Execute And Capture Program Output
        '
        Imports System.IO
        Imports System.Text.RegularExpressions

        Public Class CmdShell

          '
        ------------------------------------------------------------------------------- 
          
        ' Purpose:   Calls command line utility and returns results as String()
          ' 
        Input:     strProgram as StringstrArgs as StringstrWorkingDirectory as String 
          
        ' Output:    String() as String
          '
        ------------------------------------------------------------------------------- 
          Function 
        DoCommand(ByVal strProgram As String_
                             ByVal strArgs 
        As String_
                             ByVal strWorkingDirectory 
        As String) As String()

            
        Dim strOutput As String ""

            
        Try
              
        Dim myproc As New Process
              With myproc
                
        .StartInfo.FileName strProgram
                
        .StartInfo.Arguments strArgs
                
        .StartInfo.RedirectStandardOutput True
                
        .StartInfo.RedirectStandardError True
                
        .StartInfo.UseShellExecute False
                
        .StartInfo.CreateNoWindow True
                
        .StartInfo.WindowStyle ProcessWindowStyle.Hidden
                
        .StartInfo.WorkingDirectory strWorkingDirectory
                
        .Start()
              
        End With

              Dim StdOut 
        As StreamReader myproc.StandardOutput
              strOutput 
        StdOut.ReadToEnd()

              
        Dim StdErr As StreamReader myproc.StandardError
              strOutput 
        StdErr.ReadToEnd() & strOutput

              myproc
        .WaitForExit()

              

              ' 
        Prepare string for parsing 
              

              strOutput = Regex.Replace(strOutput, "\n+", "<br>")
              strOutput = Regex.Replace(strOutput, "\s+", " ")
              strOutput = Regex.Replace(strOutput, Chr(34), "")

            Catch pEx As Exception
              ' 
              ' Fatal error occured, so return the error within the string output 
              ' 
              
        strOutput pEx.Message
            End 
        Try

            

            ' 
        Return the output
            

            Return Regex.Split(strOutput, "<br>")

          End Function

        End Class 
        Regards,
        Ultrajones
        Last edited by Ultrajones; April 16, 2009, 06:35 PM.
        Plug-ins: UltraMon, UltraM1G, UltraCID, Ultra1Wire, UltraLog, UltraWeatherBug, UltraPioneerAVR, UltraGCIR

        Comment


          #5
          Hi, thanks for sharing. I can't get it work from homeseer. I save the lines to command.vb and tried to run it from event run script but got errors.
          PHP Code:
          17/04/2009 21:55:21  Error Script compile errorType 'Process' is not defined.on line 27 
          It's working perfectly in visual studio 2008, maybe I have make the vb.file wrong?
          </system.contextstaticattribute()></system.contextstaticattribute()></system.contextstaticattribute()>
          Please excuse any spelling and grammatical errors I may make.
          --
          Tasker Plugin / Speech Droid
          Tonlof | Sweden

          Comment


            #6
            Hi all,

            I'm trying to do something similar and also get the same error (along with another):

            Code:
            Jan-21 11:42:01 AM	 	Error	Compiling script c:\program files\homeseer hs3\scripts\sb_ambientlight.vb: 'ProcessWindowStyle' is not declared. It may be inaccessible due to its protection level.
            Jan-21 11:42:01 AM	 	Error	Compiling script c:\program files\homeseer hs3\scripts\sb_ambientlight.vb: Type 'Process' is not defined
            Anyone know what needs to be adjusted in the above script to make it work?

            Thanks
            Al
            HS 4.2.8.0: 2134 Devices 1252 Events
            Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

            Comment


              #7
              Try replacing ProcessWindowStyle.Hidden with the value of 1
              or add System.Diagnostics.ProcessWindowStyle.Hidden which will require you to import System.Diagnostics
              💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

              Comment


                #8
                Originally posted by Rupp View Post
                Try replacing ProcessWindowStyle.Hidden with the value of 1
                or add System.Diagnostics.ProcessWindowStyle.Hidden which will require you to import System.Diagnostics
                Thanks Greg,

                That pointed me in the right direction. I added

                Code:
                Imports System.Diagnostics
                Imports System.ComponentModel
                and now have it working.

                Cheers
                Al
                HS 4.2.8.0: 2134 Devices 1252 Events
                Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                Comment


                  #9
                  Thanks to UJ for posting the original script and Rupp's help to get it working, below is an updated script I have which can read the output of an executable passed in as a parameter and store it in a HS device. It's configured to store a value, not a string, but could easily be modified to store a string. The script requires 4 parameters to be passed to it which is further explained in the script itself.

                  Cheers
                  Al

                  Code:
                  Sub Main(ByVal Parms As String)
                  
                  	Dim logName As String = "App Device Update"		'set log type for HS log
                  	Dim Debug As Boolean = False				'set to True if the script give you errors and it will provide additional info in the HS3 log to help troubleshoot
                  	Dim ParmArray() as String
                  	Try 
                  		ParmArray = Parms.ToString.split("|")		'split parameter into an array
                  		Dim App = ParmArray(0)				'app to run
                  		Dim AppPath = ParmArray(1)				'path to app
                  		Dim AppParam = ParmArray(2)			'parameters to run the app with
                  		Dim devID = CInt(ParmArray(3))			'device ID to update with the value returned by the command.  The value returned must be numeric.  The script will need to be modified to work with other types of return
                  		If Right(AppPath,1) <> "\" Then AppPath = AppPath & "\"
                  
                  		If Debug Then hs.writelog(logName,AppPath & App & " " & AppParam)
                  		
                  		Dim MyCommand As New CmdShell 
                  		Dim Results() As String 
                  
                  		Results = MyCommand.DoCommand(AppPath & App, AppParam, AppPath) 
                  		For Each Line As String In Results 
                  			If IsNumeric(Line) Then
                  				If Debug Then hs.WriteLog(logName, devID & ":" & Line) 
                  				hs.SetDeviceValueByRef(devID, CDbl(Line), True)
                  			ElseIf Line <> "" Then
                  				If Debug Then hs.WriteLog(logName, Line) 
                  			End If
                  		Next  
                  	Catch Ex As Exception 
                  		hs.writelog(logName,Ex)
                  	End Try 
                  
                  End Sub
                  
                  
                  ' 
                  ' Routines To Execute And Capture Program Output 
                  ' 
                  Imports System.IO 
                  Imports System.Diagnostics
                  Imports System.ComponentModel
                  Imports System.Text.RegularExpressions 
                  
                  Public Class CmdShell 
                  
                    '-------------------------------------------------------------------------------  
                    ' Purpose:   Calls command line utility and returns results as String() 
                    ' Input:     strProgram as String, strArgs as String, strWorkingDirectory as String  
                    ' Output:    String() as String 
                    '-------------------------------------------------------------------------------  
                    Function DoCommand(ByVal strProgram As String, _ 
                                       ByVal strArgs As String, _ 
                                       ByVal strWorkingDirectory As String) As String() 
                  
                  	Dim strOutput As String = "" 
                  
                      Try 
                        Dim myproc As New Process
                        With myproc 
                          .StartInfo.FileName = strProgram 
                          .StartInfo.Arguments = strArgs 
                          .StartInfo.RedirectStandardOutput = True 
                          .StartInfo.RedirectStandardInput = True 
                          .StartInfo.RedirectStandardError = True 
                          .StartInfo.UseShellExecute = False 
                          .StartInfo.CreateNoWindow = False
                          .StartInfo.WorkingDirectory = strWorkingDirectory 
                          .Start() 
                        End With 
                  
                        Dim StdOut As StreamReader = myproc.StandardOutput 
                        strOutput = StdOut.ReadToEnd() 
                  
                        Dim StdErr As StreamReader = myproc.StandardError 
                        strOutput = StdErr.ReadToEnd() & strOutput 
                  
                        myproc.WaitForExit() 
                  
                        '  
                        ' Prepare string for parsing  
                        '  
                        strOutput = Regex.Replace(strOutput, "\n+", "<br>") 
                        strOutput = Regex.Replace(strOutput, "\s+", " ") 
                        strOutput = Regex.Replace(strOutput, Chr(34), "") 
                  
                  
                      Catch pEx As Exception 
                        '  
                        ' Fatal error occured, so return the error within the string output  
                        '  
                        strOutput = pEx.Message 
                      End Try 
                  
                      '  
                      ' Return the output 
                      '  
                      Return Regex.Split(strOutput, "<br>") 
                  
                  	End Function 
                  
                  End Class
                  HS 4.2.8.0: 2134 Devices 1252 Events
                  Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                  Comment

                  Working...
                  X