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

Battery cams api fixes #1719

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions plugins/reolink/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sleep } from '@scrypted/common/src/sleep';
import sdk, { Brightness, Camera, Device, DeviceCreatorSettings, DeviceInformation, DeviceProvider, Intercom, MediaObject, ObjectDetectionTypes, ObjectDetector, ObjectsDetected, OnOff, PanTiltZoom, PanTiltZoomCommand, Reboot, RequestPictureOptions, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, Setting } from "@scrypted/sdk";
import sdk, { Brightness, Camera, Device, DeviceCreatorSettings, DeviceInformation, DeviceProvider, Intercom, MediaObject, ObjectDetectionTypes, ObjectDetector, ObjectsDetected, Online, OnOff, PanTiltZoom, PanTiltZoomCommand, Reboot, RequestPictureOptions, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, Setting } from "@scrypted/sdk";
import { StorageSettings } from '@scrypted/sdk/storage-settings';
import { EventEmitter } from "stream";
import { createRtspMediaStreamOptions, Destroyable, RtspProvider, RtspSmartCamera, UrlMediaStreamOptions } from "../../rtsp/src/rtsp";
@@ -78,7 +78,7 @@ class ReolinkCameraFloodlight extends ScryptedDeviceBase implements OnOff, Brigh
}
}

class ReolinkCamera extends RtspSmartCamera implements Camera, DeviceProvider, Reboot, Intercom, ObjectDetector, PanTiltZoom {
class ReolinkCamera extends RtspSmartCamera implements Camera, DeviceProvider, Reboot, Intercom, ObjectDetector, PanTiltZoom, Online {
client: ReolinkCameraClient;
clientWithToken: ReolinkCameraClient;
onvifClient: OnvifCameraAPI;
@@ -380,12 +380,19 @@ class ReolinkCamera extends RtspSmartCamera implements Camera, DeviceProvider, R
try {
const { batteryPercent, sleep } = await api.getBatteryInfo();
this.batteryLevel = batteryPercent;
this.online = !sleep;

const isOnline = !sleep;
if (isOnline !== this.online) {
this.online = isOnline;
}
if (batteryPercent !== this.batteryLevel) {
this.batteryLevel = batteryPercent;
}
}
catch (e) {
this.console.log('Error in getting battery info', e);
}
}, 1000 * 60 * 30);
}, 1000 * 10);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems excessively frequent.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't making this request keep the battery camera awake?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this won't wake it up, this will be handled by the hub and battery cams only work with hub on scrypted
What value would you suggest?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think preserving existing interval for non battery cams is preferable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interval won't start for non battery cams at all

}

async reboot() {
@@ -557,10 +564,19 @@ class ReolinkCamera extends RtspSmartCamera implements Camera, DeviceProvider, R
(async () => {
while (!killed) {
try {
const { value, data } = await client.getMotionState();
if (value)
triggerMotion();
ret.emit('data', JSON.stringify(data));
// Battey cameras do not have AI state, they just send events in case of PIR sensor triggered
// which equals a motion detected
if (this.hasBattery()) {
const { value, data } = await client.getPidActive();
if (value)
triggerMotion();
ret.emit('data', JSON.stringify(data));
} else {
const { value, data } = await client.getMotionState();
if (value)
triggerMotion();
ret.emit('data', JSON.stringify(data));
}
}
catch (e) {
ret.emit('error', e);
28 changes: 28 additions & 0 deletions plugins/reolink/src/reolink-api.ts
Original file line number Diff line number Diff line change
@@ -495,4 +495,32 @@ export class ReolinkCameraClient {
sleep: channelStatusEntry?.sleep === 1,
}
}

async getPidActive() {
const url = new URL(`http://${this.host}/api.cgi`);

const body = [
{
cmd: "GetEvents",
action: 0,
param: { channel: this.channelId }
},
];

const response = await this.requestWithLogin({
url,
responseType: 'json',
method: 'POST',
}, this.createReadable(body));

const error = response.body?.find(elem => elem.error)?.error;
if (error) {
this.console.error('error during call to getEvents', error);
}

return {
value: !!response.body?.[0]?.value?.ai?.other?.alarm_state,
data: response.body,
};
}
}