JaesilModeActivity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package kr.co.icontrols.iotplatform;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.CompoundButton;
  8. import android.widget.Switch;
  9. import android.widget.TextView;
  10. import androidx.annotation.Nullable;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. import com.amazonaws.mobileconnectors.iot.AWSIotMqttNewMessageCallback;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15. import kr.co.icontrols.iotplatform.DataClass.DeviceInfo;
  16. import kr.co.icontrols.iotplatform.Mqtt.MqttController;
  17. import static kr.co.icontrols.iotplatform.PopupActivity.POPUP_TIME_INDEX;
  18. import static kr.co.icontrols.iotplatform.utils.CommonUtils.DebugLogOutput;
  19. import static kr.co.icontrols.iotplatform.utils.CommonUtils.showNotiMsg;
  20. import static kr.co.icontrols.iotplatform.utils.Constants.EXTRA_DEVICE_INFO;
  21. import static kr.co.icontrols.iotplatform.utils.Constants.MQTT_PUBLISH_DELAY_TIME;
  22. import static kr.co.icontrols.iotplatform.utils.Constants.NOTING;
  23. import static kr.co.icontrols.iotplatform.utils.Constants.POWER_OFF;
  24. import static kr.co.icontrols.iotplatform.utils.Constants.POWER_ON;
  25. import static kr.co.icontrols.iotplatform.utils.Constants.RESULT_CHANGE_JAESIL_TIME;
  26. public class JaesilModeActivity extends AppCompatActivity implements View.OnClickListener {
  27. private String TAG = "JaesilModeActivity";
  28. // private DeviceInfo deviceInfo;
  29. // private boolean isChangeData;
  30. // private Switch switchJaesil;
  31. // private int power = NOTING;
  32. // private int mSelectTime; //0:2시간, 1:1시간30분, 2:1시간, 3:30분, 4:10분
  33. // private String topicGetP,topicGetS1,topicSetP,topicSetS1;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_jaesil_mode);
  38. // isChangeData = false;
  39. // mSelectTime = 60;
  40. // deviceInfo = getIntent().getParcelableExtra(EXTRA_DEVICE_INFO);
  41. //
  42. // topicGetP = String.format(getString(R.string.topic_get_jaesil_data),deviceInfo.deviceId);
  43. // topicGetS1 = String.format(getString(R.string.topic_response_get_jaesil_data),deviceInfo.deviceId);
  44. // topicSetP = String.format(getString(R.string.topic_set_jaesil_data),deviceInfo.deviceId);
  45. // topicSetS1 = String.format(getString(R.string.topic_response_set_jaesil_data),deviceInfo.deviceId);
  46. //getJaesilData();
  47. }
  48. @Override
  49. protected void onPause() {
  50. //MqttController.getInstance().allUnsubscribeToTopics();
  51. super.onPause();
  52. }
  53. @Override
  54. protected void onResume() {
  55. super.onResume();
  56. //getJaesilData();
  57. }
  58. @Override
  59. public void onBackPressed() {
  60. super.onBackPressed();
  61. /*
  62. if(isChangeData) {
  63. setResult(RESULT_OK);
  64. finish();
  65. } else {
  66. super.onBackPressed();
  67. }
  68. */
  69. }
  70. @Override
  71. public void onClick(View v) {
  72. /*
  73. final View fv = v;
  74. fv.setEnabled(false);
  75. Timer buttonTimer = new Timer();
  76. buttonTimer.schedule(new TimerTask() {
  77. @Override
  78. public void run() {
  79. runOnUiThread(new Runnable() {
  80. @Override
  81. public void run() {
  82. try {
  83. fv.setEnabled(true);
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. } finally {
  87. }
  88. }
  89. });
  90. }
  91. }, 500);
  92. */
  93. switch (v.getId()) {
  94. /*
  95. case R.id.btnOnOff:
  96. if(power == NOTING) {
  97. showNotiMsg(JaesilModeActivity.this,"재실센서가 동작되지 않습니다.");
  98. } else if(power == POWER_OFF) {
  99. changeSetTime(true, POWER_ON, mSelectTime, 0);
  100. } else if(power == POWER_ON){
  101. changeSetTime(true, POWER_OFF, mSelectTime, 0);
  102. }
  103. break;
  104. case R.id.layoutSetTime:
  105. {
  106. Intent intent = new Intent(this, PopupActivity.class);
  107. intent.putExtra(PopupActivity.POPUP_MODE, PopupActivity.MODE_SET_JAESIL_TIME);
  108. intent.putExtra(POPUP_TIME_INDEX, 60);
  109. startActivityForResult(intent, RESULT_CHANGE_JAESIL_TIME);
  110. }
  111. break;
  112. */
  113. case R.id.btnRight:
  114. break;
  115. case R.id.btnBack:
  116. onBackPressed();
  117. break;
  118. }
  119. }
  120. /*
  121. @Override
  122. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  123. super.onActivityResult(requestCode, resultCode, data);
  124. switch (requestCode) {
  125. case RESULT_CHANGE_JAESIL_TIME:
  126. if (resultCode == RESULT_OK) {
  127. final int selectedTime = data.getIntExtra(POPUP_TIME_INDEX, 60);
  128. new Handler().postDelayed(new Runnable() {
  129. @Override
  130. public void run() {
  131. changeSetTime(true, power, selectedTime, 0);
  132. }
  133. },MQTT_PUBLISH_DELAY_TIME+20);
  134. }
  135. break;
  136. }
  137. }
  138. private void getJaesilData() {
  139. findViewById(R.id.btnOnOff).setEnabled(false);
  140. findViewById(R.id.layoutSetTime).setEnabled(false);
  141. MqttController.getInstance().subscribeToTopic(topicGetS1, new AWSIotMqttNewMessageCallback() {
  142. @Override
  143. public void onMessageArrived(String topic, byte[] data) {
  144. DebugLogOutput(TAG, "MQTT SUB / "+topic + "\n" + new String(data));
  145. parsingData(new String(data));
  146. MqttController.getInstance().unsubscribeToTopic(topicGetS1);
  147. }
  148. });
  149. MqttController.getInstance().subscribeToTopic(topicSetS1, new AWSIotMqttNewMessageCallback() {
  150. @Override
  151. public void onMessageArrived(String topic, byte[] data) {
  152. DebugLogOutput(TAG, "MQTT SUB / "+topic + "\n" + new String(data));
  153. parsingData(new String(data));
  154. }
  155. });
  156. new Handler().postDelayed(new Runnable() {
  157. @Override
  158. public void run() {
  159. MqttController.getInstance().publish(topicGetP,"{}");
  160. }
  161. },MQTT_PUBLISH_DELAY_TIME);
  162. }
  163. private void parsingData(final String data) {
  164. int temp_power = NOTING;
  165. int temp_time = NOTING;
  166. int temp_step = NOTING;
  167. try {
  168. JSONObject jsonObject = new JSONObject(data);
  169. if(!jsonObject.isNull("state")) { //Update topic
  170. JSONObject jsonState = jsonObject.getJSONObject("state");
  171. if (!jsonState.isNull("reported")) {
  172. JSONObject jsonReported = jsonState.getJSONObject("reported");
  173. //Power
  174. if (!jsonReported.isNull("sse")) {
  175. //power off
  176. temp_power = jsonReported.getInt("sse");
  177. }
  178. //Time
  179. if (!jsonReported.isNull("sot")) {
  180. temp_time = jsonReported.getInt("sot");
  181. }
  182. //Jaesil Statu
  183. if (!jsonReported.isNull("sn")) {
  184. temp_step = jsonReported.getInt("sn");
  185. }
  186. }
  187. }
  188. } catch (JSONException e) {
  189. e.printStackTrace();
  190. }
  191. final int powerFix = temp_power;
  192. final int timeFix = temp_time;
  193. final int stepFix = temp_step;
  194. if(powerFix != NOTING && timeFix != NOTING && stepFix != NOTING) {
  195. runOnUiThread(new Runnable() {
  196. @Override
  197. public void run() {
  198. findViewById(R.id.btnOnOff).setEnabled(true);
  199. changeSetTime(false, powerFix, timeFix, stepFix);
  200. }
  201. });
  202. }
  203. }
  204. private void changeSetTime(boolean send, int onOff, int time, int step) {
  205. mSelectTime = time;
  206. power = onOff;
  207. findViewById(R.id.btnOnOff).setSelected(onOff==POWER_ON?true:false);
  208. ((Button)findViewById(R.id.btnOnOff)).setText(onOff==POWER_ON?getString(R.string.turn_off_jaesil):getString(R.string.turn_on_jaesil));
  209. findViewById(R.id.layoutSetTime).setEnabled(onOff==POWER_ON?true:false);
  210. switch (mSelectTime) {
  211. case 120:
  212. ((TextView)findViewById(R.id.txtSetTime)).setText("2시간");
  213. break;
  214. case 90:
  215. ((TextView)findViewById(R.id.txtSetTime)).setText("1시간 30분");
  216. break;
  217. case 60:
  218. default:
  219. ((TextView)findViewById(R.id.txtSetTime)).setText("1시간");
  220. break;
  221. case 50:
  222. ((TextView)findViewById(R.id.txtSetTime)).setText("50분");
  223. break;
  224. case 40:
  225. ((TextView)findViewById(R.id.txtSetTime)).setText("40분");
  226. break;
  227. case 30:
  228. ((TextView)findViewById(R.id.txtSetTime)).setText("30분");
  229. break;
  230. case 20:
  231. ((TextView)findViewById(R.id.txtSetTime)).setText("20분");
  232. break;
  233. case 10:
  234. ((TextView)findViewById(R.id.txtSetTime)).setText("10분");
  235. break;
  236. case 5:
  237. ((TextView)findViewById(R.id.txtSetTime)).setText("5분");
  238. break;
  239. }
  240. if(send) {
  241. isChangeData = true;
  242. setJaesil(onOff, time, step);
  243. }
  244. }
  245. private void setJaesil(int onOff, int time, int step) {
  246. String payload = "{\n" +
  247. "\"state\": {\n" +
  248. "\"reported\" : {\n" +
  249. "\"sse\" : " + onOff + ",\n" +
  250. "\"sot\" : " + time + ",\n" +
  251. "\"sn\" : " + step + "\n" +
  252. "}\n" +
  253. "}\n" +
  254. "}";
  255. MqttController.getInstance().publish(topicSetP,payload);
  256. }
  257. */
  258. }