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 modbus_t *gCoke_Modbus;
53 time_t gtCoke_LastDispenseTime;
54 time_t gtCoke_LastReconnectTime;
55 int gbCoke_DummyMode = 1;
56 int giCoke_NextCokeSlot = 0;
59 int Coke_InitHandler()
61 // Configuable dummy/blank mode (all dispenses succeed)
62 // TODO: Find a better way of handling missing/invalid options
63 if( Config_GetValueCount("coke_dummy_mode") > 0 )
65 gbCoke_DummyMode = Config_GetValue_Bool("coke_dummy_mode", 0);
66 if(gbCoke_DummyMode == -1) gbCoke_DummyMode = 0;
70 if( !gbCoke_DummyMode )
72 Coke_int_ConnectToPLC();
78 int Coke_CanDispense(int UNUSED(User), int Item)
82 // Check for 'dummy' mode
83 if( gbCoke_DummyMode )
87 slot = Coke_int_GetSlotFromItem(Item, 0);
88 if(slot < 0) return -1;
90 return Coke_int_IsSlotEmpty(slot);
94 * \brief Actually do a dispense from the coke machine
96 int Coke_DoDispense(int UNUSED(User), int Item)
99 // Check for 'dummy' mode
100 if( gbCoke_DummyMode )
104 slot = Coke_int_GetSlotFromItem(Item, 1);
105 if(slot < 0) return -1;
107 // Make sure there are not two dispenses within n seconds
108 if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
110 int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
111 Debug_Debug("Waiting for %i seconds (rate limit)", delay);
113 Debug_Debug("wait done");
115 gtCoke_LastDispenseTime = time(NULL);
117 return Coke_int_DropSlot(slot);
120 // --- INTERNAL FUNCTIONS ---
121 int Coke_int_ConnectToPLC(void)
124 if( time(NULL) - gtCoke_LastReconnectTime < COKE_RECONNECT_RATELIMIT )
129 gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, 502);
132 perror("coke - modbus_new_tcp");
133 gtCoke_LastReconnectTime = time(NULL);
138 // Preven resource leaks
139 modbus_close(gCoke_Modbus);
141 Debug_Notice("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
143 if( modbus_connect(gCoke_Modbus) )
145 gtCoke_LastReconnectTime = time(NULL);
146 perror("coke - modbus_connect");
147 modbus_free(gCoke_Modbus);
155 int Coke_int_GetSlotFromItem(int Item, int bDispensing)
157 if( Item < 0 || Item > 6 ) return -1;
163 // Iterate though coke slots and find the first one with a drink avaliable
164 // `giCoke_NextCokeSlot` ensures that the slots rotate
165 for( int i = 0; i < 4; i ++ )
167 int slot = 6 + (i + giCoke_NextCokeSlot) % 4;
168 if( !Coke_int_IsSlotEmpty(slot) )
171 giCoke_NextCokeSlot ++;
172 if(giCoke_NextCokeSlot == 4) giCoke_NextCokeSlot = 0;
174 return slot; // Drink avaliable
179 // - Return 6, even if it's empty, the checks elsewhere will avoid problems
183 int Coke_int_IsSlotEmpty(int Slot)
187 if( Slot < 0 || Slot > 9 ) return -1;
190 if( _ReadBit(ciCoke_StatusBitBase + Slot, &status) )
192 perror("Coke_int_IsSlotEmpty - modbus_read_bits");
199 int Coke_int_DropSlot(int Slot)
203 if(Slot < 0 || Slot > 9) return -1;
205 // Check if a dispense is in progress
206 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
208 perror("Coke_int_DropSlot - modbus_read_bits#1");
213 // Manual dispense in progress
218 if( _WriteBit(ciCoke_DropBitBase + Slot, 1) )
220 perror("Coke_int_DropSlot - modbus_write_bit");
224 // Check that it started
226 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
228 perror("Coke_int_DropSlot - modbus_read_bits#2");
234 Log_Error("Drink dispense failed, bit lowered too quickly");
235 Debug_Notice("Drink dispense failed, bit lowered too quickly");
242 int _ReadBit(int BitNum, uint8_t *Value)
245 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
247 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
249 if( Coke_int_ConnectToPLC() )
251 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
256 int _WriteBit(int BitNum, uint8_t Value)
259 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
261 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )
264 if( Coke_int_ConnectToPLC() )
266 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )