PIVOT 1.1.0
This commit is contained in:
commit
8a53234eb6
161 files changed
+31109
No files matched your search
@@ -0,0 +1,81 @@
|
||||
import threading
|
||||
import time
|
||||
import requests
|
||||
import re
|
||||
from database import execute_query
|
||||
from config import Config
|
||||
from tenancy import school_singleton, met_school
|
||||
|
||||
|
||||
def lokaal_id(naam):
|
||||
"""Lokaal_ID voor een lokaalnaam; maakt het lokaal aan (zonder campus) als het nog niet bestaat."""
|
||||
from database import execute_insert
|
||||
rows = execute_query("SELECT Lokaal_ID FROM Lokalen WHERE Naam = %s ORDER BY Lokaal_ID LIMIT 1", (naam,))
|
||||
if rows:
|
||||
return rows[0]['Lokaal_ID']
|
||||
return execute_insert("INSERT INTO Lokalen (Naam) VALUES (%s)", (naam,))
|
||||
|
||||
|
||||
class CO2Scanner:
|
||||
def __new__(cls):
|
||||
# één scanner per school (elke school heeft een eigen netwerkbereik)
|
||||
def init(inst):
|
||||
inst.active = False
|
||||
inst.thread = None
|
||||
inst.auto_delete = False
|
||||
inst.conf = Config()
|
||||
return school_singleton(cls, init)
|
||||
#start service
|
||||
def start(self):
|
||||
if not self.active:
|
||||
self.active = True
|
||||
self.thread = threading.Thread(target=met_school(self._run_scan_loop), daemon=True)
|
||||
self.thread.start()
|
||||
print("CO2 Scanner Service gestart.")
|
||||
|
||||
#killswitch
|
||||
def stop(self):
|
||||
self.active = False
|
||||
print("CO2 Scanner Service gestopt.")
|
||||
#houd service in actie
|
||||
def _run_scan_loop(self):
|
||||
while self.active:
|
||||
self.perform_scan()
|
||||
# Wacht het aantal minuten uit je config.ini (bijv. 5)
|
||||
time.sleep(self.conf.CO2_INTERVAL * 60)
|
||||
#functie die effectieve taak uitvoerd
|
||||
def perform_scan(self):
|
||||
"""De logica uit je originele script om IP's af te gaan."""
|
||||
start_ip = self.conf.CO2_START_ADDR
|
||||
end_ip = self.conf.CO2_END_ADDR
|
||||
network = self.conf.CO2_NETWORK
|
||||
if not network:
|
||||
return
|
||||
|
||||
for i in range(start_ip, end_ip + 1):
|
||||
if not self.active and threading.current_thread() == self.thread:
|
||||
break
|
||||
|
||||
ip = f"{network}{i}"
|
||||
url = f"http://{ip}/v1/constellation"
|
||||
|
||||
try:
|
||||
# Time-out kort houden zodat de scan niet uren duurt
|
||||
response = requests.get(url, timeout=1.5)
|
||||
if response.status_code == 200:
|
||||
data = response.text
|
||||
# Regex uit je originele script
|
||||
conc_match = re.search(r'"concentration":\s*{\s*"unit":\s*"ppm",\s*"value":\s*(\d+)', data)
|
||||
name_match = re.search(r'"device_name":\s*{\s*"value":\s*"(.*?)"', data)
|
||||
|
||||
if conc_match:
|
||||
ppm = int(conc_match.group(1))
|
||||
lokaal = name_match.group(1) if name_match else ip
|
||||
|
||||
# Opslaan in DB (lokaal wordt aangemaakt als het nog niet bestaat)
|
||||
execute_query(
|
||||
"INSERT INTO CO2_Metingen (Lokaal_ID, ppm, Timestamp) VALUES (%s, %s, NOW())",
|
||||
(lokaal_id(lokaal), ppm), commit=True
|
||||
)
|
||||
except:
|
||||
continue
|
||||
Reference in new issue
Block a user