diff --git a/auth.py b/auth.py index d3ce5f3..ab98951 100644 --- a/auth.py +++ b/auth.py @@ -29,7 +29,8 @@ def get_device_unique(): ssh_key = log_and_run(read_ssh_key) params ={ 'ssh_anahtar': ssh_key } - data = http_post(api_base_url+"rfidCihazAnahtarGetir", data=params, j=True) + url = log_and_run(get_url_for_deg_device_key) + data = http_post(url, data=params, j=True) if data == None: log(ERROR, "Yeni anahtar alinamadi! (data: None, "+to_json_str(params)+")") return "" @@ -46,12 +47,14 @@ def get_device_unique(): return macs[0] + serial + macs[1] + key def get_auth(): + global device_table_name + unique = log_and_run(get_device_unique) if unique == "": return "" params = { - "type": "rfid_cihazlar", + "type": device_table_name, "unique": unique } @@ -68,20 +71,23 @@ def get_auth(): def run(): auth = log_and_run(get_auth) - if auth != "": data = { "token": auth["token"], - "id": auth["device"]["id"], - "name": auth["device"]["name"] + "id": auth["device"]["id"] } + + try: + data["name"] = auth["device"]["name"] + except Exception as e: + data["name"] = auth["device"]["name_basic"] write_to_file(h.auth_file_path, data, j = True) else: log_and_run(add_as_new_device) def add_as_new_device(): - global api_base_url + global api_base_url, new_device_column_set_id ips = log_and_run(get_ip_adresses) macs = log_and_run(get_mac_adresses) @@ -103,7 +109,7 @@ def add_as_new_device(): } params = { - "column_set_id": 120, + "column_set_id": new_device_column_set_id, "detail": to_json_str(detail), "id": 0 } diff --git a/lib/helper.py b/lib/helper.py index 30836f1..1ebc5d8 100644 --- a/lib/helper.py +++ b/lib/helper.py @@ -3,7 +3,11 @@ import sys import os import logging + import requests +from requests.packages.urllib3.exceptions import InsecureRequestWarning +requests.packages.urllib3.disable_warnings(InsecureRequestWarning) + import json import signal import threading @@ -25,6 +29,8 @@ import picamera +from variables import * + #### VARIABLES #### @@ -34,6 +40,7 @@ log_base_path = "/var/www/html/log/" lib_base_path = "/var/www/html/lib/" app_base_path = "/var/www/html/" +dateTimeSync_py_file_path = "/var/www/html/dateTimeSync.py" auth_file_path = "/var/www/html/file/auth.json" users_list_path = "/var/www/html/file/users.json" auth_py_file_path = "/var/www/html/auth.py" @@ -49,8 +56,6 @@ started = False debug = False loop = False -api_base_url = "https://kamu.kutahya.gov.tr/api/v1/" -update_base_url = "https://kamu.kutahya.gov.tr/uploads/2021/01/01/mesaiTakipCihazi/" last_response = None INFO = 1 @@ -76,6 +81,7 @@ rfid_same_count = 1 request_using = False +requests_timeout = 5 users = {} @@ -210,6 +216,8 @@ def rfid_init(): rfid = MFRC522.MFRC522() def log(l, s): + global debug + if debug: _log(l, s) elif l > 1: @@ -217,7 +225,14 @@ def log(l, s): def _log(l, s): global logger - logger.info("%s", str(s)) + + if l == 3: + logger.error("%s", str(s)) + elif l == 2: + logger.warning("%s", str(s)) + else: + logger.info("%s", str(s)) + print("LOG -> level:" + str(l) + " - " + str(s)) def log_e(e, m): @@ -499,9 +514,8 @@ def download_file(url, path, sllDisable = False): except Exception as e: return False -def http_get(url, return_raw = False, j = False, force = False): - - global last_response, connected, lib_base_path, request_using +def http_get(url, return_raw = False, j = False, force = False, to = requests_timeout): + global last_response, connected, lib_base_path, request_using, sll_verify try: log(INFO, "Http GET ("+url+")") @@ -513,7 +527,7 @@ def http_get(url, return_raw = False, j = False, force = False): last_response = None request_using = True - rt = requests.get(url, verify= False) + rt = requests.get(url, verify=sll_verify, timeout=to) last_response = rt.text if return_raw: @@ -543,21 +557,20 @@ def http_get(url, return_raw = False, j = False, force = False): log_e(e, "Http GET ERROR (url: " + url + ", text: "+text+")") last_response = None -def http_post(url, data = None, files = None, return_raw = False, j = False, force = False): - - global last_response, lib_base_path, request_using, connected +def http_post(url, data = None, files = None, return_raw = False, j = False, force = False, to = requests_timeout): + global last_response, lib_base_path, request_using, connected, sll_verify try: log(INFO, "Http POST ("+url+")") if connected == False and force == False: - log(INFO, "Http GET by pass") + log(INFO, "Http POST by pass") return last_response = None request_using = True - rt = requests.post(url, data=data, files=files, verify=False) + rt = requests.post(url, data=data, files=files, verify=sll_verify, timeout=to) last_response = rt.text if return_raw: @@ -596,6 +609,18 @@ def get_url_for_rfid_readed(card_id, date_time = "now"): return api_base_url + "device/" + auth["token"] + "/rfidReaded?card_id=" + encode_url(card_id) + "&time="+encode_url(date_time) +def get_url_for_deg_device_key(): + global api_base_url, get_device_key_url_path + return api_base_url + get_device_key_url_path + +def get_url_for_last_activity(): + global api_base_url, auth + + if auth == None: + return api_base_url + + return api_base_url + "device/" + auth["token"] + "/lastActivity" + def get_url_for_send_photo(): global api_base_url, auth @@ -655,7 +680,7 @@ def restart_ip(): def connection_control(): log(INFO, "Connection control") - global api_base_url, connected, loop, connection_control_timer_period, request_using, connection_controling, started, fileName + global connected, loop, connection_control_timer_period, request_using, connection_controling, started, fileName temp = threading.Timer(connection_control_timer_period, connection_control) temp.daemon = True @@ -672,7 +697,9 @@ def connection_control(): connection_controling = True try: - data = http_get(api_base_url, j=True, force=True) + + url = log_and_run(get_url_for_last_activity) + data = http_get(url, j=True, force=True) if data == None: log(INFO, "Connection control error.") @@ -705,6 +732,8 @@ def connection_control(): log(INFO, "Connection changed False to True. Try fill auth and users") + log_and_run(sync_date_time) + temp = log_and_run(fill_auth, True) if temp == True: temp = log_and_run(fill_users, True) @@ -719,16 +748,24 @@ def connection_control(): connected = False log_e(e, "Connection control error (ex).") - connection_controling = False + connection_controling = False def to_json_str(o): - return json.dumps(o) + try: + return json.dumps(o) + except Exception as e: + log_e(e, "Json parse error in to_json_str"); + return "" def from_json_str(str): try: return json.load(str) except Exception as e: - return json.loads(str) + try: + return json.loads(str) + except Exception as e: + log_e(e, "Json encode error in from_json_str"); + return None def create_dir_if_not_exist(dir): if os.path.isdir(dir) == False: @@ -786,6 +823,12 @@ def read_from_file(file_path, j=False): except Exception as e: log_e(e, "Read from file error ("+file_path+")") return None + +def sync_date_time(): + global dateTimeSync_py_file_path + + cmd = "python3 "+dateTimeSync_py_file_path + command(cmd) def fill_auth(force = False): global auth_py_file_path, auth_file_path, auth diff --git a/lib/variables.py b/lib/variables.py new file mode 100644 index 0000000..8bce0b8 --- /dev/null +++ b/lib/variables.py @@ -0,0 +1,14 @@ +new_device_column_set_id = 120 +#sll_verify = False +sll_verify = lib_base_path+"kamu.ca-bundle" +api_base_url = "https://xxx.com/api/v1/" +update_base_url = "https://xxx.com/uploads/mesaiTakipCihazi/" +get_device_key_url_path = "mesaiTakipCihaziAnahtarGetir" +device_table_name = "mesai_takip_cihazlar" + +try: + from localVariables import * +except Exception as e: + rfid_read_mode_async = True + same_rfid_read_control = True + relay_only_true_user = False \ No newline at end of file diff --git a/main.py b/main.py index 2fdfab3..c8def4e 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ -##@reboot sleep 10 && python3 /var/www/html/main.py & +##@reboot sleep 10 && python3 /var/www/html/main.py & +##2021-07-16 12:00:00 import os import sys @@ -29,11 +30,14 @@ read_rfid_timer_period = 0.25 local_last_rfid_card_id = "" -same_rfid_read_control = True -local_debug = True +last_rfid_card_id_remember_timeout = 75.0 +local_debug = False sendPhotoForever = False +tanimsiz_kullanici_ad = "Tan\u0131ms\u0131z" +tanimsiz_kullanici_soyad = "Kullan\u0131c\u0131" + try: def clear_pins(): global buzzer_pin, red_led, green_led, triger_pin @@ -56,11 +60,13 @@ def on_load(): else: log_and_run(fill_users) - log_and_run(connection_control) - if temp == False: log(INFO, "Users or Auth was failed") h.connected = False + else: + h.connected = True + + log_and_run(connection_control) log_and_run(fill_scene) @@ -325,9 +331,7 @@ def send_photo(file): record_token = temp[1] url = get_url_for_send_photo() - - print(id + " / " + record_token) - + params = { "id": id, "record_token": record_token } files = {'fotograf[]': open(h.base_photo_path+file, 'rb')} @@ -356,7 +360,7 @@ def display_message(msg, time_out = 1): display_using = False def same_rfid_read(): - global display_using, green_led + global display_using if h.rfid_same_count == 4: log(INFO, "show_ip_on_display will start") @@ -367,9 +371,7 @@ def same_rfid_read(): elif h.rfid_same_count == 8: display_using = False log_and_run(reboot_os) - return - else: - log_and_run(show_rfid_same_card_message_on_display) + return def rfid_readed_request_async(card_id, photo_name): global sendPhotoForever @@ -392,16 +394,13 @@ def rfid_readed_request_async(card_id, photo_name): log(WARNING, "rfid_readed_request_async by pass (data)") def rfid_readed_request(params): - global sendPhotoForever - - t = "Tan\u0131ms\u0131z" - k = "Kullan\u0131c\u0131" - + global sendPhotoForever, tanimsiz_kullanici_ad, tanimsiz_kullanici_soyad + try: url = get_url_for_rfid_readed(params["card_id"]) if url == "": log(WARNING, "rfid_readed_request by pass") - return (t, k) + return (tanimsiz_kullanici_ad, tanimsiz_kullanici_soyad) log_and_run(unlimit_bandwith) data = http_get(url, j=True) @@ -415,8 +414,8 @@ def rfid_readed_request(params): log(INFO, "rfid_readed_request OK") if data["data"]["user"]["name"] == None: - data["data"]["user"]["name"] = t - data["data"]["user"]["surname"] = k + data["data"]["user"]["name"] = tanimsiz_kullanici_ad + data["data"]["user"]["surname"] = tanimsiz_kullanici_soyad return (data["data"]["user"]["name"], data["data"]["user"]["surname"]) @@ -427,9 +426,23 @@ def rfid_readed_request(params): except Exception as e: log_e(e, "read_rfid rfid_readed_request") return (t, k) + + def clear_local_last_rfid_card_id(): + global local_last_rfid_card_id, local_debug + + temp = h.debug + if local_debug: + h.debug = True + + log(INFO, "clear_local_last_rfid_card_id running...") + local_last_rfid_card_id = "" + log(INFO, "clear_local_last_rfid_card_id OK") + + if local_debug: + h.debug = temp def read_rfid(): - global green_led, display_using, read_rfid_timer_period, local_last_rfid_card_id, local_debug, same_rfid_read_control + global green_led, display_using, read_rfid_timer_period, local_last_rfid_card_id, local_debug, same_rfid_read_control, last_rfid_card_id_remember_timeout, rfid_read_mode_async, relay_only_true_user, tanimsiz_kullanici_ad, tanimsiz_kullanici_soyad while True: if h.loop == False: break @@ -446,18 +459,27 @@ def read_rfid(): display_using = True - if same_rfid_read_control and local_last_rfid_card_id == card_id: + if local_last_rfid_card_id == card_id: log_and_run(same_rfid_read) - else: - pin_on(green_led, 1) + + if same_rfid_read_control and local_last_rfid_card_id == card_id: + log_and_run(show_rfid_same_card_message_on_display) + else: + if relay_only_true_user == False: + pin_on(green_led, 1) local_last_rfid_card_id = card_id - pin_on(buzzer_pin, time_out = 0.05) - pin_on(triger_pin, time_out = 0.05) + temp = threading.Timer(last_rfid_card_id_remember_timeout, clear_local_last_rfid_card_id) + temp.daemon = True + temp.start() + + if relay_only_true_user == False: + pin_on(buzzer_pin, time_out = 0.05) + pin_on(triger_pin, time_out = 0.05) - if card_id in h.users: + if rfid_read_mode_async and card_id in h.users: log_and_run_async(rfid_readed_request_async, [card_id, photo_name]) name = h.users[card_id]["name"] surname = h.users[card_id]["surname"] @@ -466,6 +488,13 @@ def read_rfid(): (name, surname) = log_and_run(rfid_readed_request, {"card_id": card_id, "photo_name": photo_name}) + if name != tanimsiz_kullanici_ad and surname != tanimsiz_kullanici_soyad and relay_only_true_user: + pin_on(green_led, 1) + pin_on(buzzer_pin, time_out = 0.05) + pin_on(triger_pin, time_out = 0.05) + elif rfid_read_mode_async == False: + pin_on(red_led, 1) + log_and_run(clear_display) write_text_on_center_of_display(name, y = 2, font_size = 20) diff --git a/send.py b/send.py index a437369..7b4ea64 100644 --- a/send.py +++ b/send.py @@ -77,7 +77,7 @@ def run(): files = {'fotograf[]': open(h.base_photo_path+"ID"+id+"_"+record_token+".jpg", 'rb')} log_and_run(limit_bandwith) - data = http_post(url, data = params, files = files, j = True) + data = http_post(url, data = params, files = files, j = True, to=100.0) if data != None: if data["status"] == "success": @@ -86,7 +86,7 @@ def run(): log(WARNING, "Send file request end with error: " + to_json_str(data)) except Exception as e: - print(e) + log_e(e, "Send photo error") temp = None diff --git a/update.py b/update.py index 212c442..a3483ac 100644 --- a/update.py +++ b/update.py @@ -11,6 +11,8 @@ file_name = os.path.basename(__file__) +local_debug = True + try: def version_control(): version_file = "version.txt" @@ -65,6 +67,8 @@ def run(): preload(file_name) + h.debug = local_debug + if h.connected: log_and_run(run) else: diff --git a/version.txt b/version.txt index 3bad788..7c32728 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.0.12 \ No newline at end of file +2.1.1 \ No newline at end of file