|
| 1 | +import json |
| 2 | +import random |
| 3 | +import logging |
| 4 | + |
| 5 | +class SatelliteNode: |
| 6 | + """ |
| 7 | + Class representing a satellite node in the space-based node network. |
| 8 | +
|
| 9 | + Attributes: |
| 10 | + node_id (str): Unique identifier for the satellite node. |
| 11 | + location (str): Geographical location of the satellite node. |
| 12 | + connected_nodes (list): List of connected satellite nodes. |
| 13 | + data_storage (dict): Dictionary to store data processed by the node. |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, node_id, location): |
| 17 | + """ |
| 18 | + Initializes a SatelliteNode instance. |
| 19 | +
|
| 20 | + Args: |
| 21 | + node_id (str): Unique identifier for the satellite node. |
| 22 | + location (str): Geographical location of the satellite node. |
| 23 | + """ |
| 24 | + self.node_id = node_id |
| 25 | + self.location = location |
| 26 | + self.connected_nodes = [] |
| 27 | + self.data_storage = {} |
| 28 | + logging.info(f"SatelliteNode {self.node_id} initialized at {self.location}.") |
| 29 | + |
| 30 | + def connect_to_node(self, other_node): |
| 31 | + """ |
| 32 | + Connects this node to another satellite node. |
| 33 | +
|
| 34 | + Args: |
| 35 | + other_node (SatelliteNode): The satellite node to connect to. |
| 36 | + """ |
| 37 | + if other_node not in self.connected_nodes: |
| 38 | + self.connected_nodes.append(other_node) |
| 39 | + other_node.connected_nodes.append(self) # Ensure bidirectional connection |
| 40 | + logging.info(f"Node {self.node_id} connected to Node {other_node.node_id}.") |
| 41 | + |
| 42 | + def disconnect_from_node(self, other_node): |
| 43 | + """ |
| 44 | + Disconnects this node from another satellite node. |
| 45 | +
|
| 46 | + Args: |
| 47 | + other_node (SatelliteNode): The satellite node to disconnect from. |
| 48 | + """ |
| 49 | + if other_node in self.connected_nodes: |
| 50 | + self.connected_nodes.remove(other_node) |
| 51 | + other_node.connected_nodes.remove(self) # Ensure bidirectional disconnection |
| 52 | + logging.info(f"Node {self.node_id} disconnected from Node {other_node.node_id}.") |
| 53 | + |
| 54 | + def process_data(self, data): |
| 55 | + """ |
| 56 | + Processes incoming data and stores the result. |
| 57 | +
|
| 58 | + Args: |
| 59 | + data (dict): The data to be processed. |
| 60 | + |
| 61 | + Returns: |
| 62 | + dict: The processed data. |
| 63 | + """ |
| 64 | + # Simulate data processing (e.g., transformation, filtering) |
| 65 | + processed_data = {key: value * random.uniform(0.5, 1.5) for key, value in data.items()} |
| 66 | + self.data_storage.update(processed_data) |
| 67 | + logging.info(f"Node {self.node_id} processed data: {processed_data}.") |
| 68 | + return processed_data |
| 69 | + |
| 70 | + def get_connected_nodes(self): |
| 71 | + """ |
| 72 | + Returns a list of connected nodes. |
| 73 | +
|
| 74 | + Returns: |
| 75 | + list: List of connected SatelliteNode instances. |
| 76 | + """ |
| 77 | + return [node.node_id for node in self.connected_nodes] |
| 78 | + |
| 79 | + def to_json(self): |
| 80 | + """ |
| 81 | + Serializes the node information to JSON format. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + str: JSON representation of the node. |
| 85 | + """ |
| 86 | + return json.dumps({ |
| 87 | + 'node_id': self.node_id, |
| 88 | + 'location': self.location, |
| 89 | + 'connected_nodes': self.get_connected_nodes(), |
| 90 | + 'data_storage': self.data_storage |
| 91 | + }) |
| 92 | + |
| 93 | +# Example usage |
| 94 | +if __name__ == "__main__": |
| 95 | + logging.basicConfig(level=logging.INFO) |
| 96 | + node1 = SatelliteNode(node_id="Node1", location="Orbit 1") |
| 97 | + node2 = SatelliteNode(node_id="Node2", location="Orbit 2") |
| 98 | + |
| 99 | + node1.connect_to_node(node2) |
| 100 | + data = {'temperature': 25, 'humidity': 60} |
| 101 | + processed_data = node1.process_data(data) |
| 102 | + print(node1.to_json()) |
0 commit comments