Power Station Simulator
This section contains the Python API documentation for the power station simulation code.
The following modules are documented here:
main()
Main function to run the Power Station Simulator.
This function: 1. Sets up command-line argument parsing 2. Loads configuration based on the provided station prefix 3. Initializes the station simulator with the configuration 4. Runs the simulator in an infinite loop until interrupted 5. Performs graceful shutdown on keyboard interrupt
Command-line arguments
-sp, --station-prefix: Prefix for power station-specific environment variables (e.g., PS_001)
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/powerstation_simulator/main.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
StationSimulator
Simulates a power station that publishes its status, metadata, and output via MQTT.
The simulator can be in one of two states: - Online but not running (power output is 0) - Online and running (generating power)
The class handles MQTT communication, publishes information on regular intervals, and responds to control commands to start or stop power generation.
Attributes:
Name | Type | Description |
---|---|---|
metadata_topic |
str
|
Topic for publishing station metadata |
output_topic |
str
|
Topic for publishing power output |
status_topic |
str
|
Topic for publishing operational status |
control_topic |
str
|
Topic for receiving control commands |
Source code in src/powerstation_simulator/station_simulator.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
|
__handle_control(client, userdata, message)
Callback handler for MQTT control messages.
Parses control commands and changes the station's running state accordingly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
Any
|
MQTT client instance (not used) |
required |
userdata
|
Any
|
MQTT user data (not used) |
required |
message
|
Any
|
MQTT message containing the control command |
required |
Source code in src/powerstation_simulator/station_simulator.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
__init__(app_config)
Initialize the station simulator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app_config
|
AppConfig
|
Configuration object containing MQTT settings, power station details, and publishing intervals |
required |
Source code in src/powerstation_simulator/station_simulator.py
36 37 38 39 40 41 42 43 44 45 46 47 |
|
__publish_metadata_loop()
Background loop that publishes metadata at regular intervals.
Runs continuously while the station is online.
Source code in src/powerstation_simulator/station_simulator.py
147 148 149 150 151 152 153 154 155 |
|
__publish_output_loop()
Background loop that publishes power output at regular intervals.
Runs continuously while the station is online.
Source code in src/powerstation_simulator/station_simulator.py
167 168 169 170 171 172 173 174 175 |
|
__publish_status_loop()
Background loop that publishes status at regular intervals.
Runs continuously while the station is online.
Source code in src/powerstation_simulator/station_simulator.py
157 158 159 160 161 162 163 164 165 |
|
control(is_start)
Change the running state of the power station.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_start
|
bool
|
True to start power generation, False to stop |
required |
Source code in src/powerstation_simulator/station_simulator.py
199 200 201 202 203 204 205 206 207 208 |
|
publish_metadata()
Publish the power station's metadata to MQTT.
Publishes location and capacity information to the metadata topic.
Source code in src/powerstation_simulator/station_simulator.py
111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
publish_output()
Publish the current power output to MQTT.
If the station is running, publishes a simulated output value; otherwise, publishes 0.
Source code in src/powerstation_simulator/station_simulator.py
136 137 138 139 140 141 142 143 144 145 |
|
publish_status()
Publish the power station's current status to MQTT.
Status will be either "running" (if generating power) or "online" (if not generating).
Source code in src/powerstation_simulator/station_simulator.py
125 126 127 128 129 130 131 132 133 134 |
|
shutdown_sequence()
Safely shut down the station simulator.
Sets the station to offline state, waits for all publisher threads to complete, and disconnects from MQTT broker.
Source code in src/powerstation_simulator/station_simulator.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
simulate_output()
Generate a simulated power output value.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
Simulated power output in kilowatts, a random value between 80% and 120% of the configured capacity |
Source code in src/powerstation_simulator/station_simulator.py
98 99 100 101 102 103 104 105 106 107 108 109 |
|
startup_sequence()
Start the station simulator and initialize all communication threads.
Connects to MQTT broker, subscribes to control topic, and starts threads for publishing metadata, status, and power output.
Source code in src/powerstation_simulator/station_simulator.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
MQTTClient
A client for connecting to an MQTT broker and publishing/subscribing to topics.
This class provides methods to connect to an MQTT broker, publish messages, subscribe to topics, and handle connection events.
Source code in src/powerstation_simulator/mqtt_client.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
__init__(client_id, host, port, username, password, enable_websocket)
Initialize an MQTT client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_id
|
str
|
Unique identifier for this client |
required |
host
|
str
|
MQTT broker hostname or IP address |
required |
port
|
int
|
MQTT broker port |
required |
username
|
str
|
Authentication username |
required |
password
|
str
|
Authentication password |
required |
enable_websocket
|
bool
|
Use websockets instead of TCP if True |
required |
Source code in src/powerstation_simulator/mqtt_client.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
connect()
Connect to the MQTT broker and start the network loop.
Source code in src/powerstation_simulator/mqtt_client.py
55 56 57 58 59 60 |
|
disconnect()
Disconnect from the MQTT broker and stop the network loop.
Source code in src/powerstation_simulator/mqtt_client.py
62 63 64 65 66 67 |
|
on_connect(client, userdata, flags, rc)
Callback for when the client connects to the broker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
Client
|
The client instance |
required |
userdata
|
Any
|
User data of any type |
required |
flags
|
dict
|
Response flags sent by the broker |
required |
rc
|
int
|
Connection result code |
required |
Source code in src/powerstation_simulator/mqtt_client.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
on_disconnect(client, userdata, rc)
Callback for when the client disconnects from the broker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
Client
|
The client instance |
required |
userdata
|
Any
|
User data of any type |
required |
rc
|
int
|
Disconnection result code |
required |
Source code in src/powerstation_simulator/mqtt_client.py
87 88 89 90 91 92 93 94 95 96 97 |
|
publish(topic, payload, qos=1)
Publish a message to a topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic
|
str
|
The topic to publish to |
required |
payload
|
Any
|
The message to publish (dictionaries will be JSON-encoded) |
required |
qos
|
int
|
Quality of Service level. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the message was published successfully, False otherwise |
Source code in src/powerstation_simulator/mqtt_client.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
subscribe(topic, qos=1, on_message=None)
Subscribe to a topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic
|
str
|
The topic to subscribe to |
required |
qos
|
int
|
Quality of Service level. Defaults to 1. |
1
|
on_message
|
callable
|
Callback for when a message is received. If None, a default handler will be used. |
None
|
Source code in src/powerstation_simulator/mqtt_client.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
get_mqtt_client(app_config)
Create and return an MQTT client using configuration from the application settings.
This function initializes an MQTTClient instance with connection parameters extracted from the provided AppConfig object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app_config
|
AppConfig
|
Application configuration containing MQTT connection settings |
required |
Returns:
Name | Type | Description |
---|---|---|
MQTTClient |
MQTTClient
|
A configured MQTT client instance ready for connection |
Source code in src/powerstation_simulator/mqtt_client.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
AppConfig
Bases: BaseSettings
AppConfig is the main configuration class for the power station application.
This class defines all the necessary configuration parameters for the application, including station metadata, MQTT broker settings, and simulator configurations. It uses Pydantic's BaseSettings for environment variable loading and validation.
Environment variables are loaded from a .env.sample file by default.
Source code in src/powerstation_simulator/config.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
load_power_station_configs(station_prefix=None)
Dynamically loads the config for a specific power station prefix, e.g., 'PS_001'. If no prefix is provided explicitly, reads from the STATION_PREFIX env var.
Source code in src/powerstation_simulator/config.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
ColoredFormatter
Bases: Formatter
A custom logging formatter that adds color to log level names in terminal output.
This formatter wraps the log level name with ANSI color codes based on the severity level. Colors are defined in the COLORS dictionary.
Methods:
Name | Description |
---|---|
format |
Overrides the base Formatter's format method to add colors. |
Source code in src/powerstation_simulator/logger.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
format(record)
Format the specified record with colored level names.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record
|
LogRecord
|
A LogRecord object containing all the information needed to generate a log message. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The formatted log message with colored level name. |
Source code in src/powerstation_simulator/logger.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
getLogger(name)
Get a logger with the specified name, ensuring the base logger is configured.
This function calls setup_base_logger() to ensure that logging is properly configured with colored output, then returns a logger with the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
A string that identifies the logger. |
required |
Returns:
Type | Description |
---|---|
Logger
|
logging.Logger: A configured logger instance with the specified name. |
Source code in src/powerstation_simulator/logger.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
setup_base_logger(level=logging.DEBUG)
Configure and return the root logger with colored output.
This function sets up the root logger with a StreamHandler that outputs to stdout and formats messages using the ColoredFormatter. If the root logger already has handlers configured, this function does nothing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
level
|
int | str
|
The logging level to set for the root logger. Default is logging.DEBUG. |
DEBUG
|
Returns:
Name | Type | Description |
---|---|---|
Logger |
Logger
|
The configured root logger instance. |
Source code in src/powerstation_simulator/logger.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|