From b01d732f72a9ba020c71016bef308b1635970e69 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Wed, 22 Aug 2018 01:57:27 +0200 Subject: [PATCH] inital spaceapi (with backup from cache) --- License | 13 ------ README.md | 18 ------- install.sh | 21 +++++++++ irc-daemon.service | 11 ----- ircbot.py | 85 --------------------------------- json2.py | 22 --------- schalter.py | 73 +++++++++++++++++++++++++++++ sopel-bot/defaults.cfg | 2 + sopel-bot/modules/spaceapi.py | 55 ++++++++++++++++++++++ spaceapi.json | 20 ++++++++ systemd/ircbot.service | 0 systemd/schalter.service | 0 www/index.html | 88 +++++++++++++++++++++++++++++++++++ 13 files changed, 259 insertions(+), 149 deletions(-) delete mode 100644 License delete mode 100644 README.md create mode 100644 install.sh delete mode 100644 irc-daemon.service delete mode 100755 ircbot.py delete mode 100644 json2.py create mode 100755 schalter.py create mode 100644 sopel-bot/defaults.cfg create mode 100644 sopel-bot/modules/spaceapi.py create mode 100644 spaceapi.json create mode 100644 systemd/ircbot.service create mode 100644 systemd/schalter.service create mode 100644 www/index.html diff --git a/License b/License deleted file mode 100644 index 456c488..0000000 --- a/License +++ /dev/null @@ -1,13 +0,0 @@ - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - Version 2, December 2004 - - Copyright (C) 2004 Sam Hocevar - - Everyone is permitted to copy and distribute verbatim or modified - copies of this license document, and changing it is allowed as long - as the name is changed. - - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/README.md b/README.md deleted file mode 100644 index 6a7794d..0000000 --- a/README.md +++ /dev/null @@ -1,18 +0,0 @@ -# Eingangsschalter -der Hebel bei der Eingangstür, der im IRC schreibt, wenn jemand den Hebel zieht. - -das python-skript legt man idealerweise in /usr/bin/ - -das systemd-ding legt man am besten in /etc/systemd/system/ - -mit den systemctl-befehlen muss man die "unit" dann noch starten. - -further reading / sources : - -https://linuxacademy.com/blog/geek/creating-an-irc-bot-with-python3/ - -http://pi4j.com/pins/model-b-rev1.html - -http://razzpisampler.oreilly.com/ch07.html - -http://wiringpi.com/the-gpio-utility/ diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..48bb5b4 --- /dev/null +++ b/install.sh @@ -0,0 +1,21 @@ +#!/usr/bin/bash +INSTALL_PATH=$PWD + +# install dependencies +# apt-get install sopel nginx certbot + +# install json (to save) +cp $INSTALL_PATH/spaceapi.json /var/www/html/spaceapi.json +chown pi:pi /var/www/html/spaceapi.json + +# install mini-website +cp $INSTALL_PATH/www/index.html /var/www/html/index.html + +# install ircbot +ln -s $INSTALL_PATH/sopel-bot /home/pi/.sopel + +#install services +cp $INSTALL_PATH/systemd/{ircbot,schalter}.service /etc/systemd/system +systemctl daemon-reload +systemctl enable ircbot schalter +systemctl start ircbot schalter diff --git a/irc-daemon.service b/irc-daemon.service deleted file mode 100644 index fc2aea0..0000000 --- a/irc-daemon.service +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=irc-daemon für türschalter -After=network.target - -[Service] -Type=simple -ExecStart=/usr/bin/ircbot.py - -[Install] -WantedBy=multi-user.target - diff --git a/ircbot.py b/ircbot.py deleted file mode 100755 index e15cd6e..0000000 --- a/ircbot.py +++ /dev/null @@ -1,85 +0,0 @@ -#!/usr/bin/python -import socket -import RPi.GPIO as GPIO -import time - - -ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -server = "irc.freenode.net" # Server -channel = "#yanniktest" # Channel -botnick = "yanniksBot" # Your bots nick -adminname = "yannik_" #Your IRC nickname. -exitcode = "bye " + botnick - - - -ircsock.connect((server, 6667)) # Here we connect to the server using the port 6667 -ircsock.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\n", "UTF-8")) #We are basically filling out a form with this line and saying to set all the fields to the bot nickname. -ircsock.send(bytes("NICK "+ botnick +"\n", "UTF-8")) # assign the nick to the bot - -def joinchan(chan): # join channel(s). - ircsock.send(bytes("JOIN "+ chan +"\n", "UTF-8")) - ircmsg = "hey, ircmsg!" - while ircmsg.find("End of /NAMES list.") == -1: - ircmsg = ircsock.recv(2048).decode("UTF-8") - ircmsg = ircmsg.strip('\n\r') - print(ircmsg) - - -def ping(): # respond to server Pings. - ircsock.send(bytes("PONG :pingis\n", "UTF-8")) - -def sendmsg(msg, target=channel): # sends messages to the target. - ircsock.send(bytes("PRIVMSG "+ target +" :"+ msg +"\n", "UTF-8")) - -def main(): - GPIO.setmode(GPIO.BCM) - GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) - - joinchan(channel) - prev=None - while 1: - #print("ein weiterer durchlauf der main-schleiffe") - current=GPIO.input(18) -# liste[i%2] = GPIO.input(18) - if ( current != prev) : - if( prev != None): - sendmsg("schalter wurde betätigt! {}".format(current)) - if (current == 0): - sendmsg("es ist nun offen!") - if (current == 1): - sendmsg("es ist nun zu, gute nacht!") - prev=current - ircmsg=" " - time.sleep(10) - #ircmsg = ircsock.recv(2048).decode("UTF-8") - #ircmsg = ircmsg.strip('\n\r') - print(ircmsg) - - if ircmsg.find("PRIVMSG") != -1: - name = ircmsg.split('!',1)[0][1:] - message = ircmsg.split('PRIVMSG',1)[1].split(':',1)[1] - - if len(name) < 17: - if message.find('Hi ' + botnick) != -1: - sendmsg("Hello " + name + "!") - if message[:5].find('.tell') != -1: - target = message.split(' ', 1)[1] - if target.find(' ') != -1: - message = target.split(' ', 1)[1] - target = target.split(' ')[0] - else: - target = name - message = "Could not parse. The message should be in the format of ‘.tell [target] [message]’ to work properly." - sendmsg(message, target) - - if name.lower() == adminname.lower() and message.rstrip() == exitcode: - sendmsg("oh...okay. :'(") - ircsock.send(bytes("QUIT \n", "UTF-8")) - return - else: - if ircmsg.find("PING :") != -1: - ping() - -main() - diff --git a/json2.py b/json2.py deleted file mode 100644 index 8f3b7ae..0000000 --- a/json2.py +++ /dev/null @@ -1,22 +0,0 @@ -import json -import time -import RPi.GPIO as GPIO -pin_number = 18; - -data = {1:"asdf"} -wert_des_schalters = False - -while True: -#/* Sphinx Vorschlag */ - GPIO.setmode(GPIO.BCM) - GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) - while True: - time.sleep(1) - if wert_des_schalters != GPIO.input(18) : # das ist gut - time.sleep(1) # // Contact bounce protection - print("wert wurde geaendert") - wert_des_schalters = GPIO.input(pin_number) - data = {1:wert_des_schalters} - with open('data.txt', 'w') as outfile: - json.dump(data, outfile) - diff --git a/schalter.py b/schalter.py new file mode 100755 index 0000000..dca9137 --- /dev/null +++ b/schalter.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +######################################################################### +# 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 +# json and raspi imports +import json +# for using GPIO pins of the raspi +import RPi.GPIO as GPIO +# for logging +import logging +import systemd.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 ") + +################################################################################# +#function definitons: +################################################################################# + +# will read the GPIO header of the pi and write the current state in the www spaceapi json file +def switched(pos): + 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) + chn_time = datetime.datetime.now().isoformat() + data.update({'state':{'open':wert_des_schalters,'lastchange':chn_time, "icon":icons}}) + with open('/var/www/html/spaceapi.json', 'w') as outfile: + json.dump(data, outfile,sort_keys=True) + return wert_des_schalters + +################################################################################# + +#init GPIO pin on raspi +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") +#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 = bool(0) +try: + while True: + time.sleep(1) + currentValue = switched(currentValue) + + #include IRCBOT status here? + #do other stuff otherwise just don't do anything?! +except KeyboardInterrupt: + GPIO.cleanup() diff --git a/sopel-bot/defaults.cfg b/sopel-bot/defaults.cfg new file mode 100644 index 0000000..7d5fd13 --- /dev/null +++ b/sopel-bot/defaults.cfg @@ -0,0 +1,2 @@ +[core] +verify_ssl = false diff --git a/sopel-bot/modules/spaceapi.py b/sopel-bot/modules/spaceapi.py new file mode 100644 index 0000000..281c410 --- /dev/null +++ b/sopel-bot/modules/spaceapi.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +import json +import sopel.module + +SPACEAPI = "/var/www/html/spaceapi.json" +CHANNEL = "#ccchb" +PLACE = "P5" + +cache = { + "open": False +} + +def get_spaceapi(): + with open(SPACEAPI, 'r') as infile: + data = json.load(infile) + return data + +def status_msg(status): + status_text = "closed" + if status: + status_text = "open" + return PLACE + ' is ' + status_text + +def change_status(bot, status): + status_text = "closed" + if status: + status_text = "open" + bot.say(PLACE +' changed to '+ status_text, CHANNEL) + + topic = status_msg(status) + + channel = bot.channels[CHANNEL] + if channel.topic != None: + topic = channel.topic.replace(status_msg(not status), topic) + bot.write(('TOPIC', CHANNEL + ' :' + topic)) + +def check_status(bot, human=False): + data = get_spaceapi() + status = data["state"]["open"] + + if status != cache["open"]: + cache["open"] = status + if CHANNEL in bot.channels: + change_status(bot, status) + + if human: + bot.reply(status_msg(status)) + +@sopel.module.interval(5) +def interval_check_status(bot): + check_status(bot) + +@sopel.module.commands('status') +def cmd_check_status(bot, trigger): + check_status(bot, True) diff --git a/spaceapi.json b/spaceapi.json new file mode 100644 index 0000000..d443641 --- /dev/null +++ b/spaceapi.json @@ -0,0 +1,20 @@ +{ + "api": "0.13", + "location": { + "address": "Postamt 5, An der Weide 50a, 28195 Bremen, Germany", + "ext_floor": 1, + "lat": 53.0815, + "lon": 8.8154 + }, + "logo": "https://ccchb.de/logo/CCCHB-logo_256x256_bw.png", + "space": "CCCHB", + "state": { + "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 + }, + "url": "https://ccchb.de/" +} diff --git a/systemd/ircbot.service b/systemd/ircbot.service new file mode 100644 index 0000000..e69de29 diff --git a/systemd/schalter.service b/systemd/schalter.service new file mode 100644 index 0000000..e69de29 diff --git a/www/index.html b/www/index.html new file mode 100644 index 0000000..7787256 --- /dev/null +++ b/www/index.html @@ -0,0 +1,88 @@ + + + + CCCHB is open + + +
+ +
+ + + +