1818import android .widget .AdapterView ;
1919import android .widget .ArrayAdapter ;
2020import android .widget .Button ;
21+ import android .widget .CheckBox ;
2122import android .widget .EditText ;
2223import android .widget .ListView ;
2324import android .widget .RadioGroup ;
2425import android .widget .Spinner ;
2526import android .widget .Toast ;
2627
28+ import org .opcfoundation .ua .builtintypes .ExtensionObject ;
29+ import org .opcfoundation .ua .builtintypes .NodeId ;
2730import org .opcfoundation .ua .builtintypes .StatusCode ;
31+ import org .opcfoundation .ua .builtintypes .UnsignedInteger ;
2832import org .opcfoundation .ua .builtintypes .Variant ;
2933import org .opcfoundation .ua .core .Attributes ;
34+ import org .opcfoundation .ua .core .CreateMonitoredItemsRequest ;
35+ import org .opcfoundation .ua .core .DataChangeFilter ;
36+ import org .opcfoundation .ua .core .DataChangeTrigger ;
37+ import org .opcfoundation .ua .core .DeadbandType ;
38+ import org .opcfoundation .ua .core .MonitoredItemCreateRequest ;
39+ import org .opcfoundation .ua .core .MonitoringMode ;
40+ import org .opcfoundation .ua .core .MonitoringParameters ;
3041import org .opcfoundation .ua .core .ReadResponse ;
42+ import org .opcfoundation .ua .core .ReadValueId ;
3143import org .opcfoundation .ua .core .TimestampsToReturn ;
3244import org .opcfoundation .ua .core .WriteResponse ;
3345
3446import java .util .ArrayList ;
3547
3648import OpcUtils .BrowseDataStamp ;
49+ import OpcUtils .ConnectionThread .ThreadCreateMonitoredItem ;
3750import OpcUtils .ConnectionThread .ThreadRead ;
3851import OpcUtils .ConnectionThread .ThreadWrite ;
3952import OpcUtils .ManagerOPC ;
@@ -84,7 +97,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
8497 listNode .setOnItemLongClickListener (new AdapterView .OnItemLongClickListener () {
8598 @ Override
8699 public boolean onItemLongClick (AdapterView <?> adapterView , View view , final int position , long l ) {
87- String [] colors = {"Read" , "Write" , "Browse" };
100+ String [] colors = {"Read" , "Write" , "Browse" , "Monitor this node" };
88101
89102 AlertDialog .Builder builder = new AlertDialog .Builder (getContext ());
90103 builder .setTitle (getString (R .string .menu_azioni ));
@@ -101,6 +114,9 @@ public void onClick(DialogInterface dialog, int which) {
101114 case 2 :
102115 ((BrowseActivity ) getActivity ()).browseToPosition (position );
103116 break ;
117+ case 3 :
118+ createMonItem (position );
119+ break ;
104120 default :
105121 dialog .cancel ();
106122 }
@@ -114,6 +130,143 @@ public void onClick(DialogInterface dialog, int which) {
114130 return view ;
115131 }
116132
133+ private void createMonItem (int position ){
134+
135+ final MonitoredItemCreateRequest [] monitoredItems = new MonitoredItemCreateRequest [1 ];
136+ monitoredItems [0 ] = new MonitoredItemCreateRequest ();
137+
138+ final Dialog dialog = new Dialog (getContext (), R .style .AppAlert );
139+ dialog .setContentView (R .layout .dialog_createmonitoreditem );
140+ final Spinner timestamps = dialog .findViewById (R .id .spinnerTimestamp );
141+ ArrayAdapter <CharSequence > spinneradapter = ArrayAdapter .createFromResource (getContext (), R .array .timestamps , android .R .layout .simple_spinner_dropdown_item );
142+ timestamps .setAdapter (spinneradapter );
143+
144+ final EditText edtMonitoredNamespace = dialog .findViewById (R .id .edtMonitoredNamespace );
145+ final EditText edtMonitoredNodeID = dialog .findViewById (R .id .edtMonitoredNodeID );
146+ final EditText edtMonitoredSampling = dialog .findViewById (R .id .edtSamplingInterval );
147+ final EditText edtMonitoredQueue = dialog .findViewById (R .id .edtQueueSize );
148+ final CheckBox checkDiscardOldest = dialog .findViewById (R .id .checkDiscardOldest );
149+ final RadioGroup rdgroupfiltro = dialog .findViewById (R .id .rdgroupDeadband );
150+ final EditText edtValDeadband = dialog .findViewById (R .id .edtValDeadband );
151+
152+ edtMonitoredNamespace .setText (dati .get (position ).namespace );
153+ edtMonitoredNodeID .setText (dati .get (position ).nodeindex );
154+ edtMonitoredNamespace .setEnabled (false );
155+ edtMonitoredNodeID .setEnabled (false );
156+ edtMonitoredNamespace .setFocusable (false );
157+ edtMonitoredNodeID .setFocusable (false );
158+
159+ edtMonitoredSampling .setHint ("Ex: " + ManagerOPC .Default_SamplingInterval );
160+ edtMonitoredQueue .setHint ("Ex: " + ManagerOPC .Default_QueueSize + "" );
161+ edtValDeadband .setHint ("Ex: " + ManagerOPC .Default_AbsoluteDeadBand + "" );
162+
163+ Button btnOkMonitored = dialog .findViewById (R .id .btnOkMonitoredItem );
164+
165+ btnOkMonitored .setOnClickListener (new View .OnClickListener () {
166+
167+ int namespace , nodeid ;
168+ String nodeid_String ;
169+ double sampling_interval ,deadband ;
170+ UnsignedInteger queue_size ;
171+ boolean discard_oldest ;
172+
173+ @ Override
174+ public void onClick (View v ) {
175+ TimestampsToReturn timestamp = null ;
176+ switch (timestamps .getSelectedItem ().toString ()) {
177+ case "Server" :
178+ timestamp = TimestampsToReturn .Server ;
179+ break ;
180+ case "Source" :
181+ timestamp = TimestampsToReturn .Source ;
182+ break ;
183+ case "Both" :
184+ timestamp = TimestampsToReturn .Both ;
185+ break ;
186+ case "Neither" :
187+ timestamp = TimestampsToReturn .Neither ;
188+ break ;
189+ }
190+
191+
192+ if (edtMonitoredNamespace .getText ().toString ().length () != 0 && edtMonitoredNodeID .getText ().toString ().length () != 0 && edtMonitoredSampling .getText ().toString ().length () != 0
193+ && edtMonitoredQueue .getText ().toString ().length () != 0 && edtValDeadband .getText ().toString ().length () != 0 ) {
194+ namespace = Integer .parseInt (edtMonitoredNamespace .getText ().toString ());
195+ try {
196+ nodeid = Integer .parseInt (edtMonitoredNodeID .getText ().toString ());
197+ nodeid_String = null ;
198+ } catch (Exception e ) {
199+ nodeid_String = edtMonitoredNodeID .getText ().toString ();
200+ }
201+ sampling_interval = Double .parseDouble (edtMonitoredSampling .getText ().toString ());
202+ queue_size = new UnsignedInteger (edtMonitoredQueue .getText ().toString ());
203+ discard_oldest = checkDiscardOldest .isChecked ();
204+ DeadbandType deadbandType = null ;
205+ switch (rdgroupfiltro .getCheckedRadioButtonId ()) {
206+ case R .id .rdAbsolute :
207+ deadbandType = DeadbandType .Absolute ;
208+ break ;
209+ case R .id .rdPercentage :
210+ deadbandType = DeadbandType .Percent ;
211+ break ;
212+ }
213+ deadband = Double .parseDouble (edtValDeadband .getText ().toString ());
214+
215+ DataChangeFilter filter = new DataChangeFilter ();
216+ filter .setTrigger (DataChangeTrigger .StatusValue );
217+ filter .setDeadbandType (new UnsignedInteger (deadbandType .getValue ()));
218+ filter .setDeadbandValue (deadband );
219+ ExtensionObject fil = new ExtensionObject (filter );
220+
221+ MonitoringParameters reqParams = new MonitoringParameters ();
222+ reqParams .setClientHandle (new UnsignedInteger (SubscriptionActivity .idchandle ++));
223+ reqParams .setSamplingInterval (sampling_interval );
224+ reqParams .setQueueSize (queue_size );
225+ reqParams .setDiscardOldest (discard_oldest );
226+ reqParams .setFilter (fil );
227+ monitoredItems [0 ].setRequestedParameters (reqParams );
228+ monitoredItems [0 ].setMonitoringMode (MonitoringMode .Reporting );
229+ NodeId nodeId ;
230+ if (nodeid_String == null )
231+ nodeId = new NodeId (namespace , nodeid );
232+ else
233+ nodeId = new NodeId (namespace , nodeid_String );
234+ monitoredItems [0 ].setItemToMonitor (new ReadValueId (nodeId , Attributes .Value , null , null ));
235+
236+ final CreateMonitoredItemsRequest mi = new CreateMonitoredItemsRequest ();
237+ mi .setSubscriptionId (sessionElement .getSubscriptions ().get (0 ).getSubscription ().getSubscriptionId ()); //FIXME gestire creazione e ricerca sottoscrizioni
238+ mi .setTimestampsToReturn (timestamp );
239+ mi .setItemsToCreate (monitoredItems );
240+
241+ ThreadCreateMonitoredItem t = new ThreadCreateMonitoredItem (sessionElement .getSubscriptions ().get (0 ), mi ); //FIXME gestire creazione e ricerca sottoscrizioni
242+ final ProgressDialog progressDialog = ProgressDialog .show ( getContext (), getString (R .string .TentativoDiConnessione ), getString (R .string .CreazioneMonItemInCorso ), true );
243+ @ SuppressLint ("HandlerLeak" ) Handler handler_monitoreditem = new Handler () {
244+ @ Override
245+ public void handleMessage (Message msg ) {
246+ progressDialog .dismiss ();
247+ if (msg .what == -1 ) {
248+ Toast .makeText (getContext (), getString (R .string .ErroreSconosciuto ) + ((StatusCode ) msg .obj ).getDescription () + "\n Code: " + ((StatusCode ) msg .obj ).getValue ().toString (), Toast .LENGTH_LONG ).show ();
249+ } else if (msg .what == -2 ) {
250+ Toast .makeText (getContext (), R .string .ServerDown , Toast .LENGTH_LONG ).show ();
251+ } else if (msg .what == -3 ) {
252+ Toast .makeText (getContext (), getString (R .string .ErroreToast ) + msg .obj .toString (), Toast .LENGTH_LONG ).show ();
253+ } else {
254+ //TODO gestire creazione con successo
255+ Toast .makeText (getContext (), "Creato con successo" , Toast .LENGTH_LONG ).show ();
256+ }
257+ }
258+ };
259+ t .start (handler_monitoreditem );
260+ dialog .dismiss ();
261+ } else {
262+ Toast .makeText (getContext (), R .string .InserisciValoriValidi , Toast .LENGTH_LONG ).show ();
263+ }
264+ }
265+ });
266+
267+ dialog .show ();
268+ }
269+
117270 private void readNode (int position ) {
118271 final Dialog dialogRead = new Dialog (getContext (), R .style .AppAlert );
119272 dialogRead .setContentView (R .layout .dialog_inserdataread );
0 commit comments