示例#1
0
def main():

    a = ArgumentParser()
    a.add_argument('--host', default='192.168.1.140',
            help='WiFi adapter address (%(default)s).')
    a.add_argument('--port', type=int, default=8899,
            help='WiFi adapter port (%(default)s).')
    a.add_argument('--inv-addr', type=int, default=2,
            help='Inverter address (%(default)s)')
    a.add_argument('--read-delay', type=float, default=0.05,
            help='Time between command and read (%(default)f).')
    a.add_argument('--connect-timeout', type=float, default=None,
            help='Specify a timeout in seconds for connecting.')
    a.add_argument('--default-timeout', type=float, default=5.0,
            help='Set a default socket timeout in seconds (%(default)s).')
    a.add_argument('--loop-interval', type=int, default=10,
            help='''Time between inverter polls.  If set to zero, the inverter
is polled a single time and the process exits (%(default)s).
Note that changing this may affect the relevance of some commands eg
"energy in last 10 seconds".''')
    a.add_argument('--backoff', type=int, default=60, 
            help='''Sleep time after a socket timeout error (%(default)s).  The inverter
goes offline each night and requests result in socket timeout and subsequent process
exit.  This timeout is intended to reduce the frequency of spawning processes during
the night.''')
    a.add_argument('--csv', help='''Optionally write CSV to file.  The name
may contain `strftime` format strings.  If the string is "stdout" the CSV
output will be directed to `sys.stdout`.''')
    opt = a.parse_args()

    log.info('aurora starting')
    log.debug(opt)

    if opt.csv:
        if opt.csv == 'stdout':
            toutput = pv.tocsv(None)
        else:
            csvname = dt.datetime.now().strftime(opt.csv)
            toutput = pv.tocsv(open(csvname, 'a'))
    else:
        toutput = pv.prettyprint()


    with skt.create_connection((opt.host, opt.port), 
            opt.connect_timeout) as sock:

        if opt.default_timeout:
            sock.settimeout(opt.default_timeout)

        inverterrdr = ft.partial(pv.execcmd, sock, opt.inv_addr, 
                readdelay=opt.read_delay)

        try:
            if opt.loop_interval:
                pv.scheduler(opt.loop_interval, inverterpoll,
                    inverterrdr=inverterrdr, operations=operations,
                    target=toutput)
            else:
                inverterpoll(inverterrdr, operations, target=toutput)

        except skt.timeout:
            log.error('Socket timed out, application will exit')
            if opt.backoff:
                log.info('Backing off for {0} seconds.'.format(opt.backoff))
                time.sleep(opt.backoff)
        except KeyboardInterrupt:
            log.warning('Ctrl-C received, application will exit')

    log.info('aurora exiting')
示例#2
0
文件: aurx.py 项目: Vice95/pyaurora
def main():

    a = ArgumentParser()
    a.add_argument('--host',
                   default='192.168.1.140',
                   help='WiFi adapter address (%(default)s).')
    a.add_argument('--port',
                   type=int,
                   default=8899,
                   help='WiFi adapter port (%(default)s).')
    a.add_argument('--inv-addr',
                   type=int,
                   default=2,
                   help='Inverter address (%(default)s)')
    a.add_argument('--read-delay',
                   type=float,
                   default=0.05,
                   help='Time between command and read (%(default)f).')
    a.add_argument('--connect-timeout',
                   type=float,
                   default=None,
                   help='Specify a timeout in seconds for connecting.')
    a.add_argument(
        '--default-timeout',
        type=float,
        default=5.0,
        help='Set a default socket timeout in seconds (%(default)s).')
    a.add_argument(
        '--loop-interval',
        type=int,
        default=10,
        help='''Time between inverter polls.  If set to zero, the inverter
is polled a single time and the process exits (%(default)s).
Note that changing this may affect the relevance of some commands eg
"energy in last 10 seconds".''')
    a.add_argument(
        '--backoff',
        type=int,
        default=60,
        help='''Sleep time after a socket timeout error (%(default)s).  The
inverter goes offline each night and requests result in socket timeout and
subsequent process exit.  This timeout is intended to reduce the frequency of
spawning processes during the night.''')
    a.add_argument('--csv',
                   help='''Optionally write CSV to file.  The name
may contain `strftime` format strings.  If the string is "stdout" the CSV
output will be directed to `sys.stdout`.''')
    a.add_argument(
        '--pub-url',
        default='tcp://127.0.0.1:8080',
        help='''Specify a zeromq URL to publish JSON to (%(default)s).''')
    a.add_argument(
        'csv_in',
        nargs='?',
        default='aurora_2015-07-20.csv',
        help='''Specify and input CSV file for testing (%(default)s).''')
    opt = a.parse_args()

    log.info('aurora starting')
    log.debug(opt)

    if opt.csv:
        if opt.csv == 'stdout':
            toutput = pv.tocsv(None)
        else:
            csvname = dt.datetime.now().strftime(opt.csv)
            toutput = pv.tocsv(open(csvname, 'a'))
    else:
        if opt.pub_url:
            context = zmq.Context()
            zock = context.socket(zmq.PUB)
            zock.bind(opt.pub_url)
            sink = tozmq(zock)
        else:
            sink = pv.tostream()

        toutput = pv.tojson(sink)

    with skt.create_connection((opt.host, opt.port),
                               opt.connect_timeout) as sock:

        if opt.default_timeout:
            sock.settimeout(opt.default_timeout)

        dictreader = csv.DictReader(open(opt.csv_in, 'r'))

        try:
            if opt.loop_interval:
                pv.scheduler(opt.loop_interval,
                             mockinverterpoll,
                             dictreader=dictreader,
                             operations=operations,
                             target=toutput)
            else:
                mockinverterpoll(dictreader, operations, target=toutput)

        except skt.timeout:
            log.error('Socket timed out, application will exit')
            if opt.backoff:
                log.info('Backing off for {0} seconds.'.format(opt.backoff))
                time.sleep(opt.backoff)
        except KeyboardInterrupt:
            log.warning('Ctrl-C received, application will exit')

    log.info('aurora exiting')
示例#3
0
def main():

    a = ArgumentParser()
    a.add_argument("--host", default="192.168.1.140", help="WiFi adapter address (%(default)s).")
    a.add_argument("--port", type=int, default=8899, help="WiFi adapter port (%(default)s).")
    a.add_argument("--inv-addr", type=int, default=2, help="Inverter address (%(default)s)")
    a.add_argument("--read-delay", type=float, default=0.05, help="Time between command and read (%(default)f).")
    a.add_argument("--connect-timeout", type=float, default=None, help="Specify a timeout in seconds for connecting.")
    a.add_argument(
        "--default-timeout", type=float, default=5.0, help="Set a default socket timeout in seconds (%(default)s)."
    )
    a.add_argument(
        "--loop-interval",
        type=int,
        default=10,
        help="""Time between inverter polls.  If set to zero, the inverter
is polled a single time and the process exits (%(default)s).
Note that changing this may affect the relevance of some commands eg
"energy in last 10 seconds".""",
    )
    a.add_argument(
        "--backoff",
        type=int,
        default=60,
        help="""Sleep time after a socket timeout error (%(default)s).  The
inverter goes offline each night and requests result in socket timeout and
subsequent process exit.  This timeout is intended to reduce the frequency of
spawning processes during the night.""",
    )
    a.add_argument(
        "--csv",
        help="""Optionally write CSV to file.  The name
may contain `strftime` format strings.  If the string is "stdout" the CSV
output will be directed to `sys.stdout`.""",
    )
    a.add_argument(
        "--pub-url", default="tcp://127.0.0.1:8080", help="""Specify a zeromq URL to publish JSON to (%(default)s)."""
    )
    a.add_argument(
        "csv_in",
        nargs="?",
        default="aurora_2015-07-20.csv",
        help="""Specify and input CSV file for testing (%(default)s).""",
    )
    opt = a.parse_args()

    log.info("aurora starting")
    log.debug(opt)

    if opt.csv:
        if opt.csv == "stdout":
            toutput = pv.tocsv(None)
        else:
            csvname = dt.datetime.now().strftime(opt.csv)
            toutput = pv.tocsv(open(csvname, "a"))
    else:
        if opt.pub_url:
            context = zmq.Context()
            zock = context.socket(zmq.PUB)
            zock.bind(opt.pub_url)
            sink = tozmq(zock)
        else:
            sink = pv.tostream()

        toutput = pv.tojson(sink)

    with skt.create_connection((opt.host, opt.port), opt.connect_timeout) as sock:

        if opt.default_timeout:
            sock.settimeout(opt.default_timeout)

        dictreader = csv.DictReader(open(opt.csv_in, "r"))

        try:
            if opt.loop_interval:
                pv.scheduler(
                    opt.loop_interval, mockinverterpoll, dictreader=dictreader, operations=operations, target=toutput
                )
            else:
                mockinverterpoll(dictreader, operations, target=toutput)

        except skt.timeout:
            log.error("Socket timed out, application will exit")
            if opt.backoff:
                log.info("Backing off for {0} seconds.".format(opt.backoff))
                time.sleep(opt.backoff)
        except KeyboardInterrupt:
            log.warning("Ctrl-C received, application will exit")

    log.info("aurora exiting")
