Skip to content

Commit

Permalink
Updated packet format
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrinka committed Jul 20, 2023
1 parent ac89e38 commit 20a947a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
33 changes: 26 additions & 7 deletions app/src/main/java/app/uvtracker/data/type/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public class Record implements IFlattenable {

private static final String TAG = Record.class.getSimpleName();

private static float round2(float input) {
if(Float.isNaN(input)) return 0.0f;
return Math.round(input * 100.0f) / 100.0f;
}

@Nullable
public static Record unflatten(@NonNull String input) {
try {
Expand All @@ -32,9 +27,17 @@ public static Record unflatten(@NonNull String input) {
}
}

@NonNull
public static Record decompress(byte uv, byte vis) {
return new Record(
(float)decompress8(uv) / 10.9375f,
(float)decompress44(vis) * 2.4f
);
}

private final float uvIndex;
private final float illuminance;

public final float uvIndex;
public final float illuminance;

@Nullable
private String flattenedString;
Expand Down Expand Up @@ -71,4 +74,20 @@ public String toString() {
return this.flatten();
}

private static float round2(float input) {
if(Float.isNaN(input)) return 0.0f;
return Math.round(input * 100.0f) / 100.0f;
}

private static int decompress44(byte input0) {
int input = Byte.toUnsignedInt(input0);
int dig = (input & 0xF0) >> 4;
int exp = (input & 0x0F);
return dig << exp;
}

private static int decompress8(byte input0) {
return Byte.toUnsignedInt(input0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,69 @@

import androidx.annotation.NonNull;

import app.uvtracker.data.type.Record;
import app.uvtracker.sensor.protocol.codec.exception.PacketFormatException;
import app.uvtracker.sensor.protocol.packet.base.Packet;
import app.uvtracker.sensor.protocol.packet.base.PacketIn;
import app.uvtracker.sensor.protocol.util.Packing;

public class PacketInNewSample extends PacketIn {

private final int intensityUV;
private final int remoteTimestamp;

private final int intensityVIS;
@NonNull
private final Record record;

public PacketInNewSample(Packet packetBase) throws PacketFormatException {
super(packetBase);
PacketFormatException.requireLength(packetBase, 6);
this.intensityVIS = Packing.unpack3(this.payload, 0);
this.intensityUV = Packing.unpack3(this.payload, 3);
PacketFormatException.requireLength(packetBase, 7);
this.remoteTimestamp = Packing.unpack4(this.payload, 0) * 60 + Packing.unpack1(this.payload, 4);
this.record = Record.decompress(this.payload[6], this.payload[5]);
}

public int getIntensityUV() {
return intensityUV;
public int getRemoteTimestamp() {
return this.remoteTimestamp;
}

public int getIntensityVIS() {
return intensityVIS;
@NonNull
public Record getRecord() {
return this.record;
}

@Override
@NonNull
public String toString() {
return this.type + String.format("{VIS=%1$d,UV=%2$d}", this.intensityVIS, this.intensityUV);
int day = 0;
int hour = 0;
int minute = 0;
int second = this.remoteTimestamp;
if(second >= 60) {
minute = second / 60;
second = second % 60;
}
if(minute >= 60) {
hour = minute / 60;
minute = minute % 60;
}
if(hour >= 24) {
day = hour / 24;
hour = hour % 24;
}
StringBuilder sb = new StringBuilder();
boolean flag = false;
if(day > 0) {
flag = true;
sb.append(day).append("d");
}
if(flag || hour > 0) {
flag = true;
sb.append(hour).append("h");
}
if(flag || minute > 0) {
sb.append(minute).append("m");
}
sb.append(second).append("s");
return this.type + String.format("{%1$s,%2$.1flux,%3$.1fuvi}", sb, this.record.illuminance, this.record.uvIndex);
}

}

0 comments on commit 20a947a

Please sign in to comment.