X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fhandler_coke.c;h=21d61f68ca2bb5b663763cbaceb7ec941e14b229;hb=da639e3f13ee62627f4e7b29eccfcac70aad681d;hp=493212e38296f0b3ada78ccc63c1c79dfa0f1f21;hpb=650ed689f2d8c30fe9c448fbec0e58037cc2ce25;p=tpg%2Fopendispense2.git diff --git a/src/server/handler_coke.c b/src/server/handler_coke.c index 493212e..21d61f6 100644 --- a/src/server/handler_coke.c +++ b/src/server/handler_coke.c @@ -6,13 +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 === @@ -28,61 +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() { - giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR); - regcomp(&gCoke_StatusRegex, "^$", REG_EXTENDED); + 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; + } + + // Open modbus + modbus_new_tcp(gsCoke_ModbusAddress, 502); + if( !gCoke_Modbus ) + { + perror("coke - modbus_new_tcp"); + } + else + { + if( modbus_connect(gCoke_Modbus) ) + { + perror("coke - modbus_connect"); + } + } + return 0; } -int Coke_CanDispense(int User, int Item) +int Coke_CanDispense(int UNUSED(User), int Item) { - char tmp[32]; - regmatch_t matches[4]; - + uint8_t status; // Sanity please if( Item < 0 || Item > 6 ) return -1; - - // Ask the coke machine - sprintf(tmp, "s%i", 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); + // Check for 'dummy' mode + if( gbCoke_DummyMode ) + return 0; + + // Can't dispense if the machine is not connected + if( !gCoke_Modbus ) + return -2; - printf("s%i response '%s'\n", Item, tmp); + modbus_read_bits(gCoke_Modbus, ciCoke_StatusBitBase + Item, 1, &status); - 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]; - regmatch_t matches[4]; - // Sanity please if( Item < 0 || Item > 6 ) return -1; - // Dispense - sprintf(tmp, "d%i", 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); + // 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 (with locking) + modbus_write_bit(gCoke_Modbus, ciCoke_DropBitBase + Item, 1); - printf("d%i response '%s'\n", Item, tmp); - return 0; } -