-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 3dbf47d Author: Lyrinka Hyacinthoides <[email protected]> Date: Fri Jul 21 03:18:02 2023 +0800 Implemented new sample event commit 20a947a Author: Lyrinka Hyacinthoides <[email protected]> Date: Fri Jul 21 03:04:31 2023 +0800 Updated packet format commit ac89e38 Author: Lyrinka Hyacinthoides <[email protected]> Date: Thu Jul 20 07:31:07 2023 +0800 Preliminary DB interface
- Loading branch information
Showing
9 changed files
with
236 additions
and
45 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
app/src/main/java/app/uvtracker/data/IPersistentDataAccess.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,23 @@ | ||
package app.uvtracker.data; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
import app.uvtracker.data.type.Record; | ||
|
||
public interface IPersistentDataAccess { | ||
|
||
void clearDB(); | ||
|
||
void writeHourlyAverage(@NonNull Date time, @NonNull Record record); | ||
|
||
@Nullable | ||
Record readHourlyAverage(@NonNull Date time); | ||
|
||
@NonNull | ||
Map<Date, Record> readAllHourlyAverage(); | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/app/uvtracker/data/type/IFlattenable.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,10 @@ | ||
package app.uvtracker.data.type; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public interface IFlattenable { | ||
|
||
@NonNull | ||
String flatten(); | ||
|
||
} |
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,94 @@ | ||
package app.uvtracker.data.type; | ||
|
||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class Record implements IFlattenable { | ||
|
||
private static final String TAG = Record.class.getSimpleName(); | ||
|
||
@Nullable | ||
public static Record unflatten(@NonNull String input) { | ||
try { | ||
JSONObject obj = new JSONObject(input); | ||
return new Record( | ||
(float)obj.getDouble("uv"), | ||
(float)obj.getDouble("vis") | ||
); | ||
} catch (JSONException e) { | ||
Log.w(TAG, "Could not parse record " + input); | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
@NonNull | ||
public static Record decompress(byte uv, byte vis) { | ||
// TODO: maybe extract this conversion out to somewhere? | ||
return new Record( | ||
(float)decompress8(uv) / 10.9375f, | ||
(float)decompress44(vis) * 2.4f | ||
); | ||
} | ||
|
||
|
||
public final float uvIndex; | ||
public final float illuminance; | ||
|
||
@Nullable | ||
private String flattenedString; | ||
|
||
public Record(float uvIndex, float illuminance) { | ||
this.uvIndex = round2(uvIndex); | ||
this.illuminance = round2(illuminance); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String flatten() { | ||
if(this.flattenedString == null) | ||
this.flattenedString = this.flattenCore(); | ||
return this.flattenedString; | ||
} | ||
|
||
@NonNull | ||
public String flattenCore() { | ||
try { | ||
JSONObject obj = new JSONObject(); | ||
obj.put("uv", this.uvIndex); | ||
obj.put("vis", this.illuminance); | ||
return obj.toString(); | ||
} catch (JSONException e) { | ||
// As per documented suggestion | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String toString() { | ||
return String.format("{%1$.1flux,%2$.1fuvi}", this.illuminance, this.uvIndex); | ||
} | ||
|
||
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); | ||
} | ||
|
||
} |
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
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
40 changes: 40 additions & 0 deletions
40
...in/java/app/uvtracker/sensor/pii/connection/application/event/NewSampleReceivedEvent.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,40 @@ | ||
package app.uvtracker.sensor.pii.connection.application.event; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.Date; | ||
|
||
import app.uvtracker.data.type.Record; | ||
import app.uvtracker.sensor.protocol.packet.in.PacketInNewSample; | ||
|
||
public class NewSampleReceivedEvent { | ||
|
||
@NonNull | ||
private final Date localTimestamp; | ||
|
||
private final int remoteTimestamp; | ||
|
||
@NonNull | ||
private final Record record; | ||
|
||
public NewSampleReceivedEvent(@NonNull PacketInNewSample packet) { | ||
this.localTimestamp = new Date(); | ||
this.remoteTimestamp = packet.getRemoteTimestamp(); | ||
this.record = packet.getRecord(); | ||
} | ||
|
||
@NonNull | ||
public Date getLocalTimestamp() { | ||
return localTimestamp; | ||
} | ||
|
||
public int getRemoteTimestamp() { | ||
return remoteTimestamp; | ||
} | ||
|
||
@NonNull | ||
public Record getRecord() { | ||
return record; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.