Exemplo n.º 1
0
    async def test_buffer_packet(self):
        """
        Testing relay callback and edge cases
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("data1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()
        shared_storage = {"pointing": ["sat1"], "mode": "receiving"}

        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        self.assertTrue(
            check_internal_data(message, nats, shared_storage, None))

        await buffer_packet(message, nats, shared_storage, None)
        self.assertEqual(buffer.qsize(), 1)
        buffer.get()

        shared_storage = {"pointing": ["sat1"], "mode": "receiving"}

        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat2"
        self.assertFalse(
            check_internal_data(message, nats, shared_storage, None))
Exemplo n.º 2
0
    async def test_take_action(self):
        """
        Testing whether the RL service emits the proper messages upon receiving a new action
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("rl1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # testing whether receiving works
        shared_storage = {
            "phonebook": {
                "sat1": False,
                "sat2": False
            },
            "iot_phonebook": {
                "iot1": True,
                "iot2": False
            },
            "ground_phonebook": {
                "grstn1": False,
                "grstn2": False
            },
            "buffered_packets": 2365,
            "packets_received": {
                "grstn1": 212,
                "grstn2": 573
            }
        }

        # testing correct internal case
        message = nats.create_message({
            "action": "sending",
            "id": "sat1"
        }, MessageSchemas.RL_ACTION_MESSAGE)

        await take_action(message, nats, shared_storage, None)
        self.assertEqual(len(nats._dict["internal.rl-action.mode"]), 1)
        self.assertEqual(nats._dict["internal.rl-action.mode"][0].data,
                         "sending")
        self.assertEqual(len(nats._dict["command.point"]), 1)
        self.assertEqual(nats._dict["command.point"][0].data, "sat1")

        # testing wrong not internal case
        message = nats.create_message({
            "action": "sending",
            "id": "sat1"
        }, MessageSchemas.RL_ACTION_MESSAGE)
        message.origin_id = "sat2"
        await take_action(message, nats, shared_storage, None)
        self.assertEqual(len(nats._dict["internal.rl-action.mode"]), 1)
        self.assertEqual(nats._dict["internal.rl-action.mode"][0].data,
                         "sending")
        self.assertEqual(len(nats._dict["command.point"]), 1)
        self.assertEqual(nats._dict["command.point"][0].data, "sat1")
Exemplo n.º 3
0
    async def test_process_data(self):
        """
        Testing application service callback
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("app1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()
        shared_storage = {}

        # testing correct case
        message = nats.create_message(23.45, MessageSchemas.IOT_DATA_MESSAGE)
        self.assertTrue(
            check_internal_data(message, nats, shared_storage, None))
        await process_data(message, nats, shared_storage, None)
        self.assertEqual(len(nats._dict["internal.data.out"]), 1)

        # testing incorrect case (not internal)
        message = nats.create_message(23.45, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat1"
        self.assertFalse(
            check_internal_data(message, nats, shared_storage, None))
Exemplo n.º 4
0
    async def test_print_log(self):
        """
        Testing printing the log info
        """
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = {"log_path": "logger_test"}

        message = nats.create_message(
            {
                "logged_at": "logging_service",
                "line_number": 43,
                "function": "logger_test",
                "level": "level one",
                "msg": "This is a test!"
            }, MessageSchemas.LOG_MESSAGE)

        await logging_service.print_log(message, nats, shared_storage, None)

        self.assertTrue("logger_test" in os.listdir())
        os.remove("./logger_test")
Exemplo n.º 5
0
    async def test_receive_data(self):
        """
        Testing true case and edge cases for receiving data
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("ground1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # testing whether receiving works
        shared_storage = {"pointing": ["sat1"], "packets_received": 0}
        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat1"
        self.assertTrue(
            check_pointing_data(message, nats, shared_storage, None))
        await receive_data(message, nats, shared_storage, None)
        self.assertEqual(len(nats._dict["groundstation.packets_received"]), 1)
        self.assertEqual(shared_storage["packets_received"], 1)

        # testing whether edge case is actually not working
        shared_storage = {"pointing": ["sat1"], "packets_received": 0}
        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat2"
        self.assertFalse(
            check_pointing_data(message, nats, shared_storage, None))
Exemplo n.º 6
0
    async def test_update_packets_received(self):
        """
        Testing whether packets received updates work properly
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("rl1", "0.0.0.0", "4222", loop=loop, user="******", password="******")
        await nats.connect()

        # testing whether receiving works
        shared_storage = {
            "phonebook": {
                "sat1": False,
                "sat2": False
            },
            "iot_phonebook": {
                "iot1": True,
                "iot2": False
            },
            "ground_phonebook": {
                "grstn1": False,
                "grstn2": False
            },
            "buffered_packets": 2365,
            "packets_received": {
                "grstn1": 212,
                "grstn2": 573
            }
        }

        # testing correct internal case
        message = nats.create_message(1234, MessageSchemas.BUFFER_MESSAGE)
        message.origin_id = "grstn1"
        await update_packets_received(message, nats, shared_storage, None)
        self.assertEqual(shared_storage["packets_received"]["grstn1"], 1446)
Exemplo n.º 7
0
    async def test_fertilization_level(self):
        """
        Tests creating fertilization data
        """
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("iot1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = {"data_rate": 5}

        message = nats.create_message({"time": "2020-07-05T09:51:49.8"},
                                      MessageSchemas.TIMESTEP_MESSAGE)
        await fertilization_level(message, nats, shared_storage, None)

        time_struct = datetime.strptime("2020-07-05T09:51:49.8",
                                        "%Y-%m-%dT%H:%M:%S.%f")
        total_seconds = (time_struct - datetime(1970, 1, 1)).total_seconds()
        seconds_in_week = total_seconds % (60 * 60 * 24 * 7)
        data_value = 0.25 * math.sin(2 * math.pi * seconds_in_week *
                                     (1 / 604800)) + 0.25

        self.assertEqual(nats._dict["data.out"][0].data, data_value)
Exemplo n.º 8
0
    async def test_update_rl(self):
        """
        Testing whether rl service provides proper new inputs for RL environment
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("rl1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # testing whether receiving works
        shared_storage = {
            "predict_mode": False,
            "phonebook": {
                "sat1": False,
                "sat2": False
            },
            "iot_phonebook": {
                "iot1": True,
                "iot2": False
            },
            "ground_phonebook": {
                "grstn1": False,
                "grstn2": False
            },
            "buffered_packets": 2365,
            "packets_received": {
                "grstn1": 212,
                "grstn2": 573
            }
        }

        message = nats.create_message(None, MessageSchemas.RL_MESSAGE)
        response = await update_rl(message, nats, shared_storage, None)
        self.assertIsInstance(response, Message)
        self.assertEqual(
            response.data.keys(), {
                "phonebook": None,
                "iot_phonebook": None,
                "ground_phonebook": None,
                "buffered_packets": None
            }.keys())
        self.assertEquals(response.data["phonebook"], {
            "sat1": False,
            "sat2": False
        })
        self.assertEquals(response.data["iot_phonebook"], {
            "iot1": True,
            "iot2": False
        })
        self.assertEquals(response.data["ground_phonebook"], {
            "grstn1": False,
            "grstn2": False
        })
        self.assertEquals(response.data["buffered_packets"], 2365)
Exemplo n.º 9
0
    async def test_send_visualization_packet(self):
        """
        TODO
        """
        logger = FakeLogger()
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()
        f = open("./config/simulation_config/simulation/czml.json")

        shared_storage = json.load(f)["shared_storage"]
        shared_storage["swarm"]["cubesat_1"] = {
            "orbit": {
                "semimajor_axis": 6801395.04,
                "eccentricity": 0.0008641,
                "inclination": 0.0,
                "perigee_argument": 2.0150855958,
                "right_ascension_of_ascending_node": 3.6338243719,
                "anomaly": 4.270387838,
                "anomaly_type": "MEAN",
                "orbit_update_date": "2020-07-15T00:00:00.000",
                "frame": "EME",
                "attitude": "nadir_tracking"
            },
            "target_in_view": True,
            "last_update_time": "2010-07-02T03:00:00.000",
            "mode": "receiving"
        }
        shared_storage["grstns"]["grstn_1"] = {
            "location": {
                "latitude": 0.0,
                "longitude": 0.0,
                "altitude": 0.0
            }
        }
        data = {
            "sender_ID": "cubesat_2",
            "time_sent": "2021-07-31T03:00:00.000",
            "data": {
                "time": "2021-07-31T03:00:00.000"
            }
        }
        message = nats.create_message(data["data"],
                                      MessageSchemas.TIMESTEP_MESSAGE)
        print(shared_storage)
        await graphics_service.send_visualization_packet(
            message, nats, shared_storage, logger)

        for i in nats._dict["graphics.grstn2sat"]:
            print(i.data)
        self.assertTrue(False)
Exemplo n.º 10
0
    async def test_update_iot_phonebook(self):
        """
        Testing whether iot phonebook updates work properly
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("rl1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # testing whether receiving works
        shared_storage = {
            "phonebook": {
                "sat1": False,
                "sat2": False
            },
            "iot_phonebook": {
                "iot1": True,
                "iot2": False
            },
            "ground_phonebook": {
                "grstn1": False,
                "grstn2": False
            },
            "buffered_packets": 2365,
            "packets_received": {
                "grstn1": 212,
                "grstn2": 573
            }
        }

        # testing correct case
        message = nats.create_message({
            "iot1": False,
            "iot2": True
        }, MessageSchemas.PHONEBOOK_MESSAGE)

        await update_iot_phonebook(message, nats, shared_storage, None)
        self.assertTrue(shared_storage["iot_phonebook"]["iot2"])
        self.assertFalse(shared_storage["iot_phonebook"]["iot1"])

        # testing wrong case
        message = nats.create_message({
            "iot1": True,
            "iot2": False
        }, MessageSchemas.PHONEBOOK_MESSAGE)
        message.origin_id = "sat2"
        await update_iot_phonebook(message, nats, shared_storage, None)
        self.assertTrue(shared_storage["iot_phonebook"]["iot2"])
        self.assertFalse(shared_storage["iot_phonebook"]["iot1"])
Exemplo n.º 11
0
 async def test_initialize_rl(self):
     """
     Test Initializing RL 
     """
     loop = asyncio.get_running_loop()
     nats = FakeNatsHandler("rl1", "0.0.0.0", "4222", loop=loop, user="******", password = "******")
     await nats.connect()
     model_path = "./saved_model_simple_scenario"
     weights_path = "./saved_weights_simple_scenario/dqn_dataEnv-v0_weights.h5f"
     shared_storage = {
       "phonebook": {"cubesat_1": True
         },
       "iot_phonebook": {"iot_1": False
       },
       "ground_phonebook":{
                         "groundstation_1": False
       },
       "packets_received": {
         "groundstation_1": 0
       },
       "buffered_packets": 0,
       "last_buffered_packets": 0,
       "predict_mode": True,
       "weights_location": weights_path,
       "model_location": model_path,
       "new_phonebook": True,
       "new_iot_phonebook": True,
       "new_ground_phonebook": True
     }
     await initialize_rl(nats, shared_storage, None)
     self.assertTrue(len(agents)>0)
Exemplo n.º 12
0
    async def test_send_timestep(self):
        """
        Test timesteps
        """
        loop = asyncio.get_running_loop()
        shared_storage = {
            "start_time": "2020-07-15T00:00:00.000000",
            "time_step": 50.0
        }
        nats = FakeNatsHandler("clock",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()
        logger = FakeLogger()

        await send_timestep(nats, shared_storage, logger)

        self.assertEqual(len(nats._dict["simulation.timestep"]), 1)
        self.assertEqual(nats._dict["simulation.timestep"][0].data["time"],
                         "2020-07-15T00:00:00.000000")
        self.assertEqual(shared_storage["start_time"],
                         "2020-07-15T00:00:50.000000")
Exemplo n.º 13
0
 async def test_iot_send_data(self):
     """
     Tests whether a new data packet is sent upon receiving a timestep signal
     """
     loop = asyncio.get_running_loop()
     nats = FakeNatsHandler("iot1",
                            "0.0.0.0",
                            "4222",
                            loop=loop,
                            user="******",
                            password="******")
     await nats.connect()
     message = nats.create_message({"time": "2020-07-05T09:51:49.8"},
                                   MessageSchemas.TIMESTEP_MESSAGE)
     shared_storage = {"data_rate": 5}
     await send_data(message, nats, shared_storage, None)
     self.assertEqual(len(nats._dict["data.out"]), 5)
Exemplo n.º 14
0
    async def test_initialize_service(self):
        """
        Test initialization of services based on config files
        """
        loop = asyncio.get_running_loop()
        shared_storage = {
            "simulation": {
                "clock": False,
                "logging": False,
                "czml": False,
                "config": False
            },
            "cubesats": {
                "cubesat_1": {
                    "orbits": False,
                    "rl": False,
                    "rl_training": False,
                    "data": False,
                    "agriculture": False
                }
            },
            "groundstations": {
                "groundstation_1": {
                    "groundstation": False
                }
            },
            "iots": {
                "iot_1": {
                    "iot": False
                }
            },
            "config_path": "./simulation_config"
        }
        nats = FakeNatsHandler("data1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()
        logger = FakeLogger()

        message = nats.create_message("data",
                                      MessageSchemas.SERVICE_TYPE_MESSAGE)
        await initialize_service(message, nats, shared_storage, logger)
        self.assertTrue(shared_storage["cubesats"]["cubesat_1"]["data"])
Exemplo n.º 15
0
    async def test_send_ip_address(self):
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = dict()

        message = nats.create_message({"time": ""},
                                      MessageSchemas.TIMESTEP_MESSAGE)

        await send_ip_address(message, nats, shared_storage, None)
        self.assertEqual(nats._dict["cluster.ip"][0].data, "api_host")
Exemplo n.º 16
0
    async def test_update_sat_ip(self):

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = {"ip_map": {}, "ip_cluster_map": {}}

        message = nats.create_message("ip_1",
                                      MessageSchemas.IP_ADDRESS_MESSAGE)
        await ground_service.update_sat_ip(message, nats, shared_storage, None)

        self.assertEqual(shared_storage["ip_map"], {"cubesat_1": "ip_1"})
        self.assertEqual(shared_storage["ip_cluster_map"], {"cubesat_1": []})
Exemplo n.º 17
0
    async def test_receive_data(self):
        """
        Testing true case and edge cases for iot send data
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("data1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # testing whether receiving works
        shared_storage = {
            "pointing": ["sat1"],
            "pointing_to": "sat1",
            "mode": "receiving"
        }
        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat1"
        self.assertTrue(
            check_pointing_and_mode(message, nats, shared_storage, None))
        await receive_packet(message, nats, shared_storage, None)
        self.assertEqual(len(nats._dict["internal.data.in"]), 1)

        # testing whether receiving blocks if in wrong mode
        shared_storage = {
            "pointing": ["sat1"],
            "pointing_to": "sat1",
            "mode": "sending"
        }
        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat1"
        self.assertFalse(
            check_pointing_and_mode(message, nats, shared_storage, None))

        # testing whether receiving blocks if not pointing
        shared_storage = {
            "pointing": ["sat2"],
            "pointing_to": "sat1",
            "mode": "receiving"
        }
        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat1"
        self.assertFalse(
            check_pointing_and_mode(message, nats, shared_storage, None))

        # testing whether receiving blocks not pointing
        shared_storage = {
            "pointing": ["sat1"],
            "pointing_to": "sat2",
            "mode": "receiving"
        }
        message = nats.create_message(23.12, MessageSchemas.IOT_DATA_MESSAGE)
        message.sender_id = "sat1"
        self.assertFalse(
            check_pointing_and_mode(message, nats, shared_storage, None))
Exemplo n.º 18
0
    async def test_update_cluster(self):
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = {
            "ip_map": {
                "cubesat_1": "ip_1",
                "cubesat_2": "ip_2",
                "cubesat_3": "ip_3"
            },
            "ip_cluster_map": {
                "cubesat_1": [],
                "cubesat_2": [],
                "cubesat_3": []
            }
        }
        message = nats.create_message(
            {
                "cubesat_1": True,
                "cubesat_2": True,
                "cubesat_3": True
            }, MessageSchemas.PHONEBOOK_MESSAGE)
        await update_cluster(message, nats, shared_storage, None)

        cluster_message = {
            "recipient": "cubesat_1",
            "ip_map": ["ip_2", "ip_3"]
        }

        self.assertTrue(nats._dict["command.cluster"][0], cluster_message)
        self.assertTrue(
            shared_storage["ip_cluster_map"]["cubesat_1"] == ["ip_2", "ip_3"])
Exemplo n.º 19
0
    async def test_soil_water_content(self):
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("iot1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = {"data_rate": 5}

        message = nats.create_message({"time": "2020-07-05T09:51:49.8"},
                                      MessageSchemas.TIMESTEP_MESSAGE)
        await soil_water_content(message, nats, shared_storage, None)

        time_struct = datetime.strptime("2020-07-05T09:51:49.8",
                                        "%Y-%m-%dT%H:%M:%S.%f").timetuple()
        total_seconds = time_struct.tm_hour * 3600 + time_struct.tm_min * 60 + time_struct.tm_sec
        data_value = 10 * math.cos(2 * math.pi * total_seconds *
                                   (1 / 86400)) + 40

        self.assertEqual(nats._dict["data.out"][0].data, data_value)
Exemplo n.º 20
0
    async def test_create_log_file(self):
        """
        Testing the creation of the log files
        """
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        shared_storage = {"log_path": "./"}
        await logging_service.create_log_file(nats, shared_storage, None)
        file_made = False
        for i in os.listdir():
            if f"log-{datetime.utcnow().isoformat()[:19]}" in i:
                file_made = True
                os.remove(f"./{i}")
                break
        self.assertTrue(file_made)
Exemplo n.º 21
0
    async def test_cubesat_state(self):
        """
        Test for cubesat_state() callback
        """
        # Creating variables that will be used as arguments for the callback
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # Opening a file that contains a sample shared storage
        f = open("test_orbits_config.json")
        shared_storage = json.load(f)

        # Updated state that will be sent
        state = {
            "cubesat_1": {
                "orbit": CONSTS.ORBIT_3,
                "last_update_time": "2022-12-02T03:00:00.000"
            }
        }

        # Data that will be sent in the message
        data = {
            "sender_ID": "cubesat_2",
            "time_sent": "2020-07-05T09:51:49",
            "data": {
                "id": "cubesat_1",
                "state": state
            }
        }

        message = Message.decode_json(data, MessageSchemas.STATE_MESSAGE)
        await orbit_service.cubesat_state(message, nats, shared_storage, None)
        # Verifying that cubesat_state() worked properly
        self.assertTrue(
            shared_storage["swarm"]["cubesat_1"] == state["cubesat_1"])
Exemplo n.º 22
0
    async def test_update_pointing_list(self):
        """
        Tests whether the list of sats pointing at the ground station is properly updated
        """

        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("ground1",
                               "0.0.0.0",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()
        shared_storage = {"pointing": []}

        message = nats.create_message(
            {
                "state": {
                    "sat1": {
                        "orbit": {
                            "semimajor_axis": 12742500.0,
                            "eccentricity": 0.0,
                            "inclination": 0.78,
                            "perigee_argument": 1.570796,
                            "right_ascension_of_ascending_node": 0.0,
                            "anomaly": 1.570796,
                            "anomaly_type": "TRUE",
                            "orbit_update_date": "2021-12-02T00:00:00.000",
                            "frame": "EME",
                            "attitude": "ground1"
                        },
                        "target_in_view": True,
                        "last_update_time": "2021-12-02T03:00:00.000"
                    }
                }
            }, MessageSchemas.STATE_MESSAGE)

        message.origin_id = "sat1"
        await update_pointing_list(message, nats, shared_storage, None)
        self.assertEqual(shared_storage["pointing"], ["sat1"])

        message = nats.create_message(
            {
                "state": {
                    "sat2": {
                        "orbit": {
                            "semimajor_axis": 12742500.0,
                            "eccentricity": 0.0,
                            "inclination": 0.1,
                            "perigee_argument": 1.0,
                            "right_ascension_of_ascending_node": 0.0,
                            "anomaly": 0.0,
                            "anomaly_type": "TRUE",
                            "orbit_update_date": "2021-12-02T00:00:00.000",
                            "frame": "EME",
                            "attitude": "ground1"
                        },
                        "target_in_view": True,
                        "last_update_time": "2021-12-02T03:00:00.000"
                    }
                }
            }, MessageSchemas.STATE_MESSAGE)
        message.origin_id = "sat2"
        await update_pointing_list(message, nats, shared_storage, None)
        self.assertEqual(shared_storage["pointing"], ["sat1", "sat2"])

        message = nats.create_message(
            {
                "state": {
                    "sat1": {
                        "orbit": {
                            "semimajor_axis": 12742500.0,
                            "eccentricity": 0.0,
                            "inclination": 0.78,
                            "perigee_argument": 1.570796,
                            "right_ascension_of_ascending_node": 0.0,
                            "anomaly": 1.570796,
                            "anomaly_type": "TRUE",
                            "orbit_update_date": "2021-12-02T00:00:00.000",
                            "frame": "EME",
                            "attitude": "iot2"
                        },
                        "target_in_view": True,
                        "last_update_time": "2021-12-02T03:00:00.000"
                    }
                }
            }, MessageSchemas.STATE_MESSAGE)
        message.origin_id = "sat1"
        await update_pointing_list(message, nats, shared_storage, None)
        self.assertEqual(shared_storage["pointing"], ["sat2"])

        message = nats.create_message(
            {
                "state": {
                    "sat2": {
                        "orbit": {
                            "semimajor_axis": 12742500.0,
                            "eccentricity": 0.0,
                            "inclination": 0.1,
                            "perigee_argument": 1.0,
                            "right_ascension_of_ascending_node": 0.0,
                            "anomaly": 0.0,
                            "anomaly_type": "TRUE",
                            "orbit_update_date": "2021-12-02T00:00:00.000",
                            "frame": "EME",
                            "attitude": "ground1"
                        },
                        "target_in_view": False,
                        "last_update_time": "2021-12-02T03:00:00.000"
                    }
                }
            }, MessageSchemas.STATE_MESSAGE)
        message.origin_id = "sat2"
        await update_pointing_list(message, nats, shared_storage, None)
        self.assertEqual(shared_storage["pointing"], [])

        message = nats.create_message(
            {
                "state": {
                    "sat1": {
                        "orbit": {
                            "semimajor_axis": 12742500.0,
                            "eccentricity": 0.0,
                            "inclination": 0.78,
                            "perigee_argument": 1.570796,
                            "right_ascension_of_ascending_node": 0.0,
                            "anomaly": 1.570796,
                            "anomaly_type": "TRUE",
                            "orbit_update_date": "2021-12-02T00:00:00.000",
                            "frame": "EME",
                            "attitude": "ground1"
                        },
                        "target_in_view": False,
                        "last_update_time": "2021-12-02T03:00:00.000"
                    }
                }
            }, MessageSchemas.STATE_MESSAGE)
        message.origin_id = "sat1"
        await update_pointing_list(message, nats, shared_storage, None)
        self.assertEqual(shared_storage["pointing"], [])
Exemplo n.º 23
0
    async def test_simulation_timepulse_propagate(self):
        """
        Test the simulation_timepulse_propagate() callback
        """
        # Creating variables that will be used as arguments for the callback
        logger = FakeLogger()
        loop = asyncio.get_running_loop()
        nats = FakeNatsHandler("cubesat_1",
                               "4222",
                               loop=loop,
                               user="******",
                               password="******")
        await nats.connect()

        # opening a file that contains a sample shared storage
        f = open("test_orbits_config.json")
        shared_storage = json.load(f)

        # Testing the callback across mulitple timesteps
        for i in range(10, 59):
            # creating sample data
            data = {
                "sender_ID": "cubesat_2",
                "time_sent": "2021-12-05T00:" + str(i) + ":00.000",
                "data": {
                    "time": "2021-12-05T00:" + str(i) + ":00.000"
                }
            }

            orbit_1 = shared_storage["swarm"]["cubesat_1"]["orbit"]
            orbit_2 = shared_storage["swarm"]["cubesat_2"]["orbit"]

            attitude_provider_1 = {
                "type":
                "moving_body_tracking",
                "parameters":
                shared_storage["swarm"][shared_storage["swarm"]["cubesat_1"]
                                        ["orbit"]["attitude"]]["orbit"]
            }
            attitude_provider_2 = {
                "type":
                "moving_body_tracking",
                "parameters":
                shared_storage["swarm"][shared_storage["swarm"]["cubesat_2"]
                                        ["orbit"]["attitude"]]["orbit"]
            }

            message = Message.decode_json(data,
                                          MessageSchemas.TIMESTEP_MESSAGE)
            await orbit_service.simulation_timepulse_propagate(
                message, nats, shared_storage, logger)

            time = absolute_time_converter_utc_string(data["data"]["time"])

            # Creating propagators and attitude provider from orbit configuration of satellites
            propagator_1 = orekit_utils.analytical_propagator(orbit_1)
            propagator_2 = orekit_utils.analytical_propagator(orbit_2)
            attitude_provider_1 = orekit_utils.attitude_provider_constructor(
                attitude_provider_1["type"], attitude_provider_1["parameters"])
            attitude_provider_2 = orekit_utils.attitude_provider_constructor(
                attitude_provider_2["type"], attitude_provider_2["parameters"])

            # Setting attitude and propagating the orbit
            propagator_1.setAttitudeProvider(attitude_provider_1)
            propagator_2.setAttitudeProvider(attitude_provider_2)
            state_1 = propagator_1.propagate(time)
            state_2 = propagator_2.propagate(time)

            # Updating param
            cubesat_1_param = orekit_utils.get_keplerian_parameters(state_1)
            cubesat_1_param.update({"attitude": "cubesat_2"})
            cubesat_1_param.update({"frame": "EME"})
            cubesat_2_param = orekit_utils.get_keplerian_parameters(state_2)
            cubesat_2_param.update({"attitude": "cubesat_1"})
            cubesat_2_param.update({"frame": "EME"})

            # Checking to see if simulation_timestep_propagate updated params correctly
            self.assertTrue(shared_storage["swarm"]["cubesat_1"]["orbit"] ==
                            cubesat_1_param)
            self.assertTrue(shared_storage["swarm"]["cubesat_2"]["orbit"] ==
                            cubesat_2_param)
            self.assertTrue(shared_storage["swarm"]["cubesat_1"]["orbit"]
                            ["attitude"] == "cubesat_2")
            self.assertTrue(shared_storage["swarm"]["cubesat_2"]["orbit"]
                            ["attitude"] == "cubesat_1")
            print(shared_storage["swarm"]["cubesat_1"]["target_in_view"])
            print(shared_storage["swarm"]["cubesat_2"]["target_in_view"])
            # Making sure that phonebook gets updated properly
            if i <= 48:
                self.assertTrue(shared_storage["sat_phonebook"]["cubesat_2"])
                self.assertTrue(
                    shared_storage["swarm"]["cubesat_2"]["target_in_view"])
            else:
                self.assertFalse(shared_storage["sat_phonebook"]["cubesat_2"])
                self.assertFalse(
                    shared_storage["swarm"]["cubesat_2"]["target_in_view"])