Created
January 26, 2016 14:14
-
-
Save Ketouem/15cb30ab657a7b7458d2 to your computer and use it in GitHub Desktop.
Bridge escputil to cgi using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 | |
import subprocess | |
print 'Content-type: text/html' | |
print '' | |
print '<html>' | |
print '<head>' | |
print '<script src="../js/raphael.2.1.0.min.js"></script>' | |
print '<script src="../js/justgage.1.0.1.js"></script>' | |
print '<title>Printer Ink Levels</title>' | |
print '</head>' | |
print '<body>' | |
color_code = {"Cyan" : "#00FFFF", "Magenta" : "#FF00FF", "Yellow" : "FFFF00", "Photo Black" : "#000000"} | |
result = subprocess.Popen(['escputil', '-e', '--raw-device=/dev/usb/lp1'], stdout=subprocess.PIPE) | |
data = [] | |
for line in result.stdout: | |
inner = line.split(" ") | |
inner = [item for item in inner if item != ""] | |
if ("Cyan" in inner) or ("Magenta" in inner) or ("Yellow" in inner): | |
data.append([inner[0], inner[1], inner[2]]) | |
elif "Black" in inner: | |
data.append([inner[0] + ' '+ inner[1], inner[2], inner[3]]) | |
if len(data) == 4: | |
for item in data: | |
print '<div style="height:200px; width:400px; float:left;">' | |
print '<div id="gauge-' + item[0] + '" class="200x100px"></div>' | |
print '</div>' | |
print '<script> var g = new JustGage({id: "gauge-' + item[0] + '", \ | |
value: ' + item[1] + ', \ | |
min: 0, max: 100, levelColors: ["'+ color_code[item[0]] +'"], \ | |
title: "' + item[0] + '(' + item[2] + ')' + '"}); </script>' | |
print '</body>' | |
print '</html>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment