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 10, 2024
2 parents 9e45550 + c0335ce commit 403203e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]
### Added
- added clone action to energy storage container in myems-api
### Changed
### Fixed
### Removed
Expand Down
44 changes: 40 additions & 4 deletions myems-api/MyEMS.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"info": {
"_postman_id": "2eb5e36a-120d-48ae-875a-8dbcd1fa898c",
"_postman_id": "bccefcdb-c194-45ed-91ef-2bed14bdf0c8",
"name": "MyEMS",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "10906958"
"_exporter_id": "32357384"
},
"item": [
{
Expand Down Expand Up @@ -5379,7 +5379,7 @@
},
{
"key": "Token",
"value": "b436afafd495b18fbac593c9cef57a5fc82114fbcaaef1bac47f2f94fb2c48cf870a2ab79ec8b37a6cd311c0a51ce0383eb72fe17b16e3b3bf36df9e12257320",
"value": "f426c08c74be8a10152e56e8479f7b10b963c0ff5f101f053d7014cc33db8e9a8e73b00238af26f1cff7378ede46ba2dd33ff35267e43b54f4dadd3217c0acb4",
"description": "Login first to get a valid token"
},
{
Expand All @@ -5391,7 +5391,7 @@
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"name\":\"Beijing Office\", \"rated_capacity\":600.000, \"rated_power\":200.000, \"contact_id\":1, \"cost_center_id\":1, \"description\":\"Classic\"}}"
"raw": "{\"data\":{\"name\":\"Beijing Office\", \"rated_capacity\":600.000, \"rated_power\":200.000, \"contact_id\":1, \"cost_center_id\":1, \"svg_id\":\"1\", \"description\":\"Classic\"}}"
},
"url": {
"raw": "{{base_url}}/energystoragecontainers",
Expand All @@ -5405,6 +5405,42 @@
},
"response": []
},
{
"name": "Clone an Energy Storage Container by ID",
"request": {
"method": "POST",
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"description": "Any admin users' UUID"
},
{
"key": "Token",
"value": "f426c08c74be8a10152e56e8479f7b10b963c0ff5f101f053d7014cc33db8e9a8e73b00238af26f1cff7378ede46ba2dd33ff35267e43b54f4dadd3217c0acb4",
"description": "Login first to get a valid token"
},
{
"key": "API-Key",
"value": "c5ee62be2792ed4a59de1096511934288f4c192363529dafc00b3b35f81f224a5cc44c9aae46ac8966dc52f1ea0039395551bdf3f86aff6bb2b6b032834fc139",
"description": "Create API Key at myems-admin",
"disabled": true
}
],
"url": {
"raw": "{{base_url}}/energystoragecontainers/1/clone",
"host": [
"{{base_url}}"
],
"path": [
"energystoragecontainers",
"1",
"clone"
]
}
},
"response": []
},
{
"name": "Update an Energy Storage Container",
"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 @@ -349,6 +349,8 @@
energystoragecontainer.EnergyStorageContainerScheduleCollection())
api.add_route('/energystoragecontainers/{id_}/schedules/{sid}',
energystoragecontainer.EnergyStorageContainerScheduleItem())
api.add_route('/energystoragecontainers/{id_}/clone',
energystoragecontainer.EnergyStorageContainerClone())

api.add_route('/energystoragepowerstations',
energystoragepowerstation.EnergyStoragePowerStationCollection())
Expand Down
69 changes: 69 additions & 0 deletions myems-api/core/energystoragecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import simplejson as json
from core.useractivity import user_logger, admin_control, access_control, api_key_control
import config
from datetime import datetime, timedelta


class EnergyStorageContainerCollection:
Expand Down Expand Up @@ -2984,3 +2985,71 @@ def on_put(req, resp, id_, sid):
cnx.close()

resp.status = falcon.HTTP_200


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

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

@staticmethod
@user_logger
def on_post(req, resp, id_):
"""Handles POST requests"""
access_control(req)
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_ENERGY_STORAGE_CONTAINER_ID')

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

query = (" SELECT id, name, uuid, "
" rated_capacity, rated_power, contact_id, cost_center_id, svg_id, description "
" FROM tbl_energy_storage_containers "
" WHERE id = %s ")
cursor.execute(query, (id_,))
row = cursor.fetchone()

if row is None:
raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND',
description='API.ENERGY_STORAGE_CONTAINER_NOT_FOUND')
else:
meta_result = {"id": row[0],
"name": row[1],
"uuid": row[2],
"rated_capacity": row[3],
"rated_power": row[4],
"contact_id": row[5],
"cost_center_id": row[6],
"svg_id": row[7],
"description": row[8],}
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6])
if config.utc_offset[0] == '-':
timezone_offset = -timezone_offset
new_name = str.strip(meta_result['name']) + \
(datetime.utcnow() + timedelta(minutes=timezone_offset)).isoformat(sep='-', timespec='seconds')
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, (new_name,
str(uuid.uuid4()),
meta_result['rated_capacity'],
meta_result['rated_power'],
meta_result['contact_id'],
meta_result['cost_center_id'],
meta_result['svg_id'],
meta_result['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 403203e

Please sign in to comment.