(bugfix) Insufficient warning level to catch that bug
[tpg/opendispense2.git] / src / server / handler_coke.c
index 850c07b..b499387 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>
@@ -19,7 +20,7 @@
 #include <modbus/modbus.h>
 #include <errno.h>
 
-#define MIN_DISPENSE_PERIOD    5
+#define MIN_DISPENSE_PERIOD    2
 #define COKE_RECONNECT_RATELIMIT       2
 
 // === CONSTANTS ===
@@ -48,10 +49,11 @@ tHandler    gCoke_Handler = {
        Coke_DoDispense
 };
 const char     *gsCoke_ModbusAddress = "130.95.13.73";
+ int           giCoke_ModbusPort = 502;
 modbus_t       *gCoke_Modbus;
 time_t gtCoke_LastDispenseTime;
 time_t gtCoke_LastReconnectTime;
- int   gbCoke_DummyMode = 1;
+bool   gbCoke_DummyMode = 1;
  int   giCoke_NextCokeSlot = 0;
 
 // == CODE ===
@@ -59,11 +61,7 @@ int Coke_InitHandler()
 {
        // Configuable dummy/blank mode (all dispenses succeed)
        // TODO: Find a better way of handling missing/invalid options
-       if( Config_GetValueCount("coke_dummy_mode") > 0 )
-       {
-               gbCoke_DummyMode = Config_GetValue_Bool("coke_dummy_mode", 0);
-               if(gbCoke_DummyMode == -1)      gbCoke_DummyMode = 0;
-       }
+       Config_GetValue_Bool("coke_dummy_mode", &gbCoke_DummyMode);
 
        // Open modbus
        if( !gbCoke_DummyMode )
@@ -107,9 +105,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);
 
@@ -125,7 +123,7 @@ int Coke_int_ConnectToPLC(void)
 
        if( !gCoke_Modbus )
        {
-               gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, 502);
+               gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, giCoke_ModbusPort);
                if( !gCoke_Modbus )
                {
                        perror("coke - modbus_new_tcp");
@@ -133,8 +131,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 +228,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 +257,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