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 TempertureProfile extends GenericProfile {
    private final boolean DBG = false;
    private final String TAG = "TempertureProfile";
    private final static String GattServ = "F000AA00-0451-4000-B000-0000000000000";
    private final static String GattData = "F000AA01-0451-4000-B000-0000000000000";
    private final static String GattConf = "F000AA02-0451-4000-B000-0000000000000";
    private final static String GattPeri = "F000AA03-0451-4000-B000-0000000000000";
    private final static byte[] Bconf =  new byte[] {(byte)0x01 };
    private double ambient;
    private double target;

    public TempertureProfile(@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, "Ambient value:" + ambient + ", Target:" + target);
        });
        return true;
    }

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

    @Override
    protected void convertRaw(byte[] bytes) {
        final double SCALE_LSB = (0.03125f);
        // TI -- TMP007 sensor algorithm
        target = (int16AtOffset(bytes, 0)>>2)*SCALE_LSB;
        ambient = (int16AtOffset(bytes, 2)>>2)*SCALE_LSB;
    }
}

@@ -102,16 +102,61 @@
 onNull;%0A
+import android.support.annotation.StringDef;%0A
 import a
@@ -232,16 +232,48 @@
 eLayout;
+%0Aimport android.widget.TableRow;
 %0A%0Aimport
@@ -314,16 +314,52 @@
 nection;
+%0Aimport com.uti.Utils.GenericTabRow;
 %0A%0Aimport
@@ -1087,16 +1087,111 @@
  target;
+%0A    protected GenericTabRow tr;%0A    protected GenericTabRow am;%0A    protected Context context;
 %0A%0A    pu
@@ -1583,24 +1583,1424 @@
 abLayout) %7B%0A
+        am = new GenericTabRow(con);%0A        am.sl1.autoScale = true;%0A        am.sl1.autoScale = true;%0A        am.sl1.autoScaleBounceBack = true;%0A        am.setIcon(%22sensortag2%22, %22temperature%22);%0A        am.title.setText(%22Ambient Temperature Data%22);%0A        am.uuidLabel.setText(GattData);%0A        am.value.setText(%220.0'C%22);%0A        am.periodMinVal = 200;%0A        am.periodBar.setMax(255 - (am.periodMinVal/10));%0A        am.periodBar.setProgress(100);%0A        am.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));%0A        tr = new GenericTabRow(con);%0A        tr.sl1.autoScale = true;%0A        tr.sl1.autoScaleBounceBack = true;%0A        tr.setIcon(%22sensortag2%22, %22irtemperature%22);%0A        tr.title.setText(%22IR Temperature Data%22);%0A        tr.uuidLabel.setText(GattData);%0A        tr.value.setText(%220.0'C%22);%0A        tr.periodMinVal = 200;%0A        tr.periodBar.setMax(255 - (tr.periodMinVal/10));%0A        tr.periodBar.setProgress(100);%0A        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));%0A        tabLayout.addView(am, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));%0A        tabLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));%0A%0A%0A
         supe
@@ -3167,16 +3167,142 @@
 arget);%0A
+            am.value.setText(String.format(%22%25.1fC%22, ambient));%0A            tr.value.setText(String.format(%22%25.1fC%22, target));%0A%0A
         

update Termperture profile