Skip to content

Commit

Permalink
controller.py update_state and bus_publish methods
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Apr 25, 2024
1 parent 5a0b346 commit 2f4ca89
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion svc/controller-py/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.1.3
VERSION=0.1.4

all:
@echo "what do you want to build today?"
Expand Down
2 changes: 1 addition & 1 deletion svc/controller-py/eva4_controller_py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.1.3'
__version__ = '0.1.4'

from evaics.sdk import Service, Controller, Action, no_rpc_method, ServiceInfo
from evaics.sdk import OID
Expand Down
46 changes: 42 additions & 4 deletions svc/controller-py/eva4_controller_py/macro_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from evaics.exceptions import InvalidParameter
from evaics.exceptions import MethodNotImplemented

from evaics.sdk import LocalProxy, rpc_e2e, pack, unpack
from evaics.sdk import LocalProxy, rpc_e2e, pack, unpack, OID
from evaics.sdk import RAW_STATE_TOPIC

from evaics.tools import dict_from_str

Expand Down Expand Up @@ -97,6 +98,43 @@ def rpc_call(method=None, _target='eva.core', _timeout=None, **kwargs):
return True


def bus_publish(topic, payload, qos=0):
"""
Publishes a message to a bus topic
Args:
topic: topic name
payload: message payload
qos: QoS level (default: 0)
"""
try:
service.bus.send(
topic,
busrt.client.Frame(pack(payload),
tp=busrt.client.OP_PUBLISH,
qos=qos)).wait_completed()
except busrt.rpc.RpcException as e:
raise rpc_e2e(e)


def update_state(oid, state):
"""
Updates item state
Args:
oid: item OID
state: new state (may contain status/value fields)
Optional:
force: force update even if the state is not changed
"""
if 'status' not in state:
state['status'] = 1
oid = OID(oid) if isinstance(oid, str) else oid
topic = f'{RAW_STATE_TOPIC}{oid.to_path()}'
bus_publish(topic, state)


def report_accounting_event(u=None,
src=None,
svc=None,
Expand Down Expand Up @@ -771,9 +809,7 @@ def ls(mask, recursive=False):
dict with fields 'name' 'file', 'size' and 'time' { 'c': created,
'm': modified }
"""
fls = [
x for x in glob.glob(mask, recursive=recursive) if os.path.isfile(x)
]
fls = [x for x in glob.glob(mask, recursive=recursive) if os.path.isfile(x)]
l = []
for x in fls:
l.append({
Expand Down Expand Up @@ -872,6 +908,7 @@ def log_trace(msg, *args, **kwargs):
'is_busy': is_busy,
'set': _set,
'state': state,
'update_state': update_state,
'sha256sum': sha256sum,
'status': status,
'value': value,
Expand All @@ -898,5 +935,6 @@ def log_trace(msg, *args, **kwargs):
'date': date,
'ls': ls,
'rpc_call': rpc_call,
'bus_publish': bus_publish,
'report_accounting_event': report_accounting_event
}
2 changes: 1 addition & 1 deletion svc/controller-py/setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.1.3'
__version__ = '0.1.4'

import setuptools

Expand Down

0 comments on commit 2f4ca89

Please sign in to comment.