From: Larry Serflaten on

"albertleng" <albertleng(a)gmail.com> wrote

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.

------------------

If a teacher swips a card at the door of a room, how do you know if its a
tap in or a tap out? Unless you plan to also keep track of individual
teachers (who reaserve slots) then I don't see how you are going to
differentiate between the two. For example, teacher swips in and later
the assistant swips the same door (by accident or whatever). Unless
you keep track of who's accessing the room, you'd think the second
swipe was a tap out and proceed to lock the door....

Until you keep track of the booking agent (teacher) any teacher could
access the room any time a booking was active. So, you'll have to get
more definition on that... (which would be another property of the
slot object)

Until you keep track of who's who, any swipe at the door is a tap in
if the room is not occupied, and a tap out if the room is occupied.
For that reason, I changed your TapIn and TapOut to just one
routine called RoomAccess. I also altered the slot object to only
allow access 10 minutes ahead of or after a booking time.

So this is what I came up with:

' [ Form1 code ]
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", #6:00:00 PM#, #7:00:00 PM# ' Note overlap

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

End Sub

Sub RoomAccess(Room As String, Time As Date)
Dim rm, so As SlotObject
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each so In rm
If so.AccessAllowed(Time) Then
so.Occupied = Not so.Occupied
' Door unlock = so.Occupied
Next
End If
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
.LightsOff = 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, so As SlotObject
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each so In rm
LightsOn = LightsOn Or ((so.Start <= When) And (so.LightsOff >= When))
Next
End If
End Function


' [ SlotObject code ]
Option Explicit
Private m_Start As Date
Private m_Finish As Date
Private m_LightsOff As Date
Private m_Occupied As Boolean

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 LightsOff() As Date
LightsOff = m_LightsOff
End Property
Public Property Let LightsOff(ByVal vLightsOff As Date)
m_LightsOff = vLightsOff
End Property
Public Property Get Occupied() As Boolean
Occupied = m_Occupied
End Property
Public Property Let Occupied(ByVal vOccupied As Boolean)
'Tapping in or out before booking ends
If Now <= m_Finish Then
'Tapping in
If vOccupied = True Then
m_LightsOff = DateAdd("n", 15, Finish)
Else
'Tapping out
m_LightsOff = DateAdd("n", 15, Now)
End If
End If
m_Occupied = vOccupied
End Property
Public Property Get AccessAllowed(ByVal vTime As Date) As Boolean
' Access allowed 10 minutes either side of booking time
AccessAllowed = (vTime > DateAdd("n", -10, m_Start) And vTime < DateAdd("n", 10, m_Finish))
End Property








From: albertleng on
Hi.

For Tap in and tap out, our partner who's in charge of card access has
programmed such a way that their system will send me message via TCPIP
like
"TapIn/Out,RoomNo,DateTime'. The tapIn and tapOut message are only
sent if it's from teaching staff. From there, my program can know
whether it's a tap in or tap out. Also, card access (lock or unlock of
the room door) is handled by their system.

So, i think i can write something like

'Called when i receive Tap In
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?

Dim rm, so As SlotObject
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each so In rm
so.Occupied = True
Next
End If

End Sub

'Called when i receive Tap Out
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?

Dim rm, so As SlotObject
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each so In rm
If so.AccessAllowed(Time) Then
so.Occupied =False
Next
End If

End Sub

I'll still have to try it out later. Anyway, thanks again.


On Jun 23, 7:20 am, "Larry Serflaten" <serfla...(a)gmail.com> wrote:
> "albertleng" <albertl...(a)gmail.com> wrote
>
> 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.
>
> ------------------
>
> If a teacher swips a card at the door of a room, how do you know if its a
> tap in or a tap out?  Unless you plan to also keep track of individual
> teachers (who reaserve slots) then I don't see how you are going to
> differentiate between the two.  For example, teacher swips in and later
> the assistant swips the same door (by accident or whatever).  Unless
> you keep track of who's accessing the room, you'd think the second
> swipe was a tap out and proceed to lock the door....
>
> Until you keep track of the booking agent (teacher) any teacher could
> access the room any time a booking was active.  So, you'll have to get
> more definition on that... (which would be another property of the
> slot object)
>
> Until you keep track of who's who, any swipe at the door is a tap in
> if the room is not occupied, and a tap out if the room is occupied.
> For that reason, I changed your TapIn and TapOut to just one
> routine called RoomAccess.  I also altered the slot object to only
> allow access 10 minutes ahead of or after a booking time.
>
> So this is what I came up with:
>
> ' [ Form1 code ]
> 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", #6:00:00 PM#, #7:00:00 PM# ' Note overlap
>
>    Debug.Print "Lights on in Room 101 at 1:00PM?", LightsOn("r101", #1:00:00 PM#)
>    Debug.Print "Lights on in Room 101 at 6:00PM?", LightsOn("r101", #6:00:00 PM#)
>    Debug.Print "Lights on in Room 222 at 1:00PM?", LightsOn("r222", #1:00:00 PM#)
>    Debug.Print "Lights on in Room 222 at 6:00PM?", LightsOn("r222", #6:00:00 PM#)
>
> End Sub
>
> Sub RoomAccess(Room As String, Time As Date)
> Dim rm, so As SlotObject
>   Set rm = FindRoom(Room)
>   If Not rm Is Nothing Then
>     For Each so In rm
>       If so.AccessAllowed(Time) Then
>         so.Occupied = Not so.Occupied
>         ' Door unlock = so.Occupied
>     Next
>   End If
> 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
>     .LightsOff = 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, so As SlotObject
>   Set rm = FindRoom(Room)
>   If Not rm Is Nothing Then
>     For Each so In rm
>       LightsOn = LightsOn Or ((so.Start <= When) And (so.LightsOff >= When))
>     Next
>   End If
> End Function
>
> ' [ SlotObject code ]
> Option Explicit
> Private m_Start As Date
> Private m_Finish As Date
> Private m_LightsOff As Date
> Private m_Occupied As Boolean
>
> 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 LightsOff() As Date
>     LightsOff = m_LightsOff
> End Property
> Public Property Let LightsOff(ByVal vLightsOff As Date)
>     m_LightsOff = vLightsOff
> End Property
> Public Property Get Occupied() As Boolean
>     Occupied = m_Occupied
> End Property
> Public Property Let Occupied(ByVal vOccupied As Boolean)
>     'Tapping in or out before booking ends
>     If Now <= m_Finish Then
>         'Tapping in
>         If vOccupied = True Then
>             m_LightsOff = DateAdd("n", 15, Finish)
>         Else
>         'Tapping out
>             m_LightsOff = DateAdd("n", 15, Now)
>         End If
>     End If
>     m_Occupied = vOccupied
> End Property
> Public Property Get AccessAllowed(ByVal vTime As Date) As Boolean
>   ' Access allowed 10 minutes either side of booking time
>   AccessAllowed = (vTime > DateAdd("n", -10, m_Start) And vTime < DateAdd("n", 10, m_Finish))
> End Property

From: Kevin Provance on

"albertleng" <albertleng(a)gmail.com> wrote in message
news:553fd80e-b4bd-4b35-b79f-a0bba2ad4092(a)j8g2000yqd.googlegroups.com...

.. Dim rm, so As SlotObject

I'm fairly certain with this line of code, fm is dimmed as variant. Is that
intended?

From: Larry Serflaten on

"Kevin Provance" <k(a)p.c> wrote
in message news:hvrlg4$c95$1(a)news.eternal-september.org...
>
> "albertleng" <albertleng(a)gmail.com> wrote in message
> news:553fd80e-b4bd-4b35-b79f-a0bba2ad4092(a)j8g2000yqd.googlegroups.com...
>
> . Dim rm, so As SlotObject
>
> I'm fairly certain with this line of code, fm is dimmed as variant. Is that
> intended?

Good catch. VB5 gets picky about what can be used in For Each cases
so if I'm going that route, I tend to default to variants. In this case, however,
rm could be declared as a collection and it would still work.

LFS


From: Larry Serflaten on

"albertleng" <albertleng(a)gmail.com> wrote

For Tap in and tap out, our partner who's in charge of card access has
programmed such a way that their system will send me message via TCPIP
like
"TapIn/Out,RoomNo,DateTime'. The tapIn and tapOut message are only
sent if it's from teaching staff. From there, my program can know
whether it's a tap in or tap out. Also, card access (lock or unlock of
the room door) is handled by their system.

-----

That would have been good to know from the start. From what you
posted it appeared you had to keep track of the door lock. Since the
door lock is being handled by somebody else, you would not need
the Occupied property and can in effect dumb down that class to
just a glorified data structure. (Left as a class to allow alterations
while being held in th room's collection)

-----

So, i think i can write something like

'Called when i receive Tap In
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?

Dim rm, so As SlotObject
Set rm = FindRoom(Room)
If Not rm Is Nothing Then
For Each so In rm
so.Occupied = True
Next
End If

End Sub

-----

In short, that would not work as intended. It would in effect
change all the LightsOff values to 15 minutes after Finish when
all you want to change is the one (or two) slot(s) that is currently
being accessed. I left the Occupied property posted below:

-----
> Public Property Let Occupied(ByVal vOccupied As Boolean)
> 'Tapping in or out before booking ends
> If Now <= m_Finish Then
> 'Tapping in
> If vOccupied = True Then
> m_LightsOff = DateAdd("n", 15, Finish) ' << See here
> Else
> 'Tapping out
> m_LightsOff = DateAdd("n", 15, Now)
> End If
> End If
> m_Occupied = vOccupied
> End Property
-----

As is, if you change Occupied on a TapIn (which you wanted to
change all of them) you also change the LightsOff time.

To process the Tap In (Tap Out would be similar) you would want
to get to the specific booking slot that is granting access. Then you'd
set the LightsOff value to after Finish and be done with it. (That is all
Occupied did other than tracking the Occupied value) Something like:

<air code warning...>
Sub TapIn(Room as String, Time as Date)
Dim bk As SlotObject
Set bk = FindBooking(Room, Time)
If Not bk Is Nothing Then
bk.LightsOff = DateAdd("n", 15, bk.Finish)
End If
End Sub

Note how I just took the "see here" line from Occupied, and moved it
into the TapIn routine. What was added was the FindBooking function
that would take the Room and Time and return the appropreate slot.

That FindBooking function would loop through the rooms slot collection
to see if there was a booking that allowed access at the desired time.

But the problem I forsee is what happens when the room is booked
from 5:00pm to 6:00pm AND from 6:00pm to 7:00pm. and you
get a TapIn call at 5:55pm. Is that from the first booking or does it
belong to the second?

A teacher may tap out at 5:45 and then want to get back in to get
some fogotten item. So at the TapOut call, the LightsOff time
is moved to 15 minutes later (6:00pm) At 5:55pm that teacher
returns which fires a TapIn call that moves the LightsOff time to
15 minutes past Finish. Thats all well and good if that was the
case. But what if the TapIn at 5:55pm was the next teacher
due to start at 6:00pm? If that is the case then the booking that is
needed is not the slot currently active, rather its the next slot.
The one from 6 to 7.

The point is; how are you going to know which slot to adjust?

Unless you get some indication of who is accessing the room,
you really have no way to even make an educated guess which
is the correct slot to adjust.

I'm thinking you are going to have to adjust them both.
What do you think?

LFS