Send reboots, handle reconnecting

This commit is contained in:
Ian Gulliver
2016-05-11 00:24:44 +00:00
parent 504c6ab81a
commit 735cb4909f

View File

@@ -4,12 +4,21 @@ let ImageController = function(container) {
this.container_ = container; this.container_ = container;
this.image_types_ = new Map(); this.image_types_ = new Map();
this.ws_ = new WebSocket('wss://' + location.host + '/ws/master', 'iconograph-master'); this.connect_();
this.ws_.addEventListener('message', (e) => this.onMessage_(JSON.parse(e.data)));
this.timer_ = setInterval((e) => this.onTick_(), 250); this.timer_ = setInterval((e) => this.onTick_(), 250);
}; };
ImageController.prototype.connect_ = function() {
this.ws_ = new WebSocket('wss://' + location.host + '/ws/master', 'iconograph-master');
this.ws_.addEventListener('message', (e) => this.onMessage_(JSON.parse(e.data)));
this.ws_.addEventListener('close', (e) => this.onClose_());
};
ImageController.prototype.onClose_ = function() {
setTimeout((e) => this.connect_(), 5000);
};
ImageController.prototype.onMessage_ = function(msg) { ImageController.prototype.onMessage_ = function(msg) {
switch (msg['type']) { switch (msg['type']) {
case 'image_types': case 'image_types':
@@ -83,10 +92,15 @@ ImageController.prototype.addInstance_ = function(type, hostname) {
value.volume_id = this.createNode_(value.section, 'volumeID'); value.volume_id = this.createNode_(value.section, 'volumeID');
value.next_timestamp = this.createNode_(value.section, 'timestamp'); value.next_timestamp = this.createNode_(value.section, 'timestamp');
value.next_volume_id = this.createNode_(value.section, 'volumeID'); value.next_volume_id = this.createNode_(value.section, 'volumeID');
value.reboot = this.createNode_(value.section, 'reboot', 'Reboot');
value.volume_id.addEventListener( value.volume_id.addEventListener(
'click', (e) => this.onVolumeIDClick_(e.target.innerText)); 'click', (e) => this.onVolumeIDClick_(e.target.innerText));
value.next_volume_id.addEventListener( value.next_volume_id.addEventListener(
'click', (e) => this.onVolumeIDClick_(e.target.innerText)); 'click', (e) => this.onVolumeIDClick_(e.target.innerText));
value.reboot.addEventListener(
'click', (e) => this.sendReboot_(hostname));
type.instances.set(hostname, value); type.instances.set(hostname, value);
}; };
@@ -108,6 +122,16 @@ ImageController.prototype.onVolumeIDClick_ = function(volume_id) {
open(base_url.replace('VOLUMEID', volume_id)); open(base_url.replace('VOLUMEID', volume_id));
}; };
ImageController.prototype.sendReboot_ = function(hostname) {
this.ws_.send(JSON.stringify({
'type': 'command',
'target': hostname,
'data': {
'command': 'reboot',
},
}));
};
ImageController.prototype.insertSorted_ = function(parent, new_child, key) { ImageController.prototype.insertSorted_ = function(parent, new_child, key) {
let insert_before = null; let insert_before = null;
for (var i = 0; i < parent.childNodes.length; i++) { for (var i = 0; i < parent.childNodes.length; i++) {