Added custom scripts base for scripting custom functions

main
Jan Beníček 2024-12-10 16:54:43 +01:00
parent 6df4788cca
commit 6a2cfc1fa0
9 changed files with 100 additions and 11 deletions

View File

@ -1,4 +1,5 @@
MODULE_RTD8_LEVELS=01234567 #number of stack level (0-7) not separated
MODULE_REL4HVI4_LEVELS=01234567 #number of stack level (0-7) not separated
MODULE_INDUSTRIALAUTOMATION_LEVELS=01234567 #number of stack level (0-7) not separated
VARIABLES_SAVE_DIR=variables_storage #path to dir for saving variables
VARIABLES_SAVE_DIR=variables_storage #path to dir for saving variables
BACKGROUND_SCRIPTS_RUN=0 #1 - true/enable | 0 - false/disable

View File

@ -4,8 +4,11 @@
[metadata]
groups = ["default"]
strategy = ["cross_platform", "inherit_metadata"]
lock_version = "4.4.1"
content_hash = "sha256:3e08a1de12505708e4539230fe7dc372f7fe295cf966f3097b3e2a12fac77e49"
lock_version = "4.5.0"
content_hash = "sha256:32227dab263e3d9c111c4b44bca30eb70535da9978c01c293064ab9bac4cf07d"
[[metadata.targets]]
requires_python = ">=3.12"
[[package]]
name = "annotated-types"
@ -13,6 +16,9 @@ version = "0.7.0"
requires_python = ">=3.8"
summary = "Reusable constraint types to use with typing.Annotated"
groups = ["default"]
dependencies = [
"typing-extensions>=4.0.0; python_version < \"3.9\"",
]
files = [
{file = "annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53"},
{file = "annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89"},
@ -25,14 +31,31 @@ requires_python = ">=3.9"
summary = "High level compatibility layer for multiple asynchronous event loop implementations"
groups = ["default"]
dependencies = [
"exceptiongroup>=1.0.2; python_version < \"3.11\"",
"idna>=2.8",
"sniffio>=1.1",
"typing-extensions>=4.1; python_version < \"3.11\"",
]
files = [
{file = "anyio-4.6.2.post1-py3-none-any.whl", hash = "sha256:6d170c36fba3bdd840c73d3868c1e777e33676a69c3a72cf0a0d5d6d8009b61d"},
{file = "anyio-4.6.2.post1.tar.gz", hash = "sha256:4c8bc31ccdb51c7f7bd251f51c609e038d63e34219b44aa86e47576389880b4c"},
]
[[package]]
name = "apscheduler"
version = "3.11.0"
requires_python = ">=3.8"
summary = "In-process task scheduler with Cron-like capabilities"
groups = ["default"]
dependencies = [
"backports-zoneinfo; python_version < \"3.9\"",
"tzlocal>=3.0",
]
files = [
{file = "APScheduler-3.11.0-py3-none-any.whl", hash = "sha256:fc134ca32e50f5eadcc4938e3a4545ab19131435e851abb40b34d63d5141c6da"},
{file = "apscheduler-3.11.0.tar.gz", hash = "sha256:4c622d250b0955a65d5d0eb91c33e6d43fd879834bf541e0a18661ae60460133"},
]
[[package]]
name = "click"
version = "8.1.7"
@ -41,6 +64,7 @@ summary = "Composable command line interface toolkit"
groups = ["default"]
dependencies = [
"colorama; platform_system == \"Windows\"",
"importlib-metadata; python_version < \"3.8\"",
]
files = [
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
@ -81,6 +105,9 @@ version = "0.14.0"
requires_python = ">=3.7"
summary = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
groups = ["default"]
dependencies = [
"typing-extensions; python_version < \"3.8\"",
]
files = [
{file = "h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761"},
{file = "h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d"},
@ -251,6 +278,7 @@ summary = "The little ASGI library that shines."
groups = ["default"]
dependencies = [
"anyio<5,>=3.4.0",
"typing-extensions>=3.10.0; python_version < \"3.10\"",
]
files = [
{file = "starlette-0.41.3-py3-none-any.whl", hash = "sha256:44cedb2b7c77a9de33a8b74b2b90e9f50d11fcf25d8270ea525ad71a25374ff7"},
@ -268,6 +296,33 @@ files = [
{file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"},
]
[[package]]
name = "tzdata"
version = "2024.2"
requires_python = ">=2"
summary = "Provider of IANA time zone data"
groups = ["default"]
marker = "platform_system == \"Windows\""
files = [
{file = "tzdata-2024.2-py2.py3-none-any.whl", hash = "sha256:a48093786cdcde33cad18c2555e8532f34422074448fbc874186f0abd79565cd"},
{file = "tzdata-2024.2.tar.gz", hash = "sha256:7d85cc416e9382e69095b7bdf4afd9e3880418a2413feec7069d533d6b4e31cc"},
]
[[package]]
name = "tzlocal"
version = "5.2"
requires_python = ">=3.8"
summary = "tzinfo object for the local timezone"
groups = ["default"]
dependencies = [
"backports-zoneinfo; python_version < \"3.9\"",
"tzdata; platform_system == \"Windows\"",
]
files = [
{file = "tzlocal-5.2-py3-none-any.whl", hash = "sha256:49816ef2fe65ea8ac19d19aa7a1ae0551c834303d5014c6d5a62e4cbda8047b8"},
{file = "tzlocal-5.2.tar.gz", hash = "sha256:8d399205578f1a9342816409cc1e46a93ebd5755e39ea2d85334bea911bf0e6e"},
]
[[package]]
name = "uvicorn"
version = "0.32.1"
@ -277,6 +332,7 @@ groups = ["default"]
dependencies = [
"click>=7.0",
"h11>=0.8",
"typing-extensions>=4.0; python_version < \"3.11\"",
]
files = [
{file = "uvicorn-0.32.1-py3-none-any.whl", hash = "sha256:82ad92fd58da0d12af7482ecdb5f2470a04c9c9a53ced65b9bbb4a205377602e"},

View File

@ -12,6 +12,7 @@ dependencies = [
"SM4relind>=1.0.3",
"python-dotenv>=1.0.1",
"SMmegaind>=1.0.3",
"apscheduler>=3.11.0",
]
requires-python = ">=3.12"
readme = "README.md"

7
scripts/example.py Normal file
View File

@ -0,0 +1,7 @@
from src.variables.service import get_variables
#modules example import: from src.modules.RTD8.service import read_temp
def test_function():
print("Example script")
print(f"variables: {" | ".join(get_variables())}")

1
scripts/readme.md Normal file
View File

@ -0,0 +1 @@
Here you can save your own scripts and run them from timer.py by using APScheduler for autostarting scripts in setted interval

17
scripts/timer.py Normal file
View File

@ -0,0 +1,17 @@
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.triggers.cron import CronTrigger
import os
scheduler = BackgroundScheduler()
if bool(os.getenv("BACKGROUND_SCRIPTS_RUN", 0)):
print("run")
scheduler.start()
#example
from scripts.example import test_function
scheduler.add_job(test_function, trigger=IntervalTrigger(seconds=5))
#end example

View File

@ -7,6 +7,8 @@ from fastapi import FastAPI
from src.modules.router import router as modulesrouter
from src.variables.router import router as variablesrouter
import scripts.timer
app = FastAPI(root_path="/api")

View File

@ -10,7 +10,7 @@ for i in stacks:
def read_temp(stack: int, channel: int) -> float | str:
def read_temp(stack: int, channel: int) -> float:
"""
Read specified channel in Celsius
"""
@ -22,13 +22,13 @@ def read_temp(stack: int, channel: int) -> float | str:
return -512
def read_temp_stack(stack: int) -> dict:
def read_temp_stack(stack: int) -> dict[int, float]:
"""
Read all inputs data in Celsius
"""
return {ch: read_temp(stack=stack, channel=ch) for ch in range(1, 9)}
def read_temp_all() -> dict:
def read_temp_all() -> dict[int, dict[int, float]]:
"""
Read all RTD8 cards inputs in Celsius
"""
@ -36,7 +36,7 @@ def read_temp_all() -> dict:
def read_resistance(stack: int, channel: int) -> float | str:
def read_resistance(stack: int, channel: int) -> float:
"""
Read specified channel in Ohm
"""
@ -48,13 +48,13 @@ def read_resistance(stack: int, channel: int) -> float | str:
return -512
def read_resistance_stack(stack: int) -> dict:
def read_resistance_stack(stack: int) -> dict[int, float]:
"""
Read all inputs data in Ohm
"""
return {ch: read_resistance(stack=stack, channel=ch) for ch in range(1, 9)}
def read_resistance_all() -> dict:
def read_resistance_all() -> dict[int, dict[int, float]]:
"""
Read all RTD8 cards inputs in Ohm
"""

View File

@ -6,10 +6,10 @@ variables: dict = {}
def __save_data(variable, data):
with open(variable, "w") as file:
file.write(data)
file.write(str(data))
def __load_data(variable, type):
def __load_data(variable):
with open(variable, "r") as file:
return file.read()
@ -50,6 +50,10 @@ def get_variable(variable: str) -> str | None:
return variables[variable]
else:
return None
def get_variables() -> list[str]:
return variables.keys()
def remove_variable(variable: str):