package kr.co.icontrols.iotplatform; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import com.amazonaws.mobileconnectors.iot.AWSIotMqttNewMessageCallback; import org.json.JSONException; import org.json.JSONObject; import kr.co.icontrols.iotplatform.DataClass.DeviceInfo; import kr.co.icontrols.iotplatform.Mqtt.MqttController; import static kr.co.icontrols.iotplatform.PopupActivity.POPUP_TIME_INDEX; import static kr.co.icontrols.iotplatform.utils.CommonUtils.DebugLogOutput; import static kr.co.icontrols.iotplatform.utils.CommonUtils.showNotiMsg; import static kr.co.icontrols.iotplatform.utils.Constants.EXTRA_DEVICE_INFO; import static kr.co.icontrols.iotplatform.utils.Constants.MQTT_PUBLISH_DELAY_TIME; import static kr.co.icontrols.iotplatform.utils.Constants.NOTING; import static kr.co.icontrols.iotplatform.utils.Constants.POWER_OFF; import static kr.co.icontrols.iotplatform.utils.Constants.POWER_ON; import static kr.co.icontrols.iotplatform.utils.Constants.RESULT_CHANGE_JAESIL_TIME; public class JaesilModeActivity extends AppCompatActivity implements View.OnClickListener { private String TAG = "JaesilModeActivity"; // private DeviceInfo deviceInfo; // private boolean isChangeData; // private Switch switchJaesil; // private int power = NOTING; // private int mSelectTime; //0:2시간, 1:1시간30분, 2:1시간, 3:30분, 4:10분 // private String topicGetP,topicGetS1,topicSetP,topicSetS1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_jaesil_mode); // isChangeData = false; // mSelectTime = 60; // deviceInfo = getIntent().getParcelableExtra(EXTRA_DEVICE_INFO); // // topicGetP = String.format(getString(R.string.topic_get_jaesil_data),deviceInfo.deviceId); // topicGetS1 = String.format(getString(R.string.topic_response_get_jaesil_data),deviceInfo.deviceId); // topicSetP = String.format(getString(R.string.topic_set_jaesil_data),deviceInfo.deviceId); // topicSetS1 = String.format(getString(R.string.topic_response_set_jaesil_data),deviceInfo.deviceId); //getJaesilData(); } @Override protected void onPause() { //MqttController.getInstance().allUnsubscribeToTopics(); super.onPause(); } @Override protected void onResume() { super.onResume(); //getJaesilData(); } @Override public void onBackPressed() { super.onBackPressed(); /* if(isChangeData) { setResult(RESULT_OK); finish(); } else { super.onBackPressed(); } */ } @Override public void onClick(View v) { /* final View fv = v; fv.setEnabled(false); Timer buttonTimer = new Timer(); buttonTimer.schedule(new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { try { fv.setEnabled(true); } catch (Exception e) { e.printStackTrace(); } finally { } } }); } }, 500); */ switch (v.getId()) { /* case R.id.btnOnOff: if(power == NOTING) { showNotiMsg(JaesilModeActivity.this,"재실센서가 동작되지 않습니다."); } else if(power == POWER_OFF) { changeSetTime(true, POWER_ON, mSelectTime, 0); } else if(power == POWER_ON){ changeSetTime(true, POWER_OFF, mSelectTime, 0); } break; case R.id.layoutSetTime: { Intent intent = new Intent(this, PopupActivity.class); intent.putExtra(PopupActivity.POPUP_MODE, PopupActivity.MODE_SET_JAESIL_TIME); intent.putExtra(POPUP_TIME_INDEX, 60); startActivityForResult(intent, RESULT_CHANGE_JAESIL_TIME); } break; */ case R.id.btnRight: break; case R.id.btnBack: onBackPressed(); break; } } /* @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case RESULT_CHANGE_JAESIL_TIME: if (resultCode == RESULT_OK) { final int selectedTime = data.getIntExtra(POPUP_TIME_INDEX, 60); new Handler().postDelayed(new Runnable() { @Override public void run() { changeSetTime(true, power, selectedTime, 0); } },MQTT_PUBLISH_DELAY_TIME+20); } break; } } private void getJaesilData() { findViewById(R.id.btnOnOff).setEnabled(false); findViewById(R.id.layoutSetTime).setEnabled(false); MqttController.getInstance().subscribeToTopic(topicGetS1, new AWSIotMqttNewMessageCallback() { @Override public void onMessageArrived(String topic, byte[] data) { DebugLogOutput(TAG, "MQTT SUB / "+topic + "\n" + new String(data)); parsingData(new String(data)); MqttController.getInstance().unsubscribeToTopic(topicGetS1); } }); MqttController.getInstance().subscribeToTopic(topicSetS1, new AWSIotMqttNewMessageCallback() { @Override public void onMessageArrived(String topic, byte[] data) { DebugLogOutput(TAG, "MQTT SUB / "+topic + "\n" + new String(data)); parsingData(new String(data)); } }); new Handler().postDelayed(new Runnable() { @Override public void run() { MqttController.getInstance().publish(topicGetP,"{}"); } },MQTT_PUBLISH_DELAY_TIME); } private void parsingData(final String data) { int temp_power = NOTING; int temp_time = NOTING; int temp_step = NOTING; try { JSONObject jsonObject = new JSONObject(data); if(!jsonObject.isNull("state")) { //Update topic JSONObject jsonState = jsonObject.getJSONObject("state"); if (!jsonState.isNull("reported")) { JSONObject jsonReported = jsonState.getJSONObject("reported"); //Power if (!jsonReported.isNull("sse")) { //power off temp_power = jsonReported.getInt("sse"); } //Time if (!jsonReported.isNull("sot")) { temp_time = jsonReported.getInt("sot"); } //Jaesil Statu if (!jsonReported.isNull("sn")) { temp_step = jsonReported.getInt("sn"); } } } } catch (JSONException e) { e.printStackTrace(); } final int powerFix = temp_power; final int timeFix = temp_time; final int stepFix = temp_step; if(powerFix != NOTING && timeFix != NOTING && stepFix != NOTING) { runOnUiThread(new Runnable() { @Override public void run() { findViewById(R.id.btnOnOff).setEnabled(true); changeSetTime(false, powerFix, timeFix, stepFix); } }); } } private void changeSetTime(boolean send, int onOff, int time, int step) { mSelectTime = time; power = onOff; findViewById(R.id.btnOnOff).setSelected(onOff==POWER_ON?true:false); ((Button)findViewById(R.id.btnOnOff)).setText(onOff==POWER_ON?getString(R.string.turn_off_jaesil):getString(R.string.turn_on_jaesil)); findViewById(R.id.layoutSetTime).setEnabled(onOff==POWER_ON?true:false); switch (mSelectTime) { case 120: ((TextView)findViewById(R.id.txtSetTime)).setText("2시간"); break; case 90: ((TextView)findViewById(R.id.txtSetTime)).setText("1시간 30분"); break; case 60: default: ((TextView)findViewById(R.id.txtSetTime)).setText("1시간"); break; case 50: ((TextView)findViewById(R.id.txtSetTime)).setText("50분"); break; case 40: ((TextView)findViewById(R.id.txtSetTime)).setText("40분"); break; case 30: ((TextView)findViewById(R.id.txtSetTime)).setText("30분"); break; case 20: ((TextView)findViewById(R.id.txtSetTime)).setText("20분"); break; case 10: ((TextView)findViewById(R.id.txtSetTime)).setText("10분"); break; case 5: ((TextView)findViewById(R.id.txtSetTime)).setText("5분"); break; } if(send) { isChangeData = true; setJaesil(onOff, time, step); } } private void setJaesil(int onOff, int time, int step) { String payload = "{\n" + "\"state\": {\n" + "\"reported\" : {\n" + "\"sse\" : " + onOff + ",\n" + "\"sot\" : " + time + ",\n" + "\"sn\" : " + step + "\n" + "}\n" + "}\n" + "}"; MqttController.getInstance().publish(topicSetP,payload); } */ }