Skip to content

Commit

Permalink
cleanup some code
Browse files Browse the repository at this point in the history
  • Loading branch information
computergeek1507 committed Mar 4, 2022
1 parent dba95de commit dd30c43
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
public class EventDTO {
public String id;
public String camera;
public double frame_time;
public double snapshot_time;
//public double frame_time;
//public double snapshot_time;
public String label;
public float top_score;
public boolean false_positive;
public double start_time;
public double end_time;
public float score;
//public int[] box; // : [424, 500, 536, 712],
public float area;
//public int[] region; // : [264, 450, 667, 853],
//public String[] current_zones;
//public String[] entered_zones;
public boolean has_snapshot;
public boolean has_clip;
public boolean stationary;
public int motionless_count;
public int position_changes;
//public boolean false_positive;
//public double start_time;
//public double end_time;
//public float score;
// public int[] box; // : [424, 500, 536, 712],
//public float area;
// public int[] region; // : [264, 450, 667, 853],
// public String[] current_zones;
// public String[] entered_zones;
//public boolean has_snapshot;
//public boolean has_clip;
//public boolean stationary;
//public int motionless_count;
//public int position_changes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -100,7 +97,7 @@ public void connectionStateChanged(MqttConnectionState state, @Nullable Throwabl
if (connection != null) {
reconnect = null;

connection.subscribe("frigate/#", this).exceptionally(exception -> {
connection.subscribe("frigate/#", this).exceptionally(exception -> {
logger.warn("MQTT subscription failed: {}", exception.getMessage());
return false;
}).thenAccept(successful -> {
Expand All @@ -121,9 +118,6 @@ public void connectionStateChanged(MqttConnectionState state, @Nullable Throwabl

@Override
public void processMessage(String topic, byte[] payload) {
// Report raw JSON reply
final String json = new String(payload, UTF_8);

receive(topic, payload);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,16 @@

import static org.openhab.binding.frigate.internal.FrigateBindingConstants.*;

import java.time.ZonedDateTime;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.frigate.internal.FrigateCameraConfiguration;
import org.openhab.binding.frigate.internal.dto.EventDTO;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand All @@ -41,10 +35,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
* The {@link FrigateCameraHandler} is responsible for handling commands, which are
* sent to one of the channels.
Expand All @@ -57,7 +47,7 @@ public class FrigateCameraHandler extends BaseThingHandler {

private @Nullable FrigateCameraConfiguration config;
private @Nullable Future<?> normalPollFuture;
//private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
// private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
private String cameraName = "";
private int refreshInterval = 60;

Expand Down Expand Up @@ -89,13 +79,15 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void initialize() {
config = getConfigAs(FrigateCameraConfiguration.class);
cameraName = config.name;
refreshInterval = config.imagerefresh;
if (config != null) {
cameraName = config.name;
refreshInterval = config.imagerefresh;
}
logger.debug("Thing Name: {}", cameraName);
updateStatus(ThingStatus.UNKNOWN);

scheduler.execute(() -> {
boolean thingReachable = updateImage(true);
boolean thingReachable = updateImage(true);
if (thingReachable) {
updateStatus(ThingStatus.ONLINE);
restartPolls();
Expand Down Expand Up @@ -123,7 +115,7 @@ private boolean updateImage(boolean updateAll) {
RawType image = localHandler.getImage(imgurl);
if (image != null) {
updateState(CHANNEL_IMAGE, image);
if(updateAll) {
if (updateAll) {
updateState(CHANNEL_IMAGE_URL, new StringType(localHandler.buildBaseUrl(imgurl)));
String streamurl = String.format("/api/%s", cameraName);
updateState(CHANNEL_VIDEO_URL, new StringType(localHandler.buildBaseUrl(streamurl)));
Expand All @@ -132,21 +124,21 @@ private boolean updateImage(boolean updateAll) {
}
}
updateState(CHANNEL_IMAGE, UnDefType.UNDEF);
if(updateAll) {
if (updateAll) {
updateState(CHANNEL_IMAGE_URL, UnDefType.UNDEF);
updateState(CHANNEL_VIDEO_URL, UnDefType.UNDEF);
}
return false;
}
private void pollImage()
{

private void pollImage() {
updateImage(false);
}

private synchronized void restartPolls() {
stopPolls();
normalPollFuture = scheduler.scheduleWithFixedDelay(this::pollImage, refreshInterval, refreshInterval,
TimeUnit.SECONDS);
TimeUnit.SECONDS);
}

private synchronized void stopPolls() {
Expand All @@ -166,14 +158,15 @@ public String GetCameraName() {

public void SetLastObject(byte[] data) {
RawType image = new RawType(data, "image/jpeg");
updateState(CHANNEL_LASTOBJECT, image);
updateState(CHANNEL_LASTOBJECT, image);
}

public void UpdateEvent (EventDTO event){
updateState(CHANNEL_EVENT_ID, new StringType(event.id));
updateState( CHANNEL_EVENT_TYPE, new StringType(event.label));
updateState( CHANNEL_EVENT_SCORE , new DecimalType(event.top_score));
//updateState( CHANNEL_EVENT_START ,new DateTimeType(ZonedDateTime.ofInstant(event.getEnd().toInstant(), timeZoneProvider.getTimeZone())));
//updateState( CHANNEL_EVENT_END , new DateTimeType(event.end_time));
public void UpdateEvent(EventDTO event) {
updateState(CHANNEL_EVENT_ID, new StringType(event.id));
updateState(CHANNEL_EVENT_TYPE, new StringType(event.label));
updateState(CHANNEL_EVENT_SCORE, new DecimalType(event.top_score));
// updateState( CHANNEL_EVENT_START ,new DateTimeType(ZonedDateTime.ofInstant(event.getEnd().toInstant(),
// timeZoneProvider.getTimeZone())));
// updateState( CHANNEL_EVENT_END , new DateTimeType(event.end_time));
}
}
Loading

0 comments on commit dd30c43

Please sign in to comment.