Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nengyuanzhang committed Jul 13, 2024
2 parents 850a77a + f87fb96 commit eb772a8
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 2 deletions.
43 changes: 41 additions & 2 deletions myems-api/MyEMS.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"info": {
"_postman_id": "68799a4a-5d3a-4405-9fa2-7de1624badc4",
"_postman_id": "6a96e345-3fed-4f92-9803-4004acf2e069",
"name": "MyEMS",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "36323627"
"_exporter_id": "32357384"
},
"item": [
{
Expand Down Expand Up @@ -5441,6 +5441,45 @@
},
"response": []
},
{
"name": "Import Energy Storage Container from JSON",
"request": {
"method": "POST",
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"description": "Any admin users' UUID"
},
{
"key": "Token",
"value": "b8a8ad6135900d3357089492b512f5d295ac5fd51fe8be64eeda4cb1503f26d589cb2412cd58d926844c89eabe3c27baf075f30ae8c7bc35ba6500ea8890ec20",
"description": "Login first to get a valid token"
},
{
"key": "API-Key",
"value": "c5ee62be2792ed4a59de1096511934288f4c192363529dafc00b3b35f81f224a5cc44c9aae46ac8966dc52f1ea0039395551bdf3f86aff6bb2b6b032834fc139",
"description": "Create API Key at myems-admin",
"disabled": true
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\": \"Beijing Office\",\r\n \"uuid\": \"9b963448-1235-4239-b0c9-c71d926cec5f\",\r\n \"rated_capacity\": 600.000,\r\n \"rated_power\": 200.000,\r\n \"contact\": {\r\n \"id\": 1,\r\n \"name\": \"albert\",\r\n \"uuid\": \"076ea179-5a7e-4a48-9fcd-f452f95f5376\"\r\n },\r\n \"cost_center\": {\r\n \"id\": 1,\r\n \"name\": \"动力中心\",\r\n \"uuid\": \"6fc174dc-9c57-4934-9896-9226e696dbf2\"\r\n },\r\n \"svg_id\": {\r\n \"id\": 1,\r\n \"name\": \"高压配电\",\r\n \"uuid\": \"c677d1b4-a482-4aad-9192-380a0d832e5f\"\r\n },\r\n \"description\": \"Classic\",\r\n \"qrcode\": \"energystoragecontainer:9b963448-1235-4239-b0c9-c71d926cec5f\"\r\n}"
},
"url": {
"raw": "{{base_url}}/energystoragecontainers/import",
"host": [
"{{base_url}}"
],
"path": [
"energystoragecontainers",
"import"
]
}
},
"response": []
},
{
"name": "Clone an Energy Storage Container by ID",
"request": {
Expand Down
2 changes: 2 additions & 0 deletions myems-api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@
energystoragecontainer.EnergyStorageContainerClone())
api.add_route('/energystoragecontainers/{id_}/export',
energystoragecontainer.EnergyStorageContainerExport)
api.add_route('/energystoragecontainers/import',
energystoragecontainer.EnergyStorageContainerImport())

api.add_route('/energystoragepowerstations',
energystoragepowerstation.EnergyStoragePowerStationCollection())
Expand Down
147 changes: 147 additions & 0 deletions myems-api/core/energystoragecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3134,3 +3134,150 @@ def on_get(req, resp, id_):
"description": row[8]}

resp.text = json.dumps(meta_result)


class EnergyStorageContainerImport:
@staticmethod
def __init__():
""""Initializes Class"""
pass

@staticmethod
def on_options(req, resp):
resp.status = falcon.HTTP_200

@staticmethod
@user_logger
def on_post(req, resp):
"""Handles POST requests"""
admin_control(req)
try:
raw_json = req.stream.read().decode('utf-8')
except Exception as ex:
raise falcon.HTTPError(status=falcon.HTTP_400,
title='API.BAD_REQUEST',
description='API.FAILED_TO_READ_REQUEST_STREAM')

new_values = json.loads(raw_json)

if 'name' not in new_values.keys() or \
not isinstance(new_values['name'], str) or \
len(str.strip(new_values['name'])) == 0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_ENERGY_STORAGE_CONTAINER_NAME')
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
if config.utc_offset[0] == '-':
timezone_offset = -timezone_offset
name = str.strip(new_values['name']) + \
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')

if 'rated_capacity' not in new_values.keys() or \
not (isinstance(new_values['rated_capacity'], float) or
isinstance(new_values['rated_capacity'], int)) or \
new_values['rated_capacity'] <= 0.0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_RATED_CAPACITY')
rated_capacity = new_values['rated_capacity']

if 'rated_power' not in new_values.keys() or \
not (isinstance(new_values['rated_power'], float) or
isinstance(new_values['rated_power'], int)) or \
new_values['rated_power'] <= 0.0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_RATED_POWER')
rated_power = new_values['rated_power']

if 'contact' not in new_values.keys() or \
'id' not in new_values['contact'].keys() or \
not isinstance(new_values['contact']['id'], int) or \
new_values['contact']['id'] <= 0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_CONTACT_ID')
contact_id = new_values['contact']['id']

if 'cost_center' not in new_values.keys() or \
'id' not in new_values['cost_center'].keys() or \
not isinstance(new_values['cost_center']['id'], int) or \
new_values['cost_center']['id'] <= 0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_COST_CENTER_ID')
cost_center_id = new_values['cost_center']['id']

if 'svg_id' in new_values['svg_id'].keys() and \
isinstance(new_values['svg_id']['id'], int) and \
new_values['svg_id']['id'] > 0:
svg_id = new_values['svg_id']['id']
else:
svg_id = None

if 'description' in new_values.keys() and \
new_values['description'] is not None and \
len(str(new_values['description'])) > 0:
description = str.strip(new_values['description'])
else:
description = None

cnx = mysql.connector.connect(**config.myems_system_db)
cursor = cnx.cursor()

cursor.execute(" SELECT name "
" FROM tbl_energy_storage_containers "
" WHERE name = %s ", (name,))
if cursor.fetchone() is not None:
cursor.close()
cnx.close()
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.ENERGY_STORAGE_CONTAINER_NAME_IS_ALREADY_IN_USE')

cursor.execute(" SELECT name "
" FROM tbl_contacts "
" WHERE id = %s ",
(new_values['contact']['id'],))
row = cursor.fetchone()
if row is None:
cursor.close()
cnx.close()
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
description='API.CONTACT_NOT_FOUND')

cursor.execute(" SELECT name "
" FROM tbl_cost_centers "
" WHERE id = %s ",
(new_values['cost_center']['id'],))
row = cursor.fetchone()
if row is None:
cursor.close()
cnx.close()
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
description='API.COST_CENTER_NOT_FOUND')

if svg_id is not None:
cursor.execute(" SELECT name "
" FROM tbl_svgs "
" WHERE id = %s ",
(svg_id,))
row = cursor.fetchone()
if row is None:
cursor.close()
cnx.close()
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
description='API.SVG_NOT_FOUND')

add_values = (" INSERT INTO tbl_energy_storage_containers "
" (name, uuid, rated_capacity, rated_power, contact_id, cost_center_id, svg_id, description) "
" VALUES (%s, %s, %s, %s, %s, %s, %s, %s) ")
cursor.execute(add_values, (name,
str(uuid.uuid4()),
rated_capacity,
rated_power,
contact_id,
cost_center_id,
svg_id,
description))
new_id = cursor.lastrowid
cnx.commit()
cursor.close()
cnx.close()

resp.status = falcon.HTTP_201
resp.location = '/energystoragecontainers/' + str(new_id)

0 comments on commit eb772a8

Please sign in to comment.