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_PATH=$PWD
# install dependencies # 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 cp $INSTALL_PATH/www/index.html /var/www/html/index.html
# install ircbot # 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 #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 daemon-reload
systemctl enable ircbot schalter systemctl enable ircbot schalter
systemctl start ircbot schalter systemctl start ircbot schalter

View File

@ -1,43 +1,49 @@
#!/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 once
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
from systemd import journal import systemd.journal
######################################################## ########################################################
#start init #start init
journal.write("CCCHB space api switch v. 0.3 ") log = logging.getLogger("SpaceAPI Schalter") #create a logger
currentState = bool(0) 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: #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))
if (pos != wert_des_schalters): icons = data.get("state").get('icon')
journal.write("state changed to: %s " %wert_des_schalters) if pos != 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
################################################################################# #################################################################################
@ -45,21 +51,22 @@ 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=600) GPIO.add_event_detect(pin_number, GPIO.BOTH, callback=switched, bouncetime=200)
journal.write("init complete... starting json foo") log.info("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)
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)
try: currentValue = False
try:
while True: while True:
time.sleep(5) time.sleep(1)
switched(currentState) currentValue = switched(currentValue)
#currentValue = #include IRCBOT status here?
#do other stuff otherwise just don't do anything?! #include IRCBOT status here?
#do other stuff otherwise just don't do anything?!
except KeyboardInterrupt: except KeyboardInterrupt:
GPIO.cleanup() GPIO.cleanup()

View File

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

View File

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

View File

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