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