|
| 1 | +from django.conf import settings |
| 2 | +from urllib.parse import urljoin |
| 3 | +import requests |
| 4 | +import logging |
| 5 | + |
| 6 | +from configdb.hardware.models import Instrument, Telescope, Site |
| 7 | +from configdb.hardware.apps import can_submit_to_heroic |
| 8 | + |
| 9 | + |
| 10 | +logger = logging.getLogger() |
| 11 | + |
| 12 | + |
| 13 | +def instrument_status_conversion(state: str): |
| 14 | + ''' Converts instrument state to HEROIC instrument status |
| 15 | + ''' |
| 16 | + if state == 'DISABLED' or state == 'MANUAL': |
| 17 | + return 'UNAVAILABLE' |
| 18 | + elif state == 'SCHEDULABLE': |
| 19 | + return 'SCHEDULABLE' |
| 20 | + else: |
| 21 | + return 'AVAILABLE' |
| 22 | + |
| 23 | + |
| 24 | +def telescope_status_conversion(telescope: Telescope): |
| 25 | + return 'SCHEDULABLE' if telescope.active and telescope.enclosure.active and telescope.enclosure.site.active else 'UNAVAILABLE' |
| 26 | + |
| 27 | + |
| 28 | +def heroic_site_id(site: Site): |
| 29 | + ''' Extract a HEROIC id for the site. |
| 30 | + This concatenates the observatory.site for human readability |
| 31 | + ''' |
| 32 | + return f"{settings.HEROIC_OBSERVATORY}.{site.code}" |
| 33 | + |
| 34 | + |
| 35 | +def heroic_telescope_id(telescope: Telescope): |
| 36 | + ''' Extract a HEROIC id for the telescope |
| 37 | + This concatenates the observatory.site.telescope for human readability |
| 38 | + ''' |
| 39 | + return f"{heroic_site_id(telescope.enclosure.site)}.{telescope.enclosure.code}-{telescope.code}" |
| 40 | + |
| 41 | + |
| 42 | +def heroic_instrument_id(instrument: Instrument): |
| 43 | + ''' Extract a HEROIC id for the instrument |
| 44 | + This concatenates the observatory.site.telescope.instrument |
| 45 | + for human-readability. |
| 46 | + ''' |
| 47 | + return f"{heroic_telescope_id(instrument.telescope)}.{instrument.code}" |
| 48 | + |
| 49 | + |
| 50 | +def heroic_optical_element_groups(instrument: Instrument): |
| 51 | + ''' Puts the optical element groups of an instrument in the format for reporting to HEROIC |
| 52 | + ''' |
| 53 | + optical_element_groups = {} |
| 54 | + for camera in instrument.science_cameras.all(): |
| 55 | + for optical_element_group in camera.optical_element_groups.all(): |
| 56 | + optical_element_groups[optical_element_group.type] = {'options': []} |
| 57 | + if optical_element_group.default: |
| 58 | + optical_element_groups[optical_element_group.type]['default'] = optical_element_group.default.code |
| 59 | + for optical_element in optical_element_group.optical_elements.all(): |
| 60 | + optical_element_groups[optical_element_group.type]['options'].append({ |
| 61 | + 'id': optical_element.code, |
| 62 | + 'name': optical_element.name, |
| 63 | + 'schedulable': optical_element.schedulable |
| 64 | + }) |
| 65 | + return optical_element_groups |
| 66 | + |
| 67 | + |
| 68 | +def heroic_operation_modes(instrument: Instrument): |
| 69 | + ''' Puts the generic mode groups of an instrument in the format for reporting to HEROIC |
| 70 | + ''' |
| 71 | + operation_modes = {} |
| 72 | + for generic_mode_group in instrument.instrument_type.mode_types.all(): |
| 73 | + operation_modes[generic_mode_group.type.id] = {'options': []} |
| 74 | + if generic_mode_group.default: |
| 75 | + operation_modes[generic_mode_group.type.id]['default'] = generic_mode_group.default.code |
| 76 | + for mode in generic_mode_group.modes.all(): |
| 77 | + operation_modes[generic_mode_group.type.id]['options'].append({ |
| 78 | + 'id': mode.code, |
| 79 | + 'name': mode.name, |
| 80 | + 'schedulable': mode.schedulable |
| 81 | + }) |
| 82 | + return operation_modes |
| 83 | + |
| 84 | + |
| 85 | +def instrument_to_heroic_instrument_capabilities(instrument: Instrument): |
| 86 | + ''' Extracts the current instrument capabilities of an instrument to send to HEROIC |
| 87 | + ''' |
| 88 | + capabilities = { |
| 89 | + 'instrument': heroic_instrument_id(instrument), |
| 90 | + 'status': instrument_status_conversion(instrument.state), |
| 91 | + 'optical_element_groups': heroic_optical_element_groups(instrument), |
| 92 | + 'operation_modes': heroic_operation_modes(instrument) |
| 93 | + } |
| 94 | + return capabilities |
| 95 | + |
| 96 | + |
| 97 | +def telescope_to_heroic_telescope_properties(telescope: Telescope): |
| 98 | + ''' Extracts the current telescope properties of a telescope to send to HEROIC |
| 99 | + ''' |
| 100 | + telescope_payload = { |
| 101 | + 'name': f"{telescope.name} - {telescope.enclosure.name}", |
| 102 | + 'site': heroic_site_id(telescope.enclosure.site), |
| 103 | + 'aperture': telescope.aperture, |
| 104 | + 'latitude': telescope.lat, |
| 105 | + 'longitude': telescope.long, |
| 106 | + 'horizon': telescope.horizon, |
| 107 | + 'negative_ha_limit': telescope.ha_limit_neg, |
| 108 | + 'positive_ha_limit': telescope.ha_limit_pos, |
| 109 | + 'zenith_blind_spot': telescope.zenith_blind_spot |
| 110 | + } |
| 111 | + return telescope_payload |
| 112 | + |
| 113 | + |
| 114 | +def send_to_heroic(api_endpoint: str, payload: dict, update: bool = False): |
| 115 | + ''' Function to send data to HEROIC API endpoints |
| 116 | + ''' |
| 117 | + headers = {'Authorization': f'Token {settings.HEROIC_API_TOKEN}'} |
| 118 | + url = urljoin(settings.HEROIC_API_URL, api_endpoint) |
| 119 | + if update: |
| 120 | + response = requests.patch(url, headers=headers, json=payload) |
| 121 | + else: |
| 122 | + response = requests.post(url, headers=headers, json=payload) |
| 123 | + logger.warning(response.json()) |
| 124 | + response.raise_for_status() |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +def create_heroic_instrument(instrument: Instrument): |
| 129 | + ''' Create a new instrument payload and send it to HEROIC |
| 130 | + ''' |
| 131 | + if (instrument.telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES and str(instrument.telescope) not in settings.HEROIC_EXCLUDE_TELESCOPES): |
| 132 | + instrument_payload = { |
| 133 | + 'id': heroic_instrument_id(instrument), |
| 134 | + 'name': f"{instrument.instrument_type.name} - {instrument.code}", |
| 135 | + 'telescope': heroic_telescope_id(instrument.telescope), |
| 136 | + 'available': True |
| 137 | + } |
| 138 | + try: |
| 139 | + send_to_heroic('instruments/', instrument_payload) |
| 140 | + except Exception as e: |
| 141 | + logger.error(f'Failed to create heroic instrument {str(instrument)}: {repr(e)}') |
| 142 | + |
| 143 | + |
| 144 | +def update_heroic_instrument_capabilities(instrument: Instrument): |
| 145 | + ''' Send the current instrument capabilities of an instrument to HEROIC |
| 146 | + if it is not DISABLED and heroic is set up in settings.py |
| 147 | + ''' |
| 148 | + if can_submit_to_heroic() and instrument.state != 'DISABLED' and instrument.telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES and str(instrument.telescope) not in settings.HEROIC_EXCLUDE_TELESCOPES: |
| 149 | + capabilities = instrument_to_heroic_instrument_capabilities(instrument) |
| 150 | + try: |
| 151 | + send_to_heroic('instrument-capabilities/', capabilities) |
| 152 | + except Exception as e: |
| 153 | + logger.error(f'Failed to create heroic instrument {str(instrument)} capability update: {repr(e)}') |
| 154 | + |
| 155 | + |
| 156 | +def create_heroic_telescope(telescope: Telescope): |
| 157 | + ''' Create a new telescope payload and send it to HEROIC |
| 158 | + ''' |
| 159 | + if telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES and str(telescope) not in settings.HEROIC_EXCLUDE_TELESCOPES: |
| 160 | + telescope_payload = telescope_to_heroic_telescope_properties(telescope) |
| 161 | + telescope_payload['id'] = heroic_telescope_id(telescope) |
| 162 | + telescope_payload['status'] = telescope_status_conversion(telescope) |
| 163 | + if telescope_payload['status'] != 'SCHEDULABLE': |
| 164 | + telescope_payload['reason'] = 'Telescope is currently marked as inactive to prevent usage' |
| 165 | + try: |
| 166 | + send_to_heroic('telescopes/', telescope_payload) |
| 167 | + except Exception as e: |
| 168 | + logger.error(f'Failed to create heroic telescope {str(telescope)}: {repr(e)}') |
| 169 | + |
| 170 | + |
| 171 | +def update_heroic_telescope_properties(telescope: Telescope): |
| 172 | + ''' Send updated telescope properties to HEROIC when they change |
| 173 | + ''' |
| 174 | + if telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES and str(telescope) not in settings.HEROIC_EXCLUDE_TELESCOPES: |
| 175 | + telescope_update_payload = telescope_to_heroic_telescope_properties(telescope) |
| 176 | + try: |
| 177 | + send_to_heroic(f'telescopes/{heroic_telescope_id(telescope)}/', telescope_update_payload, update=True) |
| 178 | + except Exception as e: |
| 179 | + logger.error(f'Failed to update heroic telescope {str(telescope)}: {repr(e)}') |
| 180 | + |
| 181 | + |
| 182 | +def site_to_heroic_site_properties(site: Site): |
| 183 | + ''' Extracts the current site properties of a site to send to HEROIC |
| 184 | + ''' |
| 185 | + site_payload = { |
| 186 | + 'name': site.name, |
| 187 | + 'observatory': settings.HEROIC_OBSERVATORY, |
| 188 | + 'elevation': site.elevation, |
| 189 | + 'timezone': site.tz |
| 190 | + } |
| 191 | + return site_payload |
| 192 | + |
| 193 | + |
| 194 | +def create_heroic_site(site: Site): |
| 195 | + ''' Create a new site payload and send it to HEROIC |
| 196 | + ''' |
| 197 | + if site.code not in settings.HEROIC_EXCLUDE_SITES: |
| 198 | + site_payload = site_to_heroic_site_properties(site) |
| 199 | + site_payload['id'] = heroic_site_id(site) |
| 200 | + try: |
| 201 | + send_to_heroic('sites/', site_payload) |
| 202 | + except Exception as e: |
| 203 | + logger.error(f'Failed to create heroic site {str(site)}: {repr(e)}') |
| 204 | + |
| 205 | + |
| 206 | +def update_heroic_site(site: Site): |
| 207 | + ''' Send updated site properties to HEROIC when they change |
| 208 | + ''' |
| 209 | + if site.code not in settings.HEROIC_EXCLUDE_SITES: |
| 210 | + site_payload = site_to_heroic_site_properties(site) |
| 211 | + try: |
| 212 | + send_to_heroic(f'sites/{heroic_site_id(site)}/', site_payload, update=True) |
| 213 | + except Exception as e: |
| 214 | + logger.error(f'Failed to update heroic site {str(site)}: {repr(e)}') |
0 commit comments