backup current state
This commit is contained in:
parent
b01d732f72
commit
f110264fc7
7 changed files with 78 additions and 49 deletions
58
schalter.py
58
schalter.py
|
@ -1,49 +1,43 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#########################################################################
|
#########################################################################
|
||||||
# Spaceapi Implementation for a 63A switch
|
# Spaceapi Implementation for a 63A switch
|
||||||
# (using 5 V 150 mA Raspberry Pi GPIO pin)
|
# (using 5 V 150 mA Raspberry Pi GPIO pin)
|
||||||
#
|
|
||||||
#
|
#
|
||||||
#########################################################################
|
#########################################################################
|
||||||
|
|
||||||
# to make sure that everything does not happen at onec
|
# to make sure that everything does not happen at onec
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
# json and raspi imports
|
# json and raspi imports
|
||||||
import json
|
import json
|
||||||
# for using GPIO pins of the raspi
|
# for using GPIO pins of the raspi
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
# for logging
|
# for logging
|
||||||
import logging
|
import logging
|
||||||
import systemd.journal
|
from systemd import journal
|
||||||
|
|
||||||
########################################################
|
########################################################
|
||||||
|
|
||||||
#start init
|
#start init
|
||||||
log = logging.getLogger("SpaceAPI Schalter") #create a logger
|
journal.write("CCCHB space api switch v. 0.3 ")
|
||||||
log_fmt = logging.Formatter("%(levelname)s %(message)s") #define logging format
|
currentState = bool(0)
|
||||||
log_ch = JournalHandler() # create logging Handler
|
|
||||||
log_ch.setFormatter(log_fmt)
|
|
||||||
log.addHandler(log_ch)
|
|
||||||
|
|
||||||
log.setLevel(logging.INFO)
|
|
||||||
log.info("CCCHB space api switch v. 0.3 ")
|
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
#function definitons:
|
#function definitons:
|
||||||
#################################################################################
|
#################################################################################
|
||||||
|
|
||||||
# will read the GPIO header of the pi and write the current state in the www spaceapi json file
|
# will read the GPIO header of the pi and write the current state in the www spaceapi json file
|
||||||
def switched(pos):
|
def switched(pos):
|
||||||
|
global currentState
|
||||||
wert_des_schalters = not bool(GPIO.input(pin_number))
|
wert_des_schalters = not bool(GPIO.input(pin_number))
|
||||||
icons= data.get("state").get('icon')
|
if (pos != wert_des_schalters):
|
||||||
if (pos!= wert_des_schalters):
|
journal.write("state changed to: %s " %wert_des_schalters)
|
||||||
log.info("state changed to: ", wert_des_schalters)
|
|
||||||
chn_time = datetime.datetime.now().isoformat()
|
chn_time = datetime.datetime.now().isoformat()
|
||||||
|
icons= data.get("state").get('icon')
|
||||||
data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}})
|
data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}})
|
||||||
|
currentState= wert_des_schalters
|
||||||
with open('/var/www/html/spaceapi.json', 'w') as outfile:
|
with open('/var/www/html/spaceapi.json', 'w') as outfile:
|
||||||
json.dump(data, outfile,sort_keys=True)
|
json.dump(data, outfile,sort_keys=True)
|
||||||
return wert_des_schalters
|
|
||||||
|
|
||||||
#################################################################################
|
#################################################################################
|
||||||
|
|
||||||
|
@ -51,8 +45,8 @@ def switched(pos):
|
||||||
pin_number = 18; #set GPIO Pin number
|
pin_number = 18; #set GPIO Pin number
|
||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200)
|
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=600)
|
||||||
log.info("init complete... starting json foo")
|
journal.write("init complete... starting json foo")
|
||||||
#read json file
|
#read json file
|
||||||
with open('spaceapi.json','r') as infile:
|
with open('spaceapi.json','r') as infile:
|
||||||
data = json.load(infile)
|
data = json.load(infile)
|
||||||
|
@ -61,13 +55,11 @@ json.JSONDecoder(data) #decode json to python (may not be neccecary.)
|
||||||
|
|
||||||
#run switched once to get current switch status and change if needed.
|
#run switched once to get current switch status and change if needed.
|
||||||
#switched(wert_des_schalters)
|
#switched(wert_des_schalters)
|
||||||
currentValue = bool(0)
|
try:
|
||||||
try:
|
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
time.sleep(5)
|
||||||
currentValue = switched(currentValue)
|
switched(currentState)
|
||||||
|
#currentValue = #include IRCBOT status here?
|
||||||
#include IRCBOT status here?
|
#do other stuff otherwise just don't do anything?!
|
||||||
#do other stuff otherwise just don't do anything?!
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
|
11
sopel-bot/default.cfg
Normal file
11
sopel-bot/default.cfg
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
[core]
|
||||||
|
nick = ccchb-schalter
|
||||||
|
host = irc.hackint.org
|
||||||
|
use_ssl = true
|
||||||
|
verify_ssl = false
|
||||||
|
port = 6697
|
||||||
|
owner = ccchb-schalter
|
||||||
|
channels = #ccchb
|
||||||
|
extra = /home/pi/Eingangsschalter/ircbot/
|
||||||
|
enable = spaceapi
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
[core]
|
|
||||||
verify_ssl = false
|
|
|
@ -9,12 +9,12 @@
|
||||||
"logo": "https://ccchb.de/logo/CCCHB-logo_256x256_bw.png",
|
"logo": "https://ccchb.de/logo/CCCHB-logo_256x256_bw.png",
|
||||||
"space": "CCCHB",
|
"space": "CCCHB",
|
||||||
"state": {
|
"state": {
|
||||||
|
"lastchange": 1518643002.5509002,
|
||||||
|
"open": false,
|
||||||
"icon": {
|
"icon": {
|
||||||
"closed": "https://ccchb.de/w/images/9/9e/SpaceClosed.svg",
|
"open": "https://files.ccchb.de/assets/SpaceOpen.svg",
|
||||||
"open": "https://ccchb.de/w/images/9/95/SpaceOpen.svg"
|
"closed": "https://files.ccchb.de/assets/SpaceClosed.svg"
|
||||||
},
|
}
|
||||||
"lastchange": 1534869423.371439,
|
|
||||||
"open": false
|
|
||||||
},
|
},
|
||||||
"url": "https://ccchb.de/"
|
"url": "https://ccchb.de/"
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Schalter IRC Bot
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/bin/sopel
|
||||||
|
User=pi
|
||||||
|
Restart=always
|
||||||
|
RestartSec=3
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
|
@ -0,0 +1,13 @@
|
||||||
|
[Unit]
|
||||||
|
Description=Eingangsschalter
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/home/pi/Eingangsschalter/schalter.py
|
||||||
|
WorkingDirectory=/home/pi/Eingangsschalter
|
||||||
|
User=pi
|
||||||
|
Restart=always
|
||||||
|
RestartSec=3
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
background-color: black;
|
background-color: black;
|
||||||
color: white;
|
color: white;
|
||||||
|
@ -42,15 +43,15 @@ div#content {
|
||||||
width: 40%;
|
width: 40%;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<title>CCCHB is open</title></head>
|
</head>
|
||||||
<body class="offline">
|
<body>
|
||||||
<nav>
|
<nav>
|
||||||
<img id="logo" src="">
|
<img id="logo" src=""/>
|
||||||
<span id="name"></span>
|
<span id="name"></span>
|
||||||
<button id="refresh" type="button" onclick="load()">Update</button>
|
<button id="refresh" type="button" onclick="load()">Update</button>
|
||||||
</nav>
|
</nav>
|
||||||
<div id="content">
|
<div id="content">
|
||||||
<img id="state" src="">
|
<img id="state" src=""/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
@ -66,7 +67,7 @@ function load() {
|
||||||
|
|
||||||
var stateurl = result.state.open ? result.state.icon.open : result.state.icon.closed;
|
var stateurl = result.state.open ? result.state.icon.open : result.state.icon.closed;
|
||||||
document.getElementById("state").src = stateurl;
|
document.getElementById("state").src = stateurl;
|
||||||
|
|
||||||
var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
|
var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
|
||||||
link.type = 'image/x-icon';
|
link.type = 'image/x-icon';
|
||||||
link.rel = 'shortcut icon';
|
link.rel = 'shortcut icon';
|
||||||
|
@ -76,7 +77,8 @@ function load() {
|
||||||
var body = document.querySelector("body");
|
var body = document.querySelector("body");
|
||||||
if (!result.state.open) {
|
if (!result.state.open) {
|
||||||
body.classList.add("closed");
|
body.classList.add("closed");
|
||||||
} else{
|
}
|
||||||
|
else{
|
||||||
body.classList.remove("closed");
|
body.classList.remove("closed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,5 +86,7 @@ function load() {
|
||||||
load();
|
load();
|
||||||
window.setInterval(load, 60 * 1000);
|
window.setInterval(load, 60 * 1000);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue