Skip to content

Commit

Permalink
[desktop]: Show error message on perform an action for a disconnected…
Browse files Browse the repository at this point in the history
… device
  • Loading branch information
tiagohm committed Nov 23, 2024
1 parent 1c07944 commit 5c226ec
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 60 deletions.
25 changes: 5 additions & 20 deletions desktop/src/app/atlas/atlas.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ONE_DECIMAL_PLACE_FORMATTER, TWO_DIGITS_FORMATTER } from '../../shared/
import { AngularService } from '../../shared/services/angular.service'
import { ApiService } from '../../shared/services/api.service'
import { BrowserWindowService } from '../../shared/services/browser-window.service'
import { DeviceService } from '../../shared/services/device.service'
import { ElectronService } from '../../shared/services/electron.service'
import { PreferenceService } from '../../shared/services/preference.service'
import { extractDate, extractTime } from '../../shared/types/angular.types'
Expand Down Expand Up @@ -58,6 +59,7 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit,
private readonly route = inject(ActivatedRoute)
private readonly preferenceService = inject(PreferenceService)
private readonly angularService = inject(AngularService)
private readonly deviceService = inject(DeviceService)

protected readonly sun = structuredClone(DEFAULT_SUN)
protected readonly moon = structuredClone(DEFAULT_MOON)
Expand Down Expand Up @@ -538,7 +540,7 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit,
this.minorPlanet.closeApproach.result = await this.api.closeApproachesOfMinorPlanets(this.minorPlanet.closeApproach.days, this.minorPlanet.closeApproach.lunarDistance, this.dateTimeAndLocation.dateTime)

if (!this.minorPlanet.closeApproach.result.length) {
this.angularService.message('No close approaches found for the given days and lunar distance', 'warning')
this.angularService.message('No close approaches found for the given days and lunar distance', 'warn')
}
}

Expand Down Expand Up @@ -974,25 +976,8 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit,
}
}

private async executeMount(action: (mount: Mount) => void | Promise<void>) {
if (await this.angularService.confirm('Are you sure that you want to proceed?')) {
return false
}

private async executeMount(action: (mount: Mount) => void | Promise<void>, showConfirmation: boolean = true) {
const mounts = await this.api.mounts()

if (mounts.length === 1) {
await action(mounts[0])
return true
} else {
const mount = await this.deviceMenu.show(mounts)

if (mount && mount !== 'NONE' && mount.connected) {
await action(mount)
return true
}
}

return false
return this.deviceService.executeAction(this.deviceMenu, mounts, action, showConfirmation)
}
}
40 changes: 4 additions & 36 deletions desktop/src/app/image/image.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { SEPARATOR_MENU_ITEM } from '../../shared/constants'
import { AngularService } from '../../shared/services/angular.service'
import { ApiService } from '../../shared/services/api.service'
import { BrowserWindowService } from '../../shared/services/browser-window.service'
import { DeviceService } from '../../shared/services/device.service'
import { ElectronService } from '../../shared/services/electron.service'
import { PreferenceService } from '../../shared/services/preference.service'
import { EquatorialCoordinateJ2000, filterAstronomicalObject } from '../../shared/types/atlas.types'
Expand Down Expand Up @@ -68,6 +69,7 @@ export class ImageComponent implements AfterViewInit, OnDestroy {
private readonly browserWindowService = inject(BrowserWindowService)
private readonly preferenceService = inject(PreferenceService)
private readonly angularService = inject(AngularService)
private readonly deviceService = inject(DeviceService)

protected readonly preference = structuredClone(DEFAULT_IMAGE_PREFERENCE)
protected readonly solver = structuredClone(DEFAULT_IMAGE_SOLVER_DIALOG)
Expand Down Expand Up @@ -1485,46 +1487,12 @@ export class ImageComponent implements AfterViewInit, OnDestroy {
}

private async executeCamera(action: (camera: Camera) => void | Promise<void>, showConfirmation: boolean = true) {
if (showConfirmation && (await this.angularService.confirm('Are you sure that you want to proceed?'))) {
return false
}

const cameras = await this.api.cameras()

if (cameras.length === 1) {
await action(cameras[0])
return true
} else {
const camera = await this.deviceMenu.show(cameras, undefined, 'CAMERA')

if (camera && camera !== 'NONE' && camera.connected) {
await action(camera)
return true
}
}

return false
return this.deviceService.executeAction(this.deviceMenu, cameras, action, showConfirmation)
}

private async executeMount(action: (mount: Mount) => void | Promise<void>, showConfirmation: boolean = true) {
if (showConfirmation && (await this.angularService.confirm('Are you sure that you want to proceed?'))) {
return false
}

const mounts = await this.api.mounts()

if (mounts.length === 1) {
await action(mounts[0])
return true
} else {
const mount = await this.deviceMenu.show(mounts, undefined, 'MOUNT')

if (mount && mount !== 'NONE' && mount.connected) {
await action(mount)
return true
}
}

return false
return this.deviceService.executeAction(this.deviceMenu, mounts, action, showConfirmation)
}
}
2 changes: 1 addition & 1 deletion desktop/src/app/sequencer/sequencer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class SequencerComponent implements AfterContentInit, OnDestroy, Tickable

private loadPlanFromJson(file: JsonFile<SequencerPlan>) {
if (!this.loadPlan(file.json)) {
this.angularService.message('No sequence found', 'warning')
this.angularService.message('No sequence found', 'warn')
this.add()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class DeviceListMenuComponent {
return new Promise<Undefinable<T | 'NONE'>>((resolve) => {
if (devices.length <= 0) {
resolve(undefined)
this.angularService.message('Please connect your equipment first!', 'warning')
this.angularService.message('No equipment available to perform this action!', 'warn')
return
}

Expand Down
2 changes: 1 addition & 1 deletion desktop/src/shared/interceptors/error.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AngularService } from '../services/angular.service'

export interface ErrorResponse {
message: string
type: 'success' | 'info' | 'warning' | 'error'
type: 'success' | 'info' | 'warn' | 'error'
}

@Injectable({ providedIn: 'root' })
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/shared/services/angular.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class AngularService {
return ConfirmDialogComponent.open(this, message)
}

message(text: string, severity: 'success' | 'info' | 'warning' | 'error' = 'success') {
message(text: string, severity: 'success' | 'info' | 'warn' | 'error' = 'success') {
this.messageService.add({ severity, detail: text, life: 4000 })
}
}
33 changes: 33 additions & 0 deletions desktop/src/shared/services/device.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { inject, Injectable } from '@angular/core'
import { DeviceListMenuComponent } from '../components/device-list-menu/device-list-menu.component'
import { Device } from '../types/device.types'
import { AngularService } from './angular.service'

@Injectable({ providedIn: 'root' })
export class DeviceService {
private readonly angularService = inject(AngularService)

async executeAction<T extends Device>(deviceMenu: DeviceListMenuComponent, devices: T[], action: (device: T) => void | Promise<void>, showConfirmation: boolean = true) {
if (showConfirmation && (await this.angularService.confirm('Are you sure that you want to proceed?'))) {
return false
}

if (!devices.length) {
this.angularService.message('No equipment available to perform this action!', 'warn')
return false
} else {
const device = devices.length === 1 ? devices[0] : await deviceMenu.show(devices, undefined, devices[0].type)

if (device && device !== 'NONE') {
if (device.connected) {
await action(device)
return true
} else {
this.angularService.message('Your equipment must be connected to perform this action', 'error')
}
}
}

return false
}
}

0 comments on commit 5c226ec

Please sign in to comment.