Skip to content

Commit

Permalink
Merge pull request #3 from ototadana/feature/improve-state-management
Browse files Browse the repository at this point in the history
improve state management
  • Loading branch information
ototadana authored Dec 30, 2022
2 parents 3e79e61 + 956b99e commit 7b6d878
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.xpfriend.tydrone'
version '1.0.0'
version '1.1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/xpfriend/tydrone/core/Facade.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public String getStates() {
return info.getStates();
}

public String getState(String key) {
return info.getState(key);
}

public void setEnabled(String name, boolean enabled) {
info.setEnabled(name, enabled);
}

public boolean isEnabled(String name) {
return info.isEnabled(name);
}


public byte[] pickImage() {
return info.pickImage();
}
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/com/xpfriend/tydrone/core/Info.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.xpfriend.tydrone.core;

import java.util.HashMap;
import java.util.Map;

public class Info {
private final Map<String, String> states = new HashMap<>();
private final Map<String, Boolean> enabled = new HashMap<>();
private byte[] image;
private boolean active = true;
private String states;
private String command;
private String notice;
private String sentCommand;
private boolean recording = false;

@SuppressWarnings("rawtypes")
private VideoFrame frame;
private boolean richStates;
Expand All @@ -30,11 +33,28 @@ public void stop() {
}

public String getStates() {
return states;
StringBuilder sb = new StringBuilder();
states.forEach((k, v) -> sb.append(k).append(':').append(v).append(';'));
if (sb.length() > 0) {
sb.deleteCharAt(sb.length() - 1);
}
return sb.toString();
}

public String getState(String key) {
return states.get(key);
}

public void setState(String key, String value) {
states.put(key, value);
}

public void setEnabled(String name, boolean enabled) {
this.enabled.put(name, enabled);
}

public void setStates(String states) {
this.states = states;
public boolean isEnabled(String name) {
return this.enabled.getOrDefault(name, false);
}

public boolean isRecording() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public void start(Info info) throws Exception {
continue;
}

if (info.isRecording()) {
info.setFrame(new VideoFrame<>(frame.clone()));
}
info.setFrame(new VideoFrame<>(frame.clone()));

if (!info.hasImage()) {
info.setImage(toByteArray(frame));
Expand Down
37 changes: 31 additions & 6 deletions src/main/java/com/xpfriend/tydrone/telloio/handlers/State.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.xpfriend.tydrone.telloio.handlers;

import com.xpfriend.tydrone.core.Info;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -185,12 +187,35 @@ public void setWifi(ByteBuffer buffer) {
wifi = buffer.get();
byte wil = buffer.get(); // Wifi Interference level (percentage)
}

public void updateStates(Info info) {
info.setState("bat", Byte.toString(bat));
info.setState("lit", Byte.toString(lit));
info.setState("wifi", Byte.toString(wifi));
info.setState("yaw", Integer.toString(yaw));
if (!info.isRichStates()) {
return;
}

public String getDefaultStates() {
return String.format("bat:%d;lit:%d;wifi:%d;yaw:%d", bat, lit, wifi, yaw);
}

public String getRichStates() {
return "bat:" + bat + ";" + "lit:" + lit + ";" + "wifi:" + wifi + ";" + "yaw:" + yaw + ";" + "pitch:" + pitch + ";" + "roll:" + roll + ";" + "h:" + h + ";" + "time:" + time + ";" + "ax:" + ax + ";" + "ay:" + ay + ";" + "az:" + az + ";" + "gx:" + gx + ";" + "gy:" + gy + ";" + "gz:" + gz + ";" + "px:" + px + ";" + "py:" + py + ";" + "pz:" + pz + ";" + "vx:" + vx + ";" + "vy:" + vy + ";" + "vz:" + vz + ";" + "vn:" + vn + ";" + "ve:" + ve + ";" + "vd:" + vd + ";";
info.setState("pitch", Integer.toString(pitch));
info.setState("roll", Integer.toString(roll));
info.setState("h", Short.toString(h));
info.setState("time", Short.toString(time));

info.setState("ax", Float.toString(ax));
info.setState("ay", Float.toString(ay));
info.setState("az", Float.toString(az));
info.setState("gx", Float.toString(gx));
info.setState("gy", Float.toString(gy));
info.setState("gz", Float.toString(gz));
info.setState("px", Float.toString(px));
info.setState("py", Float.toString(py));
info.setState("pz", Float.toString(pz));
info.setState("vx", Short.toString(vx));
info.setState("vy", Short.toString(vy));
info.setState("vz", Short.toString(vz));
info.setState("vn", Float.toString(vn));
info.setState("ve", Float.toString(ve));
info.setState("vd", Float.toString(vd));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void handleMessage(Message message, Info info) throws IOException {
state.setLight(message.getBuffer());
}

String states = info.isRichStates() ? state.getRichStates() : state.getDefaultStates();
info.setStates(states);
state.updateStates(info);
}

private void sendAckLog(short id) throws IOException {
Expand Down

0 comments on commit 7b6d878

Please sign in to comment.