Skip to content

Commit

Permalink
feeature: add ability to configure temperature instances (#112)
Browse files Browse the repository at this point in the history
* feeature: add ability to configure temperature instances

includes refactoring of temperature pgns

* test: make so tests can have mulitple sets of options
  • Loading branch information
sbender9 authored Sep 12, 2024
1 parent 0d42887 commit a818f11
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 567 deletions.
367 changes: 117 additions & 250 deletions conversions/temperature.js
Original file line number Diff line number Diff line change
@@ -1,278 +1,145 @@

let tempMessage = (temp, inst, src) => {
return [{
pgn: 130312,
SID: 0xff,
"Instance": inst,
"Source": src,
"Actual Temperature": temp,
}]
let tempMessage = (pgn, temp, inst, src) => {
return {
pgn,
prio: 2,
dst: 255,
fields: {
"Instance": inst,
"Source": src,
[pgn == 130316 ? "Temperature" : "Actual Temperature"]: temp
}
}
}

module.exports = (app, plugin) => {
return [{
pgn: 130312,
title: 'Outside Temperature (130312)',
optionKey: 'TEMPERATURE_OUTSIDE',
keys: [
"environment.outside.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 101, 1)
function makeTemperature(pgn, prefix, info)
{
let optionKey = `${prefix}_${info.option}`
return {
pgn,
title: `${info.n2kSource} (${pgn})`,
optionKey,
keys: [ info.source ],
properties: {
instance: {
title: 'N2K Temperature Instance',
type: 'number',
default: info.instance
},
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 101,
"Source": "Outside Temperature",
"Actual Temperature": 281.2

testOptions: [
{
[optionKey]: {
instance: 0
}
},
{
[optionKey]: {
}
}
],

conversions: (options) => {
let instance = options[optionKey].instance
if ( instance === undefined )
instance = info.instance
return [{
keys: [ info.source ],
callback: (temperature) => {
return [ tempMessage(pgn, temperature, instance, info.n2kSource) ]
},
tests: [
{
input: [ 281.2 ],
expected: [
(testOptions) => {
let expectedInstance = testOptions[optionKey].instance !== undefined ? testOptions[optionKey].instance : info.instance
return tempMessage(pgn, 281.2, expectedInstance, info.n2kSource)
}
]
}
]
}]
}]
}
}
}


const temperatures = [
{
n2kSource: "Outside Temperature",
source: 'environment.outside.temperature',
instance: 101,
option: 'OUTSIDE'
},
{
pgn: 130312,
title: 'Inside Temperature (130312)',
optionKey: 'TEMPERATURE_INSIDE',
keys: [
"environment.inside.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 102, 2)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 102,
"Source": "Inside Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Inside Temperature",
source: 'environment.inside.temperature',
instance: 102,
option: 'INSIDE'
},
{
pgn: 130312,
title: 'Engine Room Temperature (130312)',
optionKey: 'TEMPERATURE_ENGINEROOM',
keys: [
"environment.inside.engineRoom.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 103, 3)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 103,
"Source": "Engine Room Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Engine Room Temperature",
source: 'environment.inside.engineRoom.temperature',
instance: 103,
option: 'ENGINEROOM'
},
{
pgn: 130312,
title: 'Refrigerator Temperature (130312)',
optionKey: 'TEMPERATURE_refridgerator',
keys: [
"environment.inside.refrigerator.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 7)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Refrigeration Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Main Cabin Temperature",
source: 'environment.inside.mainCabin.temperature',
instance: 107 ,
option: 'MAINCABIN'
},
{
pgn: 130312,
title: 'Freezer Temperature (130312)',
optionKey: 'TEMPERATURE_FREEZER',
keys: [
"environment.inside.freezer.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 13)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Freezer Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Refrigeration Temperature",
source: 'environment.inside.refrigerator.temperature',
instance: 107 ,
option: 'refridgerator'
},
{
pgn: 130312,
title: 'Main Cabin Temperature (130312)',
optionKey: 'TEMPERATURE_MAINCABIN',
keys: [
"environment.inside.mainCabin.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 4)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Main Cabin Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Heating System Temperature",
source: 'environment.inside.heating.temperature',
instance: 107,
option: 'HEATINGSYSTEM'
},
{
pgn: 130312,
title: 'Heating System Temperature (130312)',
optionKey: 'TEMPERATURE_HEATINGSYSTEM',
keys: [
"environment.inside.heating.temperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 8)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Heating System Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Dew Point Temperature",
source: 'environment.outside.dewPointTemperature',
instance:107 ,
option: 'DEWPOINT'
},
{
pgn: 130312,
title: 'Dew Point Temperature (130312)',
optionKey: 'TEMPERATURE_DEWPOINT',
keys: [
"environment.outside.dewPointTemperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 9)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Dew Point Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Apparent Wind Chill Temperature",
source: 'environment.outside.apparentWindChillTemperature',
instance: 107,
option: 'APPARENTWINDCHILL'
},
{
pgn: 130312,
title: 'Apparent Wind Chill Temperature (130312)',
optionKey: 'TEMPERATURE_APPARENTWINDCHILL',
keys: [
"environment.outside.apparentWindChillTemperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 10)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Apparent Wind Chill Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Theoretical Wind Chill Temperature",
source: 'environment.outside.theoreticalWindChillTemperature',
instance: 107 ,
option: 'THEORETICALWINDCHILL'
},
{
pgn: 130312,
title: 'Theoretical Wind Chill Temperature (130312)',
optionKey: 'TEMPERATURE_THEORETICALWINDCHILL',
keys: [
"environment.outside.theoreticalWindChillTemperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 11)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Theoretical Wind Chill Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Heat Index Temperature",
source: 'environment.outside.heatIndexTemperature',
instance: 107 ,
option: 'HEATINDEX'
},
{
pgn: 130312,
title: 'Heat Index Temperature (130312)',
optionKey: 'TEMPERATURE_HEATINDEX',
keys: [
"environment.outside.heatIndexTemperature"
],
callback: (temperature) => {
return tempMessage(temperature, 107, 12)
},
tests: [{
input: [ 281.2 ],
expected: [{
"prio": 2,
"pgn": 130312,
"dst": 255,
"fields": {
"Instance": 107,
"Source": "Heat Index Temperature",
"Actual Temperature": 281.2
}
}]
}]
n2kSource: "Freezer Temperature",
source: 'environment.inside.freezer.temperature',
instance: 107 ,
option: 'FREEZER'
}
]
]

module.exports = (app, plugin) => {
return temperatures.flatMap(info => {
return [
makeTemperature(130312, 'TEMPERATURE', info),
makeTemperature(130316, 'TEMPERATURE2', info)
]
})
}
Loading

0 comments on commit a818f11

Please sign in to comment.