X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=inline;f=src%2Fserver%2Fhandler_coke.c;h=fb58f799b337307d0964ba6a85b73f158b4d2931;hb=8fda01126c68844816bb53b615b11d29d61ca55e;hp=a34d88b9603f720114f877c1282ccc4cc56695bd;hpb=a024e27494af5e9273c18b77d5ad73b6d20223e2;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index a34d88b..fb58f79 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -53,13 +53,14 @@ 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() { CompileRegex(&gCoke_StatusRegex, "^slot\\s+([0-9]+)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED); - printf("connecting to coke machine...\n"); + printf("Connecting to coke machine on '%s'\n", gsCoke_SerialPort); giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600); if( giCoke_SerialFD == -1 ) { @@ -72,23 +73,26 @@ int Coke_InitHandler() // Reset the slot names. // - Dunno why this is needed, but the machine plays silly // sometimes. - Writef("n0 Slot0\n"); + Writef("n0 Slot0\r\n"); if( !WaitForColon() ) { - Writef("n1 Slot1\n"); + Writef("n1 Slot1\r\n"); WaitForColon(); - Writef("n2 Slot2\n"); + Writef("n2 Slot2\r\n"); WaitForColon(); - Writef("n3 Slot3\n"); + Writef("n3 Slot3\r\n"); WaitForColon(); - Writef("n4 Slot4\n"); + Writef("n4 Slot4\r\n"); WaitForColon(); - Writef("n5 Slot5\n"); + Writef("n5 Slot5\r\n"); + WaitForColon(); + Writef("n6 Coke\r\n"); WaitForColon(); - Writef("n6 Coke\n"); Coke_int_UpdateSlotStatuses(); } + else + fprintf(stderr, "Coke machine timed out.\n"); } AddPeriodicFunction(Coke_int_UpdateSlotStatuses); @@ -153,7 +157,8 @@ void Coke_int_UpdateSlotStatuses(void) for( i = 0; i <= 6; i ++ ) { - len = ReadLine(sizeof tmp, tmp); + // Read until non-blank line + while( (len = ReadLine(sizeof tmp, tmp)) == 0 ) ; if( len == -1 ) { TRACE("Read failed on slot %i\n", i); goto ret; // I give up :( @@ -199,6 +204,14 @@ 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 ) + { + printf("Wait %li seconds?\n", 10 - (time(NULL) - gtCoke_LastDispenseTime)); + sleep( 10 - (time(NULL) - gtCoke_LastDispenseTime) ); + printf("wait done\n"); + } // LOCK pthread_mutex_lock(&gCoke_Mutex); @@ -206,11 +219,6 @@ int Coke_DoDispense(int UNUSED(User), int Item) TRACE("flushing input\n"); - { - char buf[512]; - while( ReadLine(512, buf) != -1 ); - } - // Wait for prompt ret = 0; while( WaitForColon() && ret < 3 ) @@ -262,6 +270,7 @@ int Coke_DoDispense(int UNUSED(User), int Item) TRACE("Updating slot status\n"); // Update status + WaitForColon(); Writef("s%i\r\n", Item); len = ReadLine(sizeof tmp, tmp); if(len == -1) gaCoke_CachedStatus[Item] = -1; @@ -270,6 +279,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);