Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mqttsubscribejava;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.CompoundButton;
- import android.widget.Switch;
- import com.r0adkll.slidr.Slidr;
- import com.r0adkll.slidr.model.SlidrInterface;
- import org.eclipse.paho.android.service.MqttAndroidClient;
- import org.eclipse.paho.client.mqttv3.IMqttActionListener;
- import org.eclipse.paho.client.mqttv3.IMqttToken;
- 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 java.io.UnsupportedEncodingException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.concurrent.TimeUnit;
- public class Settings extends AppCompatActivity {
- String clientId = MqttClient.generateClientId();
- public MqttAndroidClient client;
- private static final String DB_URL = "jdbc:mysql://##/mydb";
- private static final String USER = "##";
- private static final String PASS = "##d";
- public int stanje;
- public String nastavitev;
- private Button btn;
- private Switch switchgibanje;
- private Switch switchtemperature;
- public ArrayList<String> names = new ArrayList<String>();
- public String[] result = new String[names.size()];
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- switchgibanje = (Switch) findViewById(R.id.switchGibanje) ;
- switchtemperature = (Switch) findViewById(R.id.switchTemp) ;
- btn = (Button) findViewById(R.id.button);
- onLoadActivity mysql = new onLoadActivity();
- mysql.execute("");
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- for (int row = 0; row <= result.length / 2; row+=2) {
- //System.out.println(result[row] + result [row+1]);
- Log.d("setting", result[row] + " " + result [row +1]);
- // ViewGroup layout = (ViewGroup)findViewById(R.id.layout);
- // for (int i = 0; i < layout.getChildCount(); i++) {
- //
- // View child = layout.getChildAt(i);
- // if(child instanceof Switch)
- // {
- // Switch aSwitch = (Switch) child;
- // String id = aSwitch.getTag().toString();
- // //Log.d("Switch: ",aSwitch.getTag().toString());
- //
- // boolean hide = aSwitch.getTag() == result[row] && result[row+1] == "1";
- //
- //
- // if (hide == true) {
- // Log.d("Switch: ",aSwitch.getTag().toString() + "bi moral biti ON");
- // aSwitch.setChecked(true);
- //
- // }
- //
- //
- // }
- // }
- }
- }
- });
- client =
- new MqttAndroidClient(this.getApplicationContext(), "##",
- clientId);
- try {
- MqttConnectOptions options = new MqttConnectOptions();
- options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
- //IMqttToken token = client.connect(options);
- String topic = "zunajtemp";
- byte[] payload = "some payload".getBytes();
- options.setWill(topic, payload ,1,false);
- options.setUserName("zigajamnik");
- options.setPassword("cdmarker2266".toCharArray());
- IMqttToken token = client.connect(options);
- token.setActionCallback(new IMqttActionListener() {
- @Override
- public void onSuccess(IMqttToken asyncActionToken) {
- // We are connected
- Log.d("povezava", "Povezano!");
- String topic = "SETTINGS";
- }
- @Override
- public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
- // Something went wrong e.g. connection timeout or firewall problems
- //Log.d(TAG, "onFailure");
- Log.d("povezava", "Ni Povezano!");
- }
- });
- } catch (MqttException e) {
- e.printStackTrace();
- }
- switchgibanje.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked == true ){
- stanje = 1;
- nastavitev = "gibanje";
- Send objSend = new Send();
- objSend.execute("");
- //newSetting("settings", "1");
- }
- else {
- stanje = 0;
- nastavitev = "gibanje";
- Send objSend = new Send();
- objSend.execute("");
- //newSetting("settings", "1");
- }
- }
- });
- switchtemperature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked == true ){
- stanje = 1;
- nastavitev = "temperatura";
- Send objSend = new Send();
- objSend.execute("");
- //newSetting("settings", "1");
- }
- else {
- stanje = 0;
- nastavitev = "temperatura";
- Send objSend = new Send();
- objSend.execute("");
- //newSetting("settings", "1");
- }
- }
- });
- }
- public void newSetting (String topic, String payload) {
- byte[] encodedPayload = new byte[0];
- try {
- encodedPayload = payload.getBytes("UTF-8");
- MqttMessage message = new MqttMessage(encodedPayload);
- client.publish(topic, message);
- } catch (UnsupportedEncodingException | MqttException e) {
- e.printStackTrace();
- }
- }
- private class Send extends AsyncTask<String, String, String> {
- String msg = "";
- @Override
- protected void onPreExecute() {
- //txtv.setText("Please wait Inserting Data");
- }
- @Override
- protected String doInBackground(String... strings) {
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
- if(conn == null){
- //msg = "Connection goes wrong";
- }
- else {
- String query = "UPDATE settings SET value='"+stanje+"' WHERE setting = '"+nastavitev+"'";
- Statement stmt = conn.createStatement();
- stmt.executeUpdate(query);
- //msg = "Uspešno vnešeno!";
- }
- conn.close();
- newSetting("settings", "1");
- }
- catch (Exception e){
- //msg = "Connection goes wrong";
- e.printStackTrace();
- }
- return msg;
- }
- }
- public class onLoadActivity extends AsyncTask<String, String, String> {
- String msg = "";
- @Override
- protected String doInBackground(String... strings) {
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
- if(conn == null){
- }
- else {
- String query = "SELECT setting, value FROM settings";
- Statement stmt = conn.createStatement();
- stmt.executeQuery(query);
- ResultSet rs = stmt.executeQuery(query);
- while (rs.next()){
- names.add(rs.getString(1));
- names.add(rs.getString(2));
- }
- //String[] result = new String[names.size()];
- //result = names.toArray(result);
- //for (int row = 0; row <= result.length / 2; row+=2) {
- //
- //
- //System.out.println(result[row] +" " + result [row+1]);
- //
- // ViewGroup layout = (ViewGroup)findViewById(R.id.layout);
- // for (int i = 0; i < layout.getChildCount(); i++) {
- //
- // View child = layout.getChildAt(i);
- // if(child instanceof Switch)
- // {
- // Switch aSwitch = (Switch) child;
- // String id = aSwitch.getTag().toString();
- // //Log.d("Switch: ",aSwitch.getTag().toString());
- //
- // boolean hide = aSwitch.getTag() == result[row] && result[row+1] == "1";
- //
- //
- // if (hide == true) {
- // Log.d("Switch: ",aSwitch.getTag().toString() + "bi moral biti ON");
- // aSwitch.setChecked(true);
- //
- // }
- //
- // //new Handler().postDelayed(new Runnable() {
- // // @Override
- // // public void run() {
- // // aSwitch.setChecked(hide);
- // // }
- // //}, 1000);
- // }
- // }
- //}
- }
- conn.close();
- }
- catch (Exception e){
- e.printStackTrace();
- }
- return msg;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement