added h1
This commit is contained in:
parent
2c016376e4
commit
eaf73d067e
@ -7,17 +7,18 @@ import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class GlobalState extends Application {
|
||||
|
||||
// coil
|
||||
private String ipAddress = "192.168.157.16";
|
||||
private int port = 503;
|
||||
private HashMap<Integer, Integer> mapMappingValue = new HashMap<Integer, Integer>();
|
||||
//
|
||||
public enum InputConfig {
|
||||
Q_COIL(8192, 8),
|
||||
VM_COIL2 (1, 8),
|
||||
VM_COIL3 (65, 8),
|
||||
VM_COIL3 (64, 8),
|
||||
AI_IR(0, 2),
|
||||
I_DI(0, 12);
|
||||
|
||||
@ -40,6 +41,11 @@ public class GlobalState extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<Integer, Integer> getMapMappingValue() {
|
||||
initMappingValue();
|
||||
return mapMappingValue;
|
||||
}
|
||||
|
||||
// getter/setter
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
@ -56,4 +62,16 @@ public class GlobalState extends Application {
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
private void initMappingValue () {
|
||||
mapMappingValue.clear();
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference(), InputConfig.Q_COIL.getStartReference()); // 64/8192
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 6, InputConfig.Q_COIL.getStartReference() + 1); // 70/8193
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 1, InputConfig.Q_COIL.getStartReference() + 4); // 65/8196
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 7, InputConfig.Q_COIL.getStartReference() + 5); // 71/8197
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 4, InputConfig.Q_COIL.getStartReference() + 6); // 68/8198
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 5, InputConfig.Q_COIL.getStartReference() + 7); // 69/8199
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 2, InputConfig.Q_COIL.getStartReference() + 2); // 66/8194
|
||||
mapMappingValue.put(InputConfig.VM_COIL3.getStartReference() + 3, InputConfig.Q_COIL.getStartReference() + 3); // 67/8195
|
||||
}
|
||||
}
|
||||
|
@ -5,36 +5,62 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ManualDriveActivity extends AppCompatActivity {
|
||||
private GlobalState state;
|
||||
private Handler coilHandler;
|
||||
|
||||
private HashMap<Integer, Boolean> mapCoilCheckStatus = new HashMap <> ();
|
||||
private boolean manualMode = false;
|
||||
|
||||
private void initMap () {
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2"));
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue"));
|
||||
|
||||
state = (GlobalState) getApplicationContext();
|
||||
|
||||
coilHandler = new Handler();
|
||||
// coilHandler.post(refreshCoil);
|
||||
|
||||
initMap(); // initialise the value of the coil toggle state
|
||||
|
||||
setContentView(R.layout.activity_manual_drive);
|
||||
|
||||
ToggleButton button = (ToggleButton) findViewById(R.id.manual_override_button);
|
||||
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
ToggleButton h1ControlButton = (ToggleButton) findViewById(R.id.h1_control_button);
|
||||
ToggleButton h2ControlButton = (ToggleButton) findViewById(R.id.h2_control_button);
|
||||
Button pu1ControlButton = (Button) findViewById(R.id.pu1_control_button);
|
||||
Button pu2ControlButton = (Button) findViewById(R.id.pu2_control_button);
|
||||
Button sp1ControlButton = (Button) findViewById(R.id.sp1_control_button);
|
||||
Button b1ControlButton = (Button) findViewById(R.id.b1_control_button);
|
||||
ToggleButton m1ControlButton = (ToggleButton) findViewById(R.id.m1_control_button);
|
||||
ToggleButton m2ControlButton = (ToggleButton) findViewById(R.id.m2_control_button);
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("readVmCoil2"));
|
||||
// LocalBroadcastManager.getInstance(this).registerReceiver(coilWriterMessageReceiver, new IntentFilter("checkCoilValue"));
|
||||
|
||||
// manual drive button
|
||||
ToggleButton manualDriveOverrideButton = (ToggleButton) findViewById(R.id.manual_override_button);
|
||||
manualDriveOverrideButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
if (isChecked) {
|
||||
@ -46,6 +72,7 @@ public class ManualDriveActivity extends AppCompatActivity {
|
||||
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.VM_COIL2.getStartReference());
|
||||
serviceIntent.putExtra("extra.bit", true);
|
||||
serviceIntent.putExtra("extra.intent.name", "readVmCoil2");
|
||||
serviceIntent.putExtra("extra.is.basic.coil", false);
|
||||
getApplicationContext().startService(serviceIntent);
|
||||
} else {
|
||||
// writing the value false
|
||||
@ -56,10 +83,51 @@ public class ManualDriveActivity extends AppCompatActivity {
|
||||
serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.VM_COIL2.getStartReference());
|
||||
serviceIntent.putExtra("extra.bit", false);
|
||||
serviceIntent.putExtra("extra.intent.name", "readVmCoil2");
|
||||
serviceIntent.putExtra("extra.is.basic.coil", false);
|
||||
getApplicationContext().startService(serviceIntent);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart () {
|
||||
super.onStart();
|
||||
coilHandler = new Handler();
|
||||
coilHandler.post(refreshCoil);
|
||||
}
|
||||
|
||||
private BroadcastReceiver coilWriterMessageReceiver = new BroadcastReceiver() {
|
||||
@ -71,13 +139,41 @@ public class ManualDriveActivity extends AppCompatActivity {
|
||||
ImageView lightOn = (ImageView) findViewById(R.id.manual_override_status_light_on);
|
||||
ImageView lightOff = (ImageView) findViewById(R.id.manual_override_status_light_on);
|
||||
|
||||
System.out.print("value "+supposedToBeValue);
|
||||
if (supposedToBeValue) {
|
||||
manualMode = true;
|
||||
lightOn.setVisibility(View.VISIBLE);
|
||||
lightOff.setVisibility(View.INVISIBLE);
|
||||
} else {
|
||||
manualMode = false;
|
||||
lightOn.setVisibility(View.INVISIBLE);
|
||||
lightOff.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Runnable refreshCoil = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("pass "+manualMode);
|
||||
if (manualMode) {
|
||||
for (Map.Entry<Integer, Boolean> entry : mapCoilCheckStatus.entrySet()) {
|
||||
System.out.println("entry : " + entry.getKey() + " value :" + entry.getValue());
|
||||
if (entry.getValue() == true) { // if the thing is toggle
|
||||
Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class);
|
||||
serviceIntent.setAction("read.coil");
|
||||
serviceIntent.putExtra("extra.ip.address", state.getIpAddress());
|
||||
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");
|
||||
getApplicationContext().startService(serviceIntent);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
coilHandler.postDelayed(this, 2000);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -127,28 +127,36 @@ public class MyIntentService extends IntentService {
|
||||
}
|
||||
}
|
||||
|
||||
public void writeCoilAction (String ipAddress, int port, int ref, boolean value, String intentReceiveName) {
|
||||
public void writeCoilAction (String ipAddress, int port, int ref, boolean value, String intentReceiveName, boolean withCheck) {
|
||||
try {
|
||||
master = new ModbusTCPMaster(ipAddress, port);
|
||||
master.connect();
|
||||
|
||||
// writing the value
|
||||
boolean expectedValue = master.writeCoil(ref, value);
|
||||
System.out.println("I wrote "+expectedValue+" to coil "+ref);
|
||||
Thread.sleep(CAP_WAIT_TIME);
|
||||
boolean writtenValue = master.writeCoil(ref, value);
|
||||
if (withCheck) {
|
||||
System.out.println("I wrote " + writtenValue + " to coil " + ref);
|
||||
Thread.sleep(CAP_WAIT_TIME);
|
||||
|
||||
boolean currentValue = master.readCoils(8192, 1).getBit(0);
|
||||
sendNewBitValueToActivity (currentValue, intentReceiveName);
|
||||
GlobalState state = (GlobalState) getApplicationContext();
|
||||
|
||||
int coilToRead = state.getMapMappingValue().get(ref);
|
||||
System.out.println("Checking the value of coil "+coilToRead);
|
||||
boolean currentValue = master.readCoils(coilToRead, 1).getBit(0);
|
||||
sendNewBitValueToActivity(currentValue, intentReceiveName);
|
||||
} else {
|
||||
System.out.println("I switched the manual mode to " + writtenValue);
|
||||
sendNewBitValueToActivity(writtenValue, intentReceiveName);
|
||||
}
|
||||
Thread.sleep(CAP_WAIT_TIME);
|
||||
master.disconnect();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception in reading coil " + e);
|
||||
System.out.println("Exception in writing coil " + e);
|
||||
}
|
||||
}
|
||||
|
||||
private void sendNewBitValueToActivity (boolean newValue, String intentReceive) {
|
||||
Intent intent = new Intent(intentReceive);
|
||||
System.out.println("here "+newValue);
|
||||
intent.putExtra("coil", newValue);
|
||||
sendLocationBroadcast(intent);
|
||||
}
|
||||
@ -166,7 +174,7 @@ public class MyIntentService extends IntentService {
|
||||
// for write
|
||||
final Boolean value = intent.getBooleanExtra(EXTRA_BIT_TO_SET, false);
|
||||
|
||||
final Boolean expectedValue = intent.getBooleanExtra("extra.coil.status", false);
|
||||
final Boolean withCheck = intent.getBooleanExtra("extra.is.basic.coil", false);
|
||||
|
||||
switch(action) {
|
||||
case ACTION_READ_DISCRETE_INPUT:
|
||||
@ -179,7 +187,7 @@ public class MyIntentService extends IntentService {
|
||||
readInputRegisterAction (ipAddress, port,ref, count, intentName);
|
||||
break;
|
||||
case ACTION_WRITE_COIL:
|
||||
writeCoilAction(ipAddress, port, ref, value, intentName);
|
||||
writeCoilAction(ipAddress, port, ref, value, intentName, withCheck);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user