generate_swagger.py 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
"""
Generate swagger to view models/schemas.
Command:
1/ python3 generate_swagger.py
2/ Go to http://127.0.0.1:5000/soardocs
WARNING: API Endpoints are NOT functional. Purely for easy-reading.
"""
from flask import Flask, request
from flasgger import Swagger, LazyString
from flask_restx import Api, fields, Resource


13
app = Flask(__name__)
14
api = Api(app)
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
swagger_template = dict(
    info={
        "title": LazyString(lambda: "SoAR Backbone Message Formats"),
        "version": LazyString(lambda: "0.1"),
        "description": LazyString(
            lambda: "Backbone Message Format component for the Squad of Autonomous Robots (SoAR) message definitions."
        ),
    },
    host=LazyString(lambda: request.host),
)
message_header_schema = api.model(
    "MessageHeader",
    {
        "timestamp": fields.DateTime(
            required=True,
            description="Timestamp of message",
            example="2022-11-16T00:00:00Z",
        ),
        "source": fields.String(
            required=True,
            description="Where is this message from",
            example="autonomy_engine",
        ),
        "destination": fields.String(
            required=True,
            description="What is the destination of this message",
            example="ah-1",
        ),
        "encoded": fields.Boolean(
            required=True,
            description="Indicate that message raw (encoded) or decoded. "
            + "Options: encoded=True, decoded=False",
            example=False,
        ),
        "type": fields.String(
            required=True,
            description="Type of message",
53
            example="vehicle_status",
54
        ),
55
        "payload": fields.Raw(required=True)  # TODO: schema applicable changes according to "type"
56 57 58
    },
)

59 60
full_message_schema = api.model(
    "FullMessageSchema",
61
    {
62
        "uuid": fields.DateTime(   # add this
63
            required=True,
64 65
            description="Timestamp of message",
            example="2022-11-16T00:00:00Z",
66
        ),
67
        "timestamp": fields.DateTime(
68
            required=True,
69 70
            description="Timestamp of message",
            example="2022-11-16T00:00:00Z",
71
        ),
72
        "source": fields.String(
73
            required=True,
74 75
            description="Where is this message from",
            example="autonomy_engine",
76
        ),
77
        "destination": fields.String(
78
            required=True,
79
            description="What is the destination of this message",
80 81
            example="ah-1",
        ),
82
        "encoded": fields.Boolean(
83
            required=True,
84 85 86
            description="Indicate that message raw (encoded) or decoded. "
            + "Options: encoded=True, decoded=False",
            example=False,
87
        ),
88
        "type": fields.String(
89
            required=True,
90 91
            description="Type of message",
            example="vehicle_status",
92
        ),
93
        "payload": fields.Raw(
94
            required=True,
95 96
            description="Content of Message",
            # example="{}",
97
        ),
98
    }
99 100
)

101

102
constraints_schema = api.model(
103
    "ConstraintsSchema",
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    {
        "min_altitude": fields.Float(
            required=True,
            description="Minimum altitude set for squad.",
            example=15.2,
        ),
        "min_velocity": fields.Float(
            required=True,
            description="Minimum velocity set for squad.",
            example=0.1,
        ),
        "max_velocity": fields.Float(
            required=True,
            description="Maximum altitude set for squad.",
            example=0.9,
        ),
    },
)

123 124
vehicle_schema = api.model(
    "VehicleSchema",
125
    {
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        "vehicle_ID": fields.Integer(
            required=True,
            description="unique identifier for vehicle",
            example="ah-1",
        ),
        "serial": fields.Integer(
            required=True,
            description="vehicle serial number",
            example="ah-1",
        ),
        "model": fields.Integer(
            required=True,
            description="vehicle serial number",
            example="ah-1",
        ),
        "constraints": fields.Nested(constraints_schema),
        "active": fields.Boolean(
            required=False,
            description="When a vehicle is in deployment (executing a mission plan) this should be True",
            example=True,
        ),
147 148
    },
)
149 150 151

