Ejemplo n.º 1
0
 public JackModule() : base("/jack/{Uuid}")
 {
     Post["/connectCable/{CableUuid}"] = parameters =>
     {
         if (!Storable.TryGet(parameters.CableUuid, out Cable cable))
         {
             return(new NotFoundResponse());
         }
         if (!Storable.TryGet(parameters.Uuid, out Jack jack))
         {
             return(new NotFoundResponse());
         }
         jack.Connect(cable);
         return(new Response());
     };
     Post["/disconnectCable/{CableUuid}"] = parameters =>
     {
         if (!Storable.TryGet(parameters.CableUuid, out Cable cable))
         {
             return(new NotFoundResponse());
         }
         if (!Storable.TryGet(parameters.Uuid, out Jack jack))
         {
             return(new NotFoundResponse());
         }
         jack.Disconnect(cable);
         return(new Response());
     };
 }
Ejemplo n.º 2
0
        public AudioPlayerModule() : base("/audioplayer/{Uuid}")
        {
            Post["/upload"] = parameters =>
            {
                if (!Storable.TryGet(parameters.Uuid, out Instrument instrument))
                {
                    return(new NotFoundResponse());
                }
                var audioPlayer = (AudioPlayer)instrument;

                var file        = Request.Files.ToList()[0];
                var filename    = file.Name;
                var inputStream = file.Value;
                var fileStream  = File.Create($"./{parameters.Uuid}-{filename}");
                inputStream.CopyTo(fileStream);
                fileStream.Close();

                audioPlayer.SetFile($"./{parameters.Uuid}-{filename}");
                return(new Response());
            };
            Post["/play"] = parameters =>
            {
                if (!Storable.TryGet(parameters.Uuid, out AudioPlayer audioPlayer))
                {
                    return(new NotFoundResponse());
                }
                audioPlayer.Play();
                return(new Response());
            };
        }
Ejemplo n.º 3
0
        public Factory()
        {
            Options["*"] = _ =>
            {
                return(new Response());
            };
            Post["/{InstrumentTypeName}"] = parameters =>
            {
                var aseembly = typeof(Instrument).Assembly;
                var type     = aseembly.GetType($"MuGeonGiV2.Core.{parameters.InstrumentTypeName}");
                if (type == null)
                {
                    return(new NotFoundResponse());
                }
                var instrument = (Instrument)Activator.CreateInstance(type);
                return(Response.AsJson(instrument));
            };

            Delete["/instrument/{uuid}"] = parameters =>
            {
                if (!Storable.TryGet(parameters.uuid, out Instrument instrument))
                {
                    return(new NotFoundResponse());
                }

                instrument.Dispose();
                return(new Response());
            };
        }
Ejemplo n.º 4
0
 public SpeakerModule() : base("/speaker/{Uuid}")
 {
     Get["/devices"] = parameters =>
     {
         if (!Storable.TryGet(parameters.Uuid, out Speaker speaker))
         {
             return(new NotFoundResponse());
         }
         var devices = speaker.AvailableDevices.Select(device => device.ToString());
         return(Response.AsJson(devices));
     };
     Post["/device/{DeviceName}"] = parameters =>
     {
         if (!Storable.TryGet(parameters.Uuid, out Speaker speaker))
         {
             return(new NotFoundResponse());
         }
         speaker.SetDevice((string)parameters.DeviceName);
         return(new Response());
     };
 }
Ejemplo n.º 5
0
        public RpcModule() : base("/{Type}/{Uuid}")
        {
            Post["/{MethodName}"] = parameters =>
            {
                object caller;

                if (parameters.Type == "jack")
                {
                    if (!Jack.TryGet(parameters.Uuid, out Jack jack))
                    {
                        return(new NotFoundResponse());
                    }
                    caller = jack;
                }
                else
                {
                    if (!Instrument.TryGet(parameters.Uuid, out Instrument instrument))
                    {
                        return(new NotFoundResponse());
                    }
                    caller = instrument;
                }

                var        type   = caller.GetType();
                MethodInfo method = type.GetMethod(parameters.MethodName);
                method.Invoke(caller, null);
                return(new Response());
            };
            Post["/{Property}/{Value}"] = parameters =>
            {
                if (!Storable.TryGet(parameters.Uuid, out Storable storable))
                {
                    return(new NotFoundResponse());
                }
                // TODO : Check type

                var    type       = storable.GetType();
                string property   = parameters.Property;
                var    methodName = $"Set{ToUpper(property[0])}{property.Substring(1)}";

                var method = type.GetMethod(methodName);
                if (method != null)
                {
                    var param = Convert.ChangeType(parameters.Value, method.GetParameters()[0].ParameterType);

                    var parameterArray = new[]
                    {
                        param,
                    };
                    method.Invoke(storable, parameterArray);
                    return(new Response());
                }
                var fieldName    = $"{ToUpper(property[0])}{property.Substring(1)}";
                var propertyInfo = type.GetProperty(fieldName);
                if (propertyInfo != null)
                {
                    var fieldType = propertyInfo.PropertyType;
                    var value     = Convert.ChangeType(parameters.Value, fieldType);
                    propertyInfo.SetValue(storable, value);
                    return(new Response());
                }

                return(new NotFoundResponse());
            };
        }