From df123d712094edf846215a5d6a6bd57d5544d11b Mon Sep 17 00:00:00 2001 From: misty Date: Wed, 20 Jun 2018 21:20:20 +0200 Subject: [PATCH] all toggle --- .../user/myapp/ManualDriveActivity.java | 86 ++++++++++--------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/example/user/myapp/ManualDriveActivity.java b/app/src/main/java/com/example/user/myapp/ManualDriveActivity.java index 24c9d43..37d2042 100644 --- a/app/src/main/java/com/example/user/myapp/ManualDriveActivity.java +++ b/app/src/main/java/com/example/user/myapp/ManualDriveActivity.java @@ -24,17 +24,18 @@ public class ManualDriveActivity extends AppCompatActivity { private Handler coilHandler; private HashMap mapCoilCheckStatus = new HashMap <> (); + private HashMap mapButtonCoil = new HashMap<>(); private boolean manualMode = false; private void initMap () { + mapCoilCheckStatus.put(64, false); + mapCoilCheckStatus.put(70, false); mapCoilCheckStatus.put(65, false); mapCoilCheckStatus.put(71, false); - mapCoilCheckStatus.put(66, false); - mapCoilCheckStatus.put(72, false); - mapCoilCheckStatus.put(69, false); - mapCoilCheckStatus.put(70, false); - mapCoilCheckStatus.put(67, false); mapCoilCheckStatus.put(68, false); + mapCoilCheckStatus.put(69, false); + mapCoilCheckStatus.put(66, false); + mapCoilCheckStatus.put(67, false); } @Override @@ -42,7 +43,7 @@ public class ManualDriveActivity extends AppCompatActivity { super.onCreate(savedInstanceState); state = (GlobalState) getApplicationContext(); - initMap(); // initialise the value of the coil toggle state + initMap(); // initialise the value of the coil toggle state + the corresponding coil to button setContentView(R.layout.activity_manual_drive); @@ -55,6 +56,11 @@ public class ManualDriveActivity extends AppCompatActivity { ToggleButton m1ControlButton = (ToggleButton) findViewById(R.id.m1_control_button); ToggleButton m2ControlButton = (ToggleButton) findViewById(R.id.m2_control_button); + mapButtonCoil.put(h1ControlButton, 65); + mapButtonCoil.put(h2ControlButton, 70); + mapButtonCoil.put(m1ControlButton, 66); + mapButtonCoil.put(m2ControlButton, 67); + LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2")); // LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue")); @@ -89,38 +95,41 @@ public class ManualDriveActivity extends AppCompatActivity { } }); - //TODO refactoring starting from here !! - // button h1 - h1ControlButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (isChecked) { - // writing the value true - Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); - serviceIntent.setAction("write.coil"); - serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); - serviceIntent.putExtra("extra.ip.port", state.getPort()); - serviceIntent.putExtra("extra.ref", 64); - serviceIntent.putExtra("extra.bit", true); - serviceIntent.putExtra("extra.intent.name", "writeBit65"); - serviceIntent.putExtra("extra.is.basic.coil", true); - getApplicationContext().startService(serviceIntent); - mapCoilCheckStatus.put(64, true); - } else { - // writing the value false - Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); - serviceIntent.setAction("write.coil"); - serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); - serviceIntent.putExtra("extra.ip.port", state.getPort()); - serviceIntent.putExtra("extra.ref", 64); - serviceIntent.putExtra("extra.bit", false); - serviceIntent.putExtra("extra.intent.name", "writeBit65"); - serviceIntent.putExtra("extra.is.basic.coil", true); - getApplicationContext().startService(serviceIntent); - mapCoilCheckStatus.put(64, false); + // for each toggle button, set the event + for (Map.Entry entry : mapButtonCoil.entrySet()) { + final ToggleButton button = entry.getKey(); + final Integer coilToWrite = entry.getValue(); + button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + // writing the value true + Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); + serviceIntent.setAction("write.coil"); + serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); + serviceIntent.putExtra("extra.ip.port", state.getPort()); + serviceIntent.putExtra("extra.ref", coilToWrite); + serviceIntent.putExtra("extra.bit", true); + serviceIntent.putExtra("extra.intent.name", "writeBit65"); + serviceIntent.putExtra("extra.is.basic.coil", true); + getApplicationContext().startService(serviceIntent); + mapCoilCheckStatus.put(coilToWrite, true); + } else { + // writing the value false + Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); + serviceIntent.setAction("write.coil"); + serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); + serviceIntent.putExtra("extra.ip.port", state.getPort()); + serviceIntent.putExtra("extra.ref", coilToWrite); + serviceIntent.putExtra("extra.bit", false); + serviceIntent.putExtra("extra.intent.name", "writeBit65"); + serviceIntent.putExtra("extra.is.basic.coil", true); + getApplicationContext().startService(serviceIntent); + mapCoilCheckStatus.put(coilToWrite, false); + } } - } - }); + }); + } } @Override @@ -166,9 +175,8 @@ public class ManualDriveActivity extends AppCompatActivity { serviceIntent.putExtra("extra.ip.port", state.getPort()); serviceIntent.putExtra("extra.ref", state.getMapMappingValue().get(entry.getKey())); serviceIntent.putExtra("extra.count", 1); - serviceIntent.putExtra("extra.intent.name", "readOnlyCoil"); + serviceIntent.putExtra("extra.intent.name", "read"); getApplicationContext().startService(serviceIntent); - } } }