Initial stuff
[zanchey/dispense2.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 class VendingMachine:
9         def __init__(self, rfh, wfh):
10                 self.rfh = rfh
11                 self.wfh = wfh
12                 self.wfh.write('\n')
13                 self.await_prompt()
14
15         def await_prompt(self):
16                 self.wfh.flush()
17                 state = 0
18                 s = ''
19                 while state != 3:
20                     s = self.rfh.read(1)
21                     if s == '': raise Exception
22                     if s == '\n' and state == 0: state = 1
23                     if s == '#' and state == 1: state = 2
24                     if s == ' ' and state == 2: state = 3
25
26         def get_response(self):
27                 self.wfh.flush()
28                 s = ''
29                 while s == '':
30                     s = self.rfh.readline()
31                     if s == '': return None
32                     s = s.strip('\r\n')
33                 return s
34
35         def ping(self):
36                 self.wfh.write('PING\n')
37                 return self.get_response()
38
39 if __name__ == '__main__':
40         # Open vending machine via LAT
41         latclient = LATClient(service = 'VEND', password = 'dmscptd')
42         (rfh, wfh) = latclient.get_fh()
43         v = VendingMachine(rfh, wfh)
44         print v.ping()

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