Slight clean to coke code
[tpg/opendispense2.git] / src / server / handler_coke.c
index abd3506..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 ===
 
@@ -21,8 +31,6 @@
  int   Coke_InitHandler();
  int   Coke_CanDispense(int User, int Item);
  int   Coke_DoDispense(int User, int Item);
- int   WaitForColon();
- int   ReadLine(int len, char *output);
 
 // === GLOBALS ===
 tHandler       gCoke_Handler = {
@@ -31,167 +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()
 {
-       printf("connecting to coke machine...\n");
-       
-       giCoke_SerialFD = InitSerial(gsCoke_SerialPort, 9600);
-       if( giCoke_SerialFD == -1 ) {
-               fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
+       // 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;
        }
-       
-       CompileRegex(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
+
+       // 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];
-        int    ret;
-
+       uint8_t status;
        // Sanity please
-       if( Item < 0 || Item > 6 )      return -1;      // -EYOURBAD
-       
-       write(giCoke_SerialFD, "d7\r\n", 4);
-       write(giCoke_SerialFD, "d7\r\n", 4);
-       write(giCoke_SerialFD, "d7\r\n", 4);
-       
-       if( WaitForColon() ) {
-               fprintf(stderr, "Coke machine timed out\n");
-               return -2;      // -EMYBAD
-       }
-       
-       // Ask the coke machine
-       sprintf(tmp, "s%i\r\n", Item);
-       write(giCoke_SerialFD, tmp, 4);
+       if( Item < 0 || Item > 6 )      return -1;
 
-       ret = ReadLine(sizeof(tmp)-1, tmp);
-       printf("ret = %i, tmp = '%s'\n", ret, tmp);
+       // Check for 'dummy' mode
+       if( gbCoke_DummyMode )
+               return 0;
        
-       if( ret <= 0 ) {
-               fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret);
-               if( ret == -1 ) {
-                       perror("Coke Machine");
-               }
-               return -1;
-       }
-       ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response");
-       if( ret ) {
-               return -1;
-       }
-
-       tmp[ matches[3].rm_eo ] = '\0';
-       status = &tmp[ matches[3].rm_so ];
-
-       printf("Machine responded slot status '%s'\n", status);
+       // Can't dispense if the machine is not connected
+       if( !gCoke_Modbus )
+               return -2;
 
-       if( strcmp(status, "full") == 0 )
-               return 0;
+       if( modbus_read_bits(gCoke_Modbus, ciCoke_StatusBitBase + Item, 1, &status) )
+       {
+               // TODO: Check for a connection issue
+       }
 
-       return 1;
+       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;
 
-       WaitForColon();
-
-       // Dispense
-       sprintf(tmp, "d%i\r\n", Item);
-       write(giCoke_SerialFD, tmp, 4);
-       
-       WaitForColon();
-
-       // Get status
-       ReadLine(sizeof(tmp)-1, tmp);
+       // Check for 'dummy' mode
+       if( gbCoke_DummyMode )
+               return 0;
        
-       tmp[ matches[3].rm_eo ] = '\0';
-       status = &tmp[ matches[3].rm_so ];
-
-       printf("Machine responded slot status '%s'\n", status);
-
-       return 0;
-}
+       // Can't dispense if the machine is not connected
+       if( !gCoke_Modbus )
+               return -2;
 
-char ReadChar()
-{
-       fd_set  readfs;
-       char    ch = 0;
-        int    ret;
-       struct timeval  timeout;
-       
-       timeout.tv_sec = 5;     // 5 second timeout
-       timeout.tv_usec = 0;
-       
-       FD_ZERO(&readfs);
-       FD_SET(giCoke_SerialFD, &readfs);
-       
-       ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
-       if( ret == 0 )  return 0;       // Timeout
-       if( ret != 1 ) {
-               printf("readchar return %i\n", ret);
-               return 0;
+       // 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");
        }
        
-       ret = read(giCoke_SerialFD, &ch, 1);
-       if( ret != 1 ) {
-               printf("ret = %i\n", ret);
-               return 0;
+       // Dispense
+       if( modbus_write_bit(gCoke_Modbus, ciCoke_DropBitBase + Item, 1) )
+       {
+               // TODO: Handle connection issues
        }
        
-       return ch;
-}
-
-int WaitForColon()
-{
-       fd_set  readfs;
-       char    ch = 0;
-       
-       FD_SET(giCoke_SerialFD, &readfs);
-       
-       while( (ch = ReadChar()) != ':' && ch != 0);
-       
-       if( ch == 0 )   return -1;      // Timeout
-       
        return 0;
 }
 
-int ReadLine(int len, char *output)
-{
-       char    ch;
-        int    i = 0;
-       
-       for(;;)
-       {
-               ch = ReadChar();
-               
-               
-               if( i < len )
-                       output[i++] = ch;
-               
-               if( ch == '\0' ) {
-                       return -1;
-               }
-               if( ch == '\n' || ch == '\r' ) {
-                       if( i < len )
-                               output[--i] = '\0';
-                       return i;
-               }
-       }
-}
-
-

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