Announcement

Collapse
No announcement yet.

Help converting C# to VB.NET for InitialState.com graphing

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

    Help converting C# to VB.NET for InitialState.com graphing

    I'm an old C programmer mostly programming embedded devices with very limited resources.

    I have a very simple script that works just fine in C#.
    But unfortunately HS3 has a memory leak in C# and RJH strongly recommends converting the script to VB.NET.
    But I'm not much of a .NET programmer and there is virtually no error messages on what the script is failing on so I'm hoping someone out there can help me out and see the error in my ways...
    I even downloaded Visual Studio and pasted the script into there but the tool is way beyond my desire to learn yet another complex IDE so I am reaching out to you instead... TIA.

    The C# script which works but eventually causes HS3 to crash due to the memory leak:
    Code:
    public object Main(object[] Parms) {
    string parms = (string)Parms[0]; // get the passed parameter seperated by a comma
    string P1 = parms.Split(',')[0]; // get the Device ID#
    string P2 = parms.Split(',')[1]; // get the label for the device
    string P3 = parms.Split(',')[2]; // get the InitialStateBucket
    double frtemp;
    int DeviceRef=Convert.ToInt32(P1);
    frtemp=hs.DeviceValueEx(DeviceRef);
    string GrURL = "https://groker.initialstate.com/api/events?accessKey=PutYourKeyHere&bucketKey=" + P3 + "&"+ P2 + "=" + frtemp.ToString();
    hs.URLAction(GrURL,"POST", "","");
    return 0;
    }
    The VB.NET version I tried to convert it into is:
    Code:
    Public Function Main(Parms__1 As Object()) As Object
    	Dim parms__2 As String = DirectCast(Parms__1(0), String)
    	' get the passed parameter seperated by a comma
    	Dim P1 As String = parms__2.Split(","C)(0)
    	' get the Device ID#
    	Dim P2 As String = parms__2.Split(","C)(1)
    	' get the label for the device
    	Dim P3 As String = parms__2.Split(","C)(2)
    	' get the InitialStateBucket
    	Dim frtemp As Double
    	Dim DeviceRef As Integer = Convert.ToInt32(P1)
    	frtemp = hs.DeviceValueEx(DeviceRef)
    	Dim GrURL As String = (Convert.ToString((Convert.ToString("https://groker.initialstate.com/api/events?accessKey=PutYourKeyHere&bucketKey=") & P3) + "&") & P2) + "=" + frtemp.ToString()
    	hs.URLAction(GrURL, "POST", "", "")
    	Return 0
    End Function
    When the VB.NET script runs I get:
    VB.Net script exception(0), re-starting: Object reference not set to an instance of an object

    a line number would help just a wee little bit or maybe the failing object name???

    #2
    See if you get an error message in the log running this:

    PHP Code:
        Public Function Main(Parms__1 As Object) As Object
            
    Try
                
    Dim parms__2 As String DirectCast(Parms__1(0), String)
                
    ' get the passed parameter seperated by a comma
                Dim P1 As String = parms__2.Split(","c)(0)
                ' 
    get the Device ID#
                
    Dim P2 As String parms__2.Split(","c)(1)
                
    ' get the label for the device
                Dim P3 As String = parms__2.Split(","c)(2)
                ' 
    get the InitialStateBucket
                Dim frtemp 
    As Double
                Dim DeviceRef 
    As Integer Convert.ToInt32(P1)
                
    frtemp hs.DeviceValueEx(DeviceRef)
                
    Dim GrURL As String "https://groker.initialstate.com/api/events?accessKey=PutYourKeyHere&bucketKey=" P3 "&" P2 "=" frtemp.ToString
                hs
    .URLAction(GrURL"POST""""")
            Catch 
    Ex As Exception
                hs
    .WriteLog("Error"Ex.ToString)
            
    End Try
            Return 
    0
        End 
    Function 
    Jon

    Comment


      #3
      Thanks Jon!

      I get:

      Mar-11 10:35:37 Error System.InvalidCastException: Cannot cast from source type to destination type. at scriptcode3.VBWrapper.Main (System.Object Parms__1) [0x00000] in :0
      Mar-11 10:35:31 Event Running script in background: /usr/local/HomeSeer/scripts/InitialState.vb

      So it would seem that the DirectCast doesn't work so I tried changing it to CType and now I get:
      Mar-11 10:48:23 Error System.IndexOutOfRangeException: Array index is out of range. at scriptcode4.VBWrapper.Main (System.Object Parms__1) [0x00000] in :0

      HELP! I have no idea what this means...

      Comment


        #4
        I don't know what you are sending to the function, however does this work?

        PHP Code:
            Public Function Main(Parms__1 As Object) As Object
                
        Try
                    
        Dim P1 As String Parms__1.Split(","c)(0)
                    
        ' get the Device ID#
                    Dim P2 As String = Parms__1.Split(","c)(1)
                    ' 
        get the label for the device
                    Dim P3 
        As String Parms__1.Split(","c)(2)
                    
        ' get the InitialStateBucket
        .... 
        Jon

        Comment


          #5
          Here's a simplified version that should do what you need:

          Code:
          Sub Main(ByVal Parms as String)
           
          	Dim P() as String
          	P = Split(Parms,",")
          	Dim DeviceRef As Integer = Convert.ToInt32(P(0))
          	Dim frtemp As Double = hs.DeviceValueEx(DeviceRef)
          	Dim GrURL As String = "https://groker.initialstate.com/api/events?accessKey=PutYourKeyHere&bucketKey=" & P(2) & "&" & P(1) & "=" & frtemp.ToString()
          	hs.writelog("Test",GrURL)
          	hs.URLAction(GrURL, "POST", "", "")
          
          End Sub
          Cheers
          Al
          HS 4.2.8.0: 2134 Devices 1252 Events
          Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

          Comment


            #6
            Sparkman! IT WORKS! Thanks!

            Now - the next optimization is to use VB.NET built in functions instead of hs.URLAction. The URLAction was on occasion timing out and RJH mentioned that there are much faster/better built in functions in .NET.

            Does anyone know the VB.NET function that does the same thing as URLAction?

            Comment


              #7
              Originally posted by DrZWave View Post
              Sparkman! IT WORKS! Thanks!

              Now - the next optimization is to use VB.NET built in functions instead of hs.URLAction. The URLAction was on occasion timing out and RJH mentioned that there are much faster/better built in functions in .NET.

              Does anyone know the VB.NET function that does the same thing as URLAction?
              Great, glad that worked for you. Take a look at https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx. It's a bit more convoluted doing it that way. You will likely need to add this line above the Sub Main line for those functions to work:

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

              Comment

              Working...
              X