9221a4dd0d67feb096cca6a68c34ab18e687c10e
[uccvend-vendserver.git] / sql-edition / servers / VendServer.py
1 #!/usr/bin/python
2
3 import sys, os, string, socket, time
4 from popen2 import popen2
5 from pyPgSQL import PgSQL
6 from LATClient import LATClient
7
8 asynchronous_responses = [      '400', '401', # door open/closed
9                                                         '610',        # switches changed
10                                                  ]
11
12 class VendingMachine:
13         def __init__(self, rfh, wfh):
14                 self.rfh = rfh
15                 self.wfh = wfh
16                 self.wfh.write('\n')
17                 self.await_prompt()
18
19         def await_prompt(self):
20                 self.wfh.flush()
21                 state = 0
22                 s = ''
23                 while state != 3:
24                         s = self.rfh.read(1)
25                         if s == '': raise Exception
26                         if s == '\n' and state == 0: state = 1
27                         if (s == '#' or s == '%') and state == 1: state = 2
28                         if s == ' ' and state == 2: state = 3
29
30         def get_response(self):
31                 self.wfh.flush()
32                 s = ''
33                 while s == '':
34                         s = self.rfh.readline()
35                         if s == '': return None
36                         s = s.strip('\r\n')
37                 return (s[0:3], s[4:])
38
39         def ping(self):
40                 self.wfh.write('PING\n')
41                 (code, string) = self.get_response()
42                 return code == '000'
43
44
45 if __name__ == '__main__':
46         # Open vending machine via LAT
47         latclient = LATClient(service = 'VEND', password = 'dmscptd')
48         (rfh, wfh) = latclient.get_fh()
49         v = VendingMachine(rfh, wfh)
50         print v.ping()

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