Slight clean to coke code
[tpg/opendispense2.git] / src / server / handler_coke.c
index 13f25c0..157f16d 100644 (file)
@@ -6,14 +6,24 @@
  *
  * This file is licenced under the 3-clause BSD Licence. See the file
  * COPYING for full details.
+ *
+ * NOTES:
+ * - Remember, the coke machine echoes your text back to you!
  */
 #include "common.h"
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
+#include <pthread.h>
 #include <unistd.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <regex.h>
+#include <modbus/modbus.h>
+
+#define MIN_DISPENSE_PERIOD    5
+
+// === CONSTANTS ===
+const int      ciCoke_MinPeriod = 5;
+const int      ciCoke_DropBitBase = 0;
+const int      ciCoke_StatusBitBase = 0;
 
 // === IMPORTS ===
 
@@ -29,70 +39,97 @@ tHandler    gCoke_Handler = {
        Coke_CanDispense,
        Coke_DoDispense
 };
-char   *gsCoke_SerialPort = "/dev/ttyS0";
- int   giCoke_SerialFD;
-regex_t        gCoke_StatusRegex;
+const char     *gsCoke_ModbusAddress = "130.95.13.73";
+modbus_t       *gCoke_Modbus;
+time_t gtCoke_LastDispenseTime;
+ int   gbCoke_DummyMode = 1;
 
 // == CODE ===
 int Coke_InitHandler()
 {
-       giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR);
-       regcomp(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
+       // 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;
+       }
+
+       // Open modbus
+       if( !gbCoke_DummyMode )
+       {
+               printf("Connecting to coke machine on '%s'\n", gsCoke_ModbusAddress);
+               
+               modbus_new_tcp(gsCoke_ModbusAddress, 502);
+               if( !gCoke_Modbus )
+               {
+                       perror("coke - modbus_new_tcp");
+               }
+               
+               if( gCoke_Modbus && modbus_connect(gCoke_Modbus) )
+               {
+                       perror("coke - modbus_connect");
+                       modbus_free(gCoke_Modbus);
+                       gCoke_Modbus = NULL;
+               }
+       }
+
        return 0;
 }
 
-int Coke_CanDispense(int User, int Item)
+int Coke_CanDispense(int UNUSED(User), int Item)
 {
-       char    tmp[32], *status;
-       regmatch_t      matches[4];
-
+       uint8_t status;
        // Sanity please
        if( Item < 0 || Item > 6 )      return -1;
-       
-       // Ask the coke machine
-       sprintf(tmp, "s%i\n", Item);
-       write(giCoke_SerialFD, tmp, 2);
-
-       // Read the response
-       read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
-       regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
 
-       tmp[ matches[3].rm_eo ] = '\0';
-       status = &tmp[ matches[3].rm_so ];
-
-       printf("Machine responded slot status '%s'\n", status);
+       // Check for 'dummy' mode
+       if( gbCoke_DummyMode )
+               return 0;
+       
+       // Can't dispense if the machine is not connected
+       if( !gCoke_Modbus )
+               return -2;
 
-       if( strcmp(status, "full") == 0 )
-               return 1;
+       if( modbus_read_bits(gCoke_Modbus, ciCoke_StatusBitBase + Item, 1, &status) )
+       {
+               // TODO: Check for a connection issue
+       }
 
-       return 0;
+       return status == 0;
 }
 
 /**
  * \brief Actually do a dispense from the coke machine
  */
-int Coke_DoDispense(int User, int Item)
+int Coke_DoDispense(int UNUSED(User), int Item)
 {
-       char    tmp[32], *status;
-       regmatch_t      matches[4];
-
        // Sanity please
        if( Item < 0 || Item > 6 )      return -1;
 
+       // Check for 'dummy' mode
+       if( gbCoke_DummyMode )
+               return 0;
+       
+       // Can't dispense if the machine is not connected
+       if( !gCoke_Modbus )
+               return -2;
+
+       // Make sure there are not two dispenses within n seconds
+       if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
+       {
+                int    delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
+               printf("Wait %i seconds?\n", delay);
+               sleep( delay );
+               printf("wait done\n");
+       }
+       
        // Dispense
-       sprintf(tmp, "d%i\n", Item);
-       write(giCoke_SerialFD, tmp, 2);
-
-       // Get status
-       read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
-       regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
+       if( modbus_write_bit(gCoke_Modbus, ciCoke_DropBitBase + Item, 1) )
+       {
+               // TODO: Handle connection issues
+       }
        
-       tmp[ matches[3].rm_eo ] = '\0';
-       status = &tmp[ matches[3].rm_so ];
-
-       printf("Machine responded slot status '%s'\n", status);
-
        return 0;
 }
 
-

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