-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.ts
More file actions
181 lines (153 loc) · 4.42 KB
/
example.ts
File metadata and controls
181 lines (153 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { OpenSpaceApi } from '../src/api';
import { Socket } from '../src/socket';
import { OpenSpaceLibrary } from '../src/types/generated';
const password = '';
const socket = new Socket('localhost', 4681);
const api = new OpenSpaceApi(socket);
let openspace: OpenSpaceLibrary | null = null;
api.onDisconnect(() => {
console.log('Disconnected from OpenSpace');
});
api.onConnect(async () => {
console.log('Connected to OpenSpace');
try {
await api.authenticate(password);
} catch (e) {
console.log('Authentication failed. Error: \n', e);
return;
}
try {
openspace = await api.library();
} catch (e) {
console.log('OpenSpace library could not be loaded: Error: \n', e);
return;
}
await main();
});
api.connect();
async function main() {
await getTime();
await scaleEarth();
await getGeoPositionForCamera();
await getScaleUpdates();
for (let i = 0; i < 5; i++) {
setTimeout(() => {
addSceneGraphNode();
}, 2000);
}
setTimeout(() => {
api.disconnect();
}, 10000);
}
async function getTime() {
try {
const time = await openspace?.time.UTC();
console.log(`Current simulation time: ${time}`);
} catch (e) {
console.log('Failed to get time. Error: \n', e);
}
}
async function getGeoPositionForCamera() {
try {
if (!openspace) {
return;
}
const pos = await openspace.globebrowsing.geoPositionForCamera();
console.log(`Geo position: ${pos}`);
} catch (e) {
console.log('Failed to get geo position for camera. Error: \n', e);
}
}
async function getScaleUpdates() {
const topic = api.subscribeToProperty('Scene.Earth.Scale.Scale');
let i = 0;
async function loop() {
try {
for await (const data of topic) {
console.log(`Waiting for Earth scale update ${i}/3`);
if (data.type === 'value') {
console.log(`Earth scale value update ${i}/3: ${data.value}`);
} else {
console.log(`Earth metadata update ${i}/3: ${data.metaData}`);
}
if (i >= 3) {
console.log('Canceling topic');
topic.cancel();
return;
}
i++;
}
} catch (e) {
console.log('Failed to get data from property. Error: \n', e);
}
}
await loop();
}
let nodeIndex = 0;
async function addSceneGraphNode() {
const identifier = `TestNode${nodeIndex}`;
const name = `Test Node ${nodeIndex}`;
try {
await openspace?.addSceneGraphNode({
Identifier: identifier,
Name: name,
Parent: 'Earth',
Transform: {
Translation: {
Type: 'GlobeTranslation',
Globe: 'Earth',
Latitude: (nodeIndex * 13) % 90,
Longitude: (nodeIndex * 17) % 180,
FixedAltitude: 10
}
},
GUI: {
Path: '/Other/Test',
Name: name
}
});
} catch (e) {
console.log('Failed to add scene graph node. Error: \n ', e);
}
nodeIndex++;
console.log(`Added ${name}`);
openspace?.setPropertyValue('NavigationHandler.OrbitalNavigator.Anchor', identifier);
openspace?.setPropertyValue('NavigationHandler.OrbitalNavigator.RetargetAnchor', null);
}
async function scaleEarth() {
// There are two options for getting a property where the only difference is the type
// IntelliSense
const uri = 'Scene.Earth.Scale.Scale';
{
// Option 1: Get the property value
const data = await api.getProperty(uri);
if (data.type === 'property') {
const property = data.value; // The type of property is unknown at this point
// However, in this case we know that Earth.Scale is a DoubleProperty so we can tell
// TypeScript IntelliSense to treat it like a number to avoid TypeScript error
const value = property.value as number;
let target = 2;
if (value > 1) {
target = 1;
}
console.log(`Scaling Earth: ${target}`);
api.setProperty(uri, target);
}
}
// Option 2: Get the property with as an expected property type
{
const data = await api.getProperty(uri, 'DoubleProperty');
if (data.type === 'property') {
// If we got this far, property is now correctly narrowed to a DoubleProperty
const property = data.value;
// No need to do `as number` - value I already correctly inferred
const value = property.value;
let target = 2;
if (value > 1) {
target = 1;
}
console.log(`Scaling Earth: ${target}`);
api.setProperty(uri, target);
}
}
}