use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.
the class OpenScale method addScaleData.
public int addScaleData(final ScaleMeasurement scaleMeasurement, boolean silent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (scaleMeasurement.getUserId() == -1) {
if (prefs.getBoolean("smartUserAssign", false)) {
scaleMeasurement.setUserId(getSmartUserAssignment(scaleMeasurement.getWeight(), 15.0f));
} else {
scaleMeasurement.setUserId(getSelectedScaleUser().getId());
}
// don't add scale data if no user is selected
if (scaleMeasurement.getUserId() == -1) {
return -1;
}
}
MeasurementViewSettings settings = new MeasurementViewSettings(prefs, WaterMeasurementView.KEY);
if (settings.isEnabled() && settings.isEstimationEnabled()) {
EstimatedWaterMetric waterMetric = EstimatedWaterMetric.getEstimatedMetric(EstimatedWaterMetric.FORMULA.valueOf(settings.getEstimationFormula()));
scaleMeasurement.setWater(waterMetric.getWater(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement));
}
settings = new MeasurementViewSettings(prefs, LBWMeasurementView.KEY);
if (settings.isEnabled() && settings.isEstimationEnabled()) {
EstimatedLBWMetric lbwMetric = EstimatedLBWMetric.getEstimatedMetric(EstimatedLBWMetric.FORMULA.valueOf(settings.getEstimationFormula()));
scaleMeasurement.setLbw(lbwMetric.getLBW(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement));
}
settings = new MeasurementViewSettings(prefs, FatMeasurementView.KEY);
if (settings.isEnabled() && settings.isEstimationEnabled()) {
EstimatedFatMetric fatMetric = EstimatedFatMetric.getEstimatedMetric(EstimatedFatMetric.FORMULA.valueOf(settings.getEstimationFormula()));
scaleMeasurement.setFat(fatMetric.getFat(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement));
}
if (measurementDAO.insert(scaleMeasurement) != -1) {
ScaleUser scaleUser = getScaleUser(scaleMeasurement.getUserId());
final java.text.DateFormat dateFormat = DateFormat.getDateFormat(context);
final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context);
final Date dateTime = scaleMeasurement.getDateTime();
final Converters.WeightUnit unit = scaleUser.getScaleUnit();
if (!silent) {
String infoText = String.format(context.getString(R.string.info_new_data_added), scaleMeasurement.getConvertedWeight(unit), unit.toString(), dateFormat.format(dateTime) + " " + timeFormat.format(dateTime), scaleUser.getUserName());
Toast.makeText(context, infoText, Toast.LENGTH_LONG).show();
}
alarmHandler.entryChanged(context, scaleMeasurement);
updateScaleData();
} else {
if (!silent) {
Toast.makeText(context, context.getString(R.string.info_new_data_duplicated), Toast.LENGTH_LONG).show();
}
}
return scaleMeasurement.getUserId();
}
use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.
the class BluetoothSanitasSbf70 method nextInitCmd.
@Override
boolean nextInitCmd(int stateNr) {
switch(stateNr) {
case 0:
// Initialize data
currentScaleUserId = -1;
countRegisteredScaleUsers = -1;
maxRegisteredScaleUser = -1;
seenUsers = new TreeSet<>();
// Setup notification
setNotificationOn(CUSTOM_SERVICE_1, CUSTOM_CHARACTERISTIC_WEIGHT, CLIENT_CHARACTERISTICS_CONFIGURATION);
break;
case 1:
// Say "Hello" to the scale
writeBytes(new byte[] { (byte) 0xe6, (byte) 0x01 });
break;
case 2:
// Update timestamp of the scale
updateDateTimeSanitas();
break;
case 3:
// Set measurement unit
setUnitCommand();
break;
case 4:
// Request general user information
writeBytes(new byte[] { (byte) 0xe7, (byte) 0x33 });
break;
case 5:
// Wait for ack of all users
if (seenUsers.size() < countRegisteredScaleUsers || (countRegisteredScaleUsers == -1)) {
// Request this state again
setNextCmd(stateNr);
break;
}
// Check if not found/unknown
if (currentScaleUserId == 0) {
// Unknown user, request creation of new user
if (countRegisteredScaleUsers == maxRegisteredScaleUser) {
setBtMachineState(BT_MACHINE_STATE.BT_CLEANUP_STATE);
Log.d(TAG, "Cannot create additional scale user");
sendMessage(R.string.error_max_scale_users, 0);
break;
}
// Request creation of user
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
// We can only use up to 3 characters and have to handle them uppercase
int maxIdx = selectedUser.getUserName().length() >= 3 ? 3 : selectedUser.getUserName().length();
byte[] nick = selectedUser.getUserName().toUpperCase().substring(0, maxIdx).getBytes();
// activity level: 1 - 5
byte activity = 2;
Log.d(TAG, "Create User:" + selectedUser.getUserName());
writeBytes(new byte[] { (byte) 0xe7, (byte) 0x31, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) (seenUsers.size() > 0 ? Collections.max(seenUsers) + 1 : 101), nick[0], nick[1], nick[2], (byte) selectedUser.getBirthday().getYear(), (byte) selectedUser.getBirthday().getMonth(), (byte) selectedUser.getBirthday().getDate(), (byte) selectedUser.getBodyHeight(), (byte) (((selectedUser.getGender().isMale() ? 1 : 0) << 7) | activity) });
} else {
// Get existing user information
Log.d(TAG, "Request getUserInfo " + currentScaleUserId);
writeBytes(new byte[] { (byte) 0xe7, (byte) 0x36, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) currentScaleUserId });
}
Log.d(TAG, "scaleuserid:" + currentScaleUserId + " registered users: " + countRegisteredScaleUsers + " extracted users: " + seenUsers.size());
break;
case 6:
break;
default:
// Finish init if everything is done
return false;
}
return true;
}
use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.
the class BluetoothSanitasSbf70 method onBluetoothDataChange.
@Override
public void onBluetoothDataChange(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic gattCharacteristic) {
byte[] data = gattCharacteristic.getValue();
if (data.length == 0)
return;
if ((data[0] & 0xFF) == 0xe6 && (data[1] & 0xFF) == 0x00) {
Log.d(TAG, "ACK Scale is ready");
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0xf0 && data[2] == 0x33) {
Log.d(TAG, "ACK Got general user information");
int count = (byte) (data[4] & 0xFF);
int maxUsers = (byte) (data[5] & 0xFF);
Log.d(TAG, "Count:" + count + " maxUsers:" + maxUsers);
countRegisteredScaleUsers = count;
// Check if any scale user is registered
if (count == 0)
// Unknown user
currentScaleUserId = 0;
maxRegisteredScaleUser = maxUsers;
nextMachineStateStep();
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0x34) {
Log.d(TAG, "Ack Get UUIDSs List of Users");
byte currentUserMax = (byte) (data[2] & 0xFF);
byte currentUserID = (byte) (data[3] & 0xFF);
byte userUuid = (byte) (data[11] & 0xFF);
String name = new String(data, 12, 3);
int year = (byte) (data[15] & 0xFF);
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
// Check if we found the currently selected user
if (selectedUser.getUserName().toLowerCase().startsWith(name.toLowerCase()) && selectedUser.getBirthday().getYear() == year) {
// Found user
currentScaleUserId = userUuid;
}
// Remember this uuid from the scale
if (seenUsers.add((int) userUuid)) {
if (currentScaleUserId == -1 && seenUsers.size() == countRegisteredScaleUsers) {
// We have seen all users: user is unknown
currentScaleUserId = 0;
}
Log.d(TAG, "Send ack gotUser");
writeBytes(new byte[] { (byte) 0xe7, (byte) 0xf1, (byte) 0x34, currentUserMax, currentUserID });
}
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0xF0 && (data[2] & 0xFF) == 0x36) {
Log.d(TAG, "Ack Get User Info Initials");
String name = new String(data, 4, 3);
byte year = (byte) (data[7] & 0xFF);
byte month = (byte) (data[8] & 0xFF);
byte day = (byte) (data[9] & 0xFF);
int height = (data[10] & 0xFF);
boolean male = (data[11] & 0xF0) != 0;
byte activity = (byte) (data[11] & 0x0F);
Log.d(TAG, "Name " + name + " YY-MM-DD: " + year + " " + month + " " + day + "Height: " + height + " Sex:" + (male ? "M" : "F") + "activity: " + activity);
// Get scale status for user
writeBytes(new byte[] { (byte) 0xe7, (byte) 0x4f, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) currentScaleUserId });
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0xf0 && (data[2] & 0xFF) == 0x4F) {
Log.d(TAG, "Ack Get scale status");
int unknown = data[3];
int batteryLevel = (data[4] & 0xFF);
float weightThreshold = (data[5] & 0xFF) / 10f;
float bodyFatThreshold = (data[6] & 0xFF) / 10f;
// 1 kg, 2 lb (pounds), 3 st stone
int unit = data[7];
boolean userExists = (data[8] == 0);
boolean userReferWeightExists = (data[9] == 0);
boolean userMeasurementExist = (data[10] == 0);
int scaleVersion = data[11];
Log.d(TAG, "BatteryLevel:" + batteryLevel + " weightThreshold: " + weightThreshold + " BodyFatThresh: " + bodyFatThreshold + " Unit: " + unit + " userExists: " + userExists + " UserReference Weight Exists:" + userReferWeightExists + " UserMeasurementExists " + userMeasurementExist + " scaleVersion" + scaleVersion);
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0xf0 && data[2] == 0x31) {
Log.d(TAG, "Acknowledge creation of user");
// Indicate user to step on scale
sendMessage(R.string.info_step_on_scale, 0);
// Request basement measurement
writeBytes(new byte[] { (byte) 0xe7, 0x40, 0, 0, 0, 0, 0, 0, 0, (byte) (seenUsers.size() > 0 ? Collections.max(seenUsers) + 1 : 101) });
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0xf0 && (data[2] & 0xFF) == 0x41) {
Log.d(TAG, "Will start to receive measurements User Specific");
byte nr_measurements = data[3];
Log.d(TAG, "New measurements: " + nr_measurements / 2);
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0x42) {
Log.d(TAG, "Specific measurement User specific");
// Measurements are split into two parts
int max_items = data[2] & 0xFF;
int current_item = data[3] & 0xFF;
// Received even part
if (current_item % 2 == 1) {
receivedScaleData = new ByteArrayOutputStream();
}
try {
receivedScaleData.write(Arrays.copyOfRange(data, 4, data.length));
} catch (IOException e) {
e.printStackTrace();
}
// Send acknowledgement
writeBytes(new byte[] { (byte) 0xe7, (byte) 0xf1, (byte) 0x42, (byte) (data[2] & 0xFF), (byte) (data[3] & 0xFF) });
if (current_item % 2 == 0) {
try {
ScaleMeasurement parsedData = parseScaleData(receivedScaleData.toByteArray());
addScaleData(parsedData);
} catch (ParseException e) {
Log.d(TAG, "Could not parse byte array: " + byteInHex(receivedScaleData.toByteArray()));
e.printStackTrace();
}
}
if (current_item == max_items) {
// finish and delete
deleteScaleData();
}
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0x58) {
Log.d(TAG, "Active measurement");
if ((data[2] & 0xFF) != 0x00) {
// little endian
float weight = ((float) (((data[3] & 0xFF) << 8) + (data[4] & 0xFF))) * 50.0f / // unit is 50g
1000.0f;
// temporary value;
sendMessage(R.string.info_measuring, weight);
return;
}
// stabilized value
// little endian
float weight = ((float) (((data[3] & 0xFF) << 8) + (data[4] & 0xFF))) * 50.0f / // unit is 50g
1000.0f;
Log.i(TAG, "Got weight: " + weight);
writeBytes(new byte[] { (byte) 0xe7, (byte) 0xf1, (byte) (data[1] & 0xFF), (byte) (data[2] & 0xFF), (byte) (data[3] & 0xFF) });
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0x59) {
// Get stable measurement results
Log.d(TAG, "Get measurement data " + ((int) data[3]));
int max_items = (data[2] & 0xFF);
int current_item = (data[3] & 0xFF);
// Received first part
if (current_item == 1) {
receivedScaleData = new ByteArrayOutputStream();
} else {
try {
receivedScaleData.write(Arrays.copyOfRange(data, 4, data.length));
} catch (IOException e) {
e.printStackTrace();
}
}
// Send ack that we got the data
writeBytes(new byte[] { (byte) 0xe7, (byte) 0xf1, (byte) (data[1] & 0xFF), (byte) (data[2] & 0xFF), (byte) (data[3] & 0xFF) });
if (current_item == max_items) {
// received all parts
ScaleMeasurement parsedData = null;
try {
parsedData = parseScaleData(receivedScaleData.toByteArray());
addScaleData(parsedData);
// Delete data
deleteScaleData();
} catch (ParseException e) {
Log.d(TAG, "Parse Exception " + byteInHex(receivedScaleData.toByteArray()));
}
}
return;
}
if ((data[0] & 0xFF) == 0xe7 && (data[1] & 0xFF) == 0xf0 && (data[2] & 0xFF) == 0x43) {
Log.d(TAG, "Acknowledge: Data deleted.");
return;
}
Log.d(TAG, "DataChanged - not handled: " + byteInHex(data));
}
use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.
the class BluetoothYunmaiMini method parseBytes.
private void parseBytes(byte[] weightBytes) {
long unix_timestamp = ((weightBytes[5] & 0xFF) << 24) | ((weightBytes[6] & 0xFF) << 16) | ((weightBytes[7] & 0xFF) << 8) | (weightBytes[8] & 0xFF);
Date btDate = new Date();
btDate.setTime(unix_timestamp * 1000);
float weight = (float) (((weightBytes[13] & 0xFF) << 8) | (weightBytes[14] & 0xFF)) / 100.0f;
float fat = (float) (((weightBytes[17] & 0xFF) << 8) | (weightBytes[18] & 0xFF)) / 100.0f;
ScaleMeasurement scaleBtData = new ScaleMeasurement();
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
scaleBtData.setConvertedWeight(weight, selectedUser.getScaleUnit());
scaleBtData.setFat(fat);
scaleBtData.setDateTime(btDate);
addScaleData(scaleBtData);
}
use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.
the class BluetoothYunmaiSE method parseBytes.
private void parseBytes(byte[] weightBytes) {
long unix_timestamp = ((weightBytes[5] & 0xFF) << 24) | ((weightBytes[6] & 0xFF) << 16) | ((weightBytes[7] & 0xFF) << 8) | (weightBytes[8] & 0xFF);
Date btDate = new Date();
btDate.setTime(unix_timestamp * 1000);
float weight = (float) (((weightBytes[13] & 0xFF) << 8) | (weightBytes[14] & 0xFF)) / 100.0f;
ScaleMeasurement scaleBtData = new ScaleMeasurement();
final ScaleUser selectedUser = OpenScale.getInstance(context).getSelectedScaleUser();
scaleBtData.setConvertedWeight(weight, selectedUser.getScaleUnit());
scaleBtData.setDateTime(btDate);
addScaleData(scaleBtData);
}
Aggregations