[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+
This commit is contained in:
Josse Colpaert
2018-07-13 15:10:12 +02:00
parent e8821f0519
commit 7d34f0218b
14 changed files with 99 additions and 75 deletions
+19 -7
View File
@@ -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()
+18 -17
View File
@@ -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
@@ -161,7 +161,7 @@ class PosboxHomepage(odoo.addons.web.controllers.main.Home):
"""
return wifi_template
@http.route('/wifi_connect', type='http', auth='none', cors='*')
@http.route('/wifi_connect', type='http', auth='none', cors='*', csrf=False)
def connect_to_wifi(self, essid, password, persistent=False):
if persistent:
persistent = "1"
@@ -171,7 +171,7 @@ class PosboxHomepage(odoo.addons.web.controllers.main.Home):
subprocess.call(['/home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/connect_to_wifi.sh', essid, password, persistent])
return "connecting to " + essid
@http.route('/wifi_clear', type='http', auth='none', cors='*')
@http.route('/wifi_clear', type='http', auth='none', cors='*', csrf=False)
def clear_wifi_configuration(self):
os.system('/home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/clear_wifi_configuration.sh')
return "configuration cleared"
@@ -244,7 +244,7 @@ class PosboxHomepage(odoo.addons.web.controllers.main.Home):
"""
return ngrok_template
@http.route('/enable_ngrok', type='http', auth='none', cors='*')
@http.route('/enable_ngrok', type='http', auth='none', cors='*', csrf=False)
def enable_ngrok(self, auth_token):
if subprocess.call(['pgrep', 'ngrok']) == 1:
subprocess.Popen(['ngrok', 'tcp', '-authtoken', auth_token, '-log', '/tmp/ngrok.log', '22'])
+1 -1
View File
@@ -80,7 +80,7 @@ upgrade_template = """
</p>
<pre>
"""
upgrade_template += subprocess.check_output("git --work-tree=/home/pi/odoo/ --git-dir=/home/pi/odoo/.git log -1", shell=True).replace("\n", "<br/>")
upgrade_template += subprocess.check_output("git --work-tree=/home/pi/odoo/ --git-dir=/home/pi/odoo/.git log -1", shell=True).decode('utf-8').replace("\n", "<br/>")
upgrade_template += """
</pre>
<div class='centering'>
+9 -6
View File
@@ -21,6 +21,8 @@ BANNED_DEVICES = {
"0424:9514", # Standard Microsystem Corp. Builtin Ethernet module
"1d6b:0002", # Linux Foundation 2.0 root hub
"0424:ec00", # Standard Microsystem Corp. Other Builtin Ethernet module
"0424:2514", # Standard Microsystems Corp. USB 2.0 Hub (rpi3b+)
"0424:7800", # Standard Microsystems Corp. (rpi3b+)
}
@@ -100,15 +102,16 @@ class Proxy(http.Controller):
"""
if debug is None:
resp += """(<a href="/hw_proxy/status?debug">debug version</a>)"""
devices = subprocess.check_output("lsusb").split('\n')
devices = subprocess.check_output("lsusb").decode('utf-8').split('\n')
count = 0
resp += "<div class='devices'>\n"
for device in devices:
device_name = device[device.find('ID')+2:]
device_id = device_name.split()[0]
if not (device_id in BANNED_DEVICES):
resp += "<div class='device' data-device='"+device+"'>"+device_name+"</div>\n"
count += 1
if device_name: # to avoid last empty line
device_id = device_name.split()[0]
if not (device_id in BANNED_DEVICES):
resp += "<div class='device' data-device='"+device+"'>"+device_name+"</div>\n"
count += 1
if count == 0:
resp += "<div class='device'>No USB Device Found</div>"
@@ -124,7 +127,7 @@ class Proxy(http.Controller):
%s
</pre>
""" % subprocess.check_output('lsusb -v', shell=True)
""" % subprocess.check_output('lsusb -v', shell=True).decode('utf-8')
return request.make_response(resp,{
'Cache-Control': 'no-cache',
+14 -14
View File
@@ -28,7 +28,7 @@ except ImportError:
def _toledo8217StatusParse(status):
""" Parse a scale's status, returning a `(weight, weight_info)` pair. """
weight, weight_info = None, None
stat = ord(status[status.index('?') + 1])
stat = status[status.index(b'?') + 1]
if stat == 0:
weight_info = 'ok'
else:
@@ -68,17 +68,17 @@ Toledo8217Protocol = ScaleProtocol(
parity=serial.PARITY_EVEN,
timeout=1,
writeTimeout=1,
weightRegexp="\x02\\s*([0-9.]+)N?\\r",
statusRegexp="\x02\\s*(\\?.)\\r",
weightRegexp=b"\x02\\s*([0-9.]+)N?\\r",
statusRegexp=b"\x02\\s*(\\?.)\\r",
statusParse=_toledo8217StatusParse,
commandDelay=0.2,
weightDelay=0.5,
newWeightDelay=0.2,
commandTerminator='',
weightCommand='W',
zeroCommand='Z',
tareCommand='T',
clearCommand='C',
commandTerminator=b'',
weightCommand=b'W',
zeroCommand=b'Z',
tareCommand=b'T',
clearCommand=b'C',
emptyAnswerValid=False,
autoResetWeight=False,
)
@@ -98,15 +98,15 @@ ADAMEquipmentProtocol = ScaleProtocol(
weightRegexp=r"\s*([0-9.]+)kg", # LABEL format 3 + KG in the scale settings, but Label 1/2 should work
statusRegexp=None,
statusParse=None,
commandTerminator="\r\n",
commandTerminator=b"\r\n",
commandDelay=0.2,
weightDelay=0.5,
newWeightDelay=5, # AZExtra beeps every time you ask for a weight that was previously returned!
# Adding an extra delay gives the operator a chance to remove the products
# before the scale starts beeping. Could not find a way to disable the beeps.
weightCommand='P',
zeroCommand='Z',
tareCommand='T',
weightCommand=b'P',
zeroCommand=b'Z',
tareCommand=b'T',
clearCommand=None, # No clear command -> Tare again
emptyAnswerValid=True, # AZExtra does not answer unless a new non-zero weight has been detected
autoResetWeight=True, # AZExtra will not return 0 after removing products
@@ -165,8 +165,8 @@ class Scale(Thread):
if not char:
break
else:
answer.append(char)
return ''.join(answer)
answer.append(bytes(char))
return b''.join(answer)
def _parse_weight_answer(self, protocol, answer):
""" Parse a scale's answer to a weighing request, returning
@@ -48,14 +48,18 @@ function connect () {
if [ -z "${PASSWORD}" ] ; then
sudo iwconfig wlan0 essid "${ESSID}"
else
sudo wpa_passphrase "${ESSID}" "${PASSWORD}" > "${WPA_PASS_FILE}"
# Necessary in stretch: https://www.raspberrypi.org/forums/viewtopic.php?t=196927
sudo cp /etc/wpa_supplicant/wpa_supplicant.conf "${WPA_PASS_FILE}"
chmod 777 ${WPA_PASS_FILE}
sudo wpa_passphrase "${ESSID}" "${PASSWORD}" >> "${WPA_PASS_FILE}"
sudo wpa_supplicant -B -i wlan0 -c "${WPA_PASS_FILE}"
fi
sudo systemctl daemon-reload
sudo service dhcpcd restart
# give dhcp some time
timeout 30 sh -c 'until ifconfig wlan0 | grep "inet addr:" ; do sleep 0.1 ; done'
timeout 30 sh -c 'until ifconfig wlan0 | grep "inet " ; do sleep 0.1 ; done'
TIMEOUT_RETURN=$?
if [ ${TIMEOUT_RETURN} -eq 124 ] && [ -z "${NO_AP}" ] ; then
@@ -2,7 +2,7 @@
CURRENT_WIFI_NETWORK_FILE="/tmp/current_wifi_network.txt"
while true ; do
if [ -z "$(cat <(ifconfig eth0) <(ifconfig wlan0) | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}';)" ] ; then
if [ -z "$(cat <(ifconfig eth0) <(ifconfig wlan0) | grep "inet ";)" ] ; then
ESSID=$(head -n 1 "${CURRENT_WIFI_NETWORK_FILE}" | tr -d '\n')
PASSWORD=$(tail -n 1 "${CURRENT_WIFI_NETWORK_FILE}" | tr -d '\n')
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
FORCE_HOST_AP="${1}"
WIRED_IP=$(ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}';)
WIRED_IP=$(python3 -c "import netifaces as ni; print(ni.ifaddresses('eth0').get(ni.AF_INET) and ni.ifaddresses('eth0')[ni.AF_INET][0]['addr'] or '')")
WIFI_NETWORK_FILE="/home/pi/wifi_network.txt"
# if there is no wired ip, attempt to start an AP through wireless interface
@@ -39,6 +39,8 @@ slaac private
# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname
# stretch: we do not want to start wpa_supplicant as our script starts wpa_supplicant itself
nohook wpa_supplicant
# dhcpcd will assign zeroconf 169.254.*.* addresses when
# it can't connect, which we don't want
@@ -14,7 +14,7 @@
. /lib/lsb/init-functions
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
DAEMON=/home/pi/odoo/odoo.py
DAEMON=/home/pi/odoo/odoo-bin
NAME=odoo
DESC=odoo
CONFIG=/home/pi/odoo/addons/point_of_sale/tools/posbox/configuration/odoo.conf
@@ -21,11 +21,15 @@ apt-get update && apt-get -y upgrade
# Do not be too fast to upgrade to more recent firmware and kernel than 4.38
# Firmware 4.44 seems to prevent the LED mechanism from working
PKGS_TO_INSTALL="adduser postgresql-client python python-dateutil python-decorator python-docutils python-feedparser python-imaging python-jinja2 python-ldap python-libxslt1 python-lxml python-mako python-mock python-passlib python-psutil python-psycopg2 python-pybabel python-pychart python-pydot python-pyparsing python-pypdf python-reportlab python-requests python-tz python-vatnumber python-vobject python-werkzeug python-xlwt python-yaml postgresql python-gevent python-serial python-pip python-dev localepurge vim mc mg screen iw hostapd isc-dhcp-server git rsync console-data lightdm xserver-xorg-video-fbdev xserver-xorg-input-evdev iceweasel xdotool unclutter x11-utils openbox python-netifaces rpi-update"
PKGS_TO_INSTALL="adduser postgresql python3 python3-dateutil python3-decorator python3-docutils python3-feedparser python3-pil python3-jinja2 python3-ldap3 python3-lxml python3-mako python3-mock python3-openid python3-psutil python3-psycopg2 python3-babel python3-pydot python3-pyparsing python3-pypdf2 python3-reportlab python3-requests python3-simplejson python3-tz python3-vatnumber python3-werkzeug python3-yaml python3-serial python3-pip python3-dev localepurge vim mc mg screen iw hostapd isc-dhcp-server git rsync console-data lightdm xserver-xorg-video-fbdev xserver-xorg-input-evdev iceweasel xdotool unclutter x11-utils openbox python3-netifaces rpi-update python3-passlib python3-libsass python3-qrcode python3-html2text python3-unittest2 python3-simplejson"
# KEEP OWN CONFIG FILES DURING PACKAGE CONFIGURATION
# http://serverfault.com/questions/259226/automatically-keep-current-version-of-config-files-when-apt-get-install
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --force-yes install ${PKGS_TO_INSTALL}
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install ${PKGS_TO_INSTALL}
pg_lsclusters
systemctl start postgresql@9.6-main
systemctl status postgresql@9.6-main
sudo -u postgres createuser -s pi
apt-get clean
localepurge
@@ -34,23 +38,15 @@ rm -rf /usr/share/doc
# python-usb in wheezy is too old
# the latest pyusb from pip does not work either, usb.core.find() never returns
# this may be fixed with libusb>2:1.0.11-1, but that's the most recent one in raspbian
# so we install the latest pyusb that works with this libusb
pip install pyusb==1.0.0b1
pip install qrcode
pip install evdev
pip install simplejson
pip install unittest2
# --upgrade because websocket_client in wheezy is bad:
# https://github.com/docker/compose/issues/1288
pip install --upgrade websocket_client
# so we install the latest pyusb that works with this libusb.
# Even in stretch, we had an error with langid (but worked otherwise)
pip3 install pyusb==1.0.0b1
pip3 install evdev
groupadd usbusers
usermod -a -G usbusers pi
usermod -a -G lp pi
usermod -a -G input lightdm
sudo -u postgres createuser -s pi
mkdir /var/log/odoo
chown pi:pi /var/log/odoo
chown pi:pi -R /home/pi/odoo/
@@ -28,6 +28,8 @@ __base="$(basename ${__file} .sh)"
MOUNT_POINT="${__dir}/root_mount"
OVERWRITE_FILES_BEFORE_INIT_DIR="${__dir}/overwrite_before_init"
OVERWRITE_FILES_AFTER_INIT_DIR="${__dir}/overwrite_after_init"
VERSION=11.0
REPO=https://github.com/odoo/odoo.git
if [ ! -f kernel-qemu ] || ! file_exists *raspbian*.img ; then
./posbox_download_images.sh
@@ -42,14 +44,14 @@ rm -rf "${CLONE_DIR}"
if [ ! -d $CLONE_DIR ]; then
echo "Clone Github repo"
mkdir -p "${CLONE_DIR}"
git clone -b 8.0 --no-local --no-checkout --depth 1 https://github.com/odoo/odoo.git "${CLONE_DIR}"
git clone -b ${VERSION} --no-local --no-checkout --depth 1 ${REPO} "${CLONE_DIR}"
cd "${CLONE_DIR}"
git config core.sparsecheckout true
echo "addons/web
addons/hw_*
addons/point_of_sale/tools/posbox/configuration
openerp/
odoo.py" | tee --append .git/info/sparse-checkout > /dev/null
odoo/
odoo-bin" | tee --append .git/info/sparse-checkout > /dev/null
git read-tree -mu HEAD
fi
@@ -89,7 +91,7 @@ sleep 5
e2fsck -f "${LOOP_MAPPER_PATH}" # resize2fs requires clean fs
resize2fs "${LOOP_MAPPER_PATH}"
mkdir "${MOUNT_POINT}"
mkdir -p "${MOUNT_POINT}" #-p: no error if existing
mount "${LOOP_MAPPER_PATH}" "${MOUNT_POINT}"
# 'overlay' the overwrite directory onto the mounted image filesystem
@@ -107,7 +109,8 @@ umount "${MOUNT_POINT}"
# from http://paulscott.co.za/blog/full-raspberry-pi-raspbian-emulation-with-qemu/
# ssh pi@localhost -p10022
QEMU_OPTS=(-kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append 'root=/dev/sda2 rootfstype=ext4 rw' -hda posbox.img -net user,hostfwd=tcp::10022-:22,hostfwd=tcp::18069-:8069 -net nic)
# as of stretch with newer kernels, the versatile-pb.dtb file is necessary
QEMU_OPTS=(-kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -dtb versatile-pb.dtb -no-reboot -serial stdio -append 'root=/dev/sda2 rootfstype=ext4 rw' -hda posbox.img -net user,hostfwd=tcp::10022-:22,hostfwd=tcp::18069-:8069 -net nic)
if [ -z ${DISPLAY:-} ] ; then
QEMU_OPTS+=(-nographic)
fi
@@ -1,5 +1,8 @@
#!/bin/sh
wget 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2017-07-05/2017-07-05-raspbian-jessie-lite.zip' -O raspbian.img.zip
wget 'http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-06-29/2018-06-27-raspbian-stretch-lite.zip' -O raspbian.img.zip
unzip raspbian.img.zip
wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.4.13-jessie' -O kernel-qemu
wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.9.59-stretch' -O kernel-qemu
wget 'https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb'