Announcement

Collapse
No announcement yet.

Error on Script :(

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

    Error on Script :(

    Hi Guys,

    I have some code below which keeps triggering an error, I have done a debug using tenscripting and it did not report any error, I initially though the error might have been related to the fact that I was using arrays but this does not appear to be the case either.

    I know it is hard without the appropiate file (which if needed I can send) but can anyone take a glance over the code I have been working on and let me know if anything stands out which might be causing the error below?

    Thanks!


    *ERROR*
    PHP Code:
    Scripting runtime errorSystem.Reflection.TargetInvocationExceptionException has been thrown by the target of an invocation. --- System.ArgumentOutOfRangeExceptionIndex and length must refer to a location within the string.Parameter namelength at System.String.InternalSubStringWithChecks(Int32 startIndexInt32 lengthBoolean fAlwaysCopyat System.String.Substring(Int32 startIndexInt32 lengthat scriptcode52.scriptcode52.ApplianceLookup(String Appliancesat scriptcode52.scriptcode52.Main(Object Parms) --- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeMethodFast(Object targetObject[] argumentsSignatureStructsigMethodAttributes methodAttributesRuntimeTypeHandle typeOwnerat System.RuntimeMethodHandle.InvokeMethodFast(Object targetObject[] argumentsSignature sigMethodAttributes methodAttributesRuntimeTypeHandle typeOwnerat System.Reflection.RuntimeMethodInfo.Invoke(Object objBindingFlags invokeAttrBinder binderObject[] parametersCultureInfo cultureBoolean skipVisibilityChecksat System.Reflection.RuntimeMethodInfo.Invoke(Object objBindingFlags invokeAttrBinder binderObject[] parametersCultureInfo cultureat System.Reflection.MethodBase.Invoke(Object objObject[] parametersat Scheduler.VsaScriptHost.Invoke(String ModuleNameString MethodNameObject[] Arguments

    *** CODE ***
    Code:
    Imports System.IO
    
    Public Sub Main(Byval Parms as Object)
    hs.Writelog("Appliance Test",ApplianceLookup("HOPUT1 NAWET1"))
    End Sub
    
    Public Function ApplianceLookup(ByVal Appliances As String)
     Dim SplitStr() As String
            Dim ResultToReturn As String
            SplitStr = Split(Appliances," ")
            Dim s As StreamReader = New StreamReader("C:\Homeseer HSPRO\html\CFA_Paging\pager.csv")
            Dim ApplianceAbbreviation As String
            Dim brigade As String
            Dim BrigadeAbbrevition As String
            Dim Filename As String
            Dim ApplianceType As String
            Dim MyData As String
            Dim i As Integer
    
    for each ApplianceListed as String in SplitStr        
                ApplianceAbbreviation = ApplianceListed.Substring(3, 5)
                BrigadeAbbrevition = ApplianceAbbreviation = ApplianceListed.Substring(0, 3)
                hs.WriteLog("Appliances - Brigade", BrigadeAbbrevition)
                hs.WriteLog("Appliances", ApplianceAbbreviation)
    
                Do
                    MyData = s.ReadLine()
                    If MyData.Contains(BrigadeAbbrevition) Then
                        brigade = hs.StringItem(MyData, 2, ",").Replace(Chr(34), "")
                        Select Case ApplianceAbbreviation
                            Case "AP"
                                ApplianceType = "Aerial Pumper"
                            Case "AC"
                                ApplianceType = "Aircraft"
                            Case "BF"
                                ApplianceType = "Big Fill"
                            Case "V"
                                ApplianceType = "Boat / Vessel"
                            Case "BA"
                                ApplianceType = "Breathing Apparatus"
                            Case "BS"
                                ApplianceType = "Bus"
                            Case "CR"
                                ApplianceType = "Car"
                            Case "CR2"
                                ApplianceType = "Car 2"
                            Case "CR3"
                                ApplianceType = "Car 3"
                            Case "DMO"
                                ApplianceType = "DMO"
                            Case "FC"
                                ApplianceType = "Field Command Vehicle"
                            Case "F2"
                                ApplianceType = "Field Command Vehicle 2"
                            Case "F3"
                                ApplianceType = "Field Command Vehicle 3"
                            Case "FO"
                                ApplianceType = "Field Operations Vehicle"
                            Case "HM"
                                ApplianceType = "Hazmat"
                            Case "HD"
                                ApplianceType = "Hazmat Detection"
                            Case "HL"
                                ApplianceType = "Hose Layer"
                            Case "LP"
                                ApplianceType = "Ladder Platform"
                            Case "LT"
                                ApplianceType = "Lighting"
                            Case "CV"
                                ApplianceType = "Mobile Communications Vehicle"
                            Case "PE"
                                ApplianceType = "Protective Equipment"
                            Case "P1"
                                ApplianceType = "Pumper"
                            Case "P2"
                                ApplianceType = "Pumper 2"
                            Case "P3"
                                ApplianceType = "Pumper 3"
                            Case "PT"
                                ApplianceType = "Pumper/Tanker"
                            Case "QF"
                                ApplianceType = "Quick Fill"
                            Case "R"
                                ApplianceType = "Rescue"
                            Case "SV"
                                ApplianceType = "Salvage"
                            Case "S1"
                                ApplianceType = "Slip On"
                            Case "S2"
                                ApplianceType = "Slip On 2"
                            Case "S3"
                                ApplianceType = "Slip On 3"
                            Case "S4"
                                ApplianceType = "Slip On 4"
                            Case "SM"
                                ApplianceType = "Snow Mobile"
                            Case "SP"
                                ApplianceType = "Spare Vehicle"
                            Case "ST"
                                ApplianceType = "Staging Resource"
                            Case "SU"
                                ApplianceType = "Support"
                            Case "T1"
                                ApplianceType = "Tanker"
                            Case "T2"
                                ApplianceType = "Tanker 2"
                            Case "T3"
                                ApplianceType = "Tanker 3"
                            Case "T4"
                                ApplianceType = "Tanker 4"
                            Case "TB"
                                ApplianceType = "Teleboom"
                            Case "U1"
                                ApplianceType = "Ultra Light"
                            Case "U2"
                                ApplianceType = "Ultra Light 2"
                            Case "U3"
                                ApplianceType = "Ultra Light 3"
                            Case "U4"
                                ApplianceType = "Ultra Light 4"
                            Case Else
                                ApplianceType = "Unknown " & "(" & ApplianceAbbreviation & ")"
                        End Select
                        Exit Do
                    End If
                Loop Until MyData Is Nothing
                s.Close()
    
                If brigade <> "" Then
                    ResultToReturn = ResultToReturn & brigade & " " & ApplianceType & ", "
                Else
                    hs.WriteLog("Pager Messages", "Brigade Not Found")
                End If
            Next
            Return ResultToReturn
     End Function
    HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

    Facebook | Twitter | Flickr | Google+ | Website | YouTube

    #2
    Do you have any idea where the code breaks? You have a bunch of hs.writelog, does any of them return anything in your log or does it break before you get that far?

    The only thing I can see is that where you declare the sub function:

    Public Function ApplianceLookup(ByVal Appliances As String)

    I would have declared it slightly different, e.g. :

    Public Function ApplianceLookup(ByVal Appliances As String) as String

    To ensure the it is clear that the return is String, but I'm not sure if that is enough of an issue to cause the above to break???

    Also, maybe you can add the Try/Catch Exception pieces to your code? This might let you catch a more meaningful error message
    Nicolai L

    Comment


      #3
      I did have the try / exception parts of my code set up, I had removed them and now readded them.

      It appears to error in the function itself, when i added the function error handling I get the error below:

      PHP Code:
      11/2/2013 7:29:07 PM     Appliance Test     
      11
      /2/2013 7:29:07 PM     ERROR     Index and length must refer to a location within the string.Parameter namelength
      11
      /2/2013 7:29:07 PM     Length     2 
      The hs.Writelog parts are really more about debug than anything else, for example the length debug tells me the array has two members.
      HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

      Facebook | Twitter | Flickr | Google+ | Website | YouTube

      Comment


        #4
        Hi,
        This type of error is thrown in cases where you try to grab a piece of substring but there are no or not enough available characters to grab

        I know i sounds unclear but an example might help

        string = "123456"

        If you want to grab the string from the character 4 with a lenght of 5 characters then you get this error. It also happens when you try to do it on zero lenght strings. Putting a Try Catch block around it will help but you can also check beforehand if the piece you want to grab is there in position and lenght.
        - 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


          #5
          Thanks for your help, I will try and put some more error handling around the string. Basically what i was trying to do was to extract the first four characters and store to a variable, and then store the last two as another variable.

          So for example NAWE is variable 1, T1 is Variable 2
          HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

          Facebook | Twitter | Flickr | Google+ | Website | YouTube

          Comment


            #6
            So what you are saying is that you string must have a minimum lenght of 6 chars
            - 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


              #7
              Yep, So i am thinking it might be easiest just to do a check to see

              If ExampleString.Length > 6 then
              'do stuff
              else
              'error
              end if
              HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

              Facebook | Twitter | Flickr | Google+ | Website | YouTube

              Comment


                #8
                I also very much had a blonde moment.
                The error was exactally as you mentioned, I was using SubString as if Substring(WheretoStart,WhereToFinish) when all I actually wanted to so was Substring(WhereToStart,NumberOfDigitsToExtract)

                My Test Script works fine now.

                Thanks for the help!
                HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                Facebook | Twitter | Flickr | Google+ | Website | YouTube

                Comment


                  #9
                  Just for those that are interested, what the above script does is look at abbreviations in pager messages, in our system the first four letters are the abbreviation for the fire brigade, and the last two are the type of appliance (if there are 6 characters.

                  So for example when i put through HOPUT1 NAWET1 it works out by looking at a spreadsheet that this is for:

                  Hopetoun Tanker, Narre Warren East Tanker,

                  Thanks for the substring pointer!
                  HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                  Facebook | Twitter | Flickr | Google+ | Website | YouTube

                  Comment


                    #10
                    I'd be interested in why it appeared to be working in tenScripting but not when run under HS.

                    tenholde
                    tenholde

                    Comment


                      #11
                      I have to say, some of the things I have been coming across with this sort of stuff has been doing-my-head-in lol. Admittedly some of the strings are probably too variable so the script will struggle with it, but then some of the strings are just plain confusing.

                      For example, if it is a fire brigade it will be prefaced with a C, so for example CTOOM that is fine, i thought I would look for a C however there are some abbreviations that are CCOWL or similar so if it is a brigage it would show as CCOWL but if they wanted a specific vehicle it would show as COWLT1, I could look for specific phrases at the end of the string I guess, but then sometimes the string is even smaller than that so I am thinking it might not be the best way to go about it.

                      I was hopeful I might have been able to find a list, but no luck .

                      At the moment with my string, basically I ended up looping through each array member (split by space), looking at the length of the string, and if it is 6 chars then assume it is for a vehicle, if it is for 5 chars assume it is for a fire brigade, if anything else report unknown.
                      HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                      Facebook | Twitter | Flickr | Google+ | Website | YouTube

                      Comment


                        #12
                        I get the feeling that you are using a basic script at first and are adding more and more tasks to it. Like first build a shed and that's turned out fine. Next adding some extra levels and you now have a sky scrapper on the foundation of the shed... oef.

                        What i've seen so far of the script is rather complicated and not easiely maintainable in the future if something changes.

                        Do you have the specs of the strings you get in and do you know the meaning of every item? If so it might be better to start all over again with the lessons learned. A good developer isn't afraid to throw away some stuff and start over. I might be able to give you a hand it this if you have the specs
                        - 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
                          You are 100% correct, I probably need to go back to the foundations of the script. Initially the script did a simple thing (e.g. Write to a file) then I added bits and pieces. I moved parts to functions to try and make it easier however you are right that the foundation is a shed and then I have added another 10 stories on top of that.

                          Essentially most of the functions are extracting a certain piece from the string and then returning that for processing. Some of the information is an extracted address, other is the type of call, then after that it returns additional information (e.g. Fire as result of Accident). One of the last bits is what brigades / appliances are responding.

                          I could tie it all in the one script, but thought it would be a massive messy script.

                          I think it is almost time to start again with the lessons learnt. I do have specs for some parts but other parts there are not specific specs (e.g. the address is extremely variable as per my monster regex), It would be great if the string had markers in it (e.g. LOC before address) but alas that would be way way way to easy!

                          Thanks for the offer of the help!
                          HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                          Facebook | Twitter | Flickr | Google+ | Website | YouTube

                          Comment


                            #14
                            It is more a general question than specific to my script e.g. how do I do xyz, but if I were to redo the script (which I am seriously thinking) what is the best way to create the script by way of structure to minimize complication, and make it more easily maintainable.

                            Am I right in thinking that functions are the best way to allow for modifications in the script in the future, or is there a better way to handle that.

                            I was also thinking about the things in my script that may need fine tuning (e.g. Some of the Regex stuff), is this something that is worth me putting in config files so I can modify it without having to change the script itself?

                            Id appreciate your tips in terms of how to best code for the future, to minimize complexity (as far as possible), to allow for growth and modification.

                            I also thought given I use an access database, but have a web server (with MySQL) that it may be worth me moving the database across to the MySQL Server, so that the script in homeseer still handles getting the appropriate information, extracting and parsing it then storing it in a database. Doing it that way I can access the data either through the web server, or through Homeseer itself.
                            HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

                            Facebook | Twitter | Flickr | Google+ | Website | YouTube

                            Comment


                              #15
                              Originally posted by travisdh View Post
                              It is more a general question than specific to my script e.g. how do I do xyz, but if I were to redo the script (which I am seriously thinking) what is the best way to create the script by way of structure to minimize complication, and make it more easily maintainable.

                              Am I right in thinking that functions are the best way to allow for modifications in the script in the future, or is there a better way to handle that.

                              I was also thinking about the things in my script that may need fine tuning (e.g. Some of the Regex stuff), is this something that is worth me putting in config files so I can modify it without having to change the script itself?

                              Id appreciate your tips in terms of how to best code for the future, to minimize complexity (as far as possible), to allow for growth and modification.

                              I also thought given I use an access database, but have a web server (with MySQL) that it may be worth me moving the database across to the MySQL Server, so that the script in homeseer still handles getting the appropriate information, extracting and parsing it then storing it in a database. Doing it that way I can access the data either through the web server, or through Homeseer itself.
                              You got anyone on the inside of the IT services of your brigade? Wondering whether you can either get a hold of any sort of programming information for the system itself, there may be some unknown structure to the messages that might be much more easier to decode if the system had some sort of formatting which you may just be missing at the minute (that is unless you know exactly what it is doing?). If not maybe not would they consider making any sort of amendments? I see your posts and think it must be like pushing water uphill with the constant variations of the information. I've seen some turnout sheets from the fire service here that look much more manageable to decode (I know roughly what it means but it is not my emergency service!) like this;

                              Code:
                              MOBILISE MOBILISE MOBILISE 
                              INC. 53258081 TOC 14:37:40 TEL. 016898583XX 
                              LIGHT AIRCRAFT CRASHED ,E3 @ BROADWATER GARDENS FARNBOROUGH ORPINGTON KENT BR6
                              77.RC/150 E41 TQ 44147 64896
                              E391 @ E39 AS PA, 135, RTA, WMR, RT E401 @ E40 AS PA, 135, RTA E392 @ E43 AS PA E321 @ E32 AS PA H281 @ H28 AS PA E301 @ E30 AS PA E291 @ E29 AS USRS 
                              E286 @ E28 AS FRU, USRC SR15 @ A34 AS USR E216 @ E21 AS USRC H276 @ H27 AS USRC H429 @ H42 AS HLR CU4 X PAG @ E21 AS CU F437 @ F43 AS BFU, PM CARRYING BFU2 M29 @ E21 AS USM1, PMU CARRYING S151 USAR MODULE 1 M21 @ H31 AS USM4, PMU CARRYING S044 USAR MODULE 4 O20P X PAG @ O20 AS MRU 
                              E169 X PAG @ E28 AS GM E29 X PAG @ E41 AS SM OE66 X PAG @ E39 AS SM G88 X PAG @ O20 AS HZ E113 X PAG @ O20 AS UA OF77 X PAG @ O20 AS PR 
                              STREET INFO: RISK: CRR LHY END MESSAGE ~~
                              Which providing it was formatted always the same looks fairly easy to break down. Perhaps you could recommend something similar to help you and help others?

                              Comment

                              Working...
                              X