From c2f63e9c43a27e41020ed647c789580d1796716c Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 26 Feb 2023 21:47:17 +0100 Subject: [PATCH] [DEBUG] correct the interface of ios for python clients --- .../kar/karanage/api/StateHistoryResource.java | 8 ++++++++ .../src/org/kar/karanage/api/StateResource.java | 4 ++-- client/python/karanage/karanage/connection.py | 6 +++--- client/python/karanage/karanage/state.py | 15 +++++++++------ client/python/karanage/version.txt | 2 +- .../karanage_tools/karanage_system.py | 2 +- .../karanage_tools/karanage_tools_state_get.py | 17 ++++++++++++----- .../karanage_tools_state_history_get.py | 5 ++++- client/python/karanage_tools/version.txt | 2 +- 9 files changed, 41 insertions(+), 20 deletions(-) diff --git a/back/src/org/kar/karanage/api/StateHistoryResource.java b/back/src/org/kar/karanage/api/StateHistoryResource.java index aaf1069..6111f95 100644 --- a/back/src/org/kar/karanage/api/StateHistoryResource.java +++ b/back/src/org/kar/karanage/api/StateHistoryResource.java @@ -127,6 +127,14 @@ public class StateHistoryResource { out.append(data.modify_date.toInstant().toString()); out.append("\""); } + out.append(", \"state\":"); + if (data.state == null) { + out.append("null"); + } else { + out.append("\""); + out.append(data.state); + out.append("\""); + } out.append(", \"data\":" + data.data + "}"); } out.append("]"); diff --git a/back/src/org/kar/karanage/api/StateResource.java b/back/src/org/kar/karanage/api/StateResource.java index bd8e56b..8ddb881 100644 --- a/back/src/org/kar/karanage/api/StateResource.java +++ b/back/src/org/kar/karanage/api/StateResource.java @@ -62,7 +62,8 @@ public class StateResource { ), true); if (data == null) { - throw new FailException(Response.Status.NOT_FOUND, "Topic has no new data or does not exist: '" + topic + "'"); + //throw new FailException(Response.Status.NOT_FOUND, "Topic has no new data or does not exist: '" + topic + "'"); + return ""; } } else { data = SqlWrapper.getWhere(StateInstant.class, @@ -79,7 +80,6 @@ public class StateResource { if ("raw".equals(mode)) { double seconds = data.modify_date.toInstant().toEpochMilli() * 0.001; out.append(seconds); - return "{ \"time\": " + seconds + ", \"data\":" + data.data + "}"; } else { out.append("\""); out.append(data.modify_date.toInstant().toString()); diff --git a/client/python/karanage/karanage/connection.py b/client/python/karanage/karanage/connection.py index 9290c18..8dab556 100644 --- a/client/python/karanage/karanage/connection.py +++ b/client/python/karanage/karanage/connection.py @@ -134,7 +134,7 @@ class KaranageConnection(KaranageConnectionInterface): if token is not None: self.token = token - def get_url(self, service: str, url_offset: Optional[str]): + def get_url(self, service: str, url_offset: Optional[str] = None): """Get the formatted URL for specific service and specific offset :param service: Name of the service we want to connect on. @@ -163,7 +163,7 @@ class KaranageConnection(KaranageConnectionInterface): def post( self, service: str, - url_offset: Optional[str], + url_offset: Optional[str] = None, data: Any = None, headers: Optional[Dict] = None, params: Optional[Dict] = None, @@ -180,7 +180,7 @@ class KaranageConnection(KaranageConnectionInterface): def get( self, service: str, - url_offset: Optional[str], + url_offset: Optional[str] = None, headers: Optional[Dict] = None, params: Optional[Dict] = None, ) -> KaranageResponse: diff --git a/client/python/karanage/karanage/state.py b/client/python/karanage/karanage/state.py index 77ebab6..3f5e30a 100644 --- a/client/python/karanage/karanage/state.py +++ b/client/python/karanage/karanage/state.py @@ -83,7 +83,7 @@ class KaranageState: if since is not None: if type(since) is str: param["since"] = since - if type(since) is datetime: + elif type(since) is datetime: param["since"] = since.replace(tzinfo=timezone.utc).isoformat() else: raise KaranageException( @@ -93,7 +93,9 @@ class KaranageState: raise KaranageException( f"Fail send message sub-library return None") if ret.status is not None and 200 <= ret.status <= 299: - return convert_to_state_data(ret.data, topic) + if ret.data == "": + return None + return convert_to_state_data(json.loads(ret.data), topic) return None #raise KaranageException(f"Fail get data: '{ret.url}'", ret.status, ret.data) @@ -108,7 +110,7 @@ class KaranageState: if since is not None: if type(since) is str: param["since"] = since - if type(since) is datetime: + elif type(since) is datetime: param["since"] = since.replace(tzinfo=timezone.utc).isoformat() else: raise KaranageException( @@ -121,7 +123,7 @@ class KaranageState: out: List[StateData] = [] if ret.data is not None: for elem in json.loads(ret.data): - out.append(convert_to_state_data(elem, topic)) + out.append(convert_to_state_data(elem)) return out raise KaranageException(f"Fail get data: '{ret.url}'", ret.status, ret.data) @@ -139,16 +141,17 @@ class KaranageState: :return: A dictionary with the requested data. """ param:Dict[str, Any] = { + "mode":"raw" # request raw mode to have the timestant in a float in second } if since is not None: if type(since) is str: param["since"] = since - if type(since) is datetime: + elif type(since) is datetime: param["since"] = since.replace(tzinfo=timezone.utc).isoformat() else: raise KaranageException( - f"Wrong input parameter type Must be a str or datetime: '{type(since)}'") + f"Wrong input parameter type Must be a str or datetime: '{type(since)}' {type(str)}") if since_id is not None: param["sinceId"] = since_id if limit is not None: diff --git a/client/python/karanage/version.txt b/client/python/karanage/version.txt index 4996130..8a13399 100644 --- a/client/python/karanage/version.txt +++ b/client/python/karanage/version.txt @@ -1 +1 @@ -0.7.1-dev \ No newline at end of file +0.7.2-dev \ No newline at end of file diff --git a/client/python/karanage_tools/karanage_tools/karanage_system.py b/client/python/karanage_tools/karanage_tools/karanage_system.py index 36817c0..94f6027 100755 --- a/client/python/karanage_tools/karanage_tools/karanage_system.py +++ b/client/python/karanage_tools/karanage_tools/karanage_system.py @@ -65,7 +65,7 @@ def filter(data: Dict, filter_list: List[str]) -> Dict: def need_process(data: Dict[str, Any]) -> Any: - return configuration["cpu"] == "auto" or "include" in configuration["cpu"] + return data == "auto" or "include" in data def create_cpu_data() -> Dict: diff --git a/client/python/karanage_tools/karanage_tools/karanage_tools_state_get.py b/client/python/karanage_tools/karanage_tools/karanage_tools_state_get.py index 6e170ea..fb10cb9 100755 --- a/client/python/karanage_tools/karanage_tools/karanage_tools_state_get.py +++ b/client/python/karanage_tools/karanage_tools/karanage_tools_state_get.py @@ -48,11 +48,18 @@ def main(): # transform since in a datetime: while True: - data = stateInterface.gets(topic=args.topic, since=args.since) - for elem in data: - print(f"{elem.topic} @ {elem.time.astimezone()} => {elem.state}") - print(json.dumps(elem.data, indent=4)) - args.since = elem.time + if args.topic is not None: + elem = stateInterface.get(topic=args.topic, since=args.since) + if elem is not None: + print(f"{elem.topic} @ {elem.time.astimezone()} => {elem.state}") + print(json.dumps(elem.data, indent=4)) + args.since = elem.time + else: + data = stateInterface.gets(since=args.since) + for elem in data: + print(f"{elem.topic} @ {elem.time.astimezone()} => {elem.state}") + print(json.dumps(elem.data, indent=4)) + args.since = elem.time if not args.watch: break else: diff --git a/client/python/karanage_tools/karanage_tools/karanage_tools_state_history_get.py b/client/python/karanage_tools/karanage_tools/karanage_tools_state_history_get.py index 76ab6d7..983b84c 100755 --- a/client/python/karanage_tools/karanage_tools/karanage_tools_state_history_get.py +++ b/client/python/karanage_tools/karanage_tools/karanage_tools_state_history_get.py @@ -52,7 +52,10 @@ def main(): data = stateInterface.get_history( topic=args.topic, since=args.since, since_id=args.since_id, limit=args.limit ) - print(f"Ret = {json.dumps(data, indent=4)}") + tmp = [] + for elem in data: + print(f"{elem.topic} @ {elem.time.astimezone()} => {elem.state}") + print(json.dumps(elem.data, indent=4)) if __name__ == "__main__": main() \ No newline at end of file diff --git a/client/python/karanage_tools/version.txt b/client/python/karanage_tools/version.txt index 4996130..8a13399 100644 --- a/client/python/karanage_tools/version.txt +++ b/client/python/karanage_tools/version.txt @@ -1 +1 @@ -0.7.1-dev \ No newline at end of file +0.7.2-dev \ No newline at end of file