-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Publish | ||
|
||
Optionally configure the active limits to be published to an external system. | ||
|
||
[[toc]] | ||
|
||
## MQTT | ||
|
||
Write active limits to a MQTT topic. | ||
|
||
To configure a MQTT output, add the following property to `config.json` | ||
|
||
```js | ||
{ | ||
"publish": { | ||
"mqtt": { | ||
"host": "mqtt://192.168.1.2", // (string) required: the MQTT broker host | ||
"username": "user", // (string) optional: the MQTT broker username | ||
"password": "password", // (string) optional: the MQTT broker password | ||
"topic": "limits" // (string) required: the MQTT topic to write | ||
} | ||
} | ||
... | ||
} | ||
``` | ||
|
||
The MQTT topic will contain a JSON message that meets the following Zod schema | ||
|
||
```ts | ||
const inverterControlTypesSchema = z.union([ | ||
z.literal("fixed"), | ||
z.literal("mqtt"), | ||
z.literal("sep2"), | ||
z.literal("twoWayTariff"), | ||
z.literal("negativeFeedIn") | ||
]) | ||
|
||
const activeInverterControlLimitSchema = z.object({ | ||
opModEnergize: z.union([ | ||
z.object({ | ||
value: z.boolean(), | ||
source: inverterControlTypesSchema | ||
}), | ||
z.undefined() | ||
]), | ||
opModConnect: z.union([ | ||
z.object({ | ||
value: z.boolean(), | ||
source: inverterControlTypesSchema | ||
}), | ||
z.undefined() | ||
]), | ||
opModGenLimW: z.union([ | ||
z.object({ | ||
value: z.number(), | ||
source: inverterControlTypesSchema | ||
}), | ||
z.undefined() | ||
]), | ||
opModExpLimW: z.union([ | ||
z.object({ | ||
value: z.number(), | ||
source: inverterControlTypesSchema | ||
}), | ||
z.undefined() | ||
]), | ||
opModImpLimW: z.union([ | ||
z.object({ | ||
value: z.number(), | ||
source: inverterControlTypesSchema | ||
}), | ||
z.undefined() | ||
]), | ||
opModLoadLimW: z.union([ | ||
z.object({ | ||
value: z.number(), | ||
source: inverterControlTypesSchema | ||
}), | ||
z.undefined() | ||
]) | ||
}) | ||
``` | ||
|
||
For example | ||
|
||
```js | ||
{ | ||
"opModEnergize": { | ||
"source": "sep2", | ||
"value": true | ||
}, | ||
"opModConnect": { | ||
"source": "sep2", | ||
"value": true | ||
}, | ||
"opModExpLimW": { | ||
"source": "sep2", | ||
"value": 1500 | ||
}, | ||
"opModImpLimW": { | ||
"source": "sep2", | ||
"value": 1500 | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import mqtt from 'mqtt'; | ||
import { type Config } from '../../helpers/config.js'; | ||
import { type ActiveInverterControlLimit } from './inverterController.js'; | ||
|
||
export class Publish { | ||
private mqttClient: mqtt.MqttClient | undefined; | ||
|
||
constructor({ config }: { config: Config }) { | ||
if (config.publish?.mqtt) { | ||
this.mqttClient = mqtt.connect(config.publish.mqtt.host, { | ||
username: config.publish.mqtt.username, | ||
password: config.publish.mqtt.password, | ||
}); | ||
} | ||
} | ||
|
||
onActiveInverterControlLimit({ | ||
limit, | ||
}: { | ||
limit: ActiveInverterControlLimit; | ||
}) { | ||
if (this.mqttClient) { | ||
this.mqttClient.publish( | ||
'inverterControlLimit', | ||
JSON.stringify(limit), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters