*
* 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 ===
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 = open(gsCoke_SerialPort, O_RDWR);
- 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;
+ }
+
+ // 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");
+ }
}
- regcomp(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
+
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;
+ 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], *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);
-
- // 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);
- tmp[ matches[3].rm_eo ] = '\0';
- status = &tmp[ matches[3].rm_so ];
-
- printf("Machine responded slot status '%s'\n", status);
-
return 0;
}
-