generate_swagger.py 17.7 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
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),
)
26

27

28 29
full_message_schema = api.model(
    "FullMessageSchema",
30
    {
31
        "timestamp": fields.DateTime(
32
            required=True,
33 34
            description="Timestamp of message",
            example="2022-11-16T00:00:00Z",
35
        ),
36
        "source": fields.String(
37
            required=True,
38 39
            description="Where is this message from",
            example="autonomy_engine",
40
        ),
41
        "destination": fields.String(
42
            required=True,
43
            description="What is the destination of this message",
44 45
            example="ah-1",
        ),
46
        "encoded": fields.Boolean(
47
            required=True,
48 49 50
            description="Indicate that message raw (encoded) or decoded. "
            + "Options: encoded=True, decoded=False",
            example=False,
51
        ),
52
        "type": fields.String(
53
            required=True,
54
            description="Type of message",
55
            example="platform_status",
56
        ),
57
        "payload": fields.Raw(
58
            required=True,
59 60
            description="Content of Message",
            # example="{}",
61
        ),
62
    },
63 64 65
)

constraints_schema = api.model(
66
    "ConstraintsSchema",
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    {
        "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,
        ),
    },
)

86 87
platform_schema = api.model(
    "PlatformSchema",
88
    {
89
        "platform_ID": fields.Integer(
90
            required=True,
91
            description="unique identifier for platform",
92 93 94 95
            example="ah-1",
        ),
        "serial": fields.Integer(
            required=True,
96
            description="platform serial number",
97 98 99 100
            example="ah-1",
        ),
        "model": fields.Integer(
            required=True,
101
            description="platform serial number",
102 103 104 105 106
            example="ah-1",
        ),
        "constraints": fields.Nested(constraints_schema),
        "active": fields.Boolean(
            required=False,
107
            description="When a platform is in deployment (executing a mission plan) this should be True",
108 109
            example=True,
        ),
110 111
    },
)
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
action_schema = api.model(
    "AutonomyEngineAction",
    {
        "action": fields.String(
            required=True,
            description="Autonomy Engine's action from `move`, `payload`,"
            + " `dive`, `send_hits`, `scanline`, `scanpoint`.",
            example="move",
        ),
        "flight_style": fields.String(
            required=False,
            description="Platform-specific modes/flight styles to perform next action",
            example="orbit",
        ),
        "latitude_waypoint": fields.Float(
            required=True,
            description="Next waypoint, x-coordinate",
            example=-4.187143188645706,
        ),
        "longitude_waypoint": fields.Float(
            required=True,
            description="Next waypoint, y-coordinate",
            example=50.37072283932642,
        ),
        "altitude": fields.Float(
            required=False,
            description="Altitude of next action",
            example=15.0,
        ),
        "depth": fields.Float(
            required=False,
            description="Depth of next action",
            example=15.0,
        ),
        "activate_payload": fields.Boolean(
            required=False,
            description="To activate/deactivate sensor for Autosub "
            + "Hover-1 --> `MBES` sensor and for EcoSUB --> `Sidescan`",
            example=True,
        ),
        "send_environmental_data": fields.Boolean(
            required=False,
            description="To trigger the platform to send list of observations if any found",
            example=False,
        ),
        "scanline": fields.Boolean(
            required=False,
            description="To trigger the platform to scan-line",
            example=False,
        ),
        "scanpoint": fields.Boolean(
            required=False,
            description="To trigger the platform to send list of observations if any found",
            example=False,
        ),
    },
)

mission_plan_schema = api.model(
    "MissionPlan",
    {
        "message": fields.Nested(
            full_message_schema,
            required=True,
            description="Message header",
        ),
        "plan_ID": fields.Integer(
            required=True,
            description="Identifier of given mission sequence planned",
            example=1,
        ),
        "platform_serial": fields.String(
            required=True,
            description="Serial of target platform to send mission to",
            example="reav-60",
        ),
        "plan": fields.List(
            fields.Nested(action_schema),
            required=True,
            description="Sequence of actions/instructions generated by the " +
            " Autonomy Engine that should be compiled by the respective C2.",
        ),
    },
)

observation_schema = api.model(
    "Observation",
    {
        "message": fields.Nested(
            full_message_schema,
            required=True,
            description="Message header",
        ),
        "platform_serial": fields.String(
            required=True,
            description="Serial of platform to sendign observations",
            example="ecosub-3",
        ),
        # "observation_type" ==> payloads tied to different types maybe?
        # properties of each observation?
        "points_of_interest": fields.Float(
            required=False,
            description="Points from features of interest identified by platform if any found. DEFINE FORMAT.",
            example="",
        ),
        "region_surveyed": fields.Float(
            required=False,
            description="Region surveyed by given platform. DEFINE FORMAT. GEOJSON?",
            example="",
        ),
        "quality_of_points": fields.Float(
            required=False,
            description="Quality/strength of points from features of interest identified by platform. DEFINE FORMAT.",
            example=0.98,
        ),
        "additional_data": fields.Raw(
            required=False,
            description="Placeholder field for any additional data",
            example={"sensor_payload": False},
        ),
    },
)


region_schema = api.model(
    "RegionSchema",
    {
        "region": fields.Raw(
            required=True,
            description="Using GEOJSON, exact region of interest in rectangle format polygon",
            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],
                                ]
                            ],
                            "type": "Polygon",
                        },
                    }
                ],
            },
        ),
    },
)

267 268
squad_metadata_schema = api.model(
    "SquadMetadataSchema",
269
    {
270
        "squad_ID": fields.Integer(
271
            required=True,
272
            description="Identifier of given squad",
273
            example=23,
274
        ),
275
        "no_of_platforms": fields.Integer(
276
            required=True,
277 278
            description="Number of platforms",
            example=3,
279
        ),
280 281
        "platforms": fields.List(
            fields.Nested(platform_schema),
282
            required=True,
283
            description="Squad consists of these platforms",
284
        ),
285 286 287 288
        "squad_mission_type": fields.String(
            required=True,
            description="Mission of given squad: `tracking`, `survey`, `inspection`",
            example="survey",
289
        ),
290 291 292 293
        "squad_state": fields.Boolean(
            required=True,
            description="In execution, Waiting.. <define further>",
            example=False,
294
        ),
295 296 297 298 299 300 301 302 303 304
        "region_of_interest": fields.List(
            fields.Nested(region_schema),
            required=False,
            description="Region of interest and exclusion zones per squad.",
        ),
        "exclusion_zones": fields.List(
            fields.Nested(region_schema),
            required=True,
            description="Exclusion zones exclusion zones per squad.",
        ),
305 306 307
    },
)

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

planning_configuration_schema = api.model(
    "PlanningConfigurationSchema",
    {
        "message": fields.Nested(
            full_message_schema,
            required=True,
            description="Message header",
        ),
        "ID": fields.Integer(
            required=True,
            description="Unique identifier tagged to version of this"
            + " configuration plan",
            example=3,
        ),
        "squads": fields.Nested(
            squad_metadata_schema,
            required=False,
            description="Details of each squad",
        ),
    },
)


332 333 334 335 336
gps_schema = api.model(
    "GPS",
    {
        "gps_source": fields.Float(  # TODO: TBD with partners
            required=False,
337
            description="Source of gps position. E.g. USBL (external), platform itself (internal)",
338 339 340 341 342 343 344 345 346 347 348 349 350 351
            example="internal",
        ),
        "latitude_type": fields.String(
            required=False,
            description="",
            example="",
        ),
        "longitude_type": fields.String(
            required=False,
            description="",
            example="",
        ),
        "latitude": fields.Float(
            required=False,
352
            description="Latitude in <DEFINE UNITS>",
353 354 355 356
            example="",
        ),
        "longitude": fields.Float(
            required=False,
357
            description="Longitude in <DEFINE UNITS>",
358 359 360 361
            example="",
        ),
        "depth": fields.Float(
            required=False,
362
            description="Depth in <DEFINE UNITS>",
363 364
            example="",
        ),
365
        "altitude": fields.Float(
366
            required=False,
367
            description="Altitude in <DEFINE UNITS>",
368 369
            example="",
        ),
370
        # "gps_fix_seconds_ago"
371 372
    },
)
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

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},
        ),
392
    },
393
)
394 395
platform_status_message_schema = api.model(
    "platformStatusMessage",
396 397
    {
        "message": fields.Nested(
398
            full_message_schema,
399 400 401
            required=True,
            description="Message header",
        ),
402 403 404
        "platform": fields.Nested(platform_schema),
        "platform_state": fields.String(
            # TODO: Define dictionary with potential STATES of each platform
405
            required=False,
406
            description="Current state executed by platform. E.g. "
407 408 409
            + "STOP, IDLE, ABORT.",
            example="IDLE",
        ),
410
        "autonomy_plan_ID": fields.Integer(
411
            required=False,
412
            description="Last mission plan ID (according to Autonomy Engine's mission plan number) executed by platform",
413 414
            example=1,
        ),
415
        "mission_track_ID": fields.Integer(
416 417 418 419 420 421 422
            required=False,
            description=(
                "Track number - stage in mission (e.g. "
                + "4 --> Waypoint 3 to Waypoint 4)"
            ),
            example=4,
        ),
423 424 425 426 427
        "mission_action_ID": fields.Integer(
            required=False,
            description="to add description",
            example=1,
        ),
428 429 430 431 432
        "range_to_go": fields.Float(
            required=False,
            description="Estimated distance to reach next waypoint",
            example=124.3,
        ),
433
        "speed_over_ground": fields.Float(
434
            required=False,
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
            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,
450
            description="Health status extracted by respective platform "
451 452 453
            + "if any diagnosis available checks on sensors",
            example="Warning",
        ),
454
        "gps_data": fields.List(
455 456 457
            fields.Nested(
                gps_schema
            ),  # TODO: TBD Do we want a list of gps readings to allow > 1 reading i.e. platform + usbl
458
            required=True,
459
            description="Metadata pf each platform",
460
        ),
461
        "localisation_error": fields.Float(
462
            required=False,
463 464
            description="Localisation error at last USBL update.",
            example="",
465
        ),
466
        "usbl_fix_seconds_ago": fields.Float(
467
            required=False,
468 469
            description="",
            example="",
470
        ),
471
        "battery_remaining_capacity": fields.Float(
472 473
            required=True,
            description="Battery remaining capacity % provided by respective platform/C2.",
474 475
            example=80.0,
        ),
476 477 478
        "sensor_config": fields.Nested(
            sensor_schema
        ),  # TODO: TBD Do we want a list of sensors to allow > 1 sensor
479 480 481
    },
)

482 483
acknowledgement_schema = api.model(
    "Acknowledgement",
484 485
    {
        "message": fields.Nested(
486
            full_message_schema,
487 488
            required=True,
            description="Message header",
489
        ),
490
        "message_ID": fields.Integer(
491
            required=True,
492 493 494
            description="Identifier of message received and executed with "
            + "success for mission plans sent by the Autonomy Engine.",
            example=202,
495
        ),
496
        "status": fields.String(
497
            required=True,
498 499
            description="Highest level of acknowledgement. I.e. `c2_received`: Received by C2,"
            + " `c2_sent`: Sent from C2->Platform, `executed`: Executed by platform",
500
        ),
501 502 503
    },
)

504

505

506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
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"],
}
529

530

531 532
swagger = Swagger(app, template=swagger_template, config=swagger_config)

533 534 535 536
ns1 = api.namespace("message", description="Message Wrapper (Full Message Schema) Format")
@ns1.route("/wrapper")
class MessageWrapper(Resource):
    @ns1.response(200, "Success", full_message_schema)
537 538 539 540
    def get(self):
        pass


541
ns2 = api.namespace("platform_status", description="platform Status Message Format")
542
@ns2.route("")
543 544
class platformStatus(Resource):
    @ns2.response(200, "Success", platform_status_message_schema)
545 546 547 548 549
    def get(self):
        pass


ns3 = api.namespace(
550
    "planning_configuration", description="Planning Configuration Format. Do we want region of interest to be per squad, per platform or in the main schema?"
551 552 553
)

@ns3.route("")
554 555
class PlanningConfiguration(Resource):
    @ns3.response(200, "Success", planning_configuration_schema)
556 557 558 559
    def get(self):
        pass


560 561 562
# @api.route('/mission-plan/<str:platform_type')
# @api.doc(params={"platform_type": "The type of platform of the mission plan to target."})
ns4 = api.namespace("mission_plan", description="Mission Plan Format Per platform")
563 564 565 566 567 568

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

570
ns5 = api.namespace("observation", description="Observation Format --> Per platform or generic?")
571 572 573 574 575
@ns5.route("")
class Observation(Resource):
    @ns5.response(200, "Success", observation_schema)
    def get(self):
        pass
576 577 578 579 580 581
ns6 = api.namespace("acknowledgement", description="Acknowledgment Format")
@ns6.route("")
class Acknowledgment(Resource):
    @ns6.response(200, "Success", acknowledgement_schema)
    def get(self):
        pass
582 583 584

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