Announcement

Collapse
No announcement yet.

Read Data in from log file

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Read Data in from log file


    #2
    Code:
    Sub Main(ByVal Parms As Object)
    
    Dim str As String
    Dim FileName As string = "C:\" & DateTime.Now.ToString("yyyy-MM-dd") & ".log"
    
    If System.IO.File.Exists(FileName) Then
    
    str = My.Computer.FileSystem.ReadAllText(FileName)
    Dim LastLine As String
    Dim strSplit() As String
    Dim devSplit() As String
    Dim startDevice As Integer = 60
    Dim HCode As String = "S"
    
    strSplit = split(str, vbNewLine)
    
    LastLine = strSplit(UBound(strSplit))
    
    'hs.writelog("LastLine", LastLine)
    
    If IsNumeric(Left(LastLine, 1)) Then
    
    LastLine = Right(LastLine, Len(LastLine) - Instr(LastLine, ";D;") - 2)
    
    'hs.writelog("", LastLine)
    
    devSplit = split(LastLine, ";")
    
    For i As Integer = 0 to Ubound(devSplit)
    
    If devSplit(i) <> "" Then
    
    hs.writelog("Device Update", HCode & (startDevice + i) & " - " & devSplit(i))
    
    End If
    
    Next
    
    End If
    
    End If
    
    End Sub
    Many number of different ways of doing this and that above works as a test for me but I am not sure how it will work in the long run for you. You may need to keep an eye on the date of the file and whether the Datetime formatting is correct, it does test if the file exists before trying to open it. I'm using a number of different non .Net calls there but I doubt its going to be that much of a problem for a script this small.

    I also seem to have a distant memory (double check this though) when I was doing something like this with *.log files on XP that I may have had to make a change in the registry as a .log file is a protected system file, not sure if that was just to read it and/or to make changes to it aswell. The script engine will complain soon enough if it can't open the file.

    Comment


      #3
      I can't leave anything alone, if you wanted the units and sensors you could use this;

      Code:
      Sub Main(ByVal Parms As Object)
      
      Dim str As String
      Dim FileName As string = "C:\" & DateTime.Now.ToString("yyyy-MM-dd") & ".log"
      
      If System.IO.File.Exists(FileName) Then
      
      str = My.Computer.FileSystem.ReadAllText(FileName)
      Dim LastLine As String
      Dim strSplit() As String
      Dim devSplit() As String
      Dim unitSplit() As String
      Dim SensorStr() As String
      Dim startDevice As Integer = 60
      Dim HCode As String = "S"
      
      strSplit = split(str, vbNewLine)
      
      LastLine = strSplit(UBound(strSplit))
      
      'hs.writelog("LastLine", LastLine)
      
      If IsNumeric(Left(LastLine, 1)) Then
      
      LastLine = Right(LastLine, Len(LastLine) - Instr(LastLine, ";D;") - 2)
      
      devSplit = split(LastLine, ";")
      unitSplit = split(Right(strSplit(6), Len(strSplit(6)) - 4), ";")
      sensorStr = split(Right(strSplit(5), Len(strSplit(5)) - 24), ";")
      
      For i As Integer = 0 to Ubound(devSplit)
      
      If devSplit(i) <> "" Then
      
      hs.writelog("Device Update", HCode & (startDevice + i) & " - " & sensorStr(i) & " - " & devSplit(i) & " " & unitSplit(i))
      
      End If
      
      Next
      
      End If
      
      End If
      
      End Sub
      Which produces,

      21/02/2012 1:31:05 PM Device Update S60 - VDC1 - 270.0 VDC
      21/02/2012 1:31:05 PM Device Update S61 - IDC1 - 1.1 ADC
      21/02/2012 1:31:05 PM Device Update S62 - PDC1 - 300.7 W
      21/02/2012 1:31:05 PM Device Update S63 - VDC2 - 0.4 VDC
      21/02/2012 1:31:05 PM Device Update S64 - IDC2 - 1.8 ADC
      21/02/2012 1:31:05 PM Device Update S65 - PDC2 - 0.6 W
      21/02/2012 1:31:05 PM Device Update S66 - VAC - 239.4 V
      21/02/2012 1:31:05 PM Device Update S67 - IA C - 1.0 A
      21/02/2012 1:31:05 PM Device Update S68 - PAC - 260.7 W
      21/02/2012 1:31:05 PM Device Update S69 - TINV - 18.3 �C
      21/02/2012 1:31:05 PM Device Update S70 - TINT - 18.5 �C
      21/02/2012 1:31:05 PM Device Update S71 - ENERGY - 7 76.0 Wh
      21/02/2012 1:31:05 PM Device Update S72 - RISO - 20.0 MOhm
      21/02/2012 1:31:05 PM Device Update S73 - ILEAK - 0 mA
      21/02/2012 1:31:05 PM Device Update S74 - GENFREQ - 0 Hz

      Comment


        #4
        Adam, thanks for that, one problem is that I didn't release there is a blank line at the end of the log, can we read the last but one line?

        Comment


          #5
          Adam, wow! that was quick, I think it took me longer to create all the devices Thanks for that, one problem is that I didn't release there is a blank line at the end of the log, can we read the last but one line?

          top job!!

          Comment


            #6
            Try changing

            LastLine = strSplit(UBound(strSplit))

            to

            LastLine = strSplit(UBound(strSplit) - 1)

            Might need to change the other split lines aswell to have one line less than they have at the minute...

            Comment


              #7
              I created a copy of the log file and deleted the last line, and I get this in the log file:

              21/02/2012 14:07:34 - Event - Event Trigger "Grab Solar Logs"
              21/02/2012 14:07:34 - Event - Running script in background: grabsolarlogs.vb
              21/02/2012 14:07:34 - Device Update - S60 - 271.8
              21/02/2012 14:07:34 - Device Update - S61 - 1.3
              21/02/2012 14:07:34 - Device Update - S62 - 359.6
              21/02/2012 14:07:34 - Device Update - S63 - 0.5
              21/02/2012 14:07:34 - Device Update - S64 - 1.8
              21/02/2012 14:07:34 - Device Update - S65 - 0.6
              21/02/2012 14:07:34 - Device Update - S66 - 240.3
              21/02/2012 14:07:34 - Device Update - S67 - 1.3
              21/02/2012 14:07:34 - Device Update - S68 - 314.3
              21/02/2012 14:07:34 - Device Update - S69 - 19.5
              21/02/2012 14:07:34 - Device Update - S70 - 19.6
              21/02/2012 14:07:34 - Device Update - S71 - 1609.0
              21/02/2012 14:07:34 - Device Update - S72 - 20.0
              21/02/2012 14:07:34 - Device Update - S73 - 0
              21/02/2012 14:07:34 - Device Update - S74 - 0


              Could you make it read the total string and not stop at the "D" I was hoping to use the first time field to know that the log is being updated from the inverter

              Comment


                #8
                If you just want the time then this will do it;

                Code:
                Sub Main(ByVal Parms As Object)
                
                Dim str As String
                Dim FileName As string = "C:\" & DateTime.Now.ToString("yyyy-MM-dd") & ".log"
                
                If System.IO.File.Exists(FileName) Then
                
                str = My.Computer.FileSystem.ReadAllText(FileName)
                Dim LastLine As String
                Dim strSplit() As String
                Dim devSplit() As String
                Dim unitSplit() As String
                Dim SensorStr() As String
                Dim startDevice As Integer = 60
                Dim HCode As String = "S"
                
                strSplit = split(str, vbNewLine)
                
                LastLine = strSplit(UBound(strSplit) - 1)
                
                'hs.writelog("LastLine", LastLine)
                
                If IsNumeric(Left(LastLine, 1)) Then
                
                Dim Time As DateTime
                
                Time = Convert.ToDateTime(Left(LastLine, 5))
                
                hs.writelog("TimeUpdated", Time.ToString("HH:mm"))
                
                LastLine = Right(LastLine, Len(LastLine) - Instr(LastLine, ";D;") - 2)
                
                devSplit = split(LastLine, ";")
                unitSplit = split(Right(strSplit(6), Len(strSplit(6)) - 4), ";")
                sensorStr = split(Right(strSplit(5), Len(strSplit(5)) - 24), ";")
                
                For i As Integer = 0 to Ubound(devSplit)
                
                If devSplit(i) <> "" Then
                
                hs.writelog("Device Update", HCode & (startDevice + i) & " - " & sensorStr(i) & " - " & devSplit(i) & " " & unitSplit(i))
                
                End If
                
                Next
                
                End If
                
                End If
                
                End Sub
                Depends what you want to do with the time though, in the above it just prints it to the log...

                Comment


                  #9
                  Adam, ideally I'd just like the values without the units so I can make caculations on them easier in the future.

                  Regards to the time, as the log will not be updated when there is no sun, at the moment each time the script is run, it will read the last line of the text file with the same data over and over again. If we can read the first time field into a HS device S60 then we could say something like if the time time on the last line of the text file = s60 then set all values (apart from Time;Address;Model) to 0

                  or can you think of a better way of doing it??

                  Comment


                    #10
                    Originally posted by codey View Post
                    Adam, ideally I'd just like the values without the units so I can make caculations on them easier in the future.

                    Regards to the time, as the log will not be updated when there is no sun, at the moment each time the script is run, it will read the last line of the text file with the same data over and over again. If we can read the first time field into a HS device S60 then we could say something like if the time time on the last line of the text file = s60 then set all values (apart from Time;Address;Model) to 0

                    or can you think of a better way of doing it??
                    You can just comment out the units bit if you want, or remove it - only four lines are used in finding the units out, just just heard voices of my physics lecturer screaming at me for not putting the units in my work!

                    Code:
                    Sub Main(ByVal Parms As Object)
                    
                    Dim str As String
                    Dim FileName As string = "C:\" & DateTime.Now.ToString("yyyy-MM-dd") & ".log"
                    
                    If System.IO.File.Exists(FileName) Then
                    
                    str = My.Computer.FileSystem.ReadAllText(FileName)
                    Dim LastLine As String
                    Dim strSplit() As String
                    Dim devSplit() As String
                    Dim unitSplit() As String
                    Dim SensorStr() As String
                    Dim startDevice As Integer = 60
                    Dim HCode As String = "S"
                    
                    strSplit = split(str, vbNewLine)
                    
                    LastLine = strSplit(UBound(strSplit) - 1)
                    
                    'hs.writelog("LastLine", LastLine)
                    
                    If IsNumeric(Left(LastLine, 1)) Then
                    
                    Dim Time As DateTime
                    
                    Time = Convert.ToDateTime(Left(LastLine, 5))
                    
                    hs.writelog("Device Update", HCode & startDevice & " - " & Time.ToString("HH:mm"))
                    
                    LastLine = Right(LastLine, Len(LastLine) - Instr(LastLine, ";D;") - 2)
                    
                    devSplit = split(LastLine, ";")
                    
                    If Time > DateTime.Now Then
                    
                    hs.writelog("Before", "Fill All Values with 0's")
                    
                    For j as integer = 0 to ubound(devsplit)
                    
                    hs.writelog("Device Update", HCode & (startDevice + j + 1) & " - " & 0)
                    
                    Next
                    
                    Else
                    
                    hs.writelog("After", "Fill values with the data")
                    
                    unitSplit = split(Right(strSplit(6), Len(strSplit(6)) - 4), ";")
                    sensorStr = split(Right(strSplit(5), Len(strSplit(5)) - 24), ";")
                    
                    For i As Integer = 0 to Ubound(devSplit)
                    
                    If devSplit(i) <> "" Then
                    
                    hs.writelog("Device Update", HCode & (startDevice + i + 1) & " - " & sensorStr(i) & " - " & devSplit(i) & " " & unitSplit(i))
                    
                    End If
                    
                    Next
                    
                    End If
                    
                    End If
                    
                    End If
                    
                    End Sub
                    I think may be somewhere closer, just check the </> symbol on the date is what you are expecting. If it is the case that you want to check whether the time in the log file is earlier than the actual time, if it is then fill the values with 0's?

                    Comment


                      #11
                      yea I think that should work well, so to change it from writing to the log and to actually update hs devices status, can I just change hs.writelog to hs.devicestatus ?

                      Comment


                        #12
                        Originally posted by codey View Post
                        yea I think that should work well, so to change it from writing to the log and to actually update hs devices status, can I just change hs.writelog to hs.devicestatus ?
                        Pretty much, its just I don't have the devices here to test them with....

                        hs.writelog("Device Update", HCode & (startDevice + i + 1) & " - " & sensorStr(i) & " - " & devSplit(i) & " " & unitSplit(i))
                        hs.setdevicestring(HCode & (startDevice + i + 1), devSplit(i), True)
                        hs.setdevicevalue(HCode & (startDevice + i + 1), CInt(devSplit(i)))

                        just be careful with the last one converting it to a value, it might throw up an error if the string is not quite right...give it a go though
                        Last edited by mrhappy; February 21, 2012, 10:44 AM.

                        Comment


                          #13
                          cool that works, I need to add a ) to the end of the last line but it works.

                          I think I may not have explained myself very well with the time. My inverter shuts down when no sun is detected, so the log file will just stop. This will mean that in the evening the script will read the same line over and over again thinking that I'm still generating power. Can we read the time in from the log file into the value s60 and not the current system time? and then compare the time from the log file time with the s60 value (before the value is updated) to check if we need to add zero's or the values?

                          Comment


                            #14
                            Originally posted by codey View Post
                            cool that works, I need to add a ) to the end of the last line but it works.

                            I think I may not have explained myself very well with the time. My inverter shuts down when no sun is detected, so the log file will just stop. This will mean that in the evening the script will read the same line over and over again thinking that I'm still generating power. Can we read the time in from the log file into the value s60 and not the current system time? and then compare the time from the log file time with the s60 value (before the value is updated) to check if we need to add zero's or the values?
                            Spot the cheeky edit, I realised I missed a bracket....

                            The value is S60 is the time from the last line of the log;

                            Code:
                            Dim Time As DateTime
                            Time = Convert.ToDateTime(Left(LastLine, 5))
                            hs.writelog("Device Update", HCode & startDevice & " - " & Time.ToString("HH:mm"))
                            I've had a hard day laying about doing sweet FA so my brain is suffering a bit, but this line;

                            If Time > DateTime.Now Then

                            you may be able to change to;

                            If Time > convert.todate(hs.devicestring("S60")) Then

                            or something like it anyway...

                            Comment


                              #15
                              sorry, I didn't undertand your the syntax,

                              so firstly I'll need to write the log file date into s60 do I do this:

                              hs.writelog("Device Update", HCode & startDevice & " - " & Time.ToString("HH:mm"))

                              hs.setdevicevalue("s60" startDevice & " - " & Time.ToString("HH:mm")"

                              Comment

                              Working...
                              X