Skip to content

Commit

Permalink
Migration from retrofit2 to feign
Browse files Browse the repository at this point in the history
  • Loading branch information
cpoder committed Mar 12, 2024
1 parent b402c27 commit 24e2c54
Show file tree
Hide file tree
Showing 37 changed files with 2,385 additions and 2,126 deletions.
1,050 changes: 1,050 additions & 0 deletions eclipse-formatter.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<Release> getReleases() {
result.forEach(r -> r.getAssets().removeIf(a -> !a.getName().startsWith("lora")));
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Couldn't retrieve github releases", e);
}
return result;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public okhttp3.MediaType contentType() {
@Override
public void writeTo(BufferedSink sink) throws IOException {
URL url = new URL(asset.getBrowserDownloadUrl());
try(InputStream is = url.openStream()) {
try (InputStream is = url.openStream()) {
is.transferTo(sink.outputStream());
}
}
Expand All @@ -122,20 +122,23 @@ public void writeTo(BufferedSink sink) throws IOException {
.uploadApplicationAttachment(application.getId(), file).execute();
if (response.isSuccessful()) {
logger.info("New microservice upload was successful, now subscribing to it...");
response = tenantService.subscribeToApplication(subscriptionsService.getTenant(), new Subscription().application(new Application().self(application.getSelf()))).execute();
response = tenantService
.subscribeToApplication(subscriptionsService.getTenant(),
new Subscription().application(new Application().self(application.getSelf())))
.execute();
if (response.isSuccessful()) {
logger.info("Subscription was successful");
result = "{result: 'OK'}";
} else {
logger.error("An error occurred while subscribing to the microservice: {} {} {}", response.code(),
response.message(), response.errorBody().string());
response.message(), response.errorBody().string());
}
} else {
logger.error("An error occurred while uploading the microservice: {} {} {}", response.code(),
response.message(), response.errorBody().string());
}
} catch (IOException e1) {
e1.printStackTrace();
logger.error("Couldn't upload application file", e1);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,22 @@ private void extractDataV2(ByteBuffer buffer, ManagedObjectRepresentation mor, C
case TOF_CONFIGURED_IN_DISTANCE_MODE:
c8yData.addMeasurement(mor, "c8y_DistanceMeasurement", "distance", "mm", BigDecimal.valueOf(x), time);
break;
case BUTTON_PRESSED_OR_REED_CLOSED:
break;
case BUTTON_RELEASED_OR_REED_OPENED:
break;
case PIR_IN_MOVE_PROFILE_MOTION_ALGORITHM_ACTIVATED:
break;
case PIR_IN_MOVE_PROFILE_MOTION_ALGORITHM_ACTIVATED_AND_INACTIVE_STATE_MANAGED:
break;
case PIR_IN_MOVE_PROFILE_MOTION_ALGORITHM_DEACTIVATED:
break;
case REED_CLOSED:
break;
case REED_OPENED:
break;
default:
break;
}
int activeStatePercentage = buffer.get();
c8yData.addMeasurement(mor, "Active state percentage", "level", "%", BigDecimal.valueOf(activeStatePercentage),
Expand Down Expand Up @@ -378,7 +394,7 @@ protected C8YData decode(ManagedObjectRepresentation mor, Decode decode) {
try {
mor.set(new Configuration(new ObjectMapper().writeValueAsString(map)));
} catch (Exception e) {
e.printStackTrace();
logger.error("Error while seriliazing device configuration", e);
}
if (ra.getResponseInterval() > 0) {
mor.set(ra);
Expand Down Expand Up @@ -467,7 +483,7 @@ protected DownlinkData encode(ManagedObjectRepresentation mor, Encode encode) {
data.setPayload("0301" + param.buildPayload());
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Coudln't read operation content: {}", encode.getOperation(), e);
}
data.setFport(1);
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
public class ElsysCodec extends DeviceCodec {

Map<String, DeviceOperation> ops = new HashMap<>();
{

public ElsysCodec() {
models.put("default", "default");
models.put("desk", "ERS Desk");
models.put("eye", "ERS Eye");
Expand Down Expand Up @@ -537,7 +538,7 @@ public void process(ManagedObjectRepresentation mor, String model, ByteBuffer bu
DateTime time,
int offsetSize) {
Map<String, Object> map = new HashMap<>();
int size = buffer.get();
buffer.get(); // size, we should check the remaining bytes match
while (buffer.hasRemaining()) {
parseCurrentSetting(buffer, map);
}
Expand All @@ -548,7 +549,7 @@ public void process(ManagedObjectRepresentation mor, String model, ByteBuffer bu
mor.set(config);
c8yData.updateRootDevice(mor);
} catch (Exception e) {
e.printStackTrace();
log.error("Couldn't write deserialize device config {}", map, e);
}
c8yData.addEvent(mor, "settings", "Settings", map, getTime(buffer, time, offsetSize));
}
Expand Down Expand Up @@ -595,8 +596,7 @@ protected C8YData decode(ManagedObjectRepresentation mor, Decode decode) {
eventType.process(mor, decode.getModel(), buffer, c8yData,
new DateTime(decode.getUpdateTime()), offsetSize);
} catch (Exception e) {
e.printStackTrace();
log.error("Couldn't handle event {}", eventType);
log.error("Couldn't handle event {}", eventType, e);
c8yData.addAlarm(mor, "Event decoding error", "Error while decoding event: " + eventType,
CumulocitySeverities.WARNING,
new DateTime(decode.getUpdateTime()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ private CHGSTATE(byte value) {
enum TYPE {
REGISTER((byte) 0x10) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
String adr = (type & 8) != 0 ? "ON" : "OFF";
MODE mode = MODE.BY_MODE.get((byte) (type & 0x7));
byte smode = buffer.get();
Expand All @@ -171,20 +172,22 @@ public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffe
},
HEARTBEAT((byte) 0x20) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
long vol = buffer.get();
long rssi = -buffer.get();
long snr = 0;
if ((type & 0x07) > 0) {
snr = buffer.getShort();
}
byte gpsstat = buffer.get();
GPSSTATE gpsState = GPSSTATE.BY_VALUE.get((byte) ((gpsstat&0xff) >> 4));
GPSSTATE gpsState = GPSSTATE.BY_VALUE.get((byte) ((gpsstat & 0xff) >> 4));
int vibState = gpsstat & 0xf;
byte chgstat = buffer.get();
CHGSTATE chgState = CHGSTATE.BY_VALUE.get((byte) ((chgstat&0xff) >> 4));
CHGSTATE chgState = CHGSTATE.BY_VALUE.get((byte) ((chgstat & 0xff) >> 4));
c8yData.addEvent(mor, "Tracker status", String.format("GPSSTATE: %s\nVIBSTATE: %d\nCHGSTATE: %s",
gpsState != null ? gpsState.name() : "UNKNOWN(" + gpsstat + ")", vibState, chgState != null ? chgState.name() : "UNKNOWN(" + chgstat + ")"), null, new DateTime());
gpsState != null ? gpsState.name() : "UNKNOWN(" + gpsstat + ")", vibState,
chgState != null ? chgState.name() : "UNKNOWN(" + chgstat + ")"), null, new DateTime());
c8yData.addMeasurement(mor, "c8y_Battery", "level", "%", BigDecimal.valueOf(vol), new DateTime());
c8yData.addMeasurement(mor, "Tracker Signal Strength", "rssi", "dBm", BigDecimal.valueOf(rssi),
updateTime);
Expand All @@ -194,7 +197,8 @@ public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffe
},
PERIODICAL_POSITION((byte) 0x30) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
float lng = buffer.getFloat();
float lat = buffer.getFloat();
long time = buffer.getInt() * 1000L;
Expand All @@ -214,7 +218,8 @@ public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffe
},
ON_DEMAND_POSITION((byte) 0x40) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
buffer.get();
float lng = buffer.getFloat();
float lat = buffer.getFloat();
Expand All @@ -235,65 +240,80 @@ public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffe
},
HISTORY_POSITION((byte) 0x50) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
// TODO Auto-generated method stub

}
},
ALARM((byte) 0x60) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
// TODO Auto-generated method stub

}
},
BLE_COORDINATE((byte) 0x70) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
Beacon beacon = null;
byte move = buffer.get();
buffer.getInt();
//boolean beaconChanged = false;
// boolean beaconChanged = false;
List<Beacon> beacons = new ArrayList<>();
while (buffer.hasRemaining()) {
short major = buffer.getShort();
short minor = buffer.getShort();
byte rssi = buffer.get();
c8yData.addEvent(mor, "BLE coordinate", String.format("MOVE: %d\nMAJOR: %04X\nMINOR: %04X\nRSSI: %d", move, major, minor, rssi), null, updateTime);
c8yData.addEvent(mor, "BLE coordinate",
String.format("MOVE: %d\nMAJOR: %04X\nMINOR: %04X\nRSSI: %d", move, major, minor, rssi),
null, updateTime);
beacon = new Beacon(String.format("%04X", major), String.format("%04X", minor), rssi);
beacons.add(beacon);
c8yData.addMeasurement(mor, "Max rssi", "rssi", "dBm", BigDecimal.valueOf(beacon.getRssi()), updateTime);
c8yData.addMeasurement(mor, String.format("%04X", major) + "-" + String.format("%04X", minor), "rssi", "dBm", BigDecimal.valueOf(rssi), updateTime);
c8yData.addMeasurement(mor, "Max rssi", "rssi", "dBm", BigDecimal.valueOf(beacon.getRssi()),
updateTime);
c8yData.addMeasurement(mor, String.format("%04X", major) + "-" + String.format("%04X", minor),
"rssi", "dBm", BigDecimal.valueOf(rssi), updateTime);
}

mor.set(algo.getPosition(mor, beacons));
c8yData.updateRootDevice(mor);
/*if (beacon != null) {
if (beacon.getMajor().equals(newBeacon.getMajor()) && beacon.getMinor().equals(newBeacon.getMinor()) || newBeacon.getRssi() > beacon.getRssi()) {
mor.set(newBeacon);
c8yData.setMorToUpdate(mor);
beaconChanged = true;
beacon = newBeacon;
}
} else {
mor.set(newBeacon);
c8yData.setMorToUpdate(mor);
beaconChanged = true;
beacon = newBeacon;
}
if (beaconChanged) {
c8yData.addEvent(mor, "Nearest beacon changed", String.format("MAJOR: %s\nMINOR: %s\nRSSI: %d", beacon.getMajor(), beacon.getMinor(), beacon.getRssi()), null, updateTime);
}*/
/*
* if (beacon != null) {
* if (beacon.getMajor().equals(newBeacon.getMajor()) &&
* beacon.getMinor().equals(newBeacon.getMinor()) || newBeacon.getRssi() >
* beacon.getRssi()) {
* mor.set(newBeacon);
* c8yData.setMorToUpdate(mor);
* beaconChanged = true;
* beacon = newBeacon;
* }
* } else {
* mor.set(newBeacon);
* c8yData.setMorToUpdate(mor);
* beaconChanged = true;
* beacon = newBeacon;
* }
* if (beaconChanged) {
* c8yData.addEvent(mor, "Nearest beacon changed",
* String.format("MAJOR: %s\nMINOR: %s\nRSSI: %d", beacon.getMajor(),
* beacon.getMinor(), beacon.getRssi()), null, updateTime);
* }
*/

}
},
ACKNOWLEDGE((byte) 0xF0) {
@Override
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo) {
public void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo) {
mor.set(new Configuration("Configuration requested..."));
c8yData.updateRootDevice(mor);
}
};

/**
*
*/
Expand All @@ -316,7 +336,8 @@ private TYPE(byte type) {
}
}

public abstract void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData, DateTime updateTime, Algo algo);
public abstract void process(ManagedObjectRepresentation mor, byte type, ByteBuffer buffer, C8YData c8yData,
DateTime updateTime, Algo algo);

}

Expand Down Expand Up @@ -348,9 +369,8 @@ protected C8YData decode(ManagedObjectRepresentation mor, Decode decode) {
logger.info("Frame type: {}", t.name());
try {
t.process(mor, type, buffer, c8yData, new DateTime(decode.getUpdateTime()), currentAlgo);
} catch(Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
logger.error("Couldn't process uplink: {}", decode.getPayload(), e.getMessage());
}
}

Expand Down Expand Up @@ -380,13 +400,14 @@ protected DownlinkData encode(ManagedObjectRepresentation mor, Encode encode) {
byte selfadapt = params.get("selfadapt").asBoolean() ? (byte) 4 : 0;
byte oneoff = params.get("oneoff").asBoolean() ? (byte) 2 : 0;
byte alreport = params.get("alreport").asBoolean() ? (byte) 1 : 0;
buffer.put((byte) ((byte) 0x90 | (byte)breakpoint | (byte)selfadapt | (byte)oneoff | (byte)alreport));
buffer.put(
(byte) ((byte) 0x90 | (byte) breakpoint | (byte) selfadapt | (byte) oneoff | (byte) alreport));
buffer.putShort((short) params.get("pos").asInt());
buffer.put((byte) params.get("hb").asInt());
payload = BaseEncoding.base16().encode(buffer.array());
}
} catch (IOException e) {
e.printStackTrace();
logger.error("Couldn't process operation {}", encode.getOperation(), e);
}
return new DownlinkData(null, 1, payload);
}
Expand Down Expand Up @@ -422,8 +443,8 @@ protected Map<String, String> getChildDevicesNames() {
return null;
}

//UGLY!!!!!! not scalable!!!!!
//Need to store that in a managed object!!!
// UGLY!!!!!! not scalable!!!!!
// Need to store that in a managed object!!!
private Algo currentAlgo;

@Autowired
Expand Down
Loading

0 comments on commit 24e2c54

Please sign in to comment.