From: albertleng on
This is an extension of issue i posted back in Dec 2008 which is at
the link below:
http://groups.google.com/group/microsoft.public.vb.general.discussion/browse_thread/thread/5529416ef9423c43

Basically, i have solved the problem using LFS's idea, i.e.
Option Explicit
Private Rooms As Collection

Private Sub Form_Load()

Set Rooms = New Collection

AddSlot "r101", #12:00:00 PM#, #3:00:00 PM# ' Times could include
date if desired

AddSlot "r222", #1:00:00 PM#, #2:00:00 PM#
AddSlot "r222", #4:00:00 PM#, #6:00:00 PM#
AddSlot "r222", #5:30:00 PM#, #7:00:00 PM# ' Note overlap

Debug.Print "Are lights on for Room 101 at 1:00PM?",
LightsOn("r101", #1:00:00 PM#)
Debug.Print "Are lights on for Room 101 at 6:00PM?",
LightsOn("r101", #6:00:00 PM#)
Debug.Print "Are lights on for Room 222 at 1:00PM?",
LightsOn("r222", #1:00:00 PM#)
Debug.Print "Are lights on for Room 222 at 6:00PM?",
LightsOn("r222", #6:00:00 PM#)

End Sub

Sub AddSlot(Room As String, ByVal Start As Date, ByVal Finish As Date)
Dim rm
Set rm = FindRoom(Room)
If rm Is Nothing Then
Set rm = New Collection
Rooms.Add rm, Room
End If
rm.Add Array(Start, Finish)
End Sub

Function LightsOn(Room As String, ByVal When As Date) As Boolean
Dim rm, wn
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each wn In rm
LightsOn = LightsOn Or ((wn(0) <= When) And (wn(1) >= When))
Next
End If
End Function

Now, there are new requirements which are based on tapping in and out
of access card to the room. Each tap in and tap out sends a message
via TCP/IP to my program with the room number.

The booking scenario is like:
The booking scenario:
Booking time: 10:00-11:00
- AHU/Light will be turned on at 10:00
- Teaching staff is allowed to go in earlier by tapping in (but AHU/
Light is still Off)
- After 15 minutes from 10:00 (start-time). without tap-in by teaching
staff, AHU/Light should turn-OFF at 10:15.
- 1st tap by teaching staff unlocks the door (tap-in) and 2nd tap
locks the door (tap-out)
- AHU/Light is turned OFF after 15 minutes of tap-out.

During the booking time:
- for card access: 2nd tap - door lock, 3rd tap - door unlock, 4th tap
- door lock, and so on. Same as above where after 15 minutes of tap
out (door lock), turn off AHU/Light
- every time door lock received -> after 15 minutes, turn-off AHU/
Light, and everytime door unlocks - turn-on lights and aircon.

After the booking time:
- Teaching staff must tap out - door lock and after 15 minutes, turn
off AHU/Light.
- FBMS extends 15 min more from the ending time. if the teachings
staff forgets to tap out, on this scenario it will off at 11:15.

Basically, the start time and end time of the AHU/Light does not open
depend on booking time but also the frequent change of tap-in and out
by the teaching staff.
I believe i'll need to use Remove and Add of the Room collection for
the above new requirements. However, i'm quite confused on how to use
the collection Add and Remove based on keys/index in my case.

Please help. Thanks a lot.
From: Larry Serflaten on

"albertleng" <albertleng(a)gmail.com> wrote
<snipped for brievity>
> Basically, the start time and end time of the AHU/Light does not open
> depend on booking time but also the frequent change of tap-in and out
> by the teaching staff.
> I believe i'll need to use Remove and Add of the Room collection for
> the above new requirements. However, i'm quite confused on how to use
> the collection Add and Remove based on keys/index in my case.
>
> Please help. Thanks a lot.

See if this helps....

You'll need to keep more information about the rooms to determine
occupancy. Appearently the lights go on a the start of the booking
time whether or not the teacher shows up. But, if the teacher never
shows up, you'll want to shut the lights off early.

And, if the teacher leaves early, you'll also want to shut the lights off
early.

So where you now have:

rm.Add Array(Start, Finish)

You'd want something like:

rm.Add Array(Start, Finish, LightsOut, Occupied)

Such that LightsOut is initially set to 15 minutes after Start.
If the teacher never shows up, the lights go out 15 minutes after Start.
When the teacher taps in, you'd change Occupied to True and
change LightsOut to 15 minutes after Finish.
If the teacher fails to tap out, the lights would go out at the
LightsOut time, and you'd set Occupied to False.
When the teacher taps out early, you'd change Occupied
to False and LightsOut to 15 minutes after the tap out time.
(What if the teacher taps out late?)

In other words, you'd keep track of the booking time, and
the time the lights should go out. The LightsOut time
changes based on the tapping in and out conditions, but
in all cases, the lights go out at the LightsOut time.

I added Occupied to help you keep it straight, what is
going on with the room, but it is essentially not needed.
As long as you alter the LightsOut time appropreately, that
is all that would be added to manage the equipment.

Unfortunately, once you add an array to the collection, it
becomes read-only. So, to continue using an array, you'd
have to workaround that problem every time you wanted
to change the LightsOut time.

Another workaround would use an object (class) that
has the Start, Finish, and LightsOut properties. While
you still could not change the object in the collection, the
properties of that object can be changed, while it is still
in the collection. So the line above could be:

rm.Add NewSlot(Start, Finish)

Where

Function NewSlot(Byval Start as date, ByVal Finish as Data) as SlotObject
Set NewSlot = New SlotObject ' defined in SlotObject class
With NewSlot
.Start = Start
.Finish = Finish
.LightsOut = DateAdd("n", 15, Start)
End With
End Function

Does that sound workable to you?

LFS








From: Kevin Provance on
"Larry Serflaten" <serflaten(a)gmail.com> wrote in message
news:hvo5b0$3gv$1(a)news.eternal-september.org...
:
: See if this helps....
:
: You'll need to keep more information about the rooms to determine
: occupancy. Appearently the lights go on a the start of the booking
: time whether or not the teacher shows up. But, if the teacher never
: shows up, you'll want to shut the lights off early.
:
: And, if the teacher leaves early, you'll also want to shut the lights off
: early.
:
: So where you now have:
:
: rm.Add Array(Start, Finish)
:
: You'd want something like:
:
: rm.Add Array(Start, Finish, LightsOut, Occupied)
:
: Such that LightsOut is initially set to 15 minutes after Start.
: If the teacher never shows up, the lights go out 15 minutes after Start.
: When the teacher taps in, you'd change Occupied to True and
: change LightsOut to 15 minutes after Finish.
: If the teacher fails to tap out, the lights would go out at the
: LightsOut time, and you'd set Occupied to False.
: When the teacher taps out early, you'd change Occupied
: to False and LightsOut to 15 minutes after the tap out time.
: (What if the teacher taps out late?)
:
: In other words, you'd keep track of the booking time, and
: the time the lights should go out. The LightsOut time
: changes based on the tapping in and out conditions, but
: in all cases, the lights go out at the LightsOut time.
:
: I added Occupied to help you keep it straight, what is
: going on with the room, but it is essentially not needed.
: As long as you alter the LightsOut time appropreately, that
: is all that would be added to manage the equipment.
:
: Unfortunately, once you add an array to the collection, it
: becomes read-only. So, to continue using an array, you'd
: have to workaround that problem every time you wanted
: to change the LightsOut time.
:
: Another workaround would use an object (class) that
: has the Start, Finish, and LightsOut properties. While
: you still could not change the object in the collection, the
: properties of that object can be changed, while it is still
: in the collection. So the line above could be:
:
: rm.Add NewSlot(Start, Finish)
:
: Where
:
: Function NewSlot(Byval Start as date, ByVal Finish as Data) as SlotObject
: Set NewSlot = New SlotObject ' defined in SlotObject class
: With NewSlot
: .Start = Start
: .Finish = Finish
: .LightsOut = DateAdd("n", 15, Start)
: End With
: End Function
:
: Does that sound workable to you?
:

Nice. Sounds great to me, and it ain't even my project. <g>

--
Customer Hatred Knows No Bounds at MSFT
Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc

Bawwk! Paulie want a dingleball, bawwk!

From: Larry Serflaten on

"Kevin Provance" <k(a)p.c> wrote

> : So where you now have:
> :
> : rm.Add Array(Start, Finish)
> :
> : You'd want something like:
> :
> : rm.Add Array(Start, Finish, LightsOut, Occupied)
<...>
> : Does that sound workable to you?
> :
>
> Nice. Sounds great to me, and it ain't even my project. <g>

After I got away from it I realized that the OP may need to
keep Occupied as an indicator if the door should be opened
or locked.

If the teacher taps in while the room is not occupied,
then open the door and mark the room as occupied.
If the teacher taps in while the room is occupied,
then lock the door and mark the room as not occupied.

The nice thing about that would be that the Occupied
property (routines) in the class could be used to handle
the LightsOut adjustments such that all that on/off timing
is together in one place, the class module.

eg:

Function NewSlot(Byval Start as date, ByVal Finish as Data) as SlotObject
Set NewSlot = New SlotObject ' defined in SlotObject class
With NewSlot
.Start = Start
.Finish = Finish
.LightsOut = DateAdd("n", 15, Start)
.Occupied = False
End With
End Function


And a quick draft of the Occupied property in the class:

Public Property Get Occupied() As Boolean
Occupied = m_Occupied
End Property

Public Property Let Occupied(ByVal Value As Boolean)
' Tapping in or out before booking ends
If Now <= m_Finish Then
' Tapping in
If Value = True Then
m_LightsOut = DateAdd("n", 15, m_Finish)
Else
' Tapping out
m_LightsOut = DateAdd("n", 15, Now)
End If
End If
m_Occupied = Value
End Property


From: albertleng on
Hi LFS,

I tried to use your suggestion for this issue and i have completed
part of it and i'll need some more pointers for that. I'm stuck at
Function LightOn (how can i compare Now with SlotObject's Start and
LightOff) and
sub TapIn and sub TapOut. For TapIn and TapOut, it's mainly about when
i receive tap in or tap out, how can i retrieve the stored SlotObject
from the Room Collection?

Please help again. Thanks a lot.


Main Program:

Option Explicit
Private Rooms As Collection
Private Sub Form_Load()

Set Rooms = New Collection

AddSlot "r101", #12:00:00 PM#, #3:00:00 PM# ' Times could include
date if desired

AddSlot "r222", #1:00:00 PM#, #2:00:00 PM#
AddSlot "r222", #4:00:00 PM#, #6:00:00 PM#
AddSlot "r222", #5:30:00 PM#, #7:00:00 PM# ' Note overlap

Debug.Print "Are lights on for Room 101 at 1:00PM?",
LightsOn("r101", #1:00:00 PM#)
Debug.Print "Are lights on for Room 101 at 6:00PM?",
LightsOn("r101", #6:00:00 PM#)
Debug.Print "Are lights on for Room 222 at 1:00PM?",
LightsOn("r222", #1:00:00 PM#)
Debug.Print "Are lights on for Room 222 at 6:00PM?",
LightsOn("r222", #6:00:00 PM#)

End Sub

Sub TapIn(Room as String, Time as Date)
'How can i find the SlotObject inside the Rooms Collection, get its
stored Start and Finish and then, amend it?
End Sub

Sub TapOut(Room as String, Time as Date)
'How can i find the SlotObject inside the Rooms Collection, get its
stored Start and Finish and then, amend it?
End Sub



Function NewSlot(ByVal Start As Date, ByVal Finish As Date) As
SlotObject
Set NewSlot = New SlotObject ' defined in SlotObject class
With NewSlot
.Start = Start
.Finish = Finish
.LightOff = DateAdd("n", 15, Start)
.Occupied = False
End With
End Function


Function FindRoom(Room As String) As Collection
On Error Resume Next

Set FindRoom = Rooms(Room)
End Function

Sub AddSlot(Room As String, ByVal Start As Date, ByVal Finish As Date)
Dim rm
Set rm = FindRoom(Room)
If rm Is Nothing Then
Set rm = New Collection
Rooms.Add rm, Room
End If
rm.Add NewSlot(Start, Finish)
End Sub

Function LightsOn(Room As String, ByVal When As Date) As Boolean
Dim rm, wn
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each wn In rm
'This is the part where i don't know how i can replace wn(0)
with Start and wn(1) with LightOff from each slot object in rm
collection
LightsOn = LightsOn Or ((wn(0) <= When) And (wn(1) >= When))
Next
End If
End Function






SlotObject.cls:

Public Property Get Start() As Date
Start = m_Start
End Property
Public Property Let Start(ByVal vStart As Date)
m_Start = vStart
End Property
Public Property Get Finish() As Date
Finish = m_Finish
End Property
Public Property Let Finish(ByVal vFinish As Date)
m_Finish = vFinish
End Property
Public Property Get LightOff() As Date
LightOff = m_Lightoff
End Property
Public Property Let LightOff(ByVal vLightOff As Date)
m_Lightoff = vLightOff
End Property
Public Property Get Occupied() As Boolean
Occupied = m_Occupied
End Property
Public Property Let Occupied(ByVal Value As Boolean)
'Tapping in or out before booking ends
If Now <= Finish Then
'Tapping in
If Value = True Then
m_Lightoff = DateAdd("n", 15, Finish)
Else
'Tapping out
m_Lightoff = DateAdd("n", 15, Now)
End If
End If

m_Occupied = Value

End Property



On Jun 22, 8:07 pm, "Larry Serflaten" <serfla...(a)gmail.com> wrote:
> "Kevin Provance" <k...(a)p.c> wrote
>
>
>
> > : So where you now have:
> > :
> > :   rm.Add Array(Start, Finish)
> > :
> > : You'd want something like:
> > :
> > :  rm.Add Array(Start, Finish, LightsOut, Occupied)
> <...>
> > : Does that sound workable to you?
> > :
>
> > Nice.  Sounds great to me, and it ain't even my project.  <g>
>
> After I got away from it I realized that the OP may need to
> keep Occupied as an indicator if the door should be opened
> or locked.
>
> If the teacher taps in while the room is not occupied,
> then open the door and mark the room as occupied.
> If the teacher taps in while the room is occupied,
> then lock the door and mark the room as not occupied.
>
> The nice thing about that would be that the Occupied
> property (routines) in the class could be used to handle
> the LightsOut adjustments such that all that on/off timing
> is together in one place, the class module.
>
> eg:
>
> Function NewSlot(Byval Start as date, ByVal Finish as Data) as SlotObject
>   Set NewSlot = New SlotObject  ' defined in SlotObject class
>   With NewSlot
>     .Start = Start
>     .Finish = Finish
>     .LightsOut = DateAdd("n", 15, Start)
>     .Occupied = False
>   End With
> End Function
>
> And a quick draft of the Occupied property in the class:
>
> Public Property Get Occupied() As Boolean
>   Occupied = m_Occupied
> End Property
>
> Public Property Let Occupied(ByVal Value As Boolean)
>   ' Tapping in or out before booking ends
>   If Now <= m_Finish Then
>     ' Tapping in
>     If Value = True Then
>       m_LightsOut = DateAdd("n", 15, m_Finish)
>     Else
>       ' Tapping out
>       m_LightsOut = DateAdd("n", 15, Now)
>     End If
>   End If
>   m_Occupied = Value
> End Property