-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
154 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
src/main/java/ru/r2cloud/lora/loraat/gatt/DeviceStatus.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/ru/r2cloud/lora/loraat/gatt/LoraAtDeviceStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/ru/r2cloud/lora/loraat/gatt/StatusCharacteristic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters