Skip to content
Browse files

Renamed after being copied from other project

  • Loading branch information...
1 parent c707cc2 commit 7e517c0a409a97e2dd696dbb4d5ad86d08e3fc12 @kylegordon committed Oct 26, 2012
Showing with 286 additions and 0 deletions.
  1. +7 −0 mqtt-pwm-lights.cfg.example
  2. +2 −0 mqtt-pwm-lights.default
  3. +146 −0 mqtt-pwm-lights.init
  4. +131 −0 mqtt-pwm-lights.py
View
7 mqtt-pwm-lights.cfg.example
@@ -0,0 +1,7 @@
+[global]
+DEBUG = False
+LOGFILE = /var/log/mqtt-pwm-lights.log
+PINS = 18
+MQTT_HOST = 10.8.0.1
+MQTT_PORT = 1883
+MQTT_TOPIC = /bishopbriggs/gordonhouse/kitchen/undercabinetlights
View
2 mqtt-pwm-lights.default
@@ -0,0 +1,2 @@
+START_DAEMON=true
+VERBOSE=true
View
146 mqtt-pwm-lights.init
@@ -0,0 +1,146 @@
+#!/bin/sh
+### BEGIN INIT INFO
+# Provides: mqtt-pwm-lights
+# Required-Start: $remote_fs $syslog $network
+# Should-Start: bluetooth dbus udev
+# Required-Stop: $remote_fs $syslog $network
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: mqtt-pwm-lights daemon start/stop script
+# Description: Start/Stop script for the mqtt-pwm-lights daemon
+### END INIT INFO
+
+# Author: Kyle Gordon <kyle@lodge.glasgownet.com
+
+# Do NOT "set -e"
+
+# PATH should only include /usr/* if it runs after the mountnfs.sh script
+PATH=/sbin:/usr/sbin:/bin:/usr/bin
+DESC="mqtt-pwm-lights daemon"
+NAME=mqtt-pwm-lights
+DAEMON=/usr/local/$NAME/$NAME.py
+PIDFILE=/var/run/$NAME.pid
+SCRIPTNAME=/etc/init.d/$NAME
+
+# Exit if the package is not installed
+[ -x "$DAEMON" ] || exit 0
+
+# Read configuration variable file if it is present
+[ -r /etc/default/$NAME ] && . /etc/default/$NAME
+
+# Load the VERBOSE setting and other rcS variables
+. /lib/init/vars.sh
+
+# Define LSB log_* functions.
+# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
+. /lib/lsb/init-functions
+
+#
+# Function that starts the daemon/service
+#
+
+do_start()
+{
+ # Return
+ # 0 if daemon has been started
+ # 1 if daemon was already running
+ # 2 if daemon could not be started
+ echo "Starting mqtt-pwm-lights"
+ start-stop-daemon -b --start --quiet -m --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
+ || return 1
+ start-stop-daemon -b --start --quiet -m --pidfile $PIDFILE --exec $DAEMON -- \
+ $OPTIONS \
+ || return 2
+}
+
+#
+# Function that stops the daemon/service
+#
+do_stop()
+{
+ # Return
+ # 0 if daemon has been stopped
+ # 1 if daemon was already stopped
+ # 2 if daemon could not be stopped
+ # other if a failure occurred
+ start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
+ RETVAL="$?"
+ [ "$RETVAL" = 2 ] && return 2
+ # Many daemons don't delete their pidfiles when they exit.
+ rm -f $PIDFILE
+ return "$RETVAL"
+}
+
+#
+# Function that sends a SIGHUP to the daemon/service
+#
+do_reload() {
+ #
+ # If the daemon can reload its configuration without
+ # restarting (for example, when it is sent a SIGHUP),
+ # then implement that here.
+ #
+ start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
+ return 0
+}
+
+case "$1" in
+ start)
+ if [ "$START_DAEMON" = "true" ]; then
+ [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
+ do_start
+ case "$?" in
+ 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ else
+ [ "$VERBOSE" != no ] && \
+ log_daemon_msg "Not starting $DESC" "$NAME" && \
+ log_end_msg 0
+ fi
+ ;;
+ stop)
+ [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
+ 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
+ esac
+ ;;
+ status)
+ status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
+ ;;
+ reload|force-reload)
+ log_daemon_msg "Reloading $DESC" "$NAME"
+ do_reload
+ log_end_msg $?
+ ;;
+ restart)
+ #
+ # If the "reload" option is implemented then remove the
+ # 'force-reload' alias
+ #
+ log_daemon_msg "Restarting $DESC" "$NAME"
+ do_stop
+ case "$?" in
+ 0|1)
+ do_start
+ case "$?" in
+ 0) log_end_msg 0 ;;
+ 1) log_end_msg 1 ;; # Old process is still running
+ *) log_end_msg 1 ;; # Failed to start
+ esac
+ ;;
+ *)
+ # Failed to stop
+ log_end_msg 1
+ ;;
+ esac
+ ;;
+ *)
+ echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
+ exit 3
+ ;;
+esac
+
+:
View
131 mqtt-pwm-lights.py
@@ -0,0 +1,131 @@
+#!/usr/bin/env python
+# -*- coding: iso-8859-1 -*-
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
+
+__author__ = "Kyle Gordon"
+__copyright__ = "Copyright (C) Kyle Gordon"
+
+import os
+import csv
+import logging
+import signal
+import time
+import socket
+
+import mosquitto
+import ConfigParser
+
+# Read the config file
+config = ConfigParser.RawConfigParser()
+config.read("/etc/mqtt-pwm-lights/mqtt-pwm-lights.cfg")
+
+#Use ConfigParser to pick out the settings
+DEBUG = config.getboolean("global", "debug")
+LOGFILE = config.get("global", "logfile")
+MQTT_HOST = config.get("global", "mqtt_host")
+MQTT_PORT = config.getint("global", "mqtt_port")
+MQTT_TOPIC = config.get("global", "mqtt_topic")
+
+client_id = "PWM_Lights_%d" % os.getpid()
+mqttc = mosquitto.Mosquitto(client_id)
+
+if DEBUG:
+ logging.basicConfig(filename=LOGFILE, level=logging.DEBUG)
+else:
+ logging.basicConfig(filename=LOGFILE, level=logging.INFO)
+
+logging.info("Starting mqtt-pwm-lights")
+logging.info("INFO MODE")
+logging.debug("DEBUG MODE")
+
+def cleanup(signum, frame):
+ """
+ Signal handler to ensure we disconnect cleanly
+ in the event of a SIGTERM or SIGINT.
+ """
+ logging.info("Disconnecting from broker")
+ mqttc.publish("/status/" + socket.getfqdn(), "Offline")
+ mqttc.disconnect()
+ logging.info("Exiting on signal %d", signum)
+
+def connect():
+ """
+ Connect to the broker, define the callbacks, and subscribe
+ """
+ mqttc.connect(MQTT_HOST, MQTT_PORT, 60, True)
+
+ #define the callbacks
+ mqttc.on_message = on_message
+ mqttc.on_connect = on_connect
+ mqttc.on_disconnect = on_disconnect
+
+ mqttc.subscribe(MQTT_TOPIC, 2)
+
+def on_connect(result_code):
+ """
+ Handle connections (or failures) to the broker.
+ """
+ ## FIXME - needs fleshing out http://mosquitto.org/documentation/python/
+ if result_code == 0:
+ logging.info("Connected to broker")
+ mqttc.publish("/status/" + socket.getfqdn(), "Online")
+ else:
+ logging.warning("Something went wrong")
+ cleanup()
+
+def on_disconnect(result_code):
+ """
+ Handle disconnections from the broker
+ """
+ if result_code == 0:
+ logging.info("Clean disconnection")
+ else:
+ logging.info("Unexpected disconnection! Reconnecting in 5 seconds")
+ logging.debug("Result code: %s", result_code)
+ time.sleep(5)
+ connect()
+ main_loop()
+
+def on_message(msg):
+ """
+ What to do when the client recieves a message from the broker
+ """
+ logging.debug("Received: %s", msg.topic)
+
+
+def get_pwm_value():
+ """
+ Read the PWM value from the system
+ """
+ logging.debug("Reading PWM value of %s"), str(PIN))
+ statefile = open('/tmp/pwmstatefile', 'r')
+ pwm_value = statefile.readline()
+ statefile.close()
+
+def set_pwm_value():
+ """
+ Set the PWM value
+ """
+ logging.debug("Setting PWM value of %s"), str(PIN))
+ statefile = open('/tmp/pwmstatefile', 'w')
+ statefile.write(pwm_value)
+ statefile.close()
+ subprocess.check_output("/usr/local/bin/gpio -g pwm " + str(PIN), shell=True)
+
+
+def main_loop():
+ """
+ The main loop in which we stay connected to the broker
+ """
+ while mqttc.loop() == 0:
+ logging.debug("Looping")
+
+
+# Use the signal module to handle signals
+signal.signal(signal.SIGTERM, cleanup)
+signal.signal(signal.SIGINT, cleanup)
+
+#connect to broker
+connect()
+
+main_loop()

0 comments on commit 7e517c0

Please sign in to comment.
Something went wrong with that request. Please try again.