Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nl #6

Merged
merged 22 commits into from
Jun 16, 2024
Merged

Nl #6

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified app/debug/app-debug.apk
Binary file not shown.
24 changes: 17 additions & 7 deletions app/src/main/java/esel/esel/esel/Esel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import esel.esel.esel.receivers.KeepAliveReceiver;
import esel.esel.esel.receivers.ReadReceiver;

import esel.esel.esel.util.SP;

/**
* Created by adrian on 04/08/17.
*/
Expand All @@ -23,8 +25,12 @@ public void onCreate() {
super.onCreate();
sInstance = this;
sResources = getResources();
startReadReceiver();
startKeepAliveService();

boolean use_patched_es = SP.getBoolean("use_patched_es", false);
if (use_patched_es) {
startKeepAliveService();
startReadReceiver();
}
}

public static Esel getsInstance() {
Expand All @@ -48,23 +54,27 @@ public synchronized void startReadReceiver() {


public synchronized void stopReadReceiver() {
if (readReceiver != null)
if (readReceiver != null) {
readReceiver.cancelAlarm(this);
readReceiver = null;
}
}


private void startKeepAliveService() {
public synchronized void startKeepAliveService() {
if (keepAliveReceiver == null) {
keepAliveReceiver = new KeepAliveReceiver();
keepAliveReceiver.setAlarm(this);
} else {
keepAliveReceiver.setAlarm(this);
}
}


public void stopKeepAliveService() {
if (keepAliveReceiver != null)
public synchronized void stopKeepAliveService() {
if (keepAliveReceiver != null) {
keepAliveReceiver.cancelAlarm(this);
keepAliveReceiver = null;
}
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/esel/esel/esel/LogActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void onCreate(Bundle savedInstanceState) {
}
public static void addLog(String type,String tag, String value){
String msg = SP.getString("logging","");
int lines_limit = 500;
int lines_limit = 800;
String[] lines = msg.split("\n");
if(lines.length>lines_limit){
int limit_to = (int)(lines_limit * 0.7);
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/esel/esel/esel/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -56,6 +57,27 @@ public void onClick(View view) {
.setAction("Action", null).show();
}
});*/

SharedPreferences.OnSharedPreferenceChangeListener settingsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("use_patched_es")){
if (SP.getBoolean("use_patched_es", false)){
Esel.getsInstance().startKeepAliveService();
EselLog.LogV(TAG, "START patched_es keepAlive recievers");
Esel.getsInstance().startReadReceiver();
EselLog.LogV(TAG, "START patched_es Read recievers");
} else {
Esel.getsInstance().stopReadReceiver();
EselLog.LogV(TAG, "STOP patched_es Read recievers");
Esel.getsInstance().stopKeepAliveService();
EselLog.LogV(TAG, "STOP patched_es keepAlive recievers");
}
}
}
};
SP.sharedPreferences.registerOnSharedPreferenceChangeListener(settingsListener);

buttonReadValue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import java.util.List;

import esel.esel.esel.util.SP;
import esel.esel.esel.receivers.ReadReceiver;

/**
* Created by bernhard on 24-01-18.
*/
public class EsNotificationListener extends NotificationListenerService {

private static List<SGV> lastReadings = new ArrayList<SGV>();
private static final ReadReceiver rr = new ReadReceiver();

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Expand All @@ -33,10 +35,14 @@ public void onNotificationPosted(StatusBarNotification sbn) {
SGV sgv = generateSGV(notification,lastReadings.size());
if(sgv != null) {
lastReadings.add(sgv);
rr.CallBroadcast(null);
}
} catch (NumberFormatException err) {
err.printStackTrace();
}



}
}

Expand Down Expand Up @@ -80,7 +86,7 @@ public static SGV generateSGV(Notification notification ,int record){
SGV oldSgv = lastReadings.get(lastReadings.size() - 1);
long lastreadingtime = oldSgv.timestamp; // SP.getLong("lastreadingtime_nl",timestamp);
int lastreadingvalue = oldSgv.raw; //SP.getInt("lastreadingvalue_nl",value);
if (value == lastreadingvalue && (lastreadingtime + (five_min * 1.05)) > timestamp ) { // no new value
if (value == lastreadingvalue && (lastreadingtime + (five_min * 1.1)) > timestamp ) { // no new value // 5 min 30 secs grace time
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ public EsNowDatareader() {

static public void updateLogin(){
EsNowDatareader reader = new EsNowDatareader();

if(reader.bearer_token == "" || reader.tokenHasExpired() ) {
reader.login();
}
if(reader.userId == 0 ){
reader.currentUser();
}
}

private void login() {
Expand Down
35 changes: 18 additions & 17 deletions app/src/main/java/esel/esel/esel/receivers/ReadReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import esel.esel.esel.util.LocalBroadcaster;
import esel.esel.esel.util.SP;
import esel.esel.esel.util.ToastUtils;
import retrofit2.Call;

/**
* Created by adrian on 04/08/17.
Expand All @@ -46,10 +47,16 @@ public synchronized void onReceive(Context context, Intent intent) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Esel:ReadReceiver:Broadcast");//Boradcast
wl.acquire();

EselLog.LogV(TAG,"onReceive called");
EselLog.LogV(TAG, "onReceive called");
setAlarm(Esel.getsInstance());

CallBroadcast(context);

wl.release();

}

public void CallBroadcast(Context context){
int sync = 8;
try {

Expand Down Expand Up @@ -93,8 +100,11 @@ public synchronized void onReceive(Context context, Intent intent) {
FullSync(context,sync);
}

boolean use_esdms = SP.getBoolean("use_esdms",false);
if(use_esdms){
EsNowDatareader.updateLogin();
}

wl.release();
}

public void FullSync(Context context, int syncHours){
Expand Down Expand Up @@ -216,7 +226,7 @@ private void WriteData(File file,String data){

}

public int broadcastData(Context context, long lastReadingTime, boolean smoothEnabled) {
public int broadcastData(Context context, long lastReadingTime, boolean is_continuous_run) {
int result = 0;
try {

Expand All @@ -227,7 +237,7 @@ public int broadcastData(Context context, long lastReadingTime, boolean smoothEn
long updatedReadingTime = lastReadingTime;

boolean use_patched_es = SP.getBoolean("use_patched_es", true);
boolean use_esdms = SP.getBoolean("use_esdms",false);


do {
lastReadingTime = updatedReadingTime;
Expand All @@ -254,9 +264,6 @@ public int broadcastData(Context context, long lastReadingTime, boolean smoothEn
}
}else {
boolean read_from_nl = true;
if(use_esdms){
EsNowDatareader.updateLogin();
}
if(read_from_nl){
valueArray = EsNotificationListener.getData(size,lastReadingTime);
}
Expand All @@ -266,7 +273,7 @@ public int broadcastData(Context context, long lastReadingTime, boolean smoothEn
return result;
}

result += ProcesssValues(smoothEnabled,valueArray);
result += ProcesssValues(is_continuous_run,valueArray);

updatedReadingTime = SP.getLong("lastReadingTime", lastReadingTime);
} while (updatedReadingTime != lastReadingTime);
Expand All @@ -285,7 +292,7 @@ public int broadcastData(Context context, long lastReadingTime, boolean smoothEn
return result;
}

private int ProcesssValues( boolean smoothEnabled, List<SGV> valueArray) {
private int ProcesssValues( boolean is_continuous_run, List<SGV> valueArray) {
int result = 0;

long currentTime = System.currentTimeMillis();
Expand Down Expand Up @@ -317,17 +324,11 @@ private int ProcesssValues( boolean smoothEnabled, List<SGV> valueArray) {

if (sgv.value >= 39 /*&& oldValue >= 39*/) { //check for old value to ignore first 5 min
//ToastUtils.makeToast(sgv.toString());
if(smoothEnabled) {
if(is_continuous_run) {
boolean enable_smooth = SP.getBoolean("smooth_data", false) && !hasTimeGap;
sgv.smooth(oldValue, enable_smooth);
}

// if(SP.getBoolean("smooth_data",false) && smoothEnabled){
// sgv.smooth(oldValue,hasTimeGap);
// }else {
// sgv.smooth(oldValue,true); //
// }

double slopeByMinute = 0d;
if (oldTime != sgvTime) {
slopeByMinute = (sgv.value - oldValue ) * 60000.0d / (( sgvTime - oldTime) * 1.0d);
Expand All @@ -338,7 +339,7 @@ private int ProcesssValues( boolean smoothEnabled, List<SGV> valueArray) {

try {
if (!suppressBroadcast) {
LocalBroadcaster.broadcast(sgv);
LocalBroadcaster.broadcast(sgv,is_continuous_run);
} else {
LocalBroadcaster.addSgvEntry(output, sgv);
}
Expand Down
17 changes: 10 additions & 7 deletions app/src/main/java/esel/esel/esel/util/LocalBroadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class LocalBroadcaster {
private static final String TAG = "LocalBroadcaster";
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);

public static void broadcast(SGV sgv) {
public static void broadcast(SGV sgv, boolean log) {
try {

int timeShift = SP.getInt("shift_days",0);
Expand All @@ -40,15 +40,16 @@ public static void broadcast(SGV sgv) {
if (SP.getBoolean("send_to_AAPS", true)) {
final JSONArray entriesBody = new JSONArray();
addSgvEntry(entriesBody, sgv);
sendBundle("add", "entries", entriesBody, XDRIP_PLUS_NS_EMULATOR);
sendBundle("add", "entries", entriesBody, XDRIP_PLUS_NS_EMULATOR, log);
}

if (SP.getBoolean("send_to_NS", true)) {
sendBundle("dbAdd", "entries", generateSgvEntry(sgv), ACTION_DATABASE);
sendBundle("dbAdd", "entries", generateSgvEntry(sgv), ACTION_DATABASE, log);
}

EselLog.LogI(TAG, String.valueOf(sgv.value) + " " + sgv.direction );

if(log) {
EselLog.LogI(TAG, String.valueOf(sgv.value) + " " + sgv.direction);
}
} catch (Exception e) {
String msg = "Unable to send bundle: " + e;
EselLog.LogE(TAG,msg);
Expand Down Expand Up @@ -77,7 +78,7 @@ private static JSONObject generateSgvEntry(SGV sgv) throws Exception {
return json;
}

private static void sendBundle(String action, String collection, Object json, String intentIdAction) {
private static void sendBundle(String action, String collection, Object json, String intentIdAction,boolean log) {
final Bundle bundle = new Bundle();
bundle.putString("action", action);
bundle.putString("collection", collection);
Expand All @@ -94,7 +95,9 @@ private static void sendBundle(String action, String collection, Object json, St
if (packageName != null) {
intent.setPackage(packageName);
Esel.getsInstance().sendBroadcast(intent);
EselLog.LogI(TAG,"send to: " + packageName);
if(log) {
EselLog.LogI(TAG, "send to: " + packageName);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/esel/esel/esel/util/SP.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

public class SP {
static SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Esel.getsInstance().getApplicationContext());
static public SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(Esel.getsInstance().getApplicationContext());

static public boolean contains(String key) {
return sharedPreferences.contains(key);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<resources>
<string name="app_name">Esel</string>
<string name="app_version">Version 3.0.1</string>
<string name="app_version">Version 3.0.2_nl</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
Expand Down
Loading