Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.labb2.iotlab2;
- import android.os.AsyncTask;
- import android.os.StrictMode;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.CompoundButton;
- import android.widget.Switch;
- import android.widget.TextView;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import javax.xml.transform.Result;
- import ch.ethz.ssh2.Connection;
- import ch.ethz.ssh2.Session;
- import ch.ethz.ssh2.StreamGobbler;
- public class MainActivity extends AppCompatActivity {
- TextView txv_temp_indoor = null;
- Switch btnToggle = null;
- public StringBuilder run (String command) {
- String hostname = "130.237.177.212";
- String username = "pi";
- String password = "raspberry";
- StringBuilder out = new StringBuilder();
- try {
- StrictMode.ThreadPolicy policy = new
- StrictMode.ThreadPolicy.Builder()
- .permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection conn = new Connection(hostname); //init connection
- conn.connect(); //start connection to the hostname
- boolean isAuthenticated = conn.authenticateWithPassword(username, password);
- if (isAuthenticated == false) {
- throw new IOException("Authentication failed.");
- }
- Session sess = conn.openSession();
- sess.execCommand(command);
- InputStream stdout = new StreamGobbler(sess.getStdout());
- BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); //reads text
- while (true) {
- String line = br.readLine(); // read line
- if (line == null)
- break;
- System.out.println(line);
- out.append(line);
- } /* Show exit status, if available (otherwise "null") */
- System.out.println("ExitCode: " + sess.getExitStatus());
- sess.close(); // Close this session
- conn.close();
- }
- catch (IOException e)
- { e.printStackTrace(System.err);
- System.exit(2); }
- return out;
- //out.toString().split("\n");
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txv_temp_indoor = (TextView) findViewById(R.id.indoorTempShow);
- new AsyncTask<Integer, Void, String>(){
- @Override
- protected String doInBackground(Integer... params) {
- //your code to fetch results via SSH
- System.out.println(run("tdtool -l").toString());
- String[] temp = run("tdtool -l").toString().split("\\t");
- System.out.println(temp[1]);
- //String[] result = temp[0].split("\\t");
- return temp[12];
- }
- @Override
- protected void onPostExecute(String result) {
- txv_temp_indoor.setText(result);
- }
- }.execute(1);
- txv_temp_indoor.setText("the fetched indoor temp value");
- btnToggle = (Switch) findViewById(R.id.btnToggle);
- btnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (isChecked) {
- // Set actuator on
- new AsyncTask<Integer, Void, String>(){
- String result;
- @Override
- protected String doInBackground(Integer... params) {
- // code here
- run("tdtool --on 1");
- return result;
- }
- @Override
- protected void onPostExecute(String result) {
- }
- }.execute(1);
- }
- else {
- // Set actuator off
- new AsyncTask<Integer, Void, String>(){
- String result;
- @Override
- protected String doInBackground(Integer... params) {
- //your code to fetch results via SSH
- run("tdtool --off 1");
- return result;
- }
- @Override
- protected void onPostExecute(String result) {
- }
- }.execute(1);
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement