-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/TKOaly/info-screen
- Loading branch information
Showing
6 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Chip from '@/components/Chip'; | ||
import I18n from '@/components/I18n/I18n'; | ||
import { getPohinaFactor } from '@/server/pohinaFactor'; | ||
import { differenceInMinutes, isWithinInterval, setHours } from 'date-fns'; | ||
|
||
const PohinaFactor = async () => { | ||
const { pohinaFactor, lastActivity } = await getPohinaFactor(); | ||
|
||
const differenceMin = differenceInMinutes(new Date(), lastActivity); | ||
const differenceHours = Math.floor(differenceMin / 60); | ||
const differenceDays = Math.floor(differenceMin / 60 / 24); | ||
|
||
const differenceText = | ||
differenceMin < 60 | ||
? `Aktiivisuutta havaittu ${differenceMin} min sitten // Activity detected ${differenceMin} min ago` | ||
: differenceHours < 24 | ||
? `Aktiivisuutta havaittu ${differenceHours} h sitten // Activity detected ${differenceHours} h ago` | ||
: `Aktiivisuutta havaittu ${differenceDays} pv sitten // Activity detected ${differenceDays} d ago`; | ||
|
||
return ( | ||
!isWithinInterval(new Date(), { | ||
start: setHours(new Date(), 7), | ||
end: setHours(new Date(), 18), | ||
}) && ( | ||
<> | ||
<h3 className="p-2 text-2xl font-bold"> | ||
<I18n>Pöhinäkerroin // Pöhinäfactor</I18n> | ||
</h3> | ||
<hr className="w-full" /> | ||
<div className="p-4 pb-0"> | ||
<div className="flex gap-2"> | ||
{pohinaFactor !== undefined && ( | ||
<Chip className="bg-org-matlu-secondary text-grey-100"> | ||
<I18n> | ||
{`Pöhinäkerroin: ${pohinaFactor.toString()} // Pöhinäfactor: ${pohinaFactor.toString()}`} | ||
</I18n> | ||
</Chip> | ||
)} | ||
{lastActivity && ( | ||
<Chip className="bg-org-matlu-secondary text-grey-100"> | ||
<I18n>{differenceText}</I18n> | ||
</Chip> | ||
)} | ||
</div> | ||
</div> | ||
</> | ||
) | ||
); | ||
}; | ||
|
||
export default PohinaFactor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use server'; | ||
|
||
import { max } from 'date-fns'; | ||
import { revalidateTag } from 'next/cache'; | ||
import { GET } from './wrappers'; | ||
|
||
const ENTRYPOINT = | ||
'https://klusteri.network/api/events/latest?sensors=[%22klusteri/ac/reed-1%22,%22klusteri/ac/reed-2%22]'; | ||
|
||
type SensorData = { | ||
sensors: { | ||
[key: number]: { | ||
id: number; | ||
name: string; | ||
created_at: string; | ||
updated_at: string; | ||
}; | ||
}; | ||
events: { | ||
[key: number]: { | ||
id: string; | ||
sensor_id: number; | ||
type: string; | ||
timestamp: string; | ||
}; | ||
}; | ||
pohinaFactor: number; | ||
}; | ||
|
||
const fetchTag = 'pohinaFactor'; | ||
|
||
export const getPohinaFactor = async () => | ||
await GET<SensorData>(ENTRYPOINT, { | ||
next: { | ||
tags: [fetchTag], | ||
revalidate: 5 * 60, | ||
}, | ||
}).then(({ events, pohinaFactor }) => ({ | ||
lastActivity: max( | ||
Object.values(events).map((event) => new Date(event.timestamp)) | ||
), | ||
pohinaFactor, | ||
})); | ||
|
||
export const revalidatePohinaFactor = async () => revalidateTag(fetchTag); |