示例#4
0
def main():

    a = ArgumentParser()
    a.add_argument('--host',
                   default='192.168.1.140',
                   help='WiFi adapter address (%(default)s).')
    a.add_argument('--port',
                   type=int,
                   default=8899,
                   help='WiFi adapter port (%(default)s).')
    a.add_argument('--inv-addr',
                   type=int,
                   default=2,
                   help='Inverter address (%(default)s)')
    a.add_argument('--read-delay',
                   type=float,
                   default=0.05,
                   help='Time between command and read (%(default)f).')
    a.add_argument('--connect-timeout',
                   type=float,
                   default=None,
                   help='Specify a timeout in seconds for connecting.')
    a.add_argument(
        '--default-timeout',
        type=float,
        default=5.0,
        help='Set a default socket timeout in seconds (%(default)s).')
    a.add_argument(
        '--loop-interval',
        type=int,
        default=10,
        help='''Time between inverter polls.  If set to zero, the inverter
is polled a single time and the process exits (%(default)s).
Note that changing this may affect the relevance of some commands eg
"energy in last 10 seconds".''')
    a.add_argument(
        '--backoff',
        type=int,
        default=60,
        help=
        '''Sleep time after a socket timeout error (%(default)s).  The inverter
goes offline each night and requests result in socket timeout and subsequent process
exit.  This timeout is intended to reduce the frequency of spawning processes during
the night.''')
    a.add_argument('--csv',
                   help='''Optionally write CSV to file.  The name
may contain `strftime` format strings.  If the string is "stdout" the CSV
output will be directed to `sys.stdout`.''')
    opt = a.parse_args()

    log.info('aurora starting')
    log.debug(opt)

    if opt.csv:
        if opt.csv == 'stdout':
            toutput = pv.tocsv(None)
        else:
            csvname = dt.datetime.now().strftime(opt.csv)
            toutput = pv.tocsv(open(csvname, 'a'))
    else:
        toutput = pv.prettyprint()

    with skt.create_connection((opt.host, opt.port),
                               opt.connect_timeout) as sock:

        if opt.default_timeout:
            sock.settimeout(opt.default_timeout)

        inverterrdr = ft.partial(pv.execcmd,
                                 sock,
                                 opt.inv_addr,
                                 readdelay=opt.read_delay)

        try:
            if opt.loop_interval:
                pv.scheduler(opt.loop_interval,
                             inverterpoll,
                             inverterrdr=inverterrdr,
                             operations=operations,
                             target=toutput)
            else:
                inverterpoll(inverterrdr, operations, target=toutput)

        except skt.timeout:
            log.error('Socket timed out, application will exit')
            if opt.backoff:
                log.info('Backing off for {0} seconds.'.format(opt.backoff))
                time.sleep(opt.backoff)
        except KeyboardInterrupt:
            log.warning('Ctrl-C received, application will exit')

    log.info('aurora exiting')