-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (44 loc) · 1.23 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { PrismaClient } from "@prisma/client";
import cron from "node-cron";
import config from "./config.ts";
import { getDevices } from "./kidde.ts";
const prisma = new PrismaClient();
cron.schedule(config.CRON, async () => {
const devices = await getDevices();
for (const device of devices) {
let existingDevice = await prisma.device.findFirst({
where: { id: device.serial_number },
});
if (!existingDevice) {
existingDevice = await prisma.device.create({
data: {
id: device.serial_number,
name: device.label,
},
});
}
const metricExists = await prisma.metric.findFirst({
where: {
deviceId: device.serial_number,
timestamp: device.last_seen,
},
});
if (!metricExists) {
const { co_level, iaq, iaq_temperature, humidity, hpa, tvoc, co2 } =
device;
await prisma.metric.create({
data: {
deviceId: device.serial_number,
timestamp: device.last_seen,
temp: iaq_temperature.value,
hpa: hpa.value,
tvoc: tvoc.value,
iaq: iaq.value,
co: co_level,
co2: co2.value,
humidity: humidity.value,
},
});
}
}
});