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

Reolink - Fix get ability #1616

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 13 additions & 2 deletions plugins/reolink/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ class ReolinkCamera extends RtspSmartCamera implements Camera, DeviceProvider, R

async updateAbilities() {
const api = this.getClient();
const abilities = await api.getAbility();
const apiWithToken = this.getClientWithToken();
let abilities;
try {
abilities = await api.getAbility();
} catch(e) {
abilities = await apiWithToken.getAbility();
}
this.storageSettings.values.abilities = abilities;
this.console.log('getAbility', JSON.stringify(abilities));
}
Expand Down Expand Up @@ -797,6 +803,7 @@ class ReolinkProvider extends RtspProvider {
const rtspChannel = parseInt(settings.rtspChannel?.toString()) || 0;
if (!skipValidate) {
const api = new ReolinkCameraClient(httpAddress, username, password, rtspChannel, this.console);
const apiWithToken = new ReolinkCameraClient(httpAddress, username, password, rtspChannel, this.console, true);
try {
await api.jpegSnapshot();
}
Expand All @@ -810,7 +817,11 @@ class ReolinkProvider extends RtspProvider {
doorbell = deviceInfo.type === 'BELL';
name = deviceInfo.name ?? 'Reolink Camera';
ai = await api.getAiState();
abilities = await api.getAbility();
try {
abilities = await api.getAbility();
} catch(e) {
abilities = await apiWithToken.getAbility();
}
}
catch (e) {
this.console.error('Reolink camera does not support AI events', e);
Expand Down
30 changes: 26 additions & 4 deletions plugins/reolink/src/reolink-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,37 @@ export class ReolinkCameraClient {
const params = url.searchParams;
params.set('cmd', 'GetAbility');
params.set('channel', this.channelId.toString());
const response = await this.requestWithLogin({
let response = await this.requestWithLogin({
url,
responseType: 'json',
});
const error = response.body?.[0]?.error;
let error = response.body?.[0]?.error;
if (error) {
this.console.error('error during call to getAbility', error);
throw new Error('error during call to getAbility');
this.console.error('error during call to getAbility GET, Trying with POST', error);

url.search = '';

const body = [
{
cmd: "GetAbility",
action: 0,
param: { User: { userName: this.username } }
}
];

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

error = response.body?.[0]?.error;
if (error) {
this.console.error('error during call to getAbility GET, Trying with POST', error);
throw new Error('error during call to getAbility');
}
}

return {
value: response.body?.[0]?.value || response.body?.value,
data: response.body,
Expand Down