X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fhandler_coke.c;h=21d61f68ca2bb5b663763cbaceb7ec941e14b229;hb=da639e3f13ee62627f4e7b29eccfcac70aad681d;hp=c937756583c9bac0a7bbf1f9b65d03aface5d3f2;hpb=3e7700e1b9b652bb3ed7daaff76cf891907a63b3;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index c937756..21d61f6 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -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 #include +#include +#include #include -#include -#include -#include +#include + +#define MIN_DISPENSE_PERIOD 5 + +// === CONSTANTS === +const int ciCoke_MinPeriod = 5; +const int ciCoke_DropBitBase = 0; +const int ciCoke_StatusBitBase = 0; // === IMPORTS === @@ -29,92 +39,88 @@ 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); + printf("Connecting to coke machine on '%s'\n", gsCoke_ModbusAddress); + + // 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); - return 0; -} -int Coke_CanDispense(int User, int Item) -{ - char tmp[32], *status; - regmatch_t matches[4]; - int ret; - - // Sanity please - if( Item < 0 || Item > 6 ) return -1; // -EYOURBAD - - // Ask the coke machine - sprintf(tmp, "s%i\n", Item); - write(giCoke_SerialFD, tmp, 2); - - // Wait a little - sleep(250); - - // Read the response - tmp[0] = '\0'; - ret = read(giCoke_SerialFD, tmp, sizeof(tmp)-1); - //printf("ret = %i\n", ret); - if( ret <= 0 ) { - fprintf(stderr, "Coke machine is not being chatty (read = %i)\n", ret); - return -1; + // Open modbus + modbus_new_tcp(gsCoke_ModbusAddress, 502); + if( !gCoke_Modbus ) + { + perror("coke - modbus_new_tcp"); } - ret = RunRegex(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, "Bad Response"); - if( ret ) { - return -1; + else + { + if( modbus_connect(gCoke_Modbus) ) + { + perror("coke - modbus_connect"); + } } - tmp[ matches[3].rm_eo ] = '\0'; - status = &tmp[ matches[3].rm_so ]; + return 0; +} - printf("Machine responded slot status '%s'\n", status); +int Coke_CanDispense(int UNUSED(User), int Item) +{ + uint8_t status; + // Sanity please + if( Item < 0 || Item > 6 ) return -1; - if( strcmp(status, "full") == 0 ) + // Check for 'dummy' mode + if( gbCoke_DummyMode ) return 0; + + // Can't dispense if the machine is not connected + if( !gCoke_Modbus ) + return -2; - return 1; + modbus_read_bits(gCoke_Modbus, ciCoke_StatusBitBase + Item, 1, &status); + + 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; - // Dispense - sprintf(tmp, "d%i\n", Item); - write(giCoke_SerialFD, tmp, 2); + // Check for 'dummy' mode + if( gbCoke_DummyMode ) + return 0; - // Wait a little - sleep(250); - - // Get status - read(giCoke_SerialFD, tmp, sizeof(tmp)-1); - regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 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 (with locking) + modbus_write_bit(gCoke_Modbus, ciCoke_DropBitBase + Item, 1); - tmp[ matches[3].rm_eo ] = '\0'; - status = &tmp[ matches[3].rm_so ]; - - printf("Machine responded slot status '%s'\n", status); - return 0; } -