def __init__(self, signals, signalsLock):
        threading.Thread.__init__(self)
        self.signals = signals
        self.signalsLock = signalsLock
        self.sm = IPCMemory()
        self.smCounter = 0

        self.motionManager = MotionManager()
Esempio n. 2
0
def assignFunctionToMotion():
    motionName = request.args.get('motionName')
    functionName = request.args.get('functionName')

    motionManager = MotionManager()

    motion = motionManager.getMotion(motionName)
    function = getattr(motion.getAssignedDevice(), functionName)

    motion.assignFunction(function)
    motionManager.updateMotion(motion)

    return 'True'
Esempio n. 3
0
def assignDeviceToMotion():
    motionName = request.args.get('motionName')
    deviceName = request.args.get('deviceName')

    motionManager = MotionManager()
    deviceManager = DeviceManager()

    motion = motionManager.getMotion(motionName)
    device = deviceManager.getDevice(deviceName)

    motion.assignDevice(device)
    motionManager.updateMotion(motion)

    return 'True'
Esempio n. 4
0
    def __init__(self, signals, signalsLock):
        threading.Thread.__init__(self)

        self.signals = signals
        self.signalsLock = signalsLock
        self.signalsCounter = 0

        self.sm = IPCMemory()
        self.smCounter = 0

        self.motionManager = MotionManager()
        self.firstMotion = False

        ###
        #Teil der Bachelorarbeit
        self.subscriptionService = SubscriptionService()
Esempio n. 5
0
    def initManager(self):
        deviceManager = DeviceManager()
        deviceManager.initDevices()

        motionManager = MotionManager()
        motionManager.initMotions()
Esempio n. 6
0
class Executioner(threading.Thread):
    def __init__(self, signals, signalsLock):
        threading.Thread.__init__(self)

        self.signals = signals
        self.signalsLock = signalsLock
        self.signalsCounter = 0

        self.sm = IPCMemory()
        self.smCounter = 0

        self.motionManager = MotionManager()
        self.firstMotion = False

        ###
        #Teil der Bachelorarbeit
        self.subscriptionService = SubscriptionService()
        ###

    def run(self):
        print('Executioner is running')
        #Wenn keine Motions vorhanden sind
        if not self.motionManager.getAllMotions():
            self.firstMotion = True
            print("Bitte die erste Geste anlernen")
            self.startLearning()
        self.firstMotion = False
        self.startExecution()

    def startExecution(self):
        self.sm.put(IPCMemory.NEW_MOTION)
        #ggf kurze wartezeit um sicherzustellen dass der befehl auch ankommt?

        while True:
            self.checkSharedMemory()
            self.signalsLock.acquire()
            if not self.signalsCounter < len(self.signals):
                self.signalsLock.release()
                continue

            event = self.signals[self.signalsCounter]
            self.signalsCounter = self.signalsCounter + 1
            self.signalsLock.release()

            if isinstance(event, AboartEvent):
                ###
                #Bachelorarbeit
                self.removeRedundance()
                ###
                self.signalsLock.acquire()
                signalsCopy = self.signals[:]
                self.signalsLock.release()

                self.sm.put(IPCMemory.NEW_MOTION)

                self.startRecognition(signalsCopy)
            else:
                continue

    def startRecognition(self, signalsCopy):
        motion = self.transformMotion(signalsCopy)

        #Vergleiche die Motion mit allen anderen
        calculator = Calculator()
        knownMotions = self.motionManager.getAllMotions()
        bestMatch = None
        bestScore = None
        print('')
        for knownMotionName in knownMotions:
            knownMotion = self.motionManager.getMotion(knownMotionName)
            score = calculator.getMatchingScore(motion, knownMotion)

            print('{} - {} %'.format(knownMotionName, score))

            if bestMatch == None or score > bestScore:
                bestMatch = knownMotionName
                bestScore = score

        print('')
        print('Den besten Match gab es mit: {} - {} %'.format(
            bestMatch, bestScore))

        #Funktionalität der erkannten Geste ausführen
        print('Hier wird nun function.execute ausgeführt')
        print('')

        if bestMatch == 'learningMotion':
            print('lerne')
            self.startLearning()

        ###
        #Teil der Bachelorarbeit
        else:
            print('Firing new event')
            topic = 'newMotionEvent'
            values = dict()
            values['name'] = bestMatch
            event = Event(topic, values)
            self.subscriptionService.onEvent(event)
        ###

    def startLearning(self):
        #Send all Signals to reset Inputs
        #Zur Sicherheit hier einmal zuviel, damit wirklich
        #erst mit beginn des lernens die Signale aufgenommen werden
        self.sm.put(IPCMemory.NEW_MOTION)

        while True:
            self.checkSharedMemory()
            self.signalsLock.acquire()
            if not self.signalsCounter < len(self.signals):
                self.signalsLock.release()
                continue

            event = self.signals[self.signalsCounter]
            self.signalsCounter = self.signalsCounter + 1
            self.signalsLock.release()

            if isinstance(event, AboartEvent):
                ###
                #Bachelorarbeit
                self.removeRedundance()
                ###

                self.signalsLock.acquire()
                signalsCopy = self.signals[:]
                self.signalsLock.release()

                self.sm.put(IPCMemory.NEW_MOTION)

                self.transformAndSafeMotion(signalsCopy)
                break
            else:
                continue

    def transformAndSafeMotion(self, signalsCopy):
        motion = self.transformMotion(signalsCopy)

        if self.firstMotion:
            motion.setName('learningMotion')
        else:
            name = input('Wie heißt die Motion\n')
            motion.setName(name)

        self.motionManager.saveOrUpdateMotion(motion)

    def transformMotion(self, signalsCopy):
        del signalsCopy[len(signalsCopy) - 1]

        transformer = MotionTransformer()
        motion = transformer.transformMotion(signalsCopy)

        return motion

    ###
    #Bachelorarbeit
    def removeRedundance(self):
        self.signalsLock.acquire()
        #Remove aboartEvent
        del self.signals[-1]

        #Remove allAboartButtonEvents
        deleted = False
        i = -1
        while not deleted:
            if isinstance(self.signals[i],
                          ButtonEvent) and self.signals[i].value == 1:
                #print('Found ButtonEvent with Value {} @ {}'.format(self.signals[i].value, i))
                del self.signals[i]
                deleted = True
            else:
                i = i - 1

        deleted = False
        i = -1
        while not deleted:
            if isinstance(self.signals[i],
                          ButtonEvent) and self.signals[i].value == 0:
                #print('Found ButtonEvent with Value {} @ {}'.format(self.signals[i].value, i))
                del self.signals[i]
                deleted = True
            else:
                i = i - 1

        deleted = False
        i = -1
        while not deleted:
            if isinstance(self.signals[i],
                          ButtonEvent) and self.signals[i].value == 1:
                #print('Found ButtonEvent with Value {} @ {}'.format(self.signals[i].value, i))
                del self.signals[i]
                deleted = True
            else:
                i = i - 1

        self.signalsLock.release()

    ###

    def checkSharedMemory(self):
        import time
        if self.smCounter < self.sm.getSize():
            message = self.sm.get(self.smCounter)
            self.smCounter = self.smCounter + 1

            if message == IPCMemory.SHUTDOWN:
                print('I shall shutdown')
                time.sleep(2)
                sys.exit()
            elif message == IPCMemory.NEW_MOTION:
                self.signalsCounter = 0
