add app mode to API
This commit is contained in:
parent
e3817980ec
commit
1540b3e806
|
@ -170,6 +170,9 @@ def build_config(_config_file):
|
||||||
'MAILBOX_FILE': config.get(section, 'MAILBOX_FILE'),
|
'MAILBOX_FILE': config.get(section, 'MAILBOX_FILE'),
|
||||||
'EMERGENCY_SOS_FILE': config.get(section, 'EMERGENCY_SOS_FILE'),
|
'EMERGENCY_SOS_FILE': config.get(section, 'EMERGENCY_SOS_FILE'),
|
||||||
'USER_SETTINGS_FILE': config.get(section, 'USER_SETTINGS_FILE'),
|
'USER_SETTINGS_FILE': config.get(section, 'USER_SETTINGS_FILE'),
|
||||||
|
'USE_API': config.getboolean(section, 'USE_API'),
|
||||||
|
'AUTHORIZED_TOKENS_FILE': config.get(section, 'AUTHORIZED_TOKENS_FILE'),
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
if not CONFIG['LOGGER']['LOG_FILE']:
|
if not CONFIG['LOGGER']['LOG_FILE']:
|
||||||
|
|
|
@ -79,3 +79,20 @@ app_response = {
|
||||||
|
|
||||||
json_object = json.dumps(app_response, indent = 4)
|
json_object = json.dumps(app_response, indent = 4)
|
||||||
print(json_object)
|
print(json_object)
|
||||||
|
|
||||||
|
msg_xfer = {
|
||||||
|
'mode':'raw',
|
||||||
|
'system_name':'ABC',
|
||||||
|
'response_url':'http://localhost:8093/api/',
|
||||||
|
'auth_type':'private',
|
||||||
|
'credentials': {
|
||||||
|
'user':'test_name',
|
||||||
|
'password':'passw0rd',
|
||||||
|
},
|
||||||
|
'data':{
|
||||||
|
1:{'MMDVM Packet 1'},
|
||||||
|
2:{'MMDVM Packet 2'}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -168,8 +168,11 @@ EMERGENCY_SOS_FILE: /tmp/gps_data_user_sos.txt
|
||||||
# User settings file, MUST configure using absolute path.
|
# User settings file, MUST configure using absolute path.
|
||||||
USER_SETTINGS_FILE: /path/to/user_settings.txt
|
USER_SETTINGS_FILE: /path/to/user_settings.txt
|
||||||
|
|
||||||
|
# API settings
|
||||||
# Authorized Apps file - data used for the dashboard API
|
# Authorized Apps file - data used for the dashboard API
|
||||||
|
USE_API: True
|
||||||
AUTHORIZED_APPS_FILE: /path/to/authorized_apps.py
|
AUTHORIZED_APPS_FILE: /path/to/authorized_apps.py
|
||||||
|
AUTHORIZED_TOKENS_FILE: /tmp/hblink_auth_tokens.txt
|
||||||
|
|
||||||
# The following options are used for the dashboard. The dashboard is optional.
|
# The following options are used for the dashboard. The dashboard is optional.
|
||||||
# Title of the Dashboard
|
# Title of the Dashboard
|
||||||
|
|
|
@ -2164,6 +2164,11 @@ if __name__ == '__main__':
|
||||||
emergency_sos_file = CONFIG['GPS_DATA']['EMERGENCY_SOS_FILE']
|
emergency_sos_file = CONFIG['GPS_DATA']['EMERGENCY_SOS_FILE']
|
||||||
# User APRS settings
|
# User APRS settings
|
||||||
user_settings_file = CONFIG['GPS_DATA']['USER_SETTINGS_FILE']
|
user_settings_file = CONFIG['GPS_DATA']['USER_SETTINGS_FILE']
|
||||||
|
|
||||||
|
#API variables
|
||||||
|
auth_token_file = CONFIG['GPS_DATA']['AUTHORIZED_TOKENS_FILE']
|
||||||
|
use_api = CONFIG['GPS_DATA']['USE_API']
|
||||||
|
|
||||||
# Check if user_settings (for APRS settings of users) exists. Creat it if not.
|
# Check if user_settings (for APRS settings of users) exists. Creat it if not.
|
||||||
if Path(user_settings_file).is_file():
|
if Path(user_settings_file).is_file():
|
||||||
pass
|
pass
|
||||||
|
@ -2187,13 +2192,23 @@ if __name__ == '__main__':
|
||||||
with open(bb_file, 'w') as user_bb_file:
|
with open(bb_file, 'w') as user_bb_file:
|
||||||
user_bb_file.write("[]")
|
user_bb_file.write("[]")
|
||||||
user_bb_file.close()
|
user_bb_file.close()
|
||||||
|
#Only create if API enabled
|
||||||
|
if use_api == True:
|
||||||
|
if Path(auth_token_file).is_file():
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
Path(auth_token_file).touch()
|
||||||
|
with open(auth_token_file, 'w') as auth_token:
|
||||||
|
auth_token.write("[]")
|
||||||
|
auth_token.close()
|
||||||
|
|
||||||
if Path(the_mailbox_file).is_file():
|
if Path(the_mailbox_file).is_file():
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
Path(the_mailbox_file).touch()
|
Path(the_mailbox_file).touch()
|
||||||
with open(the_mailbox_file, 'w') as user_loc_file:
|
with open(the_mailbox_file, 'w') as mailbox_file:
|
||||||
user_loc_file.write("[]")
|
mailbox_file.write("[]")
|
||||||
user_loc_file.close()
|
mailbox_file.close()
|
||||||
try:
|
try:
|
||||||
Path('/tmp/.hblink_data_que/').mkdir(parents=True, exist_ok=True)
|
Path('/tmp/.hblink_data_que/').mkdir(parents=True, exist_ok=True)
|
||||||
except:
|
except:
|
||||||
|
@ -2274,7 +2289,7 @@ if __name__ == '__main__':
|
||||||
# Check for outgoing SMS
|
# Check for outgoing SMS
|
||||||
data_que_check()
|
data_que_check()
|
||||||
# APRS beacon and receive
|
# APRS beacon and receive
|
||||||
if aprs_callsign == 'N0CALL':
|
if 'N0CALL' in aprs_callsign:
|
||||||
logger.info('APRS callsighn set to N0CALL, packet not sent.')
|
logger.info('APRS callsighn set to N0CALL, packet not sent.')
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -721,16 +721,15 @@ def mail_rss():
|
||||||
"""
|
"""
|
||||||
return Response(rss_header + post_data + "\n</channel>\n</rss>", mimetype='text/xml')
|
return Response(rss_header + post_data + "\n</channel>\n</rss>", mimetype='text/xml')
|
||||||
|
|
||||||
@app.route('/api/<api_mode>', methods=['POST', 'GET'])
|
@app.route('/api', methods=['GET'])
|
||||||
|
@app.route('/api/<api_mode>', methods=['POST'])
|
||||||
def api(api_mode=None):
|
def api(api_mode=None):
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
print('get')
|
api_content = '<h3 style="text-align: center;"><strong>API Enabled: ' + str(use_api) + '</strong></h3>'
|
||||||
return render_template('generic.html', title = dashboard_title, content = Markup(api_content))
|
return render_template('generic.html', title = dashboard_title, content = Markup(api_content))
|
||||||
else:
|
if use_api == 'True' or use_api == "true":
|
||||||
api_data = request.json
|
api_data = request.json
|
||||||
# Find out type of JSON
|
# Find out mode of JSON
|
||||||
#print(api_data)
|
|
||||||
#print(authorized_users)
|
|
||||||
## try:
|
## try:
|
||||||
# Filter msg_xfer
|
# Filter msg_xfer
|
||||||
if api_data['mode'] == 'msg_xfer':
|
if api_data['mode'] == 'msg_xfer':
|
||||||
|
@ -758,31 +757,55 @@ def api(api_mode=None):
|
||||||
status='Generated SMS',
|
status='Generated SMS',
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return jsonify(
|
message = jsonify(message='Authentication error')
|
||||||
mode=api_data['mode'],
|
return make_response(message, 401)
|
||||||
status='Authentication error',
|
|
||||||
)
|
|
||||||
if api_data['auth_type'] == 'public':
|
if api_data['auth_type'] == 'public':
|
||||||
return jsonify(
|
message = jsonify(message='Not implemented')
|
||||||
mode=api_data['mode'],
|
return make_response(message, 403)
|
||||||
status='Not implemented at this time',
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
return jsonify(
|
message = jsonify(message='Not an authentication method')
|
||||||
mode=api_data['mode'],
|
return make_response(message, 400)
|
||||||
status='Not an authorization method',
|
|
||||||
)
|
|
||||||
if api_data['mode'] == 'app':
|
if api_data['mode'] == 'app':
|
||||||
print('implement')
|
auth_file = ast.literal_eval(os.popen('cat ' + auth_token_file).read())
|
||||||
|
for token in auth_file:
|
||||||
|
if token == api_data['auth_token']:
|
||||||
|
auth_file.remove(api_data['auth_token'])
|
||||||
|
for i in api_data['data'].items():
|
||||||
|
sms_data = i[1]
|
||||||
|
if sms_data['slot'] == 0:
|
||||||
|
send_slot = int(unit_sms_ts) - 1
|
||||||
|
if sms_data['slot'] == 1:
|
||||||
|
send_slot = 0
|
||||||
|
if sms_data['slot'] == 2:
|
||||||
|
send_slot = 1
|
||||||
|
send_sms(False, sms_data['destination_id'], sms_data['source_id'], 0000, 'unit', send_slot, sms_data['message'])
|
||||||
|
new_auth_file = auth_file
|
||||||
|
with open(auth_token_file, 'w') as auth_token:
|
||||||
|
auth_token.write(str(auth_file))
|
||||||
|
auth_token.close()
|
||||||
|
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
mode=api_data['mode'],
|
||||||
|
status='Token accepted, SMS generated',
|
||||||
|
)
|
||||||
|
if token != api_data['auth_token']:
|
||||||
|
message = jsonify(message='Auth token not found')
|
||||||
|
return make_response(message, 401)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
message = jsonify(message='Mode not found')
|
message = jsonify(message='Mode not found')
|
||||||
return make_response(message, 400)
|
return make_response(message, 404)
|
||||||
|
if use_api == "False" or use_api == "false":
|
||||||
|
message = jsonify(message='API is disabled for this server')
|
||||||
|
return make_response(message, 502)
|
||||||
## except Exception as e:
|
## except Exception as e:
|
||||||
## message = jsonify(message='Error:' + str(e))
|
## message = jsonify(message='Error:' + str(e))
|
||||||
## return make_response(message, 400)
|
## return make_response(message, 400)
|
||||||
|
|
||||||
#################### Run App ############################
|
#################### Run App ############################
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
global use_api
|
||||||
arg_parser = argparse.ArgumentParser()
|
arg_parser = argparse.ArgumentParser()
|
||||||
arg_parser.add_argument('-c', '--config', action='store', dest='CONFIG_FILE', help='/full/path/to/config.file (usually gps_data.cfg)')
|
arg_parser.add_argument('-c', '--config', action='store', dest='CONFIG_FILE', help='/full/path/to/config.file (usually gps_data.cfg)')
|
||||||
cli_args = arg_parser.parse_args()
|
cli_args = arg_parser.parse_args()
|
||||||
|
@ -843,6 +866,19 @@ if __name__ == '__main__':
|
||||||
the_mailbox_file = parser.get('GPS_DATA', 'MAILBOX_FILE')
|
the_mailbox_file = parser.get('GPS_DATA', 'MAILBOX_FILE')
|
||||||
user_settings_file = parser.get('GPS_DATA', 'USER_SETTINGS_FILE')
|
user_settings_file = parser.get('GPS_DATA', 'USER_SETTINGS_FILE')
|
||||||
|
|
||||||
|
auth_token_file = parser.get('GPS_DATA', 'AUTHORIZED_TOKENS_FILE')
|
||||||
|
use_api = parser.get('GPS_DATA', 'USE_API')
|
||||||
|
|
||||||
|
#Only create if API enabled
|
||||||
|
if use_api == True:
|
||||||
|
if Path(auth_token_file).is_file():
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
Path(auth_token_file).touch()
|
||||||
|
with open(auth_token_file, 'w') as auth_token:
|
||||||
|
auth_token.write("[]")
|
||||||
|
auth_token.close()
|
||||||
|
|
||||||
# API settings
|
# API settings
|
||||||
#authorized_apps_file = parser.get('GPS_DATA', 'AUTHORIZED_APPS_FILE')
|
#authorized_apps_file = parser.get('GPS_DATA', 'AUTHORIZED_APPS_FILE')
|
||||||
# Default SMS TS for unit calls
|
# Default SMS TS for unit calls
|
||||||
|
|
Loading…
Reference in New Issue