-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoishi.py
More file actions
90 lines (73 loc) · 2.6 KB
/
Copy pathAoishi.py
File metadata and controls
90 lines (73 loc) · 2.6 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
import firebase_admin
from firebase_admin import credentials, firestore
# Path to your service account key JSON file
SERVICE_ACCOUNT_KEY_PATH = "/Users/aoishonthgo/Firestore/powerhouse-62f4d-firebase-adminsdk-fbsvc-3cd70b197a.json"
# Initialize Firebase
cred = credentials.Certificate(SERVICE_ACCOUNT_KEY_PATH)
firebase_admin.initialize_app(cred)
# Initialize Firestore
db = firestore.client()
# Adding or Updating Data in Devices
def add_or_update_device(device_id, name, room, model, connection_type, ip_address):
# Reference to the Devices collection and document
device_ref = db.collection("Devices").document(device_id)
# Device data
device_data = {
"ID": device_id,
"Name": name,
"Room": room,
"Information": {
"Model": model,
"ConnectionType": connection_type,
"IPAddress": ip_address,
},
}
# Add or update the document
device_ref.set(device_data)
print(f"Device {device_id} added/updated successfully!")
# Example usage
add_or_update_device(
"Device1",
"Thermostat",
"Living Room",
"T1000",
"WiFi",
"192.168.1.1"
)
# Adding or Updating Data in Energy
def add_or_update_energy(document_id, total_energy_usage, daily_energy, device_fk):
# Reference to the Energy collection and document
energy_ref = db.collection("Energy").document(document_id)
# Energy data
energy_data = {
"TotalEnergyUsage": total_energy_usage,
"Daily": daily_energy,
"DeviceFK": device_fk, # Reference to a Device document ID
}
# Add or update the document
energy_ref.set(energy_data)
print(f"Energy record {document_id} added/updated successfully!")
# Example usage
add_or_update_energy(
"Energy1", # Unique ID for the energy document
500, # Total energy usage in kWh
50, # Daily energy usage in kWh
"Device1" # Foreign key referencing the Devices table
)
#linking the two tables
def add_or_update_energy(document_id, total_energy_usage, daily_energy, device_fk):
# Check if the device exists
device_ref = db.collection("Devices").document(device_fk)
if not device_ref.get().exists:
print(f"Error: Device {device_fk} does not exist!")
return
# Energy data
energy_data = {
"TotalEnergyUsage": total_energy_usage,
"Daily": daily_energy,
"DeviceFK": device_fk,
}
# Add or update the document
energy_ref = db.collection("Energy").document(document_id)
energy_ref.set(energy_data)
print(f"Energy record {document_id} added/updated successfully!")