Skip to content

Commit

Permalink
Merge pull request #37 from ZEISS/20240815-feat-service
Browse files Browse the repository at this point in the history
Enhancement of ServiceExample code and documentation
  • Loading branch information
mprinkezs authored Aug 16, 2024
2 parents 2657fdd + 23f19bd commit a9a90a7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 16 deletions.
28 changes: 21 additions & 7 deletions AppExamples/misc/ServiceExample/doc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ print(result)

```python
import sys
import time
import gom.api.services

SERVICE_NAME = "Reflector"
TIMEOUT = 30

# Wait for expected status or timeout
def wait_for_status(service, status, timeout):
for _ in range(timeout):
if service.get_status() == status:
break
time.sleep(1)

return service.get_status()

# Find service handle by name
srv = None
Expand All @@ -80,15 +91,18 @@ print(f"Endpoint: {srv.get_endpoint()}")
print(f"Number of instances: {srv.get_number_of_instances()}")

# Control service
print(srv.get_status())
print("(Re-)Starting...", end="")
srv.start()
print(srv.get_status())
status = srv.get_status()
print(f"Status: {status}")
if status == "STOPPED":
print("(Re-)Starting...", end="")
srv.start()
status = wait_for_status(srv, "RUNNING", TIMEOUT)
print(status)

print("Stopping...", end="")
srv.stop()
print(srv.get_status())
print("Starting...", end="")
srv.start()
status = wait_for_status(srv, "STOPPED", TIMEOUT)
print(status)
```

## Related
Expand Down
92 changes: 83 additions & 9 deletions AppExamples/misc/ServiceExample/scripts/usage_example.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,90 @@
# -*- coding: utf-8 -*-
#
# service_usage.py
#
# Carl Zeiss GOM Metrology GmbH, 2024
#
# Service API usage demonstration
#
# This App is part of the ZEISS INSPECT Python API Examples:
# https://zeissiqs.github.io/zeiss-inspect-addon-api/2025/python_examples/
# ---

import sys
import time
import gom
import gom.api.math
import gom.api.test
import gom.api.services

# Call reflect function from service #2
result = gom.api.test.reflect({'test': 123})
print(result)

# Call multiply function from service #1
result = gom.api.math.multiply (23, 42)
print(result)

SERVICE_NAME = "Reflector"
TIMEOUT = 30

def wait_for_status(service, status, timeout):
"""
Wait for expected service status or timeout
Args:
service: Service object
status: Expected status
timeout: Timeout in seconds
Returns:
Final status.
"""
for _ in range(timeout):
if service.get_status() == status:
break
time.sleep(1)

return service.get_status()


if __name__ == '__main__':
print('Available services:')
s_reflector = None
for s in gom.api.services.get_services():
print (s.get_name())
if s.get_name() == SERVICE_NAME:
srv = s
print("\n")

if not srv:
print(f"Failed to get '{SERVICE_NAME}' service handle")
sys.exit(0)

print(f"'{srv.get_name()}' service properties")
print(f"Autostart: {srv.get_autostart()}")
print(f"Endpoint: {srv.get_endpoint()}")
print(f"Number of instances: {srv.get_number_of_instances()}")
print("\n")

print(f"Attempting to change the '{srv.get_name()}' service status.")
status = srv.get_status()
print(f"Status: {status}")

if status == "STOPPED":
print("(Re-)Starting...", end="")
srv.start()
status = wait_for_status(srv, "RUNNING", TIMEOUT)
print(status)

print("Stopping...", end="")
srv.stop()
status = wait_for_status(srv, "STOPPED", TIMEOUT)
print(status)

print("Starting...", end="")
srv.start()
status = wait_for_status(srv, "RUNNING", TIMEOUT)
print(status)

import gom.api.math
import gom.api.test

# Call reflect function from service #2
result = gom.api.test.reflect({'test': 123})
print("gom.api.test.reflect({'test': 123}): " + f"{result}")

# Call multiply function from service #1
result = gom.api.math.multiply (23, 42)
print(f"gom.api.math.multiply (23, 42): {result}")

0 comments on commit a9a90a7

Please sign in to comment.