Please login or register.

Login with username, password and session length

Author Topic: my first try based on vbapp sample code.  (Read 6811 times)

fengtso

  • Newbie
  • Helpful Post Rating: 1
  • Posts: 8
my first try based on vbapp sample code.
« on: May 04, 2008, 08:24:30 PM »

I tried to do a simple test.
I set a TimeBudget as 10 seconds and try to turn on M15 and turn it off while TimeBudget runs out.
But my problem is that the time duration is always less than 10 seconds.
Sometimes, it will continue sending out "sendplc a1 off" after I run out of TimeBudget.
Please help me out! Thanks a lot.

==============================================
' VBScript source code

Imports ActiveHomeScriptLib
Imports System

Module Module1
    Dim WithEvents ActiveHomeObj As ActiveHome
    Dim EventStart As Date
    Dim EventEnd As Date
    Dim checkPoint As Date
    Dim TimeBudget As Long = 10

    <MTAThread()> Sub Main()
        'create ActiveHome object
        Try
            ActiveHomeObj = CreateObject("X10.ActiveHome")
        Catch exc As Exception
            Console.WriteLine("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        Finally
        End Try

        'send a1 on
        'Try
        'ActiveHomeObj.SendAction("sendplc", "a1 on")
        'Dim currentBegin As Date = Date.Now

        'Catch exc As Exception
        'Console.WriteLine("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        'Finally
        'End Try
        'wait for events
        'Dim nRead As Integer
        Dim newStatus = Status()
        Dim oldStatus

        While True

            System.Threading.Thread.Sleep(1000)
            'nRead = Console.Read()
            'checkTimeBudget()
            oldStatus = newStatus
            newStatus = Status()

            If newStatus = 1 And oldStatus = 0 Then
                Console.WriteLine("just turn on")
                checkTimeBudget(TimeBudget)
                EventStart = Date.Now


            ElseIf newStatus = 1 And oldStatus = 1 Then
                Console.WriteLine("still on")
                checkTimeBudget(TimeBudget)
                decreaseTimeBudget(TimeBudget)
            ElseIf newStatus = 0 And oldStatus = 1 Then
                Console.WriteLine("just turn off")
                EventEnd = Date.Now
                ' Console.WriteLine("{0:N2} EventStart", EventStart.Ticks)
                'Console.WriteLine("{0:N2} EventEnd", EventEnd.Ticks)
                Dim elapsedTicks As Long = EventEnd.Ticks - EventStart.Ticks
                Dim elapsedSpan As New TimeSpan(elapsedTicks)
                TimeBudget = TimeBudget - elapsedSpan.TotalSeconds
                If TimeBudget < 0 Then
                    TimeBudget = 0
                End If
                Console.WriteLine("turn off TimeBudget:{0:N2} seconds", TimeBudget)
            Else
                Console.WriteLine("TimeBudget:{0:N2} seconds", TimeBudget)
            End If
        End While



    End Sub

    'events from ActiveHome: write out received event
    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

        Console.WriteLine("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
    End Sub

    'query a1 status on return 1, off return 0
    Function Status()
        'Console.WriteLine(" Status ")
        Dim querystatus
        Status = 0
        Try
            querystatus = ActiveHomeObj.SendAction("queryplc", "a1 on")
            Status = querystatus
            'If querystatus = 0 Then
            'Console.WriteLine("A1 is off")
            'ElseIf querystatus = 1 Then
            'Console.WriteLine("A1 is on")
            'End If
        Catch ex As Exception
            Console.WriteLine("err num: " & Str(Err.Number) & " '" & ex.ToString & "'")
        End Try

    End Function


    Sub decreaseTimeBudget(ByRef TimeBudget)
        'Console.WriteLine(" check")
        checkPoint = Date.Now
        Dim elapsedTicks As Long = checkPoint.Ticks - EventStart.Ticks
        Dim elapsedSpan As New TimeSpan(elapsedTicks)
        TimeBudget = TimeBudget - elapsedSpan.TotalSeconds
        Console.WriteLine("in dec TimeBudget:{0:N2} seconds ", TimeBudget)

    End Sub

    Sub checkTimeBudget(ByRef TimeBudget)

        If TimeBudget <= 0 Then
            Try
                ActiveHomeObj.SendAction("sendplc", "a1 off")
                'If querystatus = 0 Then
                'Console.WriteLine("A1 is off")
                'ElseIf querystatus = 1 Then
                'Console.WriteLine("A1 is on")
                'End If
                TimeBudget = 0.0
                Console.WriteLine("in check TimeBudget:{0:N2} seconds ", TimeBudget)
            Catch ex As Exception
                Console.WriteLine("err num: " & Str(Err.Number) & " '" & ex.ToString & "'")
            End Try
        Else
            Console.WriteLine("in check TimeBudget:{0:N2} seconds ", TimeBudget)
        End If
    End Sub

End Module

Logged

fengtso

  • Newbie
  • Helpful Post Rating: 1
  • Posts: 8
Re: my first try based on vbapp sample code.
« Reply #1 on: May 04, 2008, 08:58:39 PM »

I think I know where the problem is. I didn't set new EventStart after I decrease.
Thanks.
Logged

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1340
  • He's just this guy. You know?
    • wgjohns.com
Re: my first try based on vbapp sample code.
« Reply #2 on: May 08, 2008, 12:57:53 AM »

Sorry, been a bit busy!

Sounds like you're getting the hang of the SDK in VB.   8)

Let me know if you need any more pointers.
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.