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 HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> ();
private HashMap<ToggleButton, Integer> 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<ToggleButton, Integer> 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);
}
}
}