Ejemplo n.º 1
0
def main() -> int:
    args = parse_cmdline()

    try:
        cfg = Config(args.syscfg)
    except Exception as e:
        print("Error loading system config file; " + str(e), file=sys.stderr)
        return 1

    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.INFO,
                        datefmt='%Y-%m-%d %H:%M:%S',
                        stream=sys.stderr)
    # eDNA uses the Broadcom SOC pin numbering scheme
    GPIO.setmode(GPIO.BCM)
    # If we don't suppress warnings, as message will be printed to stderr
    # everytime GPIO.setup is called on a pin that isn't in the default
    # state (input).
    GPIO.setwarnings(False)

    status = False
    try:
        if args.out:
            status = runtest(cfg, args, Datafile(args.out))
        else:
            status = runtest(cfg, args, None)
    except Exception as e:
        logging.exception("Error running the pump test")
    finally:
        if args.clean:
            GPIO.cleanup()
    return 0 if status else 1
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(
        description="Validate an eDNA deployment configuration")
    parser.add_argument("cfg",
                        metavar="FILE",
                        nargs="?",
                        default="",
                        help="deployment configuration file")
    parser.add_argument(
        "--sys",
        metavar="FILE",
        default=os.path.expanduser("~/.config/edna/system.cfg"),
        help="location of the system confguration file")
    args = parser.parse_args()

    try:
        cfg = Config(args.sys)
    except Exception as e:
        print("Error loading system config file; " + str(e), file=sys.stderr)
        return 1

    if args.cfg:
        try:
            cfg.load(args.cfg)
        except Exception as e:
            print("Error loading deployment config file; " + str(e),
                  file=sys.stderr)
            return 1

    try:
        missing = validate(cfg)
        if missing:
            print("Missing entries: {}".format(";".join(missing)))
            return 1
    except Exception as e:
        print("Validation failed: {}".format(str(e)), file=sys.stderr)
        return 1
    return 0
Ejemplo n.º 3
0
def main() -> int:
    args = parse_cmdline()

    try:
        cfg = Config(args.syscfg)
    except Exception as e:
        print("Error loading system config file; " + str(e), file=sys.stderr)
        return 1

    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.INFO,
                        datefmt='%Y-%m-%d %H:%M:%S',
                        stream=sys.stderr)
    status = False
    try:
        if args.out:
            status = runtest(cfg, args, Datafile(args.out))
        else:
            status = runtest(cfg, args, None)
    except Exception:
        logging.exception("Error running the sensor test")

    return 0 if status else 1
Ejemplo n.º 4
0
def main() -> int:
    args = parse_cmdline()

    try:
        cfg = Config(args.syscfg)
    except Exception as e:
        print("Error loading system config file; " + str(e), file=sys.stderr)
        return 1

    try:
        cfg.load(args.cfg)
    except Exception as e:
        print("Error loading deployment config file; " + str(e), file=sys.stderr)
        return 1

    # Validate configuration files
    missing = cfg.validate()
    if missing:
        print("Missing configuration entries: {}".format(";".join(missing)))
        return 1

    # Generate deployment ID and directory name
    id = datetime.datetime.now(tz=datetime.timezone.utc).strftime("%Y%m%dT%H%M%S")
    dir = os.path.join(args.datadir, "edna_" + id)
    deployment = Deployment(id=id, dir=dir)

    # Create deployment directory
    os.makedirs(deployment.dir, exist_ok=True)
    # Initialize logging
    init_logging(deployment.dir, deployment.id, debug=args.debug)
    # Save configuration to deployment directory
    with open(os.path.join(deployment.dir, "deploy.cfg"), "w") as fp:
        cfg.write(fp)

    # Save deployment ID if specified
    if args.id != "":
        with open(os.path.join(deployment.dir, "id"), "w") as fp:
            print(args.id, file=fp)

    signal.signal(signal.SIGINT, abort)
    signal.signal(signal.SIGTERM, abort)

    logger = logging.getLogger()
    # eDNA uses the Broadcom SOC pin numbering scheme
    GPIO.setmode(GPIO.BCM)
    # If we don't suppress warnings, a message will be printed to stderr
    # everytime GPIO.setup is called on a pin that isn't in the default
    # state (input).
    GPIO.setwarnings(False)

    prfilt: Callable[[float], float]
    if args.alpha > 0:
        prfilt = EMA(args.alpha)
    else:
        prfilt = lambda x: x

    status = False
    try:
        name = "edna_" + deployment.id + ".ndjson"
        with open(os.path.join(deployment.dir, name), "w") as fp:
            status = runedna(cfg, deployment, Datafile(fp), prfilt)
    except Exception:
        logger.exception("Deployment aborted with an exception")

    os.makedirs(args.outbox, exist_ok=True)
    # Archive the deployment directory to the OUTBOX
    arpath = os.path.join(args.outbox, "edna_" + deployment.id + ".tar.gz")
    logger.info("Archiving deployment directory to %s", arpath)
    with tarfile.open(arpath, "w:gz") as tar:
        head, tail = os.path.split(deployment.dir)
        os.chdir(head)
        tar.add(tail)

    if args.clean:
        GPIO.cleanup()

    return 0 if status else 1
Ejemplo n.º 5
0
 def setUp(self):
     path = os.path.join(os.path.dirname(__file__), "example.cfg")
     self.cfg = Config(path)
     self.badcfg = Config(StringIO(INPUT))
Ejemplo n.º 6
0
 def setUp(self):
     self.cfg = Config(StringIO(INPUT))
     self.cfg.load(StringIO(OVERRIDE))
Ejemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(description="Prime an eDNA flow path")
    parser.add_argument("cfg",
                        metavar="FILE",
                        help="system configuration file")
    parser.add_argument("valve",
                        metavar="N",
                        type=int,
                        help="valve# to prime, 1-3")
    parser.add_argument("--time",
                        metavar="SECS",
                        type=float,
                        default=30,
                        help="runtime in seconds")
    parser.add_argument("--prmax",
                        metavar="PSI",
                        type=float,
                        default=12,
                        help="maximum filter pressure in psi")
    args = parser.parse_args()

    try:
        cfg = Config(args.cfg)
    except Exception as e:
        print("Error loading config file; " + str(e), file=sys.stderr)
        sys.exit(1)

    if args.valve < 1 or args.valve > 3:
        print("Valve number must be between 1 and 3", file=sys.stderr)
        sys.exit(1)

    logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
                        level=logging.INFO,
                        datefmt='%Y-%m-%d %H:%M:%S',
                        stream=sys.stderr)

    # eDNA uses the Broadcom SOC pin numbering scheme
    GPIO.setmode(GPIO.BCM)
    try:
        adc = ADS1115(address=0x48, busnum=cfg.get_int('Adc', 'Bus'))
        pr_chan = cfg.get_int('Pressure.Filter', 'Chan')
        pr_gain = cfg.get_float('Pressure.Filter', 'Gain')

        motor = cfg.get_int('Motor.Sample', 'Enable')
        GPIO.setup(motor, GPIO.OUT)

        # Config file key for valve
        vkey = "Valve." + str(args.valve)
        sample_valve = Valve(cfg.get_int(vkey, 'Enable'),
                             cfg.get_int(vkey, 'Power'),
                             cfg.get_int(vkey, 'Gnd'))
        eth_valve = Valve(cfg.get_int('Valve.Ethanol', 'Enable'),
                          cfg.get_int('Valve.Ethanol', 'Power'),
                          cfg.get_int('Valve.Ethanol', 'Gnd'))
    except BadEntry as e:
        print(str(e), file=sys.stderr)
        GPIO.cleanup()
        sys.exit(2)

    try:
        prime_cycle(sample_valve, eth_valve, motor,
                    partial(checkpr, adc, pr_chan, pr_gain, args.prmax),
                    args.time)
    finally:
        GPIO.cleanup()