Spaces:
Sleeping
Sleeping
# salesforce_integration.py | |
from simple_salesforce import Salesforce | |
import streamlit as st | |
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 [] |