From b6e08617f8ec1b3913233f7bda297b6c71a23e39 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Tue, 23 Apr 2019 12:30:17 +0200 Subject: [PATCH 1/5] [ADD] ethernet functionality --- dicts/ras_dic.py | 3 +++ lib/Clocking.py | 58 +++++++++++++++++++++++++++++++++++++-------- lib/OdooXMLrpc.py | 16 ++++++++++++- lib/Tasks.py | 2 +- requirements.txt | 3 ++- templates/form.html | 2 ++ 6 files changed, 71 insertions(+), 13 deletions(-) diff --git a/dicts/ras_dic.py b/dicts/ras_dic.py index 855bec9..f80dc82 100644 --- a/dicts/ras_dic.py +++ b/dicts/ras_dic.py @@ -2,6 +2,9 @@ # the memory of the device WORK_DIR = "/home/pi/ras/" +#network interface: wlan0 (WLAN), eth0 (Ethernet) +NET_INTERFACE = 'eth0' + # SSID when resetting the WiFi SSID_reset = "__RAS__" diff --git a/lib/Clocking.py b/lib/Clocking.py index e47129e..b5141f4 100755 --- a/lib/Clocking.py +++ b/lib/Clocking.py @@ -1,6 +1,7 @@ import time import subprocess import logging +from dicts.ras_dic import NET_INTERFACE _logger = logging.getLogger(__name__) @@ -14,7 +15,10 @@ def __init__(self, odoo, hardware): self.Disp = hardware[1] # Display self.Reader = hardware[2] # Card Reader + self.net_interface = NET_INTERFACE # wlan0 or eth0 self.wifi = False + self.interface_stable = False + self.interface_msg = 'Unknown' self.card_logging_time_min = 1.5 # minimum amount of seconds allowed for @@ -51,6 +55,19 @@ def wifi_active(self): wifi_active = True return wifi_active + def interface_active(self): + if self.net_interface == 'wlan0': + interface_active = self.wifi_active() + elif self.net_interface == 'eth0': + ifconfig_out = subprocess.check_output( + 'ifconfig eth0', shell=True).decode('utf-8') + if 'UP' in ifconfig_out: + interface_active = True + else: + interface_active = False + _logger.warning('Ethernet Active is %s' % interface_active) + return interface_active + def get_status(self): iwresult = subprocess.check_output( "iwconfig wlan0", shell=True @@ -104,18 +121,43 @@ def wifi_signal_msg(self): self.wifi = True return msg + def get_interface_msg(self): + if self.net_interface == 'wlan0': + return self.wifi_signal_msg() + elif self.net_interface == 'eth0': + ifconfig_out = subprocess.check_output( + 'ifconfig eth0', shell=True).decode('utf-8') + if 'RUNNING' in ifconfig_out: + return ' Ethernet OK' + else: + return ' NO Ethernet' + + def interface_running(self): + if self.net_interface == 'wlan0': + self.interface_stable = self.wifi_stable() + elif self.net_interface == 'eth0': + ifconfig_out = subprocess.check_output( + 'ifconfig eth0', shell=True).decode('utf-8') + if 'RUNNING' in ifconfig_out: + self.interface_stable = True # stable in the case of ethernet + # means 'running' + else: + self.interface_stable = False + return self.interface_stable + def wifi_stable(self): msg = self.wifi_signal_msg() return self.wifi def odoo_msg(self): - msg = "NO Odoo connected" - self.odoo_conn = False - if self.wifi_stable(): + if self.interface_running(): if self.Odoo._get_user_id(): msg = " Odoo OK" self.odoo_conn = True return msg + else: + msg = "NO Odoo connected" + self.odoo_conn = False _logger.warn(msg) return msg @@ -143,12 +185,8 @@ def clock_sync(self): _logger.info("Clocking sync returns: %s" % self.msg) def get_messages(self): - self.wifi_m = self.wifi_signal_msg() # get wifi strength signal - if not self.wifi: - self.odoo_m = "NO Odoo connected" - self.odoo_conn = False - else: - self.odoo_m = self.odoo_msg() # get odoo connection msg + self.odoo_m = self.odoo_msg() + self.interface_msg = self.get_interface_msg() def clocking(self): # Main Functions of the Terminal: @@ -184,7 +222,7 @@ def clocking(self): # store the time when the card logging process begin self.wifi_m = self.wifi_signal_msg() - if not self.wifi: + if not self.interface_running(): self.msg = "ContactAdm" else: self.clock_sync() # synchronous: when odoo not diff --git a/lib/OdooXMLrpc.py b/lib/OdooXMLrpc.py index 605fed7..2122857 100644 --- a/lib/OdooXMLrpc.py +++ b/lib/OdooXMLrpc.py @@ -2,6 +2,7 @@ import time import json import logging +import requests from dicts import tz_dic from dicts.ras_dic import WORK_DIR @@ -48,7 +49,10 @@ def set_params(self): self.pswd = self.j_data["user_password"][0] self.host = self.j_data["odoo_host"][0] self.port = self.j_data["odoo_port"][0] - + if "iot_call" in self.j_data: + self.iot_call = True + else: + self.iot_call = False self.adm = self.j_data["admin_id"][0] self.tz = self.j_data["timezone"][0] @@ -87,6 +91,8 @@ def _get_object_facade(self, url): def _get_user_id(self): try: + if self.iot_call: + return True login_facade = self._get_object_facade("/xmlrpc/common") user_id = login_facade.login(self.db, self.user, self.pswd) if user_id: @@ -101,6 +107,14 @@ def _get_user_id(self): def check_attendance(self, card): try: + if self.iot_call: + return json.loads(requests.post( + '%s/iot/%s/action' % (self.url_template, self.user), + data={ + 'passphrase': self.pswd, + 'value': card, + }, + ).content.decode('utf-8')) object_facade = self._get_object_facade("/xmlrpc/object") if object_facade: res = object_facade.execute( diff --git a/lib/Tasks.py b/lib/Tasks.py index 7b3d51f..95faf45 100644 --- a/lib/Tasks.py +++ b/lib/Tasks.py @@ -26,7 +26,7 @@ def __init__(self, Odoo, Hardware): # 'are you sure?' upon selection self.get_ip = routes.get_ip self.can_connect = Odoo.can_connect - self.wifi_active = self.Clock.wifi_active + self.wifi_active = self.Clock.interface_active self.wifi_stable = self.Clock.wifi_stable # Menu vars diff --git a/requirements.txt b/requirements.txt index 1ab0bbd..5290c54 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ Flask luma.oled spi psutil -RPi.GPIO \ No newline at end of file +RPi.GPIO +requests diff --git a/templates/form.html b/templates/form.html index 95c69f6..b42013a 100644 --- a/templates/form.html +++ b/templates/form.html @@ -19,6 +19,8 @@

