Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
G
gmsec_containers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Christopher Reis
gmsec_containers
Commits
ae97da01
Commit
ae97da01
authored
May 17, 2017
by
Christopher Reis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated example server
parent
9663e486
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
202 additions
and
1 deletion
+202
-1
docker-compose.yml
docker-compose.yml
+9
-1
app.js
example_server/app.js
+127
-0
dictionary.json
example_server/dictionary.json
+66
-0
No files found.
docker-compose.yml
View file @
ae97da01
...
...
@@ -33,6 +33,14 @@ mysql:
hostname
:
"
mysql"
container_name
:
"
mysql"
spacecraft_server
:
restart
:
always
build
:
./example_server
ports
:
-
"
8082:8082"
hostname
:
"
spacecraft_server"
container_name
:
"
spacecraft_server"
gm_kerbal_adaptor
:
restart
:
always
build
:
./gmsec_ubuntu
...
...
example_server/app.js
0 → 100644
View file @
ae97da01
/*global require,process,console*/
var
CONFIG
=
{
port
:
8082
,
dictionary
:
"dictionary.json"
,
interval
:
1000
};
(
function
()
{
"use strict"
;
var
WebSocketServer
=
require
(
'ws'
).
Server
,
fs
=
require
(
'fs'
),
wss
=
new
WebSocketServer
({
port
:
CONFIG
.
port
}),
dictionary
=
JSON
.
parse
(
fs
.
readFileSync
(
CONFIG
.
dictionary
,
"utf8"
)),
spacecraft
=
{
"prop.fuel"
:
77
,
"prop.thrusters"
:
"OFF"
,
"comms.recd"
:
0
,
"comms.sent"
:
0
,
"pwr.temp"
:
245
,
"pwr.c"
:
8.15
,
"pwr.v"
:
30
},
histories
=
{},
listeners
=
[];
function
updateSpacecraft
()
{
spacecraft
[
"prop.fuel"
]
=
Math
.
max
(
0
,
spacecraft
[
"prop.fuel"
]
-
(
spacecraft
[
"prop.thrusters"
]
===
"ON"
?
0.5
:
0
)
);
spacecraft
[
"pwr.temp"
]
=
spacecraft
[
"pwr.temp"
]
*
0.985
+
Math
.
random
()
*
0.25
+
Math
.
sin
(
Date
.
now
());
spacecraft
[
"pwr.c"
]
=
spacecraft
[
"pwr.c"
]
*
0.985
;
spacecraft
[
"pwr.v"
]
=
30
+
Math
.
pow
(
Math
.
random
(),
3
);
}
function
generateTelemetry
()
{
var
timestamp
=
Date
.
now
(),
sent
=
0
;
Object
.
keys
(
spacecraft
).
forEach
(
function
(
id
)
{
var
state
=
{
timestamp
:
timestamp
,
value
:
spacecraft
[
id
]
};
histories
[
id
]
=
histories
[
id
]
||
[];
// Initialize
histories
[
id
].
push
(
state
);
spacecraft
[
"comms.sent"
]
+=
JSON
.
stringify
(
state
).
length
;
});
listeners
.
forEach
(
function
(
listener
)
{
listener
();
});
}
function
update
()
{
updateSpacecraft
();
generateTelemetry
();
}
function
handleConnection
(
ws
)
{
var
subscriptions
=
{},
// Active subscriptions for this connection
handlers
=
{
// Handlers for specific requests
dictionary
:
function
()
{
ws
.
send
(
JSON
.
stringify
({
type
:
"dictionary"
,
value
:
dictionary
}));
},
subscribe
:
function
(
id
)
{
subscriptions
[
id
]
=
true
;
},
unsubscribe
:
function
(
id
)
{
delete
subscriptions
[
id
];
},
history
:
function
(
id
)
{
ws
.
send
(
JSON
.
stringify
({
type
:
"history"
,
id
:
id
,
value
:
histories
[
id
]
}));
}
};
function
notifySubscribers
()
{
Object
.
keys
(
subscriptions
).
forEach
(
function
(
id
)
{
var
history
=
histories
[
id
];
if
(
history
)
{
ws
.
send
(
JSON
.
stringify
({
type
:
"data"
,
id
:
id
,
value
:
history
[
history
.
length
-
1
]
}));
}
});
}
// Listen for requests
ws
.
on
(
'message'
,
function
(
message
)
{
var
parts
=
message
.
split
(
' '
),
handler
=
handlers
[
parts
[
0
]];
if
(
handler
)
{
handler
.
apply
(
handlers
,
parts
.
slice
(
1
));
}
});
// Stop sending telemetry updates for this connection when closed
ws
.
on
(
'close'
,
function
()
{
listeners
=
listeners
.
filter
(
function
(
listener
)
{
return
listener
!==
notifySubscribers
;
});
});
// Notify subscribers when telemetry is updated
listeners
.
push
(
notifySubscribers
);
}
update
();
setInterval
(
update
,
CONFIG
.
interval
);
wss
.
on
(
'connection'
,
handleConnection
);
console
.
log
(
"Example spacecraft running on port "
);
console
.
log
(
"Press Enter to toggle thruster state."
);
process
.
stdin
.
on
(
'data'
,
function
(
data
)
{
spacecraft
[
'prop.thrusters'
]
=
(
spacecraft
[
'prop.thrusters'
]
===
"OFF"
)
?
"ON"
:
"OFF"
;
console
.
log
(
"Thrusters "
+
spacecraft
[
"prop.thrusters"
]);
});
}());
example_server/dictionary.json
0 → 100644
View file @
ae97da01
{
"name"
:
"Example Spacecraft"
,
"identifier"
:
"sc"
,
"subsystems"
:
[
{
"name"
:
"Propulsion"
,
"identifier"
:
"prop"
,
"measurements"
:
[
{
"name"
:
"Fuel"
,
"identifier"
:
"prop.fuel"
,
"units"
:
"kilograms"
,
"type"
:
"float"
},
{
"name"
:
"Thrusters"
,
"identifier"
:
"prop.thrusters"
,
"units"
:
"None"
,
"type"
:
"string"
}
]
},
{
"name"
:
"Communications"
,
"identifier"
:
"comms"
,
"measurements"
:
[
{
"name"
:
"Received"
,
"identifier"
:
"comms.recd"
,
"units"
:
"bytes"
,
"type"
:
"integer"
},
{
"name"
:
"Sent"
,
"identifier"
:
"comms.sent"
,
"units"
:
"bytes"
,
"type"
:
"integer"
}
]
},
{
"name"
:
"Power"
,
"identifier"
:
"pwr"
,
"measurements"
:
[
{
"name"
:
"Generator Temperature"
,
"identifier"
:
"pwr.temp"
,
"units"
:
"
\u
0080C"
,
"type"
:
"float"
},
{
"name"
:
"Generator Current"
,
"identifier"
:
"pwr.c"
,
"units"
:
"A"
,
"type"
:
"float"
},
{
"name"
:
"Generator Voltage"
,
"identifier"
:
"pwr.v"
,
"units"
:
"V"
,
"type"
:
"float"
}
]
}
]
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment