Beispiel #1
0
    def test_json_back_and_forth(self) -> None:
        """ test converting back and forth from and to json """
        # GIVEN
        input_dict = TestCoordinationInputValidation.get_default_inputs()

        # WHEN
        offset = Offset(**input_dict)

        # THEN converting back and forth should in the end give the same result
        offset_dict = offset.to_json()
        offset_from_json = Offset.from_json(offset_dict=offset_dict)
        self.assertDictEqual(offset_dict, offset_from_json.to_json())
Beispiel #2
0
    def from_json(intersection_dict: Dict) -> Intersection:
        """
        Loading intersection from json (expected same json structure as generated with to_json)
        :param intersection_dict:
        :return: intersection object
        """
        # load signal groups
        signalgroups = [
            SignalGroup.from_json(signalgroup_dict=signalgroup_dict)
            for signalgroup_dict in intersection_dict["signalgroups"]
        ]

        if "periodic_orders" in intersection_dict:
            periodic_orders = [
                PeriodicOrder.from_json(order_dict=order_dict)
                for order_dict in intersection_dict["periodic_orders"]
            ]
        else:
            periodic_orders = []

        # load conflicts
        conflicts = [
            Conflict.from_json(conflict_dict=conflict_dict)
            for conflict_dict in intersection_dict["conflicts"]
        ]

        # load other relations (synchronous starts, offsets and greenyellow_lead)
        sync_starts = []
        offsets = []
        greenyellow_leads = []
        greenyellow_trails = []
        for other_relation_dict in intersection_dict["other_relations"]:
            assert other_relation_dict["from_start_gy"] == other_relation_dict["to_start_gy"], \
                "besides conflicts, at the moment the cloud api can only handle synchronous starts, offsets, " \
                "greenyellow-leads and greenyellow-trails."
            if other_relation_dict[
                    "from_start_gy"] is True and other_relation_dict[
                        "to_start_gy"] is True:
                if other_relation_dict["min_time"] == other_relation_dict[
                        "max_time"]:
                    if other_relation_dict["min_time"] == 0:  # sync start
                        sync_starts.append(
                            SyncStart.from_json(
                                sync_start_dict=other_relation_dict))
                    else:  # offset
                        offsets.append(
                            Offset.from_json(offset_dict=other_relation_dict))
                else:  # greenyellow-leads
                    greenyellow_leads.append(
                        GreenyellowLead.from_json(
                            json_dict=other_relation_dict))
            elif other_relation_dict[
                    "from_start_gy"] is False and other_relation_dict[
                        "to_start_gy"] is False:
                greenyellow_trails.append(
                    GreenyellowTrail.from_json(json_dict=other_relation_dict))

        return Intersection(signalgroups=signalgroups,
                            conflicts=conflicts,
                            sync_starts=sync_starts,
                            offsets=offsets,
                            greenyellow_leads=greenyellow_leads,
                            greenyellow_trails=greenyellow_trails,
                            periodic_orders=periodic_orders)