Skip to content
This repository was archived by the owner on Jun 8, 2020. It is now read-only.

Commit

Permalink
Reset order timers when reaching floor
Browse files Browse the repository at this point in the history
  • Loading branch information
andersliland committed Mar 13, 2017
1 parent 7e4add2 commit 6775240
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 30 deletions.
30 changes: 20 additions & 10 deletions control/eventManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func eventManager(
var direction int

r := rand.New(rand.NewSource(time.Now().UnixNano()))
var orderTimeout = 5*time.Second + time.Duration(r.Intn(2000))*time.Millisecond // random timeout to prevent all elevator from timing out at the same time
var orderTimeout = OrderTimeout*time.Second + time.Duration(r.Intn(2000))*time.Millisecond // random timeout to prevent all elevator from timing out at the same time

// if restore order from file do ..., else:
const pollDelay = 5 * time.Millisecond
Expand Down Expand Up @@ -73,6 +73,12 @@ func eventManager(
}
default: // Insert error handling
}

// Reset order if a user is spamming button
if ElevatorStatus[localIP].CabOrders[floor] && direction != Stop {
resetTimerForAllAssignedOrders(floor, direction, orderTimeout, localIP)
}

case floor = <-floorReached:
//log.Println("floorReached state: " + StateEventManager[state])
syncFloor(floor, localIP, broadcastBackupChannel)
Expand Down Expand Up @@ -108,16 +114,8 @@ func eventManager(
} else {
motorChannel <- direction // Is this necessary?
state = syncState(Moving, localIP, broadcastBackupChannel)
resetTimerForAllAssignedOrders(floor, direction, orderTimeout, localIP)

// reset timer for all order AssignetTo == localIP
for f := floor + direction; f < NumFloors && f >= Floor1; f += direction {
for k := ButtonCallUp; k <= ButtonCallDown; k++ {
if HallOrderMatrix[f][k].AssignedTo == localIP {
HallOrderMatrix[f][k].Timer.Reset(orderTimeout)
log.Println("Reset timer on order" + ButtonType[k] + " at floor " + strconv.Itoa(f+1))
}
}
}
}
default: // Insert error handling here - elevator might possibly need to be restarted ()
}
Expand Down Expand Up @@ -164,6 +162,18 @@ func syncState(state int, localIP string, broadcastBackupChannel chan<- BackupMe
return state
}

func resetTimerForAllAssignedOrders(floor, direction int, orderTimeout time.Duration, localIP string) {
// reset timer for all order AssignetTo == localIP
for f := floor + direction; f < NumFloors && f >= Floor1; f += direction {
for k := ButtonCallUp; k <= ButtonCallDown; k++ {
if HallOrderMatrix[f][k].AssignedTo == localIP {
HallOrderMatrix[f][k].Timer.Reset(orderTimeout)
log.Println("Reset timer on order" + ButtonType[k] + " at floor " + strconv.Itoa(f+1))
}
}
}
}

