package com.kfb.kfbv1;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kfb.kfbv1.db.DbManage;
import com.kfb.kfbv1.db.Urzadzenia;
import com.kfb.kfbv1.dummy.DummyContent;

import com.kfb.kfbv1.media.KfbINtercom;
import com.kfb.kfbv1.model.KfbConstans;
import com.kfb.kfbv1.model.KfbGate;
import com.kfb.kfbv1.model.KfbHttpRequest;
import com.kfb.kfbv1.model.KfbModel;
import com.kfb.kfbv1.model.KfbTablet;
import com.kfb.kfbv1.model.KfbTask;
import com.kfb.kfbv1.model.KfbTaskTablet;
import com.kfb.kfbv1.shttp.KfbHttp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GateActivity  extends AppCompatActivity
{
    private KfbHttp server;
private GateActivity thisActivite;
    private Toolbar toolbar;
    private MyItemRecyclerViewAdapter listAdapter;
    private List<DummyContent.DummyItem> ld1 = new ArrayList<DummyContent.DummyItem>() ;
    private RecyclerView recycler;
    private KfbINtercom kfbinter = new KfbINtercom();
    private int stan1Mic=0;
    private int stan2Mic=0;
    private int stan3Mic=0;
    private int stan4Mic=0;
    private RecyclerView.LayoutManager layoutManager;
    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==2) {
                if(KfbModel.getData().getGates().getmGate().get(msg.arg1).getId()==0) {
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 0) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta);
                        i1.setImageResource(R.drawable.off);
                        i1 = (ImageView) findViewById(R.id.g1tekst);
                        i1.setImageResource(R.drawable.txt_off);
                        i1 = (ImageView) findViewById(R.id.g1hist);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g1pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 1) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta);
                        i1.setImageResource(R.drawable.standby);
                        i1 = (ImageView) findViewById(R.id.g1tekst);
                        i1.setImageResource(R.drawable.txt_standby);
                        i1 = (ImageView) findViewById(R.id.g1hist);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g1pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 2) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta);
                        i1.setImageResource(R.drawable.occupied);
                        i1 = (ImageView) findViewById(R.id.g1tekst);
                        i1.setImageResource(R.drawable.txt_occupied);
                        i1 = (ImageView) findViewById(R.id.g1hist);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHist());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g1pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 3) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta);
                        i1.setImageResource(R.drawable.ok);
                        i1 = (ImageView) findViewById(R.id.g1tekst);
                        i1.setImageResource(R.drawable.txt_ok);
                        i1 = (ImageView) findViewById(R.id.g1hist);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g1pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 4) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta);
                        i1.setImageResource(R.drawable.not_ok);
                        i1 = (ImageView) findViewById(R.id.g1tekst);
                        i1.setImageResource(R.drawable.txt_not_ok);
                        i1 = (ImageView) findViewById(R.id.g1hist);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g1pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 5) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta);
                        i1.setImageResource(R.drawable.lock);
                        i1 = (ImageView) findViewById(R.id.g1tekst);
                        i1.setImageResource(R.drawable.txt_lock);
                        i1 = (ImageView) findViewById(R.id.g1hist);
                        i1.setVisibility(View.INVISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g1pin);
                        t1.setVisibility(View.VISIBLE);
                    }
                }
                if(KfbModel.getData().getGates().getmGate().get(msg.arg1).getId()==1) {
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 0) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta2);
                        i1.setImageResource(R.drawable.off);
                        i1 = (ImageView) findViewById(R.id.g1tekst2);
                        i1.setImageResource(R.drawable.txt_off);
                        i1 = (ImageView) findViewById(R.id.g1hist2);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g2pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 1) {
                    ImageView i1 = (ImageView) findViewById(R.id.g1sta2);
                    i1.setImageResource(R.drawable.standby);
                    i1 = (ImageView) findViewById(R.id.g1tekst2);
                    i1.setImageResource(R.drawable.txt_standby);
                    i1 = (ImageView) findViewById(R.id.g1hist2);
                    i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                    EditText t1= (EditText)findViewById(R.id.g2pin);
                    t1.setVisibility(View.INVISIBLE);
                    t1.setText("");
                }
                if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 2) {
                    ImageView i1 = (ImageView) findViewById(R.id.g1sta2);
                    i1.setImageResource(R.drawable.occupied);
                    i1 = (ImageView) findViewById(R.id.g1tekst2);
                    i1.setImageResource(R.drawable.txt_occupied);
                    i1 = (ImageView) findViewById(R.id.g1hist2);
                    i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHist());
                    i1.setVisibility(View.VISIBLE);
                    EditText t1= (EditText)findViewById(R.id.g2pin);
                    t1.setVisibility(View.INVISIBLE);
                    t1.setText("");
                }
                if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 3) {
                    ImageView i1 = (ImageView) findViewById(R.id.g1sta2);
                    i1.setImageResource(R.drawable.ok);
                    i1 = (ImageView) findViewById(R.id.g1tekst2);
                    i1.setImageResource(R.drawable.txt_ok);
                    i1 = (ImageView) findViewById(R.id.g1hist2);
                    i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                    i1.setVisibility(View.VISIBLE);
                    EditText t1= (EditText)findViewById(R.id.g2pin);
                    t1.setVisibility(View.INVISIBLE);
                    t1.setText("");
                }
                if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 4) {
                    ImageView i1 = (ImageView) findViewById(R.id.g1sta2);
                    i1.setImageResource(R.drawable.not_ok);
                    i1 = (ImageView) findViewById(R.id.g1tekst2);
                    i1.setImageResource(R.drawable.txt_not_ok);
                    i1 = (ImageView) findViewById(R.id.g1hist2);
                    i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                    i1.setVisibility(View.VISIBLE);
                    EditText t1= (EditText)findViewById(R.id.g2pin);
                    t1.setVisibility(View.INVISIBLE);
                    t1.setText("");
                }
                if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 5) {
                    ImageView i1 = (ImageView) findViewById(R.id.g1sta2);
                    i1.setImageResource(R.drawable.lock);
                    i1 = (ImageView) findViewById(R.id.g1tekst2);
                    i1.setImageResource(R.drawable.txt_lock);
                    i1 = (ImageView) findViewById(R.id.g1hist2);
                    i1.setVisibility(View.INVISIBLE);
                    EditText t1= (EditText)findViewById(R.id.g2pin);
                    t1.setVisibility(View.VISIBLE);
                }
            }
                if(KfbModel.getData().getGates().getmGate().get(msg.arg1).getId()==2) {
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 0) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta3);
                        i1.setImageResource(R.drawable.off);
                        i1 = (ImageView) findViewById(R.id.g1tekst3);
                        i1.setImageResource(R.drawable.txt_off);
                        i1 = (ImageView) findViewById(R.id.g1hist3);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g3pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 1) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta3);
                        i1.setImageResource(R.drawable.standby);
                        i1 = (ImageView) findViewById(R.id.g1tekst3);
                        i1.setImageResource(R.drawable.txt_standby);
                        i1 = (ImageView) findViewById(R.id.g1hist3);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g3pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 2) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta3);
                        i1.setImageResource(R.drawable.occupied);
                        i1 = (ImageView) findViewById(R.id.g1tekst3);
                        i1.setImageResource(R.drawable.txt_occupied);
                        i1 = (ImageView) findViewById(R.id.g1hist3);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHist());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g3pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 3) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta3);
                        i1.setImageResource(R.drawable.ok);
                        i1 = (ImageView) findViewById(R.id.g1tekst3);
                        i1.setImageResource(R.drawable.txt_ok);
                        i1 = (ImageView) findViewById(R.id.g1hist3);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g3pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 4) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta3);
                        i1.setImageResource(R.drawable.not_ok);
                        i1 = (ImageView) findViewById(R.id.g1tekst3);
                        i1.setImageResource(R.drawable.txt_not_ok);
                        i1 = (ImageView) findViewById(R.id.g1hist3);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g3pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 5) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta3);
                        i1.setImageResource(R.drawable.lock);
                        i1 = (ImageView) findViewById(R.id.g1tekst3);
                        i1.setImageResource(R.drawable.txt_lock);
                        i1 = (ImageView) findViewById(R.id.g1hist3);
                        i1.setVisibility(View.INVISIBLE);
                        EditText t1= (EditText)findViewById(R.id.g3pin);
                        t1.setVisibility(View.VISIBLE);
                    }
                }
                if(KfbModel.getData().getGates().getmGate().get(msg.arg1).getId()==3) {
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 0) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta4);
                        i1.setImageResource(R.drawable.off);
                        i1 = (ImageView) findViewById(R.id.g1tekst4);
                        i1.setImageResource(R.drawable.txt_off);
                        i1 = (ImageView) findViewById(R.id.g1hist4);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1 = (EditText) findViewById(R.id.g4pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 1) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta4);
                        i1.setImageResource(R.drawable.standby);
                        i1 = (ImageView) findViewById(R.id.g1tekst4);
                        i1.setImageResource(R.drawable.txt_standby);
                        i1 = (ImageView) findViewById(R.id.g1hist4);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1 = (EditText) findViewById(R.id.g4pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 2) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta4);
                        i1.setImageResource(R.drawable.occupied);
                        i1 = (ImageView) findViewById(R.id.g1tekst4);
                        i1.setImageResource(R.drawable.txt_occupied);
                        i1 = (ImageView) findViewById(R.id.g1hist4);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHist());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1 = (EditText) findViewById(R.id.g4pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 3) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta4);
                        i1.setImageResource(R.drawable.ok);
                        i1 = (ImageView) findViewById(R.id.g1tekst4);
                        i1.setImageResource(R.drawable.txt_ok);
                        i1 = (ImageView) findViewById(R.id.g1hist4);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1 = (EditText) findViewById(R.id.g4pin);
                        t1.setVisibility(View.INVISIBLE);
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 4) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta4);
                        i1.setImageResource(R.drawable.not_ok);
                        i1 = (ImageView) findViewById(R.id.g1tekst4);
                        i1.setImageResource(R.drawable.txt_not_ok);
                        i1 = (ImageView) findViewById(R.id.g1hist4);
                        i1.setImageBitmap(KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHistSzara());
                        i1.setVisibility(View.VISIBLE);
                        EditText t1 = (EditText) findViewById(R.id.g4pin);
                        t1.setVisibility(View.INVISIBLE);
                        t1.setText("");
                    }
                    if (KfbModel.getData().getGates().getmGate().get(msg.arg1).StanTab == 5) {
                        ImageView i1 = (ImageView) findViewById(R.id.g1sta4);
                        i1.setImageResource(R.drawable.lock);
                        i1 = (ImageView) findViewById(R.id.g1tekst4);
                        i1.setImageResource(R.drawable.txt_lock);
                        i1 = (ImageView) findViewById(R.id.g1hist4);
                        i1.setVisibility(View.INVISIBLE);
                        EditText t1 = (EditText) findViewById(R.id.g4pin);
                        t1.setVisibility(View.VISIBLE);
                    }
                }
            /*    DummyContent.DummyItem dr=null;
                for(int i = 0; i<ld1.size();i++)
                {
                    dr = ld1.get(i);
                    if(dr.id== msg.arg1)
                    {
                        break;
                    }
                }
                if(dr!=null) {
                    dr.Temperatura = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMaxT();
                    dr.State = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmStatus();
                    dr.Error = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmError();
                    dr.imgHis = KfbModel.getData().getGates().getmGate().get(msg.arg1).getmMeasure().getHist();



                    float t=0;
                    if (dr.Temperatura != null) {
                        t = Float.parseFloat(dr.Temperatura);
                    }
                    if (t > 38) {
                         dr.Nok = "NOK";
                    } else {
                        dr.Nok = "OK";
                    }
                    listAdapter.notifyDataSetChanged();
                }*/
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gate);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
        thisActivite = this;
        ImageButton bConf = (ImageButton)findViewById(R.id.idConfig);
        bConf.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                onConfigClik(v);
            }
        });

        recycler = findViewById(R.id.recview);
        recycler.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);

        recycler.setLayoutManager(layoutManager);



        listAdapter = new MyItemRecyclerViewAdapter( ld1,null);
        recycler.setAdapter(listAdapter);
        KfbModel.getData().context = getApplicationContext();
        DbManage dbRep = new DbManage(getApplicationContext());
        List<Urzadzenia> urzdb = dbRep.getUrzadzeniaAll();
            if(urzdb!=null) {
                KfbModel.getData().clear();
                for(int i=0;i<urzdb.size();i++){

                    KfbGate lGate = new KfbGate();
                    lGate.setId(urzdb.get(i).ID);
                    lGate.setmAdres(urzdb.get(i).adres);
                    if(i==0) {
                        lGate.setmName("Gate 1");
                    }
                    if(i==1) {
                        lGate.setmName("Gate 2");
                    }
                    if(i==2) {
                        lGate.setmName("Gate 3");
                    }
                    if(i==3) {
                        lGate.setmName("Gate 4");
                    }
                    if(i<4) {
                        lGate.setmActive(urzdb.get(i).active);
                        KfbModel.getData().getGates().AddGate(lGate);
                    }
                    else
                    {
                        lGate.setmActive(urzdb.get(i).active);
                        KfbModel.getData().getTabletss().AddTablet(lGate);
                    }
                }
            }
                if (KfbModel.getData().getGates().getmSize() == 0) {
                    KfbModel.getData().clear();
                    KfbModel.getData().sendHttp = true;
                    KfbGate lGate = new KfbGate();
                    lGate.setId(0);
                    lGate.setmAdres("http://192.168.1.15:6505");
                    // lGate.setmAdres("http://51.83.130.248:3000");
                    lGate.setmName("Gate 1");
                    lGate.setmActive(true);
                    KfbModel.getData().getGates().AddGate(lGate);
                    Urzadzenia urzdbe = new Urzadzenia();

                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);

                    lGate = new KfbGate();
                    lGate.setmAdres("http://192.168.1.16:6505");
                    //lGate.setmAdres("http://p.sorveno.com/pp.html");
                    lGate.setmName("Gate 2");
                    lGate.setId(1);
                    lGate.setmActive(true);
                    KfbModel.getData().getGates().AddGate(lGate);
                    urzdbe = new Urzadzenia();
                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);
                    lGate = new KfbGate();
                    lGate.setmAdres("http://192.168.1.17:6505");
                    //lGate.setmAdres("http://p.sorveno.com/pp.html");
                    lGate.setmName("Gate 3");
                    lGate.setId(2);
                    lGate.setmActive(true);
                    KfbModel.getData().getGates().AddGate(lGate);
                    urzdbe = new Urzadzenia();
                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);
                    lGate = new KfbGate();
                    lGate.setmAdres("http://192.168.1.18:6505");
                    //lGate.setmAdres("http://p.sorveno.com/pp.html");
                    lGate.setmName("Gate 4");
                    lGate.setId(3);
                    lGate.setmActive(true);
                    urzdbe = new Urzadzenia();
                    urzdbe.active = lGate.ismActive();
                    urzdbe.adres = lGate.getmAdres();
                    urzdbe.ID =  lGate.getId();
                    dbRep.insertUrzazenie(urzdbe);
                    KfbModel.getData().getGates().AddGate(lGate);
                }
            dbRep.close();
            ld1.clear();
            int activ=0;
            for (int i = 0; i < KfbModel.getData().getGates().getmSize(); i++) {

                DummyContent.DummyItem d1 = new DummyContent.DummyItem(i, KfbModel.getData().getGates().getmGate().get(i).getmName(), "ON", KfbModel.getData().getGates().getmGate().get(i).getmError(), KfbModel.getData().getGates().getmGate().get(i).getmStatus(), "", "", "", KfbModel.getData().getGates().getmGate().get(i).getmMaxT());
                 if( KfbModel.getData().getGates().getmGate().get(i).ismActive()) {
                     activ++;
                     ld1.add(d1);
                 }
            }
            switch (activ){
                case 1:
                    setContentView(R.layout.activity_gate1);
                    ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic1(v);
                        }
                    });
                    EditText t1= (EditText)findViewById(R.id.g1pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(0);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                   t1.start();
                                    KfbModel.getData().getGates().getmGate().get(0).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(0).getId();
                                    handler.sendMessage(msg);
                                }
                                else
                                {
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });


                    break;
                case 2:
                    setContentView(R.layout.activity_gate2);
                    ib1 = (ImageButton) findViewById(R.id.g1mic);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic1(v);
                        }
                    });
                    ib1 = (ImageButton) findViewById(R.id.g1mic2);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic2(v);
                        }
                    });
                     t1= (EditText)findViewById(R.id.g2pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(1);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(1).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(1).getId();
                                    handler.sendMessage(msg);
                                }else
                                {
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    t1= (EditText)findViewById(R.id.g1pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(0);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(0).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(0).getId();
                                    handler.sendMessage(msg);
                                }
                                else{
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    break;
                case 3:
                    setContentView(R.layout.activity_gate3);
                    ib1 = (ImageButton) findViewById(R.id.g1mic);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic1(v);
                        }
                    });
                    ib1 = (ImageButton) findViewById(R.id.g1mic2);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic2(v);
                        }
                    });
                    ib1 = (ImageButton) findViewById(R.id.g1mic3);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic3(v);
                        }
                    });
                    t1= (EditText)findViewById(R.id.g3pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(2);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(2).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(2).getId();
                                    handler.sendMessage(msg);
                                }
                                else
                                {
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    t1= (EditText)findViewById(R.id.g1pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(0);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(0).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(0).getId();
                                    handler.sendMessage(msg);
                                }
                                else{
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    t1= (EditText)findViewById(R.id.g2pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(1);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(1).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(1).getId();
                                    handler.sendMessage(msg);
                                }
                                else{
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    break;
                case 4:
                    setContentView(R.layout.activity_gate4);
                    ib1 = (ImageButton) findViewById(R.id.g1mic);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic1(v);
                        }
                    });
                    ib1 = (ImageButton) findViewById(R.id.g1mic2);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic2(v);
                        }
                    });
                    ib1 = (ImageButton) findViewById(R.id.g1mic3);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic3(v);
                        }
                    });
                    ib1 = (ImageButton) findViewById(R.id.g1mic4);
                    ib1.setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            onMic4(v);
                        }
                    });
                    t1= (EditText)findViewById(R.id.g4pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(3);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(3).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                }
                                else
                                {
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    t1= (EditText)findViewById(R.id.g3pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(2);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(2).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(2).getId();
                                    handler.sendMessage(msg);
                                }
                                else{
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    t1= (EditText)findViewById(R.id.g2pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(1);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(1).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(1).getId();
                                    handler.sendMessage(msg);
                                }
                                else{
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    t1= (EditText)findViewById(R.id.g1pin);

                    t1.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) { }
                        @Override
                        public void afterTextChanged(Editable s) {
                            if(s.toString().length()==4) {
                                if(s.toString().equals(KfbConstans.PINGATE))
                                {
                                    final KfbGate tab1= KfbModel.getData().getTabletss().getmGate().get(0);
                                    Thread t1 = new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            KfbHttpRequest.getTabletUnlock(tab1);
                                        }
                                    });
                                    t1.start();
                                    KfbModel.getData().getGates().getmGate().get(0).StanTab = 1;
                                    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    inputManager.hideSoftInputFromWindow(thisActivite.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
                                    Message msg = handler.obtainMessage();
                                    msg.what = 2;
                                    msg.obj = "Pomiar";
                                    msg.arg1 =  KfbModel.getData().getGates().getmGate().get(0).getId();
                                    handler.sendMessage(msg);
                                }
                                else{
                                    Toast.makeText(getApplicationContext(), "Błędny PIN ", Toast.LENGTH_SHORT).show();
                                }
                            }else{

                            }
                        }
                    });
                    break;
            }
            KfbModel.getData().handler = handler;
            KfbModel.getData().setmAdapter(listAdapter);
            this.StartMes();
          //  listAdapter.notifyDataSetChanged();
        try {
            server = new KfbHttp();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
private void onConfigClik(View v)
{
    Intent i = new Intent(getApplicationContext(),ConfigActivity.class);
    startActivity(i);
}
    private void StartMes(){
        KfbModel.getData().sendHttp= true;
        KfbTask tr = new KfbTask();
        Thread T1 = new Thread(tr);
        T1.start();
        KfbTaskTablet tr1 = new KfbTaskTablet();
        Thread T2 = new Thread(tr1);
        T2.start();
    }
    @Override
    protected void onStop() {
        super.onStop();
        KfbModel.getData().sendHttp=false;
    }
    private void onMic1(View v)
    {
        if(stan1Mic==0)
        {
            stan1Mic++;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic);
            ib1.setImageResource(R.drawable.mic_active);
            if(KfbModel.getData().getTabletss().getmGate().size()==0) {
                ib1.setImageResource(R.drawable.mic_off);
                stan1Mic=0;
                return;
            }
            KfbGate tab = KfbModel.getData().getTabletss().getmGate().get(0);
            if(tab==null)
            {
                ib1.setImageResource(R.drawable.mic_off);
                stan1Mic=0;
                return;
            }
            kfbinter.setTable(tab);
            if(stan4Mic==1 || stan2Mic==1 || stan3Mic==1)
            {
                kfbinter.stopStreaming();
            }
            if(KfbModel.getData().getGates().getmGate().get(0).StanTab >= 0) {
                kfbinter.startStreaming();
            }
            stan2Mic = 0;
            stan3Mic = 0;
            stan4Mic = 0;
            ib1 = (ImageButton) findViewById(R.id.g1mic2);
if(ib1!=null) {
    ib1.setImageResource(R.drawable.mic_off);
}
            ib1 = (ImageButton) findViewById(R.id.g1mic3);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
            ib1 = (ImageButton) findViewById(R.id.g1mic4);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
        }
        else
        {
            stan1Mic=0;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic);
            ib1.setImageResource(R.drawable.mic_off);
            if(KfbModel.getData().getGates().getmGate().get(0).StanTab >= 0) {
                kfbinter.stopStreaming();
            }
        }
    }
    private void onMic2(View v)
    {
        if(stan2Mic==0)
        {
            stan2Mic++;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic2);
            ib1.setImageResource(R.drawable.mic_active);
            if(KfbModel.getData().getTabletss().getmGate().size()==0) {
                ib1.setImageResource(R.drawable.mic_off);
                stan2Mic=0;
                return;
            }
            KfbGate tab = KfbModel.getData().getTabletss().getmGate().get(1);
            if(tab==null)
            {
                ib1.setImageResource(R.drawable.mic_off);
                stan2Mic=0;
                return;
            }
            kfbinter.setTable(tab);
            if(stan1Mic==1 || stan4Mic==1 || stan3Mic==1)
            {
                kfbinter.stopStreaming();
            }
            if(KfbModel.getData().getGates().getmGate().get(1).StanTab >= 0) {
                kfbinter.startStreaming();
            }
            stan1Mic = 0;
            stan3Mic = 0;
            stan4Mic = 0;
            ib1 = (ImageButton) findViewById(R.id.g1mic);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
            ib1 = (ImageButton) findViewById(R.id.g1mic3);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
            ib1 = (ImageButton) findViewById(R.id.g1mic4);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
        }
        else
        {
            stan2Mic=0;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic2);
            ib1.setImageResource(R.drawable.mic_off);
            if(KfbModel.getData().getGates().getmGate().get(1).StanTab >= 0) {
                kfbinter.stopStreaming();
            }
        }
    }
    private void onMic3(View v)
    {
        if(stan3Mic==0)
        {
            stan3Mic++;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic3);
            ib1.setImageResource(R.drawable.mic_active);
            if(KfbModel.getData().getTabletss().getmGate().size()==0) {
                ib1.setImageResource(R.drawable.mic_off);
                stan3Mic=0;
                return;
            }
            KfbGate tab = KfbModel.getData().getTabletss().getmGate().get(2);
            if(tab==null)
            {
                ib1.setImageResource(R.drawable.mic_off);
                stan3Mic=0;
                return;
            }
            kfbinter.setTable(KfbModel.getData().getTabletss().getmGate().get(2));
            if(stan1Mic==1 || stan2Mic==1 || stan4Mic==1)
            {
                kfbinter.stopStreaming();
            }
            if(KfbModel.getData().getGates().getmGate().get(2).StanTab >= 0) {
                kfbinter.startStreaming();
            }
            stan1Mic = 0;
            stan2Mic = 0;
            stan4Mic = 0;
            ib1 = (ImageButton) findViewById(R.id.g1mic);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
            ib1 = (ImageButton) findViewById(R.id.g1mic2);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
            ib1 = (ImageButton) findViewById(R.id.g1mic4);
            if(ib1!=null) {
                ib1.setImageResource(R.drawable.mic_off);
            }
        }
        else
        {
            stan3Mic=0;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic3);
            ib1.setImageResource(R.drawable.mic_off);
            if(KfbModel.getData().getGates().getmGate().get(2).StanTab >= 0) {
                kfbinter.stopStreaming();
            }
        }
    }
    private void onMic4(View v)
    {
        if(stan4Mic==0)
        {
            stan4Mic++;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic4);
            ib1.setImageResource(R.drawable.mic_active);
            if(KfbModel.getData().getTabletss().getmGate().size()==0) {
                ib1.setImageResource(R.drawable.mic_off);
                stan4Mic=0;
                return;
            }
            KfbGate tab = KfbModel.getData().getTabletss().getmGate().get(3);
            if(tab==null)
            {
                ib1.setImageResource(R.drawable.mic_off);
                stan4Mic=0;
                return;
            }
            kfbinter.setTable(KfbModel.getData().getTabletss().getmGate().get(3));
            if(stan1Mic==1 || stan2Mic==1 || stan3Mic==1)
            {
                kfbinter.stopStreaming();
            }
            if(KfbModel.getData().getGates().getmGate().get(3).StanTab >= 0) {
                kfbinter.startStreaming();
            }
        }
        else
        {
            stan4Mic=0;
            ImageButton ib1 = (ImageButton) findViewById(R.id.g1mic4);
            ib1.setImageResource(R.drawable.mic_off);
            if(KfbModel.getData().getGates().getmGate().get(3).StanTab >= 0) {
                kfbinter.stopStreaming();
            }
            stan1Mic = 0;
            stan2Mic = 0;
            stan3Mic = 0;
            ib1 = (ImageButton) findViewById(R.id.g1mic);
            ib1.setImageResource(R.drawable.mic_off);
            ib1 = (ImageButton) findViewById(R.id.g1mic2);
            ib1.setImageResource(R.drawable.mic_off);
            ib1 = (ImageButton) findViewById(R.id.g1mic3);
            ib1.setImageResource(R.drawable.mic_off);
        }
    }
}