Skip to content

Commit d3df8c5

Browse files
authored
feat: adding support for roller shutter (#135)
1 parent 1a8dfff commit d3df8c5

File tree

6 files changed

+187
-2
lines changed

6 files changed

+187
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.bigboxer23</groupId>
66
<artifactId>switchbotapi-java</artifactId>
7-
<version>1.2.4</version>
7+
<version>1.2.5</version>
88

99
<name>switchbotapi-java</name>
1010
<url>https://github.com/bigboxer23/switchbotapi-java</url>

src/main/java/com/bigboxer23/switch_bot/IDeviceCommands.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@ public interface IDeviceCommands {
88

99
String TURN_ON = "turnOn";
1010

11+
String SET_POSITION = "setPosition";
12+
1113
DeviceCommand CURTAIN_CLOSE = new DeviceCommand(TURN_OFF, "default");
1214

1315
DeviceCommand CURTAIN_OPEN = new DeviceCommand(TURN_ON, "default");
1416

1517
DeviceCommand PLUG_MINI_OFF = new DeviceCommand(TURN_OFF, "default");
1618

1719
DeviceCommand PLUG_MINI_ON = new DeviceCommand(TURN_ON, "default");
20+
21+
DeviceCommand ROLLER_SHADE_CLOSE = new DeviceCommand(SET_POSITION, 100);
22+
23+
DeviceCommand ROLLER_SHADE_OPEN = new DeviceCommand(SET_POSITION, 0);
24+
25+
static DeviceCommand rollerShadePosition(int position) {
26+
return new DeviceCommand(SET_POSITION, position);
27+
}
1828
}

src/main/java/com/bigboxer23/switch_bot/data/DeviceCommand.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ public DeviceCommand(String command, String parameter) {
1111
this.parameter = parameter;
1212
}
1313

14+
public DeviceCommand(String command, int parameter) {
15+
this.command = command;
16+
this.commandType = "command";
17+
this.parameter = parameter;
18+
}
19+
1420
private String commandType;
1521
private String command;
16-
private String parameter;
22+
private Object parameter;
1723
}

src/test/java/com/bigboxer23/switch_bot/IDeviceCommandsTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,82 @@ public void testCommandObjectsAreNotNull() {
6060
assertNotNull(IDeviceCommands.CURTAIN_OPEN);
6161
assertNotNull(IDeviceCommands.PLUG_MINI_OFF);
6262
assertNotNull(IDeviceCommands.PLUG_MINI_ON);
63+
assertNotNull(IDeviceCommands.ROLLER_SHADE_CLOSE);
64+
assertNotNull(IDeviceCommands.ROLLER_SHADE_OPEN);
65+
}
66+
67+
@Test
68+
public void testRollerShadeCloseCommand() {
69+
DeviceCommand rollerShadeClose = IDeviceCommands.ROLLER_SHADE_CLOSE;
70+
assertNotNull(rollerShadeClose);
71+
assertEquals("setPosition", rollerShadeClose.getCommand());
72+
assertEquals(100, rollerShadeClose.getParameter());
73+
}
74+
75+
@Test
76+
public void testRollerShadeOpenCommand() {
77+
DeviceCommand rollerShadeOpen = IDeviceCommands.ROLLER_SHADE_OPEN;
78+
assertNotNull(rollerShadeOpen);
79+
assertEquals("setPosition", rollerShadeOpen.getCommand());
80+
assertEquals(0, rollerShadeOpen.getParameter());
81+
}
82+
83+
@Test
84+
public void testRollerShadePositionWithIntegerParameter() {
85+
DeviceCommand command = IDeviceCommands.rollerShadePosition(50);
86+
87+
assertNotNull(command);
88+
assertEquals("setPosition", command.getCommand());
89+
assertEquals(50, command.getParameter());
90+
assertEquals("command", command.getCommandType());
91+
}
92+
93+
@Test
94+
public void testRollerShadePositionWithZeroParameter() {
95+
DeviceCommand command = IDeviceCommands.rollerShadePosition(0);
96+
97+
assertNotNull(command);
98+
assertEquals("setPosition", command.getCommand());
99+
assertEquals(0, command.getParameter());
100+
assertEquals("command", command.getCommandType());
101+
}
102+
103+
@Test
104+
public void testRollerShadePositionWithMaximumParameter() {
105+
DeviceCommand command = IDeviceCommands.rollerShadePosition(100);
106+
107+
assertNotNull(command);
108+
assertEquals("setPosition", command.getCommand());
109+
assertEquals(100, command.getParameter());
110+
assertEquals("command", command.getCommandType());
111+
}
112+
113+
@Test
114+
public void testRollerShadePositionWithNegativeParameter() {
115+
DeviceCommand command = IDeviceCommands.rollerShadePosition(-10);
116+
117+
assertNotNull(command);
118+
assertEquals("setPosition", command.getCommand());
119+
assertEquals(-10, command.getParameter());
120+
assertEquals("command", command.getCommandType());
121+
}
122+
123+
@Test
124+
public void testRollerShadePositionCreatesNewInstances() {
125+
DeviceCommand command1 = IDeviceCommands.rollerShadePosition(25);
126+
DeviceCommand command2 = IDeviceCommands.rollerShadePosition(25);
127+
DeviceCommand command3 = IDeviceCommands.rollerShadePosition(75);
128+
129+
assertNotSame(command1, command2);
130+
assertEquals(command1.getCommand(), command2.getCommand());
131+
assertEquals(command1.getParameter(), command2.getParameter());
132+
assertEquals(command1.getCommandType(), command2.getCommandType());
133+
134+
assertNotEquals(command1.getParameter(), command3.getParameter());
135+
}
136+
137+
@Test
138+
public void testSetPositionConstant() {
139+
assertEquals("setPosition", IDeviceCommands.SET_POSITION);
63140
}
64141
}

src/test/java/com/bigboxer23/switch_bot/data/DeviceCommandTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,78 @@ public void testDeviceCommandRoundTripSerialization() throws IOException {
166166
assertEquals(originalCommand.getParameter(), deserializedCommand.getParameter());
167167
assertEquals(originalCommand.getCommandType(), deserializedCommand.getCommandType());
168168
}
169+
170+
@Test
171+
public void testDeviceCommandConstructorWithIntegerParameter() {
172+
DeviceCommand command = new DeviceCommand("setPosition", 75);
173+
174+
assertEquals("setPosition", command.getCommand());
175+
assertEquals(75, command.getParameter());
176+
assertEquals("command", command.getCommandType());
177+
}
178+
179+
@Test
180+
public void testDeviceCommandConstructorWithZeroIntegerParameter() {
181+
DeviceCommand command = new DeviceCommand("setPosition", 0);
182+
183+
assertEquals("setPosition", command.getCommand());
184+
assertEquals(0, command.getParameter());
185+
assertEquals("command", command.getCommandType());
186+
}
187+
188+
@Test
189+
public void testDeviceCommandConstructorWithNegativeIntegerParameter() {
190+
DeviceCommand command = new DeviceCommand("setPosition", -10);
191+
192+
assertEquals("setPosition", command.getCommand());
193+
assertEquals(-10, command.getParameter());
194+
assertEquals("command", command.getCommandType());
195+
}
196+
197+
@Test
198+
public void testDeviceCommandJsonSerializationWithIntegerParameter() throws IOException {
199+
DeviceCommand command = new DeviceCommand("setPosition", 50);
200+
201+
String json = adapter.toJson(command);
202+
203+
assertNotNull(json);
204+
assertTrue(json.contains("\"command\":\"setPosition\""));
205+
assertTrue(json.contains("\"parameter\":50"));
206+
assertFalse(json.contains("\"parameter\":\"50\""));
207+
assertTrue(json.contains("\"commandType\":\"command\""));
208+
}
209+
210+
@Test
211+
public void testDeviceCommandJsonSerializationIntegerVsString() throws IOException {
212+
DeviceCommand intCommand = new DeviceCommand("setPosition", 75);
213+
DeviceCommand stringCommand = new DeviceCommand("setPosition", "75");
214+
215+
String intJson = adapter.toJson(intCommand);
216+
String stringJson = adapter.toJson(stringCommand);
217+
218+
assertTrue(intJson.contains("\"parameter\":75"));
219+
assertTrue(stringJson.contains("\"parameter\":\"75\""));
220+
assertNotEquals(intJson, stringJson);
221+
}
222+
223+
@Test
224+
public void testDeviceCommandEqualsWithIntegerParameter() {
225+
DeviceCommand command1 = new DeviceCommand("setPosition", 100);
226+
DeviceCommand command2 = new DeviceCommand("setPosition", 100);
227+
DeviceCommand command3 = new DeviceCommand("setPosition", 50);
228+
229+
assertEquals(command1, command2);
230+
assertEquals(command1.hashCode(), command2.hashCode());
231+
assertNotEquals(command1, command3);
232+
assertNotEquals(command1.hashCode(), command3.hashCode());
233+
}
234+
235+
@Test
236+
public void testDeviceCommandIntegerAndStringParametersNotEqual() {
237+
DeviceCommand intCommand = new DeviceCommand("setPosition", 100);
238+
DeviceCommand stringCommand = new DeviceCommand("setPosition", "100");
239+
240+
assertNotEquals(intCommand, stringCommand);
241+
assertNotEquals(intCommand.hashCode(), stringCommand.hashCode());
242+
}
169243
}

src/test/java/com/bigboxer23/switch_bot/integration/SwitchBotApiIntegrationTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.bigboxer23.switch_bot.IDeviceTypes;
77
import com.bigboxer23.switch_bot.SwitchBotApi;
88
import com.bigboxer23.switch_bot.data.Device;
9+
import com.bigboxer23.switch_bot.data.IApiResponse;
910
import com.bigboxer23.utils.command.Command;
1011
import com.bigboxer23.utils.properties.PropertyUtils;
1112
import java.io.IOException;
@@ -26,6 +27,23 @@ public void testGetDevices() throws IOException {
2627
assertNotNull(devices.get(0).getDeviceId());
2728
}
2829

30+
@Test
31+
public void testRollerShadeClosed() throws IOException {
32+
instance.getDeviceApi().getDevices().stream()
33+
.filter(device -> device.getDeviceType().equals(IDeviceTypes.ROLLER_SHADE))
34+
.filter(Device::isMaster)
35+
.findAny()
36+
.ifPresent(device -> {
37+
try {
38+
IApiResponse response = instance.getDeviceApi()
39+
.sendDeviceControlCommands(device.getDeviceId(), IDeviceCommands.ROLLER_SHADE_CLOSE);
40+
System.out.println(response);
41+
} catch (IOException theE) {
42+
theE.printStackTrace();
43+
}
44+
});
45+
}
46+
2947
@Test
3048
public void getDeviceNameFromId() throws IOException {
3149
Device device = instance.getDeviceApi().getDevices().get(0);

0 commit comments

Comments
 (0)