Project

General

Profile

Statistics
| Revision:

root / trunk / swipe / dustmite.py @ 125

History | View | Annotate | Download (3.58 KB)

1
#!/usr/bin/python
2

    
3
################################################################################
4
## dustmite.py is the main tooltron server
5
# It connects to the civicrm database of mysql and sends commands to
6
# the cardbox and tools.  It must be connected over usb to the card
7
# reader, and the two arguments specify the devices to use for the
8
# serial communication to the card and tool boxes.
9
#
10
# This script requires the modules seen below, which may not be
11
# present by default
12
#
13
# email bradneuman@gmail.com if you have any questions
14
################################################################################
15

    
16

    
17
import re
18
import sys
19
import serial
20
import MySQLdb
21
import getpass
22

    
23

    
24
TT_GET_KEY = 'k'
25
TT_ACK     = 'a'
26
TT_NACK    = 'n'
27
TT_TO      = 'f'
28
TT_TIMEOUT = 't'
29

    
30
BAUD_RATE = 9600
31

    
32
#tools for messages are these + 1 but in decimal, not ascii
33
tools = {
34
   'Bandsaw':'2',
35
   'DrillPress':'1',
36
   'Mill':'4',
37
   'Lathe':'5',
38
   'ChopMiterSaw':'3'
39
}
40

    
41

    
42
if len(sys.argv) < 3:
43
    print "usage: dustmite.py /path/to/keypad/device /path/to/tool/bus/device"
44

    
45
else:
46

    
47

    
48
    pw = getpass.getpass("mysql password: ")
49
    db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm")
50
    print "connected, now accepting input"
51

    
52
    cursor = db.cursor()
53

    
54
    qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
55

    
56
    keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 30) 
57
    keypad.flushInput()
58
    print keypad
59

    
60
    bus = serial.Serial(sys.argv[2], BAUD_RATE, timeout = 15)
61
    bus.flushInput()
62
    print bus
63

    
64
    # ^ <src> <dest> <data>
65
    def sendTool(t):
66
        tn = int(t) + 1 
67
        msg = '^' + chr(1) + chr(tn) + 'O' + chr(1 ^ tn ^ ord('O'))
68

    
69
        bus.write(msg)
70
        return
71

    
72
    #returns [src, data] or [] on error
73
    def readTool():
74
        ret = [0,0,0]
75

    
76
        if bus.read(1) == '^':
77
            ret[0] = bus.read(1)
78
            ret[1] = bus.read(1)
79
            ret[2] = bus.read(1)
80
            x = bus.read(1)
81

    
82
            print "got packet",ret
83

    
84
            if chr(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) == x:
85
                return ret
86
            else:
87
                print "xor fail. got", x, "should have been got",(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2]))
88

    
89
        return []
90

    
91
    def checkAck(t):
92
        tn = int(t) + 1 
93
        m = readTool()
94

    
95
        if m== []:
96
            return False
97

    
98

    
99
        return m[0] == chr(tn) and m[1] == chr(1) and m[2] == 'A'
100

    
101

    
102
    while True:
103
        s = raw_input()
104
        id = re.search('%([0-9]*)=.*', s)
105
        
106
        if id != None:
107
            print "\n-----------\nid# ", id.group(1)
108

    
109

    
110
            print "sending key request"
111
            keypad.write(TT_GET_KEY)
112

    
113
            cursor.execute(qry + id.group(1))
114

    
115
            result = cursor.fetchall()
116

    
117
            acl = []
118

    
119
            for r in result:
120
                tls = r[0].split("\x01")
121
                for t in tls:
122
                    if t != '':
123
                        try:
124
                            acl.append (tools[t])
125
                        except KeyError:
126
                            #this doesn't really matter
127
                            pass
128

    
129
            print "user has access to:", acl
130

    
131
            resp = keypad.read(1)
132

    
133
            print "request:",resp
134

    
135

    
136
            if acl.count(resp) > 0:
137
                keypad.write(TT_ACK)
138
                sendTool(resp)
139
                print "ACCESS GRANTED"
140
                if checkAck(resp):
141
                    print "ACK"
142
                else:
143
                    print "NO ACK!!!!"
144

    
145
            else:
146
                keypad.write(TT_NACK)
147
                print "ACCESS DENIED!!"
148

    
149
        fflush(stdin)