squad_metadata_schema = api.model(
    "SquadMetadataSchema",
152
    {
153
        "squad_ID": fields.Integer(
154
            required=True,
155 156
            description="Identifier of given squad",
            example="ah-1",
157
        ),
158
        "no_of_vehicles": fields.Integer(
159
            required=True,
160 161
            description="number of vehicles",
            example="ah-1",
162
        ),
163 164
        "vehicles": fields.List(
            fields.Nested(vehicle_schema),
165
            required=True,
166
            description="Squad consists of these vehicles",
167
        ),
168 169 170 171
        "squad_mission_type": fields.String(
            required=True,
            description="Mission of given squad: `tracking`, `survey`, `inspection`",
            example="survey",
172
        ),
173 174 175 176
        "squad_state": fields.Boolean(
            required=True,
            description="In execution, Waiting.. <define further>",
            example=False,
177 178 179 180 181 182 183 184 185
        ),
    },
)

gps_schema = api.model(
    "GPS",
    {
        "gps_source": fields.Float(  # TODO: TBD with partners
            required=False,
186
            description="Source of gps position. E.g. USBL (external), vehicle itself (internal)",
187 188 189 190 191 192 193 194 195 196 197 198 199 200
            example="internal",
        ),
        "latitude_type": fields.String(
            required=False,
            description="",
            example="",
        ),
        "longitude_type": fields.String(
            required=False,
            description="",
            example="",
        ),
        "latitude": fields.Float(
            required=False,
201
            description="Latitude in <DEFINE UNITS>",
202 203 204 205
            example="",
        ),
        "longitude": fields.Float(
            required=False,
206
            description="Longitude in <DEFINE UNITS>",
207 208 209 210
            example="",
        ),
        "depth": fields.Float(
            required=False,
211
            description="Depth in <DEFINE UNITS>",
212 213
            example="",
        ),
214
        "altitude": fields.Float(
215
            required=False,
216
            description="Altitude in <DEFINE UNITS>",
217 218
            example="",
        ),
219 220 221 222 223
        # "gps_fix_seconds_ago": fields.Float(
        #     required=False,
        #     description="",
        #     example="",
        # ),
224 225
    },
)
226

227 228 229 230 231 232 233 234
observation_schema = api.model(
    "Observation",
    {
        "message": fields.Nested(
            message_header_schema,
            required=True,
            description="Message header",
        ),
235
        "vehicle": fields.Nested(vehicle_schema),
236 237 238 239 240
        "time": fields.String(
            required=True,
            description="Timestamp of message",
            example="2022-11-16T00:00:00Z",
        ),
241 242 243
        # "observation_type" ==> payloads tied to different types maybe?
        # properties of each observation?
        "points_of_interest": fields.Float(
244
            required=False,
245
            description="Points from features of interest identified by vehicle if any found. DEFINE FORMAT.",
246 247
            example="",
        ),
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        "region_surveyed": fields.Float(
            required=False,
            description="Region surveyed by given vehicle. DEFINE FORMAT. GEOJSON?",
            example="",
        ),
        "quality_of_points": fields.Float(
            required=False,
            description="Quality/strength of points from features of interest identified by vehicle. DEFINE FORMAT.",
            example=0.98,
        ),
        "additional_data": fields.Raw(
            required=False,
            description="Placeholder field for any additional data",
            example={"sensor_payload": False},
        ),
263 264
    }
)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
sensor_schema = api.model(
    "Sensor",
    {
        "sensor_name": fields.String(
            required=False,
            description="Name of sensor (e.g. MBES for AH1 and SideScan for Ecosub",
            example="MBES",
        ),
        "sensor_status": fields.Boolean(
            required=False,
            description="Sensor switched on (True) or off (False)",
            example=True,
        ),
        "additional_data": fields.Raw(
            required=False,
            description="Add any additional sensor-related data here.",
            example={"sensor_loadtime_seconds": 30.0},
        ),
    }
)
vehicle_status_message_schema = api.model(
    "VehicleStatusMessage",
287 288 289 290 291 292
    {
        "message": fields.Nested(
            message_header_schema,
            required=True,
            description="Message header",
        ),
293 294 295
        "vehicle": fields.Nested(vehicle_schema),
        "vehicle_state": fields.String(
            # TODO: Define dictionary with potential STATES of each vehicle
296
            required=False,
297
            description="Current state executed by vehicle. E.g. "
298 299 300
            + "STOP, IDLE, ABORT.",
            example="IDLE",
        ),
301 302 303 304 305
        "autonomy_mission_ID": fields.Integer(
            required=False,
            description="Last mission plan ID (according to Autonomy Engine's mission plan number) executed by vehicle",
            example=1,
        ),
306 307 308 309 310 311 312 313 314 315 316 317 318
        "mission_track_number": fields.Integer(
            required=False,
            description=(
                "Track number - stage in mission (e.g. "
                + "4 --> Waypoint 3 to Waypoint 4)"
            ),
            example=4,
        ),
        "range_to_go": fields.Float(
            required=False,
            description="Estimated distance to reach next waypoint",
            example=124.3,
        ),
319
        "speed_over_ground": fields.Float(
320
            required=False,
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
            description="",
            example=124.3,
        ),
        "water_current_velocity": fields.Float(
            required=False,
            description="",
            example=124.3,
        ),
        "thrust_applied": fields.Float(
            required=False,
            description="TODO: Needs further consideration",
            example=124.3,
        ),
        "health_status": fields.String(
            required=False,
            description="Health status extracted by respective vehicle "
337 338 339
            + "if any diagnosis available checks on sensors",
            example="Warning",
        ),
340 341
        "gps_data": fields.List(
            fields.Nested(gps_schema),  # TODO: TBD Do we want a list of gps readings to allow > 1 reading i.e. vehicle + usbl
342
            required=True,
343
            description="Metadata pf each vehicle",
344
        ),
345
        "localisation_error": fields.Float(
346
            required=False,
347 348
            description="Localisation error at last USBL update.",
            example="",
349
        ),
350
        "usbl_fix_seconds_ago": fields.Float(
351
            required=False,
352 353
            description="",
            example="",
354
        ),
355
        "battery_remaining_capacity": fields.Float(
356
            required=False,
357 358 359 360 361 362 363 364 365 366 367 368 369 370
            description="Battery remaining capacity % provided by respective C2 if any.",
            example=80.0,
        ),
        "sensor_config": fields.Nested(sensor_schema),  # TODO: TBD Do we want a list of sensors to allow > 1 sensor
    },
)

mission_plan_schema = api.model(
    "MissionPlan",
    {
        "message": fields.Nested(
            message_header_schema,
            required=True,
            description="Message header",
371
        ),
372 373 374 375 376 377 378 379 380 381 382 383
        "ID": fields.Integer(
            required=True,
            description="Unique identifier tagged to version of this"
            + " configuration plan",
            example=3,
        ),
        "time": fields.String(
            required=True,
            description="",
            example="",
        ),
        "vehicle_ID": fields.Integer(
384
            required=False,
385
            description="Details of each squad",
386
        ),
387 388 389
        "payload": fields.Raw(
            required=True,
        )
390 391 392
    },
)

393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
swagger_config = {
    "headers": [],
    "specs": [
        {
            "endpoint": "swagger",
            "route": "/swagger.json",
            "rule_filter": lambda rule: True,
            "model_filter": lambda tag: True,
        }
    ],
    "static_url_path": "/flasgger_static",
    "swagger_ui": True,
    "specs_route": "/soardocs/",
    "swagger": "2.0",
    "basePath": "/soar",
    "info": {
        "title": "soar",
        "version": "0.1",
        "description": "SoAR message schemas",
    },
    "produces": ["application/json"],
    "consumes": ["application/json"],
}
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
autonomy_configuration_schema = api.model(
    "SquadConfigurationSchema",
    {
        "message": fields.Nested(
            message_header_schema,
            required=True,
            description="Message header",
        ),
        "ID": fields.Integer(
            required=True,
            description="Unique identifier tagged to version of this"
            + " configuration plan",
            example=3,
        ),
        "time": fields.String(
            required=True,
            description="",
            example="",
        ),
        "squads": fields.Nested(
            squad_metadata_schema,
            required=False,
            description="Details of each squad",
        ),
        "region_of_operation": fields.Raw(
            required=True,
            description="Using GEOJSON, exact region of interest EXCLUDING exclusion zone",
            example={
                "type": "FeatureCollection",
                "features": [
                    {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "coordinates": [
                        [
                            [
                            -4.187143188645706,
                            50.37072283932642
                            ],
                            [
                            -4.202697005964865,
                            50.368816892405874
                            ],
                            [
                            -4.203156724702808,
                            50.365640144076906
                            ],
                            [
                            -4.19449868846155,
                            50.362267670845654
                            ],
                            [
                            -4.187373048015246,
                            50.35933489062816
                            ],
                            [
                            -4.1859938917992565,
                            50.36207215780212
                            ],
                            [
                            -4.188598964651192,
                            50.36471151593676
                            ],
                            [
                            -4.192506573927574,
                            50.367350727294706
                            ],
                            [
                            -4.188675584440716,
                            50.36837704762925
                            ],
                            [
                            -4.187143188645706,
                            50.37072283932642
                            ]
                        ]
                        ],
                        "type": "Polygon"
                        # "projection": coordinate system used
                    }
                    }
                ]
            }
        ),
    },
)


507 508
swagger = Swagger(app, template=swagger_template, config=swagger_config)

509 510 511 512
ns1 = api.namespace("message", description="Message Wrapper (Full Message Schema) Format")
@ns1.route("/wrapper")
class MessageWrapper(Resource):
    @ns1.response(200, "Success", full_message_schema)
513 514 515 516
    def get(self):
        pass


517
ns2 = api.namespace("vehicle_status", description="vehicle Status Message Format")
518
@ns2.route("")
519 520
class VehicleStatus(Resource):
    @ns2.response(200, "Success", vehicle_status_message_schema)
521 522 523 524 525
    def get(self):
        pass


ns3 = api.namespace(
526
    "autonomy_configuration", description="Autonomy Configuration Format. Do we want region of interest to be per squad, per vehicle or in the main schema?"
527 528 529 530 531 532 533 534 535
)

@ns3.route("")
class AutonomyConfiguration(Resource):
    @ns3.response(200, "Success", autonomy_configuration_schema)
    def get(self):
        pass


536 537 538 539 540 541 542 543 544
# @api.route('/mission-plan/<str:vehicle_type')
# @api.doc(params={"vehicle_type": "The type of vehicle of the mission plan to target."})
ns4 = api.namespace("mission_plan", description="Mission Plan Format Per vehicle")

@ns4.route("")
class MissionPlan(Resource):
    @ns4.response(200, "Success", mission_plan_schema)
    def get(self):
        pass
545 546 547

@ns4.route("/ecosub")
class MissionPlanEcosub(Resource):
548
    @ns4.response(200, "Success", mission_plan_schema)
549 550
    def get(self):
        pass
551 552
@ns4.route("/reav")
class MissionPlanReav(Resource):
553
    @ns4.response(200, "Success", mission_plan_schema)
554 555 556 557
    def get(self):
        pass
@ns4.route("/autosubhover")
class MissionPlanAutosubHover(Resource):
558
    @ns4.response(200, "Success", mission_plan_schema)
559 560 561
    def get(self):
        pass

562
ns5 = api.namespace("observation", description="Observation Format --> Per vehicle or generic?")
563 564 565 566 567
@ns5.route("")
class Observation(Resource):
    @ns5.response(200, "Success", observation_schema)
    def get(self):
        pass
568 569 570

if __name__ == "__main__":
    app.run()