Skip to content

Commit

Permalink
PEP8 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kylegordon committed Jan 5, 2014
1 parent e95503a commit 817d966
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions mqtt-gpio-trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
MQTT_TOPIC = config.get("global", "mqtt_topic")
PINS = config.get("global", "pins").split(",")

# Convert the list of strings to a list of ints. Also strips any whitespace padding
# Convert the list of strings to a list of ints.
# Also strips any whitespace padding
PINS = map(int, PINS)

# Append a column to the list of PINS. This will be used to store state
## FIXME Should this not be in the (re)connect routine?
for item in PINS:
PINS[PINS.index(item)] = [item,1]
PINS[PINS.index(item)] = [item, 1]

APPNAME = "mqtt-gpio-trigger"
PRESENCETOPIC = "clients/" + socket.getfqdn() + "/" + APPNAME + "/state"
Expand Down Expand Up @@ -205,7 +205,8 @@ def process_connection():
What to do when a new connection is established
"""
logging.debug("Subscribing to %s", MQTT_TOPIC)
# If many GPIOs are in use, a topic could be used to request a query of them all
# If many GPIOs are in use, a topic could be used
# to request a query of them all


def export_pi_gpio():
Expand All @@ -215,23 +216,30 @@ def export_pi_gpio():
for PIN in PINS:
index = [y[0] for y in PINS].index(PIN[0])
logging.debug("Exporting pin %s", str(PINS[index][0]))
result = subprocess.call("/usr/local/bin/gpio export " + str(PINS[index][0]) + " in", shell=True)
result = subprocess.call("/usr/local/bin/gpio export "
+ str(PINS[index][0])
+ " in", shell=True)
if result != 0:
logging.info("Failed to export pin %s", str(PINS[index][0]))
sys.exit(result)


def set_direction():
"""
Set the GPIO direction so that it's classed as an input
"""
for PIN in PINS:
index = [y[0] for y in PINS].index(PIN[0])
logging.debug("Setting direction of pin %s", str(PINS[index][0]))
result = subprocess.call("echo out > /sys/class/gpio/gpio" + str(PINS[index][0]) + "/direction", shell=True)
result = subprocess.call("echo out > /sys/class/gpio/gpio"
+ str(PINS[index][0])
+ "/direction", shell=True)
if result != 0:
logging.info("Failed to set the direction of pin %s", str(PINS[index][0]))
logging.info("Failed to set the direction of pin %s",
str(PINS[index][0]))
sys.exit(result)


def main_loop():
"""
The main loop in which we monitor the state of the GPIOs
Expand All @@ -240,17 +248,28 @@ def main_loop():
while True:
for PIN in PINS:
index = [y[0] for y in PINS].index(PIN[0])
logging.debug("Reading state of %s from %s", str(PINS[index][0]), str(PINS))
logging.debug("Reading state of %s from %s",
str(PINS[index][0]),
str(PINS))
path = "/sys/class/gpio/gpio" + str(PINS[index][0]) + "/value"
filehandle = open(path, "r", 0)
state = filehandle.readline()
filehandle.close()
state = int(state)
logging.debug("Read state is %s and stored state is %s", str(state), str(PINS[index][1]))
logging.debug("Read state is %s and stored state is %s",
str(state),
str(PINS[index][1]))
if state != PINS[index][1]:
logging.debug("Publishing state change. Pin %s changed from %s to %s", str(PINS[index][0]), str(PINS[index][1]), str(state))
logging.debug("Pin %s changed from %s to %s",
str(PINS[index][0]),
str(PINS[index][1]),
str(state))
PINS[index][1] = state
mqttc.publish("/raw/" + socket.getfqdn() + "/gpio/" + str(PINS[index][0]), str(state))
mqttc.publish("/raw/"
+ socket.getfqdn()
+ "/gpio/"
+ str(PINS[index][0]),
str(state))
time.sleep(1)
logging.debug("End of loop")

Expand Down

0 comments on commit 817d966

Please sign in to comment.