Skip to content

Commit

Permalink
Improve exmaple scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Bromig <lukas.bromig@tum.de>
  • Loading branch information
Lbromig committed Mar 26, 2021
1 parent 355f1e6 commit 1edf3fb
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 30 deletions.
91 changes: 91 additions & 0 deletions examples/Tutorial_1_Hello_SiLA_2_Manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
TUTORIAL 1: Hello_SiLA_2_Manager
---------------------------------------------
1.1 You can use this code editor like a regular scripting environment.
If you require specific python packages for your script, you can import them here.
Hint 1: Packages you want to import must be specified in the dockerfiles requirements.txt!
The file is located in your SiLA 2 Manager Installation directory. The default location
on Linux is /home/<your_username>/sila2_device_manager/user_script_env/requirements.txt.
If you change the requirements, you need to rerun the create_container.sh to update the
docker container image.
"""
import sys
import time
import logging
import numpy as np

"""
1.2 You can use the python logging package and configure the output format here. Logging statements
are transferred via the stderr of the docker container and are flushed by default by the logging function.
All output is forwarded to the SiLA 2 Manager frontend. You can display the logs in the "experiments"
tab by clicking on an experiment.
When an script crashes straight-away, the logs may fail to arrive at the frontend so you have to open
the files directly.
The log files are stored locally on your computer:
Linux: /tmp/device_manager/container
Windows: C:\\Users\\<your_username>\\AppData\\Local\\Temp\\device-manager\\container
"""

logging.basicConfig(format='%(levelname)-8s| %(module)s.%(funcName)s: %(message)s', level=logging.DEBUG)
logger = logging.getLogger(name=__name__)

"""
1.3 When the experiment is started, the function run() is called. Therefore, every script must
contain a run() function. The run function requires one argument: services. This argument
is used to pass the SiLA Server clients information into the script. You have to supply this
argument, even if you don't use it!
Hint 2: Use the flush argument when using print statements or add a newline character (\n) to the
end of your string. Logging statements are flushed automatically.
"""


def run(services):
""" Required to import and instantiate devices """

print('Hello SiLA2 Manager')
# The above print statement will not be shown in the experiment terminal before the statement below is executed and flushed.
time.sleep(5)
print('Yay me, i got flushed!', flush=True)
print(f'A random number: {np.random.rand()}', flush=True)

write_logging_statement()
write_to_output()
"""
1.4 You can call other functions from within thr run() function. The function "write_logging_statement"
writes logging statements of all available log_levels.
"""


def write_logging_statement():
"""Writes logging statements"""
time.sleep(1)
logger.debug('A debug statement')
time.sleep(1)
logger.info('An info statement')
time.sleep(1)
logger.warning('A warning statement')
time.sleep(1)
logger.critical('A critical warning statement')
time.sleep(1)
logger.error('An error statement\n')
time.sleep(3)

"""
1.5 If direct calls to stdout and stderr are made, they won't get flushed either. Output has to be flushed explicitly.
Hint 2: Use the flush argument when using print statements and the sys.stderr.flush and sys.stdout.flush function for
write operations with sys.
"""


def write_to_output():
"""Writes message to stderr"""
sys.stderr.write('Error\n')
sys.stderr.flush()
time.sleep(1)
sys.stdout.write('All Good\n')
sys.stdout.flush()
55 changes: 55 additions & 0 deletions examples/Tutorial_2_Incorporating_SiLA_Clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
TUTORIAL 2: Incorporating SiLA 2 Clients
---------------------------------------------
This example uses the SiLA Python HelloSiLA_Full example server.
You can download it from the repositroy at https://gitlab.com/SiLA2/sila_python/-/tree/master/examples/HelloSiLA2/HelloSiLA2_Full
To run this example follow these steps:
2.1. Add a SiLA Server to your Services (Ideally the HelloSiLA example from the SiLA Python or Tecan repository)
2.2. Go to the Data Handler tab and deactivate the "Activce" checkmark for the device you want to use
2.3. Set up an experiment with and select this script and the device you want to use
2.4. Hit the run button or wait for the scheduled execution time (You can click on the experiment
name to get the docker container stdout, i.e the output of your script)
Hint 1: The def run() method is compulsory. The services are passed to it in the same order,
that you select them in during experiment setup, i.e the order they are displayed in in
the experiment list entry of you experiment
Hint 2: Use the flush argument when using print statements.
Hint 3: The command/property call syntax is displayed in the "Services" tab. It is shown under
"Usage" on the lowest level of the device tree for every command and property.
"""
import time


def run(services):
""" Instantiates selected devices for this experiment """
client = services[0]
print(f'Service instantiated: {client.name}@{client.ip}:{client.port}', flush=True)
client.connect()
# A GET command. A call to the SiLAService feature. Request the server name.
response = client.call_property("SiLAService\n", "ServerName")
print(response, flush=True)
# A SET command. A call to the DriveController of the pump. Set speed and start the first channel.
# client.call_command(feature_id="PumpController\n",
# command_id="SetFlowrate",
# parameters = { "channel/constrained/integer": 1,"flowrate/constrained/real": 20.5}
# )
# pump.call_command(feature_id="PumpController\n",
# command_id="StartPump",
# parameters = { "channel/constrained/integer": 1}
# )

#reactor_client.call_command(feature_id="PumpController\n",
# command_id="SetDirectionClockwise",
# parameters = { "channel/constrained/integer": 1}
# )


for i in range(50):
response = client.call_property("SiLAService\n", "ServerName")
print(response, flush=True)
time.sleep(1)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
# If you require specific python packages for your script, you can import them here.
# Note: Packages you want to import must be specified in the dockerfiles requirements.txt!

print("Hello World!")
import time


def run(devices):
""" Required to import and instantiate devices """
return
File renamed without changes.
29 changes: 0 additions & 29 deletions examples/service_example.py

This file was deleted.

0 comments on commit 1edf3fb

Please sign in to comment.