Exemplo n.º 1
0
 def trackModeChanged(self):
     if self.ui.trackMode.isChecked():
         self.track = RepeatTimer(5.0, self.tracking)
         self.track.start()
         logging.debug("Track mode ON")
     else:
         self.track.cancel()
         logging.debug("Track mode Off")
Exemplo n.º 2
0
    def __init__(self):
        """ Initialize sensor readings and led animations """

        #initialize ping sensor and register callback
        self.sensor = PingSensor(self.setDistance)

        #initialize neopixel object
        self.strip = Adafruit_NeoPixel(LEDShow.LED_COUNT, LEDShow.LED_PIN,
                                       LEDShow.LED_FREQ_HZ, LEDShow.LED_DMA,
                                       LEDShow.LED_INVERT,
                                       LEDShow.LED_BRIGHTNESS)
        self.strip.begin()

        #grid conversion
        self.grid = utils.stripToGrid(LEDShow.LED_COUNT, 8)

        ###SHUFFLE THESE!!!
        #list of animations to cycle through
        self.animations = [
            AnimationWipe(self),
            AnimationPulse(self),
            AnimationLeap(self),
            AnimationMirror(self),
            AnimationDuel(self),
            AnimationWave(self),
            AnimationSquares(self)
        ]

        #initialized animation index
        self.animationIndex = -1

        #animation duration timer
        if len(self.animations) > 1:
            self.timer = RepeatTimer()
            self.timer.start(LEDShow.ANIMATION_DURATION, self.nextAnimation)

        #initialize animations
        self.currentAnimation = None
        self.nextAnimation()
        self.startPingInterval = time.time()

        #ping loop
        try:
            GPIO.setmode(GPIO.BOARD)
            self.sensor.ping()

            #emit ping every interval defined by the animation
            while (True):
                self.endPingInterval = time.time() - self.startPingInterval
                if (self.endPingInterval >= self.pingInterval):
                    self.sensor.ping()

        #cleanup
        finally:
            GPIO.cleanup()
            self.clearPixels()
Exemplo n.º 3
0
 def getTimer(self):
     if self.repeat_timer is None:
         self.repeat_timer = RepeatTimer(self.interval,
                                     self.checkServers,
                                     *args,
                                     **kwargs)