func printEventManager(s string) {
if debugEventManager {
log.Println("[eventManager]\t", s)
Expand Down
38 changes: 19 additions & 19 deletions control/systemControl.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SystemControl(
const watchdogKickTime = 100 * time.Millisecond
const watchdogLimit = 3*watchdogKickTime + 10*time.Millisecond
const ackTimeLimit = 500 * time.Millisecond
var orderTimeout = 5*time.Second + time.Duration(r.Intn(2000))*time.Millisecond // random timeout to prevent all elevator from timing out at the same time
var orderTimeout = OrderTimeout*time.Second + time.Duration(r.Intn(2000))*time.Millisecond // random timeout to prevent all elevator from timing out at the same time

// Timers
watchdogTimer := time.NewTicker(watchdogLimit)
Expand Down Expand Up @@ -165,9 +165,9 @@ func SystemControl(
}

if order.OriginIP == localIP {
printSystemControl("Starting ack timer [EventNewOrder] on order " + ButtonType[order.ButtonType] + " on floor " + strconv.Itoa(order.Floor+1))
printSystemControl("Starting ack timer on new order " + ButtonType[order.ButtonType] + " on floor " + strconv.Itoa(order.Floor+1))
HallOrderMatrix[order.Floor][order.ButtonType].Timer = time.AfterFunc(ackTimeLimit, func() {
log.Println("[systemControl]\t newOrder at floor " + strconv.Itoa(order.Floor+1) + " for " + ButtonType[order.ButtonType] + " not ACK'ed by all ")
log.Println("[systemControl]\t ACK-TIMEOUT\t newOrder at floor " + strconv.Itoa(order.Floor+1) + " for " + ButtonType[order.ButtonType] + " not ACK'ed by all ")
timeoutChannel <- ExtendedHallOrder{
Floor: order.Floor,
ButtonType: order.ButtonType,
Expand Down Expand Up @@ -251,23 +251,23 @@ func SystemControl(
if order.AssignedTo != localIP {
timeout = 2 * orderTimeout
}
if HallOrderMatrix[order.Floor][order.ButtonType].Status == UnderExecution {
log.Println("[systemConrtol]\t OriginIP start execution timer [EventOrderConfirmed] on order "+ButtonType[order.ButtonType]+" on floor "+strconv.Itoa(order.Floor+1)+" Timer: ", HallOrderMatrix[order.Floor][order.ButtonType].Timer)
HallOrderMatrix[order.Floor][order.ButtonType].Timer = time.AfterFunc(timeout, func() {
log.Println("Timeout\t\t orderUnderExecution - Elevator could not execute order (OriginIP == localIP)")
timeoutChannel <- ExtendedHallOrder{
Floor: order.Floor,
ButtonType: order.ButtonType,
OriginIP: order.OriginIP,
TimeoutState: TimeoutOrderExecution,
Order: HallOrder{
AssignedTo: order.AssignedTo,
},
}
})
}

log.Println("[systemConrtol]\t OriginIP start execution timer [EventOrderConfirmed] on order "+ButtonType[order.ButtonType]+" on floor "+strconv.Itoa(order.Floor+1)+" Timer: ", HallOrderMatrix[order.Floor][order.ButtonType].Timer)
HallOrderMatrix[order.Floor][order.ButtonType].Timer = time.AfterFunc(timeout, func() {
log.Println("Timeout\t\t orderUnderExecution - Elevator could not execute order (OriginIP == localIP)")
timeoutChannel <- ExtendedHallOrder{
Floor: order.Floor,
ButtonType: order.ButtonType,
OriginIP: order.OriginIP,
TimeoutState: TimeoutOrderExecution,
Order: HallOrder{
AssignedTo: order.AssignedTo,
},
}
})
}
}

case EventOrderCompleted:
// This case is only sent from the eventManager after it detects that an order is completed.
printSystemControl("case: EventOrderCompleted at floor " + strconv.Itoa(order.Floor+1) + " for " + ButtonType[order.ButtonType] + " for " + order.AssignedTo)
Expand All @@ -289,7 +289,7 @@ func SystemControl(

if order.AssignedTo == localIP {
HallOrderMatrix[order.Floor][order.ButtonType].Timer = time.AfterFunc(ackTimeLimit, func() {
log.Println("[systemControl]\t orderCompleted at floor " + strconv.Itoa(order.Floor+1) + " for " + ButtonType[order.ButtonType] + " not ACK'ed by all ")
log.Println("[systemControl]\t ACK-TIMEOUT\t orderCompleted at floor " + strconv.Itoa(order.Floor+1) + " for " + ButtonType[order.ButtonType] + " not ACK'ed by all ")
broadcastOrderChannel <- OrderMessage{ // Should we send to timeoutChannel - or just resend OrderMessage?
Floor: order.Floor,
ButtonType: order.ButtonType,
Expand Down
2 changes: 1 addition & 1 deletion simulator/simulatorCore/simulatorDef.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package simulatorCore
//---------------SIMULATOR CONFIGURATION PARAMETERS--------------
const DistancePassingFloors = 1820000
const DistanceBetweenFloors = 4200000
const TravelTimeBetweenFloors_ms = 1500 * 2
const TravelTimeBetweenFloors_ms = 2000
const TravelTimePassingFloor_ms = 1000
const BtnDepressedTime_ms = 200

Expand Down
1 change: 1 addition & 0 deletions utilities/typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = false
const NumElevators = 3
const NumButtons = 3
const NumFloors = 4
const OrderTimeout = 14 //seconds

var HallOrderMatrix [NumFloors][2]HallOrder
var ElevatorStatus = make(map[string]*Elevator) // containing last known state
Expand Down

0 comments on commit 6775240

Please sign in to comment.