internal AllJoynServiceException(AllJoynStatus status, IInterface iface, string operation) : base(status.StatusText)
 {
     this.StatusCode = status.StatusCode;
     Interface       = iface.Name;
     Path            = iface.BusObject.Path;
     Operation       = operation;
     Service         = iface.BusObject.Service.Name + ":" + iface.BusObject.Service.AnnouncedPort;
 }
Example #2
0
            private void ServiceJoined(IService service)
            {
                // Don't continue if the new service isn't the one we're interested in.
                if (DeviceId != "*" && service.AboutData.DeviceId != DeviceId)
                {
                    return;
                }

                // Prepare the interaction.

                IEnumerable <IBusObject> busObjects = new List <IBusObject>();

                if (Path == "*")
                {
                    // Get everything
                    try
                    {
                        busObjects = service.AllObjects().Where(x => x.Interfaces != null && x.Interfaces.Any(y => y.Name == InterfaceName));
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException("Interaction", ex);
                    }
                }
                else
                {
                    try
                    {
                        // DEMO TEMP FIX:
                        // Replace with IService::GetBusObject()
                        busObjects = new List <IBusObject>()
                        {
                            service.AllObjects().First(x => x.Path == Path)
                        };
                    }
                    catch (InvalidOperationException)
                    {
                        failDiscovery("Couldn't find bus object: " + Path);
                        return;
                    }
                }

                foreach (IBusObject busObject in busObjects)
                {
                    IInterface @interface;
                    int        delayMSec = 0;

                    try
                    {
                        @interface = busObject.Interfaces.First(x => x.Name == InterfaceName);
                    }
                    catch (InvalidOperationException)
                    {
                        failDiscovery(busObject.Path + ", couldn't find interface: " + InterfaceName);
                        return;
                    }

                    if (InteractionConfig.ContainsKey("delay"))
                    {
                        try
                        {
                            delayMSec = (int)InteractionConfig["delay"].GetNumber();
                        }
                        catch (InvalidCastException)
                        {
                            failDiscovery("Couldn't cast delay value: " + InteractionConfig.GetNamedString("delay"));
                        }
                    }

                    if (InteractionConfig.ContainsKey("propertyName"))
                    {
                        IProperty property = null;
                        object    newValue = null;

                        try
                        {
                            property = @interface.Properties.First(x => x.Name == InteractionConfig.GetNamedString("propertyName"));
                        }
                        catch (InvalidOperationException)
                        {
                            failDiscovery("Couldn't find property: " + InteractionConfig.GetNamedString("propertyName"));
                            return;
                        }

                        try
                        {
                            newValue = AllJoynTypes.Convert(property.TypeInfo, InteractionConfig["propertyValue"]);
                        }
                        catch (InvalidCastException)
                        {
                            failDiscovery("Couldn't cast property value for property: " + InteractionConfig.GetNamedString("propertyName"));
                            return;
                        }

                        Actions.Add(
                            async() =>
                        {
                            if (delayMSec > 0)
                            {
                                await System.Threading.Tasks.Task.Delay(delayMSec);
                            }

                            AllJoynStatus status = await property?.SetValueAsync(newValue);

                            if (status == null || status.IsFailure)
                            {
                                throw Logger.LogException(service.Name,
                                                          new InvalidOperationException(
                                                              string.Format("{0} - Couldn't modify property: {1}", status, property.Name)
                                                              )
                                                          );
                            }
                        }
                            );
                    }
                    else if (InteractionConfig.ContainsKey("methodName"))
                    {
                        IMethod       method     = null;
                        List <object> methodArgs = null;

                        try
                        {
                            method = @interface.Methods.First(x => x.Name == InteractionConfig.GetNamedString("methodName"));
                        }
                        catch (InvalidOperationException)
                        {
                            failDiscovery("Couldn't find method: " + InteractionConfig.GetNamedString("methodName"));
                            return;
                        }

                        try
                        {
                            methodArgs = Enumerable.Zip(method.InSignature,
                                                        InteractionConfig.GetNamedArray("methodArguments"),
                                                        (t, x) => AllJoynTypes.Convert(t.TypeDefinition, x)
                                                        ).ToList();
                        }
                        catch (InvalidCastException)
                        {
                            failDiscovery("Couldn't cast arguments for method: " + InteractionConfig.GetNamedString("methodName"));
                            return;
                        }

                        Actions.Add(
                            async() =>
                        {
                            if (delayMSec > 0)
                            {
                                await System.Threading.Tasks.Task.Delay(delayMSec);
                            }

                            InvokeMethodResult status = await method?.InvokeAsync(methodArgs);

                            if (status == null || status.Status.IsFailure)
                            {
                                throw Logger.LogException(service.Name,
                                                          new InvalidOperationException(
                                                              string.Format("{0} - Couldn't invoke method: {1}", status, method.Name)
                                                              )
                                                          );
                            }
                        }
                            );
                    }
                    else
                    {
                        failDiscovery("Interaction does not have property nor method information.");
                        return;
                    }
                }
            }