Add support for dummy probes (random temp data)

This commit is contained in:
Lance Edgar 2016-12-10 11:22:53 -06:00
parent 5f489c47dc
commit d87ea6c22d

View file

@ -28,11 +28,15 @@ from __future__ import unicode_literals, absolute_import
import time
import datetime
import random
import socket
import logging
from sqlalchemy.orm.exc import NoResultFound
from rattail.daemon import Daemon
from rattail_tempmon.db import Session, model as tempmon
from rattail.exceptions import ConfigurationError
log = logging.getLogger(__name__)
@ -48,11 +52,19 @@ class TempmonClient(Daemon):
This method is invoked upon daemon startup. It is meant to run/loop
"forever" or until daemon stop.
"""
# maybe generate random data instead of reading from true probe
self.dummy_probes = self.config.getbool('tempmon.client', 'dummy_probes', default=False)
# figure out which client we are
hostname = self.config.get('tempmon.client', 'hostname', default=socket.gethostname())
session = Session()
client = session.query(tempmon.Client)\
.filter_by(hostname=socket.gethostname())\
.one()
try:
client = session.query(tempmon.Client)\
.filter_by(hostname=hostname)\
.one()
except NoResultFound:
session.close()
raise ConfigurationError("No tempmon client configured for hostname: {}".format(hostname))
client_uuid = client.uuid
session.close()
@ -102,6 +114,8 @@ class TempmonClient(Daemon):
"""
Check for good reading, then format temperature to our liking
"""
if self.dummy_probes:
return self.random_temp(probe)
lines = self.read_temp_raw(probe)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
@ -120,6 +134,10 @@ class TempmonClient(Daemon):
with open(probe.device_path, 'rt') as therm_file:
return therm_file.readlines()
def random_temp(self, probe):
temp = random.uniform(probe.critical_temp_min - 5, probe.critical_temp_max + 5)
return round(temp, 4)
def make_daemon(config, pidfile=None):
"""