Skip to content

Commit

Permalink
This pack really doesn't like pnpm.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericm301 committed Jan 17, 2024
1 parent 913ecd5 commit 69323f2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 50 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 50 additions & 47 deletions packages/examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,75 @@ import { RingApi } from 'ring-client-api'
import { readFile, writeFile } from 'fs'
import { promisify } from 'util'

async function getAuth() {
function getAuth() {
const { env } = process,
ringApi = new RingApi({
refreshToken: env.RING_REFRESH_TOKEN!,
debug: true,
});

const sub = ringApi.onRefreshTokenUpdated.subscribe(
async ({oldRefreshToken, newRefreshToken}) => {
if (!oldRefreshToken) { return; }
}),
sub = ringApi.onRefreshTokenUpdated.subscribe(
async ({ oldRefreshToken, newRefreshToken }) => {
if (!oldRefreshToken) {
return
}
const currentConfig = await promisify(readFile)('.env'),
updatedConfig = currentConfig
.toString()
.replace(oldRefreshToken, newRefreshToken)

const currentConfig = await promisify(readFile)('.env'),
updatedConfig = currentConfig
.toString()
.replace(oldRefreshToken, newRefreshToken);
await promisify(writeFile)('.env', updatedConfig)
},
)

await promisify(writeFile)('.env', updatedConfig);
}
)

return {ringApi, sub}
return { ringApi, sub }
}

function takeTime(timeval: number): string {
// gets the time string and constructs a string like 'YYYYMMDD_HHmmss'
const t = new Date(timeval),
d2 = '2-digit',
DT_Opts: Intl.DateTimeFormatOptions = {
year: "numeric", month: d2, day: d2,
hour: d2, minute: d2, second: d2
}
const timeFormatter = Intl.DateTimeFormat('en-US', DT_Opts);
const timeFields: Intl.DateTimeFormatPart[] = timeFormatter.formatToParts(t);
const { year, month, day, hour, minute, second } = timeFields.reduce(
(obj, {type, value}) => {
if (type != 'literal') {
Object.defineProperty(obj, type, { value: value });
}
return obj;
},{}
) as any; // shut up, ts!
return `${year+month+day}_${hour+minute+second}`; // concatenation, NOT addition!
DT_Opts: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: d2,
day: d2,
hour: d2,
minute: d2,
second: d2,
},
timeFormatter = Intl.DateTimeFormat('en-US', DT_Opts),
timeFields: Intl.DateTimeFormatPart[] = timeFormatter.formatToParts(t),
{ year, month, day, hour, minute, second } = timeFields.reduce(
(obj, { type, value }) => {
if (type !== 'literal') {
Object.defineProperty(obj, type, { value: value })
}
return obj
},
{},
) as any // shut up, ts!
return `${year + month + day}_${hour + minute + second}` // concatenation, NOT addition!
}

async function example() {
if (!process.env.RING_REFRESH_TOKEN) {
throw "Go get an access token and put it in the .env file."
}
const { ringApi, sub } = getAuth(),
// locations = await ringApi.getLocations(),
// home = locations[0],
cams = await ringApi.getCameras(),
fluffyCam = cams.find((cam) => cam.name === '@Fluffy')!

const { ringApi, sub } = await getAuth();
fluffyCam.requestUpdate()

const locations = await ringApi.getLocations(),
home = locations[0],
cams = ringApi.getCameras(),
fluffyCam = (await cams).find((cam) => cam.name == '@Fluffy' )!;

fluffyCam.requestUpdate();

const snap = await fluffyCam.getSnapshot(),
snapAge = fluffyCam.currentTimestampAge;
snapAge = fluffyCam.currentTimestampAge

console.log(`\nSnap age: ${snapAge / 1000} seconds ago.\n`)
await promisify(writeFile)(
`../../snap/${takeTime(Date.now() - snapAge)}_snap.jpg`,
snap,
)

console.log(`\nSnap age: ${snapAge} seconds ago.\n`);
await promisify(writeFile)(`../../snap/${takeTime(Date.now() - 1000 * snapAge)}_snap.jpg`, snap)

await sub.unsubscribe();
process.exit(0);
sub.unsubscribe()
process.exit(0)
}

example().catch((e: any) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dotenv": "16.3.1",
"eslint-config-shared": "*",
"express": "4.18.2",
"ring-client-api": "link:..\\ring-client-api",
"ring-client-api": "../ring-client-api",
"tsconfig": "*"
}
}

0 comments on commit 69323f2

Please sign in to comment.