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!
19 #include <modbus/modbus.h>
22 #define MIN_DISPENSE_PERIOD 5
25 const int ciCoke_MinPeriod = 5;
26 const int ciCoke_DropBitBase = 1024;
27 const int ciCoke_StatusBitBase = 16;
32 int Coke_InitHandler();
33 int Coke_CanDispense(int User, int Item);
34 int Coke_DoDispense(int User, int Item);
35 int Coke_int_ConnectToPLC(void);
36 int Coke_int_GetSlotFromItem(int Item, int bDispensing);
37 int Coke_int_IsSlotEmpty(int Slot);
38 int Coke_int_DropSlot(int Slot);
41 tHandler gCoke_Handler = {
47 const char *gsCoke_ModbusAddress = "130.95.13.73";
48 modbus_t *gCoke_Modbus;
49 time_t gtCoke_LastDispenseTime;
50 int gbCoke_DummyMode = 1;
51 int giCoke_NextCokeSlot = 0;
54 int Coke_InitHandler()
56 // Configuable dummy/blank mode (all dispenses succeed)
57 // TODO: Find a better way of handling missing/invalid options
58 if( Config_GetValueCount("coke_dummy_mode") > 0 )
60 gbCoke_DummyMode = Config_GetValue_Bool("coke_dummy_mode", 0);
61 if(gbCoke_DummyMode == -1) gbCoke_DummyMode = 0;
65 if( !gbCoke_DummyMode )
67 Coke_int_ConnectToPLC();
73 int Coke_CanDispense(int UNUSED(User), int Item)
77 // Check for 'dummy' mode
78 if( gbCoke_DummyMode )
82 slot = Coke_int_GetSlotFromItem(Item, 0);
83 if(slot < 0) return -1;
85 return Coke_int_IsSlotEmpty(slot);
89 * \brief Actually do a dispense from the coke machine
91 int Coke_DoDispense(int UNUSED(User), int Item)
94 // Check for 'dummy' mode
95 if( gbCoke_DummyMode )
99 slot = Coke_int_GetSlotFromItem(Item, 1);
100 if(slot < 0) return -1;
102 // Make sure there are not two dispenses within n seconds
103 if( time(NULL) - gtCoke_LastDispenseTime < ciCoke_MinPeriod )
105 int delay = ciCoke_MinPeriod - (time(NULL) - gtCoke_LastDispenseTime);
106 printf("Wait %i seconds?\n", delay);
108 printf("wait done\n");
111 return Coke_int_DropSlot(slot);
114 // --- INTERNAL FUNCTIONS ---
115 int Coke_int_ConnectToPLC(void)
119 gCoke_Modbus = modbus_new_tcp(gsCoke_ModbusAddress, 502);
122 perror("coke - modbus_new_tcp");
126 printf("Connecting to coke PLC machine on '%s'\n", gsCoke_ModbusAddress);
128 if( modbus_connect(gCoke_Modbus) )
130 perror("coke - modbus_connect");
131 modbus_free(gCoke_Modbus);
139 int Coke_int_GetSlotFromItem(int Item, int bDispensing)
141 if( Item < 0 || Item > 6 ) return -1;
147 // Iterate though coke slots and find the first one with a drink avaliable
148 // `giCoke_NextCokeSlot` ensures that the slots rotate
149 for( int i = 0; i < 4; i ++ )
151 int slot = 6 + (i + giCoke_NextCokeSlot) % 4;
152 if( !Coke_int_IsSlotEmpty(slot) )
155 giCoke_NextCokeSlot ++;
156 if(giCoke_NextCokeSlot == 4) giCoke_NextCokeSlot = 0;
158 return slot; // Drink avaliable
163 // - Return 6, even if it's empty, the checks elsewhere will avoid problems
167 int Coke_int_IsSlotEmpty(int Slot)
171 if( Slot < 0 || Slot > 9 ) return -1;
177 if( modbus_read_bits(gCoke_Modbus, ciCoke_StatusBitBase + Slot, 1, &status) <= 0 )
179 // TODO: Retry connection, or fail
180 perror("Coke_int_IsSlotEmpty - modbus_read_bits");
187 int Coke_int_DropSlot(int Slot)
191 if(Slot < 0 || Slot > 9) return -1;
193 // Can't dispense if the machine is not connected
197 // Check if a dispense is in progress
198 if( modbus_read_bits(gCoke_Modbus, ciCoke_DropBitBase + Slot, 1, &res) <= 0 )
200 perror("Coke_int_DropSlot - modbus_read_bits#1");
205 // Manual dispense in progress
210 if( modbus_write_bit(gCoke_Modbus, ciCoke_DropBitBase + Slot, 1) < 0 )
212 perror("Coke_int_DropSlot - modbus_write_bit");
216 // Check that it started
218 if( modbus_read_bits(gCoke_Modbus, ciCoke_DropBitBase + Slot, 1, &res) <= 0 )
220 perror("Coke_int_DropSlot - modbus_read_bits#2");
226 printf("Drink dispense failed, bit lowered too quickly\n");