87 lines
2.2 KiB
Python
87 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import socket
|
|
import json
|
|
import os
|
|
from flask import Flask, render_template
|
|
from flask_cods import CORS
|
|
|
|
CheckPrinterOnline = '7e4d3630312053310d0a'
|
|
MachingInfo = "7e4d3131350d0a"
|
|
GetPrintPercent = "7e4d32370d0a"
|
|
GetPrinterTemp = "7e4d3130350d0a"
|
|
|
|
if os.environ.get('IPADDR') == None:
|
|
print("IPADDR environment variable not set please set to printer IP")
|
|
SystemExit()
|
|
HOST = os.environ.get('IPADDR') # The server's hostname or IP address
|
|
PORT = 8899 # The port used by the server
|
|
|
|
application = Flask(__name__)
|
|
CORS(application)
|
|
application.config["DEBUG"] = True
|
|
|
|
def checkPrinterOnline():
|
|
try:
|
|
data = sendCode(CheckPrinterOnline)
|
|
return data
|
|
except:
|
|
pass
|
|
|
|
def sendCode(cmddata):
|
|
command = bytes.fromhex(cmddata)
|
|
try:
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
s.settimeout(1)
|
|
s.connect((HOST, PORT))
|
|
s.sendall(command)
|
|
data = s.recv(1024)
|
|
except Exception as E:
|
|
print(E)
|
|
data = "Connection Failed"
|
|
|
|
return data
|
|
|
|
def actualPercent():
|
|
data = sendCode(GetPrintPercent)
|
|
if data == "Connection Failed":
|
|
return "COnnection Failed"
|
|
data = data.decode().split('\r\n')[1].split()[-1]
|
|
x, y = data.split('/')
|
|
return int(x) / int(y) * 100
|
|
|
|
def getTemp():
|
|
data = sendCode(GetPrinterTemp)
|
|
data = data.split()
|
|
try:
|
|
currTemp = data[3].decode().split(':')[1]
|
|
maxTemp = data[4].decode().split('/')[1]
|
|
except IndexError as E:
|
|
print(data)
|
|
return [currTemp, maxTemp]
|
|
|
|
# sendCode(CheckPrinterOnline)
|
|
# sendCode(MachingInfo)
|
|
# sendCode(GetPrintPercent)
|
|
|
|
# print(bytes.fromhex(MaybePacketStart).decode('utf-8'))
|
|
|
|
@application.route('/')
|
|
def index():
|
|
if checkPrinterOnline() != "Connection Failed":
|
|
try:
|
|
return render_template("index.html")
|
|
except Exception as E:
|
|
data = E
|
|
else:
|
|
data = ["The Printer is Offline"]
|
|
return render_template("error.html", data=data)
|
|
|
|
@application.route('/percent')
|
|
def percent():
|
|
data = {"percent": str(round(actualPercent(),3)), "temp": getTemp()}
|
|
# print()
|
|
return json.dumps(data)
|
|
|
|
if __name__ == '__main__':
|
|
application.run(host='0.0.0.0', port=80) |