2 eqemu - electronic queue system emulator
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>,
4 Eleni-Maria Stea <eleni@mutantstargoat.com>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
40 #define TMHIST_SIZE 16
42 void post_redisplay(); // defined in main.cc
45 static int report_inputs, cmd_echo;
46 static long last_ticket_msec = LONG_MIN;
48 static std::vector<CustStat> cstat;
50 time_t calc_avg_wait();
51 static void runcmd(const char *cmd);
55 static std::string cur_line;
57 int start_dev(const char *devpath)
59 if((fd = open(devpath, O_RDWR | O_NONBLOCK)) == -1) {
60 fprintf(stderr, "failed to open device: %s: %s\n", devpath, strerror(errno));
66 if(tcgetattr(fd, &term) == -1) {
67 perror("failed to retrieve terminal attributes");
71 term.c_cflag = CS8 | CLOCAL;
72 term.c_iflag &= ~(IXON | IXOFF);
75 cfsetispeed(&term, B38400);
76 cfsetospeed(&term, B38400);
78 if(tcsetattr(fd, TCSANOW, &term) == -1) {
79 perror("failed to set terminal attributes");
85 if(!(fp = fdopen(fd, "r+"))) {
86 perror("failed to attach an I/O stream to the device file\n");
90 setvbuf(fp, 0, _IONBF, 0);
104 void proc_dev_input()
108 static bool skip_line;
110 while((rdbytes = read(fd, buf, sizeof buf - 1)) > 0) {
113 /* ignore our own crap */
114 if(memcmp(buf, "OK,", 3) == 0 || memcmp(buf, "ERR,", 4) == 0) {
118 for(int i=0; i<rdbytes; i++) {
119 if(buf[i] == '\n' || buf[i] == '\r') {
120 if(!cur_line.empty()) {
121 runcmd(cur_line.c_str());
127 cur_line.push_back(buf[i]);
137 last_ticket_msec = get_msec();
147 fprintf(fp, "ticket: %d\n", ticket);
155 if(customer < ticket) {
157 last_ticket_msec = LONG_MIN;
159 for(size_t i=0; i<cstat.size(); i++) {
160 if(cstat[i].id == customer) {
161 cstat[i].end = time(0);
162 fprintf(stderr, "start/end/interval: %lu %lu %lu\n", cstat[i].start,
163 cstat[i].end, cstat[i].end - cstat[i].start);
169 fprintf(fp, "customer: %d\n", customer);
176 time_t calc_avg_wait()
181 for(size_t i=0; i<cstat.size(); i++) {
182 if(cstat[i].end != (time_t)-1) {
183 sum += cstat[i].end - cstat[i].start;
188 return count ? sum / count : 0;
191 #define TICKET_SHOW_DUR 1000
193 int get_display_number()
195 if(get_msec() - last_ticket_msec < TICKET_SHOW_DUR) {
201 int get_led_state(int led)
203 int ledon = get_msec() - last_ticket_msec < TICKET_SHOW_DUR ? 0 : 1;
204 return led == ledon ? 1 : 0;
208 "Queue system emulator v0.1"
210 static void runcmd(const char *cmd)
212 printf("DBG: runcmd(\"%s\")\n", cmd);
216 cmd_echo = !cmd_echo;
217 fprintf(fp, "OK,turning echo %s\n", cmd_echo ? "on" : "off");
221 report_inputs = !report_inputs;
222 fprintf(fp, "OK,turning input reports %s\n", report_inputs ? "on" : "off");
226 fprintf(fp, "OK,%s\n", VERSTR);
230 fprintf(fp, "OK,reseting queues\n");
233 last_ticket_msec = LONG_MIN;
238 fprintf(fp, "OK,ticket: %d\r\n", ticket);
242 fprintf(fp, "OK,customer: %d\r\n", customer);
246 fprintf(fp, "OK,issuing queue ticket\n");
251 fprintf(fp, "OK,next customer\n");
256 fprintf(fp, "OK,avg wait time: %lu\r\n", (unsigned long)calc_avg_wait());
260 fprintf(fp, "OK,commands: (e)cho, (v)ersion, (t)icket, (c)ustomer, "
261 "(n)ext, (q)ueue, (a)verage wait time, (r)eset, (i)nput-reports, "
266 fprintf(fp, "ERR,unknown command: %s\n", cmd);