예제 #1
0
def status():
    answer = {}
    app_name = request.args.get('app_name')

    answer['service_name'] = 'coverage-service'
    answer['version'] = get_env.get_version()

    print('status() : app_name=' + app_name.__str__() + ', version=' +
          answer['version'])
    response = jsonify(answer)

    return response
예제 #2
0
def status():
    answer = {}
    app_name = request.args.get('app_name')
    uuid = request.args.get('uuid')

    answer['status'] = 'OK'
    answer['uuid'] = uuid.__str__()
    answer['service_name'] = 'light-service'
    answer['version'] = get_env.get_version()

    print('status() : uuid=' + uuid + ', app_name=' + app_name.__str__() +
          ', version=' + answer['version'])
    response = jsonify(answer)

    return response
예제 #3
0
def main():
    version = get_env.get_version()
    poll_secs = get_env.get_poll_secs()

    beepy.beep(sound=2)  # run a sound when this daemon starts up
    time.sleep(2)
    print('watchdogd started, version=' + version)

    while True:
        aercus_connected = check_aercus_serial_connected()

        if not aercus_connected:
            # make some noise
            beepy.beep(sound=1)
            time.sleep(0.5)
            beepy.beep(sound=1)
            time.sleep(0.5)
            beepy.beep(sound=1)

        time.sleep(poll_secs)  # typically 60 seconds
예제 #4
0
def process_in_msg(client, display_topic, mqtt_dict):
    msg = {}
    global LastForecast

    site_height_m = 50
    alert_str = '*'

    version = get_env.get_version()

    vane_height_m = get_env_app.get_vane_height_m()
    wind_speed_multiplier = wind_calibration.calc_vane_height_to_10m_multiplier(
        vane_height_m)

    # The fundamental metrics
    pressure_absolute = mqtt_dict['pressure']
    temp_c = mqtt_dict['temp']
    humidity = mqtt_dict['humidity']

    cumulus_forecast = mqtt_dict['zambretti_forecast']
    wdir = mqtt_dict['wdir']
    winddeg = mqtt_dict['winddeg']
    wind_knots = float(mqtt_dict['wspeed'])  # averaged over 10 mins
    gust_knots = float(mqtt_dict['wgust'])  # averaged over 10 mins
    rrate = mqtt_dict['rrate']
    presstrendval = mqtt_dict['presstrendval']
    # beaufort = mqtt_dict['beaufort']
    # dew_point_cumulus = mqtt_dict['dew']

    wind_knots_corrected = round(wind_speed_multiplier * wind_knots, 1)
    wind_speed_corrected_kph = metfuncs.knots_to_kph(wind_knots_corrected)
    beaufort_corrected = 'F' + metfuncs.kph_to_beaufort(
        wind_speed_corrected_kph).__str__()

    gust_knots_corrected = round(wind_speed_multiplier * gust_knots, 1)
    gust_corrected_kph = metfuncs.knots_to_kph(gust_knots_corrected)
    gust_corrected = 'F' + metfuncs.kph_to_beaufort(
        gust_corrected_kph).__str__()

    moon_status, light_percent = moon_phase.get_moon_phase_now()

    dew_point_c = temp_derived_values.get_dew_point(temp_c, humidity)
    print('dew_point_c=' + dew_point_c.__str__())
    # print('dew_point_cumulus=' + dew_point_cumulus.__str__())

    # see https://www.weather.gov/source/zhu/ZHU_Training_Page/thunderstorm_stuff/Thunderstorms/thunderstorms.htm
    if dew_point_c >= 12:
        is_lightning_possible_str = 'LTNG?'
    else:
        is_lightning_possible_str = 'NO_LTNG'

    wet_bulb_c = temp_derived_values.get_wet_bulb(temp_c, pressure_absolute,
                                                  dew_point_c)

    fog_possible = simple_fog.fog_algo_yocto(temp_c,
                                             dew_point_c,
                                             wet_bulb_c,
                                             humidity,
                                             permitted_range=0.1)
    if fog_possible:
        fog_str = 'FOG?'
    else:
        fog_str = 'NO_FOG'

    cloud_base_ft = cloud_base.calc_cloud_base_ft(temp_c, dew_point_c)
    print('cloud_base_ft=' + cloud_base_ft.__str__())

    sunrise = '07:02'
    sunset = '21:29'

    # Testing variables
    # temp_c = -1
    # rrate = 3.3
    # beaufort = 'F6'

    # Burt says to record pressure adjusted to MSL (Mean Sea Level)
    pressure_msl = round(
        pressure_absolute +
        mean_sea_level_pressure.msl_k_factor(site_height_m, temp_c), 1)

    if presstrendval > 0:
        presstrend_str = '+'
    else:
        presstrend_str = ''

    if beaufort_corrected == 'F0':
        wind_str = 'F0'
    else:
        wind_str = beaufort_corrected + '>' + gust_corrected.lstrip(
            'F') + ' ' + wdir.__str__()

    # ALERTS
    if temp_c < 0:
        alert_str += 'FREEZE+'
    if int(gust_corrected.replace('F', '')) >= 5:
        alert_str += 'WIND+'
    if rrate > 0:
        alert_str += 'RAIN+'
    alert_str = alert_str.rstrip('+')
    alert_str += '*'

    # if no alerts, then display local time
    if alert_str == '**':
        alert_str = 'No Alerts'

    tendency, pressure_forecast = ptendency.get_tendency(presstrendval)
    print('pressure_forecast=' + pressure_forecast)
    print('tendency=' + tendency)
    # Choose the mode (most common) of the last n values of the pressure_forecast
    Forecasts.add(pressure_forecast)
    print('Forecasts=' + Forecasts.get_values().__str__())
    most_common_forecast = Forecasts.get_most_common()
    print('most_common_forecast=' + most_common_forecast)
    print('LastForecast=' + LastForecast)
    if most_common_forecast != LastForecast:
        forecast_changed_str = '*'
    else:
        forecast_changed_str = ''

    # forecast in UPPER CASE implies it is STABLE
    all_same = Forecasts.all_same_value()
    if all_same:
        most_common_forecast_display = most_common_forecast.upper()
    else:
        most_common_forecast_display = most_common_forecast.lower()

    line_pressure = pressure_msl.__str__() + \
                   ' ' + presstrend_str + presstrendval.__str__() + ' ' + forecast_changed_str + most_common_forecast_display

    line_moon = light_percent.__str__() + '%' + ' ' + moon_status

    line_metrics1 = temp_c.__str__() + 'C' +\
                   ' ' + dew_point_c.__str__() + 'C' +\
                    ' ' + humidity.__str__() + '%'

    line_metrics2 = 'Twb=' + wet_bulb_c.__str__() + 'C'

    line_metrics3 = 'Cbase=' + cloud_base_ft.__str__() + ' ft'

    line_metrics4 = 'r=' + sunrise + ' ' + 's=' + sunset

    line_metrics5 = fog_str + \
                   ' ' + is_lightning_possible_str

    line_metrics6 = rrate.__str__() + \
                   ' ' + wind_str

    line_fcast = cumulus_forecast
    line_alert = alert_str
    line_time = time.ctime()  # TODO - strip the seconds and leading space

    display_text = [
        line_pressure, line_metrics1, line_metrics2, line_metrics3,
        line_metrics4, line_metrics5, line_metrics6, line_moon, line_alert,
        line_fcast, line_time
    ]

    log_display_to_file(pressure_msl, presstrend_str, presstrendval,
                        cumulus_forecast, pressure_forecast, temp_c,
                        dew_point_c, humidity, rrate, beaufort_corrected, wdir,
                        winddeg, gust_corrected, is_lightning_possible_str,
                        fog_str, line_pressure, line_metrics1, line_metrics2,
                        line_metrics3, line_metrics4, line_metrics5,
                        line_metrics6, line_moon, alert_str)

    msg['msg_type'] = 'DISPLAY_DATA'
    msg['publisher'] = get_env.get_mqtt_name()
    msg['version'] = version
    msg['display_text'] = display_text
    msg['beep'] = 0

    jsonString = json.dumps(msg)

    print(jsonString)
    client.publish(display_topic, jsonString)

    LastForecast = most_common_forecast
예제 #5
0
# microservice
import os
import time
import random

from flask import Flask, jsonify, g

import definitions
import get_env

app = Flask(__name__)
version = get_env.get_version()

# fixme : this does not give info about the actual exception
@app.errorhandler(500)
def error_handling(error):
    answer = {}
    answer['error'] = str(error)

    print('light_service() : error : ' + error.__str__())
    response = jsonify(answer, 500)

    return response


# an endpoint that can be polled with little overhead
# @app.route('/status')
# def status():
#     answer = {}
#     app_name = request.args.get('app_name')
#     uuid = request.args.get('uuid')
예제 #6
0
def main():
    england_endpoint = (
        'https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=nation;areaName=england&structure={"date":"date", "areaName":"areaName", "areaCode":"areaCode", "newCasesByPublishDate":"newCasesByPublishDate", "cumCasesByPublishDate":"cumCasesByPublishDate", "newDeathsByDeathDate":"newDeathsByDeathDate", "cumDeathsByDeathDate":"cumDeathsByDeathDate"}'
    )
    version = get_env.get_version()
    verbose = get_env.get_verbose()
    stage = get_env.get_stage()
    poll_secs = get_env_app.get_poll_secs()
    telegraf_endpoint_host = get_env.get_telegraf_endpoint(
    )  # can be read from ENV

    print('cv19d started, version=' + version)
    print('stage=' + get_env.get_stage())
    if stage == 'DEV':
        verbose = True
    print('verbose=' + verbose.__str__())
    print('telegraf_endpoint_host=' + telegraf_endpoint_host)
    print('poll_secs=' + poll_secs.__str__())

    while True:
        try:
            print('waiting to sync main loop...')
            sync_start_time.wait_until_minute_flip(30)  # every 30 minutes
            start_secs = time.time()
            data = get_data(england_endpoint)

            if data is None:
                time.sleep(180)
                continue  # go to start of loop

            yesterdays_data = data['data'][1]
            # pprint(yesterdays_data)
            if yesterdays_data['newDeathsByDeathDate'] == None:
                newDeathsByDeathDate = 0
            else:
                newDeathsByDeathDate = yesterdays_data['newDeathsByDeathDate']

            if yesterdays_data['newCasesByPublishDate'] == None:
                newCasesByPublishDate = 0
            else:
                newCasesByPublishDate = yesterdays_data[
                    'newCasesByPublishDate']

            metrics = {
                'metric_name': 'covid19',
                'newDeaths': newDeathsByDeathDate,
                'newCases': newCasesByPublishDate
            }

            print(time.ctime())
            pprint(metrics)

            send_metrics_to_telegraf.send_metrics(telegraf_endpoint_host,
                                                  metrics, verbose)

            stop_secs = time.time()
            mins_between_updates = 30
            sleep_secs = (mins_between_updates * 60) - (stop_secs -
                                                        start_secs) - 10
            time.sleep(sleep_secs)

        except Exception as e:
            print('Error : ' + e.__str__())
            traceback.print_exc()
            time.sleep(60)
            continue
예제 #7
0
파일: bcnd.py 프로젝트: crouchr/bcn
def main():
    version = get_env.get_version()
    verbose = get_env.get_verbose()
    stage = get_env.get_stage()

    telegraf_endpoint_host = get_env_app.get_telegraf_endpoint(
    )  # can be read from ENV

    poll_secs = get_env_app.get_poll_secs()
    max_rate = -9999999  # GBP
    min_rate = 999999
    max_rate_usd = -9999999
    min_rate_usd = 999999

    last_rate_usd = None

    if stage == 'PRD':
        bcnd_config_filename = '/data/btc/bcnd.yml'
    else:
        bcnd_config_filename = '../data/btc/bcnd.yml'

    print('bcnd started, version=' + version)
    print('verbose=' + verbose.__str__())
    print('stage=' + stage.__str__())
    print('bcnd_config_filename=' + bcnd_config_filename)
    print('telegraf_endpoint_host=' + telegraf_endpoint_host)
    print('poll_secs=' + poll_secs.__str__())

    metric_name = 'bcnd_metrics'

    while True:
        try:
            status, bcn_info = get_bcn_price()
            if status != 200:
                print('error calling API, sleeping...')
                time.sleep(60)
                continue  # go to start of loop

            bcnd_vars = get_bcnd_config(bcnd_config_filename)
            btc = float(bcnd_vars['num_btc'])
            gbp_invested = float(bcnd_vars['gbp_invested'])
            high_alarm = int(bcnd_vars['high_alarm'])
            low_alarm = int(bcnd_vars['low_alarm'])
            return_percent_line = int(bcnd_vars['return_percent_line'])
            btc_in_gbp = float(btc) * bcn_info[
                'GBP']  # what are my BTC worth in GBP

            if bcn_info['GBP'] < min_rate:
                min_rate = bcn_info['GBP']
                min_rate_time = time.ctime()
            if bcn_info['GBP'] > max_rate:
                max_rate = bcn_info['GBP']
                max_rate_time = time.ctime()

            if bcn_info['USD'] < min_rate_usd:
                min_rate_usd = bcn_info['USD']
                min_rate_usd_time = time.ctime()
            if bcn_info['USD'] > max_rate_usd:
                max_rate_usd = bcn_info['USD']
                max_rate_usd_time = time.ctime()

            if last_rate_usd == None:
                last_rate_usd = bcn_info['USD']

            btc_usd_change = round(bcn_info['USD'] - last_rate_usd, 2)

            return_percent = round(100 * btc_in_gbp / gbp_invested, 2)

            print(time.ctime() + \
                  ' (updated at ' + bcn_info['updateduk'] + ')' + \
                  ' : BTC rate (GBP) = ' + bcn_info['GBP'].__str__() + \
                  ', BTC = ' + btc.__str__() + \
                  ', BTC invested = £' + round(gbp_invested, 2).__str__() + \
                  ' : => GBP value = £' + round(btc_in_gbp, 2).__str__()
                  )

            # Construct the metric bundle
            metrics = {
                'metric_name': metric_name,
                'btc': btc,
                'btc_worth_gbp': btc_in_gbp,
                'bitcoin_gbp': bcn_info['GBP'],
                'bitcoin_usd': bcn_info['USD'],
                'bitcoin_eur': bcn_info['EUR'],
                'btc_min_rate': min_rate,
                'btc_max_rate': max_rate,
                'btc_min_rate_usd': min_rate_usd,
                'btc_max_rate_usd': max_rate_usd,
                'btc_usd_change': btc_usd_change,
                'btc_invested': gbp_invested,
                'return_percent': return_percent,
                'return_percent_line': return_percent_line,
                'high_alarm': high_alarm,
                'low_alarm': low_alarm,
            }

            pprint(metrics)

            send_metrics_to_telegraf.send_metrics(telegraf_endpoint_host,
                                                  metrics, verbose)

            last_rate_usd = bcn_info['USD']

            time.sleep(poll_secs)

        except Exception as e:
            print('main() : Error : ' + e.__str__())
            print('sleeping...')
            # beep.warning(num_beeps=2, sound=3)
            time.sleep(180)  # wait 3 mins
            continue
예제 #8
0
파일: netmond.py 프로젝트: crouchr/netmon
def main():
    version = get_env.get_version()
    verbose = get_env.get_verbose()
    stage = get_env.get_stage()

    telegraf_endpoint_host = get_env_app.get_telegraf_endpoint()    # can be read from ENV
    poll_secs = get_env_app.get_poll_secs()
    probe_name = get_env_app.get_probe_name()

    print('netmond started, version=' + version)
    print('verbose=' + verbose.__str__())
    print('stage=' + stage.__str__())
    print('telegraf_endpoint_host=' + telegraf_endpoint_host)
    print('poll_secs=' + poll_secs.__str__())
    print('probe_name=' + probe_name.__str__())

    destinations = host.Destinations(probe_name)

    # destinations.add_host(host.Host('blackhole', '192.168.1.199', False))
    destinations.add_host(host.Host('kube', '192.168.1.5', True))
    # destinations.add_host(host.Host('mr-dell', '192.168.1.180', True))
    destinations.add_host(host.Host('j1900', '192.168.1.6', True))
    # destinations.add_host(host.Host('dsl_router', '192.168.1.1', False))
    destinations.add_host(host.Host('google_dns', '8.8.8.8', False))
    # destinations.add_host(host.Host('netgear', '192.168.1.8', False))
    # destinations.add_host(host.Host('pi', '192.168.1.12', True))    # add iperf to startup script
    # destinations.add_host(host.Host('web_server', '192.168.1.102', False))
    # destinations.add_host(host.Host('registry', '192.168.1.109', False))

    if stage == 'DEV':
        sudo = True
    else:
        sudo = False

    while True:
        print('-------------------------')
        start_time = time.time()
        print(start_time)
        print('start = ' + time.ctime())
        try:
            for host_to_test in destinations.hosts:

                if host_to_test.iperf_capable:
                    jitter_measurements = iperf.measure_jitter_endpoint(host_to_test.hostname)
                    throughput_measurements = iperf.measure_throughput_endpoint(host_to_test.hostname)

                ping_measurements = ping.ping_endpoint(host_to_test.hostname, sudo)

                # Construct the metric bundle
                metric_name = 'netmon_' + probe_name + '_to_' + host_to_test.name
                metrics = {
                    'metric_name': metric_name,
                    'packet_loss': ping_measurements['packet_loss'],
                    'rtt_min': ping_measurements['rtt_min'],
                    'rtt_avg': ping_measurements['rtt_avg'],
                    'rtt_max': ping_measurements['rtt_max']
                }

                # if destination is iperf-capable then add throughput metric
                if host_to_test.iperf_capable:
                    metrics['throughput_mbps'] = throughput_measurements['throughput_mbps']
                    metrics['jitter'] = jitter_measurements['jitter']

                pprint(metrics)

                send_metrics_to_telegraf.send_metrics(telegraf_endpoint_host, metrics, verbose)
                time.sleep(1)

        except Exception as e:
            print('main() : Error : ' + e.__str__())
            print('sleeping...')
            time.sleep(60)     # wait 1 mins
            continue

        stop_time = time.time()
        print('stop = ' + time.ctime())
        time_used = stop_time - start_time
        print('time_used=' + time_used.__str__())
        time_to_wait_secs = poll_secs - time_used
        print('time_to_wait_secs = ' + time_to_wait_secs.__str__())
        print('waiting...')
        time.sleep(time_to_wait_secs)