Announcement
Collapse
No announcement yet.
Read Data in from log file
Collapse
This topic is closed.
X
X
-
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
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.
-
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
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
-
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
-
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
Comment
-
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
-
Originally posted by codey View PostAdam, 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??
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
Comment
-
Originally posted by codey View Postyea 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 ?
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 thoughLast edited by mrhappy; February 21, 2012, 10:44 AM.
Comment
-
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
-
Originally posted by codey View Postcool 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?
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"))
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
Comment