Initial move to common config code directory
[tpg/opendispense2.git] / src / server / handler_coke.c
index 6908aec..b9a9cf3 100644 (file)
@@ -11,6 +11,7 @@
  * - Remember, the coke machine echoes your text back to you!
  */
 #include "common.h"
+#include "../common/config.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
@@ -107,9 +108,9 @@ int Coke_DoDispense(int UNUSED(User), int Item)
        if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
        {
                 int    delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
-               printf("Wait %i seconds?\n", delay);
+               Debug_Debug("Waiting for %i seconds (rate limit)", delay);
                sleep( delay );
-               printf("wait done\n");
+               Debug_Debug("wait done");
        }
        gtCoke_LastDispenseTime = time(NULL);
 
@@ -133,8 +134,11 @@ int Coke_int_ConnectToPLC(void)
                        return 1;
                }
        }
-       printf("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
-       fprintf(stderr, "Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
+       else {
+               // Preven resource leaks
+               modbus_close(gCoke_Modbus);
+       }
+       Debug_Notice("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
        
        if( modbus_connect(gCoke_Modbus) )
        {
@@ -227,7 +231,8 @@ int Coke_int_DropSlot(int Slot)
        if( res == 0 )
        {
                // Oops!, no drink
-               printf("Drink dispense failed, bit lowered too quickly\n");
+               Log_Error("Drink dispense failed, bit lowered too quickly");
+               Debug_Notice("Drink dispense failed, bit lowered too quickly");
                return 1;
        }
        
@@ -255,6 +260,7 @@ int _WriteBit(int BitNum, uint8_t Value)
                return -1;
        if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )
                return 0;
+       // Error case
        if( Coke_int_ConnectToPLC() )
                return -1;
        if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )

UCC git Repository :: git.ucc.asn.au