-
Notifications
You must be signed in to change notification settings - Fork 0
/
locationTrackerServer.py
139 lines (96 loc) · 3.86 KB
/
locationTrackerServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
from flask import Flask, request, jsonify
from flask_mqtt import Mqtt
import random
import time
import json
app = Flask(__name__)
app.config['MQTT_BROKER_URL'] = 'broker.emqx.io'
app.config['MQTT_BROKER_PORT'] = 1883
# Set this item when you need to verify username and password
app.config['MQTT_USERNAME'] = ''
# Set this item when you need to verify username and password
app.config['MQTT_PASSWORD'] = ''
app.config['MQTT_KEEPALIVE'] = 5 # Set KeepAlive time in seconds
# If your broker supports TLS, set it True
app.config['MQTT_TLS_ENABLED'] = False
topic_ontrip = '/crrtripdetails'
# Generate a Client ID with the publish prefix.
client_id = f'publish-{random.randint(0, 1000)}'
mqtt_client = Mqtt(app)
# Sample user data
users = [
{'email': '[email protected]', 'password': 'password1'},
{'email': '[email protected]', 'password': 'password2'},
{'email': '[email protected]', 'password': 'password3'},
{'email': '[email protected]', 'password': 'password4'},
{'email': '[email protected]', 'password': 'password5'}
]
# on connect to broker
@mqtt_client.on_connect()
def handle_connect(client, userdata, flags, rc):
if rc == 0:
print('Connected successfully')
mqtt_client.subscribe(topic_ontrip) # subscribe topic
mqtt_client.publish('/hello', 'I am Cooray')
else:
print('Bad connection. Code:', rc)
# handle mqtt incoming messages
@mqtt_client.on_message()
def handle_mqtt_message(client, userdata, message):
data = dict(
topic=message.topic,
payload=message.payload.decode()
)
# print(
# 'Received message on topic: {topic} with payload: {payload}'.format(**data))
if message.topic == topic_ontrip:
# on trip details
data_dict = json.loads(message.payload.decode())
# Access individual values
cr_uemail = data_dict['uemail']
traveled_meters = data_dict['traveledmeters']
cr_relapsed_time_seconds = data_dict['crrelapsedtimeseconds']
print("Current user :", cr_uemail)
print("Traveled Meters:", traveled_meters)
print("CR Relapsed Time Seconds:", cr_relapsed_time_seconds)
# app route defulat
@app.route('/')
def hello_world():
return 'Hello, Fiteness Tracker Backend on'
# test publish
@app.route('/publish', methods=['POST'])
def publish_message():
request_data = request.get_json()
publish_result = mqtt_client.publish(
request_data['topic'], request_data['msg'])
return jsonify({'code': publish_result[0]})
# user login app route
@app.route('/login_user', methods=['POST'])
def login_user():
data = request.get_json()
if not data or 'email' not in data or 'password' not in data:
return jsonify({'message': 'Email and password are required.', 'status': 400}), 400
email = data['email']
password = data['password']
user = next((user for user in users if user['email'] == email), None)
if user and user['password'] == password:
return jsonify({'message': 'Login successful', 'status': 200}), 200
else:
return jsonify({'message': 'Invalid email or password try again', 'status': 401}), 401
# user results
@app.route('/get_results', methods=['POST'])
def get_results():
data = request.get_json()
if not data or 'email' not in data:
return jsonify({'message': 'Email are required.', 'status': 400}), 400
email = data['email']
# get asking user data
# 1 distance should be in meters
# 2 calories hould be number
# 3 time should send as seconds
sendRet = [1200, 500, 67000, 'Explore the scenic beauty of the countryside with a weekend getaway',
'Delve into the world of classic literature with a novel by Jane Austen',
'Embark on a culinary adventure and try your hand at cooking Thai cuisine',]
return jsonify({'message': sendRet, 'status': 200}), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)