File size: 2,279 Bytes
8cd2954
1d46eeb
8cd2954
1d46eeb
8cd2954
1d46eeb
8cd2954
 
 
 
 
bef2fc1
 
8cd2954
 
 
 
 
 
2037491
11c2884
523ea10
2037491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cd2954
 
2037491
 
 
8cd2954
2037491
 
 
 
 
8cd2954
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
# salesforce_integration.py
from simple_salesforce import Salesforce
import streamlit as st

@st.cache_data(ttl=3600)
def fetch_salesforce_data(site_name):
    try:
        sf = Salesforce(
            username=st.secrets["salesforce"]["username"],
            password=st.secrets["salesforce"]["password"],
            security_token=st.secrets["salesforce"]["security_token"],
            #client_id=st.secrets["salesforce"]["consumer_key"],
            #client_secret=st.secrets["salesforce"]["consumer_secret"]
        )
        query = f"""
        SELECT Id, Name, Solar_Generation__c, Wind_Generation__c, Power_Required__c, Power_Sufficient__c,
               Camera_Status__c, Alert_Level__c, Health_Score__c, LastModifiedDate,
               Site__c, Location_Latitude__c, Location_Longitude__c
        FROM Pole__c
        WHERE Site__c = '{site_name}'
        """
        result = sf.query_all(query)  # Line 19 - IndentationError here
        data = []
        for record in result['records']:
            solar = record['Solar_Generation__c'] or 0.0
            wind = record['Wind_Generation__c'] or 0.0
            total_power = solar + wind
            power_status = 'Sufficient' if record['Power_Sufficient__c'] == 'Yes' else 'Insufficient'

            data.append({
                'Pole ID': record['Name'],
                'Site': record['Site__c'],
                'Latitude': record['Location_Latitude__c'] or 0.0,
                'Longitude': record['Location_Longitude__c'] or 0.0,
                'Solar (kWh)': solar,
                'Wind (kWh)': wind,
                'Power Required (kWh)': record['Power_Required__c'] or 0.0,
                'Total Power (kWh)': total_power,
                'Power Status': power_status,
                'Tilt Angle (°)': 0.0,
                'Vibration (g)': 0.0,
                'Camera Status': record['Camera_Status__c'] or 'Unknown',
                'Health Score': record['Health_Score__c'] or 0.0,
                'Alert Level': record['Alert_Level__c'] or 'Green',
                'Anomalies': '',
                'Last Checked': record['LastModifiedDate'] or ''
            })
        return data
    except Exception as e:
        st.error(f"Error connecting to Salesforce: {str(e)}")
        return []