-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
141 lines (117 loc) · 3.16 KB
/
example.js
File metadata and controls
141 lines (117 loc) · 3.16 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
import OpenSpaceApi from '../src/api';
import Socket from './socket';
const password = '';
const socket = new Socket('localhost', 4681);
const api = new OpenSpaceApi(socket);
api.onDisconnect(() => {
console.log('Disconnected from OpenSpace');
});
api.onConnect(async () => {
console.log('Connected to OpenSpace');
try {
await api.authenticate(password);
} catch(e) {
console.log('Authenication failed. Error: \n', e);
return;
};
let openspace = {};
try {
openspace = await api.singleReturnLibrary();
} catch (e) {
console.log('OpenSpace library could not be loaded: Error: \n', e)
return;
}
getTime(openspace);
// getGeoPositionForCamera(openspace);
// getScaleUpdates();
// setInterval(() => addSceneGraphNode(openspace), 2000);
// scaleEarth(api);
});
api.connect();
// Functions:
async function getScaleUpdates() {
const subscription = api.subscribeToProperty('Scene.Earth.Scale.Scale');
let i = 0;
(async () => {
try {
for await (const data of subscription.iterator()) {
console.log(data);
if (i > 3) {
subscription.cancel();
}
++i;
}
} catch (e) {
console.log('Failed to get data from property. Error: \n', e);
}
})();
}
async function getTime(openspace) {
try {
const t = await openspace.time.UTC();
console.log("Current simulation time: " + t);
} catch (e) {
console.log('failed to get time. Error: \n', e);
}
}
async function getGeoPosition(openspace) {
try {
const pos = await openspace.globebrowsing.getGeoPosition("Earth", 10, 10, 10);
console.log(pos);
} catch (e) {
console.log('failed to get geo position. Error: \n', e);
}
}
async function getGeoPositionForCamera(openspace) {
try {
const pos = await openspace.globebrowsing.getGeoPositionForCamera();
console.log(pos);
} catch (e) {
console.log('failed to get geo position for camera. Error: \n', e);
}
}
let nodeIndex = 0;
async function addSceneGraphNode(openspace) {
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: 'TestNode'
}
});
} catch (e) {
console.log("Failed to add scene graph node. Error: \n ", e);
}
nodeIndex++;
console.log('Added ' + name);
try {
openspace.setPropertyValue("NavigationHandler.OrbitalNavigator.Anchor", identifier);
openspace.setPropertyValue("NavigationHandler.OrbitalNavigator.RetargetAnchor", null);
} catch (e) {
console.log("Failed to set anchor node. Error: \n ", e);
}
}
async function scaleEarth(api) {
console.log('scaling earth');
const property = 'Scene.Earth.Scale.Scale';
const data = await api.getProperty(property);
let target = 2;
if (data.Value > 1) {
target = 1;
}
api.setProperty(property, target);
}