-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_api.py
87 lines (74 loc) · 4.72 KB
/
platform_api.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
from fastapi import FastAPI, HTTPException, Request
from pydantic import BaseModel
from google.oauth2 import service_account
from googleapiclient.discovery import build
from datetime import datetime
from typing import List, Union
import json
app = FastAPI()
# Google Sheets API configuration
SERVICE_ACCOUNT_FILE = '''{
"type": "service_account",
"project_id": "datapopulation-441708",
"private_key_id": "7d179783c45feed6a793ef12a8aafa18bda2d953",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCi099bzOAbhmpw\n+UPIA2k4MC/EF3o88zLS2RFWWBC0RMpk59Bnq9DmzRcCuBM1+FfDOT05xDP9QnvN\nHnBEAydEK9BYQCFg+xKcbujETfBg3pjZOg/edRxTgw9nn5eS+2IurSlERGlhn4BH\nX5tOCv7AoM73If1QPqqFsTWA8xjiyrLvUHSr5krVrrnMPwXKlWYR9RTEPF7BNGf2\nZ/6rzsHanYl2aa3aJL8awmHLhOZCdVy7FbpCH4gU+Z69DcHvX9H45vUYvnUjRVJL\nB7EgksCG4DfiWwI976rfpQDJdWCZaoFAyp0RwZLc3WafVrACafFuxyJ0N9cH6qMT\nqu6zVaiXAgMBAAECggEAE1KpaForzkMT+/bYwF0HQ4+/Bg+d21XRFjcvA4bNP1da\niBb/zbhqFg8InV39iaNrEX6IHC7Yge7ohN2z3POy2Tg0hVnhnCyvd2OsuSm2o7Lx\ng0s3TuL+9Y98Ey6xQilq7AxzSpKudCiwLx19b17b/z1rfOXKYJd54lu2nxLk78+F\nd8d3O/x3eW7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RIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "118265818436158453765",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/suryaprakash-vss%40datapopulation-441708.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}'''
SPREADSHEET_ID = '10mXRRhQ0MWphsSTdCKgBFOpQYIUZd5wwSCfHRxsc9Lo'
RANGE_NAME = 'sensor_data!A2:D' # Adjusted to allow dynamic column range
credentials_info = json.loads(SERVICE_ACCOUNT_FILE)
# Google Sheets API credentials
credentials = service_account.Credentials.from_service_account_file(
credentials_info,
scopes=["https://www.googleapis.com/auth/spreadsheets"]
)
service = build('sheets', 'v4', credentials=credentials)
sheet = service.spreadsheets()
# Define the data model for the incoming JSON structure
class SensorDataIncoming(BaseModel):
cmd: str
seqno: int = None
EUI: str
ts: int = None
fcnt: int = None
data: str
rssi: float = None
snr: float = None
protocol: str
# Define the data model for Google Sheets (subset of SensorDataIncoming)
class SensorData(BaseModel):
EUI: str
ts: int
data: str
@app.post("/Sensor-data/")
async def post_data(data: Union[List[SensorDataIncoming], SensorDataIncoming]):
try:
# Convert single object input to a list
if isinstance(data, SensorDataIncoming):
data = [data]
# Prepare data to insert into Google Sheets
values = []
for entry in data:
# Convert ts to a readable timestamp in UTC
timestamp = datetime.utcfromtimestamp(entry.ts / 1000).strftime("%m-%d-%Y %H:%M:%S") if entry.ts else ""
sensor_data = SensorData(EUI=entry.EUI, ts=entry.ts or 0, data=entry.data)
values.append([sensor_data.EUI, sensor_data.ts, sensor_data.data, timestamp])
# Prepare body for API request
body = {'values': values}
# Append data to Google Sheets
sheet.values().append(
spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME,
valueInputOption="RAW",
body=body
).execute()
return {"status": "Data successfully added to Google Sheet", "code": 201}
except Exception as e:
# Raise a 500 error with detailed information
raise HTTPException(status_code=500, detail=f"Failed to add data: {str(e)}")