Skip to content

Commit

Permalink
feature: add ability to switch solar charger on or off (#114)
Browse files Browse the repository at this point in the history
add new path 'modeSwitch', which supports PUT
  • Loading branch information
sbender9 authored Aug 25, 2022
1 parent 38c0109 commit 8a84317
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
35 changes: 25 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ module.exports = function (app) {
}
}

function chargerModeActionHandler(context, path, value, dest, cb) {
app.debug(`setting charger mode ${dest} to ${value}`)
function modeActionHandler(context, path, value, dest, cb) {
app.debug(`setting mode ${dest} to ${value}`)

if ( plugin.options.installType === 'mqtt' ) {
plugin.client.publish(dest, JSON.stringify({ value }))
Expand All @@ -238,12 +238,13 @@ module.exports = function (app) {
return { state: 'PENDING' }
}

function getChargerModeActionHandler(dest) {
function getModeActionHandler(dest) {
return (context, path, value, cb) => {
return chargerModeActionHandler(context, path, value, dest, cb)
return modeActionHandler(context, path, value, dest, cb)
}
}


/*
Called when the plugin is started (server is started with plugin enabled
or the plugin is enabled from ui on a running server).
Expand Down Expand Up @@ -290,13 +291,20 @@ module.exports = function (app) {
venusMessages => {

venusMessages.forEach(m => {
if ( m.senderName.startsWith('com.victronenergy.vebus')
if ( (m.senderName.startsWith('com.victronenergy.vebus')
|| m.senderName.startsWith('com.victronenergy.solarcharger'))
&& m.path === '/Mode'
&& modesRegistered.indexOf(m.senderName) == -1 ) {
const path = `electrical.chargers.${m.instanceName}.modeNumber`
let path

if ( m.senderName.startsWith('com.victronenergy.vebus') ) {
path = `electrical.chargers.${m.instanceName}.modeNumber`
} else {
path = `electrical.solar.${m.instanceName}.modeSwitch.state`
}
app.registerActionHandler('vessels.self',
path,
getChargerModeActionHandler(m.senderName))
getModeActionHandler(m.senderName))
modesRegistered.push(m.senderName)
}
})
Expand Down Expand Up @@ -549,14 +557,21 @@ module.exports = function (app) {

//app.debug(JSON.stringify(m))

if ( m.senderName.startsWith('com.victronenergy.vebus')
if ( (m.senderName.startsWith('com.victronenergy.vebus')
|| m.senderName.startsWith('com.victronenergy.solarcharger'))
&& m.path === '/Mode'
&& modesRegistered.indexOf(m.senderName) === -1 ) {
const path = `electrical.chargers.${m.instanceName}.modeNumber`
let path

if ( m.senderName.startsWith('com.victronenergy.vebus') ) {
path = `electrical.chargers.${m.instanceName}.modeNumber`
} else {
path = `electrical.solar.${m.instanceName}.modeSwitch.state`
}
const wtopic = 'W' + topic.slice(1)
app.registerActionHandler('vessels.self',
path,
getChargerModeActionHandler(wtopic))
getModeActionHandler(wtopic))
modesRegistered.push(m.senderName)
} else if ( m.senderName.startsWith('com.victronenergy.system')
&& parts.length > 6
Expand Down
19 changes: 17 additions & 2 deletions venusToDeltas.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ module.exports = function (app, options, handleMessage) {
path: m => {
return makePath(m, `${m.instanceName}.modeNumber`)
}
},
{
path: m => {
return m.senderName.startsWith('com.victronenergy.solarcharger') ?
makePath(m, `${m.instanceName}.modeSwitch.state`) : null
},
conversion: m => {
return m.value === 1 ? 1 : 0
}
}
],
'/ErrorCode': {
Expand Down Expand Up @@ -608,6 +617,10 @@ module.exports = function (app, options, handleMessage) {

var thePath = isFunction(mapping.path) ? mapping.path(m) : mapping.path

if ( thePath === null ) {
return
}

if (mapping.conversion) {
theValue = mapping.conversion(m, thePath)
}
Expand All @@ -628,7 +641,7 @@ module.exports = function (app, options, handleMessage) {
if ( knownPaths.indexOf(thePath) == -1 )
{
knownPaths.push(thePath)
if ( mapping.units && app.supportsMetaDeltas ) {
if ( mapping.units && app && app.supportsMetaDeltas ) {
let meta = {updates: [ { meta: [{ path: thePath, value: {units: mapping.units} }] } ]}
deltas.push(meta)
}
Expand All @@ -642,6 +655,7 @@ module.exports = function (app, options, handleMessage) {
&& m.senderName.startsWith('com.victronenergy.vebus')
&& m.path === '/Mode'
&& thePath.endsWith('modeNumber')
&& app
&& app.supportsMetaDeltas) {
deltas.push({updates: [ { meta: [{ path: thePath, value: modeMeta }] } ]})
sentModeMeta = true
Expand Down Expand Up @@ -1041,7 +1055,8 @@ function makeDelta (app, m, path, value) {
]
}

if (!app.supportsMetaDeltas
if (app
&& !app.supportsMetaDeltas
&& m.senderName.startsWith('com.victronenergy.vebus')
&& m.path === '/Mode'
&& path.endsWith('modeNumber'))
Expand Down

0 comments on commit 8a84317

Please sign in to comment.