示例#1
0
def server():
    print("Setting up memory ...", end='')
    mc_client = volmem_client.get()
    df_pub_list = mc_client.get("df_pub_list")

    while True:

        recent_messages = get_messages(limit=10, delay=10)
        old_messages = get_messages(limit=50)
        if recent_messages:
            print("Sending recent messages")
            published_ids, unpublished_ids = log_to_remote(recent_messages)
            if published_ids:
                update_messages_status(id_list=published_ids, stat=1)
            if unpublished_ids:
                update_messages_status(id_list=unpublished_ids, stat=-1)

        elif old_messages:
            print("Sending old messages")
            published_ids, unpublished_ids = log_to_remote(old_messages)
            if published_ids:
                update_messages_status(id_list=published_ids, stat=1)
            if unpublished_ids:
                update_messages_status(id_list=unpublished_ids, stat=-1)
        else:
            print("No more  messages to log")
            time.sleep(10)
示例#2
0
def set_config():
    gate_cnf = GatewayConfig()

    print(gate_cnf.config)

    mc_client = volmem_client.get()
    mc_client.set("gateway_config", gate_cnf.config)

    print("Config file {} set to memory".format(CFILE))
示例#3
0
def connect(dbname="db_local"):
    conn = client.get().get("gateway_config")[dbname]
    try:
        db = MySQLdb.connect(conn["host"], conn["user"], conn["pwd"],
                             conn["schema"])
        cur = db.cursor()
        return db, cur
    except TypeError:
        print("Error Connection Value")
        return False
    except (MySQLdb.Error, MySQLdb.Warning) as e:
        print(e)
        return False
示例#4
0
def server():
    mqtt_client = mqttlib.get_client()
    mc_client = volmem_client.get()
    df_pub_list = mc_client.get("df_pub_list")

    try:
        if not df_pub_list:
            volmem_client.reset_memory("df_pub_list")
    except ValueError:
        print("df_pub_list maybe present. continuing")

    while True:
        publish_pub_list(mqtt_client, mc_client)

        time.sleep(1)
def read_publish(delay):
    ina = read()

    btv = round(ina.supply_voltage(), 2)
    cur = round(ina.current(), 2)
    ts = dt.today().strftime("%y%m%d%H%M%S")

    mc = client.get()
    cnf = mc.get("gateway_config")
    gateway_name = "{}-{}".format(cnf["gateway"]["name"],
                                  cnf["gateway"]["logger"])

    message_value = "{}$BTV:{};BTA:{};DTM:{}$".format(gateway_name, btv, cur,
                                                      ts)

    print(message_value)

    # client.push_pub_list(message_value)
    # time.sleep(delay)
    # client.push_df_pub_list(message_value)
    txn.sql_txn_log(message_value)
from Adafruit_IO import MQTTClient
import time
from volmem import client as volmem_client
import sys

mc_client = volmem_client.get()
mqtt_config = mc_client.get("gateway_config")["mqtt"]

ADAFRUIT_IO_KEY = mqtt_config["key"]
ADAFRUIT_IO_USERNAME = mqtt_config["username"]
THROTTLE_FEED_ID = mqtt_config["feed"]


class DisconnectException(Exception):
    pass


class ThrottleException(Exception):
    pass


def publish(feed=None, value=""):
    start = time.time()
    mqtt_client = MQTTClient(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
    mqtt_client.connect()
    print("Connection time: {0}".format(time.time() - start))

    print('Publishing {0}'.format(value))
    start = time.time()
    print(mqtt_client.publish(feed, value))
    print("Publish time: {0}".format(time.time() - start))
def set():
    cfile = os.path.dirname(os.path.realpath(__file__)) + "/this_gateway.cnf"
    mem = client.get()
    config = ConfigParser()
    print(config.read(cfile))
    mem.set("gatewaycnf", config)
import logging
from ina219 import INA219
import time
import argparse
import sys
from mqtt import mqttlib
from datetime import datetime as dt
from volmem import client
from dbio import txn

SHUNT_OHMS = 0.1
MAX_EXPECTED_AMPS = 0.5
ADDRESS = client.get().get("gateway_config")["ina219"]["address"]


def read(print_val=False):
    ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS, address=ADDRESS)
    ina.configure(ina.RANGE_16V, ina.GAIN_AUTO)

    if print_val:
        print("Bus Voltage    : %.3f V" % ina.voltage())
        print("Bus Current    : %.3f mA" % ina.current())
        print("Supply Voltage : %.3f V" % ina.supply_voltage())
        print("Shunt voltage  : %.3f mV" % ina.shunt_voltage())
        print("Power          : %.3f mW" % ina.power())

    return ina


def get_arguments():
    parser = argparse.ArgumentParser()