|
| 1 | +"""Location management section for the Data Platform Toolbox""" |
| 2 | + |
| 3 | +import streamlit as st |
| 4 | +import json |
| 5 | +from dp_sdk.ocf import dp |
| 6 | +from grpclib.exceptions import GRPCError |
| 7 | + |
| 8 | + |
| 9 | +async def locations_section(data_client): |
| 10 | + """Location management section.""" |
| 11 | + |
| 12 | + # Energy source and location type mappings |
| 13 | + ENERGY_SOURCES = { |
| 14 | + "UNSPECIFIED": dp.EnergySource.UNSPECIFIED, |
| 15 | + "SOLAR": dp.EnergySource.SOLAR, |
| 16 | + "WIND": dp.EnergySource.WIND, |
| 17 | + } |
| 18 | + |
| 19 | + LOCATION_TYPES = { |
| 20 | + "UNSPECIFIED": dp.LocationType.UNSPECIFIED, |
| 21 | + "SITE": dp.LocationType.SITE, |
| 22 | + "GSP": dp.LocationType.GSP, |
| 23 | + "DNO": dp.LocationType.DNO, |
| 24 | + "NATION": dp.LocationType.NATION, |
| 25 | + "STATE": dp.LocationType.STATE, |
| 26 | + "COUNTY": dp.LocationType.COUNTY, |
| 27 | + "CITY": dp.LocationType.CITY, |
| 28 | + "PRIMARY SUBSTATION": dp.LocationType.PRIMARY_SUBSTATION, |
| 29 | + } |
| 30 | + |
| 31 | + # List Locations |
| 32 | + st.markdown( |
| 33 | + '<h2 style="color:#63BCAF;font-size:32px;">List Locations</h2>', |
| 34 | + unsafe_allow_html=True, |
| 35 | + ) |
| 36 | + with st.expander("Filter options"): |
| 37 | + energy_source_filter = st.selectbox( |
| 38 | + "Energy Source", list(ENERGY_SOURCES.keys()), key="list_loc_energy" |
| 39 | + ) |
| 40 | + location_type_filter = st.selectbox( |
| 41 | + "Location Type", list(LOCATION_TYPES.keys()), key="list_loc_type" |
| 42 | + ) |
| 43 | + user_filter = st.text_input( |
| 44 | + "Filter by User OAuth ID (optional)", |
| 45 | + key="list_loc_user", |
| 46 | + help="Leave empty to show all locations", |
| 47 | + ) |
| 48 | + if st.button("List Locations", key="list_locations_button"): |
| 49 | + if not data_client: |
| 50 | + st.error("❌ Could not connect to Data Platform") |
| 51 | + else: |
| 52 | + try: |
| 53 | + request = dp.ListLocationsRequest() |
| 54 | + if energy_source_filter != "UNSPECIFIED": |
| 55 | + request.energy_source_filter = ENERGY_SOURCES[energy_source_filter] |
| 56 | + if location_type_filter != "UNSPECIFIED": |
| 57 | + request.location_type_filter = LOCATION_TYPES[location_type_filter] |
| 58 | + if user_filter: |
| 59 | + request.user_oauth_id_filter = user_filter |
| 60 | + |
| 61 | + response = await data_client.list_locations(request) |
| 62 | + locations = response.locations |
| 63 | + |
| 64 | + if locations: |
| 65 | + st.success(f"✅ Found {len(locations)} location(s)") |
| 66 | + loc_dicts = [loc.to_dict() for loc in locations] |
| 67 | + st.write(loc_dicts) |
| 68 | + else: |
| 69 | + st.info("No locations found with the specified filters") |
| 70 | + except GRPCError as e: |
| 71 | + st.error( |
| 72 | + f"❌ gRPC Error: {e.message}" |
| 73 | + ) |
| 74 | + except Exception as e: |
| 75 | + st.error(f"❌ Error listing locations: {str(e)}") |
| 76 | + |
| 77 | + # Get Location Details |
| 78 | + st.markdown( |
| 79 | + '<h2 style="color:#63BCAF;font-size: 32px;">Get Location Details</h2>', |
| 80 | + unsafe_allow_html=True, |
| 81 | + ) |
| 82 | + loc_uuid = st.text_input("Location UUID", key="get_loc_uuid") |
| 83 | + loc_energy = st.selectbox( |
| 84 | + # to avoid defaulting to UNSPECIFIED |
| 85 | + "Energy Source", list(ENERGY_SOURCES.keys())[1:], key="get_loc_energy" |
| 86 | + ) |
| 87 | + include_geometry = st.checkbox("Include Geometry", key="get_loc_geom") |
| 88 | + if st.button("Get Location Details", key="get_location_button"): |
| 89 | + if not loc_uuid.strip(): |
| 90 | + st.warning("⚠️ Please enter a location UUID") |
| 91 | + elif not data_client: |
| 92 | + st.error("❌ Could not connect to Data Platform") |
| 93 | + else: |
| 94 | + try: |
| 95 | + response = await data_client.get_location( |
| 96 | + dp.GetLocationRequest( |
| 97 | + location_uuid=loc_uuid, |
| 98 | + energy_source=ENERGY_SOURCES[loc_energy], |
| 99 | + include_geometry=include_geometry, |
| 100 | + ) |
| 101 | + ) |
| 102 | + response_dict = response.to_dict() |
| 103 | + st.success(f"✅ Found location: {loc_uuid}") |
| 104 | + st.write(response_dict) |
| 105 | + except GRPCError as e: |
| 106 | + st.error( |
| 107 | + f"❌ gRPC Error: {e.message}" |
| 108 | + ) |
| 109 | + except Exception as e: |
| 110 | + st.error(f"❌ Error fetching location: {str(e)}") |
| 111 | + |
| 112 | + # Create Location |
| 113 | + st.markdown( |
| 114 | + '<h2 style="color:#7bcdf3;font-size:32px;">Create Location</h2>', |
| 115 | + unsafe_allow_html=True, |
| 116 | + ) |
| 117 | + with st.expander("Create new location"): |
| 118 | + loc_name = st.text_input("Location Name *", key="create_loc_name") |
| 119 | + loc_energy_src = st.selectbox( |
| 120 | + # to avoid defaulting to UNSPECIFIED |
| 121 | + "Energy Source *", list(ENERGY_SOURCES.keys())[1:], key="create_loc_energy" |
| 122 | + ) |
| 123 | + loc_type = st.selectbox( |
| 124 | + # to avoid defaulting to UNSPECIFIED |
| 125 | + "Location Type *", list(LOCATION_TYPES.keys())[1:], key="create_loc_type" |
| 126 | + ) |
| 127 | + geometry_wkt = st.text_input( |
| 128 | + "Geometry (WKT) *", |
| 129 | + placeholder="POINT(-0.127 51.507)", |
| 130 | + key="create_loc_geom", |
| 131 | + help="Enter location geometry in WKT format (e.g., POINT(lon lat))", |
| 132 | + ) |
| 133 | + capacity_watts = st.number_input( |
| 134 | + "Effective Capacity (Watts) *", |
| 135 | + min_value=0, |
| 136 | + key="create_loc_cap", |
| 137 | + help="Enter the effective capacity in watts", |
| 138 | + ) |
| 139 | + loc_metadata = st.text_area( |
| 140 | + "Metadata (JSON)", |
| 141 | + value="{}", |
| 142 | + key="create_loc_metadata", |
| 143 | + help="Enter valid JSON for location metadata", |
| 144 | + ) |
| 145 | + |
| 146 | + if st.button("Create Location", key="create_location_button"): |
| 147 | + if not data_client: |
| 148 | + st.error("❌ Could not connect to Data Platform") |
| 149 | + elif ( |
| 150 | + not loc_name.strip() or not geometry_wkt.strip() or capacity_watts <= 0 |
| 151 | + ): |
| 152 | + st.warning("⚠️ Please fill in all required fields (*)") |
| 153 | + else: |
| 154 | + try: |
| 155 | + # Parse metadata JSON |
| 156 | + metadata = json.loads(loc_metadata) if loc_metadata.strip() else {} |
| 157 | + response = await data_client.create_location( |
| 158 | + dp.CreateLocationRequest( |
| 159 | + location_name=loc_name, |
| 160 | + energy_source=ENERGY_SOURCES.get(loc_energy_src, 1), |
| 161 | + location_type=LOCATION_TYPES.get(loc_type, 1), |
| 162 | + geometry_wkt=geometry_wkt, |
| 163 | + effective_capacity_watts=int(capacity_watts), |
| 164 | + metadata=metadata, |
| 165 | + ) |
| 166 | + ) |
| 167 | + response_dict = response.to_dict() |
| 168 | + st.success(f"✅ Location '{loc_name}' created successfully!") |
| 169 | + st.write(response_dict) |
| 170 | + |
| 171 | + except json.JSONDecodeError: |
| 172 | + st.error("❌ Invalid JSON in metadata field") |
| 173 | + except GRPCError as e: |
| 174 | + st.error( |
| 175 | + f"❌ gRPC Error: {e.message}" |
| 176 | + ) |
| 177 | + except Exception as e: |
| 178 | + st.error(f"❌ Error creating location: {str(e)}") |
0 commit comments