Announcement

Collapse
No announcement yet.

Execute C# scripts without an Event

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

    Execute C# scripts without an Event

    A month or two ago I asked if anybody knew how to execute c# scripts from the command line without using Events. I did not get an answer so I asked the support desk. The response from support is "we do not assist with scripting on the helpdesk." (Never did figure it out.)

    So a different question on the same subject; is it possible to execute a c# script using runscript.html by entering the script name and method? From all the commands I've tried, it appears only usable for hs. commands.

    #2
    Can you describe the scenario where you want to execute a script without any kind of trigger? If you need something that runs periodically you can have an event trigger itself. If you need something at startup or shutdown of HS4 then you can add your script to the appropriate startup/shutdown script. If you have some external non-HS trigger you can look into integrating some "HS compatible" device that is being triggered by such an external non-HS trigger.

    Comment


      #3
      Aside from testing a script while coding directly on the device, I have custom webpages that need current data when the page opens. The pages get their data from .cs scripts. My current method requires creating an Event then triggering the event inside the webpage with the following:

      Code:
          <script>
            async function hideLink1() {
              var myFrame1 = document.createElement("iframe"); //Creating iFrame
              myFrame1.setAttribute("src", "http://192.168.xxx.xxx/JSON?request=runevent&id=77"); // Setting iFrame's source
              myFrame1.style.display = "none"; //Hiding iFrame
              document.body.appendChild(myFrame1); //Adding iFrame
              await sleep(1000); //Giving the web page time to load
              document.body.removeChild(myFrame1); //Removing iFrame
            }
          </script>​
      ​
      Seems like there should be a better way instead of having a to create an Event for each page that is only useful to populate the page with information created by the script.

      Comment


        #4
        A tablet running HS Touch can run scripts directly without any intervening event.

        And if you are real clever, you could have a script that runs when a virtual device has its device string changed, and that script will use the new name to run a script file with that name.

        Comment


          #5
          Originally posted by aa6vh View Post
          A tablet running HS Touch can run scripts directly without any intervening event.

          And if you are real clever, you could have a script that runs when a virtual device has its device string changed, and that script will use the new name to run a script file with that name.
          Creating a virtual device is more complex than a very simple Event and since I'm not using a tablet I'm guessing HS Touch is not a solution for use while I'm on the device.

          Both are nice ideas, but does not solve the issue at hand which is being able to directly reference a c# script for execution. But thank you for offering some ideas that could be useful in other scenarios.

          Comment


            #6
            I do have a solution for my database parser report... since the script does not access any hs. functions I'm able to package it as an .exe and run it like any other executable but that is not an option for either my Event or Device reports.

            Both of my Event and Device reports use hs functions and I have not figured out a way to get Visual Studio to convert those scripts to an .exe. For the Event or Device reports the workaround is to create the Events but it seems like HS should have some method to make a direct call to C# scripts.

            Comment


              #7
              HS4 Tools/Script Commands provides an interactive method to runs scripts. It can also be done on mcsMQTT Plugin Interactive Page that adds capability of using the replacement variables and expressions.

              Comment


                #8
                Originally posted by Michael McSharry View Post
                HS4 Tools/Script Commands provides an interactive method to runs scripts. It can also be done on mcsMQTT Plugin Interactive Page that adds capability of using the replacement variables and expressions.
                I've tried every way I can think of using runscript.html​ without success. Can you give me an idea of how I would run a script named csEventReports.cs with the public object csGetAllEventsReports. Thanks.

                Comment


                  #9
                  Sorry, but my experience is only with Visual Basic scripts. There may be others that have used HS for awhile and user C# on a regular basis that can help. I believe C# scripts only became available with HS version 3.

                  Comment


                    #10
                    You can run a script on any HS4 html page using modified jQuery code in the runscript.html page.

                    Just use the following code in the head of your own page:

                    Code:
                    <head>
                    {{includefile 'bootstrap/js/page_common.js'}}
                    <script>
                    let scriptname = 'test.vb';
                    let fucn = 'Main';
                    let param = 'hello';
                    $(document).ready(function(){
                        AjaxPost("action=run_script_command&scriptcommand=%26hs.runscriptfunc(%22" + scriptname + "%22%2C%22" + fucn + "%22%2C%22" + param + "%22%2CFalse%2CFalse)","runscript.html");
                    });
                    </script>
                    </head>
                    Obviously set the scriptname, function and parm as necessary. Should run vb or C# scripts.

                    This will run the script as soon as the page loads.

                    Jon

                    Comment


                      #11
                      Originally posted by jon00 View Post
                      You can run a script on any HS4 html page using modified jQuery code in the runscript.html page.

                      Just use the following code in the head of your own page:

                      Code:
                      <head>
                      {{includefile 'bootstrap/js/page_common.js'}}
                      <script>
                      let scriptname = 'test.vb';
                      let fucn = 'Main';
                      let param = 'hello';
                      $(document).ready(function(){
                      AjaxPost("action=run_script_command&scriptcommand=%26hs.runscriptfunc(%22" + scriptname + "%22%2C%22" + fucn + "%22%2C%22" + param + "%22%2CFalse%2CFalse)","runscript.html");
                      });
                      </script>
                      </head>
                      Obviously set the scriptname, function and parm as necessary. Should run vb or C# scripts.

                      This will run the script as soon as the page loads.
                      Thanks Jon. That looks similar to what I tried in the past but I could not get it to work. I don't remember my AjaxPost statement looking like what you posted. To me it seems like the C# compiler is built into HS and the OS does not see it. I will dissect what you posted and see what I can do.

                      Thanks!

                      Comment


                        #12
                        Success!!! Big shout out to Jon!

                        Still reading up on hs.runscript and hs.runscriptfunc. I was definitely skewed on my previous attempts. Thank you for helping me resolve this issue that was plaguing me for months.

                        Comment

                        Working...
                        X