This commit is contained in:
Milos Kozak 2017-02-22 13:54:50 +01:00
commit 666874c6c4
7 changed files with 19 additions and 41 deletions

View file

@ -81,9 +81,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
static ArrayList<PluginBase> pluginList;
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledPost = null;
PowerManager.WakeLock mWakeLock;
public ConfigBuilderPlugin() {
@ -815,11 +812,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
return maxIobAfterConstrain;
}
@Subscribe
public void onStatusEvent(final EventNewBG ev) {
uploadDeviceStatus(120);
}
public void uploadTempBasalStartAbsolute(Double absolute, double durationInMinutes) {
try {
Context context = MainApp.instance().getApplicationContext();
@ -923,7 +915,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
}
public void doUploadDeviceStatus() {
public void uploadDeviceStatus() {
DeviceStatus deviceStatus = new DeviceStatus();
try {
LoopPlugin.LastRun lastRun = LoopPlugin.lastRun;
@ -968,12 +960,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
int batteryLevel = BatteryLevel.getBatteryLevel();
if (batteryLevel != BatteryLevel.lastUploadedLevel) {
JSONObject uploaderBattery = new JSONObject();
uploaderBattery.put("uploaderBattery", batteryLevel);
deviceStatus.uploaderBattery = uploaderBattery;
BatteryLevel.lastUploadedLevel = batteryLevel;
}
deviceStatus.uploaderBattery = batteryLevel;
deviceStatus.created_at = DateUtil.toISOString(new Date());
deviceStatus.sendToNSClient();
} catch (JSONException e) {
@ -981,22 +969,6 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
}
}
static public void uploadDeviceStatus(int sec) {
class PostRunnable implements Runnable {
public void run() {
MainApp.getConfigBuilder().doUploadDeviceStatus();
scheduledPost = null;
}
}
// prepare task for execution
// cancel waiting task to prevent sending multiple posts
if (scheduledPost != null)
scheduledPost.cancel(false);
Runnable task = new PostRunnable();
scheduledPost = worker.schedule(task, sec, TimeUnit.SECONDS);
log.debug("Scheduling devicestatus upload in " + sec + " sec");
}
public void uploadBolusWizardRecord(Treatment t, double glucose, String glucoseType, int carbTime, JSONObject boluscalc) {
JSONObject data = new JSONObject();
try {

View file

@ -341,7 +341,7 @@ public class ExecutionService extends Service {
danaRPump.lastConnection = now;
MainApp.bus().post(new EventDanaRNewStatus());
MainApp.bus().post(new EventInitializationChanged());
MainApp.getConfigBuilder().uploadDeviceStatus(15);
MainApp.getConfigBuilder().uploadDeviceStatus();
if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning ) {
log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits);
Notification reportFail = new Notification(Notification.APPROACHING_DAILY_LIMIT, MainApp.sResources.getString(R.string.approachingdailylimit), Notification.URGENT);

View file

@ -332,7 +332,7 @@ public class ExecutionService extends Service {
danaRKoreanPump.lastConnection = now;
MainApp.bus().post(new EventDanaRNewStatus());
MainApp.bus().post(new EventInitializationChanged());
MainApp.getConfigBuilder().uploadDeviceStatus(15);
MainApp.getConfigBuilder().uploadDeviceStatus();
if (danaRKoreanPump.dailyTotalUnits > danaRKoreanPump.maxDailyTotalUnits * Constants.dailyLimitWarning ) {
log.debug("Approaching daily limit: " + danaRKoreanPump.dailyTotalUnits + "/" + danaRKoreanPump.maxDailyTotalUnits);
Notification reportFail = new Notification(Notification.APPROACHING_DAILY_LIMIT, MainApp.sResources.getString(R.string.approachingdailylimit), Notification.URGENT);

View file

@ -378,7 +378,7 @@ public class DeviceStatus {
public JSONObject enacted = null;
public JSONObject suggested = null;
public JSONObject iob = null;
public JSONObject uploaderBattery = null;
public int uploaderBattery = 0;
public String created_at = null;
public JSONObject mongoRecord () {
@ -392,9 +392,9 @@ public class DeviceStatus {
if (enacted != null) openaps.put("enacted", enacted);
if (suggested != null) openaps.put("suggested", suggested);
if (iob != null) openaps.put("iob", iob);
if (uploaderBattery != null) openaps.put("uploaderBattery", uploaderBattery);
record.put("openaps", openaps);
}
if (uploaderBattery != 0) record.put("uploaderBattery", uploaderBattery);
if (created_at != null) record.put("created_at" , created_at);
} catch (JSONException e) {
e.printStackTrace();

View file

@ -228,7 +228,7 @@ public class LoopPlugin implements PluginBase {
}
MainApp.bus().post(new EventLoopUpdateGui());
MainApp.getConfigBuilder().uploadDeviceStatus(60);
MainApp.getConfigBuilder().uploadDeviceStatus();
} finally {
if (Config.logFunctionCalls)
log.debug("invoke end");

View file

@ -272,7 +272,7 @@ public class OverviewFragment extends Fragment {
finalLastRun.setByPump = applyResult;
finalLastRun.lastEnact = new Date();
finalLastRun.lastOpenModeAccept = new Date();
MainApp.getConfigBuilder().uploadDeviceStatus(15);
MainApp.getConfigBuilder().uploadDeviceStatus();
ObjectivesPlugin objectivesPlugin = (ObjectivesPlugin) MainApp.getSpecificPlugin(ObjectivesPlugin.class);
if (objectivesPlugin != null) {
objectivesPlugin.manualEnacts++;

View file

@ -4,6 +4,9 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
/**
@ -11,18 +14,21 @@ import info.nightscout.androidaps.MainApp;
*/
public class BatteryLevel {
private static Logger log = LoggerFactory.getLogger(BatteryLevel.class);
static public int lastUploadedLevel = 0;
static public int getBatteryLevel() {
int batteryLevel = 0;
Intent batteryIntent = MainApp.instance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (level == -1 || scale == -1) {
return 50;
if (level != -1 && scale != -1) {
batteryLevel = (int) (((float) level / (float) scale) * 100.0f);
}
return (int) (((float) level / (float) scale) * 100.0f);
} else return 50;
}
log.debug("Battery level: " + batteryLevel);
return batteryLevel;
}
}