|
28 | 28 | import org.opcfoundation.ua.builtintypes.ExtensionObject; |
29 | 29 | import org.opcfoundation.ua.builtintypes.NodeId; |
30 | 30 | import org.opcfoundation.ua.builtintypes.StatusCode; |
| 31 | +import org.opcfoundation.ua.builtintypes.UnsignedByte; |
31 | 32 | import org.opcfoundation.ua.builtintypes.UnsignedInteger; |
32 | 33 | import org.opcfoundation.ua.builtintypes.Variant; |
33 | 34 | import org.opcfoundation.ua.core.Attributes; |
34 | 35 | import org.opcfoundation.ua.core.CreateMonitoredItemsRequest; |
| 36 | +import org.opcfoundation.ua.core.CreateSubscriptionRequest; |
35 | 37 | import org.opcfoundation.ua.core.DataChangeFilter; |
36 | 38 | import org.opcfoundation.ua.core.DataChangeTrigger; |
37 | 39 | import org.opcfoundation.ua.core.DeadbandType; |
|
47 | 49 |
|
48 | 50 | import OpcUtils.BrowseDataStamp; |
49 | 51 | import OpcUtils.ConnectionThread.ThreadCreateMonitoredItem; |
| 52 | +import OpcUtils.ConnectionThread.ThreadCreateSubscription; |
50 | 53 | import OpcUtils.ConnectionThread.ThreadRead; |
51 | 54 | import OpcUtils.ConnectionThread.ThreadWrite; |
52 | 55 | import OpcUtils.ManagerOPC; |
53 | 56 | import OpcUtils.SessionElement; |
54 | 57 | import OpcUtils.SubscriptionElement; |
55 | 58 | import tool.ui.NodeAdapter; |
| 59 | +import tool.ui.SubscriptionAdapter; |
56 | 60 |
|
57 | 61 | public class BrowseFragment extends Fragment { |
58 | 62 | Bundle bundle; |
@@ -131,16 +135,100 @@ public void onClick(DialogInterface dialog, int which) { |
131 | 135 | return view; |
132 | 136 | } |
133 | 137 |
|
134 | | - private void createMonItem(int position){ |
135 | | - |
136 | | - |
137 | | - if(sessionElement.getSubscriptions().isEmpty()){ |
138 | | - //creo sottoscrizione |
139 | | - }else{ |
140 | | - //chiedo quale sottoscrizione usare |
| 138 | + private void createMonItem(final int position) { |
| 139 | + if (sessionElement.getSubscriptions().isEmpty()) { |
| 140 | + Toast.makeText(getContext(),getString(R.string.createSubscriptionMon),Toast.LENGTH_LONG).show(); |
| 141 | + |
| 142 | + final Dialog dialog_subscription = new Dialog(getContext(), R.style.AppAlert); |
| 143 | + dialog_subscription.setContentView(R.layout.dialog_insertdatasubscription); |
| 144 | + |
| 145 | + final EditText edtRequestedPublishInteval = dialog_subscription.findViewById(R.id.edtRequestedPublishingInterval); |
| 146 | + final EditText edtRequestedMaxKeepAliveCount = dialog_subscription.findViewById(R.id.edtRequestedMaxKeepAliveCount); |
| 147 | + final EditText edtRequestedLifetimeCount = dialog_subscription.findViewById(R.id.edtRequestedLifetimeCount); |
| 148 | + final EditText edtMaxNotificationPerPublish = dialog_subscription.findViewById(R.id.edtMaxNotificationPerPublish); |
| 149 | + final EditText edtPriority = dialog_subscription.findViewById(R.id.edtPriotity); |
| 150 | + final CheckBox checkPublishingEnable = dialog_subscription.findViewById(R.id.checkPublishingEnable); |
| 151 | + Button btnOkSubscription = dialog_subscription.findViewById(R.id.btnOkSubscription); |
| 152 | + |
| 153 | + edtRequestedLifetimeCount.setHint("Ex: " + ManagerOPC.Default_RequestedLifetimeCount); |
| 154 | + edtMaxNotificationPerPublish.setHint("Ex: " + ManagerOPC.Default_MaxNotificationsPerPublish); |
| 155 | + edtRequestedPublishInteval.setHint("Ex: " + ManagerOPC.Default_RequestedPublishingInterval); |
| 156 | + edtRequestedMaxKeepAliveCount.setHint("Ex: " + ManagerOPC.Default_RequestedMaxKeepAliveCount); |
| 157 | + edtPriority.setHint("Ex: " + ManagerOPC.Default_Priority); |
| 158 | + |
| 159 | + btnOkSubscription.setOnClickListener(new View.OnClickListener() { |
| 160 | + @Override |
| 161 | + public void onClick(View v) { |
| 162 | + Double requestedPublishInterval; |
| 163 | + UnsignedInteger requestedLifetimeCount; |
| 164 | + UnsignedInteger requestedMaxKeepAliveCount; |
| 165 | + UnsignedInteger maxNotificationPerPublish; |
| 166 | + UnsignedByte priority; |
| 167 | + boolean publishingEnabled; |
| 168 | + |
| 169 | + if (edtRequestedLifetimeCount.getText().toString().length() == 0 || |
| 170 | + edtMaxNotificationPerPublish.getText().toString().length() == 0 || |
| 171 | + edtRequestedPublishInteval.getText().toString().length() == 0 || |
| 172 | + edtRequestedMaxKeepAliveCount.getText().toString().length() == 0 || |
| 173 | + edtPriority.getText().toString().length() == 0) { |
| 174 | + Toast.makeText(getContext(), R.string.InserisciValoriValidi, Toast.LENGTH_LONG).show(); |
| 175 | + } else { |
| 176 | + requestedLifetimeCount = new UnsignedInteger(edtRequestedLifetimeCount.getText().toString()); |
| 177 | + maxNotificationPerPublish = new UnsignedInteger(edtMaxNotificationPerPublish.getText().toString()); |
| 178 | + requestedPublishInterval = Double.parseDouble(edtRequestedPublishInteval.getText().toString()); |
| 179 | + requestedMaxKeepAliveCount = new UnsignedInteger(edtRequestedMaxKeepAliveCount.getText().toString()); |
| 180 | + priority = new UnsignedByte(edtPriority.getText().toString()); |
| 181 | + publishingEnabled = checkPublishingEnable.isChecked(); |
| 182 | + if (requestedLifetimeCount.intValue() >= 3 * requestedMaxKeepAliveCount.intValue()) { |
| 183 | + CreateSubscriptionRequest req = new CreateSubscriptionRequest(null, requestedPublishInterval, requestedLifetimeCount, requestedMaxKeepAliveCount, maxNotificationPerPublish, publishingEnabled, priority); |
| 184 | + ThreadCreateSubscription t = new ThreadCreateSubscription(sessionElement, req); |
| 185 | + final ProgressDialog progressDialog = ProgressDialog.show(getContext(),getString(R.string.TentativoDiConnessione), getString(R.string.CreazioneSottoscrizione), true); |
| 186 | + @SuppressLint("HandlerLeak") Handler handler_subscription = new Handler() { |
| 187 | + @Override |
| 188 | + public void handleMessage(Message msg) { |
| 189 | + progressDialog.dismiss(); |
| 190 | + if (msg.what == -1) { |
| 191 | + Toast.makeText(getContext(), getString(R.string.SottoscrizioneFallita) + ((StatusCode) msg.obj).getDescription() + "\nCode: " + ((StatusCode) msg.obj).getValue().toString(), Toast.LENGTH_LONG).show(); |
| 192 | + } else if (msg.what == -2) { |
| 193 | + Toast.makeText(getContext(), R.string.ServerDown, Toast.LENGTH_LONG).show(); |
| 194 | + } else { |
| 195 | + int position = (int) msg.obj; |
| 196 | + createMonitoredItemWithSubscription(sessionElement.getSubscriptions().get(position), position); |
| 197 | + } |
| 198 | + } |
| 199 | + }; |
| 200 | + dialog_subscription.dismiss(); |
| 201 | + t.start(handler_subscription); |
| 202 | + } else { |
| 203 | + Toast.makeText(getContext(), getString(R.string.DeviRispettareVincolo) + "\nLifetimeCount>3*MaxKeepAliveCount", Toast.LENGTH_LONG).show(); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + }); |
| 208 | + |
| 209 | + dialog_subscription.show(); |
| 210 | + |
| 211 | + } else { |
| 212 | + ListView listSubscriptions; |
| 213 | + SubscriptionAdapter adapter; |
| 214 | + |
| 215 | + final Dialog dialog = new Dialog(getContext(), R.style.AppAlert); |
| 216 | + dialog.setContentView(R.layout.activity_list_subscription); |
| 217 | + listSubscriptions = dialog.findViewById(R.id.listSubscriptions); |
| 218 | + adapter = new SubscriptionAdapter(getContext(), R.layout.list_subscriptions, sessionElement.getSubscriptions()); |
| 219 | + listSubscriptions.setAdapter(adapter); |
| 220 | + listSubscriptions.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 221 | + @Override |
| 222 | + public void onItemClick(AdapterView<?> parent, View view, int subPosition, long id) { |
| 223 | + createMonitoredItemWithSubscription(sessionElement.getSubscriptions().get(subPosition), position); |
| 224 | + dialog.dismiss(); |
| 225 | + } |
| 226 | + }); |
| 227 | + dialog.show(); |
141 | 228 | } |
142 | | - final SubscriptionElement subscriptionElement = sessionElement.getSubscriptions().get(0); //todo gestire ricerca ed eventuale creazione sottoscrizione |
| 229 | + } |
143 | 230 |
|
| 231 | + private void createMonitoredItemWithSubscription(final SubscriptionElement subscriptionElement, int position) { |
144 | 232 | final MonitoredItemCreateRequest[] monitoredItems = new MonitoredItemCreateRequest[1]; |
145 | 233 | monitoredItems[0] = new MonitoredItemCreateRequest(); |
146 | 234 |
|
@@ -175,7 +263,7 @@ private void createMonItem(int position){ |
175 | 263 |
|
176 | 264 | int namespace, nodeid; |
177 | 265 | String nodeid_String; |
178 | | - double sampling_interval,deadband; |
| 266 | + double sampling_interval, deadband; |
179 | 267 | UnsignedInteger queue_size; |
180 | 268 | boolean discard_oldest; |
181 | 269 |
|
@@ -248,7 +336,7 @@ public void onClick(View v) { |
248 | 336 | mi.setItemsToCreate(monitoredItems); |
249 | 337 |
|
250 | 338 | ThreadCreateMonitoredItem t = new ThreadCreateMonitoredItem(subscriptionElement, mi); |
251 | | - final ProgressDialog progressDialog = ProgressDialog.show( getContext(), getString(R.string.TentativoDiConnessione), getString(R.string.CreazioneMonItemInCorso), true); |
| 339 | + final ProgressDialog progressDialog = ProgressDialog.show(getContext(), getString(R.string.TentativoDiConnessione), getString(R.string.CreazioneMonItemInCorso), true); |
252 | 340 | @SuppressLint("HandlerLeak") Handler handler_monitoreditem = new Handler() { |
253 | 341 | @Override |
254 | 342 | public void handleMessage(Message msg) { |
@@ -394,7 +482,7 @@ private void writeNode(int position) { |
394 | 482 | public void onClick(View v) { |
395 | 483 | int namespace, nodeid; |
396 | 484 | String nodeid_string; |
397 | | - Variant value_write= null; |
| 485 | + Variant value_write = null; |
398 | 486 |
|
399 | 487 | if (edtnamespace_write.getText().toString().length() == 0 || edtnodeid_write.getText().toString().length() == 0 || edtvalue_write.getText().toString().length() == 0) { |
400 | 488 | Toast.makeText(getContext(), R.string.InserisciValoriValidi, Toast.LENGTH_SHORT).show(); |
|
0 commit comments