2016-12-05 19:06:34 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
|
|
|
# Copyright © 2010-2016 Lance Edgar
|
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
TempMon client daemon
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import unicode_literals, absolute_import
|
|
|
|
|
|
|
|
import time
|
|
|
|
import datetime
|
2016-12-10 11:22:53 -06:00
|
|
|
import random
|
2016-12-05 19:06:34 -06:00
|
|
|
import socket
|
|
|
|
import logging
|
|
|
|
|
2016-12-10 11:22:53 -06:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
from rattail.daemon import Daemon
|
|
|
|
from rattail_tempmon.db import Session, model as tempmon
|
2016-12-10 11:22:53 -06:00
|
|
|
from rattail.exceptions import ConfigurationError
|
2016-12-05 19:06:34 -06:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TempmonClient(Daemon):
|
|
|
|
"""
|
|
|
|
Linux daemon implementation of Tempmon client
|
|
|
|
"""
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
"""
|
|
|
|
This method is invoked upon daemon startup. It is meant to run/loop
|
|
|
|
"forever" or until daemon stop.
|
|
|
|
"""
|
2016-12-10 11:22:53 -06:00
|
|
|
# maybe generate random data instead of reading from true probe
|
|
|
|
self.dummy_probes = self.config.getbool('tempmon.client', 'dummy_probes', default=False)
|
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
# figure out which client we are
|
2016-12-10 11:22:53 -06:00
|
|
|
hostname = self.config.get('tempmon.client', 'hostname', default=socket.gethostname())
|
2016-12-05 19:06:34 -06:00
|
|
|
session = Session()
|
2016-12-10 11:22:53 -06:00
|
|
|
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))
|
2016-12-05 19:06:34 -06:00
|
|
|
client_uuid = client.uuid
|
2017-02-07 14:47:57 -06:00
|
|
|
self.delay = client.delay or 60
|
2016-12-05 19:06:34 -06:00
|
|
|
session.close()
|
|
|
|
|
|
|
|
# main loop: take readings, pause, repeat
|
|
|
|
while True:
|
2017-02-07 14:47:57 -06:00
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
session = Session()
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
try:
|
|
|
|
client = session.query(tempmon.Client).get(client_uuid)
|
|
|
|
self.delay = client.delay or 60
|
|
|
|
if client.enabled:
|
2016-12-05 19:06:34 -06:00
|
|
|
for probe in client.enabled_probes():
|
|
|
|
self.take_reading(session, probe)
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
except:
|
|
|
|
log.exception("Failed to read/record temperature data (but will keep trying)")
|
|
|
|
session.rollback()
|
2016-12-05 19:06:34 -06:00
|
|
|
|
|
|
|
else:
|
2017-02-07 14:47:57 -06:00
|
|
|
# make sure we show as being online
|
|
|
|
if not client.online:
|
|
|
|
client.online = True
|
2017-06-01 16:21:48 -05:00
|
|
|
try:
|
|
|
|
session.commit()
|
|
|
|
except:
|
2017-06-01 17:16:31 -05:00
|
|
|
# TODO: pretty sure we need to add a retry for this..
|
2017-06-01 16:21:48 -05:00
|
|
|
log.exception("failed to commit changes to database")
|
|
|
|
raise
|
2017-02-07 14:47:57 -06:00
|
|
|
|
|
|
|
finally:
|
2016-12-05 19:06:34 -06:00
|
|
|
session.close()
|
|
|
|
|
2017-02-07 14:47:57 -06:00
|
|
|
time.sleep(self.delay)
|
2016-12-05 19:06:34 -06:00
|
|
|
|
|
|
|
def take_reading(self, session, probe):
|
|
|
|
"""
|
|
|
|
Take a single reading and add to Rattail database.
|
|
|
|
"""
|
|
|
|
reading = tempmon.Reading()
|
|
|
|
reading.client = probe.client
|
|
|
|
reading.probe = probe
|
|
|
|
reading.degrees_f = self.read_temp(probe)
|
|
|
|
reading.taken = datetime.datetime.utcnow()
|
|
|
|
session.add(reading)
|
|
|
|
return reading
|
|
|
|
|
|
|
|
def read_temp(self, probe):
|
|
|
|
"""
|
|
|
|
Check for good reading, then format temperature to our liking
|
|
|
|
"""
|
2016-12-10 11:22:53 -06:00
|
|
|
if self.dummy_probes:
|
|
|
|
return self.random_temp(probe)
|
2016-12-05 19:06:34 -06:00
|
|
|
lines = self.read_temp_raw(probe)
|
|
|
|
while lines[0].strip()[-3:] != 'YES':
|
|
|
|
time.sleep(0.2)
|
|
|
|
lines = self.read_temp_raw(probe)
|
|
|
|
equals_pos = lines[1].find('t=')
|
|
|
|
if equals_pos != -1:
|
|
|
|
temp_string = lines[1][equals_pos+2:]
|
|
|
|
temp_c = float(temp_string) / 1000.0
|
|
|
|
temp_f = temp_c * 9.0 / 5.0 + 32.0
|
|
|
|
return round(temp_f,4)
|
|
|
|
|
|
|
|
def read_temp_raw(self, probe):
|
|
|
|
"""
|
|
|
|
Function that gets the raw temp data
|
|
|
|
"""
|
|
|
|
with open(probe.device_path, 'rt') as therm_file:
|
|
|
|
return therm_file.readlines()
|
|
|
|
|
2016-12-10 11:22:53 -06:00
|
|
|
def random_temp(self, probe):
|
|
|
|
temp = random.uniform(probe.critical_temp_min - 5, probe.critical_temp_max + 5)
|
|
|
|
return round(temp, 4)
|
|
|
|
|
2016-12-05 19:06:34 -06:00
|
|
|
|
|
|
|
def make_daemon(config, pidfile=None):
|
|
|
|
"""
|
|
|
|
Returns a tempmon client daemon instance.
|
|
|
|
"""
|
|
|
|
if not pidfile:
|
|
|
|
pidfile = config.get('rattail.tempmon', 'client.pid_path',
|
|
|
|
default='/var/run/rattail/tempmon-client.pid')
|
|
|
|
return TempmonClient(pidfile, config=config)
|