Snack handler added
[tpg/opendispense2.git] / src / server / handler_coke.c
1 /*
2  * OpenDispense 2 
3  * UCC (University [of WA] Computer Club) Electronic Accounting System
4  *
5  * handler_coke.c - Coke controller code
6  *
7  * This file is licenced under the 3-clause BSD Licence. See the file
8  * COPYING for full details.
9  */
10 #include "common.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <regex.h>
17
18 // === IMPORTS ===
19
20 // === PROTOTYPES ===
21  int    Coke_InitHandler();
22  int    Coke_CanDispense(int User, int Item);
23  int    Coke_DoDispense(int User, int Item);
24
25 // === GLOBALS ===
26 tHandler        gCoke_Handler = {
27         "coke",
28         Coke_InitHandler,
29         Coke_CanDispense,
30         Coke_DoDispense
31 };
32 char    *gsCoke_SerialPort = "/dev/ttyS0";
33  int    giCoke_SerialFD;
34 regex_t gCoke_StatusRegex;
35
36 // == CODE ===
37 int Coke_InitHandler()
38 {
39         printf("connecting to coke machine...\n");
40         giCoke_SerialFD = open(gsCoke_SerialPort, O_RDWR);
41         if( giCoke_SerialFD == -1 ) {
42                 fprintf(stderr, "ERROR: Unable to open coke serial port ('%s')\n", gsCoke_SerialPort);
43         }
44         regcomp(&gCoke_StatusRegex, "^slot\\s+(\\d)\\s+([^:]+):([a-zA-Z]+)\\s*", REG_EXTENDED);
45         return 0;
46 }
47
48 int Coke_CanDispense(int User, int Item)
49 {
50         char    tmp[32], *status;
51         regmatch_t      matches[4];
52
53         // Sanity please
54         if( Item < 0 || Item > 6 )      return -1;
55         
56         // Ask the coke machine
57         sprintf(tmp, "s%i\n", Item);
58         write(giCoke_SerialFD, tmp, 2);
59
60         // Read the response
61         read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
62         regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
63
64         tmp[ matches[3].rm_eo ] = '\0';
65         status = &tmp[ matches[3].rm_so ];
66
67         printf("Machine responded slot status '%s'\n", status);
68
69         if( strcmp(status, "full") == 0 )
70                 return 1;
71
72         return 0;
73 }
74
75 /**
76  * \brief Actually do a dispense from the coke machine
77  */
78 int Coke_DoDispense(int User, int Item)
79 {
80         char    tmp[32], *status;
81         regmatch_t      matches[4];
82
83         // Sanity please
84         if( Item < 0 || Item > 6 )      return -1;
85
86         // Dispense
87         sprintf(tmp, "d%i\n", Item);
88         write(giCoke_SerialFD, tmp, 2);
89
90         // Get status
91         read(giCoke_SerialFD, tmp, sizeof(tmp)-1);
92         regexec(&gCoke_StatusRegex, tmp, sizeof(matches)/sizeof(matches[0]), matches, 0);
93         
94         tmp[ matches[3].rm_eo ] = '\0';
95         status = &tmp[ matches[3].rm_so ];
96
97         printf("Machine responded slot status '%s'\n", status);
98
99         return 0;
100 }
101
102

UCC git Repository :: git.ucc.asn.au