-
Notifications
You must be signed in to change notification settings - Fork 2
/
printer.py
83 lines (59 loc) · 1.79 KB
/
printer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import serial
import time
import urllib2
from optparse import OptionParser
parser = OptionParser(description='A free range printer')
parser.add_option("--debug", help="output to /dev/null", action="store_true")
(options, args) = parser.parse_args()
RASPI_SERIAL = '/dev/ttyAMA0'
DEBUG_SERIAL = '/dev/master'
printerType = 'A2-raw'
printerDevice = DEBUG_SERIAL if options.debug else RASPI_SERIAL
printerId = None
printer = None
printerHost = "http://printer.gofreerange.com:80"
def readFileContents(filepath):
with open(filepath,'r') as f:
output = f.read()
return output
def getPrinterId():
return readFileContents('PRINTER_ID')
def initSettings():
print "init settings"
global printerId
printerId = getPrinterId()
def initPrinter():
global printer
printer = serial.Serial(printerDevice, 19200)
def getPrinterUrl():
return printerHost + "/printer/" + printerId
def checkForDownload():
url = getPrinterUrl()
req = urllib2.Request(url)
req.add_header('Accept', 'application/vnd.freerange.printer.' + printerType)
print "Checking for download: " + url
response = urllib2.urlopen(req)
content_length = int(response.info()['Content-length'])
print "Content length: " + str(content_length)
status = str( response.getcode() )
print "Status: " + status
if status == '200' and content_length > 0:
print "has response"
sendToPrinter(response)
elif content_length == 0:
print "content length was 0"
else:
print "got response: " + status
time.sleep(5)
def sendToPrinter(f):
print "Printing."
bytes = f.read()
for b in bytes:
printer.write(b)
printer.flush()
initSettings()
initPrinter()
print printerId
print printerDevice
#while(True):
checkForDownload()