Project

General

Profile

Revision 119

added query stuff to dustmite.py. should be working but hasn't been tested with the keypad

View differences:

dustmite.py
3 3
import re
4 4
import sys
5 5
import serial
6
import MySQLdb
7
import getpass
6 8

  
9

  
7 10
TT_GET_KEY = 'k'
8 11
TT_ACK     = 'a'
9 12
TT_NACK    = 'n'
......
12 15

  
13 16
BAUD_RATE = 9600
14 17

  
18
tools = {
19
   'Bandsaw':'1',
20
   'DrillPress':'2',
21
   'Mill':'3',
22
   'Lathe':'4',
23
   'Welder':'5',
24
   'CircSaw':'6',
25
   'ChopMiterSaw':'7',
26
   'JigSaw':'8',
27
   'Router':'9'}
28

  
15 29
if len(sys.argv) < 2:
16 30
    print "usage: dustmite.py /path/to/keypad/dev"
17 31

  
18 32
else:
19 33

  
34

  
35
    pw = getpass.getpass("mysql password: ")
36
    db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm")
37
    print "connected, now accepting input"
38

  
39
#TODO: errors!
40

  
41
    cursor = db.cursor()
42

  
43
    qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
44

  
20 45
    keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout=10) 
21 46

  
22 47
    keypad.flushInput()
23
    print keypad
24 48

  
49

  
25 50
    while True:
26 51
#TODO: only consider the last valid id so only the last swipe since a button press will work
27 52
        s = raw_input()
......
30 55
        if id != None:
31 56
            print "-----------\nid# ", id.group(1)
32 57

  
58

  
59
            print "waiting for key..."
33 60
            keypad.write(TT_GET_KEY)
34 61

  
35
            print "waiting for key"
62
            cursor.execute(qry + id.group(1))
63

  
64
            result = cursor.fetchall()
65

  
66
            acl = []
67

  
68
            for r in result:
69
                tls = r[0].split("\x01")
70
                for t in tls:
71
                    if t != '':
72
                        try:
73
                            acl.append (tools[t])
74
                        except KeyError:
75
                            print "ERROR: could not find key!"
76

  
77
            print "user has access to:", acl
36 78
            resp = keypad.read(1)
37 79

  
38
            print resp
80
            print "request:",resp
39 81

  
40
            if resp == "1" or resp == "B":
82

  
83
            if acl.count(resp) > 0:
41 84
                keypad.write(TT_ACK)
42 85
            else:
43 86
                keypad.write(TT_NACK)

Also available in: Unified diff