Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ipcamera] Improvements and fix 503 errors go to offline with Hik #11419

Merged
merged 16 commits into from
Oct 24, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;

/**
* The {@link ChannelTracking} Can be used to find the handle for a HTTP channel if you know the URL. The reply can
Expand Down Expand Up @@ -43,6 +44,15 @@ public Channel getChannel() {
return channel;
}

/**
* Closes the channel, but keeps the HTTP reply stored in the tracker.
*
* @return ChannelFuture
*/
public ChannelFuture closeChannel() {
return channel.close();
}

public String getReply() {
return storedReply;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DahuaHandler(IpCameraHandler handler, int nvrChannel) {
}

private void processEvent(String content) {
int startIndex = content.indexOf("Code=", 12) + 5;// skip --myboundary
int startIndex = content.indexOf("Code=", 12) + 5;// skip --myboundary and Code=
int endIndex = content.indexOf(";", startIndex + 1);
if (startIndex == -1 || endIndex == -1) {
ipCameraHandler.logger.debug("Code= not found in Dahua event. Content was:{}", content);
Expand Down Expand Up @@ -177,7 +177,9 @@ private void processEvent(String content) {
case "LensMaskClose":
ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.OFF);
break;
// Skip these so they are not logged.
case "TimeChange":
case "IntelliFrame":
case "NTPAdjustTime":
case "StorageChange":
case "Reboot":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void checkKeepAlive() {
}

private class IpCameraFfmpegThread extends Thread {
private ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
private ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
public int countOfMotions;

IpCameraFfmpegThread() {
Expand Down Expand Up @@ -220,6 +220,7 @@ public void stopConverting() {
Process localProcess = process;
if (localProcess != null) {
localProcess.destroyForcibly();
process = null;
}
if (format.equals(FFmpegFormat.HLS)) {
ipCameraHandler.setChannelState(CHANNEL_START_STREAM, OnOffType.OFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms
}
}
break;
default:
logger.debug("Unhandled reply-{}.", content);
break;
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public void processAuth(String authenticate, String httpMethod, String requestUR
/////// Fresh Digest Authenticate method follows as Basic is already handled and returned ////////
realm = Helper.searchString(authenticate, "realm=\"");
if (realm.isEmpty()) {
logger.warn("Could not find a valid WWW-Authenticate response in :{}", authenticate);
logger.warn(
"No valid WWW-Authenticate in response. Has the camera activated the illegal login lock? Details:{}",
authenticate);
return;
}
nonce = Helper.searchString(authenticate, "nonce=\"");
Expand Down Expand Up @@ -142,23 +144,17 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms
if (msg == null || ctx == null) {
return;
}
boolean closeConnection = true;
String authenticate = "";
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
if (response.status().code() == 401) {
ctx.close();
if (!response.headers().isEmpty()) {
String authenticate = "";
for (CharSequence name : response.headers().names()) {
for (CharSequence value : response.headers().getAll(name)) {
if (name.toString().equalsIgnoreCase("WWW-Authenticate")) {
authenticate = value.toString();
}
if (name.toString().equalsIgnoreCase("Connection")
&& value.toString().contains("keep-alive")) {
// closeConnection = false;
// trial this for a while to see if it solves too many bytes with digest turned on.
closeConnection = true;
}
}
}
if (!authenticate.isEmpty()) {
Expand All @@ -167,24 +163,22 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms
ipCameraHandler.cameraConfigError(
"Camera gave no WWW-Authenticate: Your login details must be wrong.");
}
if (closeConnection) {
ctx.close();// needs to be here
}
}
} else if (response.status().code() != 200) {
logger.debug("Camera at IP:{} gave a reply with a response code of :{}",
ipCameraHandler.cameraConfig.getIp(), response.status().code());
ctx.close();
switch (response.status().code()) {
case 403:
logger.warn(
"403 Forbidden: Check camera setup or has the camera activated the illegal login lock?");
break;
default:
logger.debug("Camera at IP:{} gave a reply with a response code of :{}",
ipCameraHandler.cameraConfig.getIp(), response.status().code());
break;
}
}
}
// Pass the Message back to the pipeline for the next handler to process//
super.channelRead(ctx, msg);
}

@Override
public void handlerAdded(@Nullable ChannelHandlerContext ctx) {
}

@Override
public void handlerRemoved(@Nullable ChannelHandlerContext ctx) {
}
}
Loading