-
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.
Merge pull request #74 from longzheng/inverters-mqtt
MQTT inverter support
- Loading branch information
Showing
10 changed files
with
358 additions
and
39 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 |
---|---|---|
@@ -1,30 +1,42 @@ | ||
import { type DERTyp } from '../connections/sunspec/models/nameplate.js'; | ||
import { type ConnectStatusValue } from '../sep2/models/connectStatus.js'; | ||
import { type OperationalModeStatusValue } from '../sep2/models/operationModeStatus.js'; | ||
import { z } from 'zod'; | ||
import { DERTyp } from '../connections/sunspec/models/nameplate.js'; | ||
import { connectStatusValueSchema } from '../sep2/models/connectStatus.js'; | ||
import { OperationalModeStatusValue } from '../sep2/models/operationModeStatus.js'; | ||
import { type SampleBase } from '../coordinator/helpers/sampleBase.js'; | ||
|
||
export type InverterData = { | ||
date: Date; | ||
inverter: { | ||
realPower: number; | ||
reactivePower: number; | ||
voltagePhaseA: number | null; | ||
voltagePhaseB: number | null; | ||
voltagePhaseC: number | null; | ||
frequency: number; | ||
}; | ||
nameplate: { | ||
type: DERTyp; | ||
maxW: number; | ||
maxVA: number; | ||
maxVar: number; | ||
}; | ||
settings: { | ||
maxW: number; | ||
maxVA: number | null; | ||
maxVar: number | null; | ||
}; | ||
status: { | ||
operationalModeStatus: OperationalModeStatusValue; | ||
genConnectStatus: ConnectStatusValue; | ||
}; | ||
}; | ||
export const inverterDataSchema = z.object({ | ||
inverter: z.object({ | ||
/** | ||
* Positive values = inverter export (produce) power | ||
* | ||
* Negative values = inverter import (consume) power | ||
* | ||
* Value is total (net across all phases) measurement | ||
*/ | ||
realPower: z.number(), | ||
reactivePower: z.number(), | ||
voltagePhaseA: z.number().nullable(), | ||
voltagePhaseB: z.number().nullable(), | ||
voltagePhaseC: z.number().nullable(), | ||
frequency: z.number(), | ||
}), | ||
nameplate: z.object({ | ||
type: z.nativeEnum(DERTyp), | ||
maxW: z.number(), | ||
maxVA: z.number(), | ||
maxVar: z.number(), | ||
}), | ||
settings: z.object({ | ||
maxW: z.number(), | ||
maxVA: z.number().nullable(), | ||
maxVar: z.number().nullable(), | ||
}), | ||
status: z.object({ | ||
operationalModeStatus: z.nativeEnum(OperationalModeStatusValue), | ||
genConnectStatus: connectStatusValueSchema, | ||
}), | ||
}); | ||
|
||
export type InverterDataBase = z.infer<typeof inverterDataSchema>; | ||
|
||
export type InverterData = SampleBase & InverterDataBase; |
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,77 @@ | ||
import { | ||
type InverterDataBase, | ||
inverterDataSchema, | ||
type InverterData, | ||
} from '../inverterData.js'; | ||
import { InverterDataPollerBase } from '../inverterDataPollerBase.js'; | ||
import { type Config } from '../../helpers/config.js'; | ||
import mqtt from 'mqtt'; | ||
|
||
export class MqttInverterDataPoller extends InverterDataPollerBase { | ||
private client: mqtt.MqttClient; | ||
private cachedMessage: InverterDataBase | null = null; | ||
|
||
constructor({ | ||
mqttConfig, | ||
inverterIndex, | ||
applyControl, | ||
}: { | ||
mqttConfig: Extract<Config['inverters'][number], { type: 'mqtt' }>; | ||
inverterIndex: number; | ||
applyControl: boolean; | ||
}) { | ||
super({ | ||
name: 'MqttInverterDataPoller', | ||
pollingIntervalMs: mqttConfig.pollingIntervalMs, | ||
applyControl, | ||
inverterIndex, | ||
}); | ||
|
||
this.client = mqtt.connect(mqttConfig.host, { | ||
username: mqttConfig.username, | ||
password: mqttConfig.password, | ||
}); | ||
|
||
this.client.on('connect', () => { | ||
this.client.subscribe(mqttConfig.topic); | ||
}); | ||
|
||
this.client.on('message', (_topic, message) => { | ||
const data = message.toString(); | ||
|
||
const result = inverterDataSchema.safeParse(JSON.parse(data)); | ||
|
||
if (!result.success) { | ||
this.logger.error({ | ||
message: `Invalid MQTT message. Error: ${result.error.message}`, | ||
data, | ||
}); | ||
return; | ||
} | ||
|
||
this.cachedMessage = result.data; | ||
}); | ||
|
||
void this.startPolling(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
override async getInverterData(): Promise<InverterData> { | ||
if (!this.cachedMessage) { | ||
throw new Error('No inverter data on MQTT'); | ||
} | ||
|
||
return { date: new Date(), ...this.cachedMessage }; | ||
} | ||
|
||
override onDestroy(): void { | ||
this.client.end(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
override async onControl(): Promise<void> { | ||
if (this.applyControl) { | ||
throw new Error('Unable to control MQTT inverter'); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.