Skip to content

Instantly share code, notes, and snippets.

@evfairchild evfairchild/fridge_pub.py Secret
Created Aug 20, 2015

Embed
What would you like to do?
import sys
import logging
import time
from tempread import TempRead
from zymbit.client import Client
from timeit import default_timer
class Publish(object):
def __init__(self):
self.evaporator = TempRead('Evaporator')
self.compressor = TempRead('Compressor')
self.ambient = TempRead('Ambient')
self.case = TempRead('Case')
self.devices = ['Evaporator', 'Compressor', 'Ambient', 'Case']
self.address = ['28-0000061543fd', '28-000006151b77', '28-0000061529d3', '28-000006156310']
self.probes = [self.evaporator, self.compressor, self.ambient, self.case]
self.temperatures = [None, None, None, None]
self.last_temperatures = [0, 0, 0, 0]
self.timer = default_timer()
self.client = Client()
def data(self):
for i in range(0, len(self.probes)):
temp_c, temp_f = self.probes[i].read_temp()
self.temperatures[i] = round(temp_c)
if self.temperatures[i] != self.last_temperatures[i]:
payload = {'Device': self.devices[i], 'Address': self.address[i], 'value': self.temperatures[i]}
self.logger.info("payload: {}".format(payload))
self.handle_message(payload)
self.last_temperatures[i] = self.temperatures[i]
elif default_timer() - self.timer > 300: # after 5 minutes send everything
payload = {'Device': self.devices[i], 'Address': self.address[i], 'value': self.temperatures[i]}
self.logger.info("payload: {}".format(payload))
self.handle_message(payload)
self.last_temperatures[i] = self.temperatures[i]
if i == len(self.probes)-1:
self.timer = default_timer()
def run(self):
self.data()
time.sleep(5)
@property
def logger(self):
name = '{}.{}'.format(__name__, self.__class__.__name__)
return logging.getLogger(name)
def handle_message(self, payload):
if payload is None:
self.logger.warning('got an empty payload')
return
try:
device = payload['Device']
address = payload['Address']
value = payload['value']
if address == 'DutyCycle':
self.client.send('data', {'key': 'analyzer.{}.{}'.format(device, address), 'value': value})
else:
self.client.send('data', {'key': 'analyzer.{}.{}'.format(device, address[0:2]+address[3:]), 'value': value})
except TypeError:
self.logger.error('unable to send payload={}'.format(payload))
if __name__ == '__main__':
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
while True:
try:
logger = logging.getLogger(__name__)
logger.info('Publishing Fridge Data to Zymbit')
fridge_pub = Publish()
fridge_pub.run()
except Exception, exc:
logger = logging.getLogger(__name__)
logger.exception(exc)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.