# Copyright 2020 Espressif Systems (Shanghai) PTE LTD # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import json import re import requests from pathlib import Path try: from rmaker_lib import session, node, serverconfig, configmanager from rmaker_lib.exceptions import NetworkError, InvalidJSONError, SSLError from rmaker_lib.logger import log except ImportError as err: print("Failed to import ESP Rainmaker library. " + str(err)) raise err def get_nodes(vars=None): """ List all nodes associated with the user. :param vars: No Parameters passed, defaults to `None` :type vars: dict | None :raises Exception: If there is an HTTP issue while getting nodes :return: None on Success :rtype: None """ try: s = session.Session() nodes = s.get_nodes() except Exception as get_nodes_err: log.error(get_nodes_err) else: if len(nodes.keys()) == 0: print('User is not associated with any nodes.') return for key in nodes.keys(): print(nodes[key].get_nodeid()) return def get_node_config(vars=None): """ Shows the configuration of the node. :param vars: `nodeid` as key - Node ID for the node, defaults to `None` :type vars: dict | None :raises Exception: If there is an HTTP issue while getting node config :return: None on Success :rtype: None """ try: n = node.Node(vars['nodeid'], session.Session()) node_config = n.get_node_config() except Exception as get_nodes_err: log.error(get_nodes_err) else: print(json.dumps(node_config, indent=4)) return def get_node_status(vars=None): """ Shows the online/offline status of the node. :param vars: `nodeid` as key - Node ID for the node, defaults to `None` :type vars: dict | None :raises Exception: If there is an HTTP issue while getting node status :return: None on Success :rtype: None """ try: n = node.Node(vars['nodeid'], session.Session()) node_status = n.get_node_status() except Exception as get_node_status_err: log.error(get_node_status_err) else: print(json.dumps(node_status, indent=4)) return def set_params(vars=None): """ Set parameters of the node. :param vars: `nodeid` as key - Node ID for the node,\n `data` as key - JSON data containing parameters to be set `or`\n `filepath` as key - Path of the JSON file containing parameters to be set,\n defaults to `None` :type vars: dict | None :raises Exception: If there is an HTTP issue while setting params or JSON format issue in HTTP response :return: None on Success :rtype: None """ log.info('Setting params of the node with nodeid : ' + vars['nodeid']) data = vars['data'] filepath = vars['filepath'] if data is not None: log.debug('Setting node parameters using JSON data.') # Trimming white spaces except the ones between two strings data = re.sub(r"(?