-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
62 lines (51 loc) · 1.67 KB
/
update.py
File metadata and controls
62 lines (51 loc) · 1.67 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
from datetime import datetime
from fastapi import FastAPI
from pymongo import MongoClient
from pydantic import BaseModel
client = MongoClient('mongodb://localhost:27017/')
mydb = client["Light"]
collection = mydb["status"]
app = FastAPI()
class ss(BaseModel):
room : str
status : int
def del1():
for x in collection.find():
print(x)
def i_log1(inn,out):
collection1 = mydb["log1"]
c2 = {"inn": inn , "out": out}
x = collection1.insert_one(c2)
def i_log2(inn,out):
collection2 = mydb["log2"]
c2 = {"inn": inn, "out": out}
x = collection2.insert_one(c2)
def i_log3(inn,out):
collection3 = mydb["log3"]
c2 = {"inn": inn , "out": out}
x = collection3.insert_one(c2)
def update(ms: ss):
nt = datetime.now()
now_time = datetime.timestamp(nt)
if(ms.status == 1):
my_query = {"room":ms.room}
new_query = {"$set": {"status":int(ms.status),"first": now_time,"last":None}}
collection.update_one(my_query,new_query)
if (ms.status == 0):
my_query = {"room": ms.room}
new_query = {"$set": {"status": int(ms.status), "last":now_time}}
collection.update_one(my_query, new_query)
result = collection.find({"room":ms.room})
result = list(result)
if len(result) == 0:
return {
"status": "room not found"
}
x = result[0]
#Insert to log
if (ms.room == 'l1'):
i_log1(x['first'],x['last'])
if (ms.room == 'l2'):
i_log2(x['first'],x['last'])
if (ms.room == 'l3'):
i_log3(x['first'],x['last'])