-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
105 lines (90 loc) · 2.56 KB
/
Copy pathdatabase.js
File metadata and controls
105 lines (90 loc) · 2.56 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
/**
* Store the data in the messages schema
* @param {Object} ms Object or JSON that has an email and the anemometer String
*/
async function storeMessage(ms) {
if (ms.string === undefined) {
return;
}
const windData = splitString(ms.string);
await prisma.messages.create({
data: {
email: ms.email,
stx: windData.stx,
node_address: windData.nodeAddress,
wind_direction: windData.windDirection,
wind_speed: windData.windSpeed,
units: windData.units,
status: windData.status,
etx: windData.etx,
checksum: windData.checksum,
raw_string: ms.string,
},
});
}
/**
* Get all registries in the messages schema
* @returns {Object} Every message stored
*/
async function getMessages() {
try {
const messages = await prisma.messages.findMany();
const data = JSON.stringify({ data: messages }, bigIntReplacer);
return JSON.parse(data);
}
catch (error) {
console.error("Error fetching messages:", error);
}
}
/**
* Get all registries in the messages schema based on the query parameters
* @param {Object} query {field: 'query string'}
* @returns {Object} Messages that match the query parameters
*/
async function getMessagesWhere(query) {
try {
const messages = await prisma.messages.findMany({
where: query,
});
const data = JSON.stringify({ data: messages }, bigIntReplacer);
return JSON.parse(data);
}
catch (error) {
console.error("Error fetching messages:", error);
}
}
const UNITS = {
M: "m/s",
N: "Nudos",
P: "mph",
K: "km/h",
F: "ft/min",
}
function splitString(rawData) {
// Raw data example: "␂Q,136,000.49,M,00,␃17"
const splitArray = rawData.split(',');
const windData = {
stx: splitArray[0][0],
nodeAddress: splitArray[0][1],
windDirection: parseInt(splitArray[1]),
windSpeed: parseFloat(splitArray[2]),
units: UNITS[splitArray[3]],
status: splitArray[4],
etx: splitArray[5][0],
checksum: splitArray[5][1] + (splitArray[5][2] ?? ""),
};
return windData;
}
function bigIntReplacer(key, value) {
if (typeof value === "bigint") {
return parseInt(value.toString()); // Convert BigInt to int
}
return value;
}
module.exports = {
storeMessage,
getMessages,
getMessagesWhere,
};