Esempio n. 1
0
    def __call__(self):
        if not self.playing:
            userlog = open("/home/pi/logs/user_log.csv", "a")
            # Lower the volume during quiet hours... Don't piss off the RA!
            self.mixer.setvolume(85 if quiet_hours() else 100)
            varID = self.ser.readline()
            print(varID)
            # mplayer will play any files sent to the FIFO file.
            if self.beep:
                self.write("loadfile", DING_SONG)
            if "ready" not in varID:
                # Turn the LEDs off
                LED.on(False)
                print("Turning off LEDs")
                # Get the username from the ibutton
                print("Getting User Data")
                uid, homedir = read_ibutton(varID)
                # Print the user's name (Super handy for debugging...)
                print("User: '******'\n")
                song = get_user_song(homedir, uid)
                print("Now playing '" + song + "'...\n")
                varID = varID[:-2]
                userlog.write("\n" + time.strftime('%Y/%m/%d %H:%M:%S') + "," + uid + "," + song)
                self.write("loadfile '" + song.replace("'", "\\'") + "'\nget_time_length",
                           delay=0.0)

                line = self.mpout.readline().strip()
                # timeout = time.time() + 5  # Five second time out to wait for song time.

                while not line.startswith("ANS_LENGTH="):
                    line = self.mpout.readline().strip()
                    if line.startswith("Playing"):  # Check if mplayer can play file.
                        if self.mpout.readline().strip() == "":
                            self.starttime = time.time()
                            self.endtime = time.time()
                            self.playing = True
                            break
                    elif line.startswith("ANS_LENGTH="):
                        duration = float(line.strip().split("=")[-1])
                        self.starttime = time.time()
                        self.endtime = time.time() + min(30, duration)
                        self.playing = True
                    else:
                        pass

                userlog.close()
        elif time.time() >= self.endtime:
            self.write("stop")
            self.playing = False
            self.ser.flushInput()
            LED.on(True)
            print("Stopped\n")

        elif time.time() >= self.starttime+28:
            # Fade out the music at the end.
            vol = int(self.mixer.getvolume()[0])
            while vol > 60:
                vol -= 1 + (100 - vol)/30.
                self.mixer.setvolume(int(vol))
                time.sleep(0.1)
Esempio n. 2
0
    def play(self, override = False):
        varID = self.ser.readline()
        if varID == None or varID == "":
            return
        uid, homedir = read_ibutton(varID)
        if uid in self.blacklist:
            print("user %s is blacklisted, not playing" % uid)
            return
        if not (not override or (override and uid in self.override)) or uid == self.current_user:
            print("user %s is not allowed to override" % uid)
            return
        print("User: '******'\n")
        self.current_user = uid
        # mplayer will play any files sent to the FIFO file.
        if self.beep:
            self.write("loadfile", DING_SONG)
        if "ready" not in varID:
            # Turn the LEDs off
            LED.on(False)
            # Get the username from the ibutton
            # uid, homedir = read_ibutton(varID)
            # Print the user's name (Super handy for debugging...)
            song = get_user_song(homedir, uid)
            print("Now playing '" + song + "'...\n")
            varID = varID[:-2]
            self.userlog.write("\n" + time.strftime('%Y/%m/%d %H:%M:%S') + "," + uid + "," + song)
            self.write("loadfile '" + song.replace("'", "\\'") + "'\nget_time_length",
                       delay=0.0)

            line = self.mpout.readline().strip()
            # timeout = time.time() + 5  # Five second time out to wait for song time.

            while not line.startswith("ANS_LENGTH="):
                line = self.mpout.readline().strip()
                if line.startswith("Playing"):  # Check if mplayer can play file.
                    if self.mpout.readline().strip() == "":
                        self.starttime = time.time()
                        self.endtime = time.time()
                        self.playing = True
                        break
                elif line.startswith("ANS_LENGTH="):
                    duration = float(line.strip().split("=")[-1])
                    self.starttime = time.time()
                    self.endtime = time.time() + min(30, duration)
                    self.playing = True
                else:
                    pass
Esempio n. 3
0
File: api.py Progetto: JDrit/Harold
def incoming_request(ibutton, song_id):
    inc_req = (ibutton, song_id)
    username, homedir = read_ibutton(inc_req[0])
    song_json = []

    if request.method == "GET":
        song_index = 0
        try:
            song_list = get_user_song(homedir, username, False)
        except:
            song_list = [False]

        try:
            if isinstance(song_list, list):
                for entry in song_list:
                    song_json.append(dict(id=song_index, name=basename(entry)))
                    song_index += 1
            else:
                song_json.append(dict(id=song_index, name=basename(song_list)))

            return jsonify(songs=song_json, user=username, status="true")

        except:
            song_json.append(dict(id=0, name="null"))
            return jsonify(songs=song_json, user=username, status="false")

    if request.method == "POST":
        try:
            user_dict = create_user_dict()
            print("User dict created")
            if username in user_dict:
                print("User found in dictionary!")
                set_song(username, song_id)
                print("Database updated.")
            else:
                print("User created in database.")
                create_user(username, song_id)
                print("Successful")
            return jsonify({"error": False})
        except:
            return jsonify({"error": True})
Esempio n. 4
0
def incoming_request(ibutton, song_id):
    inc_req = (ibutton, song_id)
    username, homedir = read_ibutton(inc_req[0])
    song_json = []

    if request.method == "GET":
        song_index = 0
        try:
            song_list = get_user_song(homedir, username, False)
        except:
            song_list = [False]

        try:
            if isinstance(song_list, list):
                for entry in song_list:
                    song_json.append(dict(id=song_index, name=basename(entry)))
                    song_index += 1
            else:
                song_json.append(dict(id=song_index, name=basename(song_list)))

            return jsonify(songs=song_json, user=username, status="true")

        except:
            song_json.append(dict(id=0, name="null"))
            return jsonify(songs=song_json, user=username, status="false")

    if request.method == "POST":
        try:
            user_dict = create_user_dict()
            print("User dict created")
            if username in user_dict:
                print("User found in dictionary!")
                set_song(username, song_id)
                print("Database updated.")
            else:
                print("User created in database.")
                create_user(username, song_id)
                print("Successful")
            return jsonify({"error": False})
        except:
            return jsonify({"error": True})