Ejemplo n.º 1
0
def main():
    global now
    utility.logging_config()
    locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
    config.read('config.ini')
    with open('sensor.json') as json_file:
        sensor_json = json_file.read()
    devices = json.loads(sensor_json,
                         object_pairs_hook=collections.OrderedDict)
    for device in devices:
        for kind, attr in device['output'].items():
            if kind == 'temperature':
                groups[attr['group']][attr['name']] = Temperature(
                    attr['low'],
                    attr['high'],
                    attr['name'],
                    device['input']['interval'],
                    attr['fail-notify'])
            elif kind == 'switch':
                groups[attr['group']][attr['name']] = Switch(
                    attr['name'],
                    device['input']['interval'],
                    attr['fail-notify'])
    with website(), api.ApiServer(accept_record), \
            notify.MailSender(
                config['email']['source_address'],
                config['email']['admin_address'],
                config['email']['user_address'],
                config['email'].getboolean('enable_email')) as mail:
        while True:
            # get new record
            start = time.perf_counter()
            now = datetime.datetime.now(tz=datetime.timezone.utc)
            record_counter = int()
            with contextlib.suppress(queue.Empty):
                while True:
                    group, name, record = inbox.get(block=False)
                    groups[group][name].save(record)
                    record_counter += 1
            # update content
            for group, series_dict in groups.items():
                for series in series_dict.values():
                    if series.error:
                        mail.queue(series.error, PAUSE_WARN_FAILURE)
                    if series.warning:
                        mail.queue(series.warning, PAUSE_WARN_VALUE)
                detail_html(group, series_dict.values())
            with contextlib.suppress(utility.CallDenied):
                make_plots()
            mail.send_all()
            # log processing
            utility.memory_check()
            logging.info('updated website in {:.3f}s, {} new records'.format(
                time.perf_counter() - start, record_counter))
            time.sleep(INTERVAL)
Ejemplo n.º 2
0
def main():
    utility.logging_config()
    hostname = socket.gethostname()
    with open(CONFIG) as json_file:
        sensor_json = json_file.read()
    sensors = list()
    for sensor in json.loads(sensor_json):
        if sensor['input']['hostname'] != hostname:
            continue
        if sensor['input']['type'] == 'thermosolar':
            sensors.append(Thermosolar(
                sensor['input']['file'],
                [sensor['output']['temperature']['group'],
                 sensor['output']['switch']['group']],
                [sensor['output']['temperature']['name'],
                 sensor['output']['switch']['name']],
                sensor['input']['interval']))
        elif sensor['input']['type'] == 'ds18b20':
            sensors.append(DS18B20(
                sensor['input']['file'],
                [sensor['output']['temperature']['group']],
                [sensor['output']['temperature']['name']],
                sensor['input']['interval']))
        elif sensor['input']['type'] == 'mdeg_celsius':
            sensors.append(MdegCelsius(
                sensor['input']['file'],
                [sensor['output']['temperature']['group']],
                [sensor['output']['temperature']['name']],
                sensor['input']['interval']))
    with api.ApiClient() as connection:
        while True:
            for sensor in sensors:
                now = datetime.datetime.now(tz=datetime.timezone.utc)
                now = now.replace(microsecond=0)
                start = time.perf_counter()
                try:
                    result = list(sensor.values())
                except utility.CallDenied:
                    continue
                except SensorError as err:
                    logging.error('failure {}: {}'.format(sensor, err))
                    continue
                logging.info('{} updated in {:.3f}s'.format(
                    sensor, time.perf_counter() - start))
                for group, name, value in result:
                    logging.info('{}/{}: {} / {}'.format(group, name,
                                                         now, value))
                    connection.send(group=group, name=name, value=value,
                                    timestamp=int(now.timestamp()))
            time.sleep(INTERVAL)