Announcement

Collapse
No announcement yet.

Very basic C# script help needed.

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

    Very basic C# script help needed.

    Would someone please show me how to modify the script below so the value of "n" in the while (i < n) loop and the integer value "dvRef" can be passed as parameters?

    public object Main(object[] Parms)
    {
    int i = 0;
    //int dvRef = hs.GetDeviceRefByName("Living Room Lamp");
    do
    {
    hs.CAPIControlHandler(hs.CAPIGetSingleControl(dvRef, false, "On", false, false));
    hs.WaitSecs(1.5);
    hs.CAPIControlHandler(hs.CAPIGetSingleControl(dvRef, false, "Off", false, false));
    i++;
    }
    while (i < n);
    return(0);

    Any help would be greatly appreciated. Thanks in advance!

    #2
    Originally posted by avpman View Post
    Would someone please show me how to modify the script below so the value of "n" in the while (i < n) loop and the integer value "dvRef" can be passed as parameters?
    You do not say where and how you are calling this procedure. Homeseer will pass parameters differently when being called from an event vs being called from a tablet running Touch. (If its called directly from within script, then you define how the parameters a passed.) Homeseer does not pass an array of objects. An event passes a single string. Touch however, passes an array of strings. You generally define the input parameter as an object, since an object will hold either a string or an array of strings.

    So when you want to pass multiple parameters in an event call, using a single string, simply add a separator character, then in the script, do a split on that separator character. If you want to be able to call that function from an event or touch, then in script you need to check the Object type and act accordingly.

    Here is a function I call in my scripts to parse the input parameters into an array, and works when called from either side. Its written in VB, but should be easy to convert.

    Code:
    Private Function TouchParseStr(ByVal parms As Object, ByVal sSep As String, Optional sDef As String = "") As String()
    ' Takes the input parameter, and returns an array of strings.
    ' If called from event, parms is a single string, so does a split on the specified delimiter.
    ' If called from HS Touch, just returns the string array.
    ' Usage: Sub Main(parms As Obect)
    ' Dim p() As String = TouchParse(parms, ",") ' p(0) now contains first, p(1) second, etc
      Dim buff() As String
      If parms Is Nothing Then parms = ""
      If parms.GetType().ToString = "System.String" Then
        If parms = "" Then parms = sDef
        parms = Trim(parms.ToString())
        buff = parms.Split(sSep)
      Else
        buff = parms
      End If
      Return buff
    End Function

    Comment


      #3
      Originally posted by aa6vh View Post

      You do not say where and how you are calling this procedure. Homeseer will pass parameters differently when being called from an event vs being called from a tablet running Touch. (If its called directly from within script, then you define how the parameters a passed.) Homeseer does not pass an array of objects. An event passes a single string. Touch however, passes an array of strings. You generally define the input parameter as an object, since an object will hold either a string or an array of strings.

      So when you want to pass multiple parameters in an event call, using a single string, simply add a separator character, then in the script, do a split on that separator character. If you want to be able to call that function from an event or touch, then in script you need to check the Object type and act accordingly.

      Here is a function I call in my scripts to parse the input parameters into an array, and works when called from either side. Its written in VB, but should be easy to convert.

      Code:
      Private Function TouchParseStr(ByVal parms As Object, ByVal sSep As String, Optional sDef As String = "") As String()
      ' Takes the input parameter, and returns an array of strings.
      ' If called from event, parms is a single string, so does a split on the specified delimiter.
      ' If called from HS Touch, just returns the string array.
      ' Usage: Sub Main(parms As Obect)
      ' Dim p() As String = TouchParse(parms, ",") ' p(0) now contains first, p(1) second, etc
      Dim buff() As String
      If parms Is Nothing Then parms = ""
      If parms.GetType().ToString = "System.String" Then
      If parms = "" Then parms = sDefi,n
      parms = Trim(parms.ToString())
      buff = parms.Split(sSep)
      Else
      buff = parms
      End If
      Return buff
      End Function
      I am calling the script from an event. So I would need to pass param as a string values I,n and then split the string and convert to int for use in the script? I know how to do this in VB, but not c#. That's my dilemma.

      Comment


        #4
        Originally posted by avpman View Post

        I am calling the script from an event. So I would need to pass param as a string values I,n and then split the string and convert to int for use in the script? I know how to do this in VB, but not c#. That's my dilemma.
        You would pass a single string containing something like "123,456".

        Then you would have inside your function:

        string parm = parms.ToString();
        string[] buff = parm.Split(',');

        So buff[0] = "123", and buff[1] "456". Now convert those strings into integers.

        My C# is rusty, so excuse any syntax errors.

        Comment


          #5
          Originally posted by aa6vh View Post

          You would pass a single string containing something like "123,456".

          Then you would have inside your function:

          string parm = parms.ToString();
          string[] buff = parm.Split(',');

          So buff[0] = "123", and buff[1] "456". Now convert those strings into integers.

          My C# is rusty, so excuse any syntax errors.
          - Thanks!
          Stay safe...

          Comment


            #6
            Originally posted by aa6vh View Post

            You would pass a single string containing something like "123,456".

            Then you would have inside your function:

            string parm = parms.ToString();
            string[] buff = parm.Split(',');

            So buff[0] = "123", and buff[1] "456". Now convert those strings into integers.

            My C# is rusty, so excuse any syntax errors.
            I still can't get this to work. Is there someplace I can look at sample HS C# scripts? Especially what is the first line of the script that accepts the params? I have tried and I still can't split the params. Right now I'm just trying to get as far as splitting the params and writing them to the logfile for testing.

            public object Main(object[] Parms)
            public object Main(String n)
            public void Main(object parms)

            Any help would be appreciated!

            Comment


              #7
              Originally posted by avpman View Post

              I still can't get this to work. Is there someplace I can look at sample HS C# scripts? Especially what is the first line of the script that accepts the params? I have tried and I still can't split the params. Right now I'm just trying to get as far as splitting the params and writing them to the logfile for testing.

              public object Main(object[] Parms)
              public object Main(String n)
              public void Main(object parms)

              Any help would be appreciated!
              Did you try exactly the code I gave you? Never mind if it is not exactly what you need, try it and get it to work.

              The declare for Main should indicate that it does not return anything, and the input parameter should be "object parms".


              public void Main(object parms) {


              Sorry but I do not write in Homeseer with C#, so do not have anything to show in regards to that language and HS. I choose to write in VB because of a lack to examples in C#. There is little advantage of one language over the other.

              Comment


                #8
                Originally posted by aa6vh View Post

                Did you try exactly the code I gave you? Never mind if it is not exactly what you need, try it and get it to work.

                The declare for Main should indicate that it does not return anything, and the input parameter should be "object parms".


                public void Main(object parms) {


                Sorry but I do not write in Homeseer with C#, so do not have anything to show in regards to that language and HS. I choose to write in VB because of a lack to examples in C#. There is little advantage of one language over the other.
                Yup, this is what I wrote:
                public void Main(object parms)
                {
                string parm = parms.ToString();
                string[] buff = parm.Split(',');

                hs.WriteLog("CSHARP","From C# script");
                hs.WriteLog("parm 1 is ",buff[0]);
                hs.WriteLog("parm 2 is ",buff[1]);

                }

                And this is the error generated:
                3/21/2021 4:27:28 PM HomeSeer Error 1 Running script more info: Index was outside the bounds of the array.
                3/21/2021 4:27:28 PM HomeSeer Error 1 Running script param.cs :Exception has been thrown by the target of an invocation.
                3/21/2021 4:27:28 PM Script parm 1 is System.Object[]
                3/21/2021 4:27:28 PM Script CSHARP From C# script
                3/21/2021 4:27:28 PM HomeSeer Event Running script in background (Script onoff): param.cs

                Comment


                  #9
                  Originally posted by avpman View Post

                  Yup, this is what I wrote:
                  public void Main(object parms)
                  {
                  string parm = parms.ToString();
                  string[] buff = parm.Split(',');

                  hs.WriteLog("CSHARP","From C# script");
                  hs.WriteLog("parm 1 is ",buff[0]);
                  hs.WriteLog("parm 2 is ",buff[1]);

                  }

                  And this is the error generated:
                  3/21/2021 4:27:28 PM HomeSeer Error 1 Running script more info: Index was outside the bounds of the array.
                  3/21/2021 4:27:28 PM HomeSeer Error 1 Running script param.cs :Exception has been thrown by the target of an invocation.
                  3/21/2021 4:27:28 PM Script parm 1 is System.Object[]
                  3/21/2021 4:27:28 PM Script CSHARP From C# script
                  3/21/2021 4:27:28 PM HomeSeer Event Running script in background (Script onoff): param.cs
                  I just commented out the index buff[1] and left buff[0] and this is what now shows in the log:
                  3/21/2021 4:37:46 PM Script parm 1 is System.Object[]
                  3/21/2021 4:37:46 PM Script CSHARP From C# script

                  It appears that the param object isn't being converted to a string and split. I even tried a different param delimiter "|" instead of a comma in the params sent to the script and in the script.

                  I originally wrote this in VB, but I have noticed that since this is meant to be a loop, C# seems faster and more reliable. I found this out by hardcoding the scripts params. I'm thoroughly confused...

                  Comment


                    #10
                    Originally posted by avpman View Post
                    I originally wrote this in VB, but I have noticed that since this is meant to be a loop, C# seems faster and more reliable. I found this out by hardcoding the scripts params. I'm thoroughly confused...
                    May I respectively suggest going back to VB. You will find a lot more examples (and help), You will have less frustration further down the line. You will be a happier camper.

                    Cannot comment on your speed of execution. I really doubt it matters that much though.

                    Comment


                      #11
                      Originally posted by aa6vh View Post

                      May I respectively suggest going back to VB. You will find a lot more examples (and help), You will have less frustration further down the line. You will be a happier camper.

                      Cannot comment on your speed of execution. I really doubt it matters that much though.
                      I believe the issue was that waitTimer worked better with C#. The script needs to loop x times, run a command, and wait y seconds before running the next loop. I'll try it again in VB 👍. Thank you for your help!

                      Comment


                        #12
                        Could you also supply the call to this script because the error suggests that the buff array has no elements in them
                        - Bram

                        Send from my Commodore VIC-20

                        Ashai_Rey____________________________________________________________ ________________
                        HS3 Pro 3.0.0.534
                        PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

                        Comment


                          #13
                          Originally posted by AshaiRey View Post
                          Could you also supply the call to this script because the error suggests that the buff array has no elements in them
                          Hi,
                          Thanks for your time. I'm calling it from an event: (p.s. the artifact between the "1,250" param is my cursor)

                          Click image for larger version  Name:	script.JPG Views:	0 Size:	29.5 KB ID:	1464671
                          Last edited by avpman; March 22, 2021, 07:29 AM. Reason: explained artifact between params

                          Comment


                            #14
                            Originally posted by avpman View Post

                            Hi,
                            Thanks for your time. I'm calling it from an event: (p.s. the artifact between the "1,250" param is my cursor)

                            Click image for larger version Name:	script.JPG Views:	0 Size:	29.5 KB ID:	1464671
                            I FOUND IT! I left out "Main" as the sub or func to be called. "Where are you calling it from..." jogged a thought. Thanks again for your time!

                            Comment

                            Working...
                            X