def code_response(ctrl: Api, resp: Union[ApiResponse, models.BaseModel]): """ Convert amplipi.ctrl.Api responses to json/http responses """ if isinstance(resp, ApiResponse): if resp.code == ApiCode.OK: # general commands return None to indicate success return ctrl.get_state() # TODO: refine error codes based on error message raise HTTPException(404, resp.msg) return resp
def view(request: Request, ctrl: Api = Depends(get_ctrl), src: int = 0): """ Webapp main view """ state = ctrl.get_state() context = { # needed for template to make response 'request': request, # simplified amplipi state 'cur_src': src, 'sources': state.sources, 'zones': state.zones, 'groups': state.groups, 'presets': state.presets, 'inputs': [ctrl.get_inputs(src) for src in state.sources], 'unused_groups': [unused_groups(ctrl, src.id) for src in state.sources if src.id is not None], 'unused_zones': [unused_zones(ctrl, src.id) for src in state.sources if src.id is not None], 'ungrouped_zones': [ungrouped_zones(ctrl, src.id) for src in state.sources if src.id is not None], 'song_info': [src.info for src in state.sources if src.info is not None], # src.info should never be None 'version': state.info.version if state.info else 'unknown', } return templates.TemplateResponse('index.html.j2', context, media_type='text/html')
def get_preset(ctrl: Api = Depends(get_ctrl), pid: int = params.PresetID) -> models.Preset: """ Get Preset with id=**pid** """ _, preset = utils.find(ctrl.get_state().presets, pid) if preset is not None: return preset raise HTTPException(404, f'preset {pid} not found')
def get_presets(ctrl: Api = Depends(get_ctrl)) -> Dict[str, List[models.Preset]]: """ Get all presets """ return {'presets' : ctrl.get_state().presets}
def get_stream(ctrl: Api = Depends(get_ctrl), sid: int = params.StreamID) -> models.Stream: """ Get Stream with id=**sid** """ _, stream = utils.find(ctrl.get_state().streams, sid) if stream is not None: return stream raise HTTPException(404, f'stream {sid} not found')
def get_streams(ctrl: Api = Depends(get_ctrl)) -> Dict[str, List[models.Stream]]: """ Get all streams """ return {'streams' : ctrl.get_state().streams}
def get_group(ctrl: Api = Depends(get_ctrl), gid: int = params.GroupID) -> models.Group: """ Get Group with id=**gid** """ _, grp = utils.find(ctrl.get_state().groups, gid) if grp is not None: return grp raise HTTPException(404, f'group {gid} not found')
def get_groups(ctrl: Api = Depends(get_ctrl)) -> Dict[str, List[models.Group]]: """ Get all groups """ return {'groups' : ctrl.get_state().groups}
def get_zone(ctrl: Api = Depends(get_ctrl), zid: int = params.ZoneID) -> models.Zone: """ Get Zone with id=**zid** """ zones = ctrl.get_state().zones if 0 <= zid < len(zones): return zones[zid] raise HTTPException(404, f'zone {zid} not found')
def get_zones(ctrl: Api = Depends(get_ctrl)) -> Dict[str, List[models.Zone]]: """ Get all zones """ return {'zones': ctrl.get_state().zones}
def get_source(ctrl: Api = Depends(get_ctrl), sid: int = params.SourceID) -> models.Source: """ Get Source with id=**sid** """ # TODO: add get_X capabilities to underlying API? sources = ctrl.get_state().sources return sources[sid]
def get_sources(ctrl: Api = Depends(get_ctrl)) -> Dict[str, List[models.Source]]: """ Get all sources """ return {'sources' : ctrl.get_state().sources}
def reset(ctrl: Api = Depends(get_ctrl)) -> models.Status: """ Reload the current configuration, resetting the firmware in the process. """ ctrl.reinit(settings=ctrl._settings, change_notifier=notify_on_change) return ctrl.get_state()
def load_config(config: models.Status, ctrl: Api = Depends(get_ctrl)) -> models.Status: """ Load a new configuration (and return the configuration loaded). This will overwrite the current configuration so it is advised to save the previous config from. """ ctrl.reinit(settings=ctrl._settings, change_notifier=notify_on_change, config=config) return ctrl.get_state()
def get_status(ctrl: Api = Depends(get_ctrl)) -> models.Status: """ Get the system status and configuration """ return ctrl.get_state()