Added '--skip-on-off' option

This commit is contained in:
Shy 2017-04-18 10:43:33 +02:00
parent b72a1ac605
commit e6e7493879

View file

@ -17,8 +17,8 @@
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>. # this program. If not, see <http://www.gnu.org/licenses/>.
def kitchentext(delay=200, wait=False, restore=False, poweron=False, def kitchentext(delay=200, skip_if_off=False, poweron=False, restore=False,
verbose=False, debug=False): wait=False, verbose=False, debug=False):
import sys, signal import sys, signal
from c4ctrl import C4Interface, Kitchenlight from c4ctrl import C4Interface, Kitchenlight
@ -49,10 +49,16 @@ def kitchentext(delay=200, wait=False, restore=False, poweron=False,
wait = wait or restore wait = wait or restore
# Store previous state. # Store previous state.
if restore: if restore or skip_if_off:
c4 = C4Interface() c4 = C4Interface()
saved_state = c4.pull([kl.topic, kl.powertopic]) saved_state = c4.pull([kl.topic, kl.powertopic])
if skip_if_off:
# Stop here if kitchenlight is turned off.
if saved_state[1].payload == b'\x00':
verbose and print("Found Kitchenlight turned off and '-i' flag given. Exiting.")
return
# We want to be able to restore the saved Kitchenlight if we receive a # We want to be able to restore the saved Kitchenlight if we receive a
# SIGERM signal. We do this by creating an registering a custom exception # SIGERM signal. We do this by creating an registering a custom exception
# class. # class.
@ -136,25 +142,28 @@ if __name__ == "__main__":
parser.add_argument( parser.add_argument(
"-d", "--delay", type=int, default=200, "-d", "--delay", type=int, default=200,
help="delay in ms (speed of the text, default is 200)") help="delay in ms (speed of the text, default is 200)")
parser.add_argument(
"-w", "--wait", action="store_true", default=False,
help="wait until the text has been displayed")
parser.add_argument(
"-r", "--restore", action="store_true", default=False,
help="restore the Kitchenlight to its prior state after the text has \
been displayed (implies --wait)")
parser.add_argument(
"-p", "--power-on", action="store_true", default=False,
help="turn on Kitchenlight if it is powered off")
parser.add_argument( parser.add_argument(
"-f", "--fork", action="store_true", "-f", "--fork", action="store_true",
help="fork to background") help="fork to background")
parser.add_argument( parser.add_argument(
"-F", action="store_true", "-F", action="store_true",
help="like '-f', but print PID of forked process to stdout") help="like '-f', but print PID of forked process to stdout")
parser.add_argument(
"-i", "--skip-if-off", action="store_true", default=False,
help="do nothing if the kitchenlight is turned off")
parser.add_argument(
"-p", "--power-on", action="store_true", default=False,
help="turn on Kitchenlight if it is powered off")
parser.add_argument(
"-r", "--restore", action="store_true", default=False,
help="restore the Kitchenlight to its prior state after the text has \
been displayed (implies --wait)")
parser.add_argument( parser.add_argument(
"-v", "--verbose", action="store_true", "-v", "--verbose", action="store_true",
help="be more verbose") help="be more verbose")
parser.add_argument(
"-w", "--wait", action="store_true", default=False,
help="wait until the text has been displayed")
args = parser.parse_args() args = parser.parse_args()
if args.fork or args.F: if args.fork or args.F:
@ -176,9 +185,10 @@ if __name__ == "__main__":
sys.exit() sys.exit()
kitchentext(delay=args.delay, kitchentext(delay=args.delay,
wait=args.wait, skip_if_off=args.skip_if_off,
restore=args.restore,
poweron=args.power_on, poweron=args.power_on,
restore=args.restore,
wait=args.wait,
verbose=args.verbose, verbose=args.verbose,
debug=args.debug) debug=args.debug)