package com.uti.sensors.bleshow;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TableLayout;

import com.polidea.rxandroidble.RxBleConnection;

import java.util.UUID;

import rx.Observable;

import static java.lang.StrictMath.pow;

/**
 * Created by kunyi on 2017/5/14.
 */

public class LuxometerProfile extends GenericProfile {
    public static final String TAG = "LuxometerProfile";
    private final static String GattServ = "F000AA70-0451-4000-B000-0000000000000";
    private final static String GattData = "F000AA71-0451-4000-B000-0000000000000";
    private final static String GattConf = "F000AA72-0451-4000-B000-0000000000000";
    private final static String GattPeri = "F000AA73-0451-4000-B000-0000000000000";
    private final static byte[] Bconf =  new byte[] {(byte)0x01 };
    private double lux;

    public LuxometerProfile(@NonNull Observable<RxBleConnection> conn) {
        super(conn,
                UUID.fromString(GattServ),
                UUID.fromString(GattConf),
                UUID.fromString(GattData),
                UUID.fromString(GattPeri),
                Bconf);
    }

    @Override
    public boolean registerNotification(Context con, View parenet, TableLayout tabLayout) {
        super.registerNotificationImp(bytes -> {
            convertRaw(bytes);
            if (DBG)
                Log.d(TAG, "Lux:" + lux);
        });
        return false;
    }

    @Override
    public boolean configuration() {
        super.configurationImp(bytes -> {
            if (DBG)
                Log.d(TAG, "Configuration");
        });
        return true;
    }

    @Override
    protected void convertRaw(byte[] bytes) {
        Integer i = u16AtOffset(bytes, 0);
        int mantissa = i & 0x0FFF;
        int exponent = (i >> 12) & 0xFF;
        double magnitude = pow(2.0f, exponent);
        lux = (magnitude * mantissa) / 100.0F;
    }
}

@@ -187,16 +187,48 @@
 eLayout;
+%0Aimport android.widget.TableRow;
 %0A%0Aimport
@@ -269,16 +269,52 @@
 nection;
+%0Aimport com.uti.Utils.GenericTabRow;
 %0A%0Aimport
@@ -498,21 +498,22 @@
  %7B%0A    p
-ublic
+rivate
  static 
@@ -926,17 +926,16 @@
  Bconf =
- 
  new byt
@@ -941,21 +941,20 @@
 te%5B%5D
- 
 %7B(byte)
+ 
 0x01
- 
 %7D;%0A 
@@ -964,22 +964,123 @@
 private 
-double
+final boolean DBG = false;%0A    protected GenericTabRow tr;%0A    protected Context context;%0A    private float
  lux;%0A%0A 
@@ -1492,77 +1492,805 @@
     
-super.registerNotificationImp(bytes -%3E %7B%0A            convertRaw(bytes
+tr = new GenericTabRow(con);%0A        tr.sl1.autoScale = true;%0A        tr.sl1.autoScaleBounceBack = true;%0A        tr.sl1.setColor(255, 0, 150, 125);%0A        tr.setIcon(%22sensortag2%22, %22lightsensor%22);%0A        tr.title.setText(%22Luxometer Data%22);%0A        tr.uuidLabel.setText(GattData);%0A        tr.value.setText(%220.0 Lux%22);%0A        tr.periodBar.setProgress(100);%0A        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));%0A        tabLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));%0A%0A        super.registerNotificationImp(bytes -%3E %7B%0A            convertRaw(bytes);%0A            tr.value.setText(String.format(%22%25.1f Lux%22, lux));%0A            tr.sl1.addValue(lux
 );%0A 
@@ -2782,14 +2782,13 @@
     
-double
+float
  mag
@@ -2796,16 +2796,23 @@
 itude = 
+(float)
 pow(2.0f

implement UI of Luxometer