Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Give kuksa-client ability to encode array values to set and get them
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmittag committed May 4, 2023
1 parent 3fe7ccc commit c1f87ce
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions kuksa-client/kuksa_client/cli_backend/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,24 @@
from kuksa_client import cli_backend
import kuksa_client.grpc
import kuksa_client.grpc.aio
from kuksa_client.grpc import EntryUpdate

from kuksa_client.grpc import EntryUpdate, DataType
from kuksa.val.v1 import types_pb2

def callback_wrapper(callback: Callable[[str], None]) -> Callable[[Iterable[EntryUpdate]], None]:
def wrapper(updates: Iterable[EntryUpdate]) -> None:
callback(json.dumps([update.to_dict() for update in updates]))
return wrapper

class DatabrokerEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (types_pb2.StringArray, types_pb2.BoolArray, types_pb2.Uint32Array, types_pb2.Uint64Array, types_pb2.FloatArray, types_pb2.Int32Array, types_pb2.Int64Array, types_pb2.DoubleArray)):
string_values = []
for value in obj.values:
value = str(value)
string_values.append(value)
return {'values': string_values}
return super().default(obj)


class Backend(cli_backend.Backend):
def __init__(self, config):
Expand Down Expand Up @@ -109,9 +119,21 @@ def setValues(self, updates: Dict[str, Any], attribute="value", timeout=5):
entry_updates = []
for path, value in updates.items():
if field is kuksa_client.grpc.Field.VALUE:
values = value.strip('[]').split(',')
array = []
if len(values) > 1 :
for val in values:
array.append(val)
value = array
entry = kuksa_client.grpc.DataEntry(
path=path, value=kuksa_client.grpc.Datapoint(value=value))
elif field is kuksa_client.grpc.Field.ACTUATOR_TARGET:
values = value.strip('[]').split(',')
array = []
if len(values) > 1 :
for val in values:
array.append(val)
value = array
entry = kuksa_client.grpc.DataEntry(
path=path, actuator_target=kuksa_client.grpc.Datapoint(
value=value),
Expand Down Expand Up @@ -191,7 +213,7 @@ def _sendReceiveMsg(self, req, timeout):
if error:
respJson = json.dumps(error, indent=4)
elif resp:
respJson = json.dumps(resp, indent=4)
respJson = json.dumps(resp, indent=4, cls=DatabrokerEncoder)
else:
respJson = "OK"
except queue.Empty:
Expand Down

0 comments on commit c1f87ce

Please sign in to comment.