Skip to content

Commit

Permalink
add support for ble protocol
Browse files Browse the repository at this point in the history
+ upgrade to version 2
  • Loading branch information
dernasherbrezon committed Dec 28, 2023
1 parent 894bea1 commit f6b6316
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 64 deletions.
14 changes: 6 additions & 8 deletions src/main/java/ru/r2cloud/device/LoraAtBleDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.LoggerFactory;

import ru.r2cloud.lora.LoraFrame;
import ru.r2cloud.lora.loraat.gatt.LoraAtDeviceStatus;
import ru.r2cloud.model.DeviceConfiguration;
import ru.r2cloud.model.DeviceConnectionStatus;
import ru.r2cloud.model.DeviceStatus;
Expand Down Expand Up @@ -59,14 +60,11 @@ public DeviceStatus getStatus() {
return result;
}

public void setStatus(int batteryLevel, int signalLevel) {
LOG.info("[{}] battery level: {} signal: {}", id, batteryLevel, signalLevel);
if (batteryLevel == 255) {
this.batteryLevel = null;
} else {
this.batteryLevel = batteryLevel;
}
this.signalLevel = signalLevel;
//TODO update rrd graphs
public void updateStatus(LoraAtDeviceStatus status) {
LOG.info("[{}] signal: {}", id, status.getBluetoothRssi());
this.batteryLevel = null;
this.signalLevel = status.getBluetoothRssi();
}

public void addFrame(LoraFrame frame) {
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/ru/r2cloud/lora/loraat/gatt/DeviceStatus.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/ru/r2cloud/lora/loraat/gatt/GattServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void start() {
ScheduleCharacteristic schedule = new ScheduleCharacteristic(LORA_SCHEDULE_PATH, new String[] { "read", "write" }, SCHEDULE_CHARACTERISTIC_UUID, LORA_SERVICE_PATH, scheduleDesc, manager, clock);

BleDescriptor statusDesc = new BleDescriptor(LORA_STATUS_PATH + "/desc0", new String[] { "read" }, "5604f205-0c14-4926-9d7d-21dbab315f2f", LORA_STATUS_PATH, "Status of all connected LoRa modules");
DeviceStatus status = new DeviceStatus(LORA_STATUS_PATH, new String[] { "write" }, STATUS_CHARACTERISTIC_UUID, LORA_SERVICE_PATH, statusDesc, manager);
StatusCharacteristic status = new StatusCharacteristic(LORA_STATUS_PATH, new String[] { "write" }, STATUS_CHARACTERISTIC_UUID, LORA_SERVICE_PATH, statusDesc, manager);
List<BleCharacteristic> characteristics = new ArrayList<>();
characteristics.add(schedule);
characteristics.add(status);
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/ru/r2cloud/lora/loraat/gatt/LoraAtDeviceStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ru.r2cloud.lora.loraat.gatt;

public class LoraAtDeviceStatus {

private int bluetoothRssi;
private int sx127xRawTemperature;
private int solarVoltage;
private int solarCurrent;
private int batteryVoltage;
private int batteryCurrent;

public int getBluetoothRssi() {
return bluetoothRssi;
}

public void setBluetoothRssi(int bluetoothRssi) {
this.bluetoothRssi = bluetoothRssi;
}

public int getSx127xRawTemperature() {
return sx127xRawTemperature;
}

public void setSx127xRawTemperature(int sx127xRawTemperature) {
this.sx127xRawTemperature = sx127xRawTemperature;
}

public int getSolarVoltage() {
return solarVoltage;
}

public void setSolarVoltage(int solarVoltage) {
this.solarVoltage = solarVoltage;
}

public int getSolarCurrent() {
return solarCurrent;
}

public void setSolarCurrent(int solarCurrent) {
this.solarCurrent = solarCurrent;
}

public int getBatteryVoltage() {
return batteryVoltage;
}

public void setBatteryVoltage(int batteryVoltage) {
this.batteryVoltage = batteryVoltage;
}

public int getBatteryCurrent() {
return batteryCurrent;
}

public void setBatteryCurrent(int batteryCurrent) {
this.batteryCurrent = batteryCurrent;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public class ScheduleCharacteristic extends BleCharacteristic {

private static final Logger LOG = LoggerFactory.getLogger(ScheduleCharacteristic.class);
private static final int PROTOCOL_VERSION = 2;
private final DeviceManager manager;
private final Clock clock;

Expand Down Expand Up @@ -66,6 +67,7 @@ public byte[] read(String bluetoothAddress) {
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DataOutputStream dos = new DataOutputStream(baos)) {
dos.writeByte(PROTOCOL_VERSION);
dos.writeLong(req.getStartTimeMillis());
dos.writeLong(req.getEndTimeMillis());
dos.writeLong(currentTime);
Expand All @@ -91,7 +93,7 @@ public byte[] read(String bluetoothAddress) {
// lora packet size cannot be more than 255 bytes
dos.writeByte(transmitter.getBeaconSizeBytes());
dos.writeShort(240); // over current protection. not used for RX
dos.writeByte(0); // pin for TX. not used in RX
dos.writeByte(0); // pin for TX. not used in RX
} catch (IOException e) {
LOG.error("[{}] can't serialize output", bluetoothAddress, e);
return new byte[0];
Expand All @@ -108,6 +110,11 @@ public void write(byte[] value, String bluetoothAddress) {
ByteArrayInputStream bais = new ByteArrayInputStream(value);
try (DataInputStream dis = new DataInputStream(bais)) {
LoraFrame frame = new LoraFrame();
int protocolVersion = dis.readUnsignedByte();
if (protocolVersion != PROTOCOL_VERSION) {
LOG.error("[{}] invalid protocol version {}, expected {}", bluetoothAddress, protocolVersion, PROTOCOL_VERSION);
return;
}
frame.setFrequencyError(dis.readInt());
frame.setRssi(dis.readShort());
frame.setSnr(dis.readFloat());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package ru.r2cloud.lora.loraat.gatt;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.r2cloud.device.Device;
import ru.r2cloud.device.DeviceManager;
import ru.r2cloud.device.LoraAtBleDevice;
import ru.r2cloud.util.Configuration;
import ru.r2cloud.util.Util;

public class StatusCharacteristic extends BleCharacteristic {

private static final Logger LOG = LoggerFactory.getLogger(StatusCharacteristic.class);
private static final int PROTOCOL_VERSION = 2;
private final DeviceManager manager;

public StatusCharacteristic(String objectPath, String[] flags, String uuId, String servicePath, BleDescriptor descriptor, DeviceManager manager) {
super(objectPath, flags, uuId, servicePath, descriptor);
this.manager = manager;
}

@Override
public byte[] read(String bluetoothAddress) {
// unsupported
return new byte[0];
}

@Override
public void write(byte[] value, String bluetoothAddress) {
if (value.length < 2) {
LOG.info("[{}] not enough bytes. expected 2, got: {}", bluetoothAddress, value.length);
return;
}
LoraAtBleDevice device = getLoraDevice(bluetoothAddress);
if (device == null) {
return;
}
ByteArrayInputStream bais = new ByteArrayInputStream(value);
try (DataInputStream dis = new DataInputStream(bais)) {
int protocolVersion = dis.readUnsignedByte();
if (protocolVersion != PROTOCOL_VERSION) {
LOG.error("[{}] invalid protocol version {}, expected {}", bluetoothAddress, protocolVersion, PROTOCOL_VERSION);
return;
}
LoraAtDeviceStatus status = new LoraAtDeviceStatus();
status.setBluetoothRssi(dis.readByte());
status.setSx127xRawTemperature(dis.readByte());
status.setSolarVoltage(dis.readUnsignedShort());
status.setSolarCurrent(dis.readShort());
status.setBatteryVoltage(dis.readUnsignedShort());
status.setBatteryCurrent(dis.readShort());
device.updateStatus(status);
} catch (IOException e) {
Util.logIOException(LOG, false, "[" + bluetoothAddress + "] can't read input", e);
return;
}

}

private LoraAtBleDevice getLoraDevice(String bluetoothAddress) {
Device device = manager.findDeviceById(Configuration.LORA_AT_DEVICE_PREFIX + bluetoothAddress);
if (device == null) {
LOG.info("[{}] ble device is not configured", bluetoothAddress);
return null;
}
if (!(device instanceof LoraAtBleDevice)) {
LOG.info("not a lora-at device: {}", bluetoothAddress);
return null;
}
return (LoraAtBleDevice) device;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void complete() {
}

private String toLoraAtFriendlyString() {
return String.format("%f,%f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", transmitter.getFrequency() / 1000000.0f, transmitter.getLoraBandwidth() / 1000.0f, transmitter.getLoraSpreadFactor(), transmitter.getLoraCodingRate(), transmitter.getLoraSyncword(), 10, transmitter.getLoraPreambleLength(),
return String.format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", transmitter.getFrequency(), transmitter.getLoraBandwidth(), transmitter.getLoraSpreadFactor(), transmitter.getLoraCodingRate(), transmitter.getLoraSyncword(), transmitter.getLoraPreambleLength(),
(int) deviceConfiguration.getGain(), transmitter.getLoraLdro(), transmitter.isLoraCrc() ? 1 : 0, transmitter.isLoraExplicitHeader() ? 1 : 0, transmitter.getBeaconSizeBytes());
}

Expand Down

0 comments on commit f6b6316

Please sign in to comment.