-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (96 loc) · 3.87 KB
/
app.py
File metadata and controls
116 lines (96 loc) · 3.87 KB
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
from flask import Flask, request, jsonify
from pymongo import MongoClient
import threading
import time
import os
import random
import openai
from dotenv import load_dotenv
load_dotenv()
# Flask app setup
app = Flask(__name__)
# MongoDB setup
client = MongoClient("mongodb://localhost:27017/")
db = client['askthings_db']
users_collection = db['users']
devices_collection = db['devices']
data_collection = db['device_data']
# OpenAI API Key setup
openai.api_key = os.environ.get("")
# In-memory simulation of IoT devices
connected_devices = {}
# Device simulation function
def simulate_device(device_id):
while device_id in connected_devices:
data = {
"device_id": device_id,
"timestamp": time.time(),
"value": random.uniform(0, 100),
}
data_collection.insert_one(data)
time.sleep(2) # Simulate data every 2 seconds
@app.route('/register', methods=['POST'])
def register_user():
data = request.json
if not data.get('username') or not data.get('password'):
return jsonify({"error": "Username and password required."}), 400
users_collection.insert_one(data)
return jsonify({"message": "User registered successfully."}), 201
@app.route('/add_device', methods=['POST'])
def add_device():
data = request.json
if not data.get('device_name') or not data.get('user_id'):
return jsonify({"error": "Device name and user ID required."}), 400
device_id = str(time.time()) # Simple device ID generation
devices_collection.insert_one({"device_id": device_id, "device_name": data['device_name'], "user_id": data['user_id']})
return jsonify({"device_id": device_id, "message": "Device added successfully."}), 201
@app.route('/start_device', methods=['POST'])
def start_device():
data = request.json
device_id = data.get('device_id')
if not device_id:
return jsonify({"error": "Device ID required."}), 400
if device_id not in connected_devices:
connected_devices[device_id] = True
threading.Thread(target=simulate_device, args=(device_id,), daemon=True).start()
return jsonify({"message": "Device started successfully."}), 200
else:
return jsonify({"error": "Device already running."}), 400
@app.route('/stop_device', methods=['POST'])
def stop_device():
data = request.json
device_id = data.get('device_id')
if not device_id:
return jsonify({"error": "Device ID required."}), 400
if device_id in connected_devices:
connected_devices.pop(device_id)
return jsonify({"message": "Device stopped successfully."}), 200
else:
return jsonify({"error": "Device not running."}), 400
@app.route('/get_data', methods=['GET'])
def get_device_data():
device_id = request.args.get('device_id')
if not device_id:
return jsonify({"error": "Device ID required."}), 400
data = list(data_collection.find({"device_id": device_id}, {"_id": 0}))
return jsonify({"data": data}), 200
@app.route('/chat', methods=['POST'])
def chat_with_device():
data = request.json
device_id = data.get('device_id')
user_prompt = data.get('prompt')
if not device_id or not user_prompt:
return jsonify({"error": "Device ID and prompt required."}), 400
device_data = list(data_collection.find({"device_id": device_id}, {"_id": 0}))[-5:] # Fetch last 5 data points
prompt = f"Device Data: {device_data}\nUser Query: {user_prompt}"
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=150
)
return jsonify({"response": response['choices'][0]['text'].strip()}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)