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