Beispiel #1
0
 def expired(self, name, delay, curr_tm=None):
     """ @return True, if delay was passed from set_tm() call
     """
     if curr_tm is None:
         curr_tm = uptime()
     last_tm = self.get_tm(name)
     return last_tm is None or last_tm + delay <= curr_tm
Beispiel #2
0
def main():
    global app_ret, app_ev

    app_ret = 0
    app_ev.clear()
    logger.info("###Starting CM sample ver %s###", NVRClient.version)
    try:
        cli = NVRClient()
        last_time = uptime()
        while app_ret == 0:
            cli.stat.clear_tm()
            cli.thread_run = threading.Thread(target=cli.run)
            cli.thread_run.setDaemon(True)
            cli.thread_run.start()
            try:
                # On Windows join w/o timeout is not interruptable
                # On Linux it is found that join is not interruptable so use event instead
                while app_ret == 0 and cli.thread_run.isAlive():
                    app_ev.wait(3)
            except IOError as e:
                if e.errno != errno.EINTR:
                    raise
            cli.stop()

            if app_ret:  # signal received or error
                break

            check_time = uptime()
            if cli.stat.get_tm("connect"):
                last_time = cli.stat.get_tm("disconnect", last_time)

            # No connection for 5 min
            if check_time - last_time > 300:
                app_ret = 6

            if app_ret == 0:
                time.sleep(2)  # dont restart too fast

        cli.camera.disconnect_all_streams()

    finally:
        pass

    logger.info("### Exiting, ret = %d ###", app_ret)
    sys.exit(app_ret)
Beispiel #3
0
def command_status(message):
    chat_id = message.chat.id
    if chat_id in ADMIN_IDS:
        chats_number = execute_sql('SELECT count(1) from chat')[0][0]
        bot.send_message(
            chat_id,
            '\n'.join([
                uptime(bot_start_time, datetime.datetime.utcnow()),
                'Number of subscriptions: {}'.format(chats_number)
            ])
        )
    else:
        bot.send_message(chat_id, 'Everything is ok. Stay in touch.')
Beispiel #4
0
    def get_tosend(self, readings):
        tosend = {}

        tosend['fridge_on'] = self.server.fridge.is_on()

        tosend['now'] = self.server.now()
        tosend['readings'] = readings

        tosend['wort_name'] = self.server.wort_name
        tosend['fridge_name'] = self.server.wort_name

        tosend['current_params'] = dict(self.server.params)
        tosend['current_params_epoch'] = self.server.params.get_epoch()

        tosend['start_time'] = self.server.start_time
        tosend['uptime'] = utils.uptime()

        return tosend
Beispiel #5
0
    def get_tosend(self, readings):
        tosend = {}

        tosend['fridge_on'] = self.server.fridge.is_on()

        tosend['now'] = self.server.now()
        tosend['readings'] = readings

        tosend['wort_name'] = self.server.wort_name
        tosend['fridge_name'] = self.server.wort_name

        tosend['current_params'] = dict(self.server.params)
        tosend['current_params_epoch'] = self.server.params.get_epoch()

        tosend['start_time'] = self.server.start_time
        tosend['uptime'] = utils.uptime()

        return tosend
Beispiel #6
0
    def _ev_loop(self):
        """
        Thread for timeouts checking
        """
        curr_tid = threading.current_thread()
        logger.info("Event loop is started")
        while self.thread_ev_loop == curr_tid:
            time.sleep(1)
            if curr_tid != self.thread_ev_loop:
                break

            try:
                tm = uptime()

                # If not connected or error is reported, stop after 5 minutes:
                if self.stat.expired("started", 300, tm):
                    connect_timeout = self.stat.get_tm("connect")
                    if not connect_timeout:
                        if connect_timeout is None:
                            connect_timeout = self.stat.get_tm("started")
                        logger.warning("Connection is not started after >5mins(%s) since application started",
                                       connect_timeout)
                        self.stop()
                        break

                if self.conn_state < self.CONN_CONNECTED:
                    continue

                if self.stat.expired("command", self.ping_interval, tm):
                    # haven't send/received commands for a long time, suspicious
                    self._cmd_ping()

                if self.dbg_event_interval > 0 and self.stat.expired('dbg_event', self.dbg_event_interval, tm):
                    self.debug_generate_camera_event()

            except:
                logger.error('Event loop error %s', error_str())

        logger.info("Event loop is stopped")
    #"!currqueue": lambda s,**kw   : utils.get_queue(s),
    # Music commands
    "sr":
    lambda s, u, m, **kw: song_request(s, u, m),
    "currentsong":
    lambda s, u, **kw: utils.current_song_chat(s, u),
    "skip":
    lambda s, u, proc, **kw: utils.skip_song(s, u, proc),
    "cp":
    lambda s, u, m, proc, **kw: utils.change_playlist(s, u, m, proc),
    "playlists":
    lambda s, u, m, **kw: utils.list_playlists(s, u),
    # Stream commands
    #"!startstream" : lambda s,u,**kw: start_stream(u),
    "uptime":
    lambda s, **kw: utils.uptime(s),
    "earthfall":
    lambda s, **kw: utils.chat(
        s,
        "Want to play Earthfall with us? Help support us as influencers by purchasing through our link! https://bit.ly/2NYsZm2"
    ),
    "so":
    lambda s, u, m, **kw: utils.shoutout(s, u, m)
    #"!togglesr": lambda s,u,m,**kw: toggle_sr(s,u,m)
    #"!commands": lambda s,**kw: chat_commands(s)
}

download_queue = []


def toggle_sr(s, u, m):
 def test_runtime(self, start, check, uptime_str):
     assert uptime(start, check) == uptime_str
Beispiel #9
0
 def set_tm(self, name, curr_tm=None):
     if curr_tm is None:
         curr_tm = uptime()
     self.stat[name] = curr_tm
def uptime():
    return utils.uptime()
Beispiel #11
0
    # init adafruit
    adafruit = init_adafruit(parser)

    # init the sensors
    sensors = init_sensors(parser)

    # init the powerswitch
    powerswitch = init_power_switch(parser)

    # init gmail
    gmail = init_gmail(parser)

    # init adafruit
    consts = init_parameters(parser)

    gmail.send("Program starting. Machine uptime %s" % utils.uptime())

    for iteration in range(consts.MAX_ITERATIONS):
        measurements = 0
        try:
            while True:
                temperatures = process_sensors(sensors, adafruit)
                process_powerswitch(powerswitch, temperatures, gmail)

                time.sleep(consts.MEASUREMENT_INTERVAL)
                measurements += 1
        except Exception, args:
            message = "Error at iteration %d, measurements %d %r" \
               % (iteration, measurements, args)
            gmail.send(message)
            logging.exception(message)
Beispiel #12
0
def system():
    time = uptime()
    template_data = {'title': 'System', 'time': time}
    return render_template("system.html", **template_data)