Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.enesb.homesmarthomev2;
- import android.content.Intent;
- import android.graphics.Color;
- import android.graphics.PorterDuff;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.view.View;
- import android.support.design.widget.NavigationView;
- import android.support.v4.view.GravityCompat;
- import android.support.v4.widget.DrawerLayout;
- import android.support.v7.app.ActionBarDrawerToggle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.Toast;
- import org.eclipse.paho.android.service.MqttAndroidClient;
- import org.eclipse.paho.client.mqttv3.IMqttActionListener;
- import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.IMqttToken;
- import org.eclipse.paho.client.mqttv3.MqttCallback;
- import org.eclipse.paho.client.mqttv3.MqttClient;
- import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
- import org.eclipse.paho.client.mqttv3.MqttException;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- import org.eclipse.paho.client.mqttv3.util.Strings;
- public class raum4 extends AppCompatActivity
- implements MqttCallback, NavigationView.OnNavigationItemSelectedListener {
- SeekBar seek_helligkeit;
- SeekBar seek_rot, seek_gruen, seek_blau;
- Button btn_helligkeit;
- int helligkeit = 0;
- int rot = 0;
- int gruen = 0;
- int blau = 0;
- int personen;
- int licht;
- int j;
- static String MQTTHOST = "";
- static String USERNAME = "";
- static String PASSWORD = "";
- public String msg;
- public String topic1;
- public MqttAndroidClient client;
- public int QOS = 1;
- public boolean KA = false;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_raum4);
- connect();
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- btn_helligkeit =(Button) findViewById(R.id.btn_helligkeit);
- seek_helligkeit = (SeekBar) findViewById(R.id.seek_helligkeit);
- seek_rot = (SeekBar) findViewById(R.id.seek_rot);
- seek_gruen = (SeekBar) findViewById(R.id.seek_gruen);
- seek_blau = (SeekBar) findViewById(R.id.seek_blau);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (licht == 1) {
- publish("Haus/Raum4/Licht", "0");
- Toast.makeText(raum4.this, "PENIS0", Toast.LENGTH_LONG).show();
- btn_helligkeit.getBackground().clearColorFilter();
- }
- if (licht == 0) {
- publish("Haus/Raum4/Licht", "1");
- Toast.makeText(raum4.this, "PENIS1", Toast.LENGTH_LONG).show();
- btn_helligkeit.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
- }
- }
- });
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
- drawer.setDrawerListener(toggle);
- toggle.syncState();
- NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
- navigationView.setNavigationItemSelectedListener(this);
- seek_helligkeit.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
- if(b){
- j++;
- if (j == 2 ){
- publish("Haus/Raum5/Helligkeit", String.valueOf(i));
- helligkeit = i;
- updateLicht();
- j = 0;
- }
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- KA = true;
- publish("Haus/Raum4/Helligkeit", String.valueOf(seekBar.getProgress()));
- }
- });
- seek_rot.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
- @Override
- public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
- rot = 0;
- if (b){
- publish("Haus/Raum4/RGB/R", String.valueOf(i));
- rot = i;
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- KA = true;
- publish("Haus/Raum4/RGB/R", String.valueOf(seekBar.getProgress()));
- }
- });
- seek_gruen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
- @Override
- public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
- if (b){
- publish("Haus/Raum4/RGB/G", String.valueOf(i));
- gruen = i;
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- KA = true;
- publish("Haus/Raum4/RGB/G", String.valueOf(seekBar.getProgress()));
- }
- });
- seek_blau.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
- @Override
- public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
- if (b){
- publish("Haus/Raum4/RGB/B", String.valueOf(i));
- blau = i;
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- KA = true;
- publish("Haus/Raum4/RGB/B", String.valueOf(seekBar.getProgress()));
- }
- });
- }
- @Override
- public void onBackPressed() {
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- if (drawer.isDrawerOpen(GravityCompat.START)) {
- drawer.closeDrawer(GravityCompat.START);
- } else {
- super.onBackPressed();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.raum4, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- @SuppressWarnings("StatementWithEmptyBody")
- @Override
- public boolean onNavigationItemSelected(MenuItem item) {
- // Handle navigation view item clicks here.
- int id = item.getItemId();
- if (id == R.id.home) {
- Intent wechsel_home = new Intent(this, MainActivity.class);
- try {
- client.disconnect();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- startActivity(wechsel_home);
- }
- else if (id == R.id.raum1) {
- Intent wechsel_r1 = new Intent (this, raum1.class);
- startActivity(wechsel_r1);
- try {
- client.disconnect();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- else if (id == R.id.raum2) {
- Intent wechsel_r2 = new Intent (this, raum2.class);
- //wechsel_r2.putExtra("", client);
- try {
- client.disconnect();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- startActivity(wechsel_r2);
- }
- else if (id == R.id.raum3) {
- Intent wechsel_r3 = new Intent (this, raum3.class);
- try {
- client.disconnect();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- startActivity(wechsel_r3);
- }
- else if (id == R.id.raum4) {
- }
- else if (id == R.id.raum5) {
- Intent wechsel_r4 = new Intent(this, raum4.class);
- try {
- client.disconnect();
- } catch (MqttException e) {
- e.printStackTrace();
- }
- startActivity(wechsel_r4);
- }
- DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
- drawer.closeDrawer(GravityCompat.START);
- return true;
- }
- public void updateLicht(){
- //tw1.setText("In diesem Raum befinden sich " + personen + " Personen");
- if (licht == 1){
- btn_helligkeit.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
- }
- else if (licht == 0){
- btn_helligkeit.getBackground().clearColorFilter();
- }
- }
- public void connect(){
- String clientId = MqttClient.generateClientId();
- client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
- MqttConnectOptions options = new MqttConnectOptions();
- options.setUserName(USERNAME);
- options.setPassword(PASSWORD.toCharArray());
- try {
- IMqttToken token = client.connect(options);
- token.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- Toast.makeText(raum4.this, "connected", Toast.LENGTH_LONG).show();
- subscribe();
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- // Something went wrong e.g. connection timeout or firewall problems
- Toast.makeText(raum4.this, "connection failed", Toast.LENGTH_LONG).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void publish(String topicStr, String message){
- //String topic = "foo/bar";
- //String message = "Hallo vom Handy";
- byte[] encodedPayload = new byte[0];
- try {
- client.publish(topicStr, message.getBytes(), QOS, KA);
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- public void subscribe(){
- client.setCallback(raum4.this);
- final String topic = "Haus/Raum4/#";
- int qos = 1;
- try {
- IMqttToken subToken = client.subscribe(String.valueOf(topic), qos);
- subToken.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- // The message was published
- Toast.makeText(raum4.this, "Successfully subscribed to: " + topic, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken,
- Throwable exception) {
- // The subscription could not be performed, maybe the user was not
- // authorized to subscribe on the specified topic e.g. using wildcards
- Toast.makeText(raum4.this, "Couldn't subscribe to: " + topic, Toast.LENGTH_SHORT).show();
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void connectionLost(Throwable cause) {
- }
- @Override
- public void messageArrived(String topic, MqttMessage message) throws Exception {
- //Toast.makeText(raum1.this, "Topic: "+topic+"\nMessage: "+message, Toast.LENGTH_LONG).show();
- msg = message.toString();
- topic1 = topic.toString();
- if (topic1.equals("Haus/Raum4/Licht")){
- licht = Integer.parseInt(msg);
- updateLicht();
- }
- else if (topic1.equals("Haus/Raum4/Helligkeit")){
- int ö;
- ö = Integer.parseInt(msg);
- seek_helligkeit.setProgress(ö);
- }
- else if (topic1.equals("Haus/Raum4/RGB/R")){
- int ü;
- ü = Integer.parseInt(msg);
- seek_rot.setProgress(ü);
- }
- else if (topic1.equals("Haus/Raum4/RGB/G")){
- int p;
- p = Integer.parseInt(msg);
- seek_gruen.setProgress(p);
- }
- else if (topic1.equals("Haus/Raum4/RGB/B")){
- int ä;
- ä = Integer.parseInt(msg);
- seek_blau.setProgress(ä);
- }
- updateLicht();
- }
- @Override
- public void deliveryComplete(IMqttDeliveryToken token) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement