Skip to content

Commit

Permalink
wip: adding group support for v47 firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
nashira committed Dec 14, 2014
1 parent ee05cb7 commit e793ff7
Show file tree
Hide file tree
Showing 23 changed files with 350 additions and 143 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions .idea/libraries/support_v4_21_0_2.xml

This file was deleted.

11 changes: 11 additions & 0 deletions .idea/libraries/support_v4_21_0_3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="okhttp-urlconnection-2.0.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.2" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.2" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
<orderEntry type="library" exported="" name="retrofit-1.7.1" level="project" />
<orderEntry type="library" exported="" name="okio-1.0.0" level="project" />
<orderEntry type="library" exported="" name="greendao-1.3.1" level="project" />
<orderEntry type="library" exported="" name="joda-time-2.5" level="project" />
<orderEntry type="library" exported="" name="okhttp-2.0.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.2" level="project" />
<orderEntry type="library" exported="" name="gson-2.3" level="project" />
</component>
</module>
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def buildTime() {

android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.nashlincoln.blink"
Expand Down Expand Up @@ -50,8 +50,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.2'
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'joda-time:joda-time:2.5'
compile 'de.greenrobot:greendao:1.3.1'
compile 'com.squareup.retrofit:retrofit:1.7.1'
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/nashlincoln/blink/app/BlinkApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void fetchData() {
if (isConfigured()) {
Syncro.getInstance().fetchAttributeTypes();
Syncro.getInstance().fetchDeviceTypes();
Syncro.getInstance().fetchDevices();
Syncro.getInstance().fetchDevicesAndGroups();
}
}

Expand All @@ -80,4 +80,8 @@ public String getSsid() {
public static SharedPreferences getPreferences() {
return getApp().mPreferences;
}
//
// public void setSsid(String ssid) {
//
// }
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/nashlincoln/blink/content/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nashlincoln.blink.model.Attribute;
import com.nashlincoln.blink.model.AttributeType;
import com.nashlincoln.blink.model.Device;
import com.nashlincoln.blink.model.Group;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -14,13 +15,15 @@ public class Command {
public static final String ADD = "add";
public static final String REMOVE = "remove";
public static final String UPDATE = "update";
public static final String UPDATE_GROUP = "update-group";
public static final String SET_NAME = "set-name";
public long id;
public String action;
public String name;
public String type;
public List<Update> updates;
public transient Device device;
public transient Group group;

public static Command add(Device device) {
Command command = new Command();
Expand Down Expand Up @@ -61,6 +64,20 @@ public static Command setName(Device device) {
return command;
}

public static Command update(Group group) {
Command command = new Command();
command.group = group;
command.action = UPDATE_GROUP;
command.id = group.getId();
command.updates = new ArrayList<>();
for (Attribute attribute : group.getAttributes()) {
if (attribute.isChanged()) {
command.updates.add(new Update(attribute));
}
}
return command;
}

public static class Update {
public long id;
public String value;
Expand Down
122 changes: 84 additions & 38 deletions app/src/main/java/com/nashlincoln/blink/content/Syncro.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.nashlincoln.blink.model.DeviceDao;
import com.nashlincoln.blink.model.DeviceType;
import com.nashlincoln.blink.model.DeviceTypeDao;
import com.nashlincoln.blink.model.Group;
import com.nashlincoln.blink.model.GroupDao;
import com.nashlincoln.blink.network.BlinkApi;
import com.nashlincoln.blink.nfc.NfcCommand;

Expand Down Expand Up @@ -83,6 +85,36 @@ public void syncDevices() {
}
}

for (Group group : mDaoSession.getGroupDao().loadAll()) {
Command command = null;
if (group.getState() == null) {
group.setState(Device.STATE_NOMINAL);
}
switch (group.getState()) {
case Device.STATE_ADDED:
// command = Command.add(group);
// break;
//
// case Device.STATE_REMOVED:
// command = Command.remove(group);
// break;
//
// case Device.STATE_NAME_SET:
// command = Command.setName(group);
// break;

case Device.STATE_UPDATED:
command = Command.update(group);
break;

case Device.STATE_NOMINAL:
break;
}
if (command != null) {
commands.add(command);
}
}

if (commands.size() > 0) {
BlinkApi.getClient().sendCommands(commands, new Callback<Response>() {
@Override
Expand All @@ -93,7 +125,12 @@ public void success(Response response, Response response2) {
public void run() {
for (Command command : commands) {
needsRefresh[0] |= command.action.equals(Command.ADD);
command.device.setNominal();
if (command.device != null) {
command.device.setNominal();
}
if (command.group != null) {
command.group.setNominal();
}
}
}
});
Expand Down Expand Up @@ -149,6 +186,11 @@ public void failure(RetrofitError error) {
});
}

public void fetchDevicesAndGroups() {
fetchDevices();
fetchGroups();
}


public void fetchDevices() {
if (!mIsConnected) {
Expand Down Expand Up @@ -190,42 +232,46 @@ public void failure(RetrofitError error) {
});
}

// public void refreshDevices() {
// if (!mIsConnected) {
// return;
// }
// BlinkApi.getClient().getDevices(new Callback<List<Device>>() {
// @Override
// public void success(final List<Device> devices, Response response) {
// final DeviceDao deviceDao = mDaoSession.getDeviceDao();
//
// mDaoSession.runInTx(new Runnable() {
// @Override
// public void run() {
// for (Device device : devices) {
// Device current = deviceDao.load(device.getId());
// if (current == null) {
// device.setAttributableType(Device.ATTRIBUTABLE_TYPE);
// device.flushAttributes();
// device.resetAttributes();
// deviceDao.insertOrReplace(device);
// } else {
// current.updateFrom(device);
// }
// }
// }
// });
//
// mDaoSession.clear();
// Event.broadcast(Device.KEY);
// }
//
// @Override
// public void failure(RetrofitError error) {
//
// }
// });
// }
public void fetchGroups() {
if (!mIsConnected) {
return;
}
BlinkApi.getClient().getGroups(new Callback<List<Group>>() {
@Override
public void success(final List<Group> groups, Response response) {
final GroupDao groupDao = mDaoSession.getGroupDao();
mDaoSession.runInTx(new Runnable() {
@Override
public void run() {
List<Group> currentGroups = groupDao.loadAll();
Set<Long> newIds = new HashSet<>();
for (Group group : groups) {
newIds.add(group.getId());
group.setAttributableType(Group.ATTRIBUTABLE_TYPE);
group.flushAttributes();
group.resetAttributes();
group.flushGroupDevices();
groupDao.insertOrReplace(group);
}

for (Group group : currentGroups) {
if (!newIds.contains(group.getId())) {
group.deleteWithReferences();
}
}

}
});
mDaoSession.clear();
Event.broadcast(Group.KEY);
}

@Override
public void failure(RetrofitError error) {

}
});
}

public synchronized void onConnected(boolean isConnected) {
Log.d(TAG, "onConnected: " + isConnected);
Expand All @@ -240,7 +286,7 @@ public void update(Observable observable, Object data) {
syncDevices();
}
});
fetchDevices();
fetchDevicesAndGroups();
}

Event.broadcast(CONNECTION, mIsConnected);
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/com/nashlincoln/blink/model/Device.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.nashlincoln.blink.model;

import java.util.List;

import com.nashlincoln.blink.content.Syncro;
import com.nashlincoln.blink.event.Event;
import com.nashlincoln.blink.model.DaoSession;
import de.greenrobot.dao.DaoException;
import de.greenrobot.dao.query.WhereCondition;

// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS

// KEEP INCLUDES - put your custom includes here
import com.nashlincoln.blink.app.BlinkApp;
import com.nashlincoln.blink.network.BlinkApi;
import com.nashlincoln.blink.nfc.NfcCommand;
import com.nashlincoln.blink.event.Event;
import java.util.ArrayList;
// KEEP INCLUDES END
/**
Expand Down
Loading

0 comments on commit e793ff7

Please sign in to comment.