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:
78
rules/lantern-dataaccess-rules/pom.xml
Normal file
78
rules/lantern-dataaccess-rules/pom.xml
Normal file
@@ -0,0 +1,78 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.lanternsoftware.rules</groupId>
|
||||
<artifactId>lantern-dataaccess-rules</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.0</version>
|
||||
<name>lantern-dataaccess-rules</name>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.rules</groupId>
|
||||
<artifactId>lantern-datamodel-rules</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.util</groupId>
|
||||
<artifactId>lantern-util-dao-mongo</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
<phase>compile</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<optimize>true</optimize>
|
||||
<showDeprecation>true</showDeprecation>
|
||||
<encoding>UTF-8</encoding>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<index>true</index>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.lanternsoftware.dataaccess.rules;
|
||||
|
||||
import com.lanternsoftware.datamodel.rules.Event;
|
||||
import com.lanternsoftware.datamodel.rules.EventType;
|
||||
import com.lanternsoftware.datamodel.rules.FcmDevice;
|
||||
import com.lanternsoftware.datamodel.rules.Rule;
|
||||
import com.lanternsoftware.util.dao.DaoQuery;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.dao.DaoSort;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoProxy;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class MongoRulesDataAccess implements RulesDataAccess {
|
||||
private final MongoProxy proxy;
|
||||
|
||||
public MongoRulesDataAccess(MongoConfig _config) {
|
||||
proxy = new MongoProxy(_config);
|
||||
proxy.ensureIndex(Rule.class, DaoSort.sort("account_id"));
|
||||
proxy.ensureIndex(Event.class, DaoSort.sort("account_id").then("type").then("source_id").then("time"));
|
||||
proxy.ensureIndex(FcmDevice.class, DaoSort.sort("account_id"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
proxy.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putRule(Rule _rule) {
|
||||
proxy.save(_rule);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Rule> getRulesForAccount(int _accountId) {
|
||||
return proxy.query(Rule.class, new DaoQuery("account_id", _accountId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteRule(String _ruleId) {
|
||||
proxy.delete(Rule.class, new DaoQuery("_id", _ruleId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putEvent(Event _event) {
|
||||
proxy.save(_event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Event getMostRecentEvent(int _accountId, EventType _type, String _sourceId) {
|
||||
return proxy.queryOne(Event.class, new DaoQuery("account_id", _accountId).and("type", DaoSerializer.toEnumName(_type)).and("source_id", _sourceId), DaoSort.sortDesc("time"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Event> getEvents(int _accountId, EventType _type, String _sourceId, Date _from, Date _to) {
|
||||
return proxy.query(Event.class, new DaoQuery("account_id", _accountId).and("type", DaoSerializer.toEnumName(_type)).and("source_id", _sourceId).andBetweenInclusiveExclusive("time", DaoSerializer.toLong(_from), DaoSerializer.toLong(_to)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putFcmDevice(FcmDevice _device) {
|
||||
proxy.save(_device);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FcmDevice> getFcmDevicesForAccount(int _accountId) {
|
||||
return proxy.query(FcmDevice.class, new DaoQuery("account_id", _accountId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.lanternsoftware.dataaccess.rules;
|
||||
|
||||
import com.lanternsoftware.datamodel.rules.Event;
|
||||
import com.lanternsoftware.datamodel.rules.EventType;
|
||||
import com.lanternsoftware.datamodel.rules.FcmDevice;
|
||||
import com.lanternsoftware.datamodel.rules.Rule;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public interface RulesDataAccess {
|
||||
void shutdown();
|
||||
void putRule(Rule _rule);
|
||||
List<Rule> getRulesForAccount(int _accountId);
|
||||
void deleteRule(String _ruleId);
|
||||
void putEvent(Event _event);
|
||||
Event getMostRecentEvent(int _accountId, EventType _type, String _sourceId);
|
||||
List<Event> getEvents(int _accountId, EventType _type, String _sourceId, Date _from, Date _to);
|
||||
void putFcmDevice(FcmDevice _device);
|
||||
List<FcmDevice> getFcmDevicesForAccount(int _accountId);
|
||||
}
|
||||
Reference in New Issue
Block a user