modified for something

This commit is contained in:
Merlin Raschtuttis 2018-06-07 20:53:24 +00:00
parent 2db0e62604
commit 526a596147

View File

@ -12,6 +12,7 @@ mqtc = mqt.Client()
access = {}
# [ code : [last_access, penalty] ]
bus_use = True
def allowed(code):
print (access)
@ -50,14 +51,14 @@ def clear_bit(value, bit):
return value & ~(1<<bit)
def i2c_status_thread_new():
services = ["strom", "licht"]
while True:
global bus_use
while bus_use:
byte = bus.read_byte(inputs[0])
for c,i in i2c_input[0].items():
on = byte | c == c
if on and state[i] == 0 or not on and state[i] == 1: #Turned on
mqtc.publish("foobar/oben/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[1 if on else 0], qos=0, retain=False)
state[i] = 1 if on else 0
ns = 1 if byte | c == c else 0
if state[i] != ns: #Turned on
mqtc.publish("foobar/aerie/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[ns], qos=0, retain=False)
state[i] = ns
time.sleep(5)
#def i2c_status_thread():
@ -77,54 +78,66 @@ def i2c_status_thread_new():
# for i,j,n in zip(state,update,names):#
# if i != j:
# if n in power:
# mqtc.publish("foobar/oben/" + n + "/strom/status", states[j], qos=0, retain=False)
# mqtc.publish("foobar/aerie/" + n + "/strom/status", states[j], qos=0, retain=False)
# else:
# mqtc.publish("foobar/oben/" + n + "/licht/status", states[j], qos=0, retain=False)
# mqtc.publish("foobar/aerie/" + n + "/licht/status", states[j], qos=0, retain=False)
# state = update
# time.sleep(5)
#Buttonbefehle
def switch(i,speed=0.5):
if allowed(i):
global bus_use
#if allowed(i):
bus_use = False
print ( "actually switched" )
o = 0
if i > 7:
o = set_bit(o,i-8)
bus.write_byte(0x21,255-o)
bus.write_byte(0x3f,255-o)
time.sleep(speed)
o = clear_bit(o,i-8)
bus.write_byte(0x21,255-o)
bus.write_byte(0x3f,255-o)
else:
o = set_bit(o, i)
bus.write_byte(0x3f,255-o)
bus.write_byte(0x21,255-o)
time.sleep(speed)
o = clear_bit(o,i)
bus.write_byte(0x3f,255-o)
bus.write_byte(0x21,255-o)
bus_use = True
services = ["strom", "licht"]
state = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
inputs = (0x23,0x21)
states = {0: "OFF", 1: "ON"}
states = {0: "off", 1: "on"}
commands = { "flur" : 0, "baellebad" : 1, "lounge-front": 2, "lounge-back" : 3,"baellebad-ein" : 4, "lounge-ein" : 5, "cantina-ein" : 6, "zentral-aus" : 7, "cantina" : 8, "flur-strobo": 100, "cantina-strobo": 99}
i2c_input = [{ 0xFE : 4, 0xFD : 5, 0xFB : 8, 0xF7 : 0 }, {}]
names = { 7: [ "zentral",0] , 4: ["baellebad",0], 5: ["lounge",0], 6: ["cantina",0], 0: ["flur",1], 1: ["baellebad",1], 2:["lounge-front",1], 3: ["lounge-back",1], 8: ["cantina",1]}
power = { "zentral" : 7 , "baellebad" : 4, "lounge" : 5, "cantina" : 6 }
light = { "flur" : 0, "baellebad" : 1, "lounge-front" : 2, "lounge-back" : 3, "cantina" : 8 }
# foobar/oben/lounge /licht/action
# foobar/aerie/lounge /licht/action
# cantina /strom/status
# flur
# baellebad
# zentral
def switch_state(i, state, speed=0.5):
if state[i] != state: # changed
def switch_state(i, state_, speed=0.5):
if state[i] != state_: # changed
switch(i,speed=0.5)
state[i] = state
state[i] = state_
return True
return False
def switch_toggle(i,speed=0.5):
switch(i, speed=0.5)
switch(i, speed=speed)
# power can only be switched on, and centrally shutdown
if i in [ 4,5,6]:
state[i] = 1
elif i == 7:
state[i] = 0
else:
state[i] = 0 if state[i] == 1 else 1
mqtc.publish("foobar/aerie/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[state[i]])
def decode_topic(topic, state):
clist = topic.split('/')
@ -133,18 +146,21 @@ def decode_topic(topic, state):
if switch_state(light[clist[2]], state):
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
elif clist[3] == "strom":
if clist[2] in power:
if switch_state(power[clist[2]], state):
if clist[2] == "zentral":
if switch_state(power[clist[2]], 0):
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
elif clist[2] in power:
if switch_state(power[clist[2]], 1):
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
def msgs(inp, topic):
c = inp.decode("utf-8")
l = len(c)
# supporting number commands
if c == "OFF" or c == "ON":
if c.find("ON") >= 0:
if c == "on" or c == "off":
if c.find("on") >= 0:
decode_topic(topic, 1)
elif c.find("OFF") >= 0:
elif c.find("off") >= 0:
decode_topic(topic, 0)
return
elif l < 3:
@ -161,7 +177,9 @@ def msgs(inp, topic):
command = 9001
#error checking
if cmds[0] in commands:
command = cmds[0]
command = commands[cmds[0]]
else:
return
try:
arg = int (cmds[1])
except ValueError:
@ -173,34 +191,32 @@ def msgs(inp, topic):
for i in range(arg):
switch((100-command)*8, speed=0.05)
time.sleep(0.06)
#command with parameter used for dimming
else:
switch_toggle(command,speed = 4*arg/100+1 )
switch(command,speed = 4*arg/100+1 )
if ( state[command] == 0 ):
state[command] == 1
#single string command without parameter
else:
try:
if c in commands:
switch_toggle(commands[c])
except KeyError:
return
return
def init_mqtt():
mqtc.connect("10.42.0.244", 1883, 60)
mqtc.subscribe("foobar/oben/licht", 0)
mqtc.subscribe("foobar/oben/lounge-back/licht/action", 0)
mqtc.subscribe("foobar/oben/lounge-front/licht/action", 0)
mqtc.subscribe("foobar/oben/lounge/strom/action", 0)
mqtc.subscribe("foobar/oben/baellebad/licht/action", 0)
mqtc.subscribe("foobar/oben/baellebad/strom/action", 0)
mqtc.subscribe("foobar/oben/cantina/licht/action", 0)
mqtc.subscribe("foobar/oben/cantina/strom/action", 0)
mqtc.subscribe("foobar/oben/flur/licht/action", 0)
mqtc.subscribe("foobar/oben/licht", 0)
mqtc.subscribe("foobar/aerie/licht", 0)
mqtc.subscribe("foobar/aerie/lounge-back/licht/action", 0)
mqtc.subscribe("foobar/aerie/lounge-front/licht/action", 0)
mqtc.subscribe("foobar/aerie/lounge/strom/action", 0)
mqtc.subscribe("foobar/aerie/baellebad/licht/action", 0)
mqtc.subscribe("foobar/aerie/baellebad/strom/action", 0)
mqtc.subscribe("foobar/aerie/cantina/licht/action", 0)
mqtc.subscribe("foobar/aerie/cantina/strom/action", 0)
mqtc.subscribe("foobar/aerie/flur/licht/action", 0)
mqtc.subscribe("foobar/aerie/licht", 0)
mqtc.subscribe("foobar/aerie/strom/zentral/licht/action", 0)
mqtc.on_message = on_message
mqtc.on_connect = on_connect
mqtc.on_subscribe = on_subscribe