def makeService(self, options): if options['data-dir'] != None: if not os.access(options['data-dir'], os.X_OK | os.W_OK): raise core.SmapException("Cannot access " + options['data-dir']) smapconf.SERVER['DataDir'] = options['data-dir'] inst = loader.load(options['conf']) # override defaults with command-line args smapconf.SERVER.update(dict([(k.lower(), v) for (k, v) in options.iteritems() if v != None])) if 'SuggestThreadPool' in smapconf.SERVER: reactor.suggestThreadPoolSize(int(smapconf.SERVER['SuggestThreadPool'])) inst.start() reactor.addSystemEventTrigger('before', 'shutdown', inst.stop) site = getSite(inst, docroot=smapconf.SERVER['docroot']) service = MultiService() # add HTTP and HTTPS servers to the twisted multiservice if 'port' in smapconf.SERVER: service.addService(internet.TCPServer(int(smapconf.SERVER['port']), site)) if 'sslport' in smapconf.SERVER: service.addService(internet.SSLServer(int(smapconf.SERVER['sslport']), site, SslServerContextFactory(smapconf.SERVER))) return service
def makeService(self, options): if options['data-dir'] != None: if not os.access(options['data-dir'], os.X_OK | os.W_OK): raise core.SmapException("Cannot access " + options['data-dir']) smapconf.SERVER['DataDir'] = options['data-dir'] inst = loader.load(options['conf']) # override defaults with command-line args smapconf.SERVER.update( dict([(k.lower(), v) for (k, v) in options.iteritems() if v != None])) if 'SuggestThreadPool' in smapconf.SERVER: reactor.suggestThreadPoolSize( int(smapconf.SERVER['SuggestThreadPool'])) inst.start() reactor.addSystemEventTrigger('before', 'shutdown', inst.stop) site = getSite(inst, docroot=smapconf.SERVER['docroot']) service = MultiService() # add HTTP and HTTPS servers to the twisted multiservice if 'port' in smapconf.SERVER: service.addService( internet.TCPServer(int(smapconf.SERVER['port']), site)) if 'sslport' in smapconf.SERVER: service.addService( internet.SSLServer(int(smapconf.SERVER['sslport']), site, SslServerContextFactory(smapconf.SERVER))) return service
def smap_load(): p = get_parser() opts, args = p.parse_args() if len(args) < 1: p.error("conf file is a required argument") log.startLogging(sys.stdout) sections = map(util.norm_path, args[1:]) inst = loader.load(args[0], sections=sections) for dpath, driver in inst.drivers.iteritems(): if len(sections) > 1 and not dpath in sections: continue if not hasattr(driver, "load"): log.err('Error: driver does not have "load" method') sys.exit(1) if hasattr(driver, 'reset') and \ callable(driver.reset) and \ opts.reset: log.msg("Resetting driver") driver.reset() try: # find the date range for loading... st, et = None, None now = dtutil.now(tzstr=opts.timezone) if (opts.start_time=="now_minus_1hour"): st = now - datetime.timedelta(hours=1) else: st = dtutil.strptime_tz(opts.start_time, opts.timefmt, opts.timezone) if (opts.end_time=="now"): et = now else: et = dtutil.strptime_tz(opts.end_time, opts.timefmt, opts.timezone) except: pass dl = [] for dpath, driver in inst.drivers.iteritems(): if len(sections) > 1 and not dpath in sections: continue # try: # loader = driver.load(st, et, cache=opts.cache) # except TypeError: dl.append(defer.maybeDeferred(driver.load, st, et, cache=opts.cache)) dl = defer.DeferredList(dl, consumeErrors=True) dl.addCallback(lambda x: inst._flush()) dl.addCallbacks(lambda x: reactor.callFromThread(reactor.stop)) reactor.run()
def makeService(options): if options['data-dir'] != None: if not os.access(options['data-dir'], os.X_OK | os.W_OK): raise util.SmapException("Cannot access " + options['data-dir']) smapconf.SERVER['DataDir'] = options['data-dir'] inst = loader.load(options['conf']) smapconf.start_logging() # override defaults with command-line args smapconf.SERVER.update(dict([(k.lower(), v) for (k, v) in options.iteritems() if v != None])) if 'SuggestThreadPool' in smapconf.SERVER: reactor.suggestThreadPoolSize(int(smapconf.SERVER['SuggestThreadPool'])) inst.start() reactor.addSystemEventTrigger('before', 'shutdown', inst.stop) site = getSite(inst, docroot=smapconf.SERVER['docroot']) service = MultiService() # add HTTP and HTTPS servers to the twisted multiservice meta = make_dns_meta(inst) af = 'tcp6' if options['ipv6'] else 'tcp' if 'port' in smapconf.SERVER: endpoint = "%s:%i" % (af, int(smapconf.SERVER['port'])) http = StreamServerEndpointService( serverFromString(reactor, endpoint), site) service.addService(http) broadcast(reactor, "_smap._tcp", int(smapconf.SERVER['port']), 'sMAP Server', meta) if 'sslport' in smapconf.SERVER: endpoint = "%s:%i" % (af, int(smapconf.SERVER['sslport'])) https = StreamServerEndpointService( serverFromString(reactor, endpoint), site) service.addService(internet.SSLServer(int(smapconf.SERVER['sslport']), site, SslServerContextFactory(smapconf.SERVER))) return service
def makeService(options): if options['data-dir'] != None: if not os.access(options['data-dir'], os.X_OK | os.W_OK): raise util.SmapException("Cannot access " + options['data-dir']) smapconf.SERVER['DataDir'] = options['data-dir'] inst = loader.load(options['conf']) smapconf.start_logging() # override defaults with command-line args smapconf.SERVER.update( dict([(k.lower(), v) for (k, v) in options.iteritems() if v != None])) if 'SuggestThreadPool' in smapconf.SERVER: reactor.suggestThreadPoolSize(int( smapconf.SERVER['SuggestThreadPool'])) inst.start() reactor.addSystemEventTrigger('before', 'shutdown', inst.stop) site = getSite(inst, docroot=smapconf.SERVER['docroot']) service = MultiService() # add HTTP and HTTPS servers to the twisted multiservice meta = make_dns_meta(inst) af = 'tcp6' if options['ipv6'] else 'tcp' if 'port' in smapconf.SERVER: endpoint = "%s:%i" % (af, int(smapconf.SERVER['port'])) http = StreamServerEndpointService(serverFromString(reactor, endpoint), site) service.addService(http) broadcast(reactor, "_smap._tcp", int(smapconf.SERVER['port']), 'sMAP Server', meta) if 'sslport' in smapconf.SERVER: endpoint = "%s:%i" % (af, int(smapconf.SERVER['sslport'])) https = StreamServerEndpointService( serverFromString(reactor, endpoint), site) service.addService( internet.SSLServer(int(smapconf.SERVER['sslport']), site, SslServerContextFactory(smapconf.SERVER))) return service
tasks.append(d.update(startdt, enddt)) startdt = startdt + datetime.timedelta(days=1) enddt = startdt + datetime.timedelta(days=1) d = defer.DeferredList(tasks) d.addCallback(lambda _: inst._flush()) return d def do_next_day(*args): global days if days > 0: days -= 1 d = next_day() d.addCallback(do_next_day) return d else: pass #reactor.stop() if __name__ == '__main__': if len(sys.argv) != 2: print "\n\t%s <conf>\n" % sys.argv[0] log.startLogging(sys.stdout) inst = loader.load(sys.argv[1]) do_next_day() reactor.run()
# you can set timeseries properties by accessing it as a dict. The # changes you make must follow the smap schema and you will get a # SmapSchemaException if you try to write an invalid object. s.get_timeseries("/sensor0")['Metadata'] = \ {'Instrument' : { 'Manufacturer' : "Stephen Dawson-Haggerty" }, 'Extra' : { 'Sucks' : 'Andrew' } } s.get_collection("/")["Metadata"] = {"Extra" : {"foo" : "bar"} } # loader.dump(s, 'default.ini') else: s = loader.load('default.ini') loader.dump(s, 'foo.ini') counter = 0 def newreading(): global counter #print '-'*50 s.get_collection('/')['Metadata']['Location'] = {'Room' : counter} s.get_collection('/').dirty_children() for i in xrange(0, 1): # s.get_timeseries('/sensor0')._add(util.now(), counter) # s.get_timeseries('/sensor1')._add(counter) s._add('/sensor0', util.now(), counter) s._add('/sensor1', counter) counter += 1 # the default flush period is one second, so we'll just rely on that
# you can set timeseries properties by accessing it as a dict. The # changes you make must follow the smap schema and you will get a # SmapSchemaException if you try to write an invalid object. s.get_timeseries("/sensor0")['Metadata'] = \ {'Instrument' : { 'Manufacturer' : "Stephen Dawson-Haggerty" }, 'Extra' : { 'Sucks' : 'Andrew' } } s.get_collection("/")["Metadata"] = {"Extra": {"foo": "bar"}} # loader.dump(s, 'default.ini') else: s = loader.load('default.ini') loader.dump(s, 'foo.ini') counter = 0 def newreading(): global counter #print '-'*50 s.get_collection('/')['Metadata']['Location'] = {'Room': counter} s.get_collection('/').dirty_children() for i in xrange(0, 1): # s.get_timeseries('/sensor0')._add(util.now(), counter) # s.get_timeseries('/sensor1')._add(counter) s._add('/sensor0', util.now(), counter) s._add('/sensor1', counter) counter += 1
for d in inst.drivers.itervalues(): if isinstance(d, bmo.BMOLoader): tasks.append(d.update(startdt, enddt)) startdt = startdt + datetime.timedelta(days=1) enddt = startdt + datetime.timedelta(days=1) d = defer.DeferredList(tasks) d.addCallback(lambda _: inst._flush()) return d def do_next_day(*args): global days if days > 0: days -= 1 d = next_day() d.addCallback(do_next_day) return d else: pass #reactor.stop() if __name__ == '__main__': if len(sys.argv) != 2: print "\n\t%s <conf>\n" % sys.argv[0] log.startLogging(sys.stdout) inst = loader.load(sys.argv[1]) do_next_day() reactor.run()