Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. package com.example.labb2.iotlab2;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.StrictMode;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.widget.CompoundButton;
  8. import android.widget.Switch;
  9. import android.widget.TextView;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15.  
  16. import javax.xml.transform.Result;
  17.  
  18. import ch.ethz.ssh2.Connection;
  19. import ch.ethz.ssh2.Session;
  20. import ch.ethz.ssh2.StreamGobbler;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.  
  24. TextView txv_temp_indoor = null;
  25. Switch btnToggle = null;
  26.  
  27.  
  28. public StringBuilder run (String command) {
  29. String hostname = "130.237.177.212";
  30. String username = "pi";
  31. String password = "raspberry";
  32.  
  33. StringBuilder out = new StringBuilder();
  34.  
  35. try {
  36. StrictMode.ThreadPolicy policy = new
  37. StrictMode.ThreadPolicy.Builder()
  38. .permitAll().build();
  39. StrictMode.setThreadPolicy(policy);
  40. Connection conn = new Connection(hostname); //init connection
  41. conn.connect(); //start connection to the hostname
  42. boolean isAuthenticated = conn.authenticateWithPassword(username, password);
  43. if (isAuthenticated == false) {
  44. throw new IOException("Authentication failed.");
  45. }
  46. Session sess = conn.openSession();
  47. sess.execCommand(command);
  48. InputStream stdout = new StreamGobbler(sess.getStdout());
  49. BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); //reads text
  50. while (true) {
  51. String line = br.readLine(); // read line
  52. if (line == null)
  53. break;
  54. System.out.println(line);
  55. out.append(line);
  56. } /* Show exit status, if available (otherwise "null") */
  57. System.out.println("ExitCode: " + sess.getExitStatus());
  58. sess.close(); // Close this session
  59. conn.close();
  60.  
  61. }
  62. catch (IOException e)
  63. { e.printStackTrace(System.err);
  64. System.exit(2); }
  65.  
  66. return out;
  67. //out.toString().split("\n");
  68.  
  69. }
  70.  
  71. @Override
  72. protected void onCreate(Bundle savedInstanceState) {
  73. super.onCreate(savedInstanceState);
  74. setContentView(R.layout.activity_main);
  75.  
  76. txv_temp_indoor = (TextView) findViewById(R.id.indoorTempShow);
  77.  
  78. new AsyncTask<Integer, Void, String>(){
  79. @Override
  80. protected String doInBackground(Integer... params) {
  81.  
  82. //your code to fetch results via SSH
  83. System.out.println(run("tdtool -l").toString());
  84. String[] temp = run("tdtool -l").toString().split("\\t");
  85. System.out.println(temp[1]);
  86. //String[] result = temp[0].split("\\t");
  87.  
  88. return temp[12];
  89. }
  90. @Override
  91. protected void onPostExecute(String result) {
  92. txv_temp_indoor.setText(result);
  93. }
  94. }.execute(1);
  95.  
  96. txv_temp_indoor.setText("the fetched indoor temp value");
  97.  
  98. btnToggle = (Switch) findViewById(R.id.btnToggle);
  99.  
  100. btnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  101. @Override
  102. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  103. if (isChecked) {
  104. // Set actuator on
  105. new AsyncTask<Integer, Void, String>(){
  106. String result;
  107. @Override
  108. protected String doInBackground(Integer... params) {
  109. // code here
  110. run("tdtool --on 1");
  111.  
  112. return result;
  113. }
  114. @Override
  115. protected void onPostExecute(String result) {
  116.  
  117. }
  118. }.execute(1);
  119. }
  120. else {
  121. // Set actuator off
  122. new AsyncTask<Integer, Void, String>(){
  123. String result;
  124. @Override
  125. protected String doInBackground(Integer... params) {
  126. //your code to fetch results via SSH
  127. run("tdtool --off 1");
  128.  
  129. return result;
  130. }
  131. @Override
  132. protected void onPostExecute(String result) {
  133. }
  134. }.execute(1);
  135. }
  136.  
  137. }
  138. });
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement