Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 KB | None | 0 0
  1. package com.example.mqttsubscribejava;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.CompoundButton;
  13. import android.widget.Switch;
  14.  
  15. import com.r0adkll.slidr.Slidr;
  16. import com.r0adkll.slidr.model.SlidrInterface;
  17.  
  18. import org.eclipse.paho.android.service.MqttAndroidClient;
  19. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  20. import org.eclipse.paho.client.mqttv3.IMqttToken;
  21. import org.eclipse.paho.client.mqttv3.MqttClient;
  22. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  23. import org.eclipse.paho.client.mqttv3.MqttException;
  24. import org.eclipse.paho.client.mqttv3.MqttMessage;
  25.  
  26. import java.io.UnsupportedEncodingException;
  27. import java.sql.Connection;
  28. import java.sql.DriverManager;
  29. import java.sql.ResultSet;
  30. import java.sql.Statement;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.concurrent.TimeUnit;
  34.  
  35. public class Settings extends AppCompatActivity {
  36.  
  37. String clientId = MqttClient.generateClientId();
  38. public MqttAndroidClient client;
  39.  
  40. private static final String DB_URL = "jdbc:mysql://##/mydb";
  41. private static final String USER = "##";
  42. private static final String PASS = "##d";
  43.  
  44. public int stanje;
  45. public String nastavitev;
  46.  
  47. private Button btn;
  48. private Switch switchgibanje;
  49. private Switch switchtemperature;
  50.  
  51. public ArrayList<String> names = new ArrayList<String>();
  52. public String[] result = new String[names.size()];
  53.  
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_main2);
  58.  
  59. switchgibanje = (Switch) findViewById(R.id.switchGibanje) ;
  60. switchtemperature = (Switch) findViewById(R.id.switchTemp) ;
  61. btn = (Button) findViewById(R.id.button);
  62.  
  63.  
  64. onLoadActivity mysql = new onLoadActivity();
  65. mysql.execute("");
  66.  
  67.  
  68.  
  69.  
  70. btn.setOnClickListener(new View.OnClickListener() {
  71.  
  72. @Override
  73. public void onClick(View v) {
  74.  
  75.  
  76.  
  77.  
  78.  
  79. for (int row = 0; row <= result.length / 2; row+=2) {
  80.  
  81. //System.out.println(result[row] + result [row+1]);
  82. Log.d("setting", result[row] + " " + result [row +1]);
  83.  
  84.  
  85. // ViewGroup layout = (ViewGroup)findViewById(R.id.layout);
  86. // for (int i = 0; i < layout.getChildCount(); i++) {
  87. //
  88. // View child = layout.getChildAt(i);
  89. // if(child instanceof Switch)
  90. // {
  91. // Switch aSwitch = (Switch) child;
  92. // String id = aSwitch.getTag().toString();
  93. // //Log.d("Switch: ",aSwitch.getTag().toString());
  94. //
  95. // boolean hide = aSwitch.getTag() == result[row] && result[row+1] == "1";
  96. //
  97. //
  98. // if (hide == true) {
  99. // Log.d("Switch: ",aSwitch.getTag().toString() + "bi moral biti ON");
  100. // aSwitch.setChecked(true);
  101. //
  102. // }
  103. //
  104. //
  105. // }
  106. // }
  107. }
  108.  
  109.  
  110. }
  111. });
  112.  
  113.  
  114.  
  115.  
  116. client =
  117. new MqttAndroidClient(this.getApplicationContext(), "##",
  118. clientId);
  119.  
  120. try {
  121. MqttConnectOptions options = new MqttConnectOptions();
  122. options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
  123. //IMqttToken token = client.connect(options);
  124.  
  125. String topic = "zunajtemp";
  126. byte[] payload = "some payload".getBytes();
  127. options.setWill(topic, payload ,1,false);
  128.  
  129.  
  130.  
  131. options.setUserName("zigajamnik");
  132. options.setPassword("cdmarker2266".toCharArray());
  133.  
  134. IMqttToken token = client.connect(options);
  135. token.setActionCallback(new IMqttActionListener() {
  136. @Override
  137. public void onSuccess(IMqttToken asyncActionToken) {
  138. // We are connected
  139. Log.d("povezava", "Povezano!");
  140.  
  141.  
  142.  
  143. String topic = "SETTINGS";
  144.  
  145. }
  146.  
  147. @Override
  148. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  149. // Something went wrong e.g. connection timeout or firewall problems
  150. //Log.d(TAG, "onFailure");
  151. Log.d("povezava", "Ni Povezano!");
  152.  
  153.  
  154. }
  155. });
  156. } catch (MqttException e) {
  157. e.printStackTrace();
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. switchgibanje.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  166. @Override
  167. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  168. if(isChecked == true ){
  169. stanje = 1;
  170. nastavitev = "gibanje";
  171. Send objSend = new Send();
  172. objSend.execute("");
  173.  
  174. //newSetting("settings", "1");
  175. }
  176. else {
  177. stanje = 0;
  178. nastavitev = "gibanje";
  179. Send objSend = new Send();
  180. objSend.execute("");
  181.  
  182. //newSetting("settings", "1");
  183. }
  184. }
  185. });
  186.  
  187.  
  188. switchtemperature.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  189. @Override
  190. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  191. if(isChecked == true ){
  192. stanje = 1;
  193. nastavitev = "temperatura";
  194. Send objSend = new Send();
  195. objSend.execute("");
  196.  
  197. //newSetting("settings", "1");
  198. }
  199. else {
  200. stanje = 0;
  201. nastavitev = "temperatura";
  202. Send objSend = new Send();
  203. objSend.execute("");
  204.  
  205. //newSetting("settings", "1");
  206. }
  207. }
  208. });
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. }
  233.  
  234. public void newSetting (String topic, String payload) {
  235. byte[] encodedPayload = new byte[0];
  236. try {
  237. encodedPayload = payload.getBytes("UTF-8");
  238. MqttMessage message = new MqttMessage(encodedPayload);
  239. client.publish(topic, message);
  240. } catch (UnsupportedEncodingException | MqttException e) {
  241. e.printStackTrace();
  242. }
  243.  
  244. }
  245.  
  246.  
  247.  
  248. private class Send extends AsyncTask<String, String, String> {
  249. String msg = "";
  250.  
  251. @Override
  252. protected void onPreExecute() {
  253. //txtv.setText("Please wait Inserting Data");
  254. }
  255.  
  256. @Override
  257. protected String doInBackground(String... strings) {
  258. try{
  259. Class.forName("com.mysql.jdbc.Driver");
  260. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  261. if(conn == null){
  262. //msg = "Connection goes wrong";
  263.  
  264.  
  265. }
  266. else {
  267. String query = "UPDATE settings SET value='"+stanje+"' WHERE setting = '"+nastavitev+"'";
  268. Statement stmt = conn.createStatement();
  269. stmt.executeUpdate(query);
  270. //msg = "Uspešno vnešeno!";
  271.  
  272. }
  273. conn.close();
  274. newSetting("settings", "1");
  275.  
  276. }
  277. catch (Exception e){
  278. //msg = "Connection goes wrong";
  279. e.printStackTrace();
  280. }
  281. return msg;
  282. }
  283. }
  284.  
  285.  
  286.  
  287. public class onLoadActivity extends AsyncTask<String, String, String> {
  288. String msg = "";
  289.  
  290.  
  291.  
  292. @Override
  293. protected String doInBackground(String... strings) {
  294. try{
  295. Class.forName("com.mysql.jdbc.Driver");
  296. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  297. if(conn == null){
  298.  
  299. }
  300. else {
  301. String query = "SELECT setting, value FROM settings";
  302. Statement stmt = conn.createStatement();
  303. stmt.executeQuery(query);
  304. ResultSet rs = stmt.executeQuery(query);
  305.  
  306.  
  307. while (rs.next()){
  308. names.add(rs.getString(1));
  309. names.add(rs.getString(2));
  310.  
  311. }
  312. //String[] result = new String[names.size()];
  313. //result = names.toArray(result);
  314.  
  315.  
  316. //for (int row = 0; row <= result.length / 2; row+=2) {
  317. //
  318. //
  319. //System.out.println(result[row] +" " + result [row+1]);
  320. //
  321. // ViewGroup layout = (ViewGroup)findViewById(R.id.layout);
  322. // for (int i = 0; i < layout.getChildCount(); i++) {
  323. //
  324. // View child = layout.getChildAt(i);
  325. // if(child instanceof Switch)
  326. // {
  327. // Switch aSwitch = (Switch) child;
  328. // String id = aSwitch.getTag().toString();
  329. // //Log.d("Switch: ",aSwitch.getTag().toString());
  330. //
  331. // boolean hide = aSwitch.getTag() == result[row] && result[row+1] == "1";
  332. //
  333. //
  334. // if (hide == true) {
  335. // Log.d("Switch: ",aSwitch.getTag().toString() + "bi moral biti ON");
  336. // aSwitch.setChecked(true);
  337. //
  338. // }
  339. //
  340. // //new Handler().postDelayed(new Runnable() {
  341. // // @Override
  342. // // public void run() {
  343. // // aSwitch.setChecked(hide);
  344. // // }
  345. // //}, 1000);
  346. // }
  347. // }
  348. //}
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357. }
  358.  
  359. conn.close();
  360.  
  361.  
  362.  
  363. }
  364. catch (Exception e){
  365.  
  366. e.printStackTrace();
  367. }
  368. return msg;
  369. }
  370. }
  371.  
  372.  
  373.  
  374.  
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement