finally get working PiIR

This commit is contained in:
mykola2312 2024-04-29 04:13:45 +03:00
parent 113f287415
commit e35d922d05
6 changed files with 55 additions and 16 deletions

View file

@ -47,8 +47,6 @@ public class DBPool implements ConnectionProvider {
protected DBConnection spawnConnection() throws SQLException {
DBConnection conn = new DBConnection(DriverManager.getConnection(url), false);
connections.add(conn);
logger.debug(String.format("spawned connection %s", conn.toString()));
return conn;
}
@ -72,7 +70,6 @@ public class DBPool implements ConnectionProvider {
conn.beingUsed = true;
logger.debug(String.format("allocated connection %s", conn.toString()));
return conn.connection;
}

View file

@ -36,28 +36,62 @@ public class PiIR implements TaskProcess {
private static final int BUFFER_SIZE = 512;
private enum ReaderState {
SKIPPING,
READING_JSON,
PARSE_JSON
}
@Override
public void run() {
ReaderState state = ReaderState.SKIPPING;
byte[] buf = new byte[BUFFER_SIZE];
int off = 0;
try {
while (running && !Thread.currentThread().isInterrupted()) {
// reader loop
int len = input.read(buf, 0, buf.length);
int len = input.read(buf, off, 1);
if (len < 0) {
logger.warn("reading error. exiting");
running = false;
return;
}
String line = new String(
Arrays.copyOfRange(buf, 0, len),
StandardCharsets.UTF_8);
try {
PiIRDump dump = PiIRDump.deserialize(line);
switch (buf[off]) {
case '{' -> state = ReaderState.READING_JSON;
case '}' -> state = ReaderState.PARSE_JSON;
}
piir.handleDump(dump);
} catch (JsonProcessingException e) {
logger.warn("failed to deserialize dump!", e);
switch (state) {
case SKIPPING -> {}
case READING_JSON -> {
off++;
if (off >= BUFFER_SIZE) {
logger.warn(String.format(
"buffer overflow from piir dump! %d >= %d", off, BUFFER_SIZE));
state = ReaderState.SKIPPING;
off = 0;
}
}
case PARSE_JSON -> {
String line = new String(
Arrays.copyOfRange(buf, 0, off+1),
StandardCharsets.UTF_8);
logger.info(line);
try {
PiIRDump dump = PiIRDump.deserialize(line);
piir.handleDump(dump);
} catch (JsonProcessingException e) {
logger.warn("failed to deserialize dump!", e);
} finally {
state = ReaderState.SKIPPING;
off = 0;
}
}
}
}
} catch (IOException e) {
@ -85,7 +119,7 @@ public class PiIR implements TaskProcess {
@Override
public boolean spawn() throws IOException {
process = Runtime.getRuntime().exec(new String[] {
exec, "dump", "--gpio", String.valueOf(gpio)
"unbuffer", exec, "dump", "--gpio", String.valueOf(gpio)
});
input = process.getInputStream();
@ -122,6 +156,7 @@ public class PiIR implements TaskProcess {
String key = formatBindKey(dump.pre_data, dump.data);
MenuAction action = binds.get(key);
if (action != null) {
logger.info("executing action " + action.toString());
Main.frame.action(action);
} else {
logger.warn(String.format(

View file

@ -1,8 +1,10 @@
package com.mykola2312.mptv.piir;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@JsonIgnoreProperties(ignoreUnknown = true)
public class PiIRDump {
public String pre_data;
public String data;

View file

@ -16,5 +16,8 @@ public enum MenuAction {
ACTION_RIGHT,
@JsonProperty("open")
ACTION_OPEN
ACTION_OPEN,
@JsonProperty("close")
ACTION_CLOSE
}

View file

@ -135,6 +135,8 @@ public class MenuPanel extends JPanel {
}
}
}
case ACTION_CLOSE -> closePlayer();
}
if (categoryIndex < 0) categoryIndex = 0;
if (channelIndex < 0) channelIndex = 0;

View file

@ -17,12 +17,12 @@
</encoder>
</appender>
<logger name="com.mykola2312" level="debug" additivity="false">
<logger name="com.mykola2312" level="info" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<root level="debug">
<root level="info">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>