Skip to content

Commit 3314505

Browse files
committed
1. Small refactoring. 2. Fixed several bugs of lora gateway emulator.
1 parent 145048c commit 3314505

File tree

23 files changed

+282
-109
lines changed

23 files changed

+282
-109
lines changed

client/concentrator/src/main/java/com/thefirstlineofcode/sand/client/concentrator/Concentrator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ protected <PA extends IAddress> void executeOnLanNode(Iq iq, Object action, bool
305305
}
306306

307307
try {
308-
if (lanTraceable) {
308+
if (lanTraceable) {
309309
LanExecution lanExecution = new LanExecution(ThingsTinyId.createRequestId(0), action);
310310
traceLanExecution(iq.getFrom(), iq.getTo(), iq.getId(), node, lanExecution, lanTimeout);
311311
communicator.send((PA)node.getCommunicationNet().parse(node.getAddress()), ObxFactory.getInstance().toBinary(lanExecution));

client/lora-dac/src/main/java/com/thefirstlineofcode/sand/client/lora/dac/LoraDacClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ public void received(LoraAddress from, byte[] data) {
112112
try {
113113
Allocation allocation = (Allocation)obxFactory.toObject(data);
114114
LoraAddress[] uplinkAddresses = getUplinkAddresses(allocation.getUplinkChannelBegin(),
115-
allocation.getUplinkChannelEnd(), allocation.getUplinkAddressHighByte(),
116-
allocation.getUplinkAddressLowByte());
115+
allocation.getUplinkChannelEnd(), allocation.getUplinkAddress());
117116
if (listener != null) {
118117
listener.allocated(uplinkAddresses,
119118
new LoraAddress(allocation.getAllocatedAddress()));
@@ -164,13 +163,13 @@ public void isConfigured(String thingId) {
164163
}
165164

166165
private LoraAddress[] getUplinkAddresses(int gatewayUplinkChannelBegin, int gatewayUplinkChannelEnd,
167-
byte gatewayUplinkAddressHighByte, byte gatewayUplinkAddressLowByte) {
166+
byte[] gatewayUplinkAddress) {
168167
int uplinkAddressesSize = gatewayUplinkChannelEnd - gatewayUplinkChannelBegin + 1;
169168
LoraAddress[] addresses = new LoraAddress[uplinkAddressesSize];
170169

171170
for (int i = 0; i < uplinkAddressesSize; i++) {
172-
addresses[i] = new LoraAddress(gatewayUplinkAddressHighByte,
173-
gatewayUplinkAddressLowByte, (byte)(gatewayUplinkChannelBegin + i));
171+
addresses[i] = new LoraAddress(gatewayUplinkAddress[0],
172+
gatewayUplinkAddress[1], (byte)(gatewayUplinkChannelBegin + i));
174173
}
175174

176175
return addresses;

client/lora-dac/src/main/java/com/thefirstlineofcode/sand/client/lora/dac/LoraDacService.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ public enum State {
5858

5959
private int uplinkChannelBegin;
6060
private int uplinkChannelEnd;
61-
private byte uplinkAddressHighByte;
62-
private byte uplinkAddressLowByte;
61+
private byte[] uplinkAddress;
6362

6463
private LoraAddress dacServiceAddress;
6564
private byte thingCommunicationChannel;
@@ -302,8 +301,7 @@ protected Allocation allocate() {
302301
Allocation allocation = new Allocation();
303302
allocation.setUplinkChannelBegin(uplinkChannelBegin);
304303
allocation.setUplinkChannelEnd(uplinkChannelEnd);
305-
allocation.setUplinkAddressHighByte(uplinkAddressHighByte);
306-
allocation.setUplinkAddressLowByte(uplinkAddressLowByte);
304+
allocation.setUplinkAddress(uplinkAddress);
307305

308306
int nodeLanId = concentrator.getBestSuitedNewLanId();
309307
nodeAllocatedAddress = new LoraAddress(new byte[] {0x0, Byte.parseByte(String.valueOf(nodeLanId)), thingCommunicationChannel});
@@ -543,13 +541,12 @@ public void setUplinkAddress(byte[] uplinkAddress) {
543541
if (uplinkAddress == null || uplinkAddress.length != 2)
544542
throw new IllegalArgumentException("Null or illegal uplink address.");
545543

546-
this.uplinkAddressHighByte = uplinkAddress[0];
547-
this.uplinkAddressLowByte = uplinkAddress[1];
544+
this.uplinkAddress = uplinkAddress;
548545
}
549546

550547
@Override
551548
public byte[] getUplinkAddress() {
552-
return new byte[] {uplinkAddressHighByte, uplinkAddressLowByte};
549+
return uplinkAddress;
553550
}
554551

555552
@Override

client/lora-gateway/src/main/java/com/thefirstlineofcode/sand/client/lora/gateway/ChangeWorkingModeExecutor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
import com.thefirstlineofcode.basalt.xmpp.core.ProtocolException;
44
import com.thefirstlineofcode.basalt.xmpp.core.stanza.Iq;
55
import com.thefirstlineofcode.sand.client.actuator.IExecutor;
6+
import com.thefirstlineofcode.sand.client.actuator.IThingControllerAware;
67
import com.thefirstlineofcode.sand.protocols.lora.gateway.ChangeWorkingMode;
78

8-
public class ChangeWorkingModeExecutor implements IExecutor<ChangeWorkingMode> {
9+
public class ChangeWorkingModeExecutor implements IExecutor<ChangeWorkingMode>,
10+
IThingControllerAware<ILoraGateway> {
911
private ILoraGateway loraGateway;
1012

13+
public ChangeWorkingModeExecutor() {
14+
this(null);
15+
}
16+
1117
public ChangeWorkingModeExecutor(ILoraGateway loraGateway) {
1218
this.loraGateway = loraGateway;
1319
}
@@ -17,4 +23,9 @@ public Object execute(Iq iq, ChangeWorkingMode changeWorkingMode) throws Protoco
1723
loraGateway.setWorkingMode(changeWorkingMode.getWorkingMode());
1824
return null;
1925
}
26+
27+
@Override
28+
public void setThingController(ILoraGateway loraGateway) {
29+
this.loraGateway = loraGateway;
30+
}
2031
}

client/lora-gateway/src/main/java/com/thefirstlineofcode/sand/client/lora/gateway/ILoraGateway.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import com.thefirstlineofcode.sand.protocols.thing.lora.LoraAddress;
1010

1111
public interface ILoraGateway {
12-
public static final byte DEFAULT_UPLINK_ADDRESS_HIGH_BYTE = 0x00;
13-
public static final byte DEFAULT_UPLINK_ADDRESS_LOW_BYTE = 0x00;
12+
public static final byte[] DEFAULT_DOWNLINK_ADDRESS = new byte[] {0x00, 0x00};
13+
public static final byte[] DEFAULT_UPLINK_ADDRESS = new byte[] {0x00, 0x00};
1414
public static final byte DEFAULT_THING_COMMUNICATION_CHANNEL = 0x17;
1515

1616
public enum Mode {

client/lora-gateway/src/main/java/com/thefirstlineofcode/sand/client/lora/gateway/LoraGateway.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,15 @@ public ILoraDacService getDacService() {
108108
if (dacService == null) {
109109
INotificationService notificationService = chatServices.createApi(INotificationService.class);
110110
INotifier notifier = notificationService.getNotifier();
111-
dacService = new LoraDacService(downlinkCommunicator, 0, uplinkCommunicators.size() - 1,
112-
new byte[] {DEFAULT_UPLINK_ADDRESS_HIGH_BYTE, DEFAULT_UPLINK_ADDRESS_LOW_BYTE},
113-
getConcentrator(), notifier);
114-
dacService.setThingCommunicationChannel(thingCommunicationChannel);
111+
112+
if (uplinkCommunicators.size() == 1 && downlinkCommunicator == uplinkCommunicators.get(0)) {
113+
dacService = new LoraDacService(downlinkCommunicator, thingCommunicationChannel, thingCommunicationChannel,
114+
DEFAULT_UPLINK_ADDRESS, getConcentrator(), notifier);
115+
} else {
116+
dacService = new LoraDacService(downlinkCommunicator, 0, uplinkCommunicators.size() - 1,
117+
DEFAULT_UPLINK_ADDRESS, getConcentrator(), notifier);
118+
dacService.setThingCommunicationChannel(thingCommunicationChannel);
119+
}
115120
}
116121

117122
return dacService;
@@ -140,17 +145,16 @@ protected void doStart() {
140145

141146
try {
142147
if (uplinkCommunicators.size() == 1 && downlinkCommunicator.equals(uplinkCommunicators.get(0))) {
143-
downlinkCommunicator.changeAddress(new LoraAddress(
144-
new byte[] {DEFAULT_UPLINK_ADDRESS_HIGH_BYTE, DEFAULT_UPLINK_ADDRESS_LOW_BYTE, 0x00}), false);
148+
downlinkCommunicator.changeAddress(new LoraAddress(new byte[] {DEFAULT_DOWNLINK_ADDRESS[0],
149+
DEFAULT_DOWNLINK_ADDRESS[1], thingCommunicationChannel}), false);
145150
} else {
146151
downlinkCommunicator.changeAddress(new LoraAddress(
147-
new byte[] {DEFAULT_UPLINK_ADDRESS_HIGH_BYTE,
148-
DEFAULT_UPLINK_ADDRESS_LOW_BYTE, thingCommunicationChannel}), false);
152+
new byte[] {DEFAULT_DOWNLINK_ADDRESS[0], DEFAULT_DOWNLINK_ADDRESS[1],
153+
thingCommunicationChannel}), false);
149154

150155
for (int i = 0; i < uplinkCommunicators.size(); i++) {
151156
uplinkCommunicators.get(i).changeAddress(new LoraAddress(
152-
new byte[] {DEFAULT_UPLINK_ADDRESS_HIGH_BYTE,
153-
DEFAULT_UPLINK_ADDRESS_LOW_BYTE, (byte)i}), false);
157+
new byte[] {DEFAULT_UPLINK_ADDRESS[0], DEFAULT_UPLINK_ADDRESS[1], (byte)i}), false);
154158
}
155159
}
156160
} catch (CommunicationException e) {
@@ -230,8 +234,7 @@ public LoraAddress[] getUplinkAddresses() {
230234
LoraAddress[] addresses = new LoraAddress[uplinkCommunicators.size()];
231235

232236
for (int i = 0; i < uplinkCommunicators.size(); i++) {
233-
addresses[i] = new LoraAddress(DEFAULT_UPLINK_ADDRESS_HIGH_BYTE,
234-
DEFAULT_UPLINK_ADDRESS_LOW_BYTE, (byte)i);
237+
addresses[i] = new LoraAddress(DEFAULT_DOWNLINK_ADDRESS[0], DEFAULT_DOWNLINK_ADDRESS[0], (byte)i);
235238
}
236239

237240
return addresses;
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#ifndef MUD_CONFIGURATION_H
22
#define MUD_CONFIGURATION_H
33

4-
// #define ENABLE_DEBUG 1
4+
#define ENABLE_DEBUG 1
55

66
// For my two Arduino Micro boards.
7-
/*#define ARDUINO_MICRO 1
7+
#define ARDUINO_MICRO 1
88

9-
// STR-01
9+
// HLT
10+
#define LORA_CHIP_MD0_PIN 2
11+
#define LORA_CHIP_MD1_PIN 3
12+
#define LORA_CHIP_AUX_PIN 4
13+
14+
/*// STR-01
1015
#define LORA_CHIP_MD0_PIN 2
1116
#define LORA_CHIP_MD1_PIN 3
1217
#define LORA_CHIP_AUX_PIN 4
@@ -16,7 +21,7 @@
1621
#define LORA_CHIP_MD0_PIN 11
1722
#define LORA_CHIP_MD1_PIN 12*/
1823

19-
// For my Arduino UNO R3 board.
24+
/*// For my Arduino UNO R3 board.
2025
#define ARDUINO_UNO 1
2126
// #define USE_SOFTWARE_SERIAL 1
2227
#define LORA_CHIP_MD0_PIN 3
@@ -27,6 +32,6 @@
2732
// For my Arduino UNO R3 board.
2833
#define SOFTWARE_SERIAL_RX_PIN 11
2934
#define SOFTWARE_SERIAL_TX_PIN 12
30-
#endif
35+
#endif*/
3136

3237
#endif

emulators/lora/lora-gateway/src/main/java/com/thefirstlineofcode/sand/emulators/lora/gateway/Gateway.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ public SimpleLight create() {
250250

251251
protected LoraCommunicator createGatewayCommunicator() {
252252
ILoraChip chip = null;
253-
chip = network.createChip(new LoraAddress(new byte[] {ILoraGateway.DEFAULT_UPLINK_ADDRESS_HIGH_BYTE,
254-
ILoraGateway.DEFAULT_UPLINK_ADDRESS_LOW_BYTE,
253+
chip = network.createChip(new LoraAddress(new byte[] {
254+
ILoraGateway.DEFAULT_UPLINK_ADDRESS[0],
255+
ILoraGateway.DEFAULT_UPLINK_ADDRESS[1],
255256
ILoraGateway.DEFAULT_THING_COMMUNICATION_CHANNEL}));
256257

257258
return new LoraCommunicator(chip);

emulators/lora/simple-light/src/main/java/com/thefirstlineofcode/sand/emulators/lora/simple/light/SimpleLight.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import com.thefirstlineofcode.sand.protocols.things.simple.light.Flash;
1818
import com.thefirstlineofcode.sand.protocols.things.simple.light.SwitchState;
1919
import com.thefirstlineofcode.sand.protocols.things.simple.light.SwitchStateChanged;
20+
import com.thefirstlineofcode.sand.protocols.things.simple.light.TurnOff;
21+
import com.thefirstlineofcode.sand.protocols.things.simple.light.TurnOn;
2022

2123
public class SimpleLight extends AbstractLoraThingEmulator implements ISimpleLightEmulator {
2224
private static final SwitchState DEFAULT_SWITCH_STATE = SwitchState.OFF;
@@ -124,14 +126,30 @@ protected void doPowerOff() {
124126
@Override
125127
protected void processAction(Object action) throws ExecutionException {
126128
if (action instanceof Flash) {
127-
if (switchState != SwitchState.CONTROL)
128-
throw new ExecutionException(ISimpleLight.ERROR_CODE_NOT_REMOTE_CONTROL_STATE);
129-
130-
flash(((Flash)action).getRepeat());
129+
processSimpleLightAction(action);
130+
} else if (action instanceof TurnOn) {
131+
processSimpleLightAction(action);
132+
} else if (action instanceof TurnOff) {
133+
processSimpleLightAction(action);
131134
} else {
132135
super.processAction(action);
133136
}
134137
}
138+
139+
private void processSimpleLightAction(Object action) throws ExecutionException {
140+
if (switchState != SwitchState.CONTROL)
141+
throw new ExecutionException(ISimpleLight.ERROR_CODE_NOT_REMOTE_CONTROL_STATE);
142+
143+
if (action instanceof Flash) {
144+
flash(((Flash)action).getRepeat());
145+
} else if (action instanceof TurnOn) {
146+
turnOn();
147+
} else if (action instanceof TurnOff) {
148+
turnOff();
149+
} else {
150+
throw new RuntimeException(String.format("Not a simple light action. Action type: %s.", action.getClass().getName()));
151+
}
152+
}
135153

136154
public void flash(int repeat) throws ExecutionException {
137155
if (!isPowered() || batteryPower == 0)

protocols/bxmpp-extensions/src/main/resources/META-INF/lora-dac-bxmpp-extension.properties

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
0x03=allocation
66
0x04=uplink-channel-begin
77
0x05=uplink-channel-end
8-
0x06=uplink-address-high-byte
9-
0x07=uplink-address-low-byte
10-
0x08=allocated-address
11-
0x09=allocated
12-
0x0a=configured
13-
0x0b=is-configured
14-
0x0c=not-configured
8+
0x06=uplink-address
9+
0x07=allocated-address
10+
0x08=allocated
11+
0x09=configured
12+
0x0a=is-configured
13+
0x0b=not-configured

0 commit comments

Comments
 (0)