Skip to content

Commit

Permalink
Merge pull request #203 from neoPix/feat/stampfile-rate
Browse files Browse the repository at this point in the history
[EU] Stamps rate
  • Loading branch information
Hacksore authored Feb 8, 2022
2 parents 773a81b + 03336c9 commit eb9579f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bluelinky",
"version": "7.6.3",
"version": "7.6.4",
"description": "An unofficial nodejs API wrapper for Hyundai bluelink",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 4 additions & 2 deletions src/constants/europe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ const cacheResult = <T>(fn: (...options: any[]) => Promise<T>, durationInMS = 60
};
};

const L15_MINUTES_IN_MS = 60000 * 15;
// The stamp files are valuables for ~5.5 hours and are generated every hour
// We're using 4 hours here by applying a 1 hour safety rule between the generation and the usage and an extra 0.5 hour for network safety
const FOUR_HOURS = 60000 * 60 * 4;

const getStampList = async (file: string, stampsFile = `https://raw.githubusercontent.com/neoPix/bluelinky-stamps/master/${file}.v2.json`): Promise<StampCollection> => {
if (stampsFile.startsWith(('file://'))) {
Expand All @@ -58,7 +60,7 @@ const getStampList = async (file: string, stampsFile = `https://raw.githubuserco
return body as StampCollection;
};

const getStamp = (brand: string, stampsTimeout: number = L15_MINUTES_IN_MS) => cacheResult(async (stampsFile?: string) => {
const getStamp = (brand: string, stampsTimeout: number = FOUR_HOURS) => cacheResult(async (stampsFile?: string) => {
const { stamps, generated, frequency } = await getStampList(brand, stampsFile);
const position = ((Date.now() - new Date(generated).getTime()) / frequency) | 0;
return stamps[Math.min(position + Math.floor(Math.random() * 5), stamps.length - 1)];
Expand Down

0 comments on commit eb9579f

Please sign in to comment.