class MotionDetecter(threading.Thread):

    MODE_RECOGNITION = 'recognition'
    MODE_LEARNING = 'learning'

    def __init__(self, signals, signalsLock):
        threading.Thread.__init__(self)
        self.signals = signals
        self.signalsLock = signalsLock
        self.sm = IPCMemory()
        self.smCounter = 0

        self.motionManager = MotionManager()

    def run(self):
        print("Motion Detecter is running")
        print(self.motionManager.getAllMotions())
        if not self.motionManager.getAllMotions():
            self.learnLearningMotion()
        self.startRecognition()

    def startRecognition(self):
        self.mode = self.MODE_RECOGNITION
        while True:
            self.waitForAboart()
            print('Geste wurde erkannt')
            self.sm.add(IPCMemory.RESET_ROTATION_SUM)

            transformer = MotionTransformer()
            motionToCompare = transformer.transformMotion(self.signalsCopy)

            c = Calculator()
            bestMotion = None
            bestScore = None
            for motion in self.motionManager.getAllMotions():
                matchingScore = c.getMatchingScore(self.motionManager.getMotion(motion), motionToCompare)
                print("Matching Score with '{}': {}".format(self.motionManager.getMotion(motion).getName(), matchingScore))

                if bestMotion == None:
                      bestScore = matchingScore
                      bestMotion = self.motionManager.getMotion(motion)
                      continue

                if matchingScore > bestScore:
                      bestScore = matchingScore
                      bestMotion = self.motionManager.getMotion(motion)

            if bestMotion == None:
                print("Es sind noch keine Motions angelernt")
            else:
                print("Motion {} für Device {} erkannt".format(bestMotion.getName(), bestMotion.getAssignedDevice()))

            if bestMotion.getName() == 'startLearning':
                self.sm.add(IPCMemory.START_LEARNING)

    def startLearning(self):
        self.mode = self.MODE_LEARNING
        self.signalsLock.acquire()
        del self.signals[:]
        self.signalsLock.release()

        self.waitForAboart()

        self.sm.add(IPCMemory.RESET_ROTATION_SUM)

        transformer = MotionTransformer()
        try :
            motion = transformer.transformMotion(self.signalsCopy)
        except NotEnoughSignals:
            print("Die Geste beinhaltet keine Aktionen. Sie wird nicht gespeichert")
            return

        name = input('Wie soll die Motion heißen?')
        motion.setName(name)

        self.motionManager.addMotion(motion)

        self.mode = self.MODE_RECOGNITION

    def learnLearningMotion(self):
        self.mode = self.MODE_LEARNING
        self.signalsLock.acquire()
        del self.signals[:]
        self.signalsLock.release()
        
        print('Jetzt bitte Geste ausführen mit der später neue Gesten angelernt werden sollen')

        self.waitForAboart()

        self.sm.add(IPCMemory.RESET_ROTATION_SUM)

        transformer = MotionTransformer()
        try :
            motion = transformer.transformMotion(self.signalsCopy)
        except NotEnoughSignals:
            print("Die Geste beinhaltet keine Aktionen. Sie wird nicht gespeichert")
            return

        motion.setName('startLearning')

        self.motionManager.addMotion(motion)

    def waitForAboart(self):
        print('Jetzt bitte Geste ausführen und mit Doppelklick bestätigen')
        counter = 0

        while True:
            self.checkSharedMemory()
            self.signalsLock.acquire()
            if len(self.signals) <= counter:
                self.signalsLock.release()
                continue
            event = self.signals[counter]

            if event != None and event.getEvent() == EVENT_ABOART:
                self.saveCopyOfSignals()
                self.removeAboartEvent(self.signalsCopy)
                self.removeNones(self.signalsCopy)
                del self.signals[:]
                self.signalsLock.release()
                break

            self.signalsLock.release()
            counter = counter + 1

    def removeAboartEvent(self, signals):
        for i in range(len(signals)-1, -1, -1):
            if signals[i].getEvent() == EVENT_ABOART:
                del signals[i]
                break

    #Signales that triggered the aboart are marked as None
    def removeNones(self, signals):
        for i in range(len(signals)-1, -1, -1):
            if signals[i] == None:
                del signals[i]

    #ATTENTION! LOCK MUST BE REQUIRED FIRST!!!
    def saveCopyOfSignals(self):
        self.signalsCopy = self.signals[:]

    def clearDoubleClick(self, signals):
        removedButtonEvents = 0
        for i in range(len(signals)-1, -1, -1):
            if removedButtonEvents == 4:
                break
            
            event = signals[i]
            if isinstance(event, ButtonEvent):
                del signals[i]
                removedButtonEvents = removedButtonEvents + 1

    def checkSharedMemory(self):
        import time
        #print('Checking Size')
        #print('SmCounter = {}'.format(self.smCounter))
        #print('SmSize = {}'.format(self.sm.getSize()))
        if self.smCounter < self.sm.getSize():
            message = self.sm.get(self.smCounter)
            #print(message)

            self.smCounter = self.smCounter + 1

            if message == IPCMemory.SHUTDOWN:
                print('I shall shutdown')
                time.sleep(2)
                sys.exit()
            elif message == IPCMemory.START_LEARNING:
                if self.mode == self.MODE_LEARNING:
                    print("I'm already learning")
                else:
                    print('I shall start learning')
                    time.sleep(2)
                    self.startLearning()
            elif message == IPCMemory.START_RECOGNIZING:
                if self.mode == self.MODE_RECOGNITION:
                    print("I'm already recognizing")
                else:
                    print('I shall start recognition')
                    time.sleep(2)
                    self.startRecognition()