Ejemplo n.º 1
0
        public void StartCycleDim()
        {
            if (PlatformConverter.ToBool(CycleDimInProgress))
            {
                return;
            }

            if (PlatformConverter.ToBool(RaiseInProgress))
            {
                StopRaise();
            }
            if (PlatformConverter.ToBool(LowerInProgress))
            {
                StopLower();
            }

            if (!State.On && _cycleDimDirection != true)
            {
                State.Brightness = BridgeClient.MinLightBulbBrightness;
            }

            StartCycleProperty(
                () => _cycleDimTimer,
                timer => _cycleDimTimer = timer,
                _cycleDimTime,
                () => _cycleDimDirection,
                direction => _cycleDimDirection  = direction,
                inProgress => CycleDimInProgress = inProgress,
                () => Brightness,
                brightness => State.Brightness = brightness,
                BridgeClient.MinLightBulbBrightness,
                BridgeClient.MaxLightBulbBrightness);
        }
Ejemplo n.º 2
0
 private void StopCycles()
 {
     if (PlatformConverter.ToBool(RaiseInProgress))
     {
         StopRaise();
     }
     if (PlatformConverter.ToBool(LowerInProgress))
     {
         StopLower();
     }
     if (PlatformConverter.ToBool(CycleDimInProgress))
     {
         StopCycleDim();
     }
     if (PlatformConverter.ToBool(CycleHueInProgress))
     {
         StopCycleHue();
     }
     if (PlatformConverter.ToBool(CycleSaturationInProgress))
     {
         StopCycleSaturation();
     }
     if (PlatformConverter.ToBool(CycleColorTemperatureInProgress))
     {
         StopCycleColorTemperature();
     }
 }
Ejemplo n.º 3
0
        public void SetProperties(LightBulbProperties lightBulbPropertiesToSet)
        {
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetBrightness) && PlatformConverter.ToBool(RaiseInProgress))
            {
                StopRaise();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetBrightness) && PlatformConverter.ToBool(LowerInProgress))
            {
                StopLower();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetBrightness) && PlatformConverter.ToBool(CycleDimInProgress))
            {
                StopCycleDim();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetHue) && PlatformConverter.ToBool(CycleHueInProgress))
            {
                StopCycleHue();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetSaturation) && PlatformConverter.ToBool(CycleSaturationInProgress))
            {
                StopCycleSaturation();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetColorTemperature) && PlatformConverter.ToBool(CycleColorTemperatureInProgress))
            {
                StopCycleColorTemperature();
            }

            SetHuePropertiesRequestAction(lightBulbPropertiesToSet);
        }
Ejemplo n.º 4
0
        public void BeginTemporaryChange()
        {
            using (new LockScope(_classLock))
            {
                if (_beforeTemporaryChangeProperties != null)
                {
                    return;
                }

                _beforeTemporaryChangeProperties            = new LightBulbProperties();
                _beforeTemporaryChangeProperties.Brightness = Brightness;
                switch (ColorMode)
                {
                case LightBulbColorMode.HueSaturation:
                    _beforeTemporaryChangeProperties.Hue           = Hue;
                    _beforeTemporaryChangeProperties.Saturation    = Saturation;
                    _beforeTemporaryChangeProperties.SetHue        = PlatformConverter.ToPlatformBool(true);
                    _beforeTemporaryChangeProperties.SetSaturation = PlatformConverter.ToPlatformBool(true);
                    break;

                case LightBulbColorMode.ColorTemperature:
                    _beforeTemporaryChangeProperties.ColorTemperature    = ColorTemperature;
                    _beforeTemporaryChangeProperties.SetColorTemperature = PlatformConverter.ToPlatformBool(true);
                    break;

                case LightBulbColorMode.XY:
                    _beforeTemporaryChangeProperties.XY    = XY;
                    _beforeTemporaryChangeProperties.SetXY = PlatformConverter.ToPlatformBool(true);
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        public void StartLower()
        {
            if (PlatformConverter.ToBool(LowerInProgress))
            {
                return;
            }

            if (PlatformConverter.ToBool(RaiseInProgress))
            {
                StopRaise();
            }
            if (PlatformConverter.ToBool(CycleDimInProgress))
            {
                StopCycleDim();
            }

            StartCycleProperty(
                () => _lowerTimer,
                timer => _lowerTimer = timer,
                _lowerTime,
                () => false,
                direction =>
            {
                if (State.Brightness <= BridgeClient.MinLightBulbBrightness)
                {
                    State.On = false;
                }
            },
                inProgress => LowerInProgress = inProgress,
                () => Brightness,
                brightness => State.Brightness = brightness,
                BridgeClient.MinLightBulbBrightness,
                BridgeClient.MaxLightBulbBrightness);
        }
Ejemplo n.º 6
0
 public void Lower(ushort brightnessChange)
 {
     SetProperties(new LightBulbProperties()
     {
         SetBrightnessOffset = PlatformConverter.ToPlatformBool(true),
         BrightnessOffset    = (short)(-1 * (short)Math.Max(
                                           (ushort)(BridgeClient.MaxLightBulbBrightness / 100.0),
                                           Math.Min(
                                               brightnessChange,
                                               (ushort)short.MaxValue)))
     });
 }
Ejemplo n.º 7
0
        public void StopCycleHue()
        {
            if (!PlatformConverter.ToBool(CycleHueInProgress))
            {
                return;
            }

            StopCycleProperty(
                ref _cycleHueTimer,
                ref _cycleHueDirection,
                inProgress => CycleHueInProgress = inProgress);
        }
Ejemplo n.º 8
0
        public void StopLower()
        {
            if (!PlatformConverter.ToBool(LowerInProgress))
            {
                return;
            }

            bool?lowerDirection = true;

            StopCycleProperty(
                ref _lowerTimer,
                ref lowerDirection,
                inProgress => LowerInProgress = inProgress);
        }
Ejemplo n.º 9
0
        public void StopRaise()
        {
            if (!PlatformConverter.ToBool(RaiseInProgress))
            {
                return;
            }

            bool?raiseDirection = true;

            StopCycleProperty(
                ref _raiseTimer,
                ref raiseDirection,
                inProgress => RaiseInProgress = inProgress);
        }
Ejemplo n.º 10
0
        public void StartCycleSaturation()
        {
            if (PlatformConverter.ToBool(CycleSaturationInProgress))
            {
                return;
            }

            StartCycleProperty(
                () => _cycleSaturationTimer,
                timer => _cycleSaturationTimer = timer,
                _cycleSaturationTime,
                () => _cycleSaturationDirection,
                direction => _cycleSaturationDirection  = direction,
                inProgress => CycleSaturationInProgress = inProgress,
                () => Saturation,
                saturation => State.Saturation = saturation,
                BridgeClient.MinLightBulbSaturation,
                BridgeClient.MaxLightBulbSaturation);
        }
Ejemplo n.º 11
0
        public void StartCycleHue()
        {
            if (PlatformConverter.ToBool(CycleHueInProgress))
            {
                return;
            }

            StartCycleProperty(
                () => _cycleHueTimer,
                timer => _cycleHueTimer = timer,
                _cycleHueTime,
                () => _cycleHueDirection,
                direction => _cycleHueDirection  = direction,
                inProgress => CycleHueInProgress = inProgress,
                () => Hue,
                hue => State.Hue = hue,
                BridgeClient.MinLightBulbHue,
                BridgeClient.MaxLightBulbHue);
        }
Ejemplo n.º 12
0
        public void StartCycleColorTemperature()
        {
            if (PlatformConverter.ToBool(CycleColorTemperatureInProgress))
            {
                return;
            }

            StartCycleProperty(
                () => _cycleColorTemperatureTimer,
                timer => _cycleColorTemperatureTimer = timer,
                _cycleColorTemperatureTime,
                () => _cycleColorTemperatureDirection,
                direction => _cycleColorTemperatureDirection  = direction,
                inProgress => CycleColorTemperatureInProgress = inProgress,
                () => ColorTemperature,
                colorTemperature => State.ColorTemperature = colorTemperature,
                BridgeClient.MinLightBulbColorTemperature,
                BridgeClient.MaxLightBulbColorTemperature);
        }
        internal LightBulbProperties(LightBulb lightBulbFromWhichToSetAll)
        {
            Bool trueBool = PlatformConverter.ToPlatformBool(true);

            SetBrightness           = trueBool;
            SetBrightnessPercentage = trueBool;
            SetHue                  = trueBool;
            SetSaturation           = trueBool;
            SetSaturationPercentage = trueBool;
            SetXY = trueBool;
            SetColorTemperature = trueBool;

            Brightness           = lightBulbFromWhichToSetAll.Brightness;
            BrightnessPercentage = lightBulbFromWhichToSetAll.BrightnessPercentage;
            Hue                  = lightBulbFromWhichToSetAll.Hue;
            Saturation           = lightBulbFromWhichToSetAll.Saturation;
            SaturationPercentage = lightBulbFromWhichToSetAll.SaturationPercentage;
            XY = lightBulbFromWhichToSetAll.XY.ToArray();
            ColorTemperature = lightBulbFromWhichToSetAll.ColorTemperature;
        }
        /// <summary>
        /// Converts older metadata to the new format.
        /// </summary>
        /// <param name="file">File to parse metadata from.</param>
        /// <param name="rewrite">Determines if the mod's configuration should be rewritten as JSON.</param>
        public static Mod Convert(string file, bool rewrite = false)
        {
            Mod mod = new()
            {
                Title       = DeserialiseKey(file, "Title"),
                Version     = DeserialiseKey(file, "Version"),
                Date        = DeserialiseKey(file, "Date"),
                Author      = DeserialiseKey(file, "Author"),
                Platform    = PlatformConverter.Convert(DeserialiseKey(file, "Platform")),
                Description = DeserialiseKey(file, "Description"),

                Merge = bool.Parse(DeserialiseKey(file, "Merge")),
                Path  = file
            };

            string requiredPatchesCSV = DeserialiseKey(file, "RequiredPatches");
            string readOnlyCSV        = DeserialiseKey(file, "Read-only");
            string customCSV          = DeserialiseKey(file, "Custom");

            if (!string.IsNullOrEmpty(requiredPatchesCSV))
            {
                mod.RequiredPatches.AddRange(requiredPatchesCSV.Split(','));
            }

            if (!string.IsNullOrEmpty(readOnlyCSV))
            {
                mod.ReadOnly.AddRange(readOnlyCSV.Split(','));
            }

            if (!string.IsNullOrEmpty(customCSV))
            {
                mod.Custom.AddRange(customCSV.Split(','));
            }

            if (rewrite)
            {
                mod.Write(mod, StringExtensions.ReplaceFilename(file, "mod.json"));
            }

            return(mod);
        }
Ejemplo n.º 15
0
        private void StopCycleProperty(
            ref CTimer cycleTimer,
            ref bool?cycleDirection,
            Action <Bool> cyclePropertyInProgressSetter)
        {
            using (new LockScope(_classLock))
            {
                if (cycleTimer != null)
                {
                    cycleTimer.Dispose();
                    cycleTimer = null;
                }

                if (cycleDirection != null)
                {
                    cycleDirection = !cycleDirection.Value;
                }

                cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(false));
            }
        }
Ejemplo n.º 16
0
 internal ErrorOccurredEventArgs(string errorMessage, bool isUserError)
 {
     ErrorMessage = errorMessage;
     IsUserError  = PlatformConverter.ToPlatformBool(isUserError);
 }
Ejemplo n.º 17
0
        public List <RemoteNotificationResult> Send()
        {
            foreach (RemoteNotification notif in m_notifications)
            {
                PlatformType platformType = PlatformConverter.FromDeviceType(notif.DeviceType);
                switch (platformType)
                {
                case PlatformType.Apple:
                    EnsureiOS();
                    AppleNotification n = NotificationFactory.Apple();
                    n.DeviceToken = notif.DeviceToken;
                    n.Payload     = new AppleNotificationPayload();
                    if (!string.IsNullOrEmpty(notif.Message))
                    {
                        n.Payload.Alert = new AppleNotificationAlert()
                        {
                            Body = notif.Message
                        }
                    }
                    ;
                    if (!string.IsNullOrEmpty(notif.Sound))
                    {
                        n.Payload.Sound = notif.Sound;
                    }
                    if (!string.IsNullOrEmpty(notif.Action) && notif.Action.Trim().Length > 0)
                    {
                        n.Payload.AddCustom("a", notif.Action);
                        if (notif.Parameters != null && notif.Parameters.Names.Count > 0)
                        {
                            n.Payload.AddCustom("p", notif.Parameters.ToJObject());
                        }
                    }
                    if (!string.IsNullOrEmpty(notif.Badge))
                    {
                        int badgeInt;
                        if (int.TryParse(notif.Badge, out badgeInt))
                        {
                            n.Payload.Badge = badgeInt;
                        }
                        else if (notif.Badge == "!")
                        {
                            n.Payload.Badge = -1;
                        }
                    }
                    if (notif.ExecutionTime != 0)
                    {
                        n.Payload.ContentAvailable = 1;
                    }

                    Service.QueueNotification(n);
                    break;

                case PlatformType.AndroidGcm:
                    EnsureAndroid();
                    GcmNotification g = NotificationFactory.Google();
                    g.RegistrationIds.Add(notif.DeviceToken);
                    g.JsonData = string.Format("{{ \"payload\":\"{0}\",\"action\":\"{1}\",\"parameters\": {2} ,\"executiontime\": {3} ,\"priority\": {4} }}"
                                               , notif.Message, notif.Action, notif.Parameters.ToJson(), notif.ExecutionTime.ToString(), notif.Delivery.Priority);
                    g.CollapseKey = "NONE";
                    Service.QueueNotification(g);
                    break;

                case PlatformType.WindowsPhone:
                    EnsureWindows();
                    WindowsNotification w = NotificationFactory.Windows();
                    w.ChannelUri    = notif.DeviceToken;
                    w.Message       = notif.Message;
                    w.Title         = notif.Title;
                    w.ImageUri      = notif.Icon;
                    w.Badge         = notif.Badge;
                    w.Sound         = notif.Sound;
                    w.Action        = notif.Action;
                    w.Parameters    = notif.Parameters;
                    w.ExecutionTime = notif.ExecutionTime;

                    Service.QueueNotification(w);
                    break;

                default:
                    RemoteNotificationResult result = RemoteNotificationResult.ForDevice(notif.DeviceType, notif.DeviceToken);
                    result.ErrorCode        = INVALID_DEVICE_TYPE;
                    result.ErrorDescription = GetErrorDescription(INVALID_DEVICE_TYPE);
                    SetResult(result);
                    break;
                }
            }

            Service.StopAllServices(true);
            return(m_results);
        }
Ejemplo n.º 18
0
        private void StartCycleProperty(
            Func <CTimer> cycleTimerGetter,
            Action <CTimer> cycleTimerSetter,
            ushort cyclePropertyTime,
            Func <bool?> cycleDirectionGetter,
            Action <bool?> cycleDirectionSetter,
            Action <Bool> cyclePropertyInProgressSetter,
            Func <ushort> propertyGetter,
            Action <ushort> propertySetter,
            ushort minPropertyValue,
            ushort maxPropertyValue)
        {
            using (new LockScope(_classLock))
            {
                if (cycleTimerGetter() != null)
                {
                    cycleTimerGetter().Dispose();
                    cycleTimerSetter(null);
                }

                if (propertyGetter().CompareTo(minPropertyValue) < 0 || !PlatformConverter.ToBool(On))
                {
                    cycleDirectionSetter(true);
                }
                else if (propertyGetter().CompareTo(maxPropertyValue) >= 0)
                {
                    cycleDirectionSetter(false);
                }
                else if (cycleDirectionGetter() == null)
                {
                    cycleDirectionSetter(true);
                }

                int repeatTime = (int)Math.Max(100.0f, (float)cyclePropertyTime / maxPropertyValue);
                // NOTE: Max 100 because Philips recommends no more than 10 commands per second be sent to a bulb

                ushort offset = (ushort)Math.Max(1.0f, maxPropertyValue / (cyclePropertyTime / repeatTime));

                cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(true));

                CCriticalSection inProgressLock = new CCriticalSection();

                cycleTimerSetter(
                    new CTimer(data =>
                {
                    using (new LockScope(inProgressLock))
                    {
                        if (cycleTimerGetter() == null)
                        {
                            return;
                        }

                        bool direction = cycleDirectionGetter() == true;
                        if (direction)
                        {
                            if (propertyGetter() >= maxPropertyValue)
                            {
                                using (new LockScope(_classLock))
                                {
                                    if (cycleTimerGetter() != null)
                                    {
                                        cycleDirectionSetter(false);
                                        cycleTimerGetter().Dispose();
                                        cycleTimerSetter(null);
                                    }

                                    cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(false));
                                }
                            }
                            else
                            {
                                ushort adjustedOffset = (ushort)Math.Min(offset, maxPropertyValue - propertyGetter());
                                propertySetter((ushort)(propertyGetter() + adjustedOffset));
                            }
                        }
                        else
                        {
                            if (propertyGetter() <= minPropertyValue)
                            {
                                using (new LockScope(_classLock))
                                {
                                    if (cycleTimerGetter() != null)
                                    {
                                        cycleDirectionSetter(true);
                                        cycleTimerGetter().Dispose();
                                        cycleTimerSetter(null);
                                    }

                                    cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(false));
                                }
                            }
                            else
                            {
                                ushort adjustedOffset = (ushort)Math.Min(offset, propertyGetter() - minPropertyValue);
                                propertySetter((ushort)(propertyGetter() - adjustedOffset));
                            }
                        }
                    }
                },
                               5000 /*dummy value until Reset is called below since CTimer doesn't seem to respect the ctor parameter*/));

                cycleTimerGetter().Reset(0, (int)repeatTime);
            }
        }