Exemplo n.º 1
0
class NestPublisher:
    def __init__(self, username, password, serial, device_id, bus_url, interval=DEFAULT_INTERVAL):
        self.client = nest.Nest(username, password)
        self.serial = serial
        self.device_id = device_id
        self.messagebus_client = MessageBusClient(bus_url)
        self.interval = interval
        self.logger = logging.getLogger("NestPublisher")

    def dump_device_data(self):

        for device in self.client.devices:
            logging.info("Device Name %s - Serial %s", device.name, device.serial)

    def get_device(self):

        for device in self.client.devices:
            if device._serial == self.serial:
                return device

        return None

    def poll_device(self):

        try:
            thermostat = self.get_device()
        except Exception as ex:
            self.logger.warn("Error getting thermostat: %s", ex)
            return

        if thermostat is None:
            raise Exception("Cannot find thermostat " + self.name)

        temperature_c = thermostat.temperature
        temperature_f = nest.utils.c_to_f(temperature_c)
        target_temperature_c = thermostat.target
        target_temperature_f = nest.utils.c_to_f(target_temperature_c)
        humidity = thermostat.humidity

        logging.debug("Temperature: %s", temperature_f)
        logging.debug("Target Temperature: %s", target_temperature_f)
        logging.debug("Humidity: %s", humidity)

        self.messagebus_client.send_reading(
            self.device_id, message_types.EVENT_TEMPERATURE, value=temperature_f, units="f"
        )
        self.messagebus_client.send_reading(
            self.device_id, message_types.EVENT_TARGET_TEMPERATURE, value=target_temperature_f, units="f"
        )
        self.messagebus_client.send_reading(self.device_id, message_types.EVENT_HUMIDITY, value=humidity)

    def start(self):

        while True:
            self.poll_device()
            time.sleep(self.interval)
Exemplo n.º 2
0
import messagebus.message_types as message_types
import messagebus.defaults as defaults

from messagebus.client import MessageBusClient

import time
import math

if __name__ == "__main__":

    bus_url = defaults.DEFAULT_POST_MESSAGE_URL
    messagebus_client = MessageBusClient(bus_url)

    while True:

        localtime = time.localtime()
        seconds_into_hour = localtime.tm_min * 60 + localtime.tm_sec        
        percent_into_hour = seconds_into_hour / 3600.0

        radians = percent_into_hour * 2 * math.pi

        value = math.sin(radians) * 100.0

        print value

        messagebus_client.send_reading("generator.sine", message_types.EVENT_TEMPERATURE, value=value, units="f")

        time.sleep(10)