Skip to content

Commit

Permalink
docs: updated/added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mon4ik committed Sep 24, 2024
1 parent 9d725a5 commit 13a3800
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/oauth/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// oAuth server

import express, { Express } from "express";
import ip from "ip"

import { Server } from "net";
import { Globals } from "../Globals.js";
import { YandexAPI } from "../yandex/api.js";
Expand Down
5 changes: 5 additions & 0 deletions src/yandex/Capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import _ from "lodash"

import type { YandexCapability } from "../types.js";

/**
* Yandex Capability, property of a `Device`, that describes its capabilities.
*
* @see https://yandex.ru/dev/dialogs/smart-home/doc/ru/concepts/capability-types
*/
export class Capability {
private _capability: YandexCapability.Any
private _pendingActions: Map<string, Record<string, any>>
Expand Down
6 changes: 5 additions & 1 deletion src/yandex/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import chalk from "chalk";
import * as hap from "hap-nodejs";
import _ from "lodash";

/**
* Class, representing yandex Device, that also
* syncs with Homekit
*/
export class Device {
private readonly _accessory: Accessory
private _service: Service
Expand All @@ -34,6 +38,7 @@ export class Device {
}

// Basic YandexAPI fields

get id() {
return this._initialDevice.id
}
Expand Down Expand Up @@ -123,6 +128,5 @@ export class Device {
await this.initCapabilities()

this._accessory.addService(this._service)
// service.getCharacteristic()
}
}
21 changes: 17 additions & 4 deletions src/yandex/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import chalk from "chalk";

import { Globals } from "../Globals.js";

import type { GetDevicesResponse, YandexResponse } from "../types.js";
import type { GetDevicesResponse, YandexDevice, YandexResponse } from "../types.js";

/**
* Yandex API
*/
export class YandexAPI {
public get token() {
return Globals.getOauth().accessToken
Expand All @@ -30,7 +33,10 @@ export class YandexAPI {

// IOT associated things //

async getDevices() {
/**
* Request yandex devices
*/
async getDevices(): Promise<YandexDevice[]> {
try {
const resp = await this.request("/user/info", {})

Expand All @@ -43,6 +49,9 @@ export class YandexAPI {
}
}

/**
* Apply actions to Yandex
*/
async applyActions(devices: any): Promise<true> {
try {
await this.request("/devices/actions", {
Expand All @@ -60,6 +69,9 @@ export class YandexAPI {

// Token associated things //

/**
* Refresh Yandex's token
*/
async refreshToken() {
const refreshToken = Globals.getOauth().refreshToken
const client = Globals.getConfig().client
Expand Down Expand Up @@ -101,6 +113,9 @@ export class YandexAPI {
})
}

/**
* Exchange code to token
*/
async exchangeToken(code: string) {
const client = Globals.getConfig().client
const authHeader = Buffer.from(`${client.id}:${client.secret}`).toString("base64")
Expand Down Expand Up @@ -133,6 +148,4 @@ export class YandexAPI {
expiresAt: Date.now() + resp.data.expires_in * 1000
})
}


}
12 changes: 7 additions & 5 deletions src/yandex/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { YandexAPI } from "./api.js";
import { YandexDevice } from "../types.js";

/*
* Controller for:
* - Syncing devices
* - Managing token
* Yandex Controller that:
* - Sync devices
* - Manage token
*/
export class YandexController {
private readonly bridge: hap.Bridge
Expand Down Expand Up @@ -97,10 +97,12 @@ export class YandexController {
await this.yandexAPI.refreshToken()

// update interval
this.refreshAt = Globals.getOauth().expiresAt - 15_000 // expiresAt - 15 secs, becoz network speed
this.refreshAt = Globals.getOauth().expiresAt - 15_000 // expiresAt - 15 secs
}

/* Verify existing token, and(or) create new */
/**
* Verify existing token, and(or) create new
*/
private async tokenVerify() {
const oauth = Globals.getOauth()

Expand Down

0 comments on commit 13a3800

Please sign in to comment.