package com.vanaluk.android_dht;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
public class MainActivity extends AppCompatActivity {
private Button releBtn, autoControlBtn, okThresholdBtn, plusDownThresholdBtn, minusDownThresholdBtn, plusUpThresholdBtn, minusUpThresholdBtn;
private TextView humidityTv, stateReleTv, curDownThresholdTv, curUpThresholdTv, downThresholdTv, upThresholdTv, autoControlTv;
private int downThreshold = 0, upThreshold = 0;
private int temp = 0;
private boolean stateRele = false;
private boolean autoControl = false;
// TCP/IP client
private Socket clientTCP;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
// UDP client
private DatagramSocket socketUDP;
private InetAddress address;
String ip="192.168.0.1";
int portTCP=8080;
int portUDP=4445;
String tagStart = "---";
String tagH = "h=";
String tagLowThreshold = "l=";
String tagUpThreshold = "up=";
String tagStateRele = "state=";
String tagAutoControl = "auto=";
String cmd = "";
enum TYPE_CMD {
SET_THRESHOLD,
SET_STATE_RELE,
SET_AUTO_CONTROL
}
int socketReadTimeOut = 300;
//int connectionTimeOutMs = 5000;
int connectionTimeOutMs = 10000;
int maxCountConnectionTimeOut = (int)(connectionTimeOutMs / socketReadTimeOut);
int countTimeOut = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downThresholdTv = (TextView) findViewById(R.id.downThreshold_tv);
upThresholdTv = (TextView) findViewById(R.id.upThreshold_tv);
stateReleTv = (TextView) findViewById(R.id.stateRele_tv);
autoControlTv = (TextView) findViewById(R.id.autoControl_tv);
curDownThresholdTv = (TextView) findViewById(R.id.curDownThreshold_tv);
curUpThresholdTv = (TextView) findViewById(R.id.curUpThreshold_tv);
humidityTv = (TextView) findViewById(R.id.humidity_tv);
downThreshold = Integer.parseInt(downThresholdTv.getText().toString());
upThreshold = Integer.parseInt(upThresholdTv.getText().toString());
releBtn = (Button) findViewById(R.id.rele_btn);
autoControlBtn = (Button) findViewById(R.id.autoControl_btn);
okThresholdBtn = (Button) findViewById(R.id.okThreshold_btn);
plusDownThresholdBtn = (Button) findViewById(R.id.plusDownThreshold_btn);
minusDownThresholdBtn = (Button) findViewById(R.id.minusDownThreshold_btn);
plusUpThresholdBtn = (Button) findViewById(R.id.plusUpThreshold_btn);
minusUpThresholdBtn = (Button) findViewById(R.id.minusUpThreshold_btn);
releBtn.setOnClickListener(onClickListener);
autoControlBtn.setOnClickListener(onClickListener);
okThresholdBtn.setOnClickListener(onClickListener);
plusDownThresholdBtn.setOnClickListener(onClickListener);
minusDownThresholdBtn.setOnClickListener(onClickListener);
plusUpThresholdBtn.setOnClickListener(onClickListener);
minusUpThresholdBtn.setOnClickListener(onClickListener);
showConnectionLost();
try {
socketProcessing();
} catch (Exception ex){
if (clientTCP != null) {
try {
clientTCP.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private final View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "";
switch (v.getId()) {
case R.id.rele_btn:
text = stateReleTv.getText().toString();
if (text.contains("Вкл.")){
stateRele = true;
}
else{
stateRele = false;
}
stateRele = !stateRele;
try {
sendPackage(TYPE_CMD.SET_STATE_RELE);
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.autoControl_btn:
text = autoControlTv.getText().toString();
if (text.contains("Вкл.")){
autoControl = true;
}
else{
autoControl = false;
}
autoControl = !autoControl;
try {
sendPackage(TYPE_CMD.SET_AUTO_CONTROL);
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.okThreshold_btn:
try {
sendPackage(TYPE_CMD.SET_THRESHOLD);
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.plusDownThreshold_btn:
if ((downThreshold + 5 >= 0) && (downThreshold + 5 <= 100) && (downThreshold + 5 < upThreshold)){
downThreshold += 5;
}
downThresholdTv.setText(Integer.toString(downThreshold));
break;
case R.id.minusDownThreshold_btn:
if ((downThreshold - 5 >= 0) && (downThreshold - 5 <= 100) && (downThreshold - 5 < upThreshold)){
downThreshold -= 5;
}
downThresholdTv.setText(Integer.toString(downThreshold));
break;
case R.id.plusUpThreshold_btn:
if ((upThreshold + 5 >= 0) && (upThreshold + 5 <= 100) && (downThreshold < upThreshold + 5)){
upThreshold += 5;
}
upThresholdTv.setText(Integer.toString(upThreshold));
break;
case R.id.minusUpThreshold_btn:
if ((upThreshold - 5 >= 0) && (upThreshold - 5 <= 100) && (downThreshold < upThreshold - 5)){
upThreshold -= 5;
}
upThresholdTv.setText(Integer.toString(upThreshold));
break;
default :
break;
}
}
};
// Этот метод вызывается из главного потока GUI.
private void socketProcessing() {
// Задачи переносятся в дочерний поток.
Thread thread = new Thread(null, socketStart,
"Background");
thread.start();
}
// Объект Runnable, который запускает метод для выполнения задач в фоновом режиме.
private Runnable socketStart = new Runnable() {
public void run() {
try {
// TCP/IP client
/*
clientTCP = new Socket(ip, portTCP);
评论0