Exemplo n.º 4
0
    def do_main_program(self):

        self.running = True
        print("do_main_program(): Spoustim sluzbu...", flush=True)
        ntf.notify("READY=1")
        ntf.notify("STATUS=Inicializuji sluzbu HMP...")

        conn = self.get_DB()
        c = conn.cursor()

        self.sett = {}
        for r in c.execute("select param, sval from setting"):
            self.sett[r[0]] = r[1]

        self.tasks.queue.clear()
        self.resp.queue.clear()

        c.execute(
            "update inprogress set probiha = 0 where akce='NACTENI_STRUKTURY_ZAPOJENI'"
        )
        c.execute(
            "update inprogress set probiha = 0, sval='Služba spuštěna' where akce='HMP_DAEMON'"
        )

        conn.commit()

        self.check_serial_comm()

        #spust GSM modul pokud je potreba
        try:
            if int(self.sett["USE_GSM_MODULE"]) == 1:
                self.gsmth = GsmDaemonTask()
                self.gsmth.start(
                    self.sett["GSM_USBINTERFACE"], self.sett["GSM_HW_ADDR"],
                    self.sett["GSM_SIM_PIN"], self.sett["GSM_APN_NAME"],
                    self.sett["GSM_APN_USER"], self.sett["GSM_APN_PASS"],
                    int(self.sett["GSM_WATCHDOG_TIMEINTERVAL"]),
                    self.sett["GSM_WATCHDOG_PING_SERVER"])
        except:
            pass

        self.tlast = time.time()
        self.thKeepAlive = RepeatTimer(15, self.keep_alive)  # kontrola co 15s

        if int(self.sett["CLOUD_SEND_14H_INTERVAL_SEC"]) > 0:
            self.thCloudExportHistory = RepeatTimer(
                int(self.sett["CLOUD_SEND_14H_INTERVAL_SEC"]),
                self.cloud_export_history)

        if int(self.sett["CLOUD_SEND_LIVE_INTERVAL_SEC"]) > 0:
            self.thCloudExportLive = RepeatTimer(
                int(self.sett["CLOUD_SEND_LIVE_INTERVAL_SEC"]),
                self.cloud_export_live)

        self.thHistoryDownload = RepeatTimer(
            int(self.sett["SERIAL_DOWNLOAD_HISTORY_INTERVAL_SEC"]),
            self.download_history)  # kontrola co 900s

        self.thKonstantyRefresh = RepeatTimer(910, self.reload_konstanty)
        self.thClearHistory = RepeatTimer(86400, self.clean_history)

        self.clean_history()
        self.reload_konstanty()
        self.download_history()

        while self.running:

            try:

                #posbirej dotazy na hmp zarizeni a posli je do 485 pouze 5 zaznamu
                for q in c.execute(
                        "select id, prikaz, typ_dotazu, isql, iscmd, iswrite from serial_prikaz where odpoved is null and islock=0 order by id asc limit 8"
                ).fetchall():

                    if q[4] == 1:  #jedna se o prikaz pro sluzbu, provadi se ihned
                        if q[1] == "FLUSH_QUEQUE":
                            with self.tasks.mutex:
                                self.tasks.queue.clear()
                        elif q[1] == "RESTART":
                            with self.tasks.mutex:
                                self.tasks.queue.clear()
                            self.running = False
                            self.restart()

                    elif q[3] == 1:  #jedna se o sql prikaz ten se provadi FIFO
                        self.tasks.put({
                            'data': q[1],
                            'id': q[0],
                            'typ': q[2],
                            'sql': 1,
                            'wr': 0
                        })
                    else:
                        bf = conv_str_hex_in_bytes(q[1])
                        self.tasks.put({
                            'data': bf,
                            'id': q[0],
                            'typ': q[2],
                            'sql': 0,
                            'wr': q[5]
                        })

                    c.execute("update serial_prikaz set islock=1 where id=?",
                              (q[0], ))
                    conn.commit()

                self.check_serial_comm()
                ntf.notify("WATCHDOG=1")

                while self.resp.qsize() > 0:  # odpoved od HMP
                    rs = self.resp.get()

                    if rs['sql'] == 1:
                        c.execute(rs['data'])
                        c.execute(
                            "update serial_prikaz set odpoved='sql_done', cas_odpoved=CURRENT_TIMESTAMP where id=?",
                            (rs['id'], ))
                        conn.commit()
                        self.resp.task_done()
                        continue

                    #zaznamenej cas kdy naposledy odpovedel
                    dfrom = telegram_get_from(rs['data'])
                    try:
                        if rs['id'] > 0:
                            self.tlast = time.time()
                            c.execute(
                                "update serial_prikaz set odpoved=?, cas_odpoved=CURRENT_TIMESTAMP where id=?",
                                (
                                    conv_hex_to_str(rs['data']),
                                    rs['id'],
                                ))
                            conn.commit()
                            fce = switcher_process(rs['typ'])
                            fce(c, rs['data'], dfrom)

                        else:  #aktualni bezny datagram spotreba
                            fce = switcher_process(-1)  #vychozi
                            fce(c, rs['data'], dfrom)

                    except Exception as e:
                        print(
                            "do_main_program()[call]: Ex: {}, Typ: {}, Id: {}, Data: {}"
                            .format(str(e), rs['typ'], rs['id'],
                                    conv_hex_to_str(rs['data'])),
                            flush=True)
                        pass

                    conn.commit()
                    self.resp.task_done()
            except sqlite3.OperationalError as ex:
                ntf.notify("ERRNO=1")

            except Exception as e:
                print(
                    "do_main_program()[2]: Ex: {}, Typ: {}, Id: {}, Data: {}".
                    format(str(e), rs['typ'], rs['id'],
                           conv_hex_to_str(rs['data'])),
                    flush=True)
                pass

            time.sleep(0.1)

        conn.close()
        #ukonci komunikaci sluzba nebezi
        self.sth.running = False
        print("do_main_program(): Konec sluzby...", flush=True)
        ntf.notify("STOPPING=1")