Ejemplo n.º 1
0
 def __init__(self):
     self.myDatabase = CardsDB()
     self.continueLaser = False
     self.cardId = -1
Ejemplo n.º 2
0
class LaserMon():
    def __init__(self):
        self.myDatabase = CardsDB()
        self.continueLaser = False
        self.cardId = -1

    def readCard(self):
        while True:
            rfid.waitTag()
            if rfid.readMifare():
                uid = rfid.getUniqueId()
                cid = self.myDatabase.cardExists(uid)
                if cid >= 0:
                    #print ("READ: Card found")
                    self.beepShort()
                    return cid
                else:
                    #print ("READ: Card NOT found")
                    self.beepLong()
                    return -1
                break
            else:
                return -1

    def checkCard(self):
        if rfid.tagIsPresent():
            if rfid.readMifare():
                uid = rfid.getUniqueId()
                cid = self.myDatabase.cardExists(uid)
                if cid >= 0:
                    #print ("CHECK: Card found")
                    return cid
                else:
                    #print ("CHECK: Card NOT found")
                    return -1
            else:
                return -1
        else:
            return -1

    def waitTillCardRemoved(self):
        while rfid.tagIsPresent():
            time.sleep(0.2)

    def beepCardLost(self):
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(0.05)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)
        time.sleep(0.02)
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(0.05)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)
        time.sleep(0.02)
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(0.2)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)

    def beepButtonPressNeeded(self):
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(0.07)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)
        time.sleep(0.08)
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(0.07)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)

    def beepShort(self):
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(0.08)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)

    def beepLong(self):
        GPIO.output(BUZZER_PIN, GPIO.LOW)
        time.sleep(1.5)
        GPIO.output(BUZZER_PIN, GPIO.HIGH)

    #  wait for a card to read
    #  if card -> start laser, init countr
    #  wait 1 Minute
    #  set alarm
    #
    #
    def run(self):

        secondsRunning = 60
        secondsWarning = 10
        usersname = self.myDatabase.get_fullname(self.cardId)
        mqttNotifyLaserHot(True, usersname)
        visualizeLaserHot()
        self.beepShort()
        self.laserOn()

        startTime = time.time()
        endTime = startTime
        self.myDatabase.log_card_activated(self.cardId)
        lostcounter = NUM_WARNINGS_CARD_LOST
        deadmanbutton_timeout_s = DEADMANBUTTON_TIMEOUT_S
        lowest_fraction = 1.0
        buttonPressDeadManSwitchDetected() # simulate Button Press (bad bad without lock... but really better not to use those in python callbacks)
        while True:
            time.sleep(LOOP_DELAY_S)
            fraction_time_remaining_deadmanbutton = 1.0 - (getSecondsSinceLastDeadmanButtonPress()/deadmanbutton_timeout_s)
            lowest_fraction = min(lowest_fraction, fraction_time_remaining_deadmanbutton)
            if lostcounter == NUM_WARNINGS_CARD_LOST:
                visualizeRemainingTimeFraction(fraction_time_remaining_deadmanbutton)
                if fraction_time_remaining_deadmanbutton < FRACTION_RED:
                    self.beepButtonPressNeeded()
            ## Check DeadMan Button
            if getSecondsSinceLastDeadmanButtonPress() > deadmanbutton_timeout_s:
                endTime = time.time()
                break
            ## if someone waits until the last second, he needs to press more often in the future
            if lowest_fraction == fraction_time_remaining_deadmanbutton and lowest_fraction < 0.10:
                deadmanbutton_timeout_s = DEADMANBUTTON_TIMEOUT_S / 2

            ## Check for Card
            cardId = self.checkCard()
            if cardId == self.cardId:
                # if card was lost and now is back, this also counts as button press
                if lostcounter < NUM_WARNINGS_CARD_LOST:
                    buttonPressDeadManSwitchDetected()
                lostcounter = NUM_WARNINGS_CARD_LOST
                continue

            # Card Lost !!
            if lostcounter == NUM_WARNINGS_CARD_LOST:
                endTime = time.time()
                visualizeCardLost()
            if lostcounter > 0:
                lostcounter -= 1
                ## last ditch rescue ability
                if getSecondsSinceLastDeadmanButtonPress() <= NUM_WARNINGS_CARD_LOST:
                    lostcounter = NUM_WARNINGS_CARD_LOST
                    continue
                if lostcounter < NUM_WARNINGS_CARD_LOST /2:
                    self.beepCardLost()
                continue

            # Lost counter == 0
            break
        # switch off laser
        visualizeLaserOff()
        self.beepLong()
        self.laserOff()

        # add minutes to card
        numberSeconds = int(endTime - startTime)
        self.myDatabase.log_card_finished(self.cardId, numberSeconds)
        print("%d: End cardID %d, seconds=%d" % (time.time(), myApp.cardId, numberSeconds))
        mqttNotifyLaserHot(False,usersname)

        # wait until card removed
        self.waitTillCardRemoved()

    def laserOff(self):
        GPIO.output(BUZZER_PIN, GPIO.HIGH)
        GPIO.output(LASER_PIN, GPIO.HIGH)

    def laserOn(self):
        GPIO.output(LASER_PIN, GPIO.LOW)