File size: 2,404 Bytes
d0258ad
 
 
1b1ea02
d0258ad
 
b3f239a
 
d0258ad
9e1f606
d0258ad
 
 
 
 
 
 
c278fa6
b16bd0d
 
3c67dd7
b16bd0d
d0258ad
 
 
 
 
 
 
 
 
 
 
 
 
052519a
d0258ad
 
 
 
 
 
 
052519a
d0258ad
 
f94f139
d0258ad
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import pandas as pd
import simple-salesforce

# ---------------------- CONFIG ----------------------
CLIENT_ID = "3MVG9VMBZCsTL9hnfx2eVMOHa56mwSZnvuAnPr3kVVBEQfeLYvrSfJNRRjjSlKWPLy99XM6kefg=="
CLIENT_SECRET = "6F7E9C95CE20CC07FC1EBD39B34909739D99975A0EEB548240926EA0686E428E"
USERNAME = "[email protected]"
PASSWORD = "Vedavathi@04"+"qe4His8AcuFJucZz5NBHfGU"
TOKEN_URL = "https://login.salesforce.com/services/oauth2/token"
API_VERSION = "v60.0"

# ---------------------- AUTH ----------------------
@st.cache_data
def get_salesforce_token():
    data = {
        "grant_type": "Vedavathi@04",
        "client_id": "3MVG9VMBZCsTL9hnfx2eVMOHa56mwSZnvuAnPr3kVVBEQfeLYvrSfJNRRjjSlKWPLy99XM6kefg==",
        "client_secret": "6F7E9C95CE20CC07FC1EBD39B34909739D99975A0EEB548240926EA0686E428E",
        "username": "[email protected]",
        "password": "Vedavathi@04" + "jqe4His8AcuFJucZz5NBHfGU"
    }
    response = requests.post(TOKEN_URL, data=data)
    if response.status_code != 200:
        st.error("Authentication failed!")
        return None, None
    res = response.json()
    return res["access_token"], res["instance_url"]

# ---------------------- FETCH DATA ----------------------
def fetch_pole_data(instance_url, access_token):
    headers = {
        "Authorization": f"Bearer {access_token}"
    }
    query = "SELECT Name, Location_Latitude__c,	Location_Longitude__c ,Camera_Status__c FROM Pole__c LIMIT 100"
    url = f"{instance_url}/services/data/{API_VERSION}/query?q={query}"
    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        st.error("Failed to fetch Pole data")
        return pd.DataFrame()
    records = response.json().get("records", [])
    df = pd.DataFrame(records)
    return df[[ "Name", "Location_Latitude__c","Location_Longitude__c", "Camera_Status__c"]]

# ---------------------- UI ----------------------
st.title("🚦Vedavathi Smart Poles Viewer")

token, instance_url = get_salesforce_token()

if token:
    if st.button("🔄 Refresh Pole Data"):
        df = fetch_pole_data(instance_url, token)
        if not df.empty:
            st.success("Pole data loaded successfully!")
            st.dataframe(df, use_container_width=True)
        else:
            st.warning("No data found.")
else:
    st.error("Salesforce authentication failed. Check credentials.")