Last active
August 18, 2018 16:08
-
-
Save enzosterro/49624c86bcc92e40e02f77d15a79b0ed to your computer and use it in GitHub Desktop.
Telegram Bot + Transmission Raspberry Pi LED indication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import RPi.GPIO as GPIO | |
import transmissionrpc | |
import time | |
import sys | |
import os | |
class Led: | |
def __init__(self, telegram_path, telegram_token, telegram_master, led=37, start_delay=10, transmission_port=9091, transmission_address='localhost'): | |
print("Main initialized") | |
self.__led = led | |
self.__setup_gpio(start_delay, led) | |
self.__setup_torrents(telegram_path, telegram_token, telegram_master, transmission_address, transmission_port) | |
def __setup_gpio(self, delay, led): | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(led, GPIO.OUT) | |
time.sleep(delay) | |
print("GPIO initialized") | |
def __gpio_cleanup(self): | |
GPIO.cleanup() | |
print("GPIO cleaned") | |
def __setup_torrents(self, telegram_path, telegram_token, telegram_master, address, port): | |
token = "-token=" + telegram_token | |
master = "-master=" + telegram_master | |
os.system("%s %s %s &" %(telegram_path, token, master)) | |
self.__tc = transmissionrpc.Client(address, port=port) | |
print("Transmission daemon initialized") | |
def __get_torrents(self): | |
torrents = self.__tc.get_torrents() | |
if len(torrents) == 0: | |
self.__led_turn_off() | |
return | |
else: | |
self.__led_turn_off() | |
statuses = list(map(lambda torrent: torrent.status, torrents)) | |
if 'downloading' in statuses: | |
self.__led_turn_on() | |
return | |
if 'seeding' in statuses: | |
self.__blink() | |
def __led_turn_on(self): | |
GPIO.output(self.__led, 1) | |
def __led_turn_off(self): | |
GPIO.output(self.__led, 0) | |
def __blink(self): | |
for _ in range(3): | |
GPIO.output(self.__led, 1) | |
time.sleep(1) | |
GPIO.output(self.__led, 0) | |
time.sleep(1) | |
def start_routine(self): | |
self.__get_torrents() | |
def stop_routine(self): | |
self.__gpio_cleanup() | |
if __name__ == '__main__': | |
params = sys.argv | |
led = Led(params[1], params[2], params[3]) | |
try: | |
while True: | |
led.start_routine() | |
time.sleep(5) | |
except KeyboardInterrupt: | |
led.stop_routine() | |
print('\nExiting') | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment