summaryrefslogtreecommitdiff
path: root/addons/hw_escpos
diff options
context:
space:
mode:
authorJosse Colpaert <jco@odoo.com>2018-07-06 12:37:55 +0200
committerJosse Colpaert <jco@odoo.com>2018-07-13 15:10:12 +0200
commit7d34f0218bf4ee04939e30c6101fb85323fccb71 (patch)
treef90efefd436631daac531b08129941cde8b35412 /addons/hw_escpos
parente8821f0519873713ba477b2c013cb0a645e6e559 (diff)
[IMP] point_of_sale: posbox upgrade to stretch / python3
- transform odoo.py -> odoo-bin / recent branches + pip3 packages - deactivate wpa_supplicant when starting dhcpcd automatically - postgresql create clusters manually - latest stretch raspbian image / add versatile-db.ptb for kernel-qemu - ifconfig output change - upgrade hw_-modules: hw_escpos: to python3 hw_posbox_homepage: to v11: csrf does not work as it needs a database hw_posbox_upgrade: to python3 decode utf-8 hw_scale: to python3 hw_proxy: to python3 With the commit, the posbox will also work on the new Raspberry Pi 3b+
Diffstat (limited to 'addons/hw_escpos')
-rw-r--r--addons/hw_escpos/controllers/main.py26
-rw-r--r--addons/hw_escpos/escpos/escpos.py35
2 files changed, 37 insertions, 24 deletions
diff --git a/addons/hw_escpos/controllers/main.py b/addons/hw_escpos/controllers/main.py
index fa57b18501e..805d8388a8a 100644
--- a/addons/hw_escpos/controllers/main.py
+++ b/addons/hw_escpos/controllers/main.py
@@ -8,6 +8,7 @@ import os
import os.path
import subprocess
import time
+import netifaces as ni
import traceback
try:
@@ -29,9 +30,10 @@ except ImportError:
usb = None
from odoo import http, _
-
from odoo.addons.hw_proxy.controllers import main as hw_proxy
+from uuid import getnode as get_mac
+
_logger = logging.getLogger(__name__)
# workaround https://bugs.launchpad.net/openobject-server/+bug/947231
@@ -196,10 +198,18 @@ class EscposDriver(Thread):
def print_status(self,eprint):
localips = ['0.0.0.0','127.0.0.1','127.0.1.1']
hosting_ap = os.system('pgrep hostapd') == 0
- ssid = subprocess.check_output('iwconfig 2>&1 | grep \'ESSID:"\' | sed \'s/.*"\\(.*\\)"/\\1/\'', shell=True).rstrip()
- mac = subprocess.check_output('ifconfig | grep -B 1 \'inet addr\' | grep -o \'HWaddr .*\' | sed \'s/HWaddr //\'', shell=True).rstrip()
- ips = [ c.split(':')[1].split(' ')[0] for c in subprocess.check_output("/sbin/ifconfig").split('\n') if 'inet addr' in c ]
- ips = [ ip for ip in ips if ip not in localips ]
+ ssid = subprocess.check_output('iwconfig 2>&1 | grep \'ESSID:"\' | sed \'s/.*"\\(.*\\)"/\\1/\'', shell=True).decode('utf-8').rstrip()
+ mac = get_mac()
+ h = iter(hex(mac)[2:].zfill(12))
+ mac = ":".join(i + next(h) for i in h)
+ interfaces = ni.interfaces()
+ ips = []
+ for iface_id in interfaces:
+ iface_obj = ni.ifaddresses(iface_id)
+ ifconfigs = iface_obj.get(ni.AF_INET, [])
+ for conf in ifconfigs:
+ if conf.get('addr'):
+ ips.append(conf.get('addr'))
eprint.text('\n\n')
eprint.set(align='center',type='b',height=2,width=2)
eprint.text('PosBox Status\n')
@@ -221,8 +231,10 @@ class EscposDriver(Thread):
eprint.text(ip+'\n')
if len(ips) >= 1:
- eprint.text('\nMAC Address:\n' + mac + '\n')
- eprint.text('\nHomepage:\nhttp://'+ips[0]+':8069\n')
+ ips_filtered = [i for i in ips if i != '127.0.0.1']
+ main_ips = ips_filtered and ips_filtered[0] or '127.0.0.1'
+ eprint.text('\nMAC Address:\n' + str(mac) + '\n')
+ eprint.text('\nHomepage:\nhttp://' + main_ips + ':8069\n')
eprint.text('\n\n')
eprint.cut()
diff --git a/addons/hw_escpos/escpos/escpos.py b/addons/hw_escpos/escpos/escpos.py
index f31b1619894..28f02e9456f 100644
--- a/addons/hw_escpos/escpos/escpos.py
+++ b/addons/hw_escpos/escpos/escpos.py
@@ -7,6 +7,7 @@ import io
import math
import re
import traceback
+import codecs
from hashlib import md5
from PIL import Image
@@ -318,9 +319,9 @@ class Escpos:
else:
image_border = 32 - (size % 32)
if (image_border % 2) == 0:
- return (image_border / 2, image_border / 2)
+ return (int(image_border / 2), int(image_border / 2))
else:
- return (image_border / 2, (image_border / 2) + 1)
+ return (int(image_border / 2), int((image_border / 2) + 1))
def _print_image(self, line, size):
""" Print formatted image """
@@ -330,8 +331,8 @@ class Escpos:
self._raw(S_RASTER_N)
- buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0)
- self._raw(buffer.decode('hex'))
+ buffer = b"%02X%02X%02X%02X" % (int((size[0]/size[1])/8), 0, size[1], 0)
+ self._raw(codecs.decode(buffer, 'hex'))
buffer = ""
while i < len(line):
@@ -340,7 +341,7 @@ class Escpos:
i += 8
cont += 1
if cont % 4 == 0:
- self._raw(buffer.decode("hex"))
+ self._raw(codecs.decode(buffer, "hex"))
buffer = ""
cont = 0
@@ -349,7 +350,7 @@ class Escpos:
i = 0
cont = 0
buffer = ""
- raw = ""
+ raw = b""
def __raw(string):
if output:
@@ -358,8 +359,8 @@ class Escpos:
self._raw(string)
raw += S_RASTER_N
- buffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1], 0)
- raw += buffer.decode('hex')
+ buffer = "%02X%02X%02X%02X" % (int((size[0]/size[1])/8), 0, size[1], 0)
+ raw += codecs.decode(buffer, 'hex')
buffer = ""
while i < len(line):
@@ -368,7 +369,7 @@ class Escpos:
i += 8
cont += 1
if cont % 4 == 0:
- raw += buffer.decode("hex")
+ raw += codecs.decode(buffer, 'hex')
buffer = ""
cont = 0
@@ -433,14 +434,14 @@ class Escpos:
print('print_b64_img')
- id = md5(img.encode('utf-8')).digest()
+ id = md5(img).digest()
if id not in self.img_cache:
print('not in cache')
- img = img[img.find(',')+1:]
- f = io.BytesIO('img')
- f.write(base64.decodestring(img))
+ img = img[img.find(b',')+1:]
+ f = io.BytesIO(b'img')
+ f.write(base64.decodebytes(img))
f.seek(0)
img_rgba = Image.open(f)
img = Image.new('RGB', img_rgba.size, (255,255,255))
@@ -677,7 +678,7 @@ class Escpos:
elif elem.tag == 'img':
if 'src' in elem.attrib and 'data:' in elem.attrib['src']:
- self.print_base64_image(elem.attrib['src'])
+ self.print_base64_image(bytes(elem.attrib['src'], 'utf-8'))
elif elem.tag == 'barcode' and 'encoding' in elem.attrib:
serializer.start_block(stylestack)
@@ -797,19 +798,19 @@ class Escpos:
(encoding, _) = remaining.popitem()
else:
encoding = 'cp437'
- encoded = '\xb1' # could not encode, output error character
+ encoded = b'\xb1' # could not encode, output error character
break;
if encoding != self.encoding:
# if the encoding changed, remember it and prefix the character with
# the esc-pos encoding change sequence
self.encoding = encoding
- encoded = encodings[encoding] + encoded
+ encoded = bytes(encodings[encoding], 'utf-8') + encoded
return encoded
def encode_str(txt):
- buffer = ''
+ buffer = b''
for c in txt:
buffer += encode_char(c)
return buffer