Skip to content

Commit

Permalink
#201 Write PLC state to log files (1)
Browse files Browse the repository at this point in the history
Add `TUMPLCLogger` class
  • Loading branch information
dostuffthatmatters committed Nov 8, 2023
1 parent 050ae7c commit 2742f2d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ pids
runtime-data/
config/**/*.json
!config/**/*.default.json
logs/**/*
logs/**/*.json
logs/**/*.log
logs/**/*.png
logs/**/*.jpg
!logs/**/.gitkeep
nohup.out

Expand Down Expand Up @@ -79,7 +82,6 @@ Temporary Items

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
Expand Down
Empty file added logs/tum-plc/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions packages/core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .functions import read_last_file_line
from .helios_image_processing import HeliosImageProcessing
from .logger import Logger
from .tum_plc_logger import TUMPLCLogger
47 changes: 47 additions & 0 deletions packages/core/utils/tum_plc_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import datetime
import os
import tum_esm_utils
from packages.core import types

_PROJECT_DIR = tum_esm_utils.files.get_parent_dir_path(
__file__, current_depth=4
)


class TUMPLCLogger:
"""A class to save the current PLC state to a log file."""
@staticmethod
def log(config: types.Config, state: types.StateObject) -> None:
now = datetime.datetime.now()
timestamp = now.timestamp()
datetime_string = now.isoformat()

log_line = [
timestamp,
datetime_string,
state.position.sun_elevation,
state.plc_state.power.heater,
state.plc_state.power.spectrometer,
state.plc_state.state.rain,
state.plc_state.actors.fan_speed,
state.plc_state.actors.current_angle,
state.plc_state.sensors.temperature,
state.plc_state.sensors.humidity,
]
stringed_log_line = [
str(item).lower() if item is not None else "null"
for item in log_line
]
current_log_file = os.path.join(
_PROJECT_DIR, "logs", "tum-plc",
f"{config.general.station_id}-plc-logs-{now.strftime('%Y-%m-%d')}.csv"
)
if not os.path.isfile(current_log_file):
with open(current_log_file, "w") as f:
f.write(
"timestamp,datetime,sun_elevation,heater_power," +
"spectrometer_power,rain_detected,fan_speed," +
"cover_angle,temperature,humidity\n"
)
with open(current_log_file, "a") as f:
f.write(",".join(stringed_log_line) + "\n")

0 comments on commit 2742f2d

Please sign in to comment.