Skip to content

Commit b4d758a

Browse files
committed
1. Refactoring actuator plugin. 2. Rename sand-demo-protocols to sand-demo-protocol.
1 parent 583d4c9 commit b4d758a

File tree

32 files changed

+68
-46
lines changed

32 files changed

+68
-46
lines changed

client/actuator/src/main/java/com/thefirstlineofcode/sand/client/actuator/Actuator.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.thefirstlineofcode.basalt.oxm.IOxmFactory;
1212
import com.thefirstlineofcode.basalt.oxm.coc.CocParserFactory;
13+
import com.thefirstlineofcode.basalt.oxm.coc.annotations.ProtocolObject;
1314
import com.thefirstlineofcode.basalt.xmpp.core.IqProtocolChain;
1415
import com.thefirstlineofcode.basalt.xmpp.core.JabberId;
1516
import com.thefirstlineofcode.basalt.xmpp.core.Protocol;
@@ -114,11 +115,17 @@ protected <T> void execute(Iq iq, Execution execution) {
114115
@SuppressWarnings("unchecked")
115116
private <T> IExecutor<T> createExecutor(T action) throws ProtocolException {
116117
IExecutorFactory<T> executorFactory = (IExecutorFactory<T>)executorFactories.get(action.getClass());
118+
117119
if (executorFactory != null)
118120
return executorFactory.create();
119121

120122
return null;
121123
}
124+
125+
@SuppressWarnings("unchecked")
126+
private <T> void injectWorker(IWorkerAware<T> executor, Object worker) {
127+
executor.setWorker((T)worker);
128+
}
122129

123130
private IExecutor<?> createExecutor(Class<? extends IExecutor<?>> executorType) {
124131
IExecutor<?> executor = null;
@@ -198,26 +205,44 @@ public void stop() {
198205
}
199206

200207
@Override
201-
public <T> void registerExecutor(Protocol protocol, Class<T> actionType, Class<? extends IExecutor<T>> executorType) {
202-
registerExecutorFactory(new CreateByTypeExecutorFactory<T>(protocol, actionType, executorType));
208+
public <T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType) {
209+
registerExecutorFactory(new CreateByTypeExecutorFactory<T>(actionType, executorType, null));
210+
}
211+
212+
@Override
213+
public <T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType, Object worker) {
214+
registerExecutorFactory(new CreateByTypeExecutorFactory<T>(actionType, executorType, worker));
203215
}
204216

205217
private class CreateByTypeExecutorFactory<T> implements IExecutorFactory<T> {
206218
private Protocol protocol;
207219
private Class<T> actionType;
208220
private Class<? extends IExecutor<T>> executorType;
221+
private Object worker;
209222

210-
public CreateByTypeExecutorFactory(Protocol protocol, Class<T> actionType,
211-
Class<? extends IExecutor<T>> executorType) {
212-
this.protocol = protocol;
223+
public CreateByTypeExecutorFactory(Class<T> actionType,
224+
Class<? extends IExecutor<T>> executorType, Object worker) {
213225
this.actionType = actionType;
214226
this.executorType = executorType;
227+
this.worker = worker;
228+
229+
ProtocolObject protocolObject = actionType.getAnnotation(ProtocolObject.class);
230+
if (protocolObject == null)
231+
throw new IllegalArgumentException(String.format("Not a protocol object. Object type: %s.",
232+
actionType.getName()));
233+
234+
protocol = new Protocol(protocolObject.namespace(), protocolObject.localName());
215235
}
216236

217237
@SuppressWarnings("unchecked")
218238
@Override
219239
public IExecutor<T> create() {
220-
return (IExecutor<T>)createExecutor(executorType);
240+
IExecutor<T> executor = (IExecutor<T>)createExecutor(executorType);
241+
if (executor instanceof IWorkerAware) {
242+
injectWorker((IWorkerAware<?>)executor, worker);
243+
}
244+
245+
return executor;
221246
}
222247

223248
@Override

client/actuator/src/main/java/com/thefirstlineofcode/sand/client/actuator/IActuator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.thefirstlineofcode.sand.client.actuator;
22

3-
import com.thefirstlineofcode.basalt.xmpp.core.Protocol;
4-
53
public interface IActuator {
6-
<T> void registerExecutor(Protocol protocol, Class<T> actionType, Class<? extends IExecutor<T>> executorType);
4+
<T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType);
5+
<T> void registerExecutor(Class<T> actionType, Class<? extends IExecutor<T>> executorType, Object worker);
76
<T> void registerExecutorFactory(IExecutorFactory<T> executorFactory);
87
boolean isExecutorRegistered(Class<?> actionType);
98
boolean unregisterExecutor(Class<?> actionType);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.thefirstlineofcode.sand.client.actuator;
2+
3+
public interface IWorkerAware<T> {
4+
void setWorker(T worker);
5+
}

demo/app-android/app/src/main/java/com/thefirstlineofcode/sand/demo/app/android/ThingsAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private static class LanNodeViewHolder {
132132
private String[] getAuthorizedThingActions(String model) {
133133
if (model.startsWith("LGE-")) {
134134
return new String[] {"Change Working Mode"};
135-
} else if (model.equals("LGSC-01")) {
135+
} else if ("LGSC-01".equals(model)) {
136136
return new String[] {
137137
"Change Working Mode",
138138
"Reset DAC Service",
@@ -156,6 +156,8 @@ private String[] getAuthorizedThingActions(String model) {
156156
};
157157
} else if (model.startsWith("SL-")) {
158158
return new String[] {"Flash", "Turn On", "Turn Off", "Stop", "Shutdown System"};
159+
} else if ("HAT".equals(model)) {
160+
return new String[] {"Flash", "Turn On", "Turn Off"};
159161
} else {
160162
throw new RuntimeException(String.format("Unknown thing model: %s.", model));
161163
}

demo/client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</dependency>
2929
<dependency>
3030
<groupId>com.thefirstlineofcode.sand.demo</groupId>
31-
<artifactId>sand-demo-protocols</artifactId>
31+
<artifactId>sand-demo-protocol</artifactId>
3232
</dependency>
3333
</dependencies>
3434

demo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<name>Sand IoT Demo Projects</name>
1818

1919
<modules>
20-
<module>protocols</module>
20+
<module>protocol</module>
2121
<module>client</module>
2222
<module>server</module>
2323
<module>server-lite</module>

demo/protocols/pom.xml renamed to demo/protocol/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</parent>
1414

1515
<groupId>com.thefirstlineofcode.sand.demo</groupId>
16-
<artifactId>sand-demo-protocols</artifactId>
17-
<name>Sand demo protocols</name>
16+
<artifactId>sand-demo-protocol</artifactId>
17+
<name>Sand demo protocol</name>
1818

1919
<dependencies>
2020
<dependency>

0 commit comments

Comments
 (0)