diff --git a/app/src/main/java/com/example/user/myapp/DisplayMessageActivity.java b/app/src/main/java/com/example/user/myapp/DisplayMessageActivity.java index 2a54828..d5e78bb 100644 --- a/app/src/main/java/com/example/user/myapp/DisplayMessageActivity.java +++ b/app/src/main/java/com/example/user/myapp/DisplayMessageActivity.java @@ -9,15 +9,15 @@ import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; +import android.widget.Toast; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; public class DisplayMessageActivity extends AppCompatActivity { private Handler handlerCoil; private Handler handlerInputRegister; + private Handler handlerDiscreteInput; private GlobalState state; @@ -31,15 +31,19 @@ public class DisplayMessageActivity extends AppCompatActivity { LocalBroadcastManager.getInstance(this).registerReceiver(discreteInputMessageReceiver, new IntentFilter("readOnlyDiscreteInput")); LocalBroadcastManager.getInstance(this).registerReceiver(inputRegisterMessageReceiver, new IntentFilter("readOnlyInputRegister")); - state = (GlobalState) getApplicationContext(); + Toast toast = Toast.makeText(getApplicationContext(), "This is my toast !", Toast.LENGTH_LONG); + toast.show(); //handler handlerCoil = new Handler(); handlerCoil.post(refreshCoil); handlerInputRegister = new Handler(); handlerInputRegister.post(refreshInputRegister); + + handlerDiscreteInput = new Handler(); + handlerDiscreteInput.post(refreshDiscreteInput); } Runnable refreshCoil = new Runnable() { @@ -49,34 +53,44 @@ public class DisplayMessageActivity extends AppCompatActivity { serviceIntent.setAction("read.coil"); serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); serviceIntent.putExtra("extra.ip.port", state.getPort()); - serviceIntent.putExtra("extra.ref", state.getCoilRef()); - serviceIntent.putExtra("extra.count", state.getCoilCount()); + serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.COIL.getStartReference()); + serviceIntent.putExtra("extra.count", GlobalState.InputConfig.COIL.getCount()); serviceIntent.putExtra("extra.intent.name", "readOnlyCoil"); getApplicationContext().startService(serviceIntent); - handlerCoil.postDelayed(this, 10000); + handlerCoil.postDelayed(this, 2000); } }; - Runnable refreshDiscretInput = new Runnable() { + Runnable refreshDiscreteInput = new Runnable() { @Override public void run() { - // TODO Auto-generated method stub - TextView textView = findViewById(R.id.textView1); - Date currentTime = Calendar.getInstance().getTime(); - textView.setText(new Double(Math.random()).toString()); - handlerCoil.postDelayed(this, 1000); + Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); + serviceIntent.setAction("read.discrete.input"); + serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); + serviceIntent.putExtra("extra.ip.port", state.getPort()); + serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.DISCRETE_INPUT.getStartReference()); + serviceIntent.putExtra("extra.count", GlobalState.InputConfig.DISCRETE_INPUT.getCount()); + serviceIntent.putExtra("extra.intent.name", "readOnlyDiscreteInput"); + getApplicationContext().startService(serviceIntent); + + handlerDiscreteInput.postDelayed(this, 5000); } }; Runnable refreshInputRegister = new Runnable() { @Override public void run() { - // TODO Auto-generated method stub - TextView textView = findViewById(R.id.textView1); - Date currentTime = Calendar.getInstance().getTime(); - textView.setText(new Double(Math.random()).toString()); - handlerCoil.postDelayed(this, 2000); + Intent serviceIntent = new Intent(getApplicationContext(), MyIntentService.class); + serviceIntent.setAction("read.input.register"); + serviceIntent.putExtra("extra.ip.address", state.getIpAddress()); + serviceIntent.putExtra("extra.ip.port", state.getPort()); + serviceIntent.putExtra("extra.ref", GlobalState.InputConfig.INPUT_REGISTER.getStartReference()); + serviceIntent.putExtra("extra.count", GlobalState.InputConfig.INPUT_REGISTER.getCount()); + serviceIntent.putExtra("extra.intent.name", "readOnlyInputRegister"); + getApplicationContext().startService(serviceIntent); + + handlerInputRegister.postDelayed(this, 1000); } }; @@ -100,7 +114,7 @@ public class DisplayMessageActivity extends AppCompatActivity { TextView textView4 = findViewById(R.id.textView4); textView4.setText(new Boolean(booleanArray[3]).toString()); - /* TextView textView5 = findViewById(R.id.textView5); + TextView textView5 = findViewById(R.id.textView5); textView5.setText(new Boolean(booleanArray[4]).toString()); TextView textView6 = findViewById(R.id.textView6); @@ -110,7 +124,7 @@ public class DisplayMessageActivity extends AppCompatActivity { textView7.setText(new Boolean(booleanArray[6]).toString()); TextView textView8 = findViewById(R.id.textView8); - textView8.setText(new Boolean(booleanArray[7]).toString()); */ + textView8.setText(new Boolean(booleanArray[7]).toString()); } }; @@ -119,17 +133,55 @@ public class DisplayMessageActivity extends AppCompatActivity { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Bundle bundle = intent.getExtras(); - ArrayList integers = bundle.getIntegerArrayList("irValues"); + ArrayList integerArrayList = bundle.getIntegerArrayList("irValues"); TextView textView9 = findViewById(R.id.textView9); - textView9.setText(integers.get(0).toString()); + textView9.setText(integerArrayList.get(0).toString()); } }; private BroadcastReceiver discreteInputMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + Bundle bundle = intent.getExtras(); + boolean[] booleanArray = bundle.getBooleanArray("discreteInputValues"); + TextView textView10 = findViewById(R.id.textView10); + textView10.setText(new Boolean(booleanArray[0]).toString()); + + TextView textView11 = findViewById(R.id.textView11); + textView11.setText(new Boolean(booleanArray[1]).toString()); + + TextView textView12 = findViewById(R.id.textView12); + textView12.setText(new Boolean(booleanArray[2]).toString()); + + TextView textView13 = findViewById(R.id.textView13); + textView13.setText(new Boolean(booleanArray[3]).toString()); + + TextView textView14 = findViewById(R.id.textView14); + textView14.setText(new Boolean(booleanArray[4]).toString()); + + TextView textView15 = findViewById(R.id.textView15); + textView15.setText(new Boolean(booleanArray[5]).toString()); + + TextView textView16 = findViewById(R.id.textView16); + textView16.setText(new Boolean(booleanArray[6]).toString()); + + TextView textView17 = findViewById(R.id.textView17); + textView17.setText(new Boolean(booleanArray[7]).toString()); + + TextView textView18 = findViewById(R.id.textView18); + textView18.setText(new Boolean(booleanArray[8]).toString()); + + TextView textView19 = findViewById(R.id.textView19); + textView19.setText(new Boolean(booleanArray[9]).toString()); + + TextView textView20 = findViewById(R.id.textView20); + textView20.setText(new Boolean(booleanArray[10]).toString()); + + TextView textView21 = findViewById(R.id.textView21); + textView21.setText(new Boolean(booleanArray[11]).toString()); } }; } diff --git a/app/src/main/java/com/example/user/myapp/GlobalState.java b/app/src/main/java/com/example/user/myapp/GlobalState.java index 7cc8b01..6a964a9 100644 --- a/app/src/main/java/com/example/user/myapp/GlobalState.java +++ b/app/src/main/java/com/example/user/myapp/GlobalState.java @@ -18,9 +18,13 @@ public class GlobalState extends Application { // public enum InputConfig { - COIL (8192, 80), - INPUT_REGISTER (0, 2), - DISCRETE_INPUT(0, 12); + /* COIL (8192, 8), + INPUT_REGISTER (1, 1), + DISCRETE_INPUT(1, 12); +*/ + COIL (1, 8), + INPUT_REGISTER (1, 8), + DISCRETE_INPUT(1, 12); private int startReference; private int count; diff --git a/app/src/main/java/com/example/user/myapp/MainActivity.java b/app/src/main/java/com/example/user/myapp/MainActivity.java index a53f039..b3a6ae5 100644 --- a/app/src/main/java/com/example/user/myapp/MainActivity.java +++ b/app/src/main/java/com/example/user/myapp/MainActivity.java @@ -19,8 +19,8 @@ public class MainActivity extends AppCompatActivity { public void sendMessage(View view) { EditText editText1 = (EditText) findViewById(R.id.ipAddress); // String ipAddress = editText1.getText().toString(); - String ipAddress = "10.0.0.60"; - int port = 502; + String ipAddress = "192.168.157.16"; + int port = 503; EditText editText2 = (EditText) findViewById(R.id.port); // int port = Integer.parseInt(editText2.getText().toString()); @@ -28,8 +28,6 @@ public class MainActivity extends AppCompatActivity { GlobalState state = (GlobalState) getApplicationContext(); state.setIpAddress(ipAddress); state.setPort(port); - // state.setCoilRef(ref); - // state.setCoilCount(count); Intent coilServiceIntent = new Intent(this, MyIntentService.class); coilServiceIntent.setAction("read.coil"); @@ -40,7 +38,7 @@ public class MainActivity extends AppCompatActivity { coilServiceIntent.putExtra("extra.intent.name", "readOnlyCoil"); this.startService(coilServiceIntent); - /* Intent discretInputserviceIntent = new Intent(this, MyIntentService.class); + Intent discretInputserviceIntent = new Intent(this, MyIntentService.class); discretInputserviceIntent.setAction("read.discrete.input"); discretInputserviceIntent.putExtra("extra.ip.address", ipAddress); discretInputserviceIntent.putExtra("extra.ip.port", port); @@ -56,7 +54,7 @@ public class MainActivity extends AppCompatActivity { inputRegisterServiceIntent.putExtra("extra.ref", GlobalState.InputConfig.INPUT_REGISTER.getStartReference()); inputRegisterServiceIntent.putExtra("extra.count", GlobalState.InputConfig.INPUT_REGISTER.getCount()); inputRegisterServiceIntent.putExtra("extra.intent.name", "readOnlyInputRegister"); - this.startService(inputRegisterServiceIntent); */ + this.startService(inputRegisterServiceIntent); Intent nextIntent = new Intent(this, DisplayMessageActivity.class); this.startActivity(nextIntent); diff --git a/app/src/main/java/com/example/user/myapp/MyIntentService.java b/app/src/main/java/com/example/user/myapp/MyIntentService.java index a967868..95922a3 100644 --- a/app/src/main/java/com/example/user/myapp/MyIntentService.java +++ b/app/src/main/java/com/example/user/myapp/MyIntentService.java @@ -34,17 +34,14 @@ public class MyIntentService extends IntentService { private static final String EXTRA_COUNT = "extra.count"; private static final String EXTRA_INTENT_NAME = "extra.intent.name"; - // private static final String EXTRA_PARAM2 = "com.example.user.myapp.extra.PARAM2"; - public MyIntentService() { super("MyIntentService"); } - private void sendBooleanListToActivity(List booleanList, String intentName) { + private void sendBooleanListToActivity(List booleanList, String intentName, String bundleName) { Intent intent = new Intent(intentName); Bundle bundle = new Bundle(); - - bundle.putBooleanArray("coilValues", toPrimitiveArray(booleanList)); + bundle.putBooleanArray(bundleName, toPrimitiveArray(booleanList)); intent.putExtras(bundle); sendLocationBroadcast(intent); } @@ -70,10 +67,10 @@ public class MyIntentService extends IntentService { BitVector bv = master.readInputDiscretes(ref, count); List listBooleanBit = new ArrayList<>(); for (int i = 0; i < count; i++) { + System.out.println(i+" discret input "+bv.getBit(i)); listBooleanBit.add(bv.getBit(i)); - System.out.println("input discrete values : "+i+" - "+bv.getBit(i)); } - sendBooleanListToActivity(listBooleanBit, intentReceiveName); + sendBooleanListToActivity(listBooleanBit, intentReceiveName, "discreteInputValues"); master.disconnect(); } catch (Exception e) { System.out.println("Exception in reading discrete input " + e); @@ -88,10 +85,10 @@ public class MyIntentService extends IntentService { BitVector bv = master.readCoils(ref,count); List listBooleanBit = new ArrayList<>(); for (int i = 0; i < count; i++) { - System.out.println("coil values : "+i+" - "+bv.getBit(i)); + System.out.println(i+" coil "+bv.getBit(i)); listBooleanBit.add(bv.getBit(i)); } - sendBooleanListToActivity(listBooleanBit, intentReceiveName); + sendBooleanListToActivity(listBooleanBit, intentReceiveName, "coilValues"); master.disconnect(); } catch (Exception e) { System.out.println("Exception in reading coil " + e); @@ -106,8 +103,8 @@ public class MyIntentService extends IntentService { InputRegister [] inputRegisters = master.readInputRegisters(ref, count); ArrayList listIntegerInputRegister = new ArrayList<>(); for (InputRegister ir : inputRegisters) { - System.out.println("ir values : "+Math.random()+" - "+ir.getValue()); listIntegerInputRegister.add(ir.getValue()); + System.out.println(ir.getValue()+" input register "+Math.random()); } sendIntegerListToActivity (listIntegerInputRegister, intentReceiveName); master.disconnect(); @@ -129,10 +126,13 @@ public class MyIntentService extends IntentService { switch(action) { case ACTION_READ_DISCRETE_INPUT: readDiscreteInputAction (ipAddress, port, ref, count, intentName); + break; case ACTION_READ_COIL : readCoilAction(ipAddress, port, ref, count, intentName); + break; case ACTION_READ_INPUT_REGISTER : readInputRegisterAction (ipAddress, port,ref, count, intentName); + break; } } } diff --git a/app/src/main/res/layout/activity_display_message.xml b/app/src/main/res/layout/activity_display_message.xml index 408d105..55ecec5 100644 --- a/app/src/main/res/layout/activity_display_message.xml +++ b/app/src/main/res/layout/activity_display_message.xml @@ -6,18 +6,6 @@ android:layout_height="match_parent" tools:context=".DisplayMessageActivity"> - - + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file