mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Add a rules engine so I can be notified when I forget to close my garage door.
This commit is contained in:
@@ -20,6 +20,7 @@ import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.bson.BsonBinaryReader;
|
||||
import org.bson.BsonBinaryWriter;
|
||||
import org.bson.Document;
|
||||
@@ -725,6 +726,14 @@ public class DaoSerializer {
|
||||
return ZipUtils.zip(toBson(_entity, true));
|
||||
}
|
||||
|
||||
public static String toBase64ZipBson(Object _o) {
|
||||
return toBase64ZipBson(toDaoEntity(_o));
|
||||
}
|
||||
|
||||
public static String toBase64ZipBson(DaoEntity _entity) {
|
||||
return Base64.encodeBase64String(toZipBson(_entity));
|
||||
}
|
||||
|
||||
public static <T> T fromZipBson(byte[] _btZipBson, Class<T> _class) {
|
||||
return DaoSerializer.fromDaoEntity(fromZipBson(_btZipBson), _class);
|
||||
}
|
||||
@@ -733,6 +742,14 @@ public class DaoSerializer {
|
||||
return fromBson(ZipUtils.unzip(_btZipBson));
|
||||
}
|
||||
|
||||
public static <T> T fromBase64ZipBson(String _zipBson, Class<T> _class) {
|
||||
return DaoSerializer.fromDaoEntity(fromBase64ZipBson(_zipBson), _class);
|
||||
}
|
||||
|
||||
public static DaoEntity fromBase64ZipBson(String _zipBson) {
|
||||
return fromBson(ZipUtils.unzip(Base64.decodeBase64(_zipBson)));
|
||||
}
|
||||
|
||||
public static <T> T fromBson(byte[] _btBson, Class<T> _class) {
|
||||
return fromDaoEntity(fromBson(_btBson), _class);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.lanternsoftware.util.dao.auth;
|
||||
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.dao.annotations.DBSerializable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@DBSerializable(autogen = false)
|
||||
public class AuthCode {
|
||||
private int accountId;
|
||||
private List<Integer> auxiliaryAccountIds;
|
||||
|
||||
public AuthCode() {
|
||||
}
|
||||
|
||||
public AuthCode(int _accountId, List<Integer> _auxiliaryAccountIds) {
|
||||
accountId = _accountId;
|
||||
auxiliaryAccountIds = _auxiliaryAccountIds;
|
||||
}
|
||||
|
||||
public int getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(int _accountId) {
|
||||
accountId = _accountId;
|
||||
}
|
||||
|
||||
public List<Integer> getAuxiliaryAccountIds() {
|
||||
return auxiliaryAccountIds;
|
||||
}
|
||||
|
||||
public void setAuxiliaryAccountIds(List<Integer> _auxiliaryAccountIds) {
|
||||
auxiliaryAccountIds = _auxiliaryAccountIds;
|
||||
}
|
||||
|
||||
public List<Integer> getAllAccountIds() {
|
||||
List<Integer> ids = new ArrayList<>();
|
||||
ids.add(accountId);
|
||||
if (auxiliaryAccountIds != null)
|
||||
ids.addAll(auxiliaryAccountIds);
|
||||
return ids;
|
||||
}
|
||||
|
||||
public boolean isAuthorized(int _accountId) {
|
||||
return accountId == _accountId || CollectionUtils.contains(auxiliaryAccountIds, _accountId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.lanternsoftware.util.dao.auth.dao;
|
||||
|
||||
import com.lanternsoftware.util.dao.auth.AuthCode;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.dao.AbstractDaoSerializer;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoProxyType;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class AuthCodeSerializer extends AbstractDaoSerializer<AuthCode>
|
||||
{
|
||||
@Override
|
||||
public Class<AuthCode> getSupportedClass()
|
||||
{
|
||||
return AuthCode.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DaoProxyType> getSupportedProxies() {
|
||||
return Collections.singletonList(DaoProxyType.MONGO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DaoEntity toDaoEntity(AuthCode _o)
|
||||
{
|
||||
DaoEntity d = new DaoEntity();
|
||||
d.put("account_id", _o.getAccountId());
|
||||
if (CollectionUtils.isNotEmpty(_o.getAuxiliaryAccountIds()))
|
||||
d.put("aux_account_ids", CollectionUtils.toByteArray(_o.getAuxiliaryAccountIds()));
|
||||
return d;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthCode fromDaoEntity(DaoEntity _d)
|
||||
{
|
||||
AuthCode o = new AuthCode();
|
||||
o.setAccountId(DaoSerializer.getInteger(_d, "account_id"));
|
||||
o.setAuxiliaryAccountIds(CollectionUtils.fromByteArrayOfIntegers(DaoSerializer.getByteArray(_d, "aux_account_ids")));
|
||||
return o;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import com.lanternsoftware.util.dao.DaoProxyType;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.jdbc.DatabaseType;
|
||||
import com.lanternsoftware.util.dao.jdbc.JdbcConfig;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
com.lanternsoftware.util.dao.auth.dao.AuthCodeSerializer
|
||||
com.lanternsoftware.util.dao.jdbc.dao.JdbcConfigSerializer
|
||||
|
||||
Reference in New Issue
Block a user