Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Restructure callbacks and device pass through
Browse files Browse the repository at this point in the history
  • Loading branch information
cutoffthetop committed Jun 20, 2017
1 parent c088655 commit 7ee1a93
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

import java.util.List;

import blue.happening.HappeningClient;
import blue.happening.dashboard.R;
import blue.happening.dashboard.fragment.DashboardFragment;
import blue.happening.dashboard.model.DashboardModel;


public class DashboardAdapter extends ArrayAdapter<DashboardModel> {
public class DashboardAdapter extends ArrayAdapter<HappeningClient> {

public DashboardAdapter(Context context, List<DashboardModel> models) {
public DashboardAdapter(Context context, List<HappeningClient> models) {
super(context, 0, models);
}

@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {

DashboardModel dashboardModel = getItem(position);
HappeningClient happeningClient = getItem(position);

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.dashboard_model, parent, false);
Expand All @@ -36,19 +36,21 @@ public View getView(final int position, View convertView, @NonNull ViewGroup par
TextView title = (TextView) convertView.findViewById(R.id.dashboard_model_title);
TextView message = (TextView) convertView.findViewById(R.id.dashboard_model_message);

if (dashboardModel != null) {
title.setText(dashboardModel.getTitle());
message.setText(dashboardModel.getMessage());
if (happeningClient != null) {
title.setText(happeningClient.getName());
message.setText(happeningClient.getUuid());
}

convertView.findViewById(R.id.dashboard_button_send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Send", Toast.LENGTH_SHORT).show();
String message = "Hey there, i'm using happening!";
DashboardModel model = getItem(position);
Log.d(getClass().getSimpleName(), "Sending Message to " + model.getMessage());
DashboardFragment.getInstance().getHappening().sendDataTo(model.getMessage(), message.getBytes());
HappeningClient client = getItem(position);
if (client != null) {
Log.d(getClass().getSimpleName(), "Sending Message to " + client.getUuid());
DashboardFragment.getInstance().getHappening().sendMessage(message.getBytes(), client);
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,37 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import blue.happening.HappeningClient;
import blue.happening.dashboard.R;
import blue.happening.dashboard.adapter.DashboardAdapter;
import blue.happening.dashboard.model.DashboardModel;
import blue.happening.sdk.Happening;
import blue.happening.sdk.HappeningCallback;

public class DashboardFragment extends Fragment {

private static DashboardFragment instance = null;
private final String TAG = this.getClass().getSimpleName();
private List<DashboardModel> dashboardModels = new ArrayList<>();

private static DashboardFragment instance = null;
private List<HappeningClient> dashboardClients;
private DashboardAdapter dashboardAdapter;
ListView listView;
private ListView listView;
private Happening happening;

private Happening happening = new Happening();
public DashboardFragment() {
happening = new Happening();
dashboardClients = new ArrayList<>();
}

public static DashboardFragment getInstance() {
if (instance == null)
instance = new DashboardFragment();
return instance;
}

public Happening getHappening() {
return happening;
Expand All @@ -37,44 +48,40 @@ public Happening getHappening() {
private HappeningCallback happeningCallback = new HappeningCallback() {
// TODO: These callback methods don't do anything useful yet.
@Override
public void onClientAdded(String client) {
Log.v(this.getClass().getSimpleName(), "onClientAdded " + client);
dashboardModels.add(new DashboardModel("add", client));
public void onClientAdded(HappeningClient client) {
dashboardClients.add(client);
dashboardAdapter.notifyDataSetChanged();
}

@Override
public void onClientUpdated(String client) {
Log.v(this.getClass().getSimpleName(), "onClientUpdated " + client);
dashboardModels.add(new DashboardModel("update", client));
public void onClientUpdated(HappeningClient client) {
onClientRemoved(client);
onClientAdded(client);
dashboardAdapter.notifyDataSetChanged();
}

@Override
public void onClientRemoved(String client) {
Log.v(this.getClass().getSimpleName(), "onClientRemoved " + client);
dashboardModels.add(new DashboardModel("remove", client));
public void onClientRemoved(HappeningClient client) {
HappeningClient removeDevice = null;
for (HappeningClient candidate : dashboardClients) {
if (candidate.getUuid().equals(client.getUuid())) {
removeDevice = candidate;
}
}
if (removeDevice != null) {
dashboardClients.remove(removeDevice);
}
dashboardAdapter.notifyDataSetChanged();
}

@Override
public void onMessageReceived(byte[] message, int deviceId) {
Log.d(TAG, "onMessageReceived: " + new String(message));
// Toast.makeText(MyApplication.getAppContext(), String.valueOf(message), Toast.LENGTH_SHORT).show();
public void onMessageReceived(byte[] message, HappeningClient source) {
Log.v(TAG, "onMessageReceived: " + new String(message) + " from " + source.getUuid());
Context context = getActivity().getApplicationContext();
Toast.makeText(context, new String(message), Toast.LENGTH_LONG).show();
}
};

public DashboardFragment() {

}

public static DashboardFragment getInstance() {
if (instance == null)
instance = new DashboardFragment();

return instance;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
Expand All @@ -86,25 +93,23 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
@Override
public void onClick(View v) {
Log.v(this.getClass().getSimpleName(), "onClick");
List<HappeningClient> devices = happening.getDevices();

dashboardModels.clear();
for (HappeningClient device : devices) {
dashboardModels.add(new DashboardModel(device.getClientName(), device.getClientId()));
Log.d(TAG, "onClick: " + device.getClientName());
List<HappeningClient> clients = happening.getClients();
dashboardClients.clear();
for (HappeningClient client : clients) {
dashboardClients.add(client);
Log.d(TAG, "onClick: " + client.getName());
}
dashboardAdapter.notifyDataSetChanged();
}
});

dashboardAdapter = new DashboardAdapter(container.getContext(), dashboardModels);
dashboardAdapter = new DashboardAdapter(container.getContext(), dashboardClients);
listView = (ListView) rootView.findViewById(R.id.dashboard_model_list);
listView.setAdapter(dashboardAdapter);

Context context = getActivity().getApplicationContext();
happening.register(context, happeningCallback);


return rootView;
}

Expand All @@ -114,5 +119,4 @@ public void onDestroyView() {
super.onDestroyView();
happening.deregister();
}

}

This file was deleted.

3 changes: 1 addition & 2 deletions mesh/src/main/java/blue/happening/mesh/ILayerCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public interface ILayerCallback {

public void onDeviceRemoved(RemoteDevice remoteDevice);

public void onMessageReceived(byte[] bytes);

public void onMessageReceived(byte[] message);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package blue.happening.mesh;

public interface IMeshHandlerCallback {

void onMessageReceived(byte[] message);
public interface IMeshHandlerCallback {

void onDeviceAdded(MeshDevice meshDevice);

void onDeviceUpdated(MeshDevice meshDevice);

void onDeviceRemoved(MeshDevice meshDevice);

void onMessageReceived(byte[] message, MeshDevice source);
}
21 changes: 21 additions & 0 deletions mesh/src/main/java/blue/happening/mesh/IRemoteDevice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package blue.happening.mesh;


public interface IRemoteDevice extends Comparable<IRemoteDevice> {

public String getUuid();

public boolean isNeighbour();

public float getTq();

public float getEq();

public float getRq();

public boolean sendMessage(Message message);

public boolean remove();

public boolean equals(Object object);
}
11 changes: 6 additions & 5 deletions mesh/src/main/java/blue/happening/mesh/MeshHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public List<MeshDevice> getDevices() {
return meshDevices;
}

public boolean sendMessage(String uuid, byte[] bytes) {
System.out.println("MeshHandler sendMessage " + new String(bytes) + " to " + uuid);
public boolean sendMessage(byte[] message, String uuid) {
System.out.println("MeshHandler sendMessage " + new String(message) + " to " + uuid);
String s = "";
for (Map.Entry<String, RemoteDevice> stringRemoteDeviceEntry : routingTable.entrySet()) {
s += stringRemoteDeviceEntry.getKey() + ", ";
Expand All @@ -85,8 +85,8 @@ public boolean sendMessage(String uuid, byte[] bytes) {
} else {
RemoteDevice bestNeighbour = routingTable.getBestNeighbourForRemoteDevice(remoteDevice);
if (bestNeighbour != null) {
Message message = new Message(this.uuid, uuid, INITIAL_MIN_SEQUENCE, MESSAGE_TYPE_UCM, bytes);
return bestNeighbour.sendMessage(message);
Message ucm = new Message(this.uuid, uuid, INITIAL_MIN_SEQUENCE, MESSAGE_TYPE_UCM, message);
return bestNeighbour.sendMessage(ucm);
} else {
return false;
}
Expand Down Expand Up @@ -158,7 +158,8 @@ public void onMessageReceived(byte[] bytes) {
}

if (propagate != null) {
meshHandlerCallback.onMessageReceived(message.getBody());
MeshDevice source = routingTable.get(message.getSource()).getMeshDevice();
meshHandlerCallback.onMessageReceived(message.getBody(), source);
}

RemoteDevice source = routingTable.get(message.getSource());
Expand Down
Loading

0 comments on commit 7ee1a93

Please sign in to comment.