User Tools

Site Tools


ircterm

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
ircterm [2015-03-21 23:04] timircterm [2016-10-07 17:03] – [Powering on/off the screen automatically] tim
Line 111: Line 111:
 /home/pi/idle.py: /home/pi/idle.py:
 <code> <code>
-#!/usr/bin/python+#!/usr/bin/env python
  
-import logging +import paho.mqtt.client as mqtt 
-import os +import subprocess
-import pika +
-import time+
  
-logging.getLogger('').setLevel(logging.INFO) +last_state = None
-logging.getLogger('pika').setLevel(logging.ERROR)+
  
-amqp_host = "amqp.hacklab" +def on_connect(client, userdata, flags, rc): 
-amqp_exchange = "events" +    client.subscribe("sensor/global/presence")
-topic = "presence.status"+
  
-tvstate = False+def on_message(client, userdata, msg): 
 +    global last_state
  
-connection = pika.BlockingConnection(pika.ConnectionParameters(host=amqp_host, heartbeat_interval=30)+    # ignore retained (non-realtimemessages 
-channel = connection.channel() +    #if msg.retain: 
-channel.exchange_declare(exchange=amqp_exchange, type="topic")+    #    return
  
-result channel.queue_declare(exclusive=True) +    if last_state !msg.payload: 
-queue_name = result.method.queue +        # state has changed 
-channel.queue_bind(exchange=amqp_exchangequeue=queue_name, routing_key=topic)+        if msg.payload == "active": 
 +            # screen on 
 +            subprocess.call("tvservice -p", shell=True) 
 +        elif msg.payload == "empty": 
 +            # screen off 
 +            subprocess.call("tvservice -o"shell=True) 
 +        last_state = msg.payload
  
-def callback(ch, method, properties, body): +mqtt.Client() 
-    global tvstate +m.on_connect on_connect 
- +m.on_message on_message 
-    changed properties.headers['changed'+m.connect("mqtt") 
-    state = properties.headers['state'+m.loop_forever()
-    old_state = properties.headers.get('old_state', None+
- +
-    if state ='ACTIVE': +
-        if tvstate == False: +
-            os.system("tvservice -p") +
-            os.system("echo `date` TV ON command issued >> /home/pi/idle.log"+
-            tvstate True +
-    elif state == 'EMPTY': +
-        os.system("tvservice -o") +
-        os.system("echo `date` TV OFF command issued >> /home/pi/idle.log"+
-        tvstate = False +
- +
- +
-channel.basic_consume(callback, +
-                      queue=queue_name, +
-                      no_ack=True) +
-channel.start_consuming()+
 </code> </code>
  
Line 163: Line 149:
 <code> <code>
 apt-get install python-pip apt-get install python-pip
-pip install pika+pip install paho-mqtt
 </code> </code>
  
ircterm.txt · Last modified: 2021-05-21 14:00 by simon

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki