from simple_salesforce import Salesforce # Salesforce Authentication def authenticate_salesforce(): return Salesforce(username='greenenergy@vedavathi.com', password='Vedavathi@04', security_token='jqe4His8AcuFJucZz5NBHfGU') # Fetch data from Salesforce custom objects (Pole, Sensor Data, Alerts) def fetch_salesforce_data(site_name): sf = authenticate_salesforce() # Salesforce SOQL Query to get pole data based on the site 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) data = [] for record in result['records']: # Calculate total power solar = record['Solar_Generation__c'] or 0.0 wind = record['Wind_Generation__c'] or 0.0 total_power = solar + wind # Map Power_Sufficient__c to Power Status 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, # Missing in Salesforce, set default (or simulate in simulate_pole) 'Vibration (g)': 0.0, # Missing in Salesforce, set default (or simulate) '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': '', # Missing in Salesforce 'Last Checked': record['LastModifiedDate'] or '' }) return data except Exception as e: st.error(f"Error connecting to Salesforce: {str(e)}") return []