Ejemplo n.º 1
0
 def dispatcher_event_thread(callback, interface):
     try:
         callback(interface)
     except:
         Logger.log_traceback(callback.im_self)
     finally:
         self._decrement_event_threads()
Ejemplo n.º 2
0
    def dispatch_command(self, irc, msg, command, params):
        def dispatcher_command_thread(callback, interface, params):
            try:
                callback(interface, params)
            except:
                Logger.log_traceback(callback.im_self)
            finally:
                self._decrement_event_threads()

        params = Arguments(params)

        callbacks = self.get_command_callbacks(command)

        Logger.debug1("Found %d receiver(s) for command '%s'" % (len(callbacks), command))

        if callbacks:
            interface = IRCMessageController(irc, msg)

            for (obj, callback, privileged) in callbacks:
                try:
                    # This will abort instantly as soon as a command without access is found
                    if privileged:
                        if not self.acl.check_access(msg.source, command.lower()):
                            interface.reply("Access denied")
                            return

                    thread = threading.Thread(target = dispatcher_command_thread,
                                              kwargs = {"callback": callback, "interface": interface, "params": params})
                    self._increment_event_threads()
                    thread.start()
                except Exception as e:
                    Logger.log_traceback(callback.im_self)
Ejemplo n.º 3
0
    def __init__(self, database = 'data/acl.db'):
        self.db = database
        self.config = ConfigController()

        try:
            conn = sqlite3.connect(self.db)
            #cursor = conn.cursor()

            # ACL
            fp = open("resources/acl/acl.sql", "r")
            data = fp.read()
            fp.close()

            # Triggers
            fp = open("resources/acl/triggers.sql", "r")
            data += fp.read()
            fp.close()

            conn.executescript(data)
            conn.commit()

            if not self._user_exists("any"):
                self.add_user("any")
                self.user_add_hostmask("any", "*!*@*")

        except sqlite3.Error as e:
            Logger.fatal("Unable to create ACL database: %s" % e)
Ejemplo n.º 4
0
    def handle_channel_msg(self, data):

        # normal msg doesn't contain subtype  =>  F**K, normal msg contain subtype now.
        #if data.get("subtype"):
        #    return

        # filter mismatch channel
        c_id = data.get("vchannel_id")
        if c_id != self.bc_default_channel:
            return

        sender_id = ""
        name = ""

        # get sender
        if data.get("subtype") == "robot":
            sender_id = data.get("robot_id")
            name = Cache.get_robot_true_name(sender_id)
        else:
            sender_id = data.get("uid")
            name = Cache.get_user_en_name(sender_id)

        # filter sender
        if sender_id in self.id_filter:
            Logger.log("sender %s (%s) in the filter list, abort msg" % (name, sender_id))
            return

        msg = data.get("text")

        # filter msg
        if msg.startswith(self.msg_enable_pre):
            msg = msg.split(self.msg_enable_pre, 1)[-1:][0]
            self.send_irc_msg(name, msg)
        else:
            Logger.log("bc msg (%s) was not the standardized format, abort forwarding" % (msg))
Ejemplo n.º 5
0
class Influence:
    def __init__(self, source, neighbors, args):
        self.source = source
        self.neighbors = neighbors
        self.args = args
        self.log = Logger().log

    def inner(self, source, target):
        return [ [] for i in range(3) ]
    
    def run(self):
        values = []
                    
        for target in self.neighbors:
            msg = '{0}: {1}->{2}'
            self.log.info(msg.format(repr(self),self.source.node,target.node))
            
            (left, right, delay) = self.inner(self.source, target)
            d = {
                'type': repr(self),
                'source': self.source.node,
                'target': target.node,
                'pearson': st.pearsonr(left, right),
                'spearman': st.spearmanr(left, right),
                'delay': np.mean(delay),
                }
            values.append(d)

        return values

    def __repr__(self):
        return type(self).__name__
Ejemplo n.º 6
0
def del_record_api():
    tmp_dict = {}
    tmp_dict["success"] = "False"
    auth_result = auth_user()
    if not auth_result[0]:
        tmp_dict["msg"] = "no sso auth"
        return json.dumps(tmp_dict)
    else:
        userinfo = auth_result[1]
    if request.method == "POST":
        try:
            domain_id = request.form["domain_id"]
            domain_info = Domain.get_domain_by_domain_id(domain_id)
            record_id = request.form["record_id"]
            if if_user_id_have_domain_rights(userinfo["user_id"], domain_id):
                Logger.action_log(userinfo["username"] + " del record ")
                if domain_info[2] == "dnspod":
                    result = del_dnspod_record_lib(domain_id, record_id)
                    tmp_dict["success"] = result[0]
                    tmp_dict["msg"] = result[1]
                    return json.dumps(tmp_dict)
                elif domain_info[2] == "local_bind":
                    result = del_dns_record_lib(domain_id, record_id)
                    tmp_dict["success"] = result[0]
                    tmp_dict["msg"] = result[1]
                    return json.dumps(tmp_dict)
            else:
                tmp_dict["msg"] = "no this domain rights"
                return json.dumps(tmp_dict)
        except Exception, e:
            print e
            tmp_dict["msg"] = "unknown error"
            return json.dumps(tmp_dict)
Ejemplo n.º 7
0
 def send_irc_msg(self, user, msg):
     if len(msg) == 0:
         return
     c = self.irc_channel
     msg = self.pre_handle_irc_msg(user, msg)
     Logger.log_msg_transfer("bc => irc: %s" % msg)
     self.irc_bot.privmsg(c, msg)
Ejemplo n.º 8
0
class LuceneEngine(object):
    
    def __init__(self, 
                 index_name='generic', 
                 wipe_index=False,
                 analyzer=lucene.SimpleAnalyzer(),
                 ):
        self.logger=Logger(logger_name='lucene')
        self.index_name=index_name
        self.analyzer=analyzer
        self.dir=os.path.join(settings.LUCENE_INDEX_DIR, self.index_name)
        self.new_index=wipe_index
        self.reader=None
        if not os.path.exists(self.dir):
            os.makedirs(self.dir, 0775)
            os.chmod(self.dir, 0775)
            self.new_index=True
        self.JCCEnvObj = lucene.getVMEnv() 
        self.JCCEnvObj.attachCurrentThread()
        self.store = lucene.SimpleFSDirectory(lucene.File(self.dir))
        self.text_blob=""

    def get_writer(self):
        try:
            self.writer = lucene.IndexWriter(self.store, 
                                             self.analyzer, 
                                             self.new_index,
                                             lucene.IndexWriter.MaxFieldLength.LIMITED)
            self.new_index=False
            return True
        except Exception, err:
            self.logger.error("%s: %s" % (Exception, err))
        return False
Ejemplo n.º 9
0
def update_dnspod_record(domain_id, record_id, name, type, line, value, ttl):
    values = {'login_email': config.DNSPOD_USER, 'login_password': config.DNSPOD_PW, 'format': 'json', 'domain_id': domain_id, 'record_id': record_id, 'sub_domain': name, 'record_type':type, 'record_line':line, 'value':value, 'ttl':ttl} 
    json_data = curl_dnspod(values, "/Record.Modify")
    Logger.action_log(str(json_data))
    if json_data['status']['code'] == "1":
        return ["true", json_data['record']['id']]
    else:
        return ["false", json_data['status']['message']]
Ejemplo n.º 10
0
def change_dnspod_record_status(domain_id, record_id, status):
    values = {'login_email': config.DNSPOD_USER, 'login_password': config.DNSPOD_PW, 'format': 'json', 'domain_id': domain_id, 'record_id': record_id, 'status': status} 
    json_data = curl_dnspod(values, "/Record.Status")
    Logger.action_log(str(json_data))
    if json_data['status']['code'] == "1":
        return ["true", ""]
    else:
        return ["false", json_data['status']['message']]
Ejemplo n.º 11
0
    def say(self, text):
        if not text:
            return

        h = {"Content-Type": "application/json; charset=UTF-8"}
        text_dict = {"text": text}
        payload = {"payload": json.dumps(text_dict)}

        r = requests.post(self.hook_url, params=payload, headers=h)
        Logger.log_msg_transfer("send msg to bearychat, response text: %s" % r.text)
Ejemplo n.º 12
0
 def down(self,url,savepath):
     try:
         urllib.urlretrieve(url, savepath)
         self.errortimes = 0
         return True
     except BaseException as e:
         Logger.error('down:%s , %s  %s'%(url,savepath,e))
         self.errortimes = self.errortimes + 1
         if self.errortimes >= self.errorlimit:
             return False;
         return self.down(url,savepath)
Ejemplo n.º 13
0
 def send_ping(self, ws):
     msg_id = 0
     while not self.exit_all:
         try:
             msg = '{"type":"ping","call_id":%d}' % msg_id
             Logger.log_bc_ws(">>> %s" % msg)
             ws.send(msg)
             msg_id += 1
             time.sleep(5)
         except Exception as exc:
             Logger.log("[send_ping]catch exception: %s" % str(exc))
             break
Ejemplo n.º 14
0
 def event_url(self, params):
     (url, irc) = params;
     Logger.info("RepostinatorPlugin: Got url '%s' from urlcatcher" % params[0])
     if irc.server.get_current_nick() == irc.message.destination:
         Logger.debug("Got url in priv ignoring")
         return
     key = "%s_%s_%s" % (irc.message.destination, irc.get_ircnet_name(), url)
     data = self.store.get(key);
     if(data):
         irc.reply('This url was first posted in this channel by %s @ %s' % (data[0], data[2]))
     else:
         data = [irc.message.source.nick,  irc.message.destination, time.ctime()]
         self.store.put(key, data);
Ejemplo n.º 15
0
    def start_server(self):
        api = BC_API()
        api.login()
        ws_url = api.get_ws_url()

        ws = create_connection(ws_url)
        Logger.log("connected to bc server")

        # send ping thread
        threading.Thread(target=self.send_ping, args=(ws, )).start()

        # loop worker
        self.server_loop(ws)
Ejemplo n.º 16
0
def f(*args):
    (_, node, cargs) = args
    log = Logger().log

    log.info('{0}: setup +'.format(node))
    with DatabaseConnection() as conn:
        source = nd.Node(node, conn)
        neighbors = [ nd.Node(x, conn) for x in source.neighbors ]
    log.info('{0}: setup -'.format(node))
    
    classes = [ WindowInfluence ] # [ MinuteInfluence, WindowInfluence ]
    
    return [ i(source, neighbors, cargs).run() for i in classes ]
Ejemplo n.º 17
0
 def postRequest(self,url,data):
     try:
         request=urllib2.Request(url)
         f =urllib2.urlopen(request,data,10000)
         response=f.read()
         self.errortimes = 0
         return response;
     except BaseException as e:
         Logger.error('postRequest:%s   %s'%(url,e))
         self.errortimes = self.errortimes + 1
         if self.errortimes >= self.errorlimit:
             return None;
         return self.postRequest(url,data)
Ejemplo n.º 18
0
    def _query(self, querystring, parameters = ()):
        conn  = sqlite3.connect(self.db)
        cursor = conn.cursor()

        try:
            cursor.execute(querystring, parameters)
            result = cursor.fetchall()
            conn.commit()

            return result
        except sqlite3.Error as e:
            Logger.warning("Query error (%s) %s: %s" % (querystring, parameters, e))
            return None
Ejemplo n.º 19
0
    def start(self):
        # Initialize data store
        DatastoreController().set_driver(self.config.get('datastore'))

        Logger.set_loglevel(self.config.get('log.level'))

        for plugin in self.config.get('coreplugins'):
            Logger.info("Loading core plugin '%s'" % plugin)
            self.plugincontroller.load_plugin(plugin, 'core')

        for plugin in self.config.get('plugins'):
            Logger.info("Loading plugin '%s'" % plugin)
            self.plugincontroller.load_plugin(plugin)

        if len(self.config.get('ircnets')) == 0:
            raise Exception("There has to be at least one ircnet to connect to")

        for net in self.config.get('ircnets'):
            irc = IRCController(self.eventcontroller)
            irc.set_configuration(net)

            Logger.info("Connecting to %s..." % irc.get_ircnet_name())
            irc.connect()

            self.ircnetscontroller.add_ircnet(irc.get_ircnet_name(), irc)
Ejemplo n.º 20
0
        def do_timeout(data):
            callback = data["callback"]
            try:
                callback(*data["args"], **data["kwargs"])
            except Exception as e:
                Logger.warning("Timer caught exception: %s" % e)

            # Free timer
            if self.moduleTimers.has_key(callback.im_self):
                for timer in self.moduleTimers[callback.im_self]:
                    if timer == threading.currentThread():
                        self.moduleTimers[callback.im_self].remove(timer)

                if len(self.moduleTimers[callback.im_self]) == 0:
                    del self.moduleTimers[callback.im_self]
Ejemplo n.º 21
0
    def _error(self, downloader, handle):
        gtk.gdk.threads_enter()

        # Terminate download process
        downloader.terminate()

        # Emit error signal
        self.emit("download-failed", handle)

        # Log error
        Logger.error("DownloadWizard", MSG_000005 % (handle.item.source))

        # Close dialog
        self.response(gtk.RESPONSE_REJECT)

        gtk.gdk.threads_leave()
Ejemplo n.º 22
0
    def reload(self):
        self.load_defaults()

        if sys.modules.has_key('config'):
            del sys.modules['config']

        try:
            mod = __import__("config")

            # Overwrite any defaults
            for key in mod.Bot.keys():
                self._config[key] = mod.Bot[key]

            del mod
        except ImportError as e:
            Logger.warning("No configuration found. Using defaults.")
Ejemplo n.º 23
0
    def __init__(self, hook=False):
        path = os.path.dirname(__file__)
        path = os.path.join(path, "screen.ui")

        self.logger = Logger()

        self.builder = gtk.Builder()
        self.builder.add_from_file(path)
        self.builder.connect_signals(self)

        self.screen = self.builder.get_object("screen_show")
        self.notebook = self.builder.get_object("notebook")
        self.statusbar = self.builder.get_object("statusbar")

        self.algorithm = None
        self.create_algorithms_menu()

        self.screen.connect('key_press_event', self.keyboard_press)
        self.screen.parent_screen = self

        self.notebook.set_scrollable(True)
        self.notebook.set_group_id(0)

        tab = None
        if not hook:
            tab = Graph(self.builder, self.tab_changed)
            self.add_notebook_tab(tab)

        self.screen.show_all()

        # HACK
        # For some reason the size of scroll only exist after show_all above
        if tab:
            tab.centralize_scroll()
Ejemplo n.º 24
0
    def load_plugin(self, name, search_dir = None):
        name = name.strip()
        import_name = None

        if self._loaded_plugins.has_key(name):
            raise PluginLoadError("Plugin is already loaded")

        import_name = self.find_plugin(name, search_dir)

        if not import_name:
            raise PluginLoadError("No such plugin")

        basename = import_name.rpartition(".")[2]

        try:
            mod = __import__(import_name)
            cls = getattr(mod, basename)
        except Exception as e:
            # Remove the system-entry
            if import_name and sys.modules.has_key(import_name):
                del sys.modules[import_name]

            Logger.log_traceback(self)
            raise PluginLoadError("Failed to load " + import_name)

        # Find the plugin entry point
        for objname in dir(cls):
            obj = getattr(cls, objname)
            if objname != 'Plugin' and type(obj) == types.ClassType and issubclass(obj, Plugin):
                Logger.debug("Plugin entry is '%s'" % objname)
                instance = new.instance(obj)

                # Initialize plugin instance
                instance.store  = DatastoreController().get_store(basename)
                instance.event  = self._eventcontroller
                instance.config = self._config
                instance.nets   = IRCNetsController()
                instance.plugin = self
                instance.__init__()

                self._loaded_plugins[basename.lower()] = (basename, instance, import_name)

                return True

        del sys.modules[import_name]
        raise PluginLoadError("Unable to find entry point")
Ejemplo n.º 25
0
    def __init__(self, _sockets, rooms=None):
        self.Sockets = _sockets
        self.pool = ThreadPool(processes=1)
        # self.process_list = Database.load_object(SharedScheduler.__filepath__, 'schedules') or []
        # self.l = Lock()

        #merging 'RoomLog' procedure and schedule objects
        from lib.logger import Logger
        self.pool.apply_async(self.process_main, (rooms, Logger.altLogger()))
Ejemplo n.º 26
0
    def __init__(self):
        """Initializes configuration"""
        self.logger = Logger()
        self.configfile = CONFIGFILE
        self.master_configfile = SYSTEM_CONFIGFILE

        if not self.__defaults_options:
            self.load()
            self.__defaults()
            self.__defaults_options = True
Ejemplo n.º 27
0
    def cmd_debug(self, irc, params):
        """
        Enable or disable debugging
            debug [1|2|3|off]
        """

        if len(params) == 0:
            irc.reply("Not enough parameters")
            return

        if params[0] == 'off':
            Logger.set_loglevel('FATAL')
            irc.reply("Debugging disabled")
        elif int(params[0]) in [1, 2, 3]:
            num_level = int(params[0])
            level = ["DEBUGL1", "DEBUGL2", "DEBUGL3"][num_level - 1]
            Logger.set_loglevel(level)
            irc.reply("Debugging enabled at level %d" % num_level)
        else:
            irc.reply("Invalid option")
Ejemplo n.º 28
0
    def send(self, data):
        params = data.split()
        if len(params) == 0:
            return

        cmd = params[0]

        if cmd == "JOIN":
            self.server_user_response("JOIN", ":" + params[1])
            self.server_user_response(ircdef.RPL_ENDOFNAMES, "%s :/End of /NAMES list." % params[1])
        elif cmd == "WHO":
            self.server_response(ircdef.RPL_WHOREPLY, "%s %s %s %s %s %s H :0 %s" % (
                self.bot_nick,
                params[1],
                self.bot_ident,
                self.bot_host,
                self.server_name,
                self.bot_nick,
                "The NeuBot"))

            self.server_response(ircdef.RPL_ENDOFWHO, "%s %s :End of /WHO list." % (self.bot_nick, params[1],))

        elif cmd == "PRIVMSG":
            match = re.match("PRIVMSG (.*?) :(.*)", data)
            channel, message = match.groups()

            print "\033[1;37m[%s:PRIVMSG %s]\033[0m %s" % (self.bot_nick, channel, message)

        elif cmd == "NOTICE":
            match = re.match("NOTICE (.*?) :(.*)", data)
            channel, message = match.groups()

            print "\033[1;35m<%s:NOTICE %s>\033[0m %s" % (self.bot_nick, channel, message)

        elif cmd == "PING":
            match = re.match("PING (.*)", data)
            self.server_response("PONG", match.group(1))

        else:
            Logger.debug("Unhandled: " + data.strip())
Ejemplo n.º 29
0
def add_record_api():
    tmp_dict = {}
    tmp_dict["success"] = "False"
    auth_result = auth_user()
    if not auth_result[0]:
        tmp_dict["msg"] = "no sso auth"
        return json.dumps(tmp_dict)
    else:
        userinfo = auth_result[1]
    if request.method == "POST":
        try:
            domain_id = request.form["domain_id"]
            domain_info = Domain.get_domain_by_domain_id(domain_id)
            name = request.form["name"]
            type = request.form["type"]
            line = request.form["line"]
            line = line.encode("utf-8")
            value = request.form["value"]
            ttl = request.form["ttl"]
            if if_user_id_have_domain_rights(userinfo["user_id"], domain_id):
                Logger.action_log(userinfo["username"] + " add record ")
                if domain_info[2] == "dnspod":
                    result = add_dnspod_record_lib(domain_id, name, type, line, value, ttl)
                    tmp_dict["success"] = result[0]
                    tmp_dict["msg"] = result[1]
                    return json.dumps(tmp_dict)
                elif domain_info[2] == "local_bind":
                    result = add_dns_record_lib(domain_id, name, ttl, type, value, line)
                    tmp_dict["success"] = result[0]
                    print result[1]
                    tmp_dict["msg"] = result[1]
                    return json.dumps(tmp_dict)
            else:
                tmp_dict["msg"] = "no this domain rights"
                return json.dumps(tmp_dict)
        except Exception, e:
            print e
            tmp_dict["msg"] = "unknown error"
            return json.dumps(tmp_dict)
Ejemplo n.º 30
0
 def __checking_live_thread(self):
     while True:
         time.sleep(20)
         Logger.log("checking server live...")
         if self.bc_server.connect_live:
             Logger.log("server is alive")
             self.bc_server.connect_live = False
         else:
             Logger.log("server is not alive, restart server...")
             self.__restart()
Ejemplo n.º 31
0
def main():

    _level = Level.INFO
    _log = Logger('main', _level)
    _loader = ConfigLoader(_level)
    _config = _loader.configure('config.yaml')

    try:
        _motors = Motors(_config, None, Level.WARN)
        _pot = Potentiometer(_config, Level.WARN)
        _pot.set_output_limits(0.0, 127.0) 

        # motor configuration: starboard, port or both?
        _orientation = Orientation.BOTH

        if _orientation == Orientation.BOTH or _orientation == Orientation.PORT:
            _port_motor = _motors.get_motor(Orientation.PORT)
            _port_pid = PIDController(_config, _port_motor, level=Level.WARN)
            _port_pid.enable()

        if _orientation == Orientation.BOTH or _orientation == Orientation.STBD:
            _stbd_motor = _motors.get_motor(Orientation.STBD)
            _stbd_pid = PIDController(_config, _stbd_motor, level=Level.WARN)
            _stbd_pid.enable()

        ROTATE = True
        _rotate = -1.0 if ROTATE else 1.0
     
#       sys.exit(0)
        _stbd_velocity = 0.0
        _port_velocity = 0.0

        _step =  0.5
        _min  =  0.0
        _max  = 70.0
        _rate = Rate(10)

        try:

#           for _value in numpy.arange(_min, _max, _step):
            while True:
                # update RGB LED
#               _pot.set_rgb(_pot.get_value())
                _value = 127.0 - _pot.get_scaled_value(True)
                if _value > 125.0:
                    _value = 127.0
                _velocity = Gamepad.convert_range(_value)
                if _orientation == Orientation.BOTH or _orientation == Orientation.PORT:
                    _port_pid.setpoint = _velocity * 100.0
                    _port_velocity = _port_pid.setpoint
                if _orientation == Orientation.BOTH or _orientation == Orientation.STBD:
                    _stbd_pid.setpoint = _rotate * _velocity * 100.0
                    _stbd_velocity = _stbd_pid.setpoint
                _log.info(Fore.WHITE + 'value: {:<5.2f}; set-point: {:5.2f}; velocity: '.format(_value, _velocity) \
                        + Fore.RED   + ' port: {:5.2f}\t'.format(_port_velocity) + Fore.GREEN + ' stbd: {:5.2f}'.format(_stbd_velocity))
                _rate.wait()

            _log.info(Fore.YELLOW + 'resting...')
            time.sleep(10.0)

#           for _value in numpy.arange(_min, _max, _step):
            while True:
                # update RGB LED
#               _pot.set_rgb(_pot.get_value())
                _value = 127.0 - _pot.get_scaled_value(True)
                if _value > 125.0:
                    _value = 127.0
                _velocity = Gamepad.convert_range(_value)
                if _orientation == Orientation.BOTH or _orientation == Orientation.PORT:
                    _port_pid.setpoint = _rotate * _velocity * 100.0
                    _port_velocity = _port_pid.setpoint
                if _orientation == Orientation.BOTH or _orientation == Orientation.STBD:
                    _stbd_pid.setpoint = _velocity * 100.0
                    _stbd_velocity = _stbd_pid.setpoint
                _log.info(Fore.MAGENTA + 'value: {:<5.2f}; set-point: {:5.2f}; velocity: '.format(_value, _velocity) \
                        + Fore.RED   + ' port: {:5.2f}\t'.format(_port_velocity) + Fore.GREEN + ' stbd: {:5.2f}'.format(_stbd_velocity))
                _rate.wait()

            _log.info(Fore.YELLOW + 'end of cruising.')

        except KeyboardInterrupt:
            _log.info(Fore.CYAN + Style.BRIGHT + 'PID test complete.')
        finally:
            if _orientation == Orientation.BOTH or _orientation == Orientation.PORT:
                _port_pid.disable()
            if _orientation == Orientation.BOTH or _orientation == Orientation.STBD:
                _stbd_pid.disable()
            _motors.brake()

    except Exception as e:
        _log.info(Fore.RED + Style.BRIGHT + 'error in PID controller: {}\n{}'.format(e, traceback.format_exc()))

    finally:
        _log.info(Fore.YELLOW + Style.BRIGHT + 'C. finally.')
Ejemplo n.º 32
0
def test_moth():
    '''
    Test the basic functionality of the Moth sensor's various outputs.
    The test is entirely visual, i.e., something to be looked at, tested
    with a bright light source.
    '''
    _log = Logger("test", Level.INFO)
    # read YAML configuration
    _loader = ConfigLoader(Level.INFO)
    filename = 'config.yaml'
    _config = _loader.configure(filename)
    _moth = Moth(_config, None, Level.INFO)

    _rgbmatrix = RgbMatrix(Level.INFO)
    _rgbmatrix.set_display_type(DisplayType.SOLID)

#   for i in range(50):
    while True:
        # orientation and bias .............................
        _orientation = _moth.get_orientation()
        _bias = _moth.get_bias()
        _log.debug('bias: {}; orientation: {}'.format(type(_bias), _orientation))
        if _orientation is Orientation.PORT:
            _rgbmatrix.show_color(Color.BLACK, Orientation.STBD)
            _rgbmatrix.show_color(Color.RED, Orientation.PORT)
            _log.info(Fore.RED    + Style.BRIGHT + '{}\t {:<6.3f}'.format(_orientation.name, _bias[0]))
        elif _orientation is Orientation.STBD:
            _rgbmatrix.show_color(Color.BLACK, Orientation.PORT)
            _rgbmatrix.show_color(Color.GREEN, Orientation.STBD)
            _log.info(Fore.GREEN  + Style.BRIGHT + '{}\t {:<6.3f}'.format(_orientation.name, _bias[1]))
        else:
            _rgbmatrix.show_color(Color.YELLOW, Orientation.PORT)
            _rgbmatrix.show_color(Color.YELLOW, Orientation.STBD)
            _log.info(Fore.YELLOW + '{}\t {:6.3f}|{:6.3f}'.format(_orientation.name, _bias[0], _bias[1]))
        # int values .......................................
        _int_values = _moth.get_int_values()
        _log.info(Fore.RED   + '{:d}\t'.format(_int_values[0]) + Fore.GREEN + '{:d}\t'.format(_int_values[1]) )
        # float values .....................................
        _values = _moth.get_values()
        _log.info(Fore.RED   + '{:<6.3f}\t'.format(_values[0]) + Fore.GREEN + '{:<6.3f}\t'.format(_values[1]) )
#       time.sleep(1.0)
        time.sleep(0.1)
Ejemplo n.º 33
0
class NXP9DoF:
    '''
        Reads from an Adafruit NXP 9-DoF FXOS8700 + FXAS21002 sensor. 

        Note that this has been calibrated based on the orientation of the
        NXP9DoF board as installed on the KR01 robot, with the Adafruit
        logo on the chip (mounted horizontally) as follows:

                                    Fore
                              _---------------
                              |           *  |
                              |              |
                              |              |
                       Port   |              | Starboard
                              |              |
                              ----------------
                                    Aft

        If you mount the chip in a different orientation you will likely
        need to multiply one or more of the axis by -1.0.

        Depending on where you are on the Earth, true North changes because
        the Earth is not a perfect sphere. You can get the declination angle
        for your location from:

            http://www.magnetic-declination.com/

        E.g., for Pukerua Bay, New Zealand:

            Latitude:  41° 1' 57.7" S
            Longitude: 174° 53' 11.8" E
            PUKERUA BAY
            Magnetic Declination: +22° 37'
            Declination is POSITIVE (EAST)
            Inclination: 66° 14'
            Magnetic field strength: 55716.5 nT

        E.g., for Wellington, New Zealand:

            Latitude:  41° 17' 11.9" S
            Longitude: 174° 46' 32.1" E
            KELBURN
            Magnetic Declination: +22° 47'
            Declination is POSITIVE (EAST)
            Inclination: 66° 28'
            Magnetic field strength: 55863.3 nT
    '''

    # permitted error between Euler and Quaternion (in degrees) to allow setting value
    ERROR_RANGE = 5.0  # was 3.0

    # ..........................................................................
    def __init__(self, config, queue, level):
        self._log = Logger("nxp9dof", level)
        if config is None:
            raise ValueError("no configuration provided.")
        self._config = config
        self._queue = queue

        _config = self._config['ros'].get('nxp9dof')
        self._quaternion_accept = _config.get(
            'quaternion_accept')  # permit Quaternion once calibrated
        self._loop_delay_sec = _config.get('loop_delay_sec')

        # verbose will print some start-up info on the IMU sensors
        self._imu = IMU(gs=4, dps=2000, verbose=True)
        # setting a bias correction for the accel only; the mags/gyros are not getting a bias correction
        self._imu.setBias((0.0, 0.0, 0.0), None, None)
        #       self._imu.setBias((0.1,-0.02,.25), None, None)

        _i2c = busio.I2C(board.SCL, board.SDA)
        self._fxos = adafruit_fxos8700.FXOS8700(_i2c)
        self._log.info('accelerometer and magnetometer ready.')
        self._fxas = adafruit_fxas21002c.FXAS21002C(_i2c)
        self._log.info('gyroscope ready.')

        self._thread = None
        self._enabled = False
        self._closed = False
        self._heading = None
        self._pitch = None
        self._roll = None
        self._is_calibrated = False
        self._log.info('ready.')

    # ..........................................................................
    def get_imu(self):
        return self._imu

    # ..........................................................................
    def get_fxos(self):
        return self._fxos

    # ..........................................................................
    def get_fxas(self):
        return self._fxas

    # ..........................................................................
    def enable(self):
        if not self._closed:
            self._enabled = True
            # if we haven't started the thread yet, do so now...
            if self._thread is None:
                self._thread = threading.Thread(target=NXP9DoF._read,
                                                args=[self])
                self._thread.start()
            self._log.info('enabled.')
        else:
            self._log.warning('cannot enable: already closed.')

    # ..........................................................................
    def get_heading(self):
        return self._heading

    # ..........................................................................
    def get_pitch(self):
        return self._pitch

    # ..........................................................................
    def get_roll(self):
        return self._roll

    # ..........................................................................
    def is_calibrated(self):
        return self._is_calibrated

    # ..........................................................................
    @staticmethod
    def _in_range(p, q):
        return p >= (q - NXP9DoF.ERROR_RANGE) and p <= (q +
                                                        NXP9DoF.ERROR_RANGE)

    # ..........................................................................
    def imu_enable(self):
        '''
            Enables the handbuilt IMU on a 20Hz loop.
        '''
        if not self._closed:
            self._enabled = True
            # if we haven't started the thread yet, do so now...
            if self._thread is None:
                self._thread = threading.Thread(target=NXP9DoF._handbuilt_imu,
                                                args=[self])
                self._thread.start()
            self._log.info('enabled.')
        else:
            self._log.warning('cannot enable: already closed.')

    # ..........................................................................
    def _handbuilt_imu(self):
        '''
            The function that reads sensor values in a loop. This checks to see
            if the 'sys' calibration is at least 3 (True), and if the Euler and
            Quaternion values are within an error range of each other, this sets
            the class variable for heading, pitch and roll. If they aren't within
            range for more than n polls, the values revert to None.
        '''
        self._log.info('starting sensor read...')
        _heading = None
        _pitch = None
        _roll = None
        _quat_w = None
        _quat_x = None
        _quat_y = None
        _quat_z = None
        count = 0
        #               2. Absolute Orientation (Quaterion, 100Hz) Four point quaternion output for more accurate data manipulation ................

        # grab data at 20Hz
        _rate = Rate(1)

        header = 90
        print('-' * header)
        #       print("| {:17} | {:20} |".format("Accels [g's]", " IMU Accels"))
        print("| {:17} |".format("Mag [xyz]"))

        while not self._closed:
            if self._enabled:

                accel_x, accel_y, accel_z = self._fxos.accelerometer  # m/s^2
                gyro_x, gyro_y, gyro_z = self._fxas.gyroscope  # radians/s
                a, m, g = self._imu.get()
                mag_x, mag_y, mag_z = self._fxos.magnetometer  # uTesla

                mag_x_g = m[0] * 100.0
                mag_y_g = m[1] * 100.0
                mag_z_g = m[2] * 100.0

                #               rate_gyro_x_dps = Convert.rps_to_dps(gyro_x)
                #               rate_gyro_y_dps = Convert.rps_to_dps(gyro_y)
                #               rate_gyro_z_dps = Convert.rps_to_dps(gyro_z)

                #               mag_x_g = mag_x * 100.0
                #               mag_y_g = mag_y * 100.0
                #               mag_z_g = mag_z * 100.0

                heading = 180.0 * math.atan2(mag_y_g, mag_x_g) / math.pi

                print(Fore.CYAN + '| x{:>6.3f} y{:>6.3f} z{:>6.3f} |'.format( mag_x_g, mag_y_g, mag_z_g) \
                        + Style.BRIGHT + '\t{:>5.2f}'.format(heading) + Style.RESET_ALL)

                #               a, m, g = self._imu.get()

                #               print(Fore.CYAN + '| {:>6.3f} {:>6.3f} {:>6.3f} | {:>6.3f} {:>6.3f} {:>6.3f} |'.format( accel_x, accel_y, accel_z, a[0], a[1], a[2]) + Style.RESET_ALL)

                _rate.wait()

#                   + Style.BRIGHT + ' {:>6.1f} {:>6.1f} {:>6.1f}° |'.format(r, p, deg) + Style.RESET_ALL)
#                   time.sleep(0.50)

#               mag_x, mag_y, mag_z       = self._fxos.magnetometer # uTesla
#               gyro_x, gyro_y, gyro_z    = self._fxas.gyroscope # radians/s

#               header = 90
#               print('-'*header)
#               print("| {:17} | {:20} | {:20} | {:20} |".format("Accels [g's]", " Magnet [uT]", "Gyros [dps]", "Roll, Pitch, Heading"))
#               print('-'*header)
#               for _ in range(10):
#                   a, m, g = self._imu.get()
#                   r, p, h = self._imu.getOrientation(a, m)
#                   deg = Convert.to_degrees(h)
#                   print(Fore.CYAN + '| {:>5.2f} {:>5.2f} {:>5.2f} | {:>6.1f} {:>6.1f} {:>6.1f} | {:>6.1f} {:>6.1f} {:>6.1f} |'.format(
#                       a[0], a[1], a[2],
#                       m[0], m[1], m[2],
#                       g[0], g[1], g[2])
#                   + Style.BRIGHT + ' {:>6.1f} {:>6.1f} {:>6.1f}° |'.format(r, p, deg) + Style.RESET_ALL)
#                   time.sleep(0.50)
#               print('-'*header)
#               print(' uT: micro Tesla')
#               print('  g: gravity')
#               print('dps: degrees per second')
#               print('')

#
#               print(Fore.MAGENTA + 'acc: ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(accel_x, accel_y, accel_z)\
#                   + Fore.YELLOW  + ' \tmag: ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(mag_x, mag_y, mag_z)\
#                   + Fore.CYAN    + ' \tgyr: ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(gyro_x, gyro_y, gyro_z) + Style.RESET_ALL)

            else:
                self._log.info('disabled loop.')
                time.sleep(10)
            count += 1

        self._log.debug('read loop ended.')

    # ..........................................................................
    def _read(self):
        '''
            The function that reads sensor values in a loop. This checks to see
            if the 'sys' calibration is at least 3 (True), and if the Euler and
            Quaternion values are within an error range of each other, this sets
            the class variable for heading, pitch and roll. If they aren't within
            range for more than n polls, the values revert to None.
        '''
        self._log.info('starting sensor read...')
        _heading = None
        _pitch = None
        _roll = None
        _quat_w = None
        _quat_x = None
        _quat_y = None
        _quat_z = None
        count = 0
        #               2. Absolute Orientation (Quaterion, 100Hz) Four point quaternion output for more accurate data manipulation ................

        # grab data at 10Hz
        rate = Rate(10)

        while not self._closed:
            if self._enabled:

                header = 91
                print(Fore.CYAN + ('-' * header) + Style.RESET_ALL)
                print(Fore.CYAN + "| {:17} | {:20} | {:20} | {:21} |".format(
                    "Accels [g's]", " Magnet [uT]", "Gyros [dps]",
                    "Roll, Pitch, Heading") + Style.RESET_ALL)
                print(Fore.CYAN + ('-' * header) + Style.RESET_ALL)
                for _ in range(10):
                    a, m, g = self._imu.get()
                    r, p, h = self._imu.getOrientation(a, m)
                    deg = Convert.to_degrees(h)
                    #                   self._log.info(Fore.GREEN + '| {:>6.1f} {:>6.1f} {:>6.1f} | {:>6.1f} {:>6.1f} {:>6.1f} |'.format(a[0], a[1], a[2], r, p, h) + Style.RESET_ALL)
                    print(Fore.CYAN + '| {:>5.2f} {:>5.2f} {:>5.2f} '.format(
                        a[0], a[1], a[2]) + Fore.YELLOW +
                          '| {:>6.1f} {:>6.1f} {:>6.1f} '.format(
                              m[0], m[1], m[2]) + Fore.MAGENTA +
                          '| {:>6.1f} {:>6.1f} {:>6.1f} '.format(
                              g[0], g[1], g[2]) + Fore.GREEN +
                          '| {:>6.1f} {:>6.1f} {:>6.1f}° |'.format(r, p, deg) +
                          Style.RESET_ALL)
                    time.sleep(0.50)
                print('-' * header)
                print(' uT: micro Tesla')
                print('  g: gravity')
                print('dps: degrees per second')
                print('')

                #               accel_x, accel_y, accel_z = self._fxos.accelerometer # m/s^2
                #               mag_x, mag_y, mag_z       = self._fxos.magnetometer # uTesla
                #               gyro_x, gyro_y, gyro_z    = self._fxas.gyroscope # radians/s
                #
                #               print(Fore.MAGENTA + 'acc: ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(accel_x, accel_y, accel_z)\
                #                   + Fore.YELLOW  + ' \tmag: ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(mag_x, mag_y, mag_z)\
                #                   + Fore.CYAN    + ' \tgyr: ({0:0.3f}, {1:0.3f}, {2:0.3f})'.format(gyro_x, gyro_y, gyro_z) + Style.RESET_ALL)

                rate.wait()
            else:
                self._log.info('disabled loop.')
                time.sleep(10)
            count += 1

        self._log.debug('read loop ended.')

    # ..........................................................................
    def disable(self):
        self._enabled = False
        self._log.info('disabled.')

    # ..........................................................................
    def close(self):
        self._closed = True
        self._log.info('closed.')
def main(argument_list):

    DEFAULT_CONFIG_PATH = const.CONFIG_PATH
    DEFAULT_CONFIG_SECTION = const.CONFIG_SECTION

    try:
        parser = ArgumentParser(add_help=False)

        parser.add_argument('--config-file')
        parser.add_argument('--config-section')
        parser.add_argument('--cluster-name')
        parser.add_argument('--backup-directory')
        parser.add_argument('--show-details', action='store_true')

        parser.add_argument('options', nargs='+')

        #args = vars(parser.parse_args(argument_list[1:]))
        known, unknown = parser.parse_known_args(argument_list[1:])
        args = vars(known)

        # get config file path and config section name
        restore_config_file = DEFAULT_CONFIG_PATH
        restore_config_section = DEFAULT_CONFIG_SECTION

        if args['config_file'] is not None:
            restore_config_file = args['config_file']
        if args['config_section'] is not None:
            restore_config_section = args['config_section']

        # create config obj and read config file data
        cfg = Config(restore_config_file, restore_config_section)
        if not cfg.is_valid():
            print("Error, fail to initialize config.")
            sys.exit(2)
        if not cfg.set_options(print_options=False):
            print("Error, fail to set config.")
            sys.exit(2)

        # get backup directory path and cluster name
        backup_directory = cfg.backup_destination_path
        cluster_name = cfg.ceph_cluster_name
        if args['backup_directory'] is not None:
            backup_directory = args['backup_directory']
        if args['cluster_name'] is not None:
            cluster_name = args['cluster_name']

        show_details = False
        if args['show_details']:
            show_details = True

        opts = args['options']

        # initial backup logging
        log = Logger(cfg.log_file_path,
                     cfg.log_level,
                     cfg.log_max_bytes,
                     cfg.log_backup_count,
                     cfg.log_delay,
                     name=const.LOG_SHOW_LOGGER_NAME)
        if not log.set_log(log_module=cfg.log_module_name):
            print("Error, unable to set logger.")
            sys.exit(2)

        if opts[0] != 'config':
            rbd_restore_list = RBD_Restore_List(log)
            backup_cluster_directory = os.path.join(backup_directory,
                                                    cluster_name)
            if not rbd_restore_list.read_meta(backup_cluster_directory):
                print("Error, unable to get cluster info.")
                sys.exit(2)

        directory = Directory(log)

        print("")
        if opts[0] == 'backup':

            if len(opts) == 1:
                print("*Show all backup name.\n")
                backup_name_list = rbd_restore_list.get_backup_name_list()
                if show_details:
                    print("[Backup Name]       [RBD Count]")
                    print("-" * 31)
                else:
                    print("[Backup Name List]")
                    print("-" * 20)

                for backup_name in backup_name_list:
                    if show_details:
                        rbd_list = rbd_restore_list.get_backup_rbd_list(
                            backup_name)

                        print("%s %s" % (backup_name, len(rbd_list)))
                    else:
                        print("%s" % backup_name)

            elif len(opts) >= 2:
                backup_name = normalize_datetime(' '.join(opts[1:]))
                rbd_list = rbd_restore_list.get_backup_rbd_list(backup_name)

                print("*Show RBD list in backup name '%s'.\n" % backup_name)
                if show_details:
                    print("[Backup Time]       [Circle name]       " \
                          "[Pool name/RBD name]  ... [Status]")
                    print("-" * 74)
                else:
                    print("[Backup RBD List]")
                    print("-" * 20)

                for rbd_info in rbd_list:
                    if show_details:
                        if rbd_info[0] == 0:
                            status = 'OK'

                        else:
                            status = 'FAIL'
                        if status == 'OK':
                            exist = directory.exist(backup_cluster_directory,
                                                    rbd_info[1], rbd_info[2],
                                                    rbd_info[3])
                            if exist == False:
                                status = 'Deleted'

                        print("%s %s %s/%s ... %s" %
                              (rbd_info[4], rbd_info[3], rbd_info[1],
                               rbd_info[2], status))
                    else:
                        print("%s/%s" % (rbd_info[1], rbd_info[2]))

        elif opts[0] == 'rbd':
            if len(opts) == 1:
                print("*Show all backuped RBD name.\n")
                rbd_list = rbd_restore_list.get_backup_rbd_list()
                if len(rbd_list) == 0:
                    return 0

                arranged_rbd_info = {}

                for rbd_info in rbd_list:
                    pool_name = rbd_info[0]
                    rbd_name = rbd_info[1]
                    info = rbd_info[2]

                    if not arranged_rbd_info.has_key(pool_name):
                        rbd_info_list = []
                    else:
                        rbd_info_list = arranged_rbd_info[pool_name]
                    info['name'] = rbd_name
                    rbd_info_list.append(info)
                    arranged_rbd_info[pool_name] = rbd_info_list

                if show_details:
                    print("[Pool name]")
                    print(
                        "  [RBD name] [block name prefix] [Num objects] [size (bytes)]"
                    )
                    print("-" * 74)
                else:
                    print("[Pool name]")
                    print("  [RBD name]")
                    print("-" * 20)
                for pool_name, rbd_list in arranged_rbd_info.iteritems():
                    print("%s" % pool_name)
                    if show_details:
                        for rbd_info in rbd_list:
                            rbd_name = rbd_info['name']
                            rbd_size = rbd_info['size']
                            rbd_objs = rbd_info['num_objs']
                            rbd_prix = rbd_info['block_name_prefix']
                            print("  %s %s %s %s" %
                                  (rbd_name, rbd_prix, rbd_objs, rbd_size))
                    else:
                        for rbd_info in rbd_list:
                            print("  %s" % rbd_info['name'])

            if len(opts) == 3:
                pool_name = opts[1]
                rbd_name = opts[2]

                print("*Show backup time of RBD '%s/%s'.\n" %
                      (pool_name, rbd_name))

                if show_details:
                    backup_info_list = rbd_restore_list.get_rbd_backup_info_list(
                        pool_name, rbd_name)
                    if len(backup_info_list) == 0:
                        return 0
                    print("[Backup time]       [Backup name]       " \
                          "[Backup circle]     [Backup size]")
                    print("-" * 74)
                    for backup_info in backup_info_list:
                        backup_file = backup_info[0]
                        backup_time = backup_info[1]
                        backup_name = backup_info[2]
                        backup_circ = backup_info[3]
                        backup_size = directory.get_used_size(
                            backup_cluster_directory, pool_name, rbd_name,
                            backup_circ, backup_file)
                        print("%s %s %s %s" % (backup_time, backup_name,
                                               backup_circ, backup_size))
                else:
                    backup_time_list = rbd_restore_list.get_rbd_backup_time_list(
                        pool_name, rbd_name)
                    if len(backup_time_list) == 0:
                        return 0

                    print("[Backup time]")
                    print("-" * 20)
                    for backup_time in backup_time_list:
                        print("%s" % backup_time)

        elif opts[0] == 'cluster':
            print("*Show backuped cluster info.\n")
            cluster_info = rbd_restore_list.get_cluster_info()
            for key, value in cluster_info.iteritems():
                print("%s: %s" % (key, value))

        elif opts[0] == 'config':
            if len(opts) == 1:
                print("*Show backup config.\n")

                cfg_opts = cfg.get_option()
                for key, value in cfg_opts.iteritems():
                    print("%s = %s" % (key, value))

            elif len(opts) == 2:
                if opts[1] == 'openstack':
                    print("*Show openstack yaml. (constructing)\n")

            elif len(opts) == 3:
                if opts[1] == 'rbd' and opts[2] == 'list':
                    print("*Show RBD backup list.")
                    print("*Yaml file: %s" % cfg.backup_list_file_path)
                    print("*Cluster name: %s" % cluster_name)
                    print("")

                    rbd_backup_list = RBD_Backup_List(log)
                    rbd_backup_list.read_yaml(cfg.backup_list_file_path)
                    rbd_name_list = rbd_backup_list.get_rbd_name_list(
                        cluster_name)

                    if show_details:
                        print("[Pool name]")
                        print(
                            "  [RBD name] [backup_type] [max_incr] [max_circ] [max_snap]"
                        )
                        print("-" * 74)
                        backup_option = RBD_Backup_Option(log)
                        for pool_name, rbd_name_list in rbd_name_list.iteritems(
                        ):
                            print("%s" % pool_name)
                            for rbd_name in rbd_name_list:
                                options = rbd_backup_list.get_rbd_options(
                                    cluster_name, pool_name, rbd_name)
                                backup_option.add_option(
                                    pool_name, rbd_name, options)
                                backup_type = backup_option.get_backup_type(
                                    pool_name, rbd_name)
                                max_incr = backup_option.get_backup_max_incr_count(
                                    pool_name, rbd_name)
                                max_circ = backup_option.get_backup_circle_retain_count(
                                    pool_name, rbd_name)
                                max_snap = backup_option.get_snapshot_retain_count(
                                    pool_name, rbd_name)
                                print("  %s %s %s %s %s" %
                                      (rbd_name, backup_type, max_incr,
                                       max_circ, max_snap))
                    else:
                        print("[Pool name]")
                        print("  [RBD name]")
                        print("-" * 24)
                        for pool_name, rbd_name_list in rbd_name_list.iteritems(
                        ):
                            print("%s" % pool_name)
                            for rbd_name in rbd_name_list:
                                print("  %s" % rbd_name)

        print("")
    except Exception as e:

        exc_type, exc_value, exc_traceback = sys.exc_info()
        traceback.print_exception(exc_type,
                                  exc_value,
                                  exc_traceback,
                                  file=sys.stdout)

        sys.exit(2)
Ejemplo n.º 35
0
class MotionDetector():
    '''
    Uses an infrared PIR sensor to scan for cats, or other causes of motion.
    '''
    def __init__(self, config, message_factory, message_bus, level):
        self._log = Logger('motion-detect', Level.INFO)
        if config is None:
            raise ValueError('no configuration provided.')
        self._message_factory = message_factory
        self._message_bus = message_bus
        _config = config['ros'].get('motion_detect')
        _pin    = _config.get('pin')

        _threshold_value = 0.1 # the value above which the device will be considered “on”
#       set up pin where PIR is connected
        self._sensor = MotionSensor(_pin, threshold=_threshold_value, pull_up=False)
        self._sensor.when_motion    = self._activated
        self._sensor.when_no_motion = self._deactivated
        self._disabling = False
        self._enabled = False
        self._closed = False
        # arm behaviour
        self._arm_movement_degree_step = 5.0
        self._arm_up_delay             = 0.09
        self._arm_down_delay           = 0.04
        self._log.info('ready.')

    # ..........................................................................
    def _activated(self):
        '''
        The default function called when the sensor is activated.
        '''
        if self._enabled:
            self._log.info(Fore.YELLOW + 'detected motion!')
            self._message_bus.handle(self._message_factory.get_message(Event.MOTION_DETECT, True))
        else:
            self._log.info('motion detector not enabled.')

    # ..........................................................................
    def _deactivated(self):
        '''
        The default function called when the sensor is deactivated.
        '''
        if self._enabled:
            self._log.info('deactivated motion detector.')
        else:
            self._log.debug('motion detector not enabled.')

    # ..........................................................................
    def enable(self):
        self._log.debug('enabling...')
        if self._closed:
            self._log.warning('cannot enable: closed.')
            return
        self._enabled = True
        self._log.debug('enabled.')

    # ..........................................................................
    def disable(self):
        if self._disabling:
            self._log.warning('already disabling.')
        else:
            self._disabling = True
            self._enabled = False
            self._log.debug('disabling...')
            self._disabling = False
            self._log.debug('disabled.')

    # ..........................................................................
    def close(self):
        self.disable()
        self._closed = True
# Self-Test file that makes sure all
# off the station identifiers are OK.

import logging
import time

from configuration import configuration
from data_sources import weather
from lib.logger import Logger
from safe_logging import safe_log, safe_log_warning

python_logger = logging.getLogger("check_config_files")
python_logger.setLevel(logging.DEBUG)
LOGGER = Logger(python_logger)


def terminal_error(error_message):
    safe_log_warning(LOGGER, error_message)
    exit(0)


try:
    airport_render_config = configuration.get_airport_configs()
except Exception as e:
    terminal_error(
        'Unable to fetch the airport configuration. Please check the JSON files. Error={}'
        .format(e))

if len(airport_render_config) == 0:
    terminal_error('No airports found in the configuration file.')
                'METHOD': inspect.stack()[0][3],
                'EXCEPTION': str(e)
            }
            self.logger.exception(message)
            raise


if __name__ == '__main__':
    if len(sys.argv) > 5:
        log_level = sys.argv[1]
        wait_time = sys.argv[2]
        manifest_file_path = sys.argv[3]
        sm_arn_scp = sys.argv[4]
        staging_bucket = sys.argv[5]

        logger = Logger(loglevel=log_level)
        scp_run = LaunchSCP(logger, wait_time, manifest_file_path, sm_arn_scp,
                            staging_bucket)
        scp_run.trigger_service_control_policy_state_machine()
        print("sm executed!")
        status, failed_execution_list = scp_run.monitor_state_machines_execution_status(
        )
        error = " Service Control Policy State Machine Execution(s) Failed. Navigate to the AWS Step Functions console and" \
                " review the following State Machine Executions. ARN List: {}".format(failed_execution_list)

        if status == 'FAILED':
            logger.error(100 * '*')
            logger.error(error)
            logger.error(100 * '*')
            sys.exit(1)
Ejemplo n.º 38
0
        )
    except AssertionError:
        print("Exported results not found, trying to use log results instead.")
        args.READ_EXPORT = False

if not args.READ_EXPORT:
    assert os.path.isfile(
        args.SOLUTION), f'SOLUTION_FILE "{args.SOLUTION}" not found'
    assert os.path.isfile(
        args.HISTORY), f'HISTORY_FILE "{args.HISTORY}" not found'
    assert os.path.isfile(
        args.MDIDS), f'MDIDS results file "{args.MDIDS}" not found'

if args.save: os.makedirs(args.EXPORT, exist_ok=True)
# Logger prints to sys.stdout and also logs the print to the log file.  Prevents duplicate code.
printlog = Logger(log_file=args.EXPORT + 'solution.txt', status=args.save)
# Debugger helps to debug the code py printing extra information and pausing the code for checks
debug = Debugger(args.DEBUG)

# history: [bbe1, bbe2, bbe3...]
# where bbe1: [#bbe, input1, ..., output1, output1_target, output1_err_abs, output1_err_rel, output1_err_weight, ..., obj, cstr1, cstr2, ...]
history = []
if args.READ_EXPORT:
    # Open the exported results and populate `history` with their data
    history_csv = csv_read(args.EXPORT + 'history.csv',
                           delimiters=args.DELIMITER)
    header = history_csv[0]
    history = [list(map(safe_float, h)) for h in history_csv[1:]]
    # Find the index of the output variables in the header
    output_idx = [idx - 2 for idx, s in enumerate(header) if '_err_abs' in s]
    # Save the input and constraints names
Ejemplo n.º 39
0
from lib.angular.core import Service
from lib.angular.core import JSDict
from lib.events import EventMixin

from lib.logger import Logger
logger = Logger(__name__)


class Task(EventMixin):
    _NEXT_TASK_ID = 0

    def __init__(self, name='', description='', clear_on_done=True):
        super(Task, self).__init__()
        self.id = Task._NEXT_TASK_ID
        Task._NEXT_TASK_ID += 1
        self.name = name
        self.description = description

    def set_progress(self, progress):
        self.progress = progress
        if self.progress == 100:
            self.emit('done', self)


class ProgressService(Service):
    def __init__(self):
        super(ProgressService, self).__init__()
        self.tasks = {}
        self.empty_task = Task()
        self.main_task = self.empty_task
Ejemplo n.º 40
0
    default=1.0,
    required=False,
    help="Segment MD propagation time tau used in hdWE simulations.")
parser.add_argument(
    '-p',
    '--plot',
    dest="plot",
    required=False,
    default=False,
    action="store_true",
    help="Plot probabilities from start state per bin and iteration")
# Initialize
args = parser.parse_args()
first_iteration = 0
last_iteration = args.last_iteration
logger = Logger(args.logdir)
if last_iteration < 0:
    last_iteration = logger.getLastIterationId()
# get the bin Ids of start state and target state
start_state = args.start_state
target_state = args.target_state
last_iteration_tmp = logger.loadIteration(last_iteration)
found_target_state = False
for this_bin in last_iteration_tmp.bins:
    if all(this_bin.getCoordinateIds() == start_state):
        start_state_id = this_bin.getId()
    if all(this_bin.getCoordinateIds() == target_state):
        target_state_id = this_bin.getId()
        found_target_state = True
if found_target_state == False:
    target_state_id = 1e99
Ejemplo n.º 41
0
def hyperloop():

    imageFolder = None
    imageNum = 0

    logger = Logger('relog')
    logger.setLogLevel('debug')
    logger.info('Started replay')

    state = State()
    for p in sys.argv:
        if (os.path.isdir(p)):
            imageFolder = p
        elif (p.isdigit()):
            imageNum = int(p)
        elif (p == "-lap"):
            state.Approaching = Signal.LAP
        elif (p == "-up"):
            state.Approaching = Signal.UPPER
        elif (p == "-lo"):
            state.Approaching = Signal.LOWER
        elif (p == "-s"):
            state.Approaching = Signal.UPPER

    if (state.Approaching != Signal.LAP):
        state.setStopSignal(1)

    camera = Camera(None, True)

    if imageFolder:
        # program loop
        files = sorted_aphanumeric(os.listdir(imageFolder))
        while True:
            try:
                file = os.path.join(imageFolder, files[imageNum])
                logger.info("[" + str(imageNum) + "] Loaded file: " + file)
                image = cv2.imread(file, 1)

                camera.capture(image)

                key = cv2.waitKey(0) & 0xFF

                if key == ord("n"):
                    # cv2.destroyAllWindows()
                    if (imageNum + 1 < len(files)):
                        imageNum += 1
                elif key == ord("b"):
                    # cv2.destroyAllWindows()
                    if (imageNum - 1 >= 0):
                        imageNum -= 1
                elif key == ord('q'):
                    break

            except KeyboardInterrupt:
                break
            except Exception as e:
                logger.logError(e)
                traceback.print_exc(limit=3, file=sys.stdout)

    logger.info('Stopped')
Ejemplo n.º 42
0
def test_matrix():

    _log = Logger('matrix-test', Level.INFO)
    _matrices = None

    try:

        _log.info(Fore.CYAN + 'start matrices test...')

        _matrices = Matrices(Level.INFO)

        _log.info('matrix write text...')
        _matrices.text('HE', 'LP')
        time.sleep(3)
        _matrices.clear()
        time.sleep(1)

        _log.info('matrix on...')
        _matrices.on()
        time.sleep(2)

        _log.info('matrix off...')
        _matrices.clear()
        time.sleep(1)

        _log.info('manual gradient wipes...')
        for i in range(1, 8):
            _matrices.vertical_gradient(i)
            time.sleep(0.02)
        for i in range(7, -1, -1):
            _matrices.vertical_gradient(i)
            time.sleep(0.02)
        time.sleep(1)
        for i in range(1, 11):
            _matrices.horizontal_gradient(i)
            time.sleep(0.02)
        for i in range(11, -1, -1):
            _matrices.horizontal_gradient(i)
            time.sleep(0.02)
        time.sleep(1)

        _log.info('starting matrix vertical wipe...')
        _matrices.wipe(Matrices.DOWN, True, 0.00)
        time.sleep(0.0)
        _matrices.wipe(Matrices.DOWN, False, 0.00)
        _matrices.clear()
        time.sleep(1)

        _log.info('starting matrix horizontal wipe right...')
        _matrices.wipe(Matrices.RIGHT, True, 0.00)
        time.sleep(0.0)
        _matrices.wipe(Matrices.RIGHT, False, 0.00)
        _matrices.clear()
        # UP and LEFT not implemented

    except KeyboardInterrupt:
        _log.info(Fore.MAGENTA + 'Ctrl-C caught: interrupted.')
    finally:
        _log.info('closing matrix test...')
        if _matrices:
            _matrices.clear()
Ejemplo n.º 43
0
                  str(minn) + ' and ' + str(maxx))
    return input


if __name__ == '__main__':
    """Show status of the Cluster Genesis environment

    Args:
        INV_FILE (string): Inventory file.
        LOG_LEVEL (string): Log level.

    Raises:
       Exception: If parameter count is invalid.
    """

    LOG = Logger(__file__)

    ARGV_MAX = 3
    ARGV_COUNT = len(sys.argv)
    if ARGV_COUNT > ARGV_MAX:
        try:
            raise Exception()
        except:
            LOG.error('Invalid argument count')
            sys.exit(1)

    INV_FILE = sys.argv[1]
    LOG.set_level(sys.argv[2])

    main(LOG, INV_FILE)
Ejemplo n.º 44
0
class Behaviours():
    '''
        This class provides avoidance and some other relatively simple behaviours.

        This version uses the PID controller rathen than directly driving the motors.
    '''
    def __init__(self, config, pid_motor_controller, level):
        self._log = Logger("behave", level)
        if config is None:
            raise ValueError('null configuration argument.')
        self._geo = Geometry(config, level)
        _config = config['ros'].get('behaviours')
        self._accel_range_cm = _config.get('accel_range_cm')
        self._cruising_velocity = _config.get('cruising_velocity')
        self._targeting_velocity = _config.get('targeting_velocity')
        self._pid_motor_controller = pid_motor_controller
        _pid_controllers = pid_motor_controller.get_pid_controllers()
        self._port_pid = _pid_controllers[0]
        self._stbd_pid = _pid_controllers[1]
        self._rgbmatrix = RgbMatrix(Level.INFO)
        self._fast_speed = 99.0
        self._slow_speed = 50.0
        self._log.info('ready.')

    # ..........................................................................
    def get_cruising_velocity(self):
        return self._cruising_velocity

    # ..........................................................................
    def back_up(self, duration_sec):
        '''
            Back up for the specified duration so we don't hang our bumper.
        '''
        self._log.info('back up.')
        #       self._motors.astern(self._fast_speed)
        #       time.sleep(duration_sec)
        pass

    # ==========================================================================
    # one_meter .....................................................................
    '''
        The configuration defines an 'acceleration range', which is the range used
        for both acceleration and deceleration. If the travel distance is greater
        than twice this range we have enough room to reach cruising speed before
        beginning to decelerate to the step target. We also define a targeting speed,
        which is a low velocity from which we are prepared to immediately halt upon
        reaching our step target.
    '''
    '''
        Geometry Notes ...................................

        494 encoder steps per rotation (maybe 493)
        68.5mm diameter tires
        215.19mm/21.2cm wheel circumference
        1 wheel rotation = 215.2mm
        2295 steps per meter
        2295 steps per second  = 1 m/sec
        2295 steps per second  = 100 cm/sec
        229.5 steps per second = 10 cm/sec
        22.95 steps per second = 1 cm/sec

        1 rotation = 215mm = 494 steps
        1 meter = 4.587 rotations
        2295.6 steps per meter
        22.95 steps per cm
    '''

    def _one_meter(self, pid, callback):
        '''
           Threaded method for one_meter, one for each PID controller.
           This will enabled the PID if not already enabled.
        '''
        _current_steps = pid.steps  # get this quickly

        _rate = Rate(20)
        if not pid.enabled:
            pid.enable()
        pid.set_slew_rate(SlewRate.SLOWER)
        pid.enable_slew(True)

        # self._geo.steps_for_distance_cm
        _distance_cm = 200.0  # should be 1m
        _target_step_count = _distance_cm * self._geo.steps_per_cm
        _target_steps = round(_current_steps + _target_step_count)
        _closing_target_steps = _target_steps - self._geo.steps_per_rotation

        # last wheel rotation
        _final_target_step_count = _target_steps - (
            1 * self._geo.steps_per_rotation)

        _proposed_accel_range_cm = _distance_cm / 4.0
        if _proposed_accel_range_cm * 2.0 >= self._accel_range_cm:  # is the proposed range greater than the standard range?
            # then we use standard acceleration/deceleration
            self._log.info(
                'using standard accel/decel range (compressed: {:5.2f}cm; standard: {:5.2f}cm)'
                .format(_proposed_accel_range_cm, self._accel_range_cm))
            _accel_range_cm = self._accel_range_cm
        else:  # otherwise compress to just one fourth of distance
            self._log.info(
                'using compressed accel/decel range (compressed: {:5.2f}cm; standard: {:5.2f}cm)'
                .format(_proposed_accel_range_cm, self._accel_range_cm))
            _accel_range_cm = _proposed_accel_range_cm

        _accel_range_steps = round(_accel_range_cm * self._geo.steps_per_cm)
        self._log.info(
            'using accel/decel range of {:5.2f}cm, or {:d} steps.'.format(
                _accel_range_cm, _accel_range_steps))

        _accel_target_steps = _current_steps + _accel_range_steps  # accelerate til this step count
        _decel_target_steps = _target_steps - _accel_range_steps  # step count when we begin to decelerate

        self._log.info(Fore.YELLOW + 'begin one meter travel for {} motor accelerating from {:d} to {:d} steps, then cruise until {:d} steps, when we decelerate to {:d} steps and halt.'.format(\
                pid.orientation.label, _current_steps, _accel_target_steps, _decel_target_steps, _target_steps))

        _actually_do_this = True

        if _actually_do_this:
            #           _accel_velocity = 0
            # accelerate to cruising velocity ............................................
            #           self._rgbmatrix.show_color(Color.CYAN, Orientation.STBD)
            self._log.info(
                Fore.YELLOW +
                '{} motor accelerating...'.format(pid.orientation.label))
            while pid.steps < _accel_target_steps:
                pid.velocity = self._cruising_velocity
                _rate.wait()

            # cruise until 3/4 of range ..................................................
            self._log.info(Fore.YELLOW +
                           '{} motor reached cruising velocity...'.format(
                               pid.orientation.label))
            #           self._rgbmatrix.show_color(Color.GREEN, Orientation.STBD)
            pid.velocity = self._cruising_velocity
            while pid.steps < _decel_target_steps:  # .....................................
                _rate.wait()

            # slow down until we reach 9/10 of range
#           self._rgbmatrix.show_color(Color.YELLOW, Orientation.STBD)
            pid.velocity = round(
                (self._cruising_velocity + self._targeting_velocity) / 2.0)
            while pid.steps < _decel_target_steps:  # .....................................
                _rate.wait()

#           self._rgbmatrix.show_color(Color.ORANGE, Orientation.STBD)
            self._log.info(
                Fore.YELLOW +
                '{} motor reached 9/10 of target, decelerating to targeting velocity...'
                .format(pid.orientation.label))
            pid.set_slew_rate(SlewRate.NORMAL)
            # decelerate to targeting velocity when we get to one wheel revo of target ...
            pid.velocity = self._targeting_velocity
            while pid.steps < _closing_target_steps:  # ...................................
                _rate.wait()

#           self._rgbmatrix.show_color(Color.RED, Orientation.STBD)
            pid.velocity = self._targeting_velocity
            while pid.steps < _target_steps:  # ...........................................
                #               self._log.info(Fore.YELLOW + Style.DIM + '{:d} steps...'.format(pid.steps))
                time.sleep(0.001)
            pid.velocity = 0.0
            #           self._rgbmatrix.show_color(Color.BLACK, Orientation.STBD)
            self._log.info(
                Fore.YELLOW +
                '{} motor stopping...'.format(pid.orientation.label))

        time.sleep(1.0)
        pid.set_slew_rate(SlewRate.NORMAL)

        self._log.info(
            Fore.YELLOW +
            'executing {} callback...'.format(pid.orientation.label))
        callback()
        self._rgbmatrix.disable()
        #       pid.reset()
        #       pid.disable()
        self._log.info(Fore.YELLOW +
                       'one meter on {} motor complete: {:d} of {:d} steps.'.
                       format(pid.orientation.label, pid.steps, _target_steps))

    def one_meter(self):
        '''
           A behaviour that drives one meter forward in a straight line, accelerating and decelerating to hit the target exactly.
        '''
        self._log.info('start one meter travel...')
        self._one_meter_port_complete = False
        self._one_meter_stbd_complete = False
        _tp = threading.Thread(target=self._one_meter,
                               args=(self._port_pid,
                                     self._one_meter_callback_port))
        _ts = threading.Thread(target=self._one_meter,
                               args=(self._stbd_pid,
                                     self._one_meter_callback_stbd))
        _tp.start()
        _ts.start()
        _tp.join()
        _ts.join()
        self._log.info('one meter travel complete.')

    # ..........................................................................
    def _one_meter_callback_port(self):
        self._log.info(Fore.RED + 'port one meter complete.')
        self._one_meter_port_complete = True

    # ..........................................................................
    def _one_meter_callback_stbd(self):
        self._log.info(Fore.GREEN + 'stbd one meter complete.')
        self._one_meter_stbd_complete = True

    # ..........................................................................
    def is_one_metering(self):
        return not self._one_meter_port_complete and not self._one_meter_stbd_complete

    # ==========================================================================
    # roam .....................................................................

    # ..........................................................................
    def _roam_callback_port(self):
        self._log.info(Fore.RED + 'port roam complete.')
        self._roam_port_complete = True

    # ..........................................................................
    def _roam_callback_stbd(self):
        self._log.info(Fore.GREEN + 'stbd roam complete.')
        self._roam_stbd_complete = True

    # ..........................................................................
    def is_roaming(self):
        return not self._roam_port_complete and not self._roam_stbd_complete

    def _roam(self, pid, velocity, steps, orientation, callback):
        '''
           Thread method for each PID controller.
        '''
        _current_steps = pid.steps
        _target_steps = _current_steps + steps
        self._log.info(Fore.YELLOW +
                       'begin {} roaming from {:d} to {:d} steps.'.format(
                           orientation.label, _current_steps, _target_steps))
        pid.velocity = velocity
        while pid.steps < _target_steps:
            # TODO accelerate
            # TODO cruise
            time.sleep(0.01)
        # TODO decelerate
        pid.velocity = 0.0

        callback()
        time.sleep(1.0)
        self._log.info('{} roaming complete: {:d} of {:d} steps.'.format(
            orientation.label, pid.steps, _target_steps))

    def roam(self):
        '''
           A pseudo-roam behaviour.
        '''
        '''
            Geometry Notes ...................................
    
            494 encoder steps per rotation (maybe 493)
            68.5mm diameter tires
            215.19mm/21.2cm wheel circumference
            1 wheel rotation = 215.2mm
            2295 steps per meter
            2295 steps per second  = 1 m/sec
            2295 steps per second  = 100 cm/sec
            229.5 steps per second = 10 cm/sec
            22.95 steps per second = 1 cm/sec
    
            1 rotation = 215mm = 494 steps
            1 meter = 4.587 rotations
            2295.6 steps per meter
            22.95 steps per cm
        '''
        self._log.info('roam.')
        self._roam_port_complete = False
        self._roam_stbd_complete = False
        _port_current_velocity = 0.0
        self._port_pid
        self._stbd_pid

        _forward_steps_per_rotation = 494
        _rotations = 5
        _forward_steps = _forward_steps_per_rotation * _rotations
        _velocity = 35.0  #Velocity.HALF
        self._log.info(
            'start roaming at velocity {:5.2f} for {:d} steps'.format(
                _velocity, _forward_steps))

        _tp = threading.Thread(target=self._roam,
                               args=(self._port_pid, _velocity, _forward_steps,
                                     Orientation.PORT,
                                     self._roam_callback_port))
        _ts = threading.Thread(target=self._roam,
                               args=(self._stbd_pid, _velocity, _forward_steps,
                                     Orientation.STBD,
                                     self._roam_callback_stbd))
        _tp.start()
        _ts.start()
        _tp.join()
        _ts.join()
        self._log.info('complete.')
        pass

    # avoid ....................................................................
    def avoid(self, orientation):
        '''
            Avoids differently depending on orientation.           
        '''
        self._log.info(Fore.CYAN + 'avoid {}.'.format(orientation.name))

        self._log.info('action complete: avoid.')

    # sniff ....................................................................
    def sniff(self):
        if self._rgbmatrix.is_disabled():
            self._rgbmatrix.set_solid_color(Color.BLUE)
            self._rgbmatrix.set_display_type(DisplayType.SOLID)
            #           self._rgbmatrix.set_display_type(DisplayType.RANDOM)
            self._rgbmatrix.enable()
        else:
            self._rgbmatrix.disable()
Ejemplo n.º 45
0
class DriverCommands:
    def __init__(self, driver):
        self.driver = driver
        self.wait_time = 5
        self.log = Logger()
        self.base_url = CONFIG['BASE_URL']

    def find_element(self, selector):
        """Find element on application view.

            :param selector: tuple (eg. By.ID, 'element/id')
            :return: elements handler
        """
        element = self.driver.find_element(*selector)
        return element

    def find_elements(self, selector):
        """Find all element on visible view with selector

            :param selector: elements selector
        """
        elements = self.driver.find_elements(*selector)
        return elements

    def click_element(self, selector):
        """Find element and click on it.

            :param selector: tuple (eg. By.ID, 'element/id')
        """
        element = self.wait_for_element_to_be_clickable(selector, 2)
        element.click()

    def select_dd_element(self, selector, option_name):
        element = self.find_element(selector)
        select = Select(element)
        select.select_by_visible_text(option_name)

    def fill_in(self, selector, value, confirm=False):
        """Find element and enter text to the field

            :param selector: tuple (eg. By.ID, 'element/id')
            :param value: text
            :param confirm: if True field is confirmed by send keyboard button Enter
        """
        element = self.wait_for_element_visibility(selector)
        element.clear()
        element.send_keys(value)
        if confirm:
            element.submit()

    def move_to_element(self, selector):
        """
        move mouse coursor up to element
        :param selector: selector for element"""
        element = self.find_element(selector)
        ActionChains(self.driver).move_to_element(element).perform()

    def check_page_title(self, expected_title, wait=5):
        """Wait some time until title will be equal to expected

            :param expected_title: expected title to compare
            :param wait: time to wait
        """
        wait = wait or self.wait_time
        try:
            WebDriverWait(self.driver, wait).until(EC.title_is(expected_title))
            self.log.logger('INFO', 'Page title is correct')
        except TimeoutException:
            title = self.get_page_title()
            raise AssertionError(
                'Wrong page title, should be "%s", but is: "%s"' %
                (expected_title, title))

    def get_page_title(self):
        """Get current page title
        :return: page title
        """
        return self.driver.title

    def get_text_from_element(self, selector):
        """Find element and get text from it.

            :param selector: tuple (eg. By.ID, 'element/id')
            :return: text from element
        """
        element = self.find_element(selector)
        return element.text

    def get_attribute_from_element(self, selector, attribute='value'):
        """Find element and get text from it.

            :param selector: tuple (eg. By.ID, 'element/id')
            :param attribute: html value
            :return: text from element
        """
        element = self.find_element(selector)
        return element.get_attribute(attribute)

    def check_element_attribute(self, selector, attribute, expected):
        """Find element, get attribute from it and compare with your expectation.

            :param selector: tuple - element to get attribute
            :param attribute: string - web element attribute e.g ()
            :param expected: text to compare with text from web element attribute
        """
        self.log.logger('INFO', 'Checking text. Text should be %s' % expected)
        attribute_value = self.get_attribute_from_element(selector, attribute)
        assert attribute_value == expected, "Wrong attribute value. Should be '%s' instead of '%s'" % (
            expected, attribute_value)

    def check_checkbox_is_selected(self, selector):
        """Find checkbox and check if it is selected

            :param selector: tuple (eg. By.ID, 'element/id')
            :return: checkbox state (True or False)
        """
        element = self.find_element(selector)
        return element.is_selected()

    # WAIT METHODS

    def wait_for_element_to_be_clickable(self, selector, wait=None):
        wait = wait or self.wait_time
        try:
            return WebDriverWait(self.driver, wait).until(
                EC.element_to_be_clickable(selector))
        except TimeoutException:
            raise AssertionError('Element is not visible or not clickable')

    def wait_for_element_visibility(self, selector, wait=None):
        """Wait some time until expected element will be visible on current page

            :param selector: element selector
            :param wait: time to wait
        """
        wait = wait or self.wait_time
        try:
            element = WebDriverWait(self.driver, wait).until(
                EC.visibility_of_element_located(selector))
            return element
        except (TimeoutException, NoSuchElementException):
            raise AssertionError('Could not find element' + str(selector))

    def wait_for_element_not_visibility(self, selector, wait=None):
        """Wait some time until visible element disappear

            :param selector: element selector
            :param wait: time to wait
        """
        wait = wait or self.wait_time
        try:
            WebDriverWait(self.driver, wait).until_not(
                EC.visibility_of_element_located(selector))
        except (TimeoutException, NoSuchElementException):
            return False

    def wait_for_expected_text(self, selector, expected_text, wait=None):
        """Wait some time until expected text will be visible on current page

            :param expected_text: expected title to compare
            :param selector: element selector
            :param wait: time to wait
        """
        wait = wait or self.wait_time
        try:
            text_is_present = WebDriverWait(self.driver, wait).until(
                EC.text_to_be_present_in_element(selector, expected_text))
            return text_is_present
        except (TimeoutException, NoSuchElementException):
            raise AssertionError(
                'Something went wrong with reading text from the element' +
                str(selector))

    def wait_for_alert(self, wait=None):
        """
        Wait some time until for alert presented
        :param wait: time to wait
        :return: Bool
        """
        wait = wait or self.wait_time
        try:
            WebDriverWait(self.driver, wait).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
            self.log.logger('INFO',
                            'Alert presented with message: %s' % alert.text)
            return True
        except TimeoutException:
            self.log.logger('INFO', 'Any alert presented')
            return False

    @staticmethod
    def wait(seconds):
        time.sleep(seconds)

    def open_url(self, url):
        self.driver.get(url)
        self.log.logger('INFO', 'Opened url: %s' % url)

    def get_screenshot_file(self, driver, file_name):
        """
        Function create screenshot png file in the screenshot directory
        :param driver: webdriver
        :param file_name: name of the screenshot file
        """
        scr_dir = os.path.join(
            os.path.abspath(os.path.dirname(os.path.dirname(__file__))),
            'screenshots/')
        scr_file = scr_dir + file_name + '.png'
        try:
            driver.get_screenshot_as_file(scr_file)
            self.log.logger('INFO', 'Screenshot saved: %s' % scr_file)
        except NoSuchWindowException:
            self.log.logger('ERROR', 'Browser unable to get a screenshot')
Ejemplo n.º 46
0
 def __init__(self, num, percent, value):
     self._log = Logger('velocity', Level.INFO)
     self._percent = percent
     self._value = value # how many ticks per 20Hz loop?
Ejemplo n.º 47
0
 def __init__(self, driver):
     self.driver = driver
     self.wait_time = 5
     self.log = Logger()
     self.base_url = CONFIG['BASE_URL']
Ejemplo n.º 48
0
                        self.logger.error("Push Error: {}".format(e))


class MyThread(threading.Thread):
    def __init__(self, target, args):
        super().__init__()
        self.target = target
        self.args = args

    def run(self):
        self.target(*self.args)


if __name__ == "__main__":
    # k线 logger日志
    logger = Logger.get_logger("huobipro_trade_log")
    # 获取代码目录绝对路径
    last_dir = os.path.abspath(os.path.dirname(os.getcwd()))

    # 创建 conf/common_conf/common_conf.yaml 配置对象
    common_path = '{}/conf/common_conf/common_conf.yaml'.format(last_dir)
    common_config = Config(common_path)

    # 读取redis数据库配置,并创建redis数据库连接
    redis_conf = common_config.get_value("redis")
    redis_connect = redis.Redis(**redis_conf)
    logger.info("redis初始化成功.")


    # 创建 conf/script_conf/trade_socket/heyue.yaml 配置对象
    script_path = '{}/conf/script_conf/trade_socket/huobipro.yaml'.format(last_dir)
Ejemplo n.º 49
0
def main():

    signal.signal(signal.SIGINT, signal_handler)

    _log = Logger("scanner", Level.INFO)
    _log.info(Fore.CYAN + Style.BRIGHT + ' INFO  : starting test...')
    _log.info(Fore.YELLOW + Style.BRIGHT + ' INFO  : Press Ctrl+C to exit.')

    try:
        _i2c_scanner = I2CScanner(Level.WARN)
        _addresses = _i2c_scanner.get_int_addresses()
        _hex_addresses = _i2c_scanner.get_hex_addresses()
        _addrDict = dict(
            list(map(lambda x, y: (x, y), _addresses, _hex_addresses)))
        for i in range(len(_addresses)):
            _log.debug(Fore.BLACK + Style.DIM +
                       'found device at address: {}'.format(_hex_addresses[i]))

        vl53l1x_available = (0x29 in _addresses)
        ultraborg_available = (0x36 in _addresses)

        if not vl53l1x_available:
            raise OSError('VL53L1X hardware dependency not available.')
        if not ultraborg_available:
            #           raise OSError('UltraBorg hardware dependency not available.')
            print('UltraBorg hardware dependency not available.')

        _log.info('starting scan...')
        #       _player = Player(Level.INFO)

        _loader = ConfigLoader(Level.INFO)
        filename = 'config.yaml'
        _config = _loader.configure(filename)

        _lidar = Lidar(_config, Level.INFO)
        _lidar.enable()

        for i in range(5):
            values = _lidar.scan()
            _angle_at_min = values[0]
            if _angle_at_min < 0:
                _mm = values[3]
                _log.info(Fore.CYAN + Style.BRIGHT +
                          'distance:\t{}mm'.format(_mm))
            else:
                _min_mm = values[1]
                _angle_at_max = values[2]
                _max_mm = values[3]
                _log.info(Fore.CYAN + Style.BRIGHT +
                          'min. distance at {:>5.2f}°:\t{}mm'.format(
                              _angle_at_min, _min_mm))
                _log.info(Fore.CYAN + Style.BRIGHT +
                          'max. distance at {:>5.2f}°:\t{}mm'.format(
                              _angle_at_max, _max_mm))
            time.sleep(1.0)

        _lidar.close()
        _log.info(Fore.CYAN + Style.BRIGHT + 'test complete.')

    except Exception:
        _log.info(traceback.format_exc())
        sys.exit(1)
Ejemplo n.º 50
0
 def __init__(self, level):
     self._log = Logger('configloader', level)
     self._log.info('ready.')
Ejemplo n.º 51
0
class IMU():
    '''
    Composite IMU.
    '''
    def __init__(self, config, level):
        self._log = Logger("imu", level)
        if config is None:
            raise ValueError("no configuration provided.")
        # ICM20948 configuration .....................................
        _icm20948_config = config['ros'].get('icm20948')
        self._icm20948_heading_trim = _icm20948_config.get('heading_trim')
        self._log.info('trim: heading: {:<5.2f}° (ICM20948)'.format(
            self._icm20948_heading_trim))
        self._icm20948 = ICM20948(i2c_addr=0x69)
        self._amin = list(self._icm20948.read_magnetometer_data())
        self._amax = list(self._icm20948.read_magnetometer_data())
        # BNO055 configuration .......................................
        self._bno055 = BNO055(config, Level.INFO)

        self._log.info('ready.')

    # ..........................................................................
    def read_icm20948_magnetometer(self):
        return self._icm20948.read_magnetometer_data()

    # ..........................................................................
    def read_icm20948_accelerometer_gyro_data(self):
        # ax, ay, az, gx, gy, gz = _imu.read_accelerometer_gyro()
        return self._icm20948.read_accelerometer_gyro_data()

    # ..........................................................................
    def read_icm20948_heading(self):
        _mag = self._icm20948.read_magnetometer_data()
        _orig_heading = Convert.heading_from_magnetometer(
            self._amin, self._amax, _mag)
        _heading = Convert.offset_in_degrees(_orig_heading,
                                             self._icm20948_heading_trim)
        self._log.info(Fore.GREEN + 'heading:\t{:>9.2f}°\t'.format(_heading) + Style.DIM \
                + 'orig: {:>9.2f}°\ttrim: {:>9.2f}°; icm20948'.format(_orig_heading, self._icm20948_heading_trim))
        return _heading

    # ..........................................................................
    def read_bno055_heading(self):
        _bno_reading = self._bno055.read()
        return _bno_reading

    # ..........................................................................
    def read_heading(self):
        '''
        Read a composite heading, returning the BNO055 and ICM20948 values as a tuple.
        '''
        _bno_reading = self.read_bno055_heading()
        _bno_heading = _bno_reading[1]
        _icm20948_heading = self.read_icm20948_heading()
        if _bno_heading and _icm20948_heading:
            _diff = _bno_heading - _icm20948_heading
            if _diff < 90.0:
                self._log.info(Fore.CYAN + 'diff: {:5.2f}°\t'.format(_diff) +
                               Fore.BLACK +
                               '(bno: {:5.2f}°; icm: {:5.2f}°)'.format(
                                   _bno_heading, _icm20948_heading))
            else:
                self._log.info(Fore.YELLOW + 'diff: {:5.2f}°\t'.format(_diff) +
                               Fore.BLACK +
                               '(bno: {:5.2f}°; icm: {:5.2f}°)'.format(
                                   _bno_heading, _icm20948_heading))
            return [_bno_heading, _icm20948_heading]
        else:
            self._log.info('unavailable.')
            return [-1.0, -1.0]
Ejemplo n.º 52
0
class Video():
    '''
    Provides a video-to-file and optional video-to-stream (web) functionality,
    with options for dynamically altering the camera settings to account for
    light levels based on a Pimoroni LTR-559 lux sensor (at 0x23), as well as 
    automatic and manual controls for camera lighting using either one or two 
    Pimoroni Matrix 11x7 displays (white LEDs, at 0x75 and 0x77).

    The camera image is annotated with a title and timestamp. The output filename
    is timestamped and written to a './videos' directory in H.264 video format.
    '''
    def __init__(self, config, level):
        super().__init__()
        global video_width, video_height, annotation_title
        self._log = Logger('video', level)
        if config is None:
            raise ValueError("no configuration provided.")
        self._config = config
        _config = self._config['ros'].get('video')
        self._enable_streaming = _config.get('enable_streaming')
        self._port = _config.get('port')
        self._lux_threshold = _config.get('lux_threshold')
        self._counter = itertools.count()
        self._server = None

        # camera configuration
        self._ctrl_lights = _config.get('ctrl_lights')
        self._width = _config.get('width')
        self._height = _config.get('height')
        self._resolution = (self._width, self._height)
        self._framerate = _config.get('framerate')
        self._convert_mp4 = _config.get('convert_mp4')
        self._remove_h264 = _config.get('remove_h264')
        self._quality = _config.get('quality')
        self._annotate = _config.get('annotate')
        self._title = _config.get('title')
        self._basename = _config.get('basename')
        self._dirname = _config.get('dirname')
        # set globals
        video_width = self._width
        video_height = self._height
        annotation_title = self._title
        self._filename = None
        self._thread = None
        self._killer = None

        # scan I2c bus for devices
        _i2c_scanner = I2CScanner(Level.DEBUG)
        if _i2c_scanner.has_address([0x23]):
            self._lux = Lux(Level.INFO)
            self._log.info(
                'LTR-559 lux sensor found: camera adjustment active.')
        else:
            self._lux = None
            self._log.warning(
                'no LTR-559 lux sensor found: camera adjustment inactive.')

        # lighting configuration
        self._port_light = None
        self._stbd_light = None
        if self._ctrl_lights:
            if _i2c_scanner.has_address([0x77]):  # port
                self._port_light = Matrix(Orientation.PORT, Level.INFO)
                self._log.info('port-side camera lighting available.')
            else:
                self._log.warning('no port-side camera lighting available.')
            if _i2c_scanner.has_address([0x75]):  # starboard
                self._stbd_light = Matrix(Orientation.STBD, Level.INFO)
                self._log.info('starboard-side camera lighting available.')
            else:
                self._log.warning(
                    'no starboard-side camera lighting available.')
        else:
            self._log.info('lighting control is disabled.')

        if self._enable_streaming:
            self._log.info('ready: streaming on port {:d}'.format(self._port))
        else:
            self._log.info('ready: save to file only, no streaming.')

    # ..........................................................................
    def set_compass(self, compass):
        global g_compass
        g_compass = compass  # used by annotation
        self._compass = compass

    # ..........................................................................
    @staticmethod
    def get_annotation():
        global annotation_title, g_compass
        _heading = ''  #g_compass.get_heading_message() if g_compass is not None else ''
        return '{} {} {}'.format(
            annotation_title,
            dt.now(tzlocal.get_localzone()).strftime('%Y-%m-%d %H:%M:%S %Z'),
            _heading)

    # ..........................................................................
    def is_night_mode(self):
        if self._lux is None:
            return False
        _value = self._lux.get_value()
        _threshold = self._lux_threshold
        self._log.debug('lux: {:>5.2f} <  threshold: {:>5.2f}?'.format(
            _value, _threshold))
        return _value < _threshold

    # ..........................................................................
    def _get_timestamp(self):
        return dt.utcfromtimestamp(
            dt.utcnow().timestamp()).isoformat().replace(':', '_').replace(
                '-', '_').replace('.', '_')

    # ..........................................................................
    def get_filename(self):
        return self._filename

    # ..........................................................................
    def get_ip_address(self):
        _socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        try:
            _socket.connect(('10.255.255.255', 1))
            _ip = _socket.getsockname()[0]
        except Exception:
            _ip = '127.0.0.1'
        finally:
            _socket.close()
        return _ip

    # ..........................................................................
    def _start(self, output_splitter, f_is_enabled):
        global output  # necessary for access from StreamingHandler, passed as type
        output = output_splitter
        self._output = output_splitter
        self._filename = output_splitter.get_filename()
        if self._enable_streaming:
            self._log.info('starting video with capture to file: {}'.format(
                output_splitter.get_filename()))
        else:
            self._log.info('starting capture to file: {}'.format(
                output_splitter.get_filename()))
        with picamera.PiCamera(resolution=self._resolution,
                               framerate=self._framerate) as camera:
            self._log.info('camera framerate: {}'.format(camera.framerate))
            self._log.info('camera ISO: {}'.format(camera.iso))
            self._log.info('camera mode: {}'.format(camera.exposure_mode))
            self._log.info('camera shutter speed: {}'.format(
                camera.shutter_speed))
            if self._annotate:
                camera.annotate_text_size = 12
                self.set_night_mode(camera, self.is_night_mode())
                #               camera.annotate_text = Video.get_annotation() # initial text
                # start video annotation thread
                self._annot = threading.Thread(
                    target=Video._annotate, args=[self, camera, f_is_enabled])
                self._annot.setDaemon(True)
                self._annot.start()
            if self._quality > 0:
                # values 1 (highest quality) to 40 (lowest quality), with typical values between 20 and 25
                self._log.info('camera quality: {}'.format(self._quality))
                camera.start_recording(output_splitter,
                                       format='mjpeg',
                                       quality=self._quality)
            else:
                camera.start_recording(output_splitter, format='mjpeg')
            # ............
            try:
                if self._enable_streaming:
                    if self._server is None:
                        self._log.info('starting streaming server...')
                        address = ('', self._port)
                        self._server = StreamingServer(address,
                                                       StreamingHandler,
                                                       f_is_enabled)
                        self._killer = lambda: self.close(
                            camera, output_splitter)
                        self._server.serve_forever()
                    else:
                        self._log.info('streaming server already active.')
                else:  # keepalive
                    while f_is_enabled():
                        self._log.debug('keep-alive...')
                        time.sleep(1.0)
                self._log.info(Fore.RED + 'exited video loop.')
            except Exception:
                self._log.error('error streaming video: {}'.format(
                    traceback.format_exc()))
            finally:
                self._log.info(Fore.RED + 'finally.')
#               self.close(camera, output_splitter)
        self._log.info('_start: complete.')

    # ..........................................................................
    def _annotate(self, camera, f_is_enabled):
        '''
            Update the video annotation every second.
        '''
        self._camera = camera
        _count = 0
        while f_is_enabled():
            #           _count = next(self._counter)
            self._camera.annotate_text = Video.get_annotation()
            #           if ( _count % 5 ) == 0: # every five seconds
            self.set_night_mode(camera, self.is_night_mode())
            time.sleep(1.0)

    # ..........................................................................
    def set_night_mode(self, camera, enabled):
        # NOTE: setting 'iso' overrides exposure mode
        _compass_calibrated = False  # True if self._compass and self._compass.is_calibrated() else False
        if enabled:
            self._log.debug('night mode.')
            #           camera.exposure_mode = 'nightpreview'
            '''
                off
                auto: use automatic exposure mode
                night: select setting for night shooting
                nightpreview:
                backlight: select setting for backlit subject
                spotlight:
                sports: select setting for sports (fast shutter etc.)
                snow: select setting optimised for snowy scenery
                beach: select setting optimised for beach
                verylong: select setting for long exposures
                fixedfps: constrain fps to a fixed value
                antishake: antishake mode
                fireworks: select setting optimised for fireworks

                source: https://www.raspberrypi.org/documentation/raspbian/applications/camera.md
            '''
            camera.iso = 800
            camera.led = False
            camera.annotate_foreground = Color.from_string('#ffdada')
            if _compass_calibrated:
                camera.annotate_background = Color.from_string('#440000')
            else:
                camera.annotate_background = Color.from_string('#440088')
            if self._ctrl_lights:
                self.set_lights(True)
        else:
            self._log.debug('day mode.')
            camera.iso = 100
            #           camera.exposure_mode = 'off'
            camera.led = True
            camera.annotate_foreground = Color.from_string('#111111')
            if _compass_calibrated:
                camera.annotate_background = Color.from_string('#ffffff')
            else:
                camera.annotate_background = Color.from_string('#ffff00')
            if self._ctrl_lights:
                self.set_lights(False)

    # ..........................................................................
    def set_lights(self, on):
        if self._port_light:
            if on:
                self._port_light.light()
            else:
                self._port_light.clear()
        if self._stbd_light:
            if on:
                self._stbd_light.light()
            else:
                self._stbd_light.clear()

    # ..........................................................................
    def is_enabled(self):
        return self._enabled

    # ..........................................................................
    @property
    def active(self):
        return self._thread is not None

    # ..........................................................................
    def start(self):
        if self._thread is not None:
            self._log.info('video already started.')
            return
        self._log.info('start.')
        self._enabled = True
        if not os.path.isdir(self._dirname):
            os.makedirs(self._dirname)
        self._filename = os.path.join(
            self._dirname,
            self._basename + '_' + self._get_timestamp() + '.h264')
        _output = OutputSplitter(self._filename)
        self._thread = threading.Thread(target=Video._start,
                                        args=[
                                            self,
                                            _output,
                                            lambda: self.is_enabled(),
                                        ])
        self._thread.setDaemon(True)
        self._thread.start()
        _ip = self.get_ip_address()
        self._log.info(
            Fore.MAGENTA + Style.BRIGHT +
            'video started on:\thttp://{}:{:d}/'.format(_ip, self._port))

    # ..........................................................................
    def stop(self):
        if self._thread is None:
            self._log.info('video already stopped.')
            return
        self._log.info('stopping video capture on file: {}'.format(
            self._filename))
        print(Fore.GREEN + 'setting enabled flag to False.' + Style.RESET_ALL)
        self._enabled = False

        if self._killer is not None:
            print(Fore.GREEN + 'KILLING...' + Style.RESET_ALL)
            self._killer()
        else:
            print(Fore.GREEN + 'NO KILLER.' + Style.RESET_ALL)

        if self._output is not None:
            print(Fore.GREEN + 'flushing and closing...' + Style.RESET_ALL)
            self._output.flush()
            self._output.close()
            self._output = None
            print(Fore.GREEN + 'closed.' + Style.RESET_ALL)
        self._log.info('joining thread...')
        self._thread.join(timeout=1.0)
        self._thread = None
        if self._convert_mp4:
            self._convert_to_mp4()
        self._log.info('stopped.')

    # ..........................................................................
    def _convert_to_mp4(self):
        if os.path.exists(self._filename):
            self._log.info('converting file {} to mp4...'.format(
                self._filename))
            _mp4_filename = Path(self._filename).stem + ".mp4"
            os.system('ffmpeg -loglevel panic -hide_banner -r {:d} -i {:s} -vcodec copy {:s}'.format(\
                    self._framerate, self._filename, _mp4_filename))
            self._log.info('mp4 conversion complete.')
            if self._remove_h264:
                os.remove(self._filename)
                self._log.info('removed h264 video source.')
        else:
            self._log.warning(
                'could not convert to mp4: file {} did not exist.'.format(
                    self._filename))

    # ..........................................................................
    def close(self, camera, output):
        self._log.info('closing video...')
        if self._ctrl_lights:
            self.set_lights(False)
        if camera:
            if camera.recording:
                camera.stop_recording()
                self._log.debug('camera stopped recording.')
            if not camera.closed:
                camera.close()
                self._log.debug('camera closed.')
        if output:
            output.flush()
            self._log.debug('output flushed.')
            output.close()
            self._log.debug('output closed.')
        if self._server is not None:
            self._log.info('shutting down server...')
            self._server.shutdown()
            self._log.info('server shut down.')
        self._log.info(Fore.MAGENTA + Style.BRIGHT + 'video closed.')
from lib.awsapi_helpers import AWSClient, BotoSession
from lib.applogger import LogHandler
from lib.metrics import Metrics

#------------------------------
# Remediation-Specific
#------------------------------
LAMBDA_ROLE = 'SO0111_CIS43_memberRole'
REMEDIATION = 'Remove all rules from the default security group'
AFFECTED_OBJECT = 'Security Group'
#------------------------------

PLAYBOOK = os.path.basename(__file__[:-3])
# initialise LOGGERs
LOG_LEVEL = os.getenv('log_level', 'info')
LOGGER = Logger(loglevel=LOG_LEVEL)
APPLOGGER = LogHandler(PLAYBOOK)  # application LOGGER for CW Logs

# Get AWS region from Lambda environment. If not present then we're not
# running under lambda, so defaulting to us-east-1
AWS_REGION = os.getenv('AWS_DEFAULT_REGION', 'us-east-1')
# Append region name to LAMBDA_ROLE
LAMBDA_ROLE += '_' + AWS_REGION
BOTO_CONFIG = Config(retries={'max_attempts': 10}, region_name=AWS_REGION)
AWS = AWSClient()


#------------------------------------------------------------------------------
# HANDLER
#------------------------------------------------------------------------------
def lambda_handler(event, context):
Ejemplo n.º 54
0
 def __init__(self, address, streaming_handler, f_is_enabled):
     super().__init__(address, streaming_handler)
     self._log = Logger('server', Level.INFO)
     self._enabled_flag = f_is_enabled
     self._log.info('ready.')
Ejemplo n.º 55
0
from lib.logger import Level, Logger
from lib.motors_v2 import Motors
from lib.config_loader import ConfigLoader


def brake():
    _log.info('braking...')
    _motors.brake()
    _motors.stop()
    _motors.close()
    _log.info('done.')


# ..............................................................................
_motors = None
_log = Logger('brake', Level.INFO)

try:

    _log.info('configuring...')
    # read YAML configuration
    _loader = ConfigLoader(Level.WARN)
    filename = 'config.yaml'
    _config = _loader.configure(filename)
    _motors = Motors(_config, None, Level.WARN)

except KeyboardInterrupt:
    _log.error('Ctrl-C caught in main: exiting...')
finally:
    _log.info('complete.')
Ejemplo n.º 56
0
    def __init__(self, config, level):
        super().__init__()
        global video_width, video_height, annotation_title
        self._log = Logger('video', level)
        if config is None:
            raise ValueError("no configuration provided.")
        self._config = config
        _config = self._config['ros'].get('video')
        self._enable_streaming = _config.get('enable_streaming')
        self._port = _config.get('port')
        self._lux_threshold = _config.get('lux_threshold')
        self._counter = itertools.count()
        self._server = None

        # camera configuration
        self._ctrl_lights = _config.get('ctrl_lights')
        self._width = _config.get('width')
        self._height = _config.get('height')
        self._resolution = (self._width, self._height)
        self._framerate = _config.get('framerate')
        self._convert_mp4 = _config.get('convert_mp4')
        self._remove_h264 = _config.get('remove_h264')
        self._quality = _config.get('quality')
        self._annotate = _config.get('annotate')
        self._title = _config.get('title')
        self._basename = _config.get('basename')
        self._dirname = _config.get('dirname')
        # set globals
        video_width = self._width
        video_height = self._height
        annotation_title = self._title
        self._filename = None
        self._thread = None
        self._killer = None

        # scan I2c bus for devices
        _i2c_scanner = I2CScanner(Level.DEBUG)
        if _i2c_scanner.has_address([0x23]):
            self._lux = Lux(Level.INFO)
            self._log.info(
                'LTR-559 lux sensor found: camera adjustment active.')
        else:
            self._lux = None
            self._log.warning(
                'no LTR-559 lux sensor found: camera adjustment inactive.')

        # lighting configuration
        self._port_light = None
        self._stbd_light = None
        if self._ctrl_lights:
            if _i2c_scanner.has_address([0x77]):  # port
                self._port_light = Matrix(Orientation.PORT, Level.INFO)
                self._log.info('port-side camera lighting available.')
            else:
                self._log.warning('no port-side camera lighting available.')
            if _i2c_scanner.has_address([0x75]):  # starboard
                self._stbd_light = Matrix(Orientation.STBD, Level.INFO)
                self._log.info('starboard-side camera lighting available.')
            else:
                self._log.warning(
                    'no starboard-side camera lighting available.')
        else:
            self._log.info('lighting control is disabled.')

        if self._enable_streaming:
            self._log.info('ready: streaming on port {:d}'.format(self._port))
        else:
            self._log.info('ready: save to file only, no streaming.')
Ejemplo n.º 57
0
def run(opt):
    torch.manual_seed(opt.seed)
    torch.backends.cudnn.benchmark = not opt.not_cuda_benchmark and not opt.test

    print('Setting up data...')
    Dataset = get_dataset(opt.dataset,
                          opt.task)  # if opt.task==mot -> JointDataset

    f = open(opt.data_cfg)  # 选择什么数据集进行训练测试 '../src/lib/cfg/mot15.json',
    data_config = json.load(f)
    trainset_paths = data_config['train']  # 训练集路径
    dataset_root = data_config['root']  # 数据集所在目录
    print("Dataset root: %s" % dataset_root)
    f.close()

    transforms = T.Compose([T.ToTensor()])
    dataset = Dataset(opt=opt,
                      root=dataset_root,
                      paths=trainset_paths,
                      img_size=(1088, 608),
                      augment=True,
                      transforms=transforms)
    opt = opts().update_dataset_info_and_set_heads(opt, dataset)
    print("opt:\n", opt)

    logger = Logger(opt)

    # os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    # os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str  # 多GPU训练
    # print("opt.gpus_str: ", opt.gpus_str)
    # opt.device = torch.device('cuda: 0' if opt.gpus[0] >= 0 else 'cpu')  # 设置GPU
    opt.device = device
    opt.gpus = my_visible_devs

    # 构建模型
    print('Creating model...')
    model = create_model(opt.arch, opt.heads, opt.head_conv)

    # 初始化优化器
    optimizer = torch.optim.Adam(model.parameters(), opt.lr)

    start_epoch = 0
    if opt.load_model != '':
        model, optimizer, start_epoch = load_model(model, opt.load_model,
                                                   optimizer, opt.resume,
                                                   opt.lr, opt.lr_step)

    # Get dataloader
    if opt.is_debug:
        train_loader = torch.utils.data.DataLoader(
            dataset=dataset,
            batch_size=opt.batch_size,
            shuffle=True,
            pin_memory=True,
            drop_last=True)  # debug时不设置线程数(即默认为0)
    else:
        train_loader = torch.utils.data.DataLoader(dataset=dataset,
                                                   batch_size=opt.batch_size,
                                                   shuffle=True,
                                                   num_workers=opt.num_workers,
                                                   pin_memory=True,
                                                   drop_last=True)

    print('Starting training...')
    Trainer = train_factory[opt.task]
    trainer = Trainer(opt=opt, model=model, optimizer=optimizer)
    # trainer.set_device(opt.gpus, opt.chunk_sizes, opt.device)
    trainer.set_device(opt.gpus, opt.chunk_sizes, device)

    best = 1e10
    for epoch in range(start_epoch + 1, opt.num_epochs + 1):
        mark = epoch if opt.save_all else 'last'

        # 训练的核心函数
        log_dict_train, _ = trainer.train(epoch, train_loader)

        logger.write('epoch: {} |'.format(epoch))
        for k, v in log_dict_train.items():
            logger.scalar_summary('train_{}'.format(k), v, epoch)
            logger.write('{} {:8f} | '.format(k, v))

        if opt.val_intervals > 0 and epoch % opt.val_intervals == 0:
            save_model(os.path.join(opt.save_dir, 'model_{}.pth'.format(mark)),
                       epoch, model, optimizer)
        else:
            save_model(os.path.join(opt.save_dir, 'model_last.pth'), epoch,
                       model, optimizer)
        logger.write('\n')

        if epoch in opt.lr_step:
            save_model(
                os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)),
                epoch, model, optimizer)
            lr = opt.lr * (0.1**(opt.lr_step.index(epoch) + 1))
            print('Drop LR to', lr)
            for param_group in optimizer.param_groups:
                param_group['lr'] = lr
        if epoch % 5 == 0:
            save_model(
                os.path.join(opt.save_dir, 'model_{}.pth'.format(epoch)),
                epoch, model, optimizer)
    logger.close()
Ejemplo n.º 58
0
class Rate():
    '''
       Loops at a fixed rate, specified in hertz (Hz).

       :param hertz:   the frequency of the loop in Hertz
       :param level:   the log level
       :param use_ns:  (optional) if True use a nanosecond counter instead of milliseconds
    '''
    def __init__(self, hertz, level=Level.INFO, use_ns=False):
        self._log = Logger('rate', level)
        self._last_ns = time.perf_counter_ns()
        self._last_time = time.time()
        self._dt = 1 / hertz
        self._dt_ms = self._dt * 1000
        self._dt_ns = self._dt_ms * 1000000
        self._use_ns = use_ns
        if self._use_ns:
            self._log.info(
                'nanosecond rate set for {:d}Hz (period: {:>4.2f}sec/{:d}ms)'.
                format(hertz, self.get_period_sec(), self.get_period_ms()))
        else:
            self._log.info(
                'millisecond rate set for {:d}Hz (period: {:>4.2f}sec/{:d}ms)'.
                format(hertz, self.get_period_sec(), self.get_period_ms()))

    # ..........................................................................
    def get_period_sec(self):
        '''
            Returns the period in seconds, as a float.
        '''
        return self._dt

    # ..........................................................................
    def get_period_ms(self):
        '''
            Returns the period in milliseconds, rounded to an int.
        '''
        return round(self._dt * 1000)

    # ..........................................................................
    def waiting(self):
        '''
           Return True if still waiting for the current loop to complete.
        '''
        return self._dt < (time.time() - self._last_time)

    # ..........................................................................
    def wait(self):
        """
            If called before the allotted period will wait the remaining time
            so that the loop is no faster than the specified frequency. If
            called after the allotted period has passed, no waiting takes place.

            E.g.:

                # execute loop at 60Hz
                rate = Rate(60)

                while True:
                    # do something...
                    rate.wait()

        """
        if self._use_ns:
            _ns_diff = time.perf_counter_ns() - self._last_ns
            _delay_sec = (self._dt_ns - _ns_diff) / (1000 * 1000000)
            if self._dt_ns > _ns_diff:
                #               print('...')
                time.sleep(_delay_sec)
#           self._log.info(Fore.BLACK + Style.BRIGHT + 'dt_ns: {:7.4f}; diff: {:7.4f}ns; delay: {:7.4f}s'.format(self._dt_ns, _ns_diff, _delay_sec))
            self._last_ns = time.perf_counter_ns()
        else:
            _diff = time.time() - self._last_time
            _delay_sec = self._dt - _diff
            if self._dt > _diff:
                time.sleep(_delay_sec)
#           self._log.info(Fore.BLACK + Style.BRIGHT + 'dt: {:7.4f}; diff: {:7.4f}ms; delay: {:7.4f}s'.format(self._dt, _diff, _delay_sec))
            self._last_time = time.time()
Ejemplo n.º 59
0
class OutputSplitter(object):
    '''
        An output (as far as picamera is concerned), is just a filename or an object
        which implements a write() method (and optionally the flush() and close() methods)

        If the filename parameter is None no file is written.
    '''
    def __init__(self, filename):
        self.frame = None
        self.buffer = io.BytesIO()
        self._log = Logger('output', Level.INFO)
        self._filename = filename
        if self._filename:
            self._output_file = io.open(filename, 'wb')
            self._log.info(Fore.MAGENTA + 'output file: {}'.format(filename))
        else:
            self._output_file = None
            self._log.info(Fore.MAGENTA +
                           'no output file generated from video.')
        self._condition = Condition()
        self._log.info('ready.')

    def get_filename(self):
        return self._filename

    def write(self, buf):
        if buf.startswith(b'\xff\xd8'):
            # new frame, copy existing buffer's content and notify all clients it's available
            self.buffer.truncate()
            with self._condition:
                self.frame = self.buffer.getvalue()
                self._condition.notify_all()
            self.buffer.seek(0)
        if self._output_file and not self._output_file.closed:
            self._output_file.write(buf)
        return self.buffer.write(buf)

    def flush(self):
        self._log.debug('flushing...')
        if self._output_file and not self._output_file.closed:
            self._output_file.flush()
        if not self.buffer.closed:
            self.buffer.flush()
        self._log.debug('flushed.')

    def close(self):
        self._log.info('closing...')
        if self._output_file and not self._output_file.closed:
            self._output_file.close()
        if not self.buffer.closed:
            self.buffer.close()
        self._log.info('closed.')
Ejemplo n.º 60
0
class RoamBehaviour():
    '''
        This action class provides a roaming behaviour.
    '''
    def __init__(self, motors, level):
        self._log = Logger("roam", level)
        self._port_pid = motors._port_pid
        self._stbd_pid = motors._stbd_pid
        self._roam_port_complete = False
        self._roam_stbd_complete = False
        self._log.info('ready.')

    # ..........................................................................
    def _callback_port(self):
        self._log.info(Fore.RED   + 'port roam complete.')
        self._roam_port_complete = True

    # ..........................................................................
    def _callback_stbd(self):
        self._log.info(Fore.GREEN + 'stbd roam complete.')
        self._roam_stbd_complete = True

    # ..........................................................................
    def is_roaming(self):
        return not self._roam_port_complete and not self._roam_stbd_complete

    def _roam(self, velocity, direction, slew_rate, steps, callback, orientation):
        self._log.info('begin roaming...')
        for i in range(10):
            self._log.info(Fore.GREEN + 'roam [{:d}]'.format(i))
            if orientation is Orientation.PORT:
                self._port_pid.step_to(velocity, direction, slew_rate, steps)
            elif orientation is Orientation.STBD:
                self._stbd_pid.step_to(velocity, direction, slew_rate, steps)
            time.sleep(1.0)
        callback()
        self._log.info('stop roaming.')

    # ..........................................................................
    def roam(self):
        '''
            Begin roaming.
        '''
        self._log.info('roaming...')

        self._roam_port_complete = False
        self._roam_stbd_complete = False

        _forward_steps_per_rotation = 494
        _rotations = 2
        _forward_steps = _forward_steps_per_rotation * _rotations
        _velocity = 35.0 #Velocity.HALF
        _direction = Direction.FORWARD
        _slew_rate = SlewRate.SLOW
        self._log.info('start roaming at velocity: {}'.format(_velocity))
        _tp = Thread(target=self._roam, args=(_velocity, _direction, _slew_rate, _forward_steps, self._callback_port, Orientation.PORT))
        _ts = Thread(target=self._roam, args=(_velocity, _direction, _slew_rate, _forward_steps, self._callback_stbd, Orientation.STBD))
        _tp.start()
        _ts.start()
        _tp.join()
        _ts.join()
        self._log.info('complete.')