all toggle

This commit is contained in:
misty 2018-06-20 21:20:20 +02:00
parent eaf73d067e
commit df123d7120

View File

@ -24,17 +24,18 @@ public class ManualDriveActivity extends AppCompatActivity {
private Handler coilHandler; private Handler coilHandler;
private HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> (); private HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> ();
private HashMap<ToggleButton, Integer> mapButtonCoil = new HashMap<>();
private boolean manualMode = false; private boolean manualMode = false;
private void initMap () { private void initMap () {
mapCoilCheckStatus.put(64, false);
mapCoilCheckStatus.put(70, false);
mapCoilCheckStatus.put(65, false); mapCoilCheckStatus.put(65, false);
mapCoilCheckStatus.put(71, 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(68, false);
mapCoilCheckStatus.put(69, false);
mapCoilCheckStatus.put(66, false);
mapCoilCheckStatus.put(67, false);
} }
@Override @Override
@ -42,7 +43,7 @@ public class ManualDriveActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
state = (GlobalState) getApplicationContext(); 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); 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 m1ControlButton = (ToggleButton) findViewById(R.id.m1_control_button);
ToggleButton m2ControlButton = (ToggleButton) findViewById(R.id.m2_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("readVmCoil2"));
// LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue")); // LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue"));
@ -89,38 +95,41 @@ public class ManualDriveActivity extends AppCompatActivity {
} }
}); });
//TODO refactoring starting from here !! // for each toggle button, set the event
// button h1 for (Map.Entry<ToggleButton, Integer> entry : mapButtonCoil.entrySet()) {
h1ControlButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { final ToggleButton button = entry.getKey();
@Override final Integer coilToWrite = entry.getValue();
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
if (isChecked) { @Override
// writing the value true public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); if (isChecked) {
serviceIntent.setAction("write.coil"); // writing the value true
serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.putExtra("extra.ip.port", state.getPort()); serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ref", 64); serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.bit", true); serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.intent.name", "writeBit65"); serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.is.basic.coil", true); serviceIntent.putExtra("extra.bit", true);
getApplicationContext().startService(serviceIntent); serviceIntent.putExtra("extra.intent.name", "writeBit65");
mapCoilCheckStatus.put(64, true); serviceIntent.putExtra("extra.is.basic.coil", true);
} else { getApplicationContext().startService(serviceIntent);
// writing the value false mapCoilCheckStatus.put(coilToWrite, true);
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); } else {
serviceIntent.setAction("write.coil"); // writing the value false
serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
serviceIntent.putExtra("extra.ip.port", state.getPort()); serviceIntent.setAction("write.coil");
serviceIntent.putExtra("extra.ref", 64); serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
serviceIntent.putExtra("extra.bit", false); serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.intent.name", "writeBit65"); serviceIntent.putExtra("extra.ref", coilToWrite);
serviceIntent.putExtra("extra.is.basic.coil", true); serviceIntent.putExtra("extra.bit", false);
getApplicationContext().startService(serviceIntent); serviceIntent.putExtra("extra.intent.name", "writeBit65");
mapCoilCheckStatus.put(64, false); serviceIntent.putExtra("extra.is.basic.coil", true);
getApplicationContext().startService(serviceIntent);
mapCoilCheckStatus.put(coilToWrite, false);
}
} }
} });
}); }
} }
@Override @Override
@ -166,9 +175,8 @@ public class ManualDriveActivity extends AppCompatActivity {
serviceIntent.putExtra("extra.ip.port", state.getPort()); serviceIntent.putExtra("extra.ip.port", state.getPort());
serviceIntent.putExtra("extra.ref", state.getMapMappingValue().get(entry.getKey())); serviceIntent.putExtra("extra.ref", state.getMapMappingValue().get(entry.getKey()));
serviceIntent.putExtra("extra.count", 1); serviceIntent.putExtra("extra.count", 1);
serviceIntent.putExtra("extra.intent.name", "readOnlyCoil"); serviceIntent.putExtra("extra.intent.name", "read");
getApplicationContext().startService(serviceIntent); getApplicationContext().startService(serviceIntent);
} }
} }
} }