Skip to content

Commit 9604dfc

Browse files
committed
1. Remove restart action from supported actions of edge thing. 2. Send initial presencre while thing has been available.
1 parent b776fdb commit 9604dfc

File tree

15 files changed

+102
-112
lines changed

15 files changed

+102
-112
lines changed

client/edge/src/main/java/com/thefirstlineofcode/sand/client/edge/AbstractEdgeThing.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.thefirstlineofcode.basalt.xmpp.Constants;
2828
import com.thefirstlineofcode.basalt.xmpp.core.ProtocolException;
2929
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.FeatureNotImplemented;
30+
import com.thefirstlineofcode.basalt.xmpp.im.stanza.Presence;
3031
import com.thefirstlineofcode.chalk.core.AuthFailureException;
3132
import com.thefirstlineofcode.chalk.core.IChatClient;
3233
import com.thefirstlineofcode.chalk.core.StandardChatClient;
@@ -272,8 +273,6 @@ public void run() {
272273
printConsoleHelp();
273274
} else if ("exit".equals(command)) {
274275
stop();
275-
} else if ("restart".equals(command)) {
276-
restart();
277276
} else {
278277
System.out.println(String.format("Unknown command: '%s'", command));
279278
printConsoleHelp();
@@ -287,7 +286,6 @@ public void run() {
287286
private void printConsoleHelp() {
288287
System.out.println("Commands:");
289288
System.out.println("help Display help information.");
290-
System.out.println("restart Restart program.");
291289
System.out.println("exit Exit program.");
292290
System.out.print("$");
293291
}
@@ -517,12 +515,6 @@ public boolean removeConnectionListener(IConnectionListener connectionListener)
517515
return connectionListeners.remove(connectionListener);
518516
}
519517

520-
@Override
521-
public void restart() {
522-
stop();
523-
start();
524-
}
525-
526518
@Override
527519
public void messageReceived(String message) {}
528520

@@ -573,6 +565,9 @@ protected void connected(IChatClient chatClient) {
573565

574566
startIotComponents();
575567
startAutoReconnectThread();
568+
569+
// Initial presence
570+
chatClient.getChatServices().getPresenceService().send(new Presence());
576571
}
577572

578573
protected void FailedToConnect(ConnectionException e) {

client/edge/src/main/java/com/thefirstlineofcode/sand/client/edge/EdgeThingPlugin.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.thefirstlineofcode.chalk.core.IChatSystem;
99
import com.thefirstlineofcode.chalk.core.IPlugin;
1010
import com.thefirstlineofcode.sand.client.actuator.ActuatorPlugin;
11-
import com.thefirstlineofcode.sand.protocols.edge.Restart;
1211
import com.thefirstlineofcode.sand.protocols.edge.ShutdownSystem;
1312
import com.thefirstlineofcode.sand.protocols.edge.Stop;
1413

@@ -21,10 +20,6 @@ public void init(IChatSystem chatSystem, Properties properties) {
2120
new CocParserFactory<>(Stop.class));
2221
chatSystem.registerTranslator(Stop.class,
2322
new CocTranslatorFactory<>(Stop.class));
24-
chatSystem.registerParser(new IqProtocolChain(Restart.PROTOCOL),
25-
new CocParserFactory<>(Restart.class));
26-
chatSystem.registerTranslator(Restart.class,
27-
new CocTranslatorFactory<>(Restart.class));
2823
chatSystem.registerParser(new IqProtocolChain(ShutdownSystem.PROTOCOL),
2924
new CocParserFactory<>(ShutdownSystem.class));
3025
chatSystem.registerTranslator(ShutdownSystem.class,
@@ -35,8 +30,6 @@ public void init(IChatSystem chatSystem, Properties properties) {
3530
public void destroy(IChatSystem chatSystem) {
3631
chatSystem.unregisterTranslator(ShutdownSystem.class);
3732
chatSystem.unregisterParser(new IqProtocolChain(ShutdownSystem.PROTOCOL));
38-
chatSystem.unregisterTranslator(Restart.class);
39-
chatSystem.unregisterParser(new IqProtocolChain(Restart.PROTOCOL));
4033
chatSystem.unregisterTranslator(Stop.class);
4134
chatSystem.unregisterParser(new IqProtocolChain(Stop.PROTOCOL));
4235
chatSystem.unregister(ActuatorPlugin.class);

client/edge/src/main/java/com/thefirstlineofcode/sand/client/edge/RestartExecutor.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

client/thing/src/main/java/com/thefirstlineofcode/sand/client/thing/IThing.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,5 @@ public interface IThing {
1818
void stop() throws ExecutionException;
1919
boolean isStopped();
2020

21-
void restart() throws ExecutionException;
22-
2321
void shutdownSystem(boolean restart) throws ExecutionException;
2422
}

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
import com.thefirstlineofcode.basalt.xmpp.core.stanza.Stanza;
4545
import com.thefirstlineofcode.basalt.xmpp.core.stanza.error.StanzaError;
4646
import com.thefirstlineofcode.basalt.xmpp.core.stream.error.StreamError;
47+
import com.thefirstlineofcode.basalt.xmpp.im.stanza.Presence;
4748
import com.thefirstlineofcode.chalk.core.ErrorException;
4849
import com.thefirstlineofcode.chalk.core.IChatClient;
4950
import com.thefirstlineofcode.chalk.core.IChatServices;
5051
import com.thefirstlineofcode.chalk.core.IErrorListener;
5152
import com.thefirstlineofcode.chalk.core.ITask;
5253
import com.thefirstlineofcode.chalk.core.IUnidirectionalStream;
54+
import com.thefirstlineofcode.chalk.im.stanza.IPresenceListener;
5355
import com.thefirstlineofcode.sand.client.operator.IOperator;
5456
import com.thefirstlineofcode.sand.client.remoting.IRemoting;
5557
import com.thefirstlineofcode.sand.client.sensor.IDataProcessor;
@@ -64,7 +66,6 @@
6466
import com.thefirstlineofcode.sand.demo.protocols.DeliverTemperatureToOwner;
6567
import com.thefirstlineofcode.sand.demo.protocols.RecordedVideo;
6668
import com.thefirstlineofcode.sand.demo.protocols.VideoRecorded;
67-
import com.thefirstlineofcode.sand.protocols.edge.Restart;
6869
import com.thefirstlineofcode.sand.protocols.edge.ShutdownSystem;
6970
import com.thefirstlineofcode.sand.protocols.edge.Stop;
7071
import com.thefirstlineofcode.sand.protocols.lora.dac.ResetLoraDacService;
@@ -127,6 +128,9 @@ protected void onCreate(Bundle savedInstanceState) {
127128
}
128129

129130
IChatClient chatClient = ChatClientSingleton.get(this);
131+
// Initial presence.
132+
chatClient.getChatServices().getPresenceService().send(new Presence());
133+
130134
reportService = chatClient.createApi(IReportService.class);
131135

132136
listenNetConfigEvents();
@@ -400,6 +404,27 @@ public void retrieved(AuthorizedThings authorizedThings) {
400404

401405
expandNodes(elvThings, things);
402406
}
407+
408+
IChatClient chatClient = ChatClientSingleton.get(this);
409+
chatClient.getChatServices().getPresenceService().addListener(new IPresenceListener() {
410+
411+
@Override
412+
public void received(Presence presence) {
413+
AuthorizedThing[] things = thingsAdapter.getThings();
414+
if (things == null || things.length == 0) {
415+
return;
416+
}
417+
418+
for (int i = 0; i < things.length; i++) {
419+
if (things[i].getThingName().equals(presence.getFrom().getNode())) {
420+
final String thingId = things[i].getThingId();
421+
runOnUiThread(() -> Toast.makeText(MainActivity.this,
422+
String.format("Thing %s is avaiable now.", thingId),
423+
Toast.LENGTH_LONG).show());
424+
}
425+
}
426+
}
427+
});
403428
});
404429
}
405430

@@ -849,15 +874,11 @@ public void stop(JabberId target) {
849874
controlThing(target, new Stop(), "Stop");
850875
}
851876

852-
public void restart(JabberId target) {
853-
controlThing(target, new Restart(), "Restart");
854-
}
855-
856877
public void shutdownSystem(JabberId target) {
857878
ShutdownSystem shutdownSystem = new ShutdownSystem();
858879

859880
AlertDialog.Builder builder = new AlertDialog.Builder(this);
860-
builder.setTitle("Restart").setMultiChoiceItems(new String[] {"Restart after shutdown"}, new boolean[] {false},
881+
builder.setTitle("Shutdown System").setMultiChoiceItems(new String[] {"Restart after shutdown"}, new boolean[] {false},
861882
new DialogInterface.OnMultiChoiceClickListener() {
862883
@Override
863884
public void onClick(DialogInterface dialog, int which, boolean isChecked) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public ThingsAdapter(MainActivity mainActivity, String host, AuthorizedThing[] t
3333
public void setThings(AuthorizedThing[] things) {
3434
this.things = things;
3535
}
36+
public AuthorizedThing[] getThings() {
37+
return things;
38+
}
3639

3740
@Override
3841
public int getGroupCount() {
@@ -140,7 +143,6 @@ private String[] getAuthorizedThingActions(String model) {
140143
"Show Recorded Videos",
141144
"Open Live Streaming",
142145
"Stop",
143-
"Restart",
144146
"Shutdown System"
145147
};
146148
} else if (model.startsWith("LG-")) {
@@ -307,9 +309,6 @@ public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
307309
case "Stop":
308310
mainActivity.stop(getJidTargetByThingId(thingId));
309311
break;
310-
case "Restart":
311-
mainActivity.restart(getJidTargetByThingId(thingId));
312-
break;
313312
case "Shutdown System":
314313
mainActivity.shutdownSystem(getJidTargetByThingId(thingId));
315314
break;

demo/app-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ allprojects {
4343

4444
maven {
4545
allowInsecureProtocol = true
46-
url 'http://47.115.36.99/repository/maven-releases'
46+
url 'http://120.25.166.188:9090/repository/maven-releases'
4747
}
4848
}
4949
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thefirstlineofcode.sand.demo.server;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.thefirstlineofcode.basalt.xmpp.core.JabberId;
7+
import com.thefirstlineofcode.basalt.xmpp.im.stanza.Presence;
8+
import com.thefirstlineofcode.granite.framework.core.annotations.BeanDependency;
9+
import com.thefirstlineofcode.granite.framework.core.pipeline.stages.event.IEventContext;
10+
import com.thefirstlineofcode.granite.framework.core.pipeline.stages.event.IEventListener;
11+
import com.thefirstlineofcode.granite.framework.im.IResource;
12+
import com.thefirstlineofcode.granite.framework.im.IResourcesService;
13+
import com.thefirstlineofcode.granite.framework.im.ResourceAvailableEvent;
14+
import com.thefirstlineofcode.sand.server.things.IThingManager;
15+
16+
public class EdgeThingAvailableEventListener implements IEventListener<ResourceAvailableEvent> {
17+
private static final Logger logger = LoggerFactory.getLogger(EdgeThingAvailableEventListener.class);
18+
19+
@BeanDependency
20+
private IThingManager thingManager;
21+
22+
@BeanDependency
23+
private IAclService aclService;
24+
25+
@BeanDependency
26+
private IResourcesService resourceService;
27+
28+
@Override
29+
public void process(IEventContext context, ResourceAvailableEvent event) {
30+
if (!thingManager.thingNameExists(event.getJid().getNode())) {
31+
if (logger.isDebugEnabled())
32+
logger.debug("Not a thing. Node name: {}.", event.getJid().getNode());
33+
return;
34+
}
35+
36+
String thingId = thingManager.getThingIdByThingName(event.getJid().getNode());
37+
String owner = aclService.getOwner(thingId);
38+
39+
if (owner == null) {
40+
if (logger.isErrorEnabled())
41+
logger.error("Thing owner not found. Thing name: {}.", event.getJid().getNode());
42+
return;
43+
}
44+
45+
String domain = event.getJid().getDomain();
46+
JabberId ownerJid = new JabberId(owner, domain);
47+
48+
IResource[] ownerResources = resourceService.getResources(ownerJid);
49+
if (ownerResources == null || ownerResources.length == 0)
50+
return;
51+
52+
Presence presence = new Presence();
53+
presence.setFrom(event.getJid());
54+
55+
for (IResource resource : ownerResources) {
56+
context.write(resource.getJid(), presence);
57+
}
58+
}
59+
60+
}

demo/server/src/main/java/com/thefirstlineofcode/sand/demo/server/PipelineExtendersContributor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.thefirstlineofcode.basalt.xmpp.core.IqProtocolChain;
66
import com.thefirstlineofcode.granite.framework.core.pipeline.stages.IPipelineExtendersConfigurator;
77
import com.thefirstlineofcode.granite.framework.core.pipeline.stages.PipelineExtendersConfigurator;
8+
import com.thefirstlineofcode.granite.framework.im.ResourceAvailableEvent;
89
import com.thefirstlineofcode.sand.demo.protocols.AccessControlList;
910
import com.thefirstlineofcode.sand.demo.protocols.AuthorizedThings;
1011
import com.thefirstlineofcode.sand.demo.protocols.DeliverTemperatureToOwner;
@@ -45,6 +46,7 @@ protected void configure(IPipelineExtendersConfigurator configurator) {
4546
configurator.registerEventListener(ThingRegistrationEvent.class, new ThingRegistrationListener());
4647
configurator.registerEventListener(NodeConfirmationRequestEvent.class, new NodeConfirmationRequestListener());
4748
configurator.registerEventListener(NodeAdditionEvent.class, new NodeAdditionListener());
49+
configurator.registerEventListener(ResourceAvailableEvent.class, new EdgeThingAvailableEventListener());
4850

4951
configurator.registerPipelinePreprocessor(new AclPipelinePreprocessor());
5052

demo/things/lgsc-01/src/main/java/com/thefirstlineofcode/sand/demo/things/lgsc01/LoraGatewayAndCamera.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import com.thefirstlineofcode.sand.client.concentrator.SyncNodesExecutor;
4040
import com.thefirstlineofcode.sand.client.edge.AbstractEdgeThing;
4141
import com.thefirstlineofcode.sand.client.edge.ResponseInAdvanceExecutor;
42-
import com.thefirstlineofcode.sand.client.edge.RestartExecutor;
4342
import com.thefirstlineofcode.sand.client.edge.ShutdownSystemExecutor;
4443
import com.thefirstlineofcode.sand.client.edge.StopExecutor;
4544
import com.thefirstlineofcode.sand.client.friends.IFollowProcessor;
@@ -65,7 +64,6 @@
6564
import com.thefirstlineofcode.sand.demo.protocols.VideoRecorded;
6665
import com.thefirstlineofcode.sand.demo.protocols.VideoRecorded.RecordingReason;
6766
import com.thefirstlineofcode.sand.protocols.actuator.ExecutionException;
68-
import com.thefirstlineofcode.sand.protocols.edge.Restart;
6967
import com.thefirstlineofcode.sand.protocols.edge.ShutdownSystem;
7068
import com.thefirstlineofcode.sand.protocols.edge.Stop;
7169
import com.thefirstlineofcode.sand.protocols.lora.dac.ResetLoraDacService;
@@ -277,7 +275,6 @@ private void registerExecutors(IActuator actuator) {
277275

278276
private void registerExecutorsForEdgeThing(IActuator actuator) {
279277
actuator.registerExecutorFactory(createStopExecutatorFactory());
280-
actuator.registerExecutorFactory(createRestartExecutatorFactory());
281278
actuator.registerExecutorFactory(createShutdownSystemExecutatorFactory());
282279
}
283280

@@ -303,28 +300,6 @@ public IExecutor<ShutdownSystem> create() {
303300
};
304301
}
305302

306-
private IExecutorFactory<?> createRestartExecutatorFactory() {
307-
return new IExecutorFactory<Restart>() {
308-
private IExecutor<Restart> executor = new ResponseInAdvanceExecutor<Restart>(
309-
new RestartExecutor(LoraGatewayAndCamera.this), LoraGatewayAndCamera.this);
310-
311-
@Override
312-
public Protocol getProtocol() {
313-
return Restart.PROTOCOL;
314-
}
315-
316-
@Override
317-
public Class<Restart> getActionType() {
318-
return Restart.class;
319-
}
320-
321-
@Override
322-
public IExecutor<Restart> create() {
323-
return executor;
324-
}
325-
};
326-
}
327-
328303
private IExecutorFactory<?> createStopExecutatorFactory() {
329304
return new IExecutorFactory<Stop>() {
330305
private IExecutor<Stop> executor = new ResponseInAdvanceExecutor<Stop>(
@@ -744,6 +719,9 @@ private String getVideoOutputPath() {
744719
}
745720

746721
public static Capability getRequestedWebcamCapability(String sRequestedWebcamCapability) {
722+
if (sRequestedWebcamCapability == null)
723+
return DEFAULLT_REQUESTED_WEBCAM_CAPABILITY;
724+
747725
StringTokenizer st = new StringTokenizer(sRequestedWebcamCapability, ",");
748726
if (st.countTokens() != 3)
749727
throw new IllegalArgumentException("Illegal webcam capability format. Capability format: WIDTH,HEIGHT,MAX_FPS.");

0 commit comments

Comments
 (0)