Skip to content

Commit

Permalink
Röle Yönetimi
Browse files Browse the repository at this point in the history
  • Loading branch information
omersavas26 committed Jul 16, 2021
1 parent 00ee3df commit 180edff
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 54 deletions.
20 changes: 13 additions & 7 deletions auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}
Expand Down
77 changes: 60 additions & 17 deletions lib/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +29,8 @@

import picamera

from variables import *



#### VARIABLES ####
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -76,6 +81,7 @@
rfid_same_count = 1

request_using = False
requests_timeout = 5

users = {}

Expand Down Expand Up @@ -210,14 +216,23 @@ def rfid_init():
rfid = MFRC522.MFRC522()

def log(l, s):
global debug

if debug:
_log(l, s)
elif l > 1:
_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):
Expand Down Expand Up @@ -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+")")
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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.")
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions lib/variables.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 180edff

Please sign in to comment.