Skip to content

Commit

Permalink
changed send command from GET action to PUT action in myems-api
Browse files Browse the repository at this point in the history
  • Loading branch information
nengyuanzhang committed May 26, 2023
1 parent c6ce9d7 commit 0586455
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
64 changes: 34 additions & 30 deletions myems-api/MyEMS.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1016,36 +1016,6 @@
},
"response": []
},
{
"name": "GET Send a Command by ID",
"request": {
"method": "GET",
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "default"
},
{
"key": "Token",
"value": "aaaa27328292c4f5b6367a6d0f4db5c627a1231877f23cf80659956ddf96ad89ff06bfa804feff04cf3dafebab8ae165b23a887264a8ade69d689b7df8526c4",
"type": "default"
}
],
"url": {
"raw": "{{base_url}}/commands/1/send",
"host": [
"{{base_url}}"
],
"path": [
"commands",
"1",
"send"
]
}
},
"response": []
},
{
"name": "POST Create New Command (without set_value)",
"request": {
Expand Down Expand Up @@ -1219,6 +1189,40 @@
}
},
"response": []
},
{
"name": "PUT Send a Command by ID",
"request": {
"method": "PUT",
"header": [
{
"key": "User-UUID",
"value": "dcdb67d1-6116-4987-916f-6fc6cf2bc0e4",
"type": "default"
},
{
"key": "Token",
"value": "49338f0c8b985ac7bf9ee8d53c2bdf1d51d0ab98da6835bd6b8a814e1f703e308a703e9273e6376532f7f03e11bb92dcbe24f7fc99ef3c9b0d57c608b5f7ff04",
"type": "default"
}
],
"body": {
"mode": "raw",
"raw": "{\"data\":{\"set_value\": 2.0}}"
},
"url": {
"raw": "{{base_url}}/commands/1/send",
"host": [
"{{base_url}}"
],
"path": [
"commands",
"1",
"send"
]
}
},
"response": []
}
]
},
Expand Down
37 changes: 27 additions & 10 deletions myems-api/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,14 @@ def on_put(req, resp, id_):
description='API.INVALID_PAYLOAD')
payload = str.strip(new_values['data']['payload'])

if 'set_value' in new_values['data'].keys() and \
not (isinstance(new_values['data']['set_value'], float) or
isinstance(new_values['data']['set_value'], int)):
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_SET_VALUE')
if 'set_value' not in new_values['data'].keys():
set_value = None
elif isinstance(new_values['data']['set_value'], float) or \
isinstance(new_values['data']['set_value'], int):
set_value = float(new_values['data']['set_value'])
else:
set_value = None
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_SET_VALUE')

if 'description' in new_values['data'].keys() and \
new_values['data']['description'] is not None and \
Expand Down Expand Up @@ -303,18 +303,36 @@ def on_options(req, resp, id_):
resp.status = falcon.HTTP_200

@staticmethod
def on_get(req, resp, id_):
def on_put(req, resp, id_):
"""Handles GET requests"""
access_control(req)
# Get command by ID
if not id_.isdigit() or int(id_) <= 0:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_COMMAND_ID')

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 'set_value' not in new_values['data'].keys():
set_value = None
elif isinstance(new_values['data']['set_value'], float) or \
isinstance(new_values['data']['set_value'], int):
set_value = float(new_values['data']['set_value'])
else:
raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST',
description='API.INVALID_SET_VALUE')

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

query = (" SELECT id, name, uuid, topic, payload, set_value, description "
query = (" SELECT id, name, uuid, topic, payload, set_value "
" FROM tbl_commands "
" WHERE id = %s ")
cursor.execute(query, (id_,))
Expand All @@ -331,8 +349,7 @@ def on_get(req, resp, id_):
"uuid": row[2],
"topic": row[3],
"payload": row[4],
"set_value": row[5],
"description": row[6]}
"set_value": set_value if set_value else row[5]}

mqc = None
try:
Expand Down

0 comments on commit 0586455

Please sign in to comment.