Vklop izklop security kamer

security virtualna masina

Princip je tak:

crontab:

tine@security:~$ crontab -l
# run Security Cam script every minute
0-59 5-23 * * *   /usr/bin/python /home/tine/bin/CheckSyslogForSecurityCams.py
0 0 * * *   /usr/bin/python /home/tine/bin/EnableSecurityCams.py
58 4 * * *   /usr/bin/python /home/tine/bin/DisableSecurityCams.py


# run script after system (re)boot
@reboot       /usr/bin/python /home/tine/bin/CheckSyslogForSecurityCams.py
@reboot (sleep 30s ; cd /home/tine ; /usr/bin/docker-compose up -d )&

skripta za preverjanje alarma:

tine@security:~$ cat /home/tine/bin/CheckSyslogForSecurityCams.py
##############################
# Skripta, ki preverja syslog, ce je Loxone poslal request za alarm
# in na osnovi tega vklaplja/izklaplja posamezne security kamere
#
# /tine 08.02.2019

import requests
import json
import base64
from datetime import datetime

#cameras
camVhod = "http://192.168.100.201/api/v1/condition/enabled"
camZadaj = "http://192.168.100.201:81/api/v1/condition/enabled"
camDnevna = "http://192.168.100.201:82/api/v1/condition/enabled"

username = "root"
password = "SuprSekjur42"
basicAuth = base64.b64encode('%s:%s' % (username, password))
headers = {"Authorization": "Basic " + basicAuth, "Content-Type": "application/json"}

logFile = open("/home/tine/SecurityCam.log", "a")
time = datetime.now().strftime('%Y/%m/%d %H:%M:%S :: ')


# Kaksno je prejsnje stanje?
with open('/home/tine/SecurityCam.log', 'r') as prej:
    lines = prej.read().splitlines()
    prej = lines[-1]
    prejsnjeStanje = prej.split(":: ")

# Preberi zadnjo vrstico iz Sysloga, ki ga posilja Loxone
with open('/var/log/192.168.100.3.log', 'r') as f:
    lines = f.read().splitlines()
    last_line = lines[-1]
    last_line2 = last_line.split(";")
    zeljenoStanje = last_line2[2].split("#")

# Vklopi kamere ne glede na vse (rocno vklopljene preko Loxone, dopusti ipd)
if int(zeljenoStanje[0]) == 1 and prejsnjeStanje[1] == "KamereIzklopljene":
    data = '{"active": "true"}'
    response = requests.put(camVhod, data=data, headers=headers)
    response = requests.put(camZadaj, data=data, headers=headers)
    response = requests.put(camDnevna, data=data, headers=headers)
    logFile.write(time + "KamereVklopljene\n")

# Izklopi kamere ne glede na vse (rocno vklopljene preko Loxone, dopusti ipd)
if int(zeljenoStanje[0]) == 0 and prejsnjeStanje[1] == "KamereVklopljene":
    data = '{"active": "false"}'
    response = requests.put(camVhod, data=data, headers=headers)
    response = requests.put(camZadaj, data=data, headers=headers)
    response = requests.put(camDnevna, data=data, headers=headers)
    logFile.write(time + "KamereIzklopljene\n")



logFile.close()

skripta za vklop:

tine@security:~$ cat /home/tine/bin/EnableSecurityCams.py

##############################
## Skripta, ki vklopi security kamere
##
## /tine 08.02.2019

import requests
import json
import base64
from datetime import datetime

#cameras
camVhod = "http://192.168.100.201/api/v1/condition/enabled"
camZadaj = "http://192.168.100.201:81/api/v1/condition/enabled"
camDnevna = "http://192.168.100.201:82/api/v1/condition/enabled"

username = "root"
password = "SuprSekjur42"
basicAuth = base64.b64encode('%s:%s' % (username, password))
headers = {"Authorization": "Basic " + basicAuth, "Content-Type": "application/json"}

logFile = open("/home/tine/SecurityCam.log", "a")
time = datetime.now().strftime('%Y/%m/%d %H:%M:%S :: ')


# Vklopi kamere
data = '{"active": "true"}'
response = requests.put(camVhod, data=data, headers=headers)
response = requests.put(camZadaj, data=data, headers=headers)
response = requests.put(camDnevna, data=data, headers=headers)

logFile.write(time + "KamereVklopljene\n")

logFile.close()