Skip to content

Commit

Permalink
file based logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksanislo committed Dec 31, 2018
1 parent 630bfb8 commit af1a20d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ RUN mkdir -p /usr/src/tcc-exporter && chown tcc-exporter:tcc-exporter /usr/src/t
WORKDIR /usr/src/tcc-exporter
RUN pip install dumb-init
RUN pip install pyyaml
COPY --chown=tcc-exporter:tcc-exporter . /usr/src/tcc-exporter/
ENV TCC_CONFIG_FILE="persistent/config.yml"
ENTRYPOINT ["dumb-init", "--"]
CMD ["python", "./tcc-exporter"]
ENV TCC_CONFIG_FILE="persistent/config.yml"
COPY --chown=tcc-exporter:tcc-exporter . /usr/src/tcc-exporter/
RUN python -OO -m py_compile tcc-exporter
4 changes: 4 additions & 0 deletions persistent/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
# Log entries for all previous levels are also included.
#log.level: INFO

# Optionally, you can provide a log.file where messages
# can be sent in addition to seeing them on stdout.
#log.file: None

# These are the username and password used to login to your
# mytotalconnectcomfort.com account to collect your stats.
# These must be filled, there are no default values for this.
Expand Down
28 changes: 20 additions & 8 deletions tcc-exporter
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ from http.cookiejar import LWPCookieJar
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.error import HTTPError, URLError

VERSION = '1.0.1'
VERSION = '1.1.0'
CONFIG_FILE = os.environ.get('TCC_CONFIG_FILE', 'persistent/config.yml')
PREFIX = 'https://mytotalconnectcomfort.com/'

# Set your TCC_LOGLEVEL to either the string or index number for your base logging level.
LOGLEVELS = ['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG']

lfh = None

class Client(object):
_backoff = 0 # No login delay at startup
Expand Down Expand Up @@ -137,18 +137,18 @@ class Client(object):

def locations(self):
path = 'portal/Location/GetLocationListData?page=1&filter='
data = self._request_data(path, {})
data = self._request_data(path, {}) # POST but no data.
return data

def location_overview(self, locationId):
path = 'portal/Device/GetZoneListData?locationId=%s&page=1' % (locationId,)
data = self._request_data(path, {})
data = self._request_data(path, {}) # POST but no data.
return data

def device_status(self, device_id):
utc_milliseconds = int(round(time.time() * 1000))
path = 'portal/Device/CheckDataSession/%s?_=%s' % (device_id, utc_milliseconds)
data = self._request_data(path)
data = self._request_data(path) # GET
return data


Expand Down Expand Up @@ -213,17 +213,25 @@ def do_stuff(device, name, obj):


def log(level, *message):
global config
global config, lfh
# If we were called with a descriptive string, find the index.
if isinstance(level, str):
level = LOGLEVELS.index(level)
level = LOGLEVELS.index(level.upper())
# TCC_LOGLEVEL can be either a text or a number.
if config['log.level'].isdigit():
debug_level = int(config['log.level'])
else:
debug_level = LOGLEVELS.index(config['log.level'].upper())
if (level <= debug_level):
print("[{0}] {1}".format(LOGLEVELS[level], " ".join(message)))
if config['log.file']:
lfh = open(config['log.file'], 'a')
try:
timestring = time.strftime('%D %T')
lfh.write("{0} [{1}] {2}\n".format(timestring, LOGLEVELS[level], " ".join(message)))
lfh.flush()
except:
pass


def hup_handler(signum, frame):
Expand Down Expand Up @@ -257,11 +265,13 @@ def find_devices():


def load_config():
global client, config
global client, config, lfh
if not 'config' in globals():
config = {}
if 'log.level' in config: # We can't log unless we know our level.
log('DEBUG', 'Loading config: {0}'.format(CONFIG_FILE))
if lfh: # Close any previously open log file.
lfh.close()
# Load config file.
try:
with open(CONFIG_FILE, 'r') as cfile:
Expand All @@ -273,6 +283,8 @@ def load_config():
# Set defaults if missing.
if not 'log.level' in config:
config['log.level'] = 'INFO'
if not 'log.file' in config:
config['log.file'] = None
if not 'tcc.timeout' in config:
config['tcc.timeout'] = 10
if not 'tcc.backoff' in config:
Expand Down

0 comments on commit af1a20d

Please sign in to comment.