Configuration portal

If you use default HTTPS port 443 leave port field empty
+ +
From 2092f2c3a312e193adb14258228e2b7382edd695 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Tue, 23 Apr 2019 12:37:47 +0200 Subject: [PATCH 2/5] fixup! [ADD] ethernet functionality --- lib/Clocking.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Clocking.py b/lib/Clocking.py index b5141f4..69d213f 100755 --- a/lib/Clocking.py +++ b/lib/Clocking.py @@ -220,7 +220,7 @@ def clocking(self): begin_card_logging = time.perf_counter() # store the time when the card logging process begin - self.wifi_m = self.wifi_signal_msg() + self.wifi_m = self.get_interface_msg() if not self.interface_running(): self.msg = "ContactAdm" From 281ded151a8e98b6fc09101a41f026b1cbb6e8ac Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Wed, 17 Jul 2019 15:41:15 +0200 Subject: [PATCH 3/5] [FIX] lecture of cards --- lib/CardReader.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/CardReader.py b/lib/CardReader.py index bfcce20..290cc80 100644 --- a/lib/CardReader.py +++ b/lib/CardReader.py @@ -492,11 +492,7 @@ def scan_card(self): # If we have the UID, continue if status == self.MI_OK: - card = ( - hex(int(uid[0])).split("x")[-1] - + hex(int(uid[1])).split("x")[-1] - + hex(int(uid[2])).split("x")[-1] - + hex(int(uid[3])).split("x")[-1] - ) + card = '{:02x}{:02x}{:02x}{:02x}'.format( + uid[0], uid[1], uid[2], uid[3]) return card From c2c3929b9b7cd6b4e00a2803ecc86abc439c9bf8 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Wed, 17 Jul 2019 16:26:48 +0200 Subject: [PATCH 4/5] [FIX] iot url --- lib/OdooXMLrpc.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/OdooXMLrpc.py b/lib/OdooXMLrpc.py index 2122857..e5bfd89 100644 --- a/lib/OdooXMLrpc.py +++ b/lib/OdooXMLrpc.py @@ -63,20 +63,22 @@ def set_params(self): self.https_on = False else: self.https_on = True - - if self.https_on: - if self.port: - self.url_template = "https://%s:%s" % ( - self.host, - self.port, - ) - else: - self.url_template = "https://%s" % self.host + if self.iot_call: + self.url_template = self.host else: - if self.port: - self.url_template = "http://%s:%s" % (self.host, self.port) + if self.https_on: + if self.port: + self.url_template = "https://%s:%s" % ( + self.host, + self.port, + ) + else: + self.url_template = "https://%s" % self.host else: - self.url_template = "http://%s" % self.host + if self.port: + self.url_template = "http://%s:%s" % (self.host, self.port) + else: + self.url_template = "http://%s" % self.host self.uid = self._get_user_id() From cb628d8b6620c6a15b26aa6083b56100f29dbd67 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Thu, 1 Aug 2019 12:18:10 +0200 Subject: [PATCH 5/5] [FIX] initialization when eth0 --- lib/OdooXMLrpc.py | 1 + lib/Tasks.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/OdooXMLrpc.py b/lib/OdooXMLrpc.py index e5bfd89..89e9288 100644 --- a/lib/OdooXMLrpc.py +++ b/lib/OdooXMLrpc.py @@ -43,6 +43,7 @@ def set_params(self): self.https_on = False self.url_template = False self.uid = False + self.iot_call = False else: self.db = self.j_data["db"][0] self.user = self.j_data["user_name"][0] diff --git a/lib/Tasks.py b/lib/Tasks.py index 95faf45..a23ef7a 100644 --- a/lib/Tasks.py +++ b/lib/Tasks.py @@ -27,7 +27,7 @@ def __init__(self, Odoo, Hardware): self.get_ip = routes.get_ip self.can_connect = Odoo.can_connect self.wifi_active = self.Clock.interface_active - self.wifi_stable = self.Clock.wifi_stable + self.wifi_stable = self.Clock.interface_running # Menu vars self.begin_option = 0 # the Terminal begins with this option