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 = {
52 const char *gsCoke_ModbusAddress = "130.95.13.73";
53 int giCoke_ModbusPort = 502;
54 bool gbCoke_DummyMode = false;
56 modbus_t *gCoke_Modbus;
57 time_t gtCoke_LastDispenseTime;
58 time_t gtCoke_LastReconnectTime;
59 int giCoke_NextCokeSlot = 0;
62 int Coke_InitHandler()
64 // Configuable dummy/blank mode (all dispenses succeed)
65 // TODO: Find a better way of handling missing/invalid options
66 Config_GetValue_Bool("coke_dummy_mode", &gbCoke_DummyMode);
69 if( !gbCoke_DummyMode )
71 Coke_int_ConnectToPLC();
77 int Coke_CanDispense(int UNUSED(User), int Item)
81 // Check for 'dummy' mode
82 if( gbCoke_DummyMode )
86 slot = Coke_int_GetSlotFromItem(Item, 0);
87 if(slot < 0) return -1;
89 return Coke_int_IsSlotEmpty(slot);
93 * \brief Actually do a dispense from the coke machine
95 int Coke_DoDispense(int UNUSED(User), int Item)
98 // Check for 'dummy' mode
99 if( gbCoke_DummyMode )
103 slot = Coke_int_GetSlotFromItem(Item, 1);
104 if(slot < 0) return -1;
106 // Make sure there are not two dispenses within n seconds
107 if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
109 int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
110 Debug_Debug("Waiting for %i seconds (rate limit)", delay);
112 Debug_Debug("wait done");
114 gtCoke_LastDispenseTime = time(NULL);
116 return Coke_int_DropSlot(slot);
119 // --- INTERNAL FUNCTIONS ---
120 int Coke_int_ConnectToPLC(void)
123 time_t elapsed = time(NULL) - gtCoke_LastReconnectTime;
124 if( elapsed < COKE_RECONNECT_RATELIMIT ) {
125 Debug_Notice("Not reconnecting, only %llis have pased, %i limit", (long long)elapsed, COKE_RECONNECT_RATELIMIT);
132 gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, giCoke_ModbusPort);
135 perror("coke - modbus_new_tcp");
136 gtCoke_LastReconnectTime = time(NULL);
141 // Preven resource leaks
142 modbus_close(gCoke_Modbus);
144 Debug_Notice("Connecting to coke PLC machine on '%s':%i", gsCoke_ModbusAddress, giCoke_ModbusPort);
146 if( modbus_connect(gCoke_Modbus) )
148 gtCoke_LastReconnectTime = time(NULL);
149 perror("coke - modbus_connect");
150 modbus_free(gCoke_Modbus);
158 int Coke_int_GetSlotFromItem(int Item, int bDispensing)
160 if( Item < 0 || Item > 6 ) return -1;
166 // Iterate though coke slots and find the first one with a drink avaliable
167 // `giCoke_NextCokeSlot` ensures that the slots rotate
168 for( int i = 0; i < 4; i ++ )
170 int slot = 6 + (i + giCoke_NextCokeSlot) % 4;
171 if( !Coke_int_IsSlotEmpty(slot) )
174 giCoke_NextCokeSlot ++;
175 if(giCoke_NextCokeSlot == 4) giCoke_NextCokeSlot = 0;
177 return slot; // Drink avaliable
182 // - Return 6, even if it's empty, the checks elsewhere will avoid problems
186 int Coke_int_IsSlotEmpty(int Slot)
190 if( Slot < 0 || Slot > 9 ) return -1;
193 if( _ReadBit(ciCoke_StatusBitBase + Slot, &status) )
195 perror("Coke_int_IsSlotEmpty - modbus_read_bits");
202 int Coke_int_DropSlot(int Slot)
206 if(Slot < 0 || Slot > 9) return -1;
208 // Check if a dispense is in progress
209 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
211 perror("Coke_int_DropSlot - modbus_read_bits#1");
216 // Manual dispense in progress
221 if( _WriteBit(ciCoke_DropBitBase + Slot, 1) )
223 perror("Coke_int_DropSlot - modbus_write_bit");
227 // Check that it started
229 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
231 perror("Coke_int_DropSlot - modbus_read_bits#2");
237 Log_Error("Drink dispense failed, bit lowered too quickly");
238 Debug_Notice("Drink dispense failed, bit lowered too quickly");
245 int _ReadBit(int BitNum, uint8_t *Value)
248 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
250 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
252 if( Coke_int_ConnectToPLC() )
254 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
259 int _WriteBit(int BitNum, uint8_t Value)
262 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
264 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )
267 if( Coke_int_ConnectToPLC() )
269 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )