power now toggleable
This commit is contained in:
parent
526a596147
commit
7a5c541167
101
power_mqtt1.py
101
power_mqtt1.py
@ -14,6 +14,7 @@ access = {}
|
|||||||
|
|
||||||
bus_use = True
|
bus_use = True
|
||||||
|
|
||||||
|
|
||||||
def allowed(code):
|
def allowed(code):
|
||||||
print (access)
|
print (access)
|
||||||
if code in access:
|
if code in access:
|
||||||
@ -27,6 +28,7 @@ def allowed(code):
|
|||||||
access[code] = [datetime.now(), 0]
|
access[code] = [datetime.now(), 0]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def millis_since(start_time):
|
def millis_since(start_time):
|
||||||
dt = datetime.now() - start_time
|
dt = datetime.now() - start_time
|
||||||
ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
|
ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
|
||||||
@ -52,6 +54,7 @@ def clear_bit(value, bit):
|
|||||||
|
|
||||||
def i2c_status_thread_new():
|
def i2c_status_thread_new():
|
||||||
global bus_use
|
global bus_use
|
||||||
|
while True:
|
||||||
while bus_use:
|
while bus_use:
|
||||||
byte = bus.read_byte(inputs[0])
|
byte = bus.read_byte(inputs[0])
|
||||||
for c,i in i2c_input[0].items():
|
for c,i in i2c_input[0].items():
|
||||||
@ -60,36 +63,34 @@ def i2c_status_thread_new():
|
|||||||
mqtc.publish("foobar/aerie/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[ns], qos=0, retain=False)
|
mqtc.publish("foobar/aerie/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[ns], qos=0, retain=False)
|
||||||
state[i] = ns
|
state[i] = ns
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
time.sleep(1)
|
||||||
#def i2c_status_thread():
|
|
||||||
# global state
|
|
||||||
#ports = (0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F)
|
|
||||||
# bael loun cant flur
|
|
||||||
# leba e ina-
|
|
||||||
# d lich
|
|
||||||
#ports = (0xF7,0,0,0xFE,0xFD,0xFB,0,0,0,0,0,0,0,0,0,0)
|
|
||||||
# while True:
|
|
||||||
# byte_ = bus.read_byte(inputs[0])
|
|
||||||
# print(byte_)
|
|
||||||
# update = list(map( lambda x: 1 if (byte_ | x == x) else 0, ports))
|
|
||||||
# byte_ = bus.read_byte(inputs[1])
|
|
||||||
# print(byte_)
|
|
||||||
# update[8:] = list(map( lambda x: 1 if (byte_ | x == x) else 0, ports[8:]))
|
|
||||||
# for i,j,n in zip(state,update,names):#
|
|
||||||
# if i != j:
|
|
||||||
# if n in power:
|
|
||||||
# mqtc.publish("foobar/aerie/" + n + "/strom/status", states[j], qos=0, retain=False)
|
|
||||||
# else:
|
|
||||||
# mqtc.publish("foobar/aerie/" + n + "/licht/status", states[j], qos=0, retain=False)
|
|
||||||
# state = update
|
|
||||||
# time.sleep(5)
|
|
||||||
|
|
||||||
#Buttonbefehle
|
#Buttonbefehle
|
||||||
def switch(i,speed=0.5):
|
def switch(i,speed=0.5):
|
||||||
global bus_use
|
global bus_use
|
||||||
#if allowed(i):
|
#if allowed(i):
|
||||||
bus_use = False
|
bus_use = False
|
||||||
print ( "actually switched" )
|
print ( "Switched: " + str(i) + " Speed: " + str(speed) )
|
||||||
|
o = 0
|
||||||
|
if i > 7 and i < 16:
|
||||||
|
o = set_bit(o, i - 8)
|
||||||
|
bus.write_byte(0x3f, 255 - o)
|
||||||
|
time.sleep(speed)
|
||||||
|
o = clear_bit(o, i - 8)
|
||||||
|
bus.write_byte(0x3f, 255 - o)
|
||||||
|
elif i < 8:
|
||||||
|
o = set_bit(o, i)
|
||||||
|
bus.write_byte(0x21, 255 - o)
|
||||||
|
time.sleep(speed)
|
||||||
|
o = clear_bit(o, i)
|
||||||
|
bus.write_byte(0x21, 255 - o)
|
||||||
|
bus_use = True
|
||||||
|
|
||||||
|
|
||||||
|
def strobo_switch(switch_list, speed=0.5):
|
||||||
|
global bus_use
|
||||||
|
bus_use = False
|
||||||
|
for i in switch_list:
|
||||||
o = 0
|
o = 0
|
||||||
if i > 7:
|
if i > 7:
|
||||||
o = set_bit(o, i - 8)
|
o = set_bit(o, i - 8)
|
||||||
@ -105,15 +106,25 @@ def switch(i,speed=0.5):
|
|||||||
bus.write_byte(0x21, 255 - o)
|
bus.write_byte(0x21, 255 - o)
|
||||||
bus_use = True
|
bus_use = True
|
||||||
|
|
||||||
|
|
||||||
services = ["strom", "licht"]
|
services = ["strom", "licht"]
|
||||||
state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||||
inputs = (0x23, 0x21)
|
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}
|
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,
|
||||||
|
"deineelternhabenneziemlichepartygefeiert": 101}
|
||||||
i2c_input = [{0xFE: 4, 0xFD: 5, 0xFB: 8, 0xF7: 0}, {}]
|
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]}
|
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}
|
power = {"zentral": 7, "baellebad": 4, "lounge": 5, "cantina": 6}
|
||||||
light = { "flur" : 0, "baellebad" : 1, "lounge-front" : 2, "lounge-back" : 3, "cantina" : 8 }
|
light = {"flur": 0, "baellebad": 1, "lounge-front": 2,
|
||||||
|
"lounge-back": 3, "cantina": 8}
|
||||||
|
|
||||||
# foobar/aerie/lounge /licht/action
|
# foobar/aerie/lounge /licht/action
|
||||||
# cantina /strom/status
|
# cantina /strom/status
|
||||||
@ -135,22 +146,20 @@ def switch_toggle(i,speed=0.5):
|
|||||||
state[i] = 1
|
state[i] = 1
|
||||||
elif i == 7:
|
elif i == 7:
|
||||||
state[i] = 0
|
state[i] = 0
|
||||||
else:
|
elif i < 16:
|
||||||
state[i] = 0 if state[i] == 1 else 1
|
state[i] = 0 if state[i] == 1 else 1
|
||||||
mqtc.publish("foobar/aerie/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[state[i]])
|
mqtc.publish("foobar/aerie/" + names[i][0] + "/" + services[names[i][1]] + "/status", states[state[i]])
|
||||||
|
|
||||||
def decode_topic(topic, state):
|
def decode_topic(topic, state):
|
||||||
clist = topic.split('/')
|
clist = topic.split('/')
|
||||||
if clist[3] == "licht":
|
if clist[3] == "strom" and clist[2] == "zentral":
|
||||||
if clist[2] in light:
|
|
||||||
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] == "zentral":
|
|
||||||
if switch_state(power[clist[2]], 0):
|
if switch_state(power[clist[2]], 0):
|
||||||
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
|
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
|
||||||
elif clist[2] in power:
|
return 0
|
||||||
if switch_state(power[clist[2]], 1):
|
|
||||||
|
if clist[3] == "licht" or clist[3] == "strom":
|
||||||
|
if clist[2] in light or clist[2] in power:
|
||||||
|
if switch_state(commands[clist[2]], state):
|
||||||
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
|
mqtc.publish(topic.replace("action", "status"), states[state], qos=0, retain=False)
|
||||||
|
|
||||||
def msgs(inp, topic):
|
def msgs(inp, topic):
|
||||||
@ -186,19 +195,23 @@ def msgs(inp, topic):
|
|||||||
return
|
return
|
||||||
|
|
||||||
#strobo
|
#strobo
|
||||||
if ( command == 100 or command == 99 ):
|
if ((command == 100 or command == 99) and arg < 100):
|
||||||
if arg < 100:
|
|
||||||
for i in range(arg):
|
for i in range(arg):
|
||||||
switch((100 - command) * 8, speed=0.05)
|
switch((100 - command) * 8, speed=0.05)
|
||||||
time.sleep(0.06)
|
time.sleep(0.06)
|
||||||
|
elif command == 101 and arg < 100:
|
||||||
|
for i in range(arg):
|
||||||
|
strobo_switch([0, 8], speed=0.05)
|
||||||
|
time.sleep(0.01)
|
||||||
#command with parameter used for dimming
|
#command with parameter used for dimming
|
||||||
else:
|
else:
|
||||||
switch(command, speed=4 * arg / 100 + 1)
|
switch(command, speed=4 * arg / 100 + 1)
|
||||||
if ( state[command] == 0 ):
|
if state[command] == 0:
|
||||||
state[command] == 1
|
state[command] == 1
|
||||||
|
|
||||||
#single string command without parameter
|
#single string command without parameter
|
||||||
else:
|
else:
|
||||||
|
print ("One Command")
|
||||||
if c in commands:
|
if c in commands:
|
||||||
switch_toggle(commands[c])
|
switch_toggle(commands[c])
|
||||||
return
|
return
|
||||||
@ -215,15 +228,21 @@ def init_mqtt():
|
|||||||
mqtc.subscribe("foobar/aerie/cantina/licht/action", 0)
|
mqtc.subscribe("foobar/aerie/cantina/licht/action", 0)
|
||||||
mqtc.subscribe("foobar/aerie/cantina/strom/action", 0)
|
mqtc.subscribe("foobar/aerie/cantina/strom/action", 0)
|
||||||
mqtc.subscribe("foobar/aerie/flur/licht/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.subscribe("foobar/aerie/strom/zentral/licht/action", 0)
|
||||||
mqtc.on_message = on_message
|
mqtc.on_message = on_message
|
||||||
mqtc.on_connect = on_connect
|
mqtc.on_connect = on_connect
|
||||||
mqtc.on_subscribe = on_subscribe
|
mqtc.on_subscribe = on_subscribe
|
||||||
|
|
||||||
_thread.start_new_thread(i2c_status_thread_new, ())
|
_thread.start_new_thread(i2c_status_thread_new, ())
|
||||||
|
mqtc.loop_start()
|
||||||
|
while True:
|
||||||
|
#try:
|
||||||
|
# print("Err")
|
||||||
|
# mqtc.loop_forever()
|
||||||
|
#except:
|
||||||
|
# pass
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
mqtc.loop_forever()
|
|
||||||
|
|
||||||
init_mqtt()
|
init_mqtt()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user