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 gsCoke_ModbusPort = 502;
53 modbus_t *gCoke_Modbus;
54 time_t gtCoke_LastDispenseTime;
55 time_t gtCoke_LastReconnectTime;
56 int 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 if( Config_GetValueCount("coke_dummy_mode") > 0 )
66 gbCoke_DummyMode = Config_GetValue_Bool("coke_dummy_mode", 0);
67 if(gbCoke_DummyMode == -1) gbCoke_DummyMode = 0;
71 if( !gbCoke_DummyMode )
73 Coke_int_ConnectToPLC();
79 int Coke_CanDispense(int UNUSED(User), int Item)
83 // Check for 'dummy' mode
84 if( gbCoke_DummyMode )
88 slot = Coke_int_GetSlotFromItem(Item, 0);
89 if(slot < 0) return -1;
91 return Coke_int_IsSlotEmpty(slot);
95 * \brief Actually do a dispense from the coke machine
97 int Coke_DoDispense(int UNUSED(User), int Item)
100 // Check for 'dummy' mode
101 if( gbCoke_DummyMode )
105 slot = Coke_int_GetSlotFromItem(Item, 1);
106 if(slot < 0) return -1;
108 // Make sure there are not two dispenses within n seconds
109 if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
111 int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
112 Debug_Debug("Waiting for %i seconds (rate limit)", delay);
114 Debug_Debug("wait done");
116 gtCoke_LastDispenseTime = time(NULL);
118 return Coke_int_DropSlot(slot);
121 // --- INTERNAL FUNCTIONS ---
122 int Coke_int_ConnectToPLC(void)
125 if( time(NULL) - gtCoke_LastReconnectTime < COKE_RECONNECT_RATELIMIT )
130 gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, gsCoke_ModbusPort);
133 perror("coke - modbus_new_tcp");
134 gtCoke_LastReconnectTime = time(NULL);
139 // Preven resource leaks
140 modbus_close(gCoke_Modbus);
142 Debug_Notice("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
144 if( modbus_connect(gCoke_Modbus) )
146 gtCoke_LastReconnectTime = time(NULL);
147 perror("coke - modbus_connect");
148 modbus_free(gCoke_Modbus);
156 int Coke_int_GetSlotFromItem(int Item, int bDispensing)
158 if( Item < 0 || Item > 6 ) return -1;
164 // Iterate though coke slots and find the first one with a drink avaliable
165 // `giCoke_NextCokeSlot` ensures that the slots rotate
166 for( int i = 0; i < 4; i ++ )
168 int slot = 6 + (i + giCoke_NextCokeSlot) % 4;
169 if( !Coke_int_IsSlotEmpty(slot) )
172 giCoke_NextCokeSlot ++;
173 if(giCoke_NextCokeSlot == 4) giCoke_NextCokeSlot = 0;
175 return slot; // Drink avaliable
180 // - Return 6, even if it's empty, the checks elsewhere will avoid problems
184 int Coke_int_IsSlotEmpty(int Slot)
188 if( Slot < 0 || Slot > 9 ) return -1;
191 if( _ReadBit(ciCoke_StatusBitBase + Slot, &status) )
193 perror("Coke_int_IsSlotEmpty - modbus_read_bits");
200 int Coke_int_DropSlot(int Slot)
204 if(Slot < 0 || Slot > 9) return -1;
206 // Check if a dispense is in progress
207 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
209 perror("Coke_int_DropSlot - modbus_read_bits#1");
214 // Manual dispense in progress
219 if( _WriteBit(ciCoke_DropBitBase + Slot, 1) )
221 perror("Coke_int_DropSlot - modbus_write_bit");
225 // Check that it started
227 if( _ReadBit(ciCoke_DropBitBase + Slot, &res) )
229 perror("Coke_int_DropSlot - modbus_read_bits#2");
235 Log_Error("Drink dispense failed, bit lowered too quickly");
236 Debug_Notice("Drink dispense failed, bit lowered too quickly");
243 int _ReadBit(int BitNum, uint8_t *Value)
246 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
248 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
250 if( Coke_int_ConnectToPLC() )
252 if( modbus_read_bits( gCoke_Modbus, BitNum, 1, Value) >= 0 )
257 int _WriteBit(int BitNum, uint8_t Value)
260 if( !gCoke_Modbus && Coke_int_ConnectToPLC() )
262 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )
265 if( Coke_int_ConnectToPLC() )
267 if( modbus_write_bit( gCoke_Modbus, BitNum, Value != 0 ) >= 0 )