mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Initial Commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.lanternsoftware.thermometer;
|
||||
|
||||
import com.lanternsoftware.thermometer.context.ThermometerApp;
|
||||
|
||||
public class TestThermo {
|
||||
public static void main(String[] args) {
|
||||
ThermometerApp app = new ThermometerApp();
|
||||
app.start();
|
||||
try {
|
||||
Thread.sleep(20000);
|
||||
} catch (InterruptedException _e) {
|
||||
_e.printStackTrace();
|
||||
}
|
||||
app.stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.lanternsoftware.thermometer.context;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
public class Globals implements ServletContextListener {
|
||||
public static ThermometerApp app;
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
app = new ThermometerApp();
|
||||
app.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
if (app != null) {
|
||||
app.stop();
|
||||
app = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.lanternsoftware.thermometer.context;
|
||||
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.concurrency.ConcurrencyUtils;
|
||||
import org.hid4java.HidDevice;
|
||||
import org.hid4java.HidManager;
|
||||
import org.hid4java.HidServices;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
public class ThermometerApp {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ThermometerApp.class);
|
||||
|
||||
private HidDevice device;
|
||||
private final Timer timer = new Timer();
|
||||
private double lastTemp;
|
||||
|
||||
public void start() {
|
||||
HidServices hs = HidManager.getHidServices();
|
||||
for (HidDevice d : hs.getAttachedHidDevices()) {
|
||||
if (NullUtils.isEqual(d.getVendorId(), (short) 0x413d) && NullUtils.isEqual(d.getProductId(), (short) 0x2107)) {
|
||||
if (d.getInterfaceNumber() == 1)
|
||||
device = d;
|
||||
}
|
||||
}
|
||||
if ((device != null) && device.open()) {
|
||||
synchronized (device) {
|
||||
read(hexToByte("0182770100000000"));
|
||||
read(hexToByte("0186ff0100000000"));
|
||||
read(hexToByte("0182770100000000"));
|
||||
read(hexToByte("0182770100000000"));
|
||||
}
|
||||
} else {
|
||||
LOG.error("Failed to open HID Device");
|
||||
return;
|
||||
}
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
lastTemp = readTemperature();
|
||||
}
|
||||
}, 0L, 10000L);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
timer.cancel();
|
||||
ConcurrencyUtils.sleep(10000);
|
||||
if (device != null) {
|
||||
device.close();
|
||||
device = null;
|
||||
}
|
||||
HidServices hs = HidManager.getHidServices();
|
||||
hs.stop();
|
||||
hs.shutdown();
|
||||
}
|
||||
|
||||
private byte[] read(byte[] _request) {
|
||||
int RETRIES = 8;
|
||||
int stat = -1;
|
||||
int attempts = 0;
|
||||
while ((stat <= 0) && (attempts < RETRIES)) {
|
||||
attempts++;
|
||||
try {
|
||||
stat = device.write(_request, _request.length*8, (byte) 0);
|
||||
}
|
||||
catch (Exception _e) {
|
||||
LOG.error("Exception while writing", _e);
|
||||
}
|
||||
if (stat <= 0) {
|
||||
if (attempts == RETRIES) {
|
||||
LOG.error("Failed max number of retires, returning null");
|
||||
return null;
|
||||
}
|
||||
LOG.error("Write attempt " + attempts + " failed, waiting 250ms to retry");
|
||||
ConcurrencyUtils.sleep(250);
|
||||
}
|
||||
}
|
||||
byte[] response = new byte[32];
|
||||
stat = -1;
|
||||
attempts = 0;
|
||||
while ((stat <= 0) && (attempts < RETRIES)) {
|
||||
attempts++;
|
||||
try {
|
||||
stat = device.read(response, 500);
|
||||
}
|
||||
catch (Exception _e) {
|
||||
LOG.error("Exception while reading", _e);
|
||||
}
|
||||
if (stat <= 0) {
|
||||
if (attempts == RETRIES) {
|
||||
LOG.error("Failed max number of retires, returning null");
|
||||
return null;
|
||||
}
|
||||
LOG.error("Read attempt " + attempts + " failed, waiting 250ms to retry");
|
||||
ConcurrencyUtils.sleep(250);
|
||||
}
|
||||
}
|
||||
return response;
|
||||
}
|
||||
public double getTemperature() {
|
||||
return lastTemp;
|
||||
}
|
||||
|
||||
public double readTemperature() {
|
||||
if (device != null) {
|
||||
synchronized (device) {
|
||||
byte[] response = read(hexToByte("0180330100000000"));
|
||||
if (response == null)
|
||||
return 5.0;
|
||||
int rawReading = ((response[3] & 0xFF) + (response[2] << 8));
|
||||
if (rawReading == 0)
|
||||
return 5.0;
|
||||
return rawReading / 100.0;
|
||||
}
|
||||
|
||||
}
|
||||
return 5.0;
|
||||
}
|
||||
|
||||
private static byte[] hexToByte(String s) {
|
||||
int len = s.length();
|
||||
byte[] data = new byte[len / 2];
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.lanternsoftware.thermometer.servlet;
|
||||
|
||||
import com.lanternsoftware.thermometer.context.Globals;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
@WebServlet("/temp")
|
||||
public class TempServlet extends ThermoServlet {
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
setResponseEntity(resp, "application/json", "{\"temp\": "+ Globals.app.getTemperature() + "}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.lanternsoftware.thermometer.servlet;
|
||||
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public abstract class ThermoServlet extends HttpServlet {
|
||||
public static void setResponseHtml(HttpServletResponse _response, String _sHtml) {
|
||||
setResponseEntity(_response, "text/html", _sHtml);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, String _sContentType, String _sEntity) {
|
||||
setResponseEntity(_response, 200, _sContentType, _sEntity);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, String _sContentType, byte[] _btData) {
|
||||
setResponseEntity(_response, 200, _sContentType, _btData);
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, int _iStatus, String _sContentType, String _sEntity) {
|
||||
setResponseEntity(_response, _iStatus, _sContentType, NullUtils.toByteArray(_sEntity));
|
||||
}
|
||||
|
||||
public static void setResponseEntity(HttpServletResponse _response, int _iStatus, String _sContentType, byte[] _btData) {
|
||||
OutputStream os = null;
|
||||
try {
|
||||
_response.setStatus(_iStatus);
|
||||
_response.setCharacterEncoding("UTF-8");
|
||||
_response.setContentType(_sContentType);
|
||||
if ((_btData != null) && (_btData.length > 0)) {
|
||||
_response.setContentLength(_btData.length);
|
||||
os = _response.getOutputStream();
|
||||
os.write(_btData);
|
||||
} else
|
||||
_response.setContentLength(0);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<property name="log.pattern" value="%date %-5level %logger{0} - %message%n"/>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>/opt/currentmonitor/log/log.txt</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>/opt/currentmonitor/log/log.%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
|
||||
<maxFileSize>20MB</maxFileSize>
|
||||
<maxHistory>20</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="com.lanternsoftware" level="INFO"/>
|
||||
|
||||
<root level="OFF">
|
||||
<appender-ref ref="FILE"/>
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE web-app PUBLIC
|
||||
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
|
||||
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||
|
||||
<web-app>
|
||||
<listener>
|
||||
<listener-class>com.lanternsoftware.thermometer.context.Globals</listener-class>
|
||||
</listener>
|
||||
</web-app>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.lanternsoftware.thermometer;
|
||||
|
||||
import com.lanternsoftware.thermometer.context.ThermometerApp;
|
||||
|
||||
public class TestStartup {
|
||||
public static void main(String[] args) {
|
||||
ThermometerApp app = new ThermometerApp();
|
||||
app.start();
|
||||
try {
|
||||
Thread.sleep(20000);
|
||||
} catch (InterruptedException _e) {
|
||||
_e.printStackTrace();
|
||||
}
|
||||
app.stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user