Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.89 KB | None | 0 0
  1. package com.example.enesb.homesmarthomev2;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.graphics.PorterDuff;
  6. import android.os.Bundle;
  7. import android.support.design.widget.FloatingActionButton;
  8. import android.support.design.widget.Snackbar;
  9. import android.view.View;
  10. import android.support.design.widget.NavigationView;
  11. import android.support.v4.view.GravityCompat;
  12. import android.support.v4.widget.DrawerLayout;
  13. import android.support.v7.app.ActionBarDrawerToggle;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.support.v7.widget.Toolbar;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.widget.Button;
  19. import android.widget.SeekBar;
  20. import android.widget.Toast;
  21.  
  22. import org.eclipse.paho.android.service.MqttAndroidClient;
  23. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  24. import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
  25. import org.eclipse.paho.client.mqttv3.IMqttToken;
  26. import org.eclipse.paho.client.mqttv3.MqttCallback;
  27. import org.eclipse.paho.client.mqttv3.MqttClient;
  28. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  29. import org.eclipse.paho.client.mqttv3.MqttException;
  30. import org.eclipse.paho.client.mqttv3.MqttMessage;
  31. import org.eclipse.paho.client.mqttv3.util.Strings;
  32.  
  33. public class raum4 extends AppCompatActivity
  34. implements MqttCallback, NavigationView.OnNavigationItemSelectedListener {
  35.  
  36. SeekBar seek_helligkeit;
  37. SeekBar seek_rot, seek_gruen, seek_blau;
  38.  
  39. Button btn_helligkeit;
  40. int helligkeit = 0;
  41. int rot = 0;
  42. int gruen = 0;
  43. int blau = 0;
  44. int personen;
  45. int licht;
  46. int j;
  47.  
  48. static String MQTTHOST = "";
  49. static String USERNAME = "";
  50. static String PASSWORD = "";
  51. public String msg;
  52. public String topic1;
  53.  
  54. public MqttAndroidClient client;
  55.  
  56. public int QOS = 1;
  57. public boolean KA = false;
  58.  
  59. @Override
  60. protected void onCreate(Bundle savedInstanceState) {
  61. super.onCreate(savedInstanceState);
  62. setContentView(R.layout.activity_raum4);
  63.  
  64. connect();
  65.  
  66. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  67. setSupportActionBar(toolbar);
  68.  
  69. btn_helligkeit =(Button) findViewById(R.id.btn_helligkeit);
  70. seek_helligkeit = (SeekBar) findViewById(R.id.seek_helligkeit);
  71.  
  72. seek_rot = (SeekBar) findViewById(R.id.seek_rot);
  73. seek_gruen = (SeekBar) findViewById(R.id.seek_gruen);
  74. seek_blau = (SeekBar) findViewById(R.id.seek_blau);
  75.  
  76. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  77. fab.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View view) {
  80. if (licht == 1) {
  81. publish("Haus/Raum4/Licht", "0");
  82. Toast.makeText(raum4.this, "PENIS0", Toast.LENGTH_LONG).show();
  83. btn_helligkeit.getBackground().clearColorFilter();
  84. }
  85. if (licht == 0) {
  86. publish("Haus/Raum4/Licht", "1");
  87. Toast.makeText(raum4.this, "PENIS1", Toast.LENGTH_LONG).show();
  88. btn_helligkeit.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
  89. }
  90. }
  91. });
  92.  
  93. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  94. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  95. this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  96. drawer.setDrawerListener(toggle);
  97. toggle.syncState();
  98.  
  99. NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  100. navigationView.setNavigationItemSelectedListener(this);
  101.  
  102. seek_helligkeit.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  103. @Override
  104. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  105. if(b){
  106. j++;
  107. if (j == 2 ){
  108. publish("Haus/Raum5/Helligkeit", String.valueOf(i));
  109. helligkeit = i;
  110. updateLicht();
  111. j = 0;
  112. }
  113. }
  114. }
  115.  
  116. @Override
  117. public void onStartTrackingTouch(SeekBar seekBar) {
  118. }
  119.  
  120. @Override
  121. public void onStopTrackingTouch(SeekBar seekBar) {
  122. KA = true;
  123. publish("Haus/Raum4/Helligkeit", String.valueOf(seekBar.getProgress()));
  124. }
  125. });
  126.  
  127. seek_rot.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
  128. @Override
  129. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  130. rot = 0;
  131. if (b){
  132. publish("Haus/Raum4/RGB/R", String.valueOf(i));
  133. rot = i;
  134. }
  135. }
  136.  
  137. @Override
  138. public void onStartTrackingTouch(SeekBar seekBar) {
  139.  
  140. }
  141.  
  142. @Override
  143. public void onStopTrackingTouch(SeekBar seekBar) {
  144. KA = true;
  145. publish("Haus/Raum4/RGB/R", String.valueOf(seekBar.getProgress()));
  146. }
  147. });
  148.  
  149. seek_gruen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
  150. @Override
  151. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  152. if (b){
  153. publish("Haus/Raum4/RGB/G", String.valueOf(i));
  154. gruen = i;
  155. }
  156. }
  157.  
  158. @Override
  159. public void onStartTrackingTouch(SeekBar seekBar) {
  160.  
  161. }
  162.  
  163. @Override
  164. public void onStopTrackingTouch(SeekBar seekBar) {
  165. KA = true;
  166. publish("Haus/Raum4/RGB/G", String.valueOf(seekBar.getProgress()));
  167. }
  168. });
  169.  
  170. seek_blau.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
  171. @Override
  172. public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
  173. if (b){
  174. publish("Haus/Raum4/RGB/B", String.valueOf(i));
  175. blau = i;
  176. }
  177. }
  178.  
  179. @Override
  180. public void onStartTrackingTouch(SeekBar seekBar) {
  181.  
  182. }
  183.  
  184. @Override
  185. public void onStopTrackingTouch(SeekBar seekBar) {
  186. KA = true;
  187. publish("Haus/Raum4/RGB/B", String.valueOf(seekBar.getProgress()));
  188. }
  189. });
  190. }
  191.  
  192. @Override
  193. public void onBackPressed() {
  194. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  195. if (drawer.isDrawerOpen(GravityCompat.START)) {
  196. drawer.closeDrawer(GravityCompat.START);
  197. } else {
  198. super.onBackPressed();
  199. }
  200. }
  201.  
  202. @Override
  203. public boolean onCreateOptionsMenu(Menu menu) {
  204. // Inflate the menu; this adds items to the action bar if it is present.
  205. getMenuInflater().inflate(R.menu.raum4, menu);
  206. return true;
  207. }
  208.  
  209. @Override
  210. public boolean onOptionsItemSelected(MenuItem item) {
  211. // Handle action bar item clicks here. The action bar will
  212. // automatically handle clicks on the Home/Up button, so long
  213. // as you specify a parent activity in AndroidManifest.xml.
  214. int id = item.getItemId();
  215.  
  216. //noinspection SimplifiableIfStatement
  217. if (id == R.id.action_settings) {
  218. return true;
  219. }
  220.  
  221. return super.onOptionsItemSelected(item);
  222. }
  223.  
  224. @SuppressWarnings("StatementWithEmptyBody")
  225. @Override
  226. public boolean onNavigationItemSelected(MenuItem item) {
  227. // Handle navigation view item clicks here.
  228. int id = item.getItemId();
  229.  
  230. if (id == R.id.home) {
  231. Intent wechsel_home = new Intent(this, MainActivity.class);
  232. try {
  233. client.disconnect();
  234. } catch (MqttException e) {
  235. e.printStackTrace();
  236. }
  237. startActivity(wechsel_home);
  238. }
  239.  
  240. else if (id == R.id.raum1) {
  241. Intent wechsel_r1 = new Intent (this, raum1.class);
  242. startActivity(wechsel_r1);
  243. try {
  244. client.disconnect();
  245. } catch (MqttException e) {
  246. e.printStackTrace();
  247. }
  248. }
  249.  
  250. else if (id == R.id.raum2) {
  251. Intent wechsel_r2 = new Intent (this, raum2.class);
  252. //wechsel_r2.putExtra("", client);
  253. try {
  254. client.disconnect();
  255. } catch (MqttException e) {
  256. e.printStackTrace();
  257. }
  258. startActivity(wechsel_r2);
  259. }
  260.  
  261. else if (id == R.id.raum3) {
  262. Intent wechsel_r3 = new Intent (this, raum3.class);
  263. try {
  264. client.disconnect();
  265. } catch (MqttException e) {
  266. e.printStackTrace();
  267. }
  268. startActivity(wechsel_r3);
  269. }
  270.  
  271. else if (id == R.id.raum4) {
  272.  
  273. }
  274.  
  275. else if (id == R.id.raum5) {
  276. Intent wechsel_r4 = new Intent(this, raum4.class);
  277. try {
  278. client.disconnect();
  279. } catch (MqttException e) {
  280. e.printStackTrace();
  281. }
  282. startActivity(wechsel_r4);
  283. }
  284.  
  285. DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  286. drawer.closeDrawer(GravityCompat.START);
  287. return true;
  288. }
  289.  
  290. public void updateLicht(){
  291. //tw1.setText("In diesem Raum befinden sich " + personen + " Personen");
  292. if (licht == 1){
  293. btn_helligkeit.getBackground().setColorFilter(0xffffff00, PorterDuff.Mode.MULTIPLY);
  294. }
  295. else if (licht == 0){
  296. btn_helligkeit.getBackground().clearColorFilter();
  297.  
  298. }
  299. }
  300.  
  301. public void connect(){
  302. String clientId = MqttClient.generateClientId();
  303. client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
  304.  
  305. MqttConnectOptions options = new MqttConnectOptions();
  306. options.setUserName(USERNAME);
  307. options.setPassword(PASSWORD.toCharArray());
  308.  
  309. try {
  310. IMqttToken token = client.connect(options);
  311. token.setActionCallback(new IMqttActionListener() {
  312. @Override
  313. public void onSuccess(IMqttToken asyncActionToken) {
  314. Toast.makeText(raum4.this, "connected", Toast.LENGTH_LONG).show();
  315. subscribe();
  316. }
  317.  
  318. @Override
  319. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  320. // Something went wrong e.g. connection timeout or firewall problems
  321. Toast.makeText(raum4.this, "connection failed", Toast.LENGTH_LONG).show();
  322. }
  323. });
  324. } catch (MqttException e) {
  325. e.printStackTrace();
  326. }
  327. }
  328.  
  329. public void publish(String topicStr, String message){
  330. //String topic = "foo/bar";
  331. //String message = "Hallo vom Handy";
  332. byte[] encodedPayload = new byte[0];
  333. try {
  334. client.publish(topicStr, message.getBytes(), QOS, KA);
  335. } catch (MqttException e) {
  336. e.printStackTrace();
  337. }
  338. }
  339.  
  340. public void subscribe(){
  341. client.setCallback(raum4.this);
  342. final String topic = "Haus/Raum4/#";
  343. int qos = 1;
  344. try {
  345. IMqttToken subToken = client.subscribe(String.valueOf(topic), qos);
  346. subToken.setActionCallback(new IMqttActionListener() {
  347. @Override
  348. public void onSuccess(IMqttToken asyncActionToken) {
  349. // The message was published
  350. Toast.makeText(raum4.this, "Successfully subscribed to: " + topic, Toast.LENGTH_SHORT).show();
  351. }
  352.  
  353. @Override
  354. public void onFailure(IMqttToken asyncActionToken,
  355. Throwable exception) {
  356. // The subscription could not be performed, maybe the user was not
  357. // authorized to subscribe on the specified topic e.g. using wildcards
  358. Toast.makeText(raum4.this, "Couldn't subscribe to: " + topic, Toast.LENGTH_SHORT).show();
  359. }
  360. });
  361. } catch (MqttException e) {
  362. e.printStackTrace();
  363. }
  364. }
  365.  
  366. @Override
  367. public void connectionLost(Throwable cause) {
  368.  
  369. }
  370.  
  371. @Override
  372. public void messageArrived(String topic, MqttMessage message) throws Exception {
  373. //Toast.makeText(raum1.this, "Topic: "+topic+"\nMessage: "+message, Toast.LENGTH_LONG).show();
  374. msg = message.toString();
  375. topic1 = topic.toString();
  376. if (topic1.equals("Haus/Raum4/Licht")){
  377. licht = Integer.parseInt(msg);
  378. updateLicht();
  379. }
  380. else if (topic1.equals("Haus/Raum4/Helligkeit")){
  381. int ö;
  382. ö = Integer.parseInt(msg);
  383. seek_helligkeit.setProgress(ö);
  384. }
  385. else if (topic1.equals("Haus/Raum4/RGB/R")){
  386. int ü;
  387. ü = Integer.parseInt(msg);
  388. seek_rot.setProgress(ü);
  389. }
  390. else if (topic1.equals("Haus/Raum4/RGB/G")){
  391. int p;
  392. p = Integer.parseInt(msg);
  393. seek_gruen.setProgress(p);
  394. }
  395. else if (topic1.equals("Haus/Raum4/RGB/B")){
  396. int ä;
  397. ä = Integer.parseInt(msg);
  398. seek_blau.setProgress(ä);
  399. }
  400. updateLicht();
  401. }
  402.  
  403. @Override
  404. public void deliveryComplete(IMqttDeliveryToken token) {
  405.  
  406. }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement