]> git.basschouten.com Git - openhab-addons.git/blob
73f9d6437df6c8968b2462a92072baa14fbd5922
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lifx.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.lifx.internal.LifxBindingConstants;
18 import org.openhab.binding.lifx.internal.fields.HSBK;
19
20 /**
21  * This class represents LIFX Tile effect
22  *
23  * @author Pawel Pieczul - initial contribution
24  */
25 @NonNullByDefault
26 public class Effect {
27     public enum EffectType {
28         OFF(0),
29         MORPH(2),
30         FLAME(3);
31
32         Integer type;
33
34         EffectType(Integer type) {
35             this.type = type;
36         }
37
38         public static EffectType fromValue(Integer value) {
39             switch (value) {
40                 case 0:
41                     return OFF;
42                 case 2:
43                     return MORPH;
44                 case 3:
45                     return FLAME;
46                 default:
47                     throw new IllegalArgumentException("Unknown effect type");
48             }
49         }
50
51         public static EffectType fromValue(String value) {
52             if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_OFF.equals(value)) {
53                 return OFF;
54             } else if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_MORPH.equals(value)) {
55                 return MORPH;
56             } else if (LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_FLAME.equals(value)) {
57                 return FLAME;
58             }
59             throw new IllegalArgumentException("Unknown effect type");
60         }
61
62         public Integer intValue() {
63             return type;
64         }
65
66         public String stringValue() {
67             switch (type) {
68                 case 2:
69                     return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_MORPH;
70                 case 3:
71                     return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_FLAME;
72                 default:
73                     return LifxBindingConstants.CHANNEL_TYPE_EFFECT_OPTION_OFF;
74             }
75         }
76     }
77
78     final EffectType type;
79     final Long speed;
80     final Long duration;
81     final HSBK[] palette;
82
83     public EffectType getType() {
84         return type;
85     }
86
87     public Long getSpeed() {
88         return speed;
89     }
90
91     public Long getDuration() {
92         return duration;
93     }
94
95     HSBK[] getPalette() {
96         return palette;
97     }
98
99     public Effect(EffectType type, Long speed, Long duration, HSBK[] palette) {
100         this.type = type;
101         this.palette = palette;
102         this.duration = duration;
103         this.speed = speed;
104     }
105
106     public Effect(Integer type, Long speed, Long duration, HSBK[] palette) {
107         this(EffectType.fromValue(type), speed, duration, palette);
108     }
109
110     public Effect() {
111         this(EffectType.OFF, 3000L, 0L, new HSBK[0]);
112     }
113
114     public static Effect createDefault(String type, @Nullable Long morphSpeed, @Nullable Long flameSpeed) {
115         Long speed;
116         EffectType effectType = EffectType.fromValue(type);
117         switch (effectType) {
118             case OFF:
119                 return new Effect(effectType, 0L, 0L, new HSBK[0]);
120             case MORPH:
121                 if (morphSpeed == null) {
122                     speed = 3000L;
123                 } else {
124                     speed = morphSpeed;
125                 }
126                 HSBK[] p = { new HSBK(0, 65535, 65535, 3500), new HSBK(7281, 65535, 65535, 3500),
127                         new HSBK(10922, 65535, 65535, 3500), new HSBK(22209, 65535, 65535, 3500),
128                         new HSBK(43507, 65535, 65535, 3500), new HSBK(49333, 65535, 65535, 3500),
129                         new HSBK(53520, 65535, 65535, 3500) };
130                 return new Effect(effectType, speed, 0L, p);
131             case FLAME:
132                 if (flameSpeed == null) {
133                     speed = 4000L;
134                 } else {
135                     speed = flameSpeed;
136                 }
137                 return new Effect(effectType, speed, 0L, new HSBK[0]);
138             default:
139                 throw new IllegalArgumentException("Unknown effect type");
140         }
141     }
142
143     @Override
144     public boolean equals(@Nullable Object o) {
145         if (this == o) {
146             return true;
147         }
148         if (o == null) {
149             return false;
150         }
151         if (o.getClass() != getClass()) {
152             return false;
153         }
154         Effect n = (Effect) o;
155         return n.getType().equals(this.getType()) && n.duration.equals(this.duration) && n.speed.equals(this.speed)
156                 && n.palette == this.palette;
157     }
158
159     @Override
160     public int hashCode() {
161         Long hash = 1L;
162         int prime = 31;
163         hash = prime * hash + type.hashCode();
164         hash = prime * hash + duration;
165         hash = prime * hash + speed;
166         hash = prime * hash + palette.hashCode();
167         return hash.intValue();
168     }
169 }