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

View file

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

View file

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

View file

@ -16,5 +16,8 @@ public enum MenuAction {
ACTION_RIGHT, ACTION_RIGHT,
@JsonProperty("open") @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 (categoryIndex < 0) categoryIndex = 0;
if (channelIndex < 0) channelIndex = 0; if (channelIndex < 0) channelIndex = 0;

View file

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