fixes for pyropeter

This commit is contained in:
Martin/Geno 2018-08-22 22:39:18 +02:00
parent 03b246879a
commit 5d1dbfba31
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
5 changed files with 59 additions and 43 deletions

10
install.sh Normal file → Executable file
View File

@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/sh
INSTALL_PATH=$PWD
# install dependencies
@ -12,10 +12,14 @@ chown pi:pi /var/www/html/spaceapi.json
cp $INSTALL_PATH/www/index.html /var/www/html/index.html
# install ircbot
ln -s $INSTALL_PATH/sopel-bot /home/pi/.sopel
ln -s $INSTALL_PATH/sopel-bot/modules /home/pi/.sopel
if [ ! -f /home/pi/.sopel/default.cfg ]; then
cp $INSTALL_PATH/sopel-bot/default.cfg /home/pi/.sopel/
fi
#install services
cp $INSTALL_PATH/systemd/{ircbot,schalter}.service /etc/systemd/system
cp $INSTALL_PATH/systemd/schalter.service /etc/systemd/system
cp $INSTALL_PATH/systemd/ircbot.service /etc/systemd/system
systemctl daemon-reload
systemctl enable ircbot schalter
systemctl start ircbot schalter

View File

@ -3,9 +3,10 @@
# 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
# to make sure that everything does not happen at once
import time
import datetime
# json and raspi imports
@ -14,13 +15,19 @@ import json
import RPi.GPIO as GPIO
# for logging
import logging
from systemd import journal
import systemd.journal
########################################################
#start init
journal.write("CCCHB space api switch v. 0.3 ")
currentState = bool(0)
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 ")
#################################################################################
#function definitons:
@ -28,16 +35,15 @@ currentState = bool(0)
# 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))
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')
if pos != wert_des_schalters:
log.info("state changed to: ", wert_des_schalters)
chn_time = datetime.datetime.now().isoformat()
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
#################################################################################
@ -45,21 +51,22 @@ 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=600)
journal.write("init complete... starting json foo")
GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200)
log.info("init complete... starting json foo")
#read json file
with open('spaceapi.json','r') as infile:
data = json.load(infile)
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 = False
try:
while True:
time.sleep(5)
switched(currentState)
#currentValue = #include IRCBOT status here?
time.sleep(1)
currentValue = switched(currentValue)
#include IRCBOT status here?
#do other stuff otherwise just don't do anything?!
except KeyboardInterrupt:
GPIO.cleanup()

View File

@ -3,7 +3,6 @@ import json
import sopel.module
SPACEAPI = "/var/www/html/spaceapi.json"
CHANNEL = "#ccchb"
PLACE = "ccchb"
cache = {
@ -19,20 +18,26 @@ def status_msg(status):
status_text = "closed"
if status:
status_text = "open"
return PLACE + ' is ' + status_text
return '{} is {}'.format(PLACE, status_text)
def change_status(bot, status):
status_text = "closed"
if status:
status_text = "open"
bot.say(PLACE +' changed to '+ status_text, CHANNEL)
for ch in bot.channels:
bot.notice('{} changed to {}'.format(PLACE, status_text), ch)
topic = status_msg(status)
channel = bot.channels[CHANNEL]
for ch in bot.channels:
channel = bot.channels[ch]
topic_new = topic
topic_cur = status_msg(not status)
if channel.topic != None:
topic = channel.topic.replace(status_msg(not status), topic)
bot.write(('TOPIC', CHANNEL + ' :' + topic))
topic_new = channel.topic.replace(topic_cur, topic_new)
topic_cur = channel.topic
if topic_new != topic_cur:
bot.write(('TOPIC', ch), topic_new)
def check_status(bot, human=False):
data = get_spaceapi()
@ -40,7 +45,6 @@ def check_status(bot, human=False):
if status != cache["open"]:
cache["open"] = status
if CHANNEL in bot.channels:
change_status(bot, status)
if human:

View File

@ -4,8 +4,9 @@ Description=Schalter IRC Bot
[Service]
ExecStart=/usr/bin/sopel
User=pi
Group=pi
Restart=always
RestartSec=3
RestartSec=300
[Install]
WantedBy=multi-user.target

View File

@ -5,9 +5,9 @@ Description=Eingangsschalter
ExecStart=/home/pi/Eingangsschalter/schalter.py
WorkingDirectory=/home/pi/Eingangsschalter
User=pi
Group=pi
Restart=always
RestartSec=3
RestartSec=30
[Install]
WantedBy=multi-user.target