Skip to content

Commit 1a1dfd7

Browse files
committed
Cache data from the serial buffer to try and improve reliability
1 parent d99f41d commit 1a1dfd7

File tree

5 files changed

+69
-26
lines changed

5 files changed

+69
-26
lines changed

package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "PSCWeather",
23
"scripts": {
34
"start": "node ./src/main.js"
45
},

src/main.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const SerialPort = require('serialport')
22
const Delimiter = require('@serialport/parser-delimiter')
33
const logger = require("./logger")
44
const responseParser = require("./parser")
5+
const responseValidator = require("./validator")
56
const sleep = require("./sleep")
67
const windy = require("./targets/windy")
78
const windguru = require("./targets/windguru")
@@ -44,17 +45,43 @@ function querySerial() {
4445
querySerial() //Request again
4546
})
4647
}
48+
/***
49+
* Cache the serial connection as the messages can come through from the device in chunks
50+
*/
51+
let serialCache = {
52+
"cache": Buffer.from([]),
53+
"updated": Date.now()
54+
}
55+
function cacheSerial(message) { //Build up a bit of a cache of serial messages because they seem to come in chunks
56+
if (serialCache["updated"] < (Date.now() - 65000)) { //If the cache is older than 65 seconds
57+
serialCache["cache"] = Buffer.from([]) //Reset the cache
58+
}
59+
serialCache["updated"] = Date.now()
60+
serialCache["cache"] = Buffer.concat([serialCache["cache"],message]) //Add the new message to the cache
61+
62+
let offset = responseValidator(serialCache["cache"]) //Check if the cache is a complete message
63+
if (offset !== false) { // If we have a full message then parse it, otherwise wait and let the cache build up a bit
64+
const response = responseParser(serialCache["cache"], offset) //Parse the message
65+
serialCache = { //Reset the serialCache
66+
"cache": Buffer.from([]),
67+
"updated": Date.now()
68+
}
69+
if (response) {
70+
logger.log("debug","Received parsed weather data", response)
71+
windy(response) // Send the data to windy
72+
windguru(response) // Send the data to windguru
73+
}
74+
}
75+
}
76+
/**
77+
* Handle messages from the device
78+
*/
4779
parser.on('data', function(message) {
4880
if (notHeadFromDevice) {
4981
notHeadFromDevice = false
5082
querySerial() // Trigger the first query, function then starts calling itself
5183
}
52-
const response = responseParser(message)
53-
if (response) {
54-
logger.log("debug","Received parsed weather data", response)
55-
windy(response)
56-
windguru(response)
57-
}
84+
cacheSerial(message)
5885
})
5986

6087

src/parser.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,7 @@ const logger = require("./logger")
33
* Script to parse a buffer of the format "loop" and turn it into to an object
44
* @param {Buffer} inputBuffer
55
*/
6-
const parser = (inputBuffer) => {
7-
let offset = false
8-
if (inputBuffer.length < 40) {
9-
logger.log("warn",`Buffer received too short - length is ${inputBuffer.length}`,inputBuffer)
10-
return false
11-
}
12-
for (let thisOffset = 0; thisOffset < (inputBuffer.length-3); thisOffset++) {
13-
if (inputBuffer.slice(thisOffset,thisOffset+3).toString('utf8') === "LOO") {
14-
offset = thisOffset
15-
break
16-
}
17-
}
18-
if (!offset) {
19-
logger.log("warn","Buffer received doesn't contain LOO (76,79,79)",inputBuffer)
20-
return false
21-
} else {
22-
logger.log("debug",`This offset is ${offset}`)
23-
}
24-
6+
const parser = (inputBuffer, offset) => {
257
const data = {
268
temperatureRaw: inputBuffer.readUInt16LE(offset+12), // In degrees F multiplied by 10
279
windDirection: inputBuffer.readUInt16LE(offset+16), //In degrees

src/validator.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const logger = require("./logger")
2+
/**
3+
* Script to validate a buffer is of the format "loop"
4+
* @param {Buffer} inputBuffer
5+
*/
6+
const validator = (inputBuffer) => {
7+
let offset = false
8+
const inputBufferLength = inputBuffer.length
9+
if (inputBufferLength < 40) {
10+
logger.log("debug",`Buffer received too short - length is ${inputBufferLength}`,inputBuffer)
11+
return false
12+
}
13+
for (let thisOffset = 0; thisOffset < (inputBufferLength-3); thisOffset++) {
14+
if (inputBuffer.slice(thisOffset,thisOffset+3).toString('utf8') === "LOO") {
15+
offset = thisOffset
16+
break
17+
}
18+
}
19+
if (!offset) {
20+
logger.log("debug","Buffer received doesn't contain LOO (76,79,79)",inputBuffer)
21+
return false
22+
}
23+
if ((inputBufferLength-offset) < 40) {
24+
logger.log("debug",`Buffer received too short - length is ${inputBufferLength} and offset is ${offset} so there is not 40 after the offset`,inputBuffer)
25+
return false
26+
}
27+
28+
logger.log("debug",`This offset is ${offset}`)
29+
return offset
30+
}
31+
module.exports = validator

0 commit comments

Comments
 (0)