From d0ae1092d99a7ed59f734f0c94525d1dafb74f08 Mon Sep 17 00:00:00 2001 From: Dirk Jahnke Date: Mon, 30 May 2022 11:46:11 +0200 Subject: [PATCH] New scanners added: Chrome + Smart Phone with JS library --- booker/templates/booker/chrome_scanner.html | 226 +++++++++++++ booker/templates/booker/index.html | 3 +- .../templates/booker/smartphone_scanner.html | 311 ++++++++++++------ booker/urls.py | 4 +- booker/views.py | 57 +++- 5 files changed, 497 insertions(+), 104 deletions(-) create mode 100644 booker/templates/booker/chrome_scanner.html diff --git a/booker/templates/booker/chrome_scanner.html b/booker/templates/booker/chrome_scanner.html new file mode 100644 index 0000000..e5dabbb --- /dev/null +++ b/booker/templates/booker/chrome_scanner.html @@ -0,0 +1,226 @@ +{% extends "base.html" %} +{% load static %} + +{% block additional_css %} + + +{% endblock %} + +{% block title %}Smartphone Scanner{% endblock %} + +{% block content %} +
+ +
+ Start + Reset +
+
+
+ +
+ + + +
+ +
+
    +
    +
    + +
    + + + +
    +
    +
    +
    + + +
    +
    +
    +
    + + +
    +
    +
    +
    + + +
    +
    +
    +
    +
    + + +
    +
    + + + +{% endblock %} diff --git a/booker/templates/booker/index.html b/booker/templates/booker/index.html index e99ca10..dc2be88 100644 --- a/booker/templates/booker/index.html +++ b/booker/templates/booker/index.html @@ -56,7 +56,8 @@

    Use your smartphone

    -

    Use your smartphone to scan.

    +

    Use your smartphone to scan.

    +

    Use your chrome Barcode-Scanner to scan.

    {% endblock %} diff --git a/booker/templates/booker/smartphone_scanner.html b/booker/templates/booker/smartphone_scanner.html index f57d80c..9a0f0d0 100644 --- a/booker/templates/booker/smartphone_scanner.html +++ b/booker/templates/booker/smartphone_scanner.html @@ -2,118 +2,223 @@ {% load static %} {% block additional_css %} - - {% endblock %} {% block title %}Smartphone Scanner{% endblock %} {% block content %} -
    + -
    - Start - Reset +
    +
    +
    + Start +
    +
    + Reset +
    +
    + Clear +
    -
    - +
    +
    - - +
    + - -
    - -
    +
    -
    - - + + document.getElementById('resetButton').addEventListener('click', () => { + document.getElementById('result').textContent = ''; + Quagga.stop(); + console.log('Reset/stopped.') + }) + + document.getElementById('clearOverlayButton').addEventListener('click', () => { + clearCanvas(); + }) + }) + .catch(function(err) { + console.log(err); + }); + + }); +
    @@ -145,14 +250,22 @@ var containerIsValid = false; var assetIsValid = false; + async function getObjectInfo(url) { + const response = await fetch(url); + const object_json = response.json(); + return object_json; + } + async function getContainerInfo(containerId) { document.querySelector('#container_id').value = containerId; url = '/api/containers?named_id=' + containerId; document.querySelector('#container_details').value = 'Trying ' + url; - const response = await fetch(url); - const container = await response.json(); + const container = await getObjectInfo(url); if (container.count >= 1) { - document.querySelector('#container_details').value = container.results[0].description; + c = container.results[0]; + console.log(c); + containerType = await getObjectInfo(c.container_type); + document.querySelector('#container_details').value = c.description + "\n" + c.named_id + ': ' + c.color + "\n" + containerType.description; containerIsValid = true; } else { document.querySelector('#container_details').value = document.querySelector('#container_details').value + "\nERROR: No result\n"; @@ -178,15 +291,11 @@ } function useScannedText(msg) { - // const data = JSON.parse(e.data); - // msg = String(data.message); document.querySelector('#chat-log').value += (msg + '\n'); if (msg.startsWith('C-')) { - getContainerInfo(data.message); - // document.querySelector('#container_id').value = msg; + getContainerInfo(msg); } else if (msg.startsWith('A-')) { - getAssetInfo(data.message); - // document.querySelector('#asset_id').value = msg; + getAssetInfo(msg); } else { assetId = 'A-' + msg; document.querySelector('#asset_id').value = assetId; diff --git a/booker/urls.py b/booker/urls.py index af2da1e..87c8fee 100644 --- a/booker/urls.py +++ b/booker/urls.py @@ -6,5 +6,7 @@ app_name = 'booker' urlpatterns = [ path('', views.IndexView.as_view(), name='index'), path('smartphone_scanner/', views.SmartphoneScannerView.as_view(), name='smartphone_scanner'), - path('/', views.scanner, name='scanner') + path('chrome_scanner/', views.ChromeScannerView.as_view(), name='chrome_scanner'), + path('/', views.scanner_view, name='scanner'), + path('webscanner//', views.web_scanner_view, name='web_scanner'), ] diff --git a/booker/views.py b/booker/views.py index d6128f7..014a943 100644 --- a/booker/views.py +++ b/booker/views.py @@ -1,6 +1,18 @@ +from datetime import datetime +from channels.layers import get_channel_layer from django.shortcuts import render +from django.views.decorators.csrf import csrf_exempt from django.views.generic import ListView, TemplateView +from gmqtt import Client +from rest_framework.utils import json + +from homelog import settings +from homelog.system_user import get_system_user from .models import Scanner +from django.http import HttpResponse +import logging + +LOGGER = logging.getLogger(__name__) class IndexView(ListView): @@ -8,10 +20,53 @@ class IndexView(ListView): template_name = 'booker/index.html' -def scanner(request, scanner_id): +def scanner_view(request, scanner_id): return render(request, 'booker/scanner.html', { 'scanner_id': scanner_id }) + class SmartphoneScannerView(TemplateView): template_name = 'booker/smartphone_scanner.html' + + +class ChromeScannerView(TemplateView): + template_name = 'booker/chrome_scanner.html' + + +@csrf_exempt +def web_scanner_view(request, scanner_id): + def get_or_create_scanner(named_id): + scanner, created = Scanner.objects.get_or_create(named_id=named_id, defaults={ + # 'lwt_topic': topic, + 'description': 'WebAPI scanner', + 'last_online_ts': datetime.now(), + 'created_by': get_system_user(), + 'changed_by': get_system_user() + }) + if created: + LOGGER.debug(f"Created new scanner entry for {named_id}") + else: + LOGGER.debug(f"Updated scanner entry for {named_id}") + return scanner + + print(f"Got {request.POST['scan']} from scanner {scanner_id}") + get_or_create_scanner(scanner_id) + topic = f"barcodescanner/{scanner_id}/scan" + ''' + client = Client('homelog-webscanner') + client.set_auth_credentials(username=settings.MQTT_USER, password=settings.MQTT_PASSWORD) + + client.connect(settings.MQTT_HOST, 1883, keepalive=60, version=settings.MQTT_VERSION) + client.publish(topic, request.POST['scan']) + client.disconnect() + # MqttConsumer().mqtt_publish({'topic': topic, 'payload': request.POST['scan'], 'retain': False}) + ''' + channel_layer = get_channel_layer() + channel_layer.group_send( + 'mqtt', # scanner_id, + {"type": "distribute", "text": json.dumps({'message': request.POST['scan'], 'topic': topic})} + ) + ''' + ''' + return HttpResponse(f"

    OK, got {request.POST['scan']} from {scanner_id}

    ")