def send(self, message): if type(message) is str: line = message else: notice('Out: %s' % message) line = encode(message) while True: try: self.socket.send(line) break except socket.timeout: pass
def read(self): while self.socket is not None and '\n' not in self.buffer: self.socket.settimeout(READ_TIMEOUT) try: input = self.socket.recv(BUFFER_SIZE) except socket.timeout: return None if len(input) == 0: self.close() break #print 'received "%s"' % input self.buffer = self.buffer + input.replace('\r\n', '\n') if '\n' in self.buffer: line,self.buffer = self.buffer.split('\n', 1) else: warn('No CRLF in buffer') line = self.buffer message = decode(line) notice('In: %s' % message) return message
def check(self, mode): # check profile config self.aProfile = [] # stop if selected (mode) profile are disabled if mode != '0' and 'false' in sProfile[int(mode)]: debug.notify(ADDON_LANG(32103) + ' (' + sName[int(mode)] + ')') debug.notice( '[CHECK]: This profile is disabled in addon settings - ' + str(mode)) return False # check if profile have settings file for key in sProfile: if 'true' in sProfile[key]: if not xbmcvfs.exists(ADDON_PATH_DATA + 'profile' + str(key) + '.json'): debug.notify( ADDON_LANG(32101) + ' ' + str(key) + ' (' + sName[key] + ')') debug.error('[PROFILE FILE]: not exist for profile - ' + str(key)) return False self.aProfile.append(str(key))
def profile(self, profile): # read addon settings sVolume = ADDON.getSetting('volume') sPlayer = ADDON.getSetting('player') sVideo = ADDON.getSetting('video') sCec = ADDON.getSetting('profile' + profile + '_cec') # read settings from profile f = xbmcvfs.File(ADDON_PATH_DATA + 'profile' + profile + '.json', 'r') result = f.read() try: jsonResult = json.loads(result) f.close() except: debug.notify( ADDON_LANG(32104) + ' ' + profile + ' (' + sName[int(profile)] + ')') debug.error( '[LOAD JSON FROM FILE]: Error reading from profile - ' + str(profile)) return False # settings needed quote for value quote_needed = [ 'audiooutput.audiodevice', 'audiooutput.passthroughdevice', 'locale.audiolanguage', 'lookandfeel.soundskin' ] # set settings readed from profile file debug.notice('[RESTORING SETTING]: ' + sName[int(profile)]) for setName, setValue in jsonResult.items(): # skip setting that type is disable to changing if 'false' in sPlayer and setName.startswith('videoplayer'): continue if 'false' in sVideo and setName.startswith('videoscreen'): continue debug.debug('[RESTORING SETTING]: ' + setName + ': ' + setValue) # add quotes if setName in quote_needed: setValue = '"' + setValue + '"' # set setting if 'true' in sVolume and setName == 'volume': xbmc.executeJSONRPC( '{"jsonrpc": "2.0", "method": "Application.SetVolume", "params": {"volume": ' + jsonResult['volume'] + '}, "id": 1}') else: xbmc.executeJSONRPC( '{"jsonrpc": "2.0", "method": "Settings.SetSettingValue", "params": {"setting": "' + setName + '", "value": ' + setValue.encode('utf-8') + '}, "id": 1}') debug.notify(sName[int(profile)].decode('utf-8')) # write curent profile f = xbmcvfs.File(ADDON_PATH_DATA + 'profile', 'w') f.write(profile) f.close() # CEC if sCec != '' and int(sCec) > 0: debug.notice('[SENDING CEC COMMAND]: ' + cecCommands[int(sCec)]) xbmc.executebuiltin(cecCommands[int(sCec)])
def save(self): xbmc_version = int(xbmc.getInfoLabel('System.BuildVersion')[0:2]) debug.debug('[XBMC VERSION]: ' + str(xbmc_version)) enabledProfiles = self.getEnabledProfiles() ret = dialog.DIALOG().start('script-audio-profiles-menu.xml', labels={10071: ADDON_LANG(32100)}, buttons=enabledProfiles[1], list=10070) if ret is None: return False else: button = enabledProfiles[0][ret] settingsToSave = {} if xbmc_version < 17: json_s = [ # get all settings from System / Audio section '{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"audiooutput"}},"id":1}', # get volume level '{"jsonrpc": "2.0", "method": "Application.GetProperties", "params": {"properties": ["volume"]}, "id": 1}', # get all settings from Video / Playback section '{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"videos","category":"videoplayer"}}, "id":1}', # get all settings from System / Video section '{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"videoscreen"}}, "id":1}' ] else: json_s = [ # get all settings from System / Audio section '{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"audio"}},"id":1}', # get volume level '{"jsonrpc": "2.0", "method": "Application.GetProperties", "params": {"properties": ["volume"]}, "id": 1}', # get all settings from Video / Playback section '{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"player","category":"videoplayer"}}, "id":1}', # get all settings from System / Video section '{"jsonrpc":"2.0","method":"Settings.GetSettings", "params":{"level": "expert", "filter":{"section":"system","category":"display"}}, "id":1}' ] # send json requests for j in json_s: jsonGet = xbmc.executeJSONRPC(j) jsonGet = json.loads(unicode(jsonGet, 'utf-8')) debug.debug('[JSON]: ' + str(jsonGet)) if 'result' in jsonGet: if 'settings' in jsonGet['result']: for set in jsonGet['result']['settings']: if 'value' in set.keys(): if set['value'] == True or set[ 'value'] == False: # lowercase bolean values settingsToSave[set['id']] = str( set['value']).lower() else: if type(set['value']) is int: settingsToSave[set['id']] = str( set['value']) else: settingsToSave[set['id']] = str( set['value']).encode('utf-8') if 'volume' in jsonGet['result']: settingsToSave['volume'] = str(jsonGet['result']['volume']) # prepare JSON string to save to file jsonToWrite = json.dumps(settingsToSave) # create dir in addon data if not exist if not xbmcvfs.exists(ADDON_PATH_DATA): xbmcvfs.mkdir(ADDON_PATH_DATA) # save profile file debug.notice('[SAVING SETTING]: ' + sName[button]) f = xbmcvfs.File(ADDON_PATH_DATA + 'profile' + str(button) + '.json', 'w') result = f.write(jsonToWrite) f.close() debug.notify( ADDON_LANG(32102) + ' ' + str(button) + ' (' + sName[button] + ')')