+ // Get slot
+ slot = Coke_int_GetSlotFromItem(Item, 1);
+ if(slot < 0) return -1;
+
+ // 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);
+ Debug_Debug("Waiting for %i seconds (rate limit)", delay);
+ sleep( delay );
+ Debug_Debug("wait done");
+ }
+ gtCoke_LastDispenseTime = time(NULL);
+
+ return Coke_int_DropSlot(slot);
+}
+
+// --- INTERNAL FUNCTIONS ---
+int Coke_int_ConnectToPLC(void)
+{
+ // Ratelimit
+ if( time(NULL) - gtCoke_LastReconnectTime < COKE_RECONNECT_RATELIMIT )
+ return -1;
+
+ if( !gCoke_Modbus )
+ {
+ gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, 502);
+ if( !gCoke_Modbus )
+ {
+ perror("coke - modbus_new_tcp");
+ gtCoke_LastReconnectTime = time(NULL);
+ return 1;
+ }
+ }
+ else {
+ // Preven resource leaks
+ modbus_close(gCoke_Modbus);
+ }
+ Debug_Notice("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
+
+ if( modbus_connect(gCoke_Modbus) )
+ {
+ gtCoke_LastReconnectTime = time(NULL);
+ perror("coke - modbus_connect");
+ modbus_free(gCoke_Modbus);
+ gCoke_Modbus = NULL;