From a818f114ae2c00dd40ce8504c1ac1e479172ba1a Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Thu, 12 Sep 2024 17:59:09 -0400 Subject: [PATCH] feeature: add ability to configure temperature instances (#112) * feeature: add ability to configure temperature instances includes refactoring of temperature pgns * test: make so tests can have mulitple sets of options --- conversions/temperature.js | 367 ++++++++++++------------------------ conversions/temperature2.js | 278 --------------------------- test/test.js | 85 +++++---- 3 files changed, 163 insertions(+), 567 deletions(-) delete mode 100644 conversions/temperature2.js diff --git a/conversions/temperature.js b/conversions/temperature.js index 9f4ea9e..fc87024 100644 --- a/conversions/temperature.js +++ b/conversions/temperature.js @@ -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) + ] + }) } diff --git a/conversions/temperature2.js b/conversions/temperature2.js deleted file mode 100644 index d24b370..0000000 --- a/conversions/temperature2.js +++ /dev/null @@ -1,278 +0,0 @@ - -let tempMessage = (temp, inst, src) => { - return [{ - pgn: 130316, - SID: 0xff, - "Instance": inst, - "Source": src, - "Temperature": temp, - }] -} - -module.exports = (app, plugin) => { - return [{ - pgn: 130316, - title: 'Outside Temperature (130316)', - optionKey: 'TEMPERATURE2_OUTSIDE', - keys: [ - "environment.outside.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 101, 1) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 101, - "Source": "Outside Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Inside Temperature (130316)', - optionKey: 'TEMPERATURE2_INSIDE', - keys: [ - "environment.inside.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 102, 2) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 102, - "Source": "Inside Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Engine Room Temperature (130316)', - optionKey: 'TEMPERATURE2_ENGINEROOM', - keys: [ - "environment.inside.engineRoom.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 103, 3) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 103, - "Source": "Engine Room Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Refrigerator Temperature (130316)', - optionKey: 'TEMPERATURE2_refridgerator', - keys: [ - "environment.inside.refrigerator.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 7) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Refrigeration Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Freezer Temperature (130316)', - optionKey: 'TEMPERATURE2_FREEZER', - keys: [ - "environment.inside.freezer.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 13) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Freezer Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Main Cabin Temperature (130316)', - optionKey: 'TEMPERATURE2_MAINCABIN', - keys: [ - "environment.inside.mainCabin.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 4) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Main Cabin Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Heating System Temperature (130316)', - optionKey: 'TEMPERATURE2_HEATINGSYSTEM', - keys: [ - "environment.inside.heating.temperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 8) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Heating System Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Dew Point Temperature (130316)', - optionKey: 'TEMPERATURE2_DEWPOINT', - keys: [ - "environment.outside.dewPointTemperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 9) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Dew Point Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Apparent Wind Chill Temperature (130316)', - optionKey: 'TEMPERATURE2_APPARENTWINDCHILL', - keys: [ - "environment.outside.apparentWindChillTemperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 10) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Apparent Wind Chill Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Theoretical Wind Chill Temperature (130316)', - optionKey: 'TEMPERATURE2_THEORETICALWINDCHILL', - keys: [ - "environment.outside.theoreticalWindChillTemperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 11) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Theoretical Wind Chill Temperature", - "Temperature": 281.2 - } - }] - }] - }, - { - pgn: 130316, - title: 'Heat Index Temperature (130316)', - optionKey: 'TEMPERATURE2_HEATINDEX', - keys: [ - "environment.outside.heatIndexTemperature" - ], - callback: (temperature) => { - return tempMessage(temperature, 107, 12) - }, - tests: [{ - input: [ 281.2 ], - expected: [{ - "prio": 2, - "pgn": 130316, - "dst": 255, - "fields": { - "Instance": 107, - "Source": "Heat Index Temperature", - "Temperature": 281.2 - } - }] - }] - } - ] -} diff --git a/test/test.js b/test/test.js index 8c8a1a2..69346ec 100644 --- a/test/test.js +++ b/test/test.js @@ -46,7 +46,7 @@ describe('every conversion has a test', () => { if ( typeof subConversions === 'undefined' ) { subConversions = [ conversion ] } else if ( typeof subConversions === 'function' ) { - subConversions = subConversions(conversion.testOptions || {}) + subConversions = subConversions(Array.isArray(conversion.testOptions) ? conversion.testOptions[0] : conversion.testOptions) } assert(subConversions != undefined) subConversions.forEach(subConv => { @@ -65,48 +65,55 @@ describe('conversions work', () => { } conversion.forEach(conversion => { - var subConversions = conversion.conversions - if ( typeof subConversions === 'undefined' ) { - subConversions = [ conversion ] - } else if ( typeof subConversions === 'function' ) { - subConversions = subConversions(conversion.testOptions || {}) - } - subConversions.forEach(subConv => { - //subConv.should.have.property('tests') - if ( subConv.tests ) { - subConv.tests.forEach((test, idx) => { - it(`${conversion.title} test # ${idx} works`, function (done) { - skData = test.skData || {} - skSelfData = test.skSelfData || {} - let results = subConv.callback.call(null, ...test.input) - assert.equal(results.length, test.expected.length, 'number of results returned does not match the number of expected results') - let error - results.forEach((res, idx) => { - try - { - let encoded = pgnToActisenseSerialFormat(res) - let pgn = parser.parseString(encoded) - delete pgn.description - delete pgn.src - delete pgn.timestamp - delete pgn.input + let optionsList = Array.isArray(conversion.testOptions) ? conversion.testOptions : [ conversion.testOptions ] - let expected = test.expected[idx] - let preprocess = expected["__preprocess__"] - if ( preprocess ) { - preprocess(pgn) - delete expected["__preprocess__"] + optionsList.forEach((options, oidx) => { + var subConversions = conversion.conversions + if ( typeof subConversions === 'undefined' ) { + subConversions = [ conversion ] + } else if ( typeof subConversions === 'function' ) { + subConversions = subConversions(options || {}) + } + subConversions.forEach(subConv => { + //subConv.should.have.property('tests') + if ( subConv.tests ) { + subConv.tests.forEach((test, idx) => { + it(`${conversion.title} test # ${oidx}/${idx} works`, function (done) { + skData = test.skData || {} + skSelfData = test.skSelfData || {} + let results = subConv.callback.call(null, ...test.input) + assert.equal(results.length, test.expected.length, 'number of results returned does not match the number of expected results') + let error + results.forEach((res, idx) => { + try + { + let encoded = pgnToActisenseSerialFormat(res) + let pgn = parser.parseString(encoded) + delete pgn.description + delete pgn.src + delete pgn.timestamp + delete pgn.input + + let expected = test.expected[idx] + if ( typeof expected === 'function' ) { + expected = expected(options) + } + let preprocess = expected["__preprocess__"] + if ( preprocess ) { + preprocess(pgn) + delete expected["__preprocess__"] + } + //console.log('parsed: ' + JSON.stringify(pgn, null, 2)) + pgn.should.jsonEqual(expected) + } catch ( e ) { + error = e } - //console.log('parsed: ' + JSON.stringify(pgn, null, 2)) - pgn.should.jsonEqual(expected) - } catch ( e ) { - error = e - } + }) + done(error) }) - done(error) }) - }) - } + } + }) }) }) })