示例#1
0
        public override void Call()
        {
            IController controller = ControllerRegistry.Get(GetRequiredArgument <string>("Controller")).New();

            //Set thermistors
            if (controller is ITemperatureDependantController)
            {
                ((ITemperatureDependantController)controller).SetThermistor((IThermistor)RootDevice.FindDeviceByPath(GetRequiredArgument <string>("Thermistor")));
            }

            //Set fixed values
            if (controller is IFixedNumberController)
            {
                ((IFixedNumberController)controller).Value = GetRequiredArgument <double>("Value");
            }

            if (controller is IFixedColorController)
            {
                ((IFixedColorController)controller).Value = GetRequiredArgument <Color>("Value");
            }

            if (controller is IFixedColorCycleController)
            {
                ((IFixedColorCycleController)controller).Value = new List <Color>(GetRequiredArgument <IEnumerable <Color> >("Value")).ToArray();
            }

            //Set curves
            if (controller is ICurveColorController)
            {
                ((ICurveColorController)controller).Value = GetRequiredArgument <ControlCurve <double, Color> >("Value");
            }

            if (controller is ICurveNumberController)
            {
                ((ICurveNumberController)controller).Value = GetRequiredArgument <ControlCurve <double, double> >("Value");
            }

            ((IControllableSensor)Device).Controller = controller;
        }
示例#2
0
        private void _Start()
        {
            string absoluteUri = request.Uri.AbsolutePath;

            while (absoluteUri.IndexOf("//") >= 0)
            {
                absoluteUri = absoluteUri.Replace("//", "/");
            }

            if (absoluteUri.StartsWith("/api"))
            {
                if (!request.Headers.Contains("Authorization"))
                {
                    RespondWithJSON("Please authenticate", false, 401, new Dictionary <string, string> {
                        { "WWW-Authenticate", "Basic realm=\"CorsairLinkPlusPlus API\"" }
                    });
                    return;
                }

                currentUser = GetCurrentUser(request.Headers["Authorization"]);
                if (currentUser == null)
                {
                    RespondWithJSON("Invalid login", false, 401, new Dictionary <string, string> {
                        { "WWW-Authenticate", "Basic realm=\"CorsairLinkPlusPlus API\"" }
                    });
                    return;
                }

                if (request.HttpMethod == "OPTIONS" || request.HttpMethod == "HEAD")
                {
                    RespondWithJSON("Yep yep, no need for these");
                    return;
                }

                try
                {
                    IDevice device = RootDevice.FindDeviceByPath(absoluteUri.Substring(4));

                    if (device == null)
                    {
                        RespondWithJSON("Device not found", false, 404);
                        return;
                    }

                    if (request.HttpMethod == "POST")
                    {
                        if (currentUser.Value.readOnly)
                        {
                            RespondWithJSON("Your user is read-only", false, 403);
                            return;
                        }

                        JSONMethodCall methodCall;
                        using (StreamReader bodyReader = new StreamReader(request.Body))
                        {
                            methodCall = JsonConvert.DeserializeObject <JSONMethodCall>(bodyReader.ReadToEnd());
                        }
                        BaseMethod.Execute(methodCall.Name, device, methodCall.Params);
                        RespondWithJSON("OK");
                        return;
                    }

                    RespondWithDevice(device);
                    return;
                }
                catch (Exception e)
                {
                    try
                    {
                        RespondWithJSON(e.Message, false, 500);

                        Console.Error.WriteLine("------------------");
                        Console.Error.WriteLine("Error in HTTP");
                        Console.Error.WriteLine(e);
                        Console.Error.WriteLine("------------------");
                    }
                    catch (Exception se)
                    {
                        Console.Error.WriteLine("------------------");
                        Console.Error.WriteLine("Error in HTTP error handler");
                        Console.Error.WriteLine(se);
                        Console.Error.WriteLine("Caused by");
                        Console.Error.WriteLine(e);
                        Console.Error.WriteLine("------------------");
                    }
                    return;
                }
            }
            else
            {
                if (absoluteUri == "/" || absoluteUri == "")
                {
                    absoluteUri = "index.html";
                }
                else
                {
                    absoluteUri = absoluteUri.Substring(1);
                }

                FileInfo fileInfo = new FileInfo(Program.WEB_ROOT_ABSOLUTE + '/' + absoluteUri);

                if (!fileInfo.Exists)
                {
                    RespondWithJSON("Static content not found", false, 404);
                    return;
                }

                if (fileInfo.Attributes.HasFlag(FileAttributes.Directory))
                {
                    RespondWithJSON("Static content is a directory", false, 403);
                    return;
                }

                DirectoryInfo directory = fileInfo.Directory;
                do
                {
                    if (Program.WEB_ROOT_ABSOLUTE.Equals(directory.FullName))
                    {
                        break;
                    }
                    directory = directory.Parent;
                } while (directory != null);

                if (directory == null)
                {
                    RespondWithJSON("Nope, sorry!", false, 403);
                    return;
                }

                string mineType = MIMEAssistant.GetMIMEType(fileInfo.Extension);

                RespondWithRaw(fileInfo.OpenRead(), 200, mineType);
            }
        }