uploader battery always in json

This commit is contained in:
Milos Kozak 2017-02-21 13:45:33 +01:00
parent 2e0f6e8914
commit 4b6276c0b1
2 changed files with 14 additions and 10 deletions

View file

@ -960,12 +960,10 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
} }
int batteryLevel = BatteryLevel.getBatteryLevel(); int batteryLevel = BatteryLevel.getBatteryLevel();
if (batteryLevel != BatteryLevel.lastUploadedLevel) { JSONObject uploaderBattery = new JSONObject();
JSONObject uploaderBattery = new JSONObject(); uploaderBattery.put("uploaderBattery", batteryLevel);
uploaderBattery.put("uploaderBattery", batteryLevel); deviceStatus.uploaderBattery = uploaderBattery;
deviceStatus.uploaderBattery = uploaderBattery;
BatteryLevel.lastUploadedLevel = batteryLevel;
}
deviceStatus.created_at = DateUtil.toISOString(new Date()); deviceStatus.created_at = DateUtil.toISOString(new Date());
deviceStatus.sendToNSClient(); deviceStatus.sendToNSClient();
} catch (JSONException e) { } catch (JSONException e) {

View file

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