@Override public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception { if (MQTTMessageWrapper.isMyMessage(mqttMessage.toString())) return; String command = MQTTMessageWrapper.getCommand(mqttMessage.toString()); switch (command) { case MQTTMessageWrapper.RcommandGetListDevice: // fine-tuning the GUI JSONObject jason = new JSONObject(mqttMessage.toString()); JSONArray deviceList = jason.getJSONArray("devicesList"); devices = new ArrayList<>(); adapter.clear(); for (int i = 0; i < deviceList.length(); i++) { JSONObject device = deviceList.getJSONObject(i); String friendlyName = (String) device.get("FriendlyName"); // String index = new String(i + 1); devices.add(new Device(i + 1, friendlyName + " " + String.valueOf(i + 1), false, true)); // Iterate through the elements of the array i. // Get thier value. // Get the value for the first element and the value for the last element. } // String friendlyName = (String) jason.get("FriendlyName"); // devices.add(new Device(1, "Z-Wave Light Bulb 1", false, true)); // devices.add(new Device(1, "Z-Wave Light Bulb 2", false, true)); ListView list = (ListView) findViewById(R.id.list); adapter = new Adapter(this, devices); list.setAdapter(adapter); break; case MQTTMessageWrapper.RcommandAddDevice: // Add new device break; default: break; } Log.d(TAG, "Received data from topic: " + topic + ", Mqtt message: " + mqttMessage.toString()); if (adapter != null) { Device device = adapter.getItem(0); if (mqttMessage.toString().equals("on")) { device.setTurnOn(true); } else if (mqttMessage.toString().equals("off")) { device.setTurnOn(false); } adapter.notifyDataSetChanged(); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.option_menu_shareTopic_searching: alljoyn_wrapper.mBusHandler.sendEmptyMessage(alljoyn_wrapper.mBusHandler.CONNECT); mHandler.sendEmptyMessage(alljoyn_wrapper.MESSAGE_ALLJOYN_START_PROGRESS_DIALOG); return true; case R.id.option_menu_shareTopic_get_topic: Message msg = alljoyn_wrapper.mBusHandler.obtainMessage( AlljoynWrapper.BusHandler.GET_TOPIC, alljoyn_wrapper.keyValue); alljoyn_wrapper.mBusHandler.sendMessage(msg); return true; case R.id.option_menu_inviteShare_genTopic: { HttpWrapper http_request = new HttpWrapper(); http_request.topic = topic; http_request.setOutputListener(this); SettingActivity.cur_command = SettingActivity.COMMAND_INVITESHARE_GEN_PINCODE; String url = new String("https://"); url += inviteSRV + "/generateInvite"; http_request.execute(url); } return true; case R.id.option_menu_inviteShare_getTopic: { HttpWrapper http_request = new HttpWrapper(); http_request.setOutputListener(this); SettingActivity.cur_command = SettingActivity.COMMAND_INVITESHARE_GET_TOPIC; String url = new String("https://"); url += inviteSRV + "/getTopic/" + pincode; http_request.execute(url); } return true; case R.id.option_menu_setting: Intent intent1 = new Intent(this, SettingActivity.class); intent1.putExtra(share_invite_srv, inviteSRV); intent1.putExtra(share_mqtt_srv, mqttSRV); intent1.putExtra(share_topic, topic); intent1.putExtra(share_pin, pincode); intent1.putExtra(share_key, alljoyn_wrapper.keyValue); startActivityForResult(intent1, INTENT_SETTING_RESULT_CODE); return true; case R.id.option_menu_remoteAccess_get_list: // Get list of menu item MqttMessage message = null; try { message = MQTTMessageWrapper.CreateGetListMsg(); } catch (JSONException e) { e.printStackTrace(); } try { MainActivity.client.publish(topic, message); } catch (MqttException e) { Log.d(MainActivity.TAG, "Publish error with message: " + e.getMessage()); } return true; case R.id.option_menu_help: return true; case R.id.option_menu_wifi_setup_scan: Intent intent2 = new Intent(this, ScanWIFIActivity.class); startActivity(intent2); return true; default: return super.onOptionsItemSelected(item); } }