backup current state

This commit is contained in:
Martin/Geno 2018-08-22 19:03:34 +02:00
parent b01d732f72
commit f110264fc7
No known key found for this signature in database
GPG key ID: 9D7D3C6BFF600C6A
7 changed files with 78 additions and 49 deletions

View file

@ -1,49 +1,43 @@
#!/usr/bin/env python3
#########################################################################
# Spaceapi Implementation for a 63A switch
# (using 5 V 150 mA Raspberry Pi GPIO pin)
#
# Spaceapi Implementation for a 63A switch
# (using 5 V 150 mA Raspberry Pi GPIO pin)
#
#########################################################################
# to make sure that everything does not happen at onec
import time
import datetime
# to make sure that everything does not happen at onec
import time
import datetime
# json and raspi imports
import json
# for using GPIO pins of the raspi
# for using GPIO pins of the raspi
import RPi.GPIO as GPIO
# for logging
import logging
import systemd.journal
# for logging
import logging
from systemd import journal
########################################################
#start init
log = logging.getLogger("SpaceAPI Schalter") #create a logger
log_fmt = logging.Formatter("%(levelname)s %(message)s") #define logging format
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 ")
#start init
journal.write("CCCHB space api switch v. 0.3 ")
currentState = bool(0)
#################################################################################
#function definitons:
#function definitons:
#################################################################################
# will read the GPIO header of the pi and write the current state in the www spaceapi json file
def switched(pos):
global currentState
wert_des_schalters = not bool(GPIO.input(pin_number))
icons= data.get("state").get('icon')
if (pos!= wert_des_schalters):
log.info("state changed to: ", wert_des_schalters)
if (pos != wert_des_schalters):
journal.write("state changed to: %s " %wert_des_schalters)
chn_time = datetime.datetime.now().isoformat()
icons= data.get("state").get('icon')
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:
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
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200)
log.info("init complete... starting json foo")
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=600)
journal.write("init complete... starting json foo")
#read json file
with open('spaceapi.json','r') as 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.
#switched(wert_des_schalters)
currentValue = bool(0)
try:
try:
while True:
time.sleep(1)
currentValue = switched(currentValue)
#include IRCBOT status here?
#do other stuff otherwise just don't do anything?!
time.sleep(5)
switched(currentState)
#currentValue = #include IRCBOT status here?
#do other stuff otherwise just don't do anything?!
except KeyboardInterrupt:
GPIO.cleanup()

11
sopel-bot/default.cfg Normal file
View 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

View file

@ -1,2 +0,0 @@
[core]
verify_ssl = false

View file

@ -9,12 +9,12 @@
"logo": "https://ccchb.de/logo/CCCHB-logo_256x256_bw.png",
"space": "CCCHB",
"state": {
"lastchange": 1518643002.5509002,
"open": false,
"icon": {
"closed": "https://ccchb.de/w/images/9/9e/SpaceClosed.svg",
"open": "https://ccchb.de/w/images/9/95/SpaceOpen.svg"
},
"lastchange": 1534869423.371439,
"open": false
"open": "https://files.ccchb.de/assets/SpaceOpen.svg",
"closed": "https://files.ccchb.de/assets/SpaceClosed.svg"
}
},
"url": "https://ccchb.de/"
}

View file

@ -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

View file

@ -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

View file

@ -1,6 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<style>
<head>
<style>
body {
background-color: black;
color: white;
@ -42,15 +43,15 @@ div#content {
width: 40%;
}
</style>
<title>CCCHB is open</title></head>
<body class="offline">
</head>
<body>
<nav>
<img id="logo" src="">
<img id="logo" src=""/>
<span id="name"></span>
<button id="refresh" type="button" onclick="load()">Update</button>
</nav>
<div id="content">
<img id="state" src="">
<img id="state" src=""/>
</div>
<script>
@ -66,7 +67,7 @@ function load() {
var stateurl = result.state.open ? result.state.icon.open : result.state.icon.closed;
document.getElementById("state").src = stateurl;
var link = document.querySelector("link[rel*='icon']") || document.createElement('link');
link.type = 'image/x-icon';
link.rel = 'shortcut icon';
@ -76,7 +77,8 @@ function load() {
var body = document.querySelector("body");
if (!result.state.open) {
body.classList.add("closed");
} else{
}
else{
body.classList.remove("closed");
}
}
@ -84,5 +86,7 @@ function load() {
load();
window.setInterval(load, 60 * 1000);
</script>
</body>
</body>
</html>