-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/current target #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { procedure } from '../api'; | ||
| import { nina } from '../nina'; | ||
|
|
||
| export default procedure.GET.query(async () => await nina.getLiveStatus()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import { NINA_BASE_URL, UPDATE_THRESHOLD_COUNT } from './variables'; | ||
|
|
||
| interface NinaResponse<T> { | ||
| Response: T; | ||
| Success: boolean; | ||
| Error: string; | ||
| } | ||
|
|
||
| interface ImageHistoryItem { | ||
| ExposureTime: number; | ||
| Temperature: number; | ||
| CameraName: string; | ||
| TargetName: string; | ||
| Gain: number; | ||
| Date: string; | ||
| TelescopeName: string; | ||
| FocalLength: number; | ||
| } | ||
|
|
||
| interface MountInfo { | ||
| RightAscensionString: string; | ||
| DeclinationString: string; | ||
| } | ||
|
|
||
| interface SequenceItem { | ||
| Status: string; | ||
| Items?: SequenceItem[]; | ||
| Name?: string; | ||
| Triggers?: SequenceItem[]; | ||
| } | ||
|
|
||
| export type LiveStatus = { | ||
| active: boolean; | ||
| imageInfo?: ImageHistoryItem; | ||
| mountInfo?: MountInfo; | ||
| currentAction?: string; | ||
| }; | ||
|
|
||
| export class NinaClient { | ||
| private baseUrl: string; | ||
| private updateThreshold: number; | ||
| private lastUpdate: number = 0; | ||
|
|
||
| private cachedLiveStatus: LiveStatus | undefined; | ||
|
|
||
| private cachedLiveImage: Buffer | undefined; | ||
|
|
||
| constructor() { | ||
| this.baseUrl = NINA_BASE_URL ?? 'http://10.10.10.211:1888/'; | ||
| this.updateThreshold = parseInt(UPDATE_THRESHOLD_COUNT || '30'); // Default 30 seconds | ||
| } | ||
|
|
||
| private async fetch<T>(endpoint: string): Promise<T | null> { | ||
| try { | ||
| const res = await fetch(`${this.baseUrl}/v2/${endpoint}`); | ||
| if (!res.ok) return null; | ||
| return await res.json(); | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(`Error fetching ${endpoint}:`, e); | ||
| return null; | ||
patrick11514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private mapRunningAction(name: string): string { | ||
| const map: Record<string, string> = { | ||
| 'Meridian Flip_Trigger': 'Meridian flip', | ||
| 'Smart Exposure': 'Exposing', | ||
| 'Slew and center': 'Slewing', | ||
| 'Start Guiding': 'Start Guiding', | ||
| 'Center After Drift_Trigger': 'Slewing', | ||
| 'Restore Guiding_Trigger': 'Start Guiding', | ||
| 'AF After HFR Increase_Trigger': 'Focusing', | ||
| 'Cool Camera': 'Cooling Camera', | ||
| 'Wait for Time': 'Waiting', | ||
| 'Wait for Time Span': 'Waiting' | ||
| }; | ||
|
|
||
| return map[name] || name; | ||
| } | ||
|
|
||
| // Helper to actually implement the recursive search properly aligned with logic | ||
| private getDeepestRunningName(items: SequenceItem[]): string | undefined { | ||
| for (const item of items) { | ||
| // Check if this item is RUNNING | ||
| if (item.Status === 'RUNNING') { | ||
| // 1. Check Triggers for this item | ||
| if (item.Triggers && Array.isArray(item.Triggers)) { | ||
| const runningTrigger = item.Triggers.find((t) => t.Status === 'RUNNING'); | ||
| if (runningTrigger) return runningTrigger.Name; | ||
| } | ||
|
|
||
| // 2. Check Children Items | ||
| if (item.Items && item.Items.length > 0) { | ||
| const deepName = this.getDeepestRunningName(item.Items); | ||
| if (deepName) return deepName; | ||
| } | ||
|
|
||
| // 3. If no children/triggers running, this is the deepest running item | ||
| return item.Name; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Re-write of isSequenceRunning to use getDeepestRunningName logic or keep simple? | ||
| // We need both active check AND name extraction. | ||
|
|
||
| async getLiveStatus() { | ||
| const now = Date.now(); | ||
|
|
||
| // Check threshold with time (seconds * 1000) | ||
| // Actually prompt said updateThreshold is in seconds. | ||
| const timeDiff = (now - this.lastUpdate) / 1000; | ||
|
|
||
| if (this.cachedLiveStatus && timeDiff < this.updateThreshold) { | ||
| return this.cachedLiveStatus; | ||
| } | ||
|
|
||
| // Always check sequence status first | ||
| const sequenceData = | ||
| await this.fetch<NinaResponse<SequenceItem[]>>('api/sequence/json'); | ||
|
|
||
| const deepestName = | ||
| sequenceData?.Success && sequenceData.Response | ||
| ? this.getDeepestRunningName(sequenceData.Response) | ||
| : undefined; | ||
| const isRunning = !!deepestName; | ||
|
|
||
| if (!isRunning) { | ||
| this.cachedLiveStatus = { active: false }; | ||
patrick11514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.cachedLiveImage = undefined; | ||
| return this.cachedLiveStatus; | ||
| } | ||
|
|
||
| // Sequence is running | ||
| const mountData = await this.fetch<NinaResponse<MountInfo>>( | ||
| 'api/equipment/mount/info' | ||
| ); | ||
|
|
||
| let imageInfo = this.cachedLiveStatus?.imageInfo; | ||
|
|
||
| // Update Image Info only if threshold reached or not cached | ||
| // NOTE: This logic assumes 'getLiveStatus' is called ~every 30s. | ||
| // If we rely on timeDiff for image update: | ||
| if (!imageInfo || timeDiff >= this.updateThreshold) { | ||
| const imageCount = await this.fetch<NinaResponse<number>>( | ||
| 'api/image-history?count=true' | ||
| ); | ||
| if (imageCount?.Success) { | ||
| const imageData = await this.fetch<NinaResponse<ImageHistoryItem[]>>( | ||
| 'api/image-history?index=' + (imageCount.Response - 1) | ||
| ); | ||
| if (imageData?.Success && imageData.Response?.length > 0) { | ||
| imageInfo = imageData.Response[0]; | ||
| } | ||
| } | ||
|
|
||
| // Update Buffer | ||
| this.updateImageBuffer(); | ||
patrick11514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| this.lastUpdate = now; | ||
| } | ||
|
|
||
| this.cachedLiveStatus = { | ||
| active: true, | ||
| mountInfo: mountData?.Response, | ||
| imageInfo, | ||
| currentAction: this.mapRunningAction(deepestName!) | ||
patrick11514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| return this.cachedLiveStatus; | ||
| } | ||
|
|
||
| async updateImageBuffer() { | ||
| try { | ||
| const res = await fetch(`${this.baseUrl}/v2/api/prepared-image`); | ||
| if (!res.ok) return; | ||
| this.cachedLiveImage = Buffer.from(await res.arrayBuffer()); | ||
| } catch (e) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error updating image buffer:', e); | ||
| } | ||
| } | ||
|
|
||
| async getLiveImage() { | ||
| if (!this.cachedLiveImage) { | ||
| await this.updateImageBuffer(); | ||
| } | ||
| return this.cachedLiveImage; | ||
| } | ||
| } | ||
|
|
||
| export const nina = new NinaClient(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.