raumgirl/plugins/Lampel/plugin.py

173 lines
6.5 KiB
Python

###
# Copyright (c) 2011, henne
# All rights reserved.
#
#
###
from threading import Event, Thread
import subprocess
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
import ampel
import random
import time
class Lampel(callbacks.Plugin):
# state = 00
threaded = True
discoan = False
def __init__(self, irc):
self.__parent = super(Lampel, self)
self.__parent.__init__(irc)
# def __del__(self, irc):
# ampel.connection.close()
def start(self, irc, msg, args):
irc.reply("Lampel kann nun gesteuert werden (noch nicht.....)")
# ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
# ampel.connection.write("io get port 2\n")
# buf = ampel.connection.read_until("\n", 1)
# self.state = buf[-2]
# irc.reply(self.state)
start = wrap(start)
def rot(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 04\n")
ampel.connection.close()
rot = wrap(rot)
red = wrap(rot)
def gelb(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 02\n")
ampel.connection.close()
gelb = wrap(gelb)
yellow = wrap(gelb)
def gruen(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 01\n")
ampel.connection.close()
gruen = wrap(gruen)
green = wrap(gruen)
def rotgelbgruen(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 07\n")
ampel.connection.close()
rotgelbgruen = wrap(rotgelbgruen)
allcolors = wrap(rotgelbgruen)
allefarben = wrap(rotgelbgruen)
alle = wrap(rotgelbgruen)
def rotgelb(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 06\n")
ampel.connection.close()
rotgelb = wrap(rotgelb)
gelbrot = wrap(rotgelb)
redyellow = wrap(rotgelb)
yellowred = wrap(rotgelb)
def rotgruen(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 05\n")
ampel.connection.close()
rotgruen = wrap(rotgruen)
redgreen = wrap(rotgruen)
gruenrot = wrap(rotgruen)
greenred = wrap(rotgruen)
def gelbgruen(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 03\n")
ampel.connection.close()
gelbgruen = wrap(gelbgruen)
yellowgreen = wrap(gelbgruen)
gruengelb = wrap(gelbgruen)
greenyellow = wrap(gelbgruen)
def aus(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 00\n")
ampel.connection.close()
aus = wrap(aus)
off = wrap(aus)
def hilfe(self, irc, msg, args):
irc.reply("Help for the Lampelcontrol")
irc.reply("rot: Nur rot an (oder red)")
irc.reply("gelb: Nur gelb an (oder yellow)")
irc.reply("gruen: Nur gruen an (oder gruen)")
irc.reply("rotgelb: Rot und Gelb an (oder gelbrot, redyellow, yellowred)")
irc.reply("rotgruen: Rot und Gruen an (oder gruenrot, greenred, redgreen)")
irc.reply("gelbgruen: Gelb und Gruen an (oder gruengelb, greenyellow, yellowgreen)")
irc.reply("rotgelbgruen: alle an (oder alle, allefarben, allcolors)")
irc.reply("aus: alle aus (oder off)")
irc.reply("hilfe: Hilft dir...")
hilfe = wrap(hilfe)
def ampelaus(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
for i in range(1, 5):
ampel.connection.write("io set port 2 00\n")
time.sleep(1)
ampel.connection.write("io set port 2 02\n")
time.sleep(1)
ampel.connection.close()
def moo(self, irc, msg, args):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 00\n")
time.sleep(1)
ampel.connection.write("io set port 2 01\n")
time.sleep(1)
ampel.connection.write("io set port 2 02\n")
time.sleep(1)
ampel.connection.write("io set port 2 04\n")
time.sleep(1)
ampel.connection.write("io set port 2 00\n")
ampel.connection.close()
moo = wrap(moo)
def disco(self, irc, msg, args):
if not self.discoan:
irc.reply("And the Party begins")
self.discoTimer = meinTimer(2, self.discof, 0, [irc])
self.discoTimer.daemon = True
self.discoan = True
self.discoTimer.start()
else:
irc.reply("The Party is over")
self.discoTimer.cancel()
self.discoan = False
self.aus();
disco = wrap(disco)
def discof(self, irc):
ampel.connection.open(self.registryValue('host'), self.registryValue('port'))
ampel.connection.write("io set port 2 0" + str(random.randint(0, 7)) + "\n")
# ampel.connection.write("io set port 2 01\n")
ampel.connection.close()
# def getState(self, irc, msg, args):
# ampel.connection.write("io get port 2\n")
# buf = ampel.connection.read_until("\n", 1)
# self.state = buf[-2]
# irc.reply(buf)
# getstate = wrap(getState)
class meinTimer(Thread):
def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.iterations = iterations
self.args = args
self.kwargs = kwargs
self.finished = Event()
def run(self):
count = 0
while not self.finished.isSet() and (self.iterations <= 0 or count < self.iterations):
self.finished.wait(self.interval)
if not self.finished.isSet():
self.function(*self.args, **self.kwargs)
count += 1
def cancel(self):
self.finished.set()
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: