|
|
|
|
@ -1,11 +1,15 @@
|
|
|
|
|
package com.example.pdaplccontrol;
|
|
|
|
|
|
|
|
|
|
import android.content.res.ColorStateList;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.Looper;
|
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
@ -14,18 +18,45 @@ import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
|
|
|
|
|
|
|
|
|
private final Button[] lineButtons = new Button[9];
|
|
|
|
|
private final Handler monitorHandler = new Handler(Looper.getMainLooper());
|
|
|
|
|
private final Runnable monitorRunnable = new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
readCurrentValues();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
private EditText editWeightKg;
|
|
|
|
|
private TextView textMonitorStatus;
|
|
|
|
|
private LinearLayout rootLayout;
|
|
|
|
|
private boolean monitorRunning;
|
|
|
|
|
private boolean monitorRequestInFlight;
|
|
|
|
|
private ColorStateList defaultLineButtonBackgroundTint;
|
|
|
|
|
private ColorStateList defaultLineButtonTextColors;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
|
|
rootLayout = findViewById(R.id.rootLayout);
|
|
|
|
|
editWeightKg = findViewById(R.id.editWeightKg);
|
|
|
|
|
textMonitorStatus = findViewById(R.id.textMonitorStatus);
|
|
|
|
|
setupWeightInput();
|
|
|
|
|
setupLineButtons();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStart() {
|
|
|
|
|
super.onStart();
|
|
|
|
|
startMonitor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStop() {
|
|
|
|
|
stopMonitor();
|
|
|
|
|
super.onStop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setupWeightInput() {
|
|
|
|
|
editWeightKg.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
@ -35,6 +66,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
|
|
&& event.getAction() == KeyEvent.ACTION_UP
|
|
|
|
|
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
|
|
|
|
|
if (isDone) {
|
|
|
|
|
rootLayout.requestFocus();
|
|
|
|
|
sendWeight();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
@ -58,6 +90,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
|
|
lineButtons[i].setTag((short) (PlcConfig.MIN_LINE_NUMBER + i));
|
|
|
|
|
lineButtons[i].setOnClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
defaultLineButtonBackgroundTint = lineButtons[0].getBackgroundTintList();
|
|
|
|
|
defaultLineButtonTextColors = lineButtons[0].getTextColors();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@ -73,18 +107,19 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendWeight() {
|
|
|
|
|
final short weightGrams;
|
|
|
|
|
final short weightKg;
|
|
|
|
|
try {
|
|
|
|
|
weightGrams = WeightInputParser.kgToGrams(editWeightKg.getText().toString());
|
|
|
|
|
weightKg = WeightInputParser.parseKg(editWeightKg.getText().toString());
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setControlsEnabled(false);
|
|
|
|
|
Toast.makeText(this, "正在向 PLC 发送重量 " + weightGrams + "g...", Toast.LENGTH_SHORT).show();
|
|
|
|
|
Toast.makeText(this, "正在向 PLC 发送重量 "
|
|
|
|
|
+ WeightInputParser.kgToDisplayText(weightKg) + "kg...", Toast.LENGTH_SHORT).show();
|
|
|
|
|
|
|
|
|
|
PlcManager.getInstance().sendWeightGramsAsync(weightGrams, createCallback());
|
|
|
|
|
PlcManager.getInstance().sendWeightKgAsync(weightKg, createCallback());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PlcManager.PlcCallback createCallback() {
|
|
|
|
|
@ -111,4 +146,99 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startMonitor() {
|
|
|
|
|
monitorRunning = true;
|
|
|
|
|
monitorHandler.removeCallbacks(monitorRunnable);
|
|
|
|
|
renderMonitorPending();
|
|
|
|
|
monitorHandler.post(monitorRunnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stopMonitor() {
|
|
|
|
|
monitorRunning = false;
|
|
|
|
|
monitorHandler.removeCallbacks(monitorRunnable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void readCurrentValues() {
|
|
|
|
|
if (!monitorRunning || monitorRequestInFlight) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
monitorRequestInFlight = true;
|
|
|
|
|
PlcManager.getInstance().readCurrentValuesAsync(new PlcManager.PlcCurrentValuesCallback() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onSuccess(PlcCurrentValues values) {
|
|
|
|
|
monitorRequestInFlight = false;
|
|
|
|
|
if (monitorRunning) {
|
|
|
|
|
renderCurrentValues(values);
|
|
|
|
|
}
|
|
|
|
|
scheduleNextMonitorRead();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onError(String error) {
|
|
|
|
|
monitorRequestInFlight = false;
|
|
|
|
|
if (monitorRunning) {
|
|
|
|
|
renderMonitorStale();
|
|
|
|
|
}
|
|
|
|
|
scheduleNextMonitorRead();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void scheduleNextMonitorRead() {
|
|
|
|
|
if (monitorRunning) {
|
|
|
|
|
monitorHandler.postDelayed(monitorRunnable, PlcConfig.MONITOR_INTERVAL_MILLIS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renderCurrentValues(PlcCurrentValues values) {
|
|
|
|
|
renderMonitorNormal();
|
|
|
|
|
if (!editWeightKg.hasFocus()) {
|
|
|
|
|
editWeightKg.setText(WeightInputParser.kgToDisplayText(values.getWeightKg()));
|
|
|
|
|
}
|
|
|
|
|
renderCurrentLineNumber(values.getLineNumber());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renderMonitorPending() {
|
|
|
|
|
textMonitorStatus.setText("正在读取PLC...");
|
|
|
|
|
textMonitorStatus.setTextColor(getColor(R.color.monitor_status_pending));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renderMonitorNormal() {
|
|
|
|
|
textMonitorStatus.setText("监控正常");
|
|
|
|
|
textMonitorStatus.setTextColor(getColor(R.color.monitor_status_normal));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renderMonitorStale() {
|
|
|
|
|
textMonitorStatus.setText("已失联/数据过期");
|
|
|
|
|
textMonitorStatus.setTextColor(getColor(R.color.monitor_status_stale));
|
|
|
|
|
clearCurrentLineNumberHighlight();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void renderCurrentLineNumber(short currentLineNumber) {
|
|
|
|
|
for (Button btn : lineButtons) {
|
|
|
|
|
if (btn == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short buttonLineNumber = (short) btn.getTag();
|
|
|
|
|
if (buttonLineNumber == currentLineNumber) {
|
|
|
|
|
btn.setBackgroundTintList(ColorStateList.valueOf(getColor(R.color.line_monitor_active)));
|
|
|
|
|
btn.setTextColor(getColor(R.color.line_monitor_active_text));
|
|
|
|
|
} else {
|
|
|
|
|
btn.setBackgroundTintList(defaultLineButtonBackgroundTint);
|
|
|
|
|
btn.setTextColor(defaultLineButtonTextColors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void clearCurrentLineNumberHighlight() {
|
|
|
|
|
for (Button btn : lineButtons) {
|
|
|
|
|
if (btn != null) {
|
|
|
|
|
btn.setBackgroundTintList(defaultLineButtonBackgroundTint);
|
|
|
|
|
btn.setTextColor(defaultLineButtonTextColors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|