mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Return struct instead of object path directly for dbus list devices
This commit is contained in:
parent
7829a8d631
commit
c56a8df9b2
3 changed files with 77 additions and 40 deletions
|
@ -145,13 +145,14 @@ public class DbusManagerImpl implements Manager {
|
|||
@Override
|
||||
public List<Device> getLinkedDevices() throws IOException {
|
||||
final var thisDevice = signal.getThisDevice();
|
||||
return signal.listDevices().stream().map(devicePath -> {
|
||||
final var device = getRemoteObject(devicePath, Signal.Device.class).GetAll("org.asamk.Signal.Device");
|
||||
return signal.listDevices().stream().map(d -> {
|
||||
final var device = getRemoteObject(d.getObjectPath(),
|
||||
Signal.Device.class).GetAll("org.asamk.Signal.Device");
|
||||
return new Device((long) device.get("Id").getValue(),
|
||||
(String) device.get("Name").getValue(),
|
||||
(long) device.get("Created").getValue(),
|
||||
(long) device.get("LastSeen").getValue(),
|
||||
thisDevice.equals(devicePath));
|
||||
thisDevice.equals(d.getObjectPath()));
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ public class DbusSignalImpl implements Signal {
|
|||
private final String objectPath;
|
||||
|
||||
private DBusPath thisDevice;
|
||||
private final List<DBusPath> devices = new ArrayList<>();
|
||||
private final List<StructDevice> devices = new ArrayList<>();
|
||||
|
||||
public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
|
||||
this.m = m;
|
||||
|
@ -115,41 +115,11 @@ public class DbusSignalImpl implements Signal {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<DBusPath> listDevices() {
|
||||
public List<StructDevice> listDevices() {
|
||||
updateDevices();
|
||||
return this.devices;
|
||||
}
|
||||
|
||||
private void updateDevices() {
|
||||
List<org.asamk.signal.manager.api.Device> linkedDevices;
|
||||
try {
|
||||
linkedDevices = m.getLinkedDevices();
|
||||
} catch (IOException | Error.Failure e) {
|
||||
throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
|
||||
}
|
||||
|
||||
unExportDevices();
|
||||
|
||||
linkedDevices.forEach(d -> {
|
||||
final var object = new DbusSignalDeviceImpl(d);
|
||||
final var deviceObjectPath = object.getObjectPath();
|
||||
try {
|
||||
connection.exportObject(object);
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (d.isThisDevice()) {
|
||||
thisDevice = new DBusPath(deviceObjectPath);
|
||||
}
|
||||
this.devices.add(new DBusPath(deviceObjectPath));
|
||||
});
|
||||
}
|
||||
|
||||
private void unExportDevices() {
|
||||
this.devices.stream().map(DBusPath::getPath).forEach(connection::unExportObject);
|
||||
this.devices.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DBusPath getThisDevice() {
|
||||
updateDevices();
|
||||
|
@ -802,21 +772,55 @@ public class DbusSignalImpl implements Signal {
|
|||
return name.isEmpty() ? null : name;
|
||||
}
|
||||
|
||||
private String emptyIfNull(final String string) {
|
||||
return string == null ? "" : string;
|
||||
}
|
||||
|
||||
private static String getDeviceObjectPath(String basePath, long deviceId) {
|
||||
return basePath + "/Devices/" + deviceId;
|
||||
}
|
||||
|
||||
private void updateDevices() {
|
||||
List<org.asamk.signal.manager.api.Device> linkedDevices;
|
||||
try {
|
||||
linkedDevices = m.getLinkedDevices();
|
||||
} catch (IOException e) {
|
||||
throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
|
||||
}
|
||||
|
||||
unExportDevices();
|
||||
|
||||
linkedDevices.forEach(d -> {
|
||||
final var object = new DbusSignalDeviceImpl(d);
|
||||
final var deviceObjectPath = object.getObjectPath();
|
||||
try {
|
||||
connection.exportObject(object);
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (d.isThisDevice()) {
|
||||
thisDevice = new DBusPath(deviceObjectPath);
|
||||
}
|
||||
this.devices.add(new StructDevice(new DBusPath(deviceObjectPath), d.getId(), emptyIfNull(d.getName())));
|
||||
});
|
||||
}
|
||||
|
||||
private void unExportDevices() {
|
||||
this.devices.stream()
|
||||
.map(StructDevice::getObjectPath)
|
||||
.map(DBusPath::getPath)
|
||||
.forEach(connection::unExportObject);
|
||||
this.devices.clear();
|
||||
}
|
||||
|
||||
public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device {
|
||||
|
||||
private final org.asamk.signal.manager.api.Device device;
|
||||
|
||||
public DbusSignalDeviceImpl(final org.asamk.signal.manager.api.Device device) {
|
||||
super();
|
||||
super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
|
||||
List.of(new DbusProperty<>("Id", device::getId),
|
||||
new DbusProperty<>("Name",
|
||||
() -> device.getName() == null ? "" : device.getName(),
|
||||
this::setDeviceName),
|
||||
new DbusProperty<>("Name", () -> emptyIfNull(device.getName()), this::setDeviceName),
|
||||
new DbusProperty<>("Created", device::getCreated),
|
||||
new DbusProperty<>("LastSeen", device::getLastSeen))));
|
||||
this.device = device;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue