Remove example data, catch errors

This commit is contained in:
Jonathan Flueren 2023-08-17 21:23:49 +02:00
parent 99ed992b22
commit 6d6d242346
4 changed files with 18 additions and 15 deletions

View file

@ -7,12 +7,13 @@ import json
import hmac import hmac
import hashlib import hashlib
IDENTITIES_URL = "http://localhost:8080/identities.json" BASE_URL = "http://localhost:8080/"
WEBHOOK_URL = "http://localhost:8080/update.php"
WEBHOOK_SECRET = "CHANGE-THIS" WEBHOOK_SECRET = "CHANGE-THIS"
WIFI_MAP_PATH = "wifi_map.yaml"
def get_identities(): def get_identities():
resp = requests.get(IDENTITIES_URL) identities_url = BASE_URL.rstrip('/') + '/identities.json'
resp = requests.get(identities_url)
return resp.json() return resp.json()
@ -47,12 +48,8 @@ def parse_wifi_map(map_path):
return filtered_identities return filtered_identities
if __name__ == '__main__': if __name__ == '__main__':
wifi_map_path = 'wifi_map.yaml' filtered_identities = parse_wifi_map(WIFI_MAP_PATH)
if len(sys.argv) > 1:
wifi_map_path = sys.argv[1]
filtered_identities = parse_wifi_map(wifi_map_path)
# build request # build request
json_payload = json.dumps(list(filtered_identities)).encode("utf-8") json_payload = json.dumps(list(filtered_identities)).encode("utf-8")
@ -68,8 +65,10 @@ if __name__ == '__main__':
'x-hmac-hash': signature 'x-hmac-hash': signature
} }
webhook_url = BASE_URL.rstrip('/') + '/update.php'
# send request # send request
r = requests.post(url=WEBHOOK_URL, data=json_payload, headers=req_headers) r = requests.post(url=webhook_url, data=json_payload, headers=req_headers)
print("Upload response:") print("Upload response:")
print(r.status_code) print(r.status_code)

View file

@ -1 +0,0 @@
[{"name":"Null","mac_hash":"38fbdde984330e50c02382e647c576b71f41cc5c45b193d4f3177e6ee8f22a78"},{"name":"BROADCAST","mac_hash":"ef85d972b07fccdd79085ddb4713cd487c3838e128a7c4d11092909675c2022d"}]

View file

@ -8,7 +8,9 @@ define('NAME_PATTERN', "^([ a-zA-Z0-9'\-]){1,30}$");
define('MAC_PATTERN', "^([0-9A-Fa-f]{2}[:-s]){5}([0-9A-Fa-f]{2})$"); define('MAC_PATTERN', "^([0-9A-Fa-f]{2}[:-s]){5}([0-9A-Fa-f]{2})$");
define('PAGE_TITLE', 'CZI Presence Detector'); define('PAGE_TITLE', 'CZI Presence Detector');
$present = json_decode(file_get_contents(PRESENT_FILE), true); $present = file_exists(PRESENT_FILE)
? json_decode(file_get_contents(PRESENT_FILE), true)
: ["timestamp" => 0, "names" => []];
$tz = 'Europe/Berlin'; $tz = 'Europe/Berlin';
$datetime = new DateTime("now", new DateTimeZone($tz)); $datetime = new DateTime("now", new DateTimeZone($tz));
@ -34,7 +36,9 @@ function add_identity($name, $mac)
$hashed_mac = hash_mac($mac); $hashed_mac = hash_mac($mac);
$identities = json_decode(file_get_contents(IDENTITIES_FILE), true); $identities = file_exists(IDENTITIES_FILE)
? json_decode(file_get_contents(IDENTITIES_FILE), true)
: [];
$url = strtok($_SERVER['REQUEST_URI'], '?'); $url = strtok($_SERVER['REQUEST_URI'], '?');
@ -71,7 +75,9 @@ function remove_identity($name)
$url = strtok($_SERVER['REQUEST_URI'], '?'); $url = strtok($_SERVER['REQUEST_URI'], '?');
$identities = json_decode(file_get_contents(IDENTITIES_FILE), true); $identities = file_exists(IDENTITIES_FILE)
? json_decode(file_get_contents(IDENTITIES_FILE), true)
: [];
$new_identities = array(); $new_identities = array();

View file

@ -1 +0,0 @@
{"timestamp":1691957288,"names":["Jon"]}