File size: 3,965 Bytes
b640433
 
aa7707f
380708b
aa7707f
 
 
b640433
 
 
 
aa7707f
b640433
aa7707f
 
b640433
aa7707f
 
cc4f6fd
 
 
 
 
 
 
 
380708b
 
 
 
 
 
 
 
 
fe76b31
 
 
aa7707f
2fb2469
aa7707f
fe76b31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9d4f34
fe76b31
b640433
30c909d
380708b
aa7707f
380708b
aa7707f
 
380708b
aa7707f
380708b
 
 
 
 
b640433
aa7707f
b640433
 
 
 
aa7707f
b640433
aa7707f
 
b640433
 
aa7707f
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
import streamlit as st
import pandas as pd
import os
from simple_salesforce import Salesforce, SalesforceAuthenticationFailed
from modules.simulator import simulate_data
from modules.filters import apply_filters
from modules.visuals import display_dashboard, display_charts

st.set_page_config(page_title="Vedavathi Smart Pole Monitoring", layout="wide")
st.title("📡 Vedavathi Smart Pole Monitoring - PoC Simulator")

# Sidebar Controls
st.sidebar.header("🛠️ Simulation Controls")
data_source = st.sidebar.radio("Data Source", ["Simulated", "Salesforce"])

simulate_faults = st.sidebar.checkbox("Simulate Random Faults", value=True)
num_poles = st.sidebar.slider("Number of Poles", min_value=5, max_value=50, value=10) if data_source == "Simulated" else None

def connect_to_salesforce():
    try:
        sf = Salesforce(
            username="greenenergy@vedavathi.com",
            password="Vedavathi@04",
            security_token="jqe4His8AcuFJucZz5NBHfGU",
            domain="login"  # "login" for production & developer edition; use "test" for sandbox
        )
        return sf
    except SalesforceAuthenticationFailed as e:
        print("Salesforce authentication failed:", e)
        return None
    except Exception as e:
        print("Unexpected error connecting to Salesforce:", e)
        return None

def fetch_salesforce_data(sf):
    # Fetch Pole data
    pole_query = """
    SELECT Id, Name, Alert_Level__c, Camera_Status__c, Health_Score__c, Location_Latitude__c, Location_Longitude__c, Power_Required__c, Power_Sufficient__c, RFID_Tag__c, Site__c, Solar_Generation__c, Wind_Generation__c
    FROM Pole__c
    LIMIT 48
    """
    poles = sf.query(pole_query)["records"]

    # Fetch SensorData
    sensor_query = """
    SELECT Pole__c, Vibration__c, Tilt__c
    FROM SensorData__c
    WHERE Pole__c != NULL
    """
    sensors = sf.query(sensor_query)["records"]

    # Convert to DataFrames
    poles_df = pd.DataFrame([{
        "Pole ID": p.get("Name"),
        "Pole Salesforce ID": p.get("Id"),
        "Alert Level": p.get("Alert_Level__c"),
        "Camera Status": p.get("Camera_Status__c"),
        "Health Score": p.get("Health_Score__c"),
        "Latitude": p.get("Location_Latitude__c"),
        "Longitude": p.get("Location_Longitude__c"),
        "Power Required (kWh)": p.get("Power_Required__c"),
        "Power Sufficient": p.get("Power_Sufficient__c"),
        "RFID Tag": p.get("RFID_Tag__c"),
        "Site": p.get("Site__c"),
        "Solar Gen (kWh)": p.get("Solar_Generation__c"),
        "Wind Gen (kWh)": p.get("Wind_Generation__c")
    } for p in poles])

    sensors_df = pd.DataFrame([{
        "Pole Salesforce ID": s.get("Pole__c"),
        "Vibration (g)": s.get("Vibration__c"),
        "Tilt (°)": s.get("Tilt__c")
    } for s in sensors])

    # Merge Sensor Data into Pole Data
    df = pd.merge(poles_df, sensors_df, on="Pole Salesforce ID", how="left")

    # Drop internal Salesforce ID after merge
    df.drop(columns=["Pole Salesforce ID"], inplace=True)

    return df

sf = connect_to_salesforce()
if data_source == "Salesforce" and sf:
    try:
        df = fetch_salesforce_data(sf)
        st.success("Fetched data from Salesforce successfully.")
    except Exception as e:
        st.error(f"Failed to fetch data from Salesforce: {e}")
        st.stop()
elif data_source == "Simulated":
    df = simulate_data(num_poles, simulate_faults)
else:
    st.error("Failed to connect to Salesforce.")
    st.stop()

# Filters
st.sidebar.header("📂 Filter Data")
alert_filter = st.sidebar.multiselect("Alert Level", ["Green", "Yellow", "Red"], default=["Green", "Yellow", "Red"])
cam_filter = st.sidebar.selectbox("Camera Status", ["All", "Online", "Offline"], index=0)

filtered_df = apply_filters(df, alert_filter, cam_filter)

# Dashboard + Charts
display_dashboard(df)
st.subheader("📋 Pole Monitoring Table")
st.dataframe(filtered_df, use_container_width=True)
display_charts(df)