Exemple #1
0
def readGroupConfig(data):
    name, data = readString(data)
    mInt, data = readInt(data)
    master = mInt == 1
    print "Updating Group Configuration:"
    print "Name: %s" % (name)
    print "Master: ", master
    configtool.setGroupConfigValue("group", name)
    configtool.setGroupConfigValue("group_master", mInt)
    gConf = configtool.readGroupConfig()
    GroupManager.ReInitGroupManager(gConf)
def readGroupConfig(data):
    name, data = readString(data)
    mInt, data = readInt(data)
    master = mInt == 1
    print "Updating Group Configuration:"
    print "Name: %s" % (name)
    print "Master: ", master
    configtool.setGroupConfigValue("group", name)
    configtool.setGroupConfigValue("group_master", mInt)
    gConf = configtool.readGroupConfig()
    GroupManager.ReInitGroupManager(gConf)
Exemple #3
0
def getGroupConfigMessage():
    config = configtool.readGroupConfig()
    configStr = str(config)
    confBytes = bytearray(configStr)

    data = bytearray()
    size = 10 + len(confBytes)
    appendInt(data, size)
    appendShort(data, GROUP_CONFIG_REQUEST)
    appendInt(data, len(confBytes))
    appendBytes(data, confBytes)

    return data
Exemple #4
0
def interpret(msg_data, sender_ip=None):
    #print "Interpreting incoming data..."
    if str(msg_data).startswith("rm"):
        # string interpretation
        result = INTERPRETER_SUCCESS
        msg = None
        values = str(msg_data).split(":")
        cmd = values[1]
        if cmd == "state":
            val = values[2]
            # switch state
            mediaplayer.setState(int(val))
        elif cmd == "number":
            val = values[2]
            mediaplayer.setMediaFileNumber(int(val))
        elif cmd == "next":
            mediaplayer.nextFile()
        elif cmd == "prev":
            mediaplayer.prevFile()
        elif cmd == "config":
            # config must be sent as key/value pair
            if len(values) == 4:
                key = values[2]
                value = values[3]
                print "New Key/Value Pair:"
                print "KEY: ", key
                print "VALUE: ", value
                configtool.setConfigValue(key, value)
        elif cmd == "hdmi":
            val = values[2]
            os.system(os.getcwd() + "/scripts/rpi-hdmi.sh " + val)
        else:
            result = INTERPRETER_ERROR
            msg = "Unknown UDP string command"
    else:
        data = bytearray(msg_data)
        size, data = readInt(data)
        #print "Size: " + str(size)

        flag, data = readShort(data)
        result = flag
        msg = None
        #print "Flag: " + str(flag)
        if flag == CONFIG_UPDATE:
            data = readConfigUpdate(data)
        elif flag == PLAYER_START:
            print 'UDP COMMAND Mediaplayer start...'
            mediaplayer.setState(1)
        elif flag == PLAYER_STOP:
            print 'UDP COMMAND Mediaplayer stop...'
            mediaplayer.setState(0)
        elif flag == PLAYER_PAUSE:
            mediaplayer.setState(3)
        elif flag == PLAYER_RESTART:
            print 'UDP COMMAND Mediaplayer restart...'
            mediaplayer.playerState = PLAYER_STOPPED
            mediaplayer.stop()
            time.sleep(2)
            mediaplayer.playerState = PLAYER_STARTED
            mediaplayer.play()
        elif flag == SERVER_REQUEST:
            data = None
            result = SERVER_REQUEST
        elif flag == FILELIST_REQUEST:
            result = FILELIST_REQUEST
        elif flag == FILELIST_RESPONSE:
            readFileList(data)
            result = FILELIST_REQUEST
        elif flag == CONFIG_REQUEST:
            result = CONFIG_REQUEST
        elif flag == DELETE_FILE:
            numFiles, data = readInt(data)
            files = []
            for i in range(numFiles):
                msg, data = readString(data)
                if msg:
                    files.append(msg)
            mediaplayer.deleteFiles(files)
        elif flag == DELETE_ALL_IMAGES:
            files = mediaplayer.getImageFilelist()
            mediaplayer.deleteFiles(files)
        elif flag == PLAYER_IDENTIFY:
            print 'Showing identify image...'
            mediaplayer.identifySelf()
        elif flag == PLAYER_IDENTIFY_DONE:
            print 'Identifying done...'
            mediaplayer.identifyDone()
        elif flag == PLAYER_REBOOT:
            os.system("sudo reboot")
        elif flag == PLAYER_UPDATE:
            if is_connected():
                os.system("/home/pi/raspmedia/Raspberry/scripts/update.sh")
            else:
                result = PLAYER_UPDATE_ERROR
                msg = "Player is not connected to the internet."
        elif flag == WIFI_CONFIG:
            setupWifi(data)
        ### GROUP AND ACTION MESSAGE FLAGS ###
        elif flag == GROUP_CONFIG_REQUEST:
            result = flag
        elif flag == GROUP_MEMBER_REQUEST:
            group, data = readString(data)
            GroupManager.MemberRequest(group, sender_ip)
        elif flag == GROUP_MEMBER_ACKNOWLEDGE:
            group, data = readString(data)
            byRequest, data = readInt(data)
            GroupManager.MemberAcknowledge(group, sender_ip, byRequest)
        elif flag == GROUP_CONFIG:
            result = flag
            readGroupConfig(data)
        elif flag == GROUP_DELETE:
            result = flag
            groupName, data = readString(data)
            if configtool.readGroupConfig()['group'] == groupName:
                configtool.resetGroupConfig()
                gConf = configtool.readGroupConfig()
                GroupManager.ReInitGroupManager(gConf)
        elif flag == GROUP_CONFIG_ADD_ACTION:
            result = flag
            msg, data = readString(data)
        elif flag == GROUP_CONFIG_ACTION_DELETE:
            result = flag
            msg, data = readString(data)
        elif flag == FILE_DATA_REQUEST:
            result = flag
        elif flag == PLAYER_START_FILENUMBER:
            print "UDP COMMAND Start Filenumber..."
            number, data = readInt(data)
            mediaplayer.startFileNumber(number)
        elif flag == DISK_INFO_REQUEST:
            result = flag
        else:
            result = INTERPRETER_ERROR
            msg = "Unknown command flag"

        #print "Remaining data: " + data.decode("utf-8")

    return result, msg
Exemple #5
0
    def handle(self):
        data = self.request[0]
        inData = self.request[0].strip()
        cSocket = self.request[1]
        curThread = threading.current_thread()
        if not self.client_address[0] in netutil.ip4_addresses():
            result, msg = interpreter.interpret(data, self.client_address[0])


            if result == SERVER_REQUEST:
                freeSpace = self.FreeDiskSpace()
                responseData = messages.getMessage(SERVER_REQUEST_ACKNOWLEDGE, ["-i", str(TYPE_RASPMEDIA_PLAYER), "-i", "0","-s", str(configtool.readConfig()['player_name']), "-i", str(freeSpace[0]), "-i", str(freeSpace[1])])
                addr = (self.client_address[0], UDP_PORT)
                print "Sending response to client:"
                print (addr)
                if cSocket.sendto(responseData, addr):
                    print "Response sent!"
                else:
                    print "Sending response failed!"
            elif result == FILELIST_REQUEST:
                files = mediaplayer.getMediaFileList()
                args = ['-i', str(len(files))]
                for file in files:
                    args.append('-s')
                    args.append(file)
                responseData = messages.getMessage(FILELIST_RESPONSE,args)
                if cSocket.sendto(responseData, (self.client_address[0], UDP_PORT)):
                    print "Filelist sent!"
            elif result == CONFIG_REQUEST:
                responseData = messages.getConfigMessage()
                if cSocket.sendto(responseData, (self.client_address[0], UDP_PORT)):
                    print "Config sent!"
            elif result == GROUP_CONFIG_REQUEST:
                response = messages.getGroupConfigMessage()
                if cSocket.sendto(response, (self.client_address[0], UDP_PORT)):
                    print "Group Config sent!"
            elif result == GROUP_CONFIG_ADD_ACTION:
                    configtool.addGroupAction(msg)
                    gConf = configtool.readGroupConfig()
                    response = messages.getMessage(GROUP_CONFIG_ADD_ACTION)
                    if cSocket.sendto(response, (self.client_address[0], UDP_PORT)):
                        print "Action saved confirmation sent!"
                    GroupManager.ReInitGroupManager(gConf)
            elif result == GROUP_CONFIG_ACTION_DELETE:
                    configtool.deleteGroupAction(msg)
                    gConf = configtool.readGroupConfig()
                    response = messages.getMessage(GROUP_CONFIG_ACTION_DELETE)
                    if cSocket.sendto(response, (self.client_address[0], UDP_PORT)):
                        print "Action deleted confirmation sent!"
                    GroupManager.ReInitGroupManager(gConf)
            elif result == PLAYER_UPDATE_ERROR:
                responseData = messages.getMessage(PLAYER_UPDATE_ERROR, ["-s", str(msg)])
                cSocket.sendto(responseData, (self.client_address[0], UDP_PORT))
            elif result == FILE_DATA_REQUEST:
                # send images from player over tcp to host in separate thread to not block other udp handling
                t = threading.Thread(target=self.SendImagesOverTCP, args=[self.client_address[0]])
                t.daemon = True
                t.start()
            elif result == DISK_INFO_REQUEST:
                freeSpace = self.FreeDiskSpace()
                responseData = messages.getMessage(DISK_INFO_REQUEST, ["-i", str(freeSpace[0]), "-i", str(freeSpace[1])])
                addr = (self.client_address[0], UDP_PORT)
                if cSocket.sendto(responseData, addr):
                    print "Disk Info sent!"
                else:
                    print "Sending disk info failed!"
        else:
            print "Received own broadcast... ignored."
