X10 Community Forum

🖥️ActiveHome Pro => SDK => Topic started by: Mustang65 on December 17, 2012, 09:54:27 PM

Title: X10 Activity to text file
Post by: Mustang65 on December 17, 2012, 09:54:27 PM
Question:  Below is the code that displays the X10 activity in a text box (VB 2008).  Works fine!  What I need, is to take the "X10 Activity Data" and append it to a text file (X10Activity.txt)....  while it is also being sent to the TextBox. I understand that the motion sensors will add quickly to the size of the text file, but I will limit the number of records that are retained in the file (Limit 500 using FIFO).  Does anyone have the code to accomplish this task. I am looking at this to be real time.  The file will then be uploaded to my webserver where I will have access to the data while I am on the road. I eventually will generate a txt msg if any of the security sensors trip, while I am on the road.  My SC12A is not reliable.

Thanks in advance for any assistance you can offer.

Don
-------------------------------------------------------------------------------------------------------------
Sub ActiveHomeTrafficAction(ByVal bszRecv As Object _
                            , ByVal vParm1 As Object _
                            , ByVal vParm2 As Object _
                            , ByVal vParm3 As Object _
                            , ByVal vParm4 As Object _
                            , ByVal vParm5 As Object _
                            , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction

        ActiveHomeTraffic = (bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
        AHEvents = DateString & " - " & TimeString & " - " & bszRecv & " - " & vParm1 & " - " & vParm2
        AHEvents = AHEvents & vbCrLf
        TextBox1.AppendText(AHEvents)

    End Sub
-------------------------------------------------------------------------------------------------------------
Title: Re: X10 Activity to text file
Post by: spval on December 18, 2012, 04:55:27 AM
Maybe something like this: (I use a second textbox (textbox2)

 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

        Dim TargetFile As StreamWriter
        Try
            System.IO.File.WriteAllText("C:\X10Activity.txt", "")
            TargetFile = New StreamWriter("C:\X10Activity.txt", True)
            TargetFile.Write(Now())
            TargetFile.Write(TextBox2.Text)
            TargetFile.WriteLine()
            TargetFile.WriteLine()
            TargetFile.Close()
            Dim clsRequest As System.Net.FtpWebRequest = _
           DirectCast(System.Net.WebRequest.Create("ftp://ftp.<your-website>.com/X10Activity.txt"), System.Net.FtpWebRequest)
            clsRequest.Credentials = New System.Net.NetworkCredential("<your ftp login>", "your ftp password")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

            ' read in file...
            Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\X10Activity.txt")

            ' upload file...
            Dim clsStream As System.IO.Stream = _
           clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
            TextBox2.AppendText(TextBox1.Text & "  " & Date.Now & vbCrLf)
        Catch exc As Exception
            Console.WriteLine("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        End Try
End Sub


If you would like the code to generate a text message from your application (not from the web data), let me know.

- spval

Title: Re: X10 Activity to text file
Post by: Mustang65 on December 18, 2012, 08:34:53 AM
Thank you very much!!!!!
I will give this a try this afternoon.
Have a GREAT DAY!
Don
Title: Re: X10 Activity to text file
Post by: Tuicemen on December 18, 2012, 09:26:56 AM
One line added to your current Sub code will write the text file.
 
Code: [Select]
My.Computer.FileSystem.WriteAllText(My.Application.Info.DirectoryPath + "\Logs\X10Received.log", AHEvents, True)          no need to even have a text box
However I add a date to my text file name so I can refere back to previous days activity using
Code: [Select]
Dim tday As String = Today
            tday = tday.Replace("/", "-")
          My.Computer.FileSystem.WriteAllText(My.Application.Info.DirectoryPath + "\Logs\X10Received"+tday+".log", AHEvents, True)[/
>!
Title: Re: X10 Activity to text file
Post by: spval on December 18, 2012, 11:35:48 AM
Yes, there are different ways to arrive at the same result. I like to have multiple text boxes so that I can see what is going on during the process. It helps me to troubleshoot problems later on. I built the code from scratch this time, just to make sure that I show all of the code required:

Imports ActiveHomeScriptLib
Imports System.IO

Public Class Form1
    Dim WithEvents ActiveHomeObj As ActiveHomeScriptLib.ActiveHome

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ActiveHomeObj = New ActiveHome
    End Sub

    Sub ActiveHome_RecvAction(ByVal bszRecv As Object _
                           , ByVal vParm1 As Object _
                           , ByVal vParm2 As Object _
                           , ByVal vParm3 As Object _
                           , ByVal vParm4 As Object _
                           , ByVal vParm5 As Object _
                           , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction

        TextBox1.Text = "  " & (vParm1 & " " & vParm2 & "  " & vParm3)

    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim TargetFile As StreamWriter
        Try
            System.IO.File.WriteAllText("C:\X10Activity.txt", "")
            TargetFile = New StreamWriter("C:\X10Activity.txt", True)
            TargetFile.Write(Now())
            TargetFile.Write(TextBox2.Text)
            TargetFile.WriteLine()
            TargetFile.WriteLine()
            TargetFile.Close()
            Dim clsRequest As System.Net.FtpWebRequest = _
         DirectCast(System.Net.WebRequest.Create("http://ftp://ftp.<your-website>.com/X10Activity.txt"), System.Net.FtpWebRequest)
            clsRequest.Credentials = New System.Net.NetworkCredential("<your ftp login>", "your ftp password")
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

            ' read in file...
            Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\X10Activity.txt")

            ' upload file...
            Dim clsStream As System.IO.Stream = _
           clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)
            clsStream.Close()
            clsStream.Dispose()
            TextBox2.AppendText(TextBox1.Text & "  " & Date.Now & vbCrLf)
        Catch exc As Exception
            Console.WriteLine("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        End Try
    End Sub

End Class

The program will hang momentarily when it receives the first event. Just let it run and trigger your events and watch the file up on the web.
The file on the web will reset (zero out) every time the program is launched.

Don can modify it according to his needs.

- spval