diff --git a/back/pom.xml b/back/pom.xml index 204d311..cc44aa8 100644 --- a/back/pom.xml +++ b/back/pom.xml @@ -2,7 +2,7 @@ 4.0.0 org.kar karanage - 0.2.0 + 0.2.1 3.1 @@ -22,7 +22,7 @@ kangaroo-and-rabbit archidata - 0.2.0 + 0.2.1 diff --git a/client/.connection.json b/client/.connection.json new file mode 100644 index 0000000..eac6fc1 --- /dev/null +++ b/client/.connection.json @@ -0,0 +1,4 @@ +{ + "url": "http://localhost:20080/karanage/api", + "group": "home" +} diff --git a/client/.karanage.json b/client/.karanage.json deleted file mode 100644 index fc730cd..0000000 --- a/client/.karanage.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "url": "http://localhost:20080/karanage/api/state", - "group": "home" -} diff --git a/client/python/karanage-tools/bin/karanage-system b/client/python/karanage-tools/bin/karanage-system index 39c5dc3..8777de1 100755 --- a/client/python/karanage-tools/bin/karanage-system +++ b/client/python/karanage-tools/bin/karanage-system @@ -125,7 +125,7 @@ if __name__ == '__main__': parser.add_argument("-C", "--connection", type=str, default="/etc/karanage/connection.json", help="json configuration file") # This element are read from the connection file: - parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api/state", help="Base URL of the web service") + parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api", help="Base URL of the web service") parser.add_argument("-g", "--group", type=str, default="home", help="Group the the message") parser.add_argument("-T", "--token", type=str, default="", help="Token to access to the server") @@ -205,7 +205,7 @@ if __name__ == '__main__': print(json.dumps(out, indent=4)) # send message to the server: try: - restInterface.send_to_server(configuration["config"]["topic"], out) - except karanage.KarangeSendError as ex: + restInterface.send_state_to_server(configuration["config"]["topic"], out) + except karanage.KarangeException as ex: print(f"Can not send to the server: {ex}") time.sleep(configuration["config"]["sleep"]) \ No newline at end of file diff --git a/client/python/karanage-tools/bin/karanage-tools-get b/client/python/karanage-tools/bin/karanage-tools-state-get similarity index 83% rename from client/python/karanage-tools/bin/karanage-tools-get rename to client/python/karanage-tools/bin/karanage-tools-state-get index 97054c7..37dc424 100755 --- a/client/python/karanage-tools/bin/karanage-tools-get +++ b/client/python/karanage-tools/bin/karanage-tools-state-get @@ -14,15 +14,15 @@ if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-C", "--connection", type=str, default="/etc/karanage/connection.json", help="json configuration file") parser.add_argument("-t", "--topic", type=str, default="", help="Topic of the message") + parser.add_argument("-s", "--since", type=str, default=None, help="Iso date since the value time must be") # This element are read from the connection file: - parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api/state", help="Base URL of the web service") + parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api", help="Base URL of the web service") parser.add_argument("-g", "--group", type=str, default="home", help="Group the the message") parser.add_argument("-T", "--token", type=str, default="", help="Token to access to the server") args = parser.parse_args() - if Path(args.connection).exists(): f = open(args.connection, "r") connection = json.loads(f.read()) @@ -44,8 +44,8 @@ if __name__ == '__main__': connection["token"]) if args.topic == "": - data = restInterface.get_all() + data = restInterface.get_state_all(since=args.since) print(f"Ret = {json.dumps(data, indent=4)}") else: - data = restInterface.get_topic(args.topic) + data = restInterface.get_state_topic(args.topic, since=args.since) print(f"Ret = {json.dumps(data, indent=4)}") \ No newline at end of file diff --git a/client/python/karanage-tools/bin/karanage-tools-state-history-get b/client/python/karanage-tools/bin/karanage-tools-state-history-get new file mode 100755 index 0000000..c7411ab --- /dev/null +++ b/client/python/karanage-tools/bin/karanage-tools-state-history-get @@ -0,0 +1,52 @@ +#!/bin/python3 +# Importing the library +import psutil +import argparse +import time +import subprocess +import json +from pathlib import Path +from typing import Dict, List +import karanage + +if __name__ == '__main__': + # Load arguments: + parser = argparse.ArgumentParser() + parser.add_argument("-C", "--connection", type=str, default="/etc/karanage/connection.json", help="json configuration file") + parser.add_argument("-t", "--topic", type=str, default="", help="Topic of the message") + parser.add_argument("-s", "--since", type=str, default=None, help="Iso date since the value time must be") + parser.add_argument("-S", "--since-id", type=str, default=None, help="Remote BDD id to start request") + parser.add_argument("-l", "--limit", type=int, default=100, help="Limit the number of request") + + # This element are read from the connection file: + parser.add_argument("-u", "--url", type=str, default="http://localhost:20080/karanage/api", help="Base URL of the web service") + parser.add_argument("-g", "--group", type=str, default="home", help="Group the the message") + parser.add_argument("-T", "--token", type=str, default="", help="Token to access to the server") + + args = parser.parse_args() + + if Path(args.connection).exists(): + f = open(args.connection, "r") + connection = json.loads(f.read()) + f.close() + else: + connection = {} + # manage the connection model + if "url" not in connection: + connection["url"] = args.url + if "group" not in connection: + connection["group"] = args.group + if "token" not in connection: + connection["token"] = args.token + + # create the rest interface of karanage + restInterface = karanage.KaranageREST( + connection["url"], + connection["group"], + connection["token"]) + + if args.topic == "": + print("Missing TOPIC ...") + else: + data = restInterface.get_state_history_topic(args.topic, since=args.since, since_id=args.since_id, limit=args.limit) + print(f"Ret = {json.dumps(data, indent=4)}") \ No newline at end of file diff --git a/client/python/karanage-tools/setup.py b/client/python/karanage-tools/setup.py index d1460df..e45b7f1 100755 --- a/client/python/karanage-tools/setup.py +++ b/client/python/karanage-tools/setup.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- ## ## @author Edouard DUPIN @@ -49,7 +49,8 @@ setup(name='karanage-tools', keywords='system cpu status', scripts=[ 'bin/karanage-system', - 'bin/karanage-tools-get', + 'bin/karanage-tools-state-get', + 'bin/karanage-tools-state-history-get', ], include_package_data = True, zip_safe=False) diff --git a/client/python/karanage-tools/version.txt b/client/python/karanage-tools/version.txt index 49ffebc..b4f09dd 100644 --- a/client/python/karanage-tools/version.txt +++ b/client/python/karanage-tools/version.txt @@ -1 +1 @@ -0.1.0-dev \ No newline at end of file +0.2.0-dev \ No newline at end of file diff --git a/client/python/karanage/karanage/__init__.py b/client/python/karanage/karanage/__init__.py index 6f1bb61..35cc386 100755 --- a/client/python/karanage/karanage/__init__.py +++ b/client/python/karanage/karanage/__init__.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- ## ## @author Edouard DUPIN @@ -7,4 +7,4 @@ ## ## @license MPL v2.0 (see license file) ## -from .interface import StateSystem, KaranageREST, KarangeSendError +from .interface import StateSystem, KaranageREST, KarangeSendError, KarangeException diff --git a/client/python/karanage/karanage/interface.py b/client/python/karanage/karanage/interface.py index c8d9cc0..928a42b 100644 --- a/client/python/karanage/karanage/interface.py +++ b/client/python/karanage/karanage/interface.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- ## ## @author Edouard DUPIN @@ -24,6 +24,13 @@ class KarangeSendError(Exception): def __str__(self): return f"{Exception.__str__(self)} Status={self.error_id} message='{self.error_message}'" +class KarangeException(KarangeSendError): + def __init__(self, message, error_id, error_message): + # Call the base class constructor with the parameters it needs + super().__init__(message, error_id, error_message ) + + + class StateSystem(enum.Enum): OK = "OK" FAIL = "FAIL" @@ -43,10 +50,13 @@ class KaranageREST: self.url = url self.group = group self.token = token - self.root_url = f"{self.url}/{self.group}" + def get_url(self, service: str, topic: Optional[str] = None): + if topic is None: + return f"{self.url}/{service}/{self.group}" + return f"{self.url}/{service}/{self.group}/{topic}" - def send_to_server(self, topic: str, data: Optional[Dict], state: StateSystem = StateSystem.OK) -> None: + def send_state_to_server(self, topic: str, data: Optional[Dict], state: StateSystem = StateSystem.OK) -> None: """ @brief Send a message to the server. @param[in] topic Topic where to publish the data. @@ -62,39 +72,80 @@ class KaranageREST: if self.token is not None and len(self.token) >15: header['Authorization'] = f"zota {self.token}" try: - ret = requests.post(f"{self.root_url}/{topic}", json=data, headers=header, params=param) + ret = requests.post(self.get_url("state", topic), json=data, headers=header, params=param) except requests.exceptions.ConnectionError as ex: - raise KarangeSendError(f"Fail connect server: {self.root_url}/{topic}", 0, str(ex)) + raise KarangeException(f"Fail connect server: {self.get_url('state', topic)}", 0, str(ex)) if 200 <= ret.status_code <= 299: pass else: - raise KarangeSendError(f"Fail send message: {self.root_url}/{topic}", ret.status_code, ret.content.decode("utf-8")) + raise KarangeException(f"Fail send message: {self.get_url('state', topic)}", ret.status_code, ret.content.decode("utf-8")) - def get_all(self) -> Dict: + def get_state_all(self, since: Optional[str] = None) -> Dict: """ @brief Get all the topic fom the server. + @param since ISO1866 time value. @return A dictionnary with the requested data. """ param = { } header = { } if self.token is not None and len(self.token) >15: header['Authorization'] = f"zota {self.token}" - ret = requests.get(self.root_url, headers=header, params=param) + if since is not None: + param["since"] = since + ret = requests.get(self.get_url("state"), headers=header, params=param) if 200 == ret.status_code: return json.loads(ret.content.decode('utf-8')) - raise KarangeSendError(f"Fail get data: {self.root_url}", ret.status_code, ret.content.decode("utf-8")) + raise KarangeException(f"Fail get data: {self.get_url('state')}", ret.status_code, ret.content.decode("utf-8")) - def get_topic(self, topic: str) -> Dict: + def get_state_topic(self, topic: str, since: Optional[str] = None) -> Dict: """ @brief Get all the topic fom the server. + @param since ISO1866 time value. @return A dictionnary with the requested data. """ param = { } header = { } if self.token is not None and len(self.token) >15: header['Authorization'] = f"zota {self.token}" - ret = requests.get(f"{self.root_url}/{topic}", headers=header, params=param) + if since is not None: + param["since"] = since + ret = requests.get(self.get_url("state", topic), headers=header, params=param) #print(ret.content.decode('utf-8')) if 200 == ret.status_code: return json.loads(ret.content.decode('utf-8')) - raise KarangeSendError(f"Fail get data: {self.root_url}/{topic}", ret.status_code, ret.content.decode("utf-8")) + raise KarangeException(f"Fail get data: {self.get_url('state', topic)}", ret.status_code, ret.content.decode("utf-8")) + + def get_state_history_topic(self, topic: str, since: Optional[str] = None, since_id: Optional[int] = None, limit: Optional[int] = None) -> Dict: + """ + @brief Get all the topic fom the server. + @param since ISO1866 time value. + @param since_id remote BDD index of tje fielf. + @param limit Number of value we want to get + @return A dictionnary with the requested data. + """ + param = { } + header = { } + if self.token is not None and len(self.token) >15: + header['Authorization'] = f"zota {self.token}" + if since is not None: + param["since"] = since + if since_id is not None: + param["sinceId"] = since_id + if limit is not None: + param["limit"] = limit + ret = requests.get(self.get_url("state_history", topic), headers=header, params=param) + #print(ret.content.decode('utf-8')) + if 200 == ret.status_code: + return json.loads(ret.content.decode('utf-8')) + raise KarangeException(f"Fail get data: {self.get_url('state_history', topic)}", ret.status_code, ret.content.decode("utf-8")) + + + def get_all(self) -> Dict: + """Deprecated""" + return self.get_state_all() + def get_topic(self, topic: str) -> Dict: + """Deprecated""" + return self.get_state_topic(topic) + def send_to_server(self, topic: str, data: Optional[Dict], state: StateSystem = StateSystem.OK) -> None: + """Deprecated""" + return self.send_state_to_server(topic, data, state) \ No newline at end of file diff --git a/client/python/karanage/setup.py b/client/python/karanage/setup.py index a8b9435..9b31ae6 100755 --- a/client/python/karanage/setup.py +++ b/client/python/karanage/setup.py @@ -54,3 +54,6 @@ setup(name='karanage', # sudo ./setup.py develop --user #TO register all in pip: ./setup.py register sdist upload +# https://packaging.python.org/en/latest/tutorials/packaging-projects/ +# python3 -m build +# python3 -m twine upload dist/* \ No newline at end of file diff --git a/client/python/karanage/version.txt b/client/python/karanage/version.txt index 49ffebc..b4f09dd 100644 --- a/client/python/karanage/version.txt +++ b/client/python/karanage/version.txt @@ -1 +1 @@ -0.1.0-dev \ No newline at end of file +0.2.0-dev \ No newline at end of file