def interpret(msg_data, sender_ip=None):
    #print "Interpreting incoming data..."
    if str(msg_data).startswith("rm"):
        # string interpretation
        result = INTERPRETER_SUCCESS
        msg = None
        values = str(msg_data).split(":")
        cmd = values[1]
        if cmd == "state":
            val = values[2]
            # switch state
            mediaplayer.setState(int(val))
        elif cmd == "number":
            val = values[2]
            mediaplayer.setMediaFileNumber(int(val))
        elif cmd == "next":
            mediaplayer.nextFile()
        elif cmd == "prev":
            mediaplayer.prevFile()
        elif cmd == "config":
            # config must be sent as key/value pair
            if len(values) == 4:
                key = values[2]
                value = values[3]
                print "New Key/Value Pair:"
                print "KEY: ", key
                print "VALUE: ", value
                configtool.setConfigValue(key, value)
        elif cmd == "hdmi":
            val = values[2]
            os.system(os.getcwd()+"/scripts/rpi-hdmi.sh "+val)
        else:
            result = INTERPRETER_ERROR
            msg = "Unknown UDP string command"
    else:
        data = bytearray(msg_data)
        size, data = readInt(data)
        #print "Size: " + str(size)

        flag, data = readShort(data)
        result = flag;
        msg = None
        #print "Flag: " + str(flag)
        if flag == CONFIG_UPDATE:
            data = readConfigUpdate(data)
        elif flag == PLAYER_START:
            print 'UDP COMMAND Mediaplayer start...'
            mediaplayer.setState(1)
        elif flag == PLAYER_STOP:
            print 'UDP COMMAND Mediaplayer stop...'
            mediaplayer.setState(0)
        elif flag == PLAYER_PAUSE:
            mediaplayer.setState(3)
        elif flag == PLAYER_RESTART:
            print 'UDP COMMAND Mediaplayer restart...'
            mediaplayer.playerState = PLAYER_STOPPED
            mediaplayer.stop()
            time.sleep(2)
            mediaplayer.playerState = PLAYER_STARTED
            mediaplayer.play()
        elif flag == SERVER_REQUEST:
            data = None
            result = SERVER_REQUEST
        elif flag == FILELIST_REQUEST:
            result = FILELIST_REQUEST
        elif flag == FILELIST_RESPONSE:
            readFileList(data)
            result = FILELIST_REQUEST
        elif flag == CONFIG_REQUEST:
            result = CONFIG_REQUEST
        elif flag == DELETE_FILE:
            numFiles, data = readInt(data)
            files = []
            for i in range(numFiles):
                msg, data = readString(data)
                if msg:
                    files.append(msg)
            mediaplayer.deleteFiles(files)
        elif flag == DELETE_ALL_IMAGES:
            files = mediaplayer.getImageFilelist()
            mediaplayer.deleteFiles(files)
        elif flag == PLAYER_IDENTIFY:
            print 'Showing identify image...'
            mediaplayer.identifySelf()
        elif flag == PLAYER_IDENTIFY_DONE:
            print 'Identifying done...'
            mediaplayer.identifyDone()
        elif flag == PLAYER_REBOOT:
            os.system("sudo reboot")
        elif flag == PLAYER_UPDATE:
            if is_connected():
                os.system("/home/pi/raspmedia/Raspberry/scripts/update.sh")
            else:
                result = PLAYER_UPDATE_ERROR
                msg = "Player is not connected to the internet."
        elif flag == WIFI_CONFIG:
            setupWifi(data)
        ### GROUP AND ACTION MESSAGE FLAGS ###
        elif flag == GROUP_CONFIG_REQUEST:
            result = flag
        elif flag == GROUP_MEMBER_REQUEST:
            group, data = readString(data)
            GroupManager.MemberRequest(group, sender_ip)
        elif flag == GROUP_MEMBER_ACKNOWLEDGE:
            group, data = readString(data)
            byRequest , data = readInt(data)
            GroupManager.MemberAcknowledge(group, sender_ip, byRequest)
        elif flag == GROUP_CONFIG:
            result = flag
            readGroupConfig(data)
        elif flag == GROUP_DELETE:
            result = flag
            groupName, data = readString(data)
            if configtool.readGroupConfig()['group'] == groupName:
                configtool.resetGroupConfig()
                gConf = configtool.readGroupConfig()
                GroupManager.ReInitGroupManager(gConf)
        elif flag == GROUP_CONFIG_ADD_ACTION:
            result = flag
            msg, data = readString(data)
        elif flag == GROUP_CONFIG_ACTION_DELETE:
            result = flag
            msg, data = readString(data)
        elif flag == FILE_DATA_REQUEST:
            result = flag
        elif flag == PLAYER_START_FILENUMBER:
            print "UDP COMMAND Start Filenumber..."
            number, data = readInt(data)
            mediaplayer.startFileNumber(number)
        elif flag == DISK_INFO_REQUEST:
            result = flag
        else:
            result = INTERPRETER_ERROR
            msg = "Unknown command flag"

        #print "Remaining data: " + data.decode("utf-8")

    return result, msg
Exemple #7
0
    def handle(self):
        data = self.request[0]
        inData = self.request[0].strip()
        cSocket = self.request[1]
        curThread = threading.current_thread()
        if not self.client_address[0] in netutil.ip4_addresses():
            result, msg = interpreter.interpret(data, self.client_address[0])

            if result == SERVER_REQUEST:
                freeSpace = self.FreeDiskSpace()
                responseData = messages.getMessage(
                    SERVER_REQUEST_ACKNOWLEDGE, [
                        "-i",
                        str(TYPE_RASPMEDIA_PLAYER), "-i", "0", "-s",
                        str(configtool.readConfig()['player_name']), "-i",
                        str(freeSpace[0]), "-i",
                        str(freeSpace[1])
                    ])
                addr = (self.client_address[0], UDP_PORT)
                print "Sending response to client:"
                print(addr)
                if cSocket.sendto(responseData, addr):
                    print "Response sent!"
                else:
                    print "Sending response failed!"
            elif result == FILELIST_REQUEST:
                files = mediaplayer.getMediaFileList()
                args = ['-i', str(len(files))]
                for file in files:
                    args.append('-s')
                    args.append(file)
                responseData = messages.getMessage(FILELIST_RESPONSE, args)
                if cSocket.sendto(responseData,
                                  (self.client_address[0], UDP_PORT)):
                    print "Filelist sent!"
            elif result == CONFIG_REQUEST:
                responseData = messages.getConfigMessage()
                if cSocket.sendto(responseData,
                                  (self.client_address[0], UDP_PORT)):
                    print "Config sent!"
            elif result == GROUP_CONFIG_REQUEST:
                response = messages.getGroupConfigMessage()
                if cSocket.sendto(response,
                                  (self.client_address[0], UDP_PORT)):
                    print "Group Config sent!"
            elif result == GROUP_CONFIG_ADD_ACTION:
                configtool.addGroupAction(msg)
                gConf = configtool.readGroupConfig()
                response = messages.getMessage(GROUP_CONFIG_ADD_ACTION)
                if cSocket.sendto(response,
                                  (self.client_address[0], UDP_PORT)):
                    print "Action saved confirmation sent!"
                GroupManager.ReInitGroupManager(gConf)
            elif result == GROUP_CONFIG_ACTION_DELETE:
                configtool.deleteGroupAction(msg)
                gConf = configtool.readGroupConfig()
                response = messages.getMessage(GROUP_CONFIG_ACTION_DELETE)
                if cSocket.sendto(response,
                                  (self.client_address[0], UDP_PORT)):
                    print "Action deleted confirmation sent!"
                GroupManager.ReInitGroupManager(gConf)
            elif result == PLAYER_UPDATE_ERROR:
                responseData = messages.getMessage(PLAYER_UPDATE_ERROR,
                                                   ["-s", str(msg)])
                cSocket.sendto(responseData,
                               (self.client_address[0], UDP_PORT))
            elif result == FILE_DATA_REQUEST:
                # send images from player over tcp to host in separate thread to not block other udp handling
                t = threading.Thread(target=self.SendImagesOverTCP,
                                     args=[self.client_address[0]])
                t.daemon = True
                t.start()
            elif result == DISK_INFO_REQUEST:
                freeSpace = self.FreeDiskSpace()
                responseData = messages.getMessage(
                    DISK_INFO_REQUEST,
                    ["-i", str(freeSpace[0]), "-i",
                     str(freeSpace[1])])
                addr = (self.client_address[0], UDP_PORT)
                if cSocket.sendto(responseData, addr):
                    print "Disk Info sent!"
                else:
                    print "Sending disk info failed!"
        else:
            print "Received own broadcast... ignored."