3 * UCC (University [of WA] Computer Club) Electronic Accounting System
5 * handler_coke.c - Coke controller code
7 * This file is licenced under the 3-clause BSD Licence. See the file
8 * COPYING for full details.
11 * - Remember, the coke machine echoes your text back to you!
14 #include "../common/config.h"
20 #include <modbus/modbus.h>
23 #define MIN_DISPENSE_PERIOD 2
24 #define COKE_RECONNECT_RATELIMIT 2
27 const int ciCoke_MinPeriod = 5;
28 const int ciCoke_DropBitBase = 1024;
29 const int ciCoke_StatusBitBase = 16;
34 int Coke_InitHandler();
35 int Coke_CanDispense(int User, int Item);
36 int Coke_DoDispense(int User, int Item);
37 int Coke_int_ConnectToPLC(void);
38 int Coke_int_GetSlotFromItem(int Item, int bDispensing);
39 int Coke_int_IsSlotEmpty(int Slot);
40 int Coke_int_DropSlot(int Slot);
41 static int _ReadBit(int BitNum, uint8_t *Value);
42 static int _WriteBit(int BitNum, uint8_t Value);
45 tHandler gCoke_Handler = {
51 const char *gsCoke_ModbusAddress = "130.95.13.73";
52 int giCoke_ModbusPort = 502;
53 modbus_t *gCoke_Modbus;
54 time_t gtCoke_LastDispenseTime;
55 time_t gtCoke_LastReconnectTime;
56 bool gbCoke_DummyMode = 1;
57 int giCoke_NextCokeSlot = 0;
60 int Coke_InitHandler()
62 // Configuable dummy/blank mode (all dispenses succeed)
63 // TODO: Find a better way of handling missing/invalid options
64 Config_GetValue_Bool("coke_dummy_mode", &gbCoke_DummyMode);
67 if( !gbCoke_DummyMode )
69 Coke_int_ConnectToPLC();
75 int Coke_CanDispense(int UNUSED(User), int Item)
79 // Check for 'dummy' mode
80 if( gbCoke_DummyMode )
84 slot = Coke_int_GetSlotFromItem(Item, 0);
85 if(slot < 0) return -1;
87 return Coke_int_IsSlotEmpty(slot);
91 * \brief Actually do a dispense from the coke machine
93 int Coke_DoDispense(int UNUSED(User), int Item)
96 // Check for 'dummy' mode
97 if( gbCoke_DummyMode )
101 slot = Coke_int_GetSlotFromItem(Item, 1);
102 if(slot < 0) return -1;
104 // Make sure there are not two dispenses within n seconds
105 if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
107 int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
108 Debug_Debug("Waiting for %i seconds (rate limit)", delay);
110 Debug_Debug("wait done");
112 gtCoke_LastDispenseTime = time(NULL);
114 return Coke_int_DropSlot(slot);
117 // --- INTERNAL FUNCTIONS ---
118 int Coke_int_ConnectToPLC(void)
121 if( time(NULL) - gtCoke_LastReconnectTime < COKE_RECONNECT_RATELIMIT )
126 gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, giCoke_ModbusPort);
129 perror("coke - modbus_new_tcp");
130 gtCoke_LastReconnectTime = time(NULL);
135 // Preven resource leaks
136 modbus_close(gCoke_Modbus);
138 Debug_Notice("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
140 if( modbus_connect(gCoke_Modbus) )
142 gtCoke_LastReconnectTime = time(NULL);
143 perror("coke - modbus_connect");
144 modbus_free(gCoke_Modbus);
152 int Coke_int_GetSlotFromItem(int Item, int bDispensing)
154 if( Item < 0 || Item > 6 ) return -1;
160 // Iterate though coke slots and find the first one with a drink avaliable
161 // `giCoke_NextCokeSlot` ensures that the slots rotate
162 for( int i = 0; i < 4; i ++ )
164 int slot = 6 + (i + giCoke_NextCokeSlot) % 4;
165 if( !Coke_int_IsSlotEmpty(slot) )
168 giCoke_NextCokeSlot ++;
169 if(giCoke_NextCokeSlot == 4) giCoke_NextCokeSlot = 0;
171 return slot; // Drink avaliable
176 // - Return 6, even if it's empty, the checks elsewhere will avoid problems
180 int Coke_int_IsSlotEmpty(int Slot)
184 if( Slot < 0 || Slot > 9 ) return -1;
187 if( _ReadBit(ciCoke_StatusBitBase + Slot, &status) )
189 perror("Coke_int_IsSlotEmpty - modbus_read_bits");
196 int Coke_int_DropSlot(int Slot)
200 if(Slot < 0 || Slot > 9) return -1;
202 // Check if a dispense is in progress
203 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
205 perror("Coke_int_DropSlot - modbus_read_bits#1");
210 // Manual dispense in progress
215 if( _WriteBit(ciCoke_DropBitBase + Slot, 1) )
217 perror("Coke_int_DropSlot - modbus_write_bit");
221 // Check that it started
223 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
225 perror("Coke_int_DropSlot - modbus_read_bits#2");
231 Log_Error("Drink dispense failed, bit lowered too quickly");
232 Debug_Notice("Drink dispense failed, bit lowered too quickly");
239 int _ReadBit(int BitNum, uint8_t *Value)
242 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
244 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
246 if( Coke_int_ConnectToPLC() )
248 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
253 int _WriteBit(int BitNum, uint8_t Value)
256 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
258 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )
261 if( Coke_int_ConnectToPLC() )
263 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )