Пример #1
0
        public OpenRGBSink(
            OpenRGBSinkOptions options,
            IOpenRGBClient client) : base(options)
        {
            Client  = client;
            Options = options;
            Next.Subscribe((value) =>
            {
                switch (value)
                {
                case Ref <System.Drawing.Color> it:
                    UpdateAll(it);
                    break;

                case string it:
                    if (it.EndsWith(".orp"))
                    {
                        LoadProfile(it);
                        break;
                    }
                    goto default;

                default:
                    Logger.Error($"Sink {nameof(OpenRGBSink)} received type {value.GetType()} it cannot handle. Please provide a {typeof(System.Drawing.Color)} or a string containing a profile name.");
                    break;
                }
            });
        }
Пример #2
0
        /// <summary>
        /// Decodes a byte array into a Zone array.
        /// Increments the offset accordingly
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="zoneCount"></param>
        /// <param name="deviceID"></param>
        /// <param name="zoneID"></param>
        internal static Zone[] Decode(BinaryReader reader, ushort zoneCount, IOpenRGBClient client, int deviceID)
        {
            var zones = new Zone[zoneCount];

            for (int i = 0; i < zoneCount; i++)
            {
                zones[i] = new Zone
                {
                    Client   = client,
                    DeviceID = deviceID,
                    ID       = i,
                    Name     = reader.ReadLengthAndString(),
                    Type     = (ZoneType)reader.ReadUInt32(),
                    LedsMin  = reader.ReadUInt32(),
                    LedsMax  = reader.ReadUInt32(),
                    LedCount = reader.ReadUInt32()
                };

                var zoneMatrixLength = reader.ReadUInt16();

                if (zoneMatrixLength > 0)
                {
                    zones[i].MatrixMap = MatrixMap.Decode(reader);
                }
                else
                {
                    zones[i].MatrixMap = null;
                }
            }

            return(zones);
        }
Пример #3
0
        /// <summary>
        /// Decodes a byte array into a Device.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="protocol"></param>
        /// <param name="client">The owning OpenRGBClient for this device.</param>
        /// <param name="deviceId">The device ID under the owning client.</param>
        internal static Device Decode(byte[] buffer, uint protocol, IOpenRGBClient client, int deviceId)
        {
            var dev = new Device
            {
                ID     = deviceId,
                Client = client
            };

            using (var reader = new BinaryReader(new MemoryStream(buffer)))
            {
                var duplicatePacketLength = reader.ReadUInt32();

                var deviceType = reader.ReadInt32();
                if (Enum.IsDefined(typeof(DeviceType), deviceType))
                {
                    dev.Type = (DeviceType)deviceType;
                }
                else
                {
                    dev.Type = DeviceType.Unknown;
                }

                dev.Name = reader.ReadLengthAndString();

                if (protocol >= 1)
                {
                    dev.Vendor = reader.ReadLengthAndString();
                }
                else
                {
                    dev.Vendor = null;
                }

                dev.Description = reader.ReadLengthAndString();

                dev.Version = reader.ReadLengthAndString();

                dev.Serial = reader.ReadLengthAndString();

                dev.Location = reader.ReadLengthAndString();

                var modeCount = reader.ReadUInt16();
                dev.ActiveModeIndex = reader.ReadInt32();
                dev.Modes           = Mode.Decode(reader, modeCount);

                var zoneCount = reader.ReadUInt16();
                dev.Zones = Zone.Decode(reader, zoneCount, client, deviceId);

                var ledCount = reader.ReadUInt16();
                dev.Leds = Led.Decode(reader, ledCount);

                var colorCount = reader.ReadUInt16();
                dev.Colors = Color.Decode(reader, colorCount);
                return(dev);
            }
        }
Пример #4
0
        public static T RequestCatching <T>(this IOpenRGBClient client, Func <T> request)
        {
            try
            {
                if (!client.Connected)
                {
                    client.Connect();
                }

                return(request());
            }
            catch (Exception e)
            {
                Logger.Error($"Disconnected from OpenRGB ({e.Message})");
                return(client.Reconnect(() => client.RequestCatching(request)));
            }
        }
Пример #5
0
        public static T Reconnect <T>(this IOpenRGBClient client, Func <T> onSuccess)
        {
            // we have to be a little nasty here, since the client library does not expose any means of reconnecting
            var clientType  = typeof(OpenRGBClient);
            var socketField = clientType.GetField("_socket", BindingFlags.NonPublic | BindingFlags.Instance);

            try
            {
                socketField.SetValue(client, new Socket(SocketType.Stream, ProtocolType.Tcp));
                client.Connect();
                return(onSuccess());
            }
            catch (Exception e)
            {
                Logger.Error($"Connection to OpenRGB failed: {e.Message}");
            }

            return(default);
Пример #6
0
        public OpenRGBSource(
            OpenRGBSourceOptions options,
            IOpenRGBClient client,
            IObservable <long> pollingInterval
            ) : base(options)
        {
            Options = options;
            Value   = Subject.AsObservable();
            Client  = client;

            pollingInterval.Subscribe((_) =>
            {
                var devices = Client.RequestCatching(() => Client.GetAllControllerData());

                if (LastDevices != null && devices.SequenceEqual(LastDevices, new DeviceEqualityComparer()))
                {
                    return;
                }

                Logger.Info($"OpenRGB device color changed");

                LastDevices = devices;

                var deviceStates = new Dictionary <string, DeviceState>();

                devices.ToList().ForEach(it =>
                {
                    deviceStates.Add(it.Name, new DeviceState()
                    {
                        Colors = it.Colors.Select(color => color.ToSystemColor())
                    });
                });

                Subject.OnNext(deviceStates);
            });
        }