From c0baa7cdec5263d46f5d4eca02a6e517c5193872 Mon Sep 17 00:00:00 2001
From: John Hodge <tpg@heathred.ucc.asn.au>
Date: Mon, 28 Feb 2011 11:03:11 +0800
Subject: [PATCH] coke - Added a delay to coke dispense

---
 src/server/handler_coke.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c
index 867ac6a..8c82a07 100644
--- a/src/server/handler_coke.c
+++ b/src/server/handler_coke.c
@@ -53,6 +53,7 @@ char	*gsCoke_SerialPort = "/dev/ttyS0";
 regex_t	gCoke_StatusRegex;
  int	gaCoke_CachedStatus[7];
 pthread_mutex_t	gCoke_Mutex = PTHREAD_MUTEX_INITIALIZER;
+time_t	gtCoke_LastDispenseTime;
 
 // == CODE ===
 int Coke_InitHandler()
@@ -199,6 +200,12 @@ int Coke_DoDispense(int UNUSED(User), int Item)
 	// Can't dispense if the machine is not connected
 	if( giCoke_SerialFD == -1 )
 		return -2;
+
+	// Make sure there are not two dispenses within n seconds
+	if( time(NULL) - gtCoke_LastDispenseTime < 10 )
+	{
+		sleep( 10 - time(NULL) - gtCoke_LastDispenseTime );
+	}
 	
 	// LOCK
 	pthread_mutex_lock(&gCoke_Mutex);
@@ -266,6 +273,8 @@ int Coke_DoDispense(int UNUSED(User), int Item)
 		char buf[512];
 		read(giCoke_SerialFD, buf, 512);	// Flush
 	}
+
+	gtCoke_LastDispenseTime = time(NULL);
 	
 	// Release and return
 	pthread_mutex_unlock(&gCoke_Mutex);
-- 
GitLab