def GetAboutData(self): proxyBusObject = ProxyBusObject.ProxyBusObject(self.bus, SERVICE_NAME, "/About", self.session_id) iface = self.bus.GetInterface("org.alljoyn.About") proxyBusObject.AddInterface(iface) # <method name="GetAboutData"> # <arg name="languageTag" type="s" direction="in"/> # <arg name="aboutData" type="a{sv}" direction="out"/> # </method> languageTag = MsgArg.MsgArg() languageTag.SetString("en") replyMsg = Message.Message(g_bus) proxyBusObject.MethodCall('org.alljoyn.About', "GetAboutData", languageTag, 1, replyMsg, 25000, 0) arg = replyMsg.GetArg(0) # Todo Tidy up MsgArg code. Could there be a way to dynamically create types based on the dbus signature ? num = C.c_uint() entries = MsgArg.MsgArg() arg.Get("a{sv}", [C.POINTER(C.c_uint), C.POINTER(MsgArg.MsgArgHandle)], [C.byref(num), C.byref(entries.handle)]) for i in range(num.value): key = C.c_char_p() value_string = C.c_char_p() element = entries.ArrayElement(i) try: element.Get("{ss}", [C.POINTER(C.c_char_p), C.POINTER(C.c_char_p)], [C.byref(key), C.byref(value_string)]) self.device_details[key.value] = value_string.value except QStatusException as ex: pass
def UpdatePlaylist(self, tracks): number_of_tracks = len(tracks) arg = MsgArg.MsgArg() entries = MsgArg.MsgArg.ArrayCreate(number_of_tracks) for i, track in enumerate(tracks): url = C.c_char_p(track['url']) title = C.c_char_p(track.get('title', 'None')) artist = C.c_char_p(track.get('artist', 'None')) thumbnail_url = C.c_char_p(track.get('thumbnail_url', 'None')) duration = C.c_int64(track.get('duration', 'None',)) media_type = C.c_char_p(track.get('media_type', 'None')) album = C.c_char_p(track.get('album', 'None')) genre = C.c_char_p(track.get('genre', 'None')) num = C.c_size_t(0) # a{ss}: other data other_data = MsgArg.MsgArg.ArrayCreate(0) # a{sv}: medium description (codec, container, protocol, medium_data = MsgArg.MsgArg.ArrayCreate(0) user_data = MsgArg.MsgArg() element = entries.ArrayElement(i) # (ssssxsssa{ss}a{sv}v) element.Set( "(ssssxsssa{ss}a{sv}v)", [C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_int64), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle), C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle), C.POINTER(MsgArgHandle)], [C.byref(url), C.byref(title), C.byref(artist), C.byref(thumbnail_url), C.byref(duration), C.byref(mediaType), C.byref(album), C.byref(genre), C.byref(num), C.byref(other_data.handle), C.byref(num), C.byref(medium_data.handle), C.byref(user_data.handle)]) arg.Set("a(ssssxsssa{ss}a{sv}v)", number_of_tracks, entries)
def AdjustVolumePercent(self, percent): """ Not interested in this at this time. Left for future The change has floating point values between -1.0 and 1.0 to represent volume changes between -100% to 100%. A positive value (respectively negative), will increase (respectively decrease) the volume by the percentage of the "remaining range" towards the maximum (respectively minimum) value, i.e. difference between the current volume and the maximum (respectively minimum) volume. For example, when the volume range is [0-100] and we want to adjust by +50%: If the current volume is 25, the increment will be: "(100-25)*50%=75*0.5=38" (once rounded) so the new volume will be 63. Another adjustment by +50% will be "(100-63)*0.5=19" to a volume of 82. If we want instead to adjust by -50%, the decrement would be "(25-0)*0.5=13" to a volume of 12, and another adjustment by -50% would be "(12-0)*0.5=6" to a volume of 6. """ percent = min(max(0.0, percent), 100.0) param = MsgArg.MsgArg() param.SetDouble(percent) self.proxyBusObject.MethodCallNoReply( "org.alljoyn.Control.Volume", "AdjustVolumePercent", param, 1, 0) logging.info( "adjust volume for %s (%s) to %s", self.device_name, self.device_id, percent)
def SetMute(self): proxyBusObject = ProxyBusObject.ProxyBusObject(self.bus, SERVICE_NAME, SERVICE_PATH, self.session_id) iface = self.bus.GetInterface("org.alljoyn.Control.Volume") proxyBusObject.AddInterface(iface) param = MsgArg.MsgArg() param.SetBool(True) proxyBusObject.GetProperty("org.alljoyn.Control.Volume", "Mute", param)
def test_basic2(self): # Test (sai) arg = MsgArg.MsgArg() l = [-6, -66, 666, 6666] num = C.c_size_t(len(l)) ArrayType = C.c_int32 * len(l) array = ArrayType() array[:] = l resultArray = C.POINTER( C.c_int32)() # defines the type AND create an instance of it returnNum = C.c_size_t() try: arg.Set("ai", [C.c_size_t, ArrayType], [num, array]) except QStatusException as ex: print str(ex) assert False try: arg.Get( "ai", [C.POINTER(C.c_size_t), C.POINTER(C.POINTER(C.c_int32))], [C.byref(returnNum), C.byref(resultArray)]) self.assertEqual(returnNum.value, 4, 'wrong result') l2 = [resultArray[i] for i in range(4)] self.assertItemsEqual(l, l2, 'wrong result') except QStatusException as ex: print str(ex) assert False
def OnZoneChanged(member, srcpath, message): # <signal name="OnZoneChanged"> # <arg name="zoneId" type="s" direction="out"/> # <arg name="timestamp" type="i" direction="out"/> # <arg name="slaves" type="a{si}" direction="out"/> # </signal> message = Message.Message.FromHandle(message) slaves = message.GetArg(2) # Todo Tidy up MsgArg code. Could there be a way to dynamically create # types based on the dbus signature ? num = C.c_uint() entries = MsgArg.MsgArg() slaves.Get("a{si}", [C.POINTER(C.c_uint), C.POINTER(MsgArg.MsgArgHandle)], [C.byref(num), C.byref(entries.handle)]) for i in range(num.value): key = C.c_char_p() value = C.c_int() element = entries.ArrayElement(i) element.Get("{si}", [C.POINTER(C.c_char_p), C.c_int_p], [C.byref(key), C.byref(value)]) print key.value, ":", value.value
def SetVolume(self, volume): param = MsgArg.MsgArg() param.SetInt16(volume) self.proxyBusObject.SetProperty( "org.alljoyn.Control.Volume", "Volume", param) logging.info("setting volume for device %s (%s) to %s", self.device_name, self.device_id, volume)
def SetLoopMode(self, value): """ <property name="LoopMode" type="s" access="readwrite"/> ONE, ALL, NONE """ param = MsgArg.MsgArg() param.SetString(value) self.proxyBusObject.SetProperty("net.allplay.MediaPlayer", "LoopMode", param)
def AdjustVolumePercent(self, percent): proxyBusObject = ProxyBusObject.ProxyBusObject(self.bus, "SERVICE_NAME", SERVICE_PATH, self.session_id) iface = self.bus.GetInterface("org.alljoyn.Control.Volume") proxyBusObject.AddInterface(iface) percent = min(max(0.0, percent), 100.0) param = MsgArg.MsgArg() param.SetDouble(percent) proxyBusObject.MethodCallNoReply("org.alljoyn.Control.Volume", "AdjustVolumePercent", param, 1, 0)
def cat(busobject_handle, member, msg): # Concatenate the two input strings and reply with the result. message = Message.Message.FromHandle(msg) output = message.GetArg(0).GetString() + message.GetArg(1).GetString() replyArg = MsgArg.MsgArg() replyArg.SetString(output) BusObject.BusObject.FromHandle(busobject_handle).MethodReplyArgs(message, replyArg, 1) replyArg.Destroy()
def Echo(busobject_handle, member, msg): # Respond to remote method call `Echo` by returning the string back to the # sender. message = Message.Message.FromHandle(msg) msgarg = message.GetArg(0) text = msgarg.GetString() print "Server Echo method recieved:", text replyArg = MsgArg.MsgArg() replyArg.SetString("Echoing back:" + text) print BusObject.BusObject.FromHandle(busobject_handle).MethodReplyArgs(message, replyArg, 1)
def GetPlayingState(self): param = MsgArg.MsgArg() self.proxyBusObject.GetProperty( "net.allplay.MediaPlayer", "PlayState", param) play_state = C.c_char_p() position = C.c_int64() current_sample_rate = C.c_uint32() audio_channels = C.c_uint32() bits_per_sample = C.c_uint32() index_current_item = C.c_int32() index_next_item = C.c_int32() num = C.c_size_t() entries = MsgArg.MsgArg() # (sxuuuiia(ssssxsssa{ss}a{sv}v)) param.Get("(sxuuuii*)", [C.POINTER(C.c_char_p), C.POINTER(C.c_int64), C.POINTER(C.c_uint32), C.POINTER(C.c_uint32), C.POINTER(C.c_uint32), C.POINTER(C.c_int32), C.POINTER(C.c_int32), C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle) ], [C.byref(play_state), C.byref(position), C.byref(current_sample_rate), C.byref(audio_channels), C.byref(bits_per_sample), C.byref(index_current_item), C.byref(index_next_item), C.byref(num), C.byref(entries.handle)]) return play_state.value, position.value
def test_basic(self): arg = MsgArg.MsgArg() intSet = C.c_int32(-9999) intResult = C.c_int32() try: arg.Set("i", [C.c_int32], [intSet]) arg.Get("i", [C.POINTER(C.c_int32)], [intResult]) except QStatusException as ex: print str(ex) assert False self.assertEqual(intResult.value, -9999, 'wrong result')
def test_array(self): # Test (sai) arg = MsgArg.MsgArg() stringSet = C.c_char_p("Hello") stringResult = C.c_char_p() l = [-8, -88, 888, 8888] num = C.c_size_t(len(l)) ArrayType = C.c_int32 * len(l) array = ArrayType() array[:] = l resultArray = C.POINTER( C.c_int32)() # defines the type AND create an instance of it returnNum = C.c_size_t() try: arg.Set("(sai)", [C.c_char_p, C.c_size_t, ArrayType], [stringSet, num, array]) except QStatusException as ex: print str(ex) assert False try: arg.Get("(sai)", [ C.POINTER(C.c_char_p), C.POINTER(C.c_size_t), C.POINTER(C.POINTER(C.c_int32)) ], [ C.byref(stringResult), C.byref(returnNum), C.byref(resultArray) ]) self.assertEqual(stringResult.value, "Hello", 'wrong result') self.assertEqual(returnNum.value, 4, 'wrong result') l2 = [resultArray[i] for i in range(4)] self.assertItemsEqual(l, l2, 'wrong result') except QStatusException as ex: print str(ex) assert False
def OnPlayStateChanged(self, member, srcpath, message): """ (sxuuuiia(ssssxsssa{ss}a{sv}v)) """ message = Message.Message.FromHandle(message) arg = message.GetArg(0) play_state = C.c_char_p() position = C.c_int64() current_sample_rate = C.c_uint32() audio_channels = C.c_uint32() bits_per_sample = C.c_uint32() index_current_item = C.c_int32() index_next_item = C.c_int32() num = C.c_size_t() entries = MsgArg.MsgArg() arg.Get("(sxuuuii*)", [C.POINTER(C.c_char_p), C.POINTER(C.c_int64), C.POINTER(C.c_uint32), C.POINTER(C.c_uint32), C.POINTER(C.c_uint32), C.POINTER(C.c_int32), C.POINTER(C.c_int32), C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle) ], [C.byref(play_state), C.byref(position), C.byref(current_sample_rate), C.byref(audio_channels), C.byref(bits_per_sample), C.byref(index_current_item), C.byref(index_next_item), C.byref(num), C.byref(entries.handle)]) print play_state.value
def OnZoneChanged(member, srcpath, message): print "OnZoneChanged" print member, srcpath, message print vars(member) args = Message.Message.FromHandle(message).GetArgs() print "args", args slaves = args[2] # Todo Tidy up MsgArg code. Could there be a way to dynamically create types based on the dbus signature ? num = C.c_uint() entries = MsgArg.MsgArg() slaves.Get("a{si}", [C.POINTER(C.c_uint), C.POINTER(MsgArg.MsgArgHandle)], [C.byref(num), C.byref(entries.handle)]) for i in range(num.value): key = C.c_char_p() value = C.c_int() element = entries.ArrayElement(i) try: element.Get("{si}", [C.POINTER(C.c_char_p), C.c_int_p], [C.byref(key), C.byref(value)]) print key.value, ":", value.value except QStatusException as ex: pass
def CreateZone(self, device_ids): # <method name="CreateZone"> # <arg name="slaves" type="as" direction="in"/> # <arg name="zoneId" type="s" direction="out"/> # <arg name="timestamp" type="i" direction="out"/> # <arg name="slaves" type="a{si}" direction="out"/> # </method> # We must remove the dice id that this player is as the speaker does # not accept it. self.device_ids = [d for d in device_ids if d != self.device_id] print "%s CreateZone: %s" % (str(self.device_id), str(self.device_ids)) self.arg = MsgArg.MsgArg() size = len(self.device_ids) self.array = (C.c_char_p * size)() self.array[:] = self.device_ids self.arg.Set("as", [C.c_int, C.POINTER(C.c_char_p)], [size, self.array]) replyMsg = Message.Message(self.bus) try: self.proxyBusObject.MethodCall('net.allplay.ZoneManager', "CreateZone", self.arg, 1, replyMsg, 100000, 0) print "zoneId:", replyMsg.GetArg(0).GetString() print "timestamp:", replyMsg.GetArg(1).GetInt32() # callback = MessageReceiver.MessageReceiverReplyHandlerFuncType(AllPlayer.OnReplyMessageCallback) # proxyBusObject.MethodCallAsync('net.allplay.ZoneManager', "CreateZone", callback, self.arg, 1, None, 55000, 0) except QStatusException as ex: print replyMsg raise
def CreateZone(self, device_ids): # We must remove the id that this player is as the speaker does # not accept it. self.device_ids = [d for d in device_ids if d != self.device_id] self.arg = MsgArg.MsgArg() size = len(self.device_ids) self.array = (C.c_char_p * size)() print "CreateZone", self.device_ids self.array[:] = self.device_ids self.arg.Set( "as", [C.c_int, C.POINTER(C.c_char_p)], [size, self.array]) replyMsg = Message.Message(self.bus) try: self.proxyBusObject.MethodCall( 'net.allplay.ZoneManager', "CreateZone", self.arg, 1, replyMsg, 100000, 0) except QStatusException: print replyMsg raise
def AdjustVolumePercent(self, percent): percent = min(max(0.0, percent), 100.0) param = MsgArg.MsgArg() param.SetDouble(percent) AllPlayer.proxyBusObject.MethodCallNoReply( "org.alljoyn.Control.Volume", "AdjustVolumePercent", param, 1, 0)
def SetPosition(self, position): param = MsgArg.MsgArg() param.SetInt64(position) self.proxyBusObject.MethodCallNoReply("net.allplay.MediaPlayer", "SetPosition", param, 1, 0)
def SetMute(self): param = MsgArg.MsgArg() param.SetBool(True) self.proxyBusObject.GetProperty("org.alljoyn.Control.Volume", "Mute", param)
def GetMuteStatus(self): param = MsgArg.MsgArg() self.proxyBusObject.GetProperty("org.alljoyn.Control.Volume", "Mute", param) return bool(param.GetBool())
def OnAboutListenerCallBack(self, context, busName, version, port, objectDescriptionArg, aboutDataArg): print "BusName", busName print "objectDescriptionArg", objectDescriptionArg, type( objectDescriptionArg) print "aboutDataArg", aboutDataArg, type(aboutDataArg) objectDescription = AboutObjectDescription.AboutObjectDescription( objectDescriptionArg) print "*********************************************************************************" print "Announce signal discovered" print "\tFrom bus", busName print "\tAbout version", version print "\tSessionPort", port print "\tObjectDescription:" print "*********************************************************************************" print "Announce signal discovered" for path in objectDescription.GetPaths(): print "\t\t", path for interface in objectDescription.GetInterfaces(path): print "\t\t\t", interface print "\tAboutData:" aboutData = AboutData.AboutData(aboutDataArg, language="en") self.printAboutData(aboutData, None, 2) print "*********************************************************************************" opts = Session.SessionOpts(Session.ALLJOYN_TRAFFIC_TYPE_MESSAGES, False, Session.ALLJOYN_PROXIMITY_ANY, TransportMask.ALLJOYN_TRANSPORT_ANY) self.bus_attachment.EnableConcurrentCallBacks() print "JoiningSession", "busName", busName, "port", port, "opts", opts sessionId = self.bus_attachment.JoinSession(busName, port, self.sessionListener, opts) print "SessionJoined sessionId = ", sessionId aboutProxy = AboutProxy.AboutProxy(self.bus_attachment, busName, sessionId) objArg = aboutProxy.GetObjectDescription() print "*********************************************************************************" print "AboutProxy.GetObjectDescription:" aboutObjectDescription = AboutObjectDescription.AboutObjectDescription( objArg) for path in aboutObjectDescription.GetPaths(): print "\t", path for interface in aboutObjectDescription.GetInterfaces(path): print "\t\t", interface aArg = aboutProxy.GetAboutData() print "*********************************************************************************" print "AboutProxy.GetAboutData: (Default Language)" defaultLangAboutData = AboutData.AboutData() self.printAboutData(defaultLangAboutData, None, 1) defaultLanguage = defaultLangAboutData.GetDefaultLanguage() # Print out the AboutData for every language but the default it has already been printed. for lang in defaultLangAboutData.GetSupportedLanguages(): if lang != defaultLanguage: aArg = aboutProxy.GetAboutData(language=lang) printAboutData(aArg, lang, 1) ver = aboutProxy.GetVersion() print "*********************************************************************************" print "AboutProxy.GetVersion %hd" % (ver, ) print "*********************************************************************************" path = objectDescription.GetInterfacePaths(INTERFACE_NAME)[0] print "Calling %s/%s" % (path, INTERFACE_NAME) print "busName", busName, type(busName) print "path", path, type(path) print "sessionId", sessionId, type(sessionId) proxyBusObject = ProxyBusObject.ProxyBusObject( g_bus, busName, path, sessionId) print "proxyBusObject", proxyBusObject try: proxyBusObject.IntrospectRemoteObject() except QStatusException, ex: print "Failed to introspect remote object." arg = MsgArg.MsgArg() arg.SetString("ECHO Echo echo...") replyMsg = Message.Message(self.bus_attachment) proxyBusObject.MethodCall(INTERFACE_NAME, "Echo", arg, 1, replyMsg, 25000, 0) print "Echo method reply:", replyMsg.GetArg(0).GetString()
def GetPlaylist(self): """ <method name="GetPlaylist"> <arg name="items" type="a(ssssxsssa{ss}a{sv}v)" direction="out"/> <!-- see UpdatePlaylist --> <arg name="controllerType" type="s" direction="out"/> <arg name="playlistUserData" type="s" direction="out"/> </method> """ replyMsg = Message.Message(self.bus) try: self.proxyBusObject.MethodCall( 'net.allplay.MediaPlayer', "GetPlaylist", None, 0, replyMsg, 25000, 0) except QStatusException: return [] arg = replyMsg.GetArg(0) num = C.c_size_t() entries = MsgArg.MsgArg() arg.Get("a(ssssxsssa{ss}a{sv}v)", [C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle)], [ C.byref(num), C.byref(entries.handle)]) items = [] for i in range(num.value): url = C.c_char_p() title = C.c_char_p() artist = C.c_char_p() thumbnail_url = C.c_char_p() duration = C.c_int64() mediaType = C.c_char_p() album = C.c_char_p() genre = C.c_char_p() # a{ss}: other data other_data_num = C.c_size_t() other_data = MsgArg.MsgArg() # a{sv}: medium description (codec, container, protocol, medium_data_num = C.c_size_t() medium_data = MsgArg.MsgArg() user_data = MsgArg.MsgArg() element = entries.ArrayElement(i) try: # (ssssxsssa{ss}a{sv}v) element.Get( "(ssssxsssa{ss}a{sv}v)", [C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_int64), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_char_p), C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle), C.POINTER(C.c_size_t), C.POINTER(MsgArgHandle), C.POINTER(MsgArgHandle)], [C.byref(url), C.byref(title), C.byref(artist), C.byref(thumbnail_url), C.byref(duration), C.byref(mediaType), C.byref(album), C.byref(genre), C.byref(other_data_num), C.byref(other_data.handle), C.byref(medium_data_num), C.byref(medium_data.handle), C.byref(user_data.handle)]) items.append({'url': url.value, 'title': title.value, 'artist': artist.value, 'thumbnail_url': thumbnail_url.value, 'duration': duration.value, 'media_type': mediaType.value, 'album': album.value, 'genre': genre.value}) except QStatusException, ex: print ex
def GetVolume(self): param = MsgArg.MsgArg() self.proxyBusObject.GetProperty( "org.alljoyn.Control.Volume", "Volume", param) return param.GetInt16()
def GetLoopMode(self): param = MsgArg.MsgArg() self.proxyBusObject.GetProperty("net.allplay.MediaPlayer", "LoopMode", param) return param.GetString()