Commit 63587a63 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

software/js-drone: Comply with JSLint

parent 8141a390
...@@ -26,12 +26,12 @@ md5sum = 1ff50063f5a54712a0bc0ff38fa74630 ...@@ -26,12 +26,12 @@ md5sum = 1ff50063f5a54712a0bc0ff38fa74630
[main] [main]
filename = main.js filename = main.js
md5sum = c22879bb553a0932dd145a58acdf37e6 md5sum = 2118d7908a909c585e03531147b1d540
[pubsub] [pubsub]
filename = pubsub.js filename = pubsub.js
md5sum = c732be66f8ec97bd16cd34d06a0c0a0b md5sum = 1555496ad591a31a845f33488d5c335d
[worker] [worker]
filename = worker.js filename = worker.js
md5sum = d919ce35d42561bc38a06273781cb702 md5sum = bb067644bb1ee2e8eda1324718e317b1
/* global console */ /*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global arm, console, exit, open, scriptArgs, setTimeout, start, stop,
stopPubsub, takeOffAndWait, Worker*/
import { import {
arm, arm,
start, start,
...@@ -9,16 +11,23 @@ import { ...@@ -9,16 +11,23 @@ import {
import { setTimeout, Worker } from "os"; import { setTimeout, Worker } from "os";
import { open, exit } from "std"; import { open, exit } from "std";
(function (console, setTimeout, Worker) { (function (arm, console, exit, open, scriptArgs, setTimeout, start, stop,
stopPubsub, takeOffAndWait, Worker) {
"use strict"; "use strict";
const CONF_PATH = {{ json_module.dumps(configuration) }};
var conf_file = open(CONF_PATH, "r"); var CONF_PATH = {{ json_module.dumps(configuration) }},
const configuration = JSON.parse(conf_file.readAsString()); conf_file = open(CONF_PATH, "r"),
conf_file.close(); configuration = JSON.parse(conf_file.readAsString()),
URL = "udp://" + configuration.autopilotIp + ":7909",
LOG_FILE = "{{ log_dir }}/mavsdk-log",
pubsubWorker,
worker,
user_script = scriptArgs[1],
FPS = 50, // Minimum sampling interval for open62541 monitored items
previous_timestamp,
can_update = false;
const URL = "udp://" + configuration.autopilotIp + ":7909", conf_file.close();
LOG_FILE = "{{ log_dir }}/mavsdk-log";
// Use a Worker to ensure the user script // Use a Worker to ensure the user script
// does not block the main script // does not block the main script
...@@ -26,17 +35,14 @@ import { open, exit } from "std"; ...@@ -26,17 +35,14 @@ import { open, exit } from "std";
// Create the update loop in the main script // Create the update loop in the main script
// to prevent it to finish (and so, exit the quickjs process) // to prevent it to finish (and so, exit the quickjs process)
var pubsubWorker, worker = new Worker("{{ worker_script }}");
worker = new Worker("{{ worker_script }}"),
user_script = scriptArgs[1],
// Minimum sampling interval for open62541 monitored items
FPS = 50,
previous_timestamp,
can_update = false;
function connect() { function quit(is_a_drone, exit_code) {
console.log("Will connect to", URL); stopPubsub();
exitOnFail(start(URL, LOG_FILE, 60), "Failed to connect to " + URL); if (is_a_drone) {
stop();
}
exit(exit_code);
} }
function exitOnFail(ret, msg) { function exitOnFail(ret, msg) {
...@@ -46,12 +52,9 @@ import { open, exit } from "std"; ...@@ -46,12 +52,9 @@ import { open, exit } from "std";
} }
} }
function quit(is_a_drone, exit_code) { function connect() {
stopPubsub(); console.log("Will connect to", URL);
if (is_a_drone) { exitOnFail(start(URL, LOG_FILE, 60), "Failed to connect to " + URL);
stop();
}
exit(exit_code);
} }
if (configuration.isADrone) { if (configuration.isADrone) {
...@@ -60,11 +63,11 @@ import { open, exit } from "std"; ...@@ -60,11 +63,11 @@ import { open, exit } from "std";
} }
pubsubWorker = new Worker("{{ pubsub_script }}"); pubsubWorker = new Worker("{{ pubsub_script }}");
pubsubWorker.onmessage = function(e) { pubsubWorker.onmessage = function (e) {
if (!e.data.publishing) { if (!e.data.publishing) {
pubsubWorker.onmessage = null; pubsubWorker.onmessage = null;
} }
} };
worker.postMessage({type: "initPubsub"}); worker.postMessage({type: "initPubsub"});
...@@ -91,7 +94,7 @@ import { open, exit } from "std"; ...@@ -91,7 +94,7 @@ import { open, exit } from "std";
} }
function loop() { function loop() {
let timestamp = Date.now(), var timestamp = Date.now(),
timeout; timeout;
if (can_update) { if (can_update) {
if (FPS <= (timestamp - previous_timestamp)) { if (FPS <= (timestamp - previous_timestamp)) {
...@@ -117,7 +120,7 @@ import { open, exit } from "std"; ...@@ -117,7 +120,7 @@ import { open, exit } from "std";
} }
worker.onmessage = function (e) { worker.onmessage = function (e) {
let type = e.data.type; var type = e.data.type;
if (type === 'initialized') { if (type === 'initialized') {
pubsubWorker.postMessage({ pubsubWorker.postMessage({
action: "run", action: "run",
...@@ -141,4 +144,5 @@ import { open, exit } from "std"; ...@@ -141,4 +144,5 @@ import { open, exit } from "std";
quit(configuration.isADrone, 1); quit(configuration.isADrone, 1);
} }
}; };
}(console, setTimeout, Worker)); }(arm, console, exit, open, scriptArgs, setTimeout, start, stop, stopPubsub,
takeOffAndWait, Worker));
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, open, runPubsub, Worker*/
import {runPubsub} from {{ json_module.dumps(qjs_wrapper) }}; import {runPubsub} from {{ json_module.dumps(qjs_wrapper) }};
import {Worker} from "os"; import {Worker} from "os";
import {open} from "std"; import {open} from "std";
const CONF_PATH = {{ json_module.dumps(configuration) }}, (function (console, open, runPubsub, Worker) {
PORT = "4840"; "use strict";
let parent = Worker.parent; var CONF_PATH = {{ json_module.dumps(configuration) }},
PORT = "4840",
parent = Worker.parent,
conf_file = open(CONF_PATH, "r"),
configuration = JSON.parse(conf_file.readAsString());
conf_file.close();
var conf_file = open(CONF_PATH, "r"); function handle_msg(e) {
const configuration = JSON.parse(conf_file.readAsString()); switch (e.data.action) {
conf_file.close();
function handle_msg(e) {
switch(e.data.action) {
case "run": case "run":
runPubsub(configuration.multicastIp, PORT, configuration.netIf, e.data.id, e.data.interval, e.data.publish); runPubsub(
configuration.multicastIp,
PORT,
configuration.netIf,
e.data.id,
e.data.interval,
e.data.publish
);
parent.postMessage({running: false}); parent.postMessage({running: false});
parent.onmessage = null; parent.onmessage = null;
break; break;
default: default:
console.log("Undefined action from parent: ", e.data.action); console.log("Undefined action from parent: ", e.data.action);
} }
} }
parent.onmessage = handle_msg; parent.onmessage = handle_msg;
}(console, open, runPubsub, Worker));
\ No newline at end of file
/* global console, std */ /*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, getAltitude, getAltitudeRel, getInitialAltitude, getLatitude,
getLongitude, getYaw, execUserScript, initPubsub, isInManualMode, landed,
loiter, setAirspeed, setAltitude, setManualControlInput, setMessage,
setTargetCoordinates, std, triggerParachute, Drone, Worker*/
import { import {
Drone, Drone,
triggerParachute, triggerParachute,
...@@ -21,17 +25,18 @@ import { ...@@ -21,17 +25,18 @@ import {
import * as std from "std"; import * as std from "std";
import { Worker } from "os"; import { Worker } from "os";
(function (console, Worker) { (function (console, getAltitude, getAltitudeRel, getInitialAltitude,
getLatitude, getLongitude, getYaw, initPubsub, isInManualMode,
landed, loiter, setAirspeed, setAltitude, setManualControlInput,
setMessage, setTargetCoordinates, std, triggerParachute, Drone,
Worker) {
// Every script is evaluated per drone // Every script is evaluated per drone
"use strict"; "use strict";
const CONF_PATH = {{ json_module.dumps(configuration) }},
drone_dict = {};
var conf_file = std.open(CONF_PATH, "r"); var CONF_PATH = {{ json_module.dumps(configuration) }},
const configuration = JSON.parse(conf_file.readAsString()); conf_file = std.open(CONF_PATH, "r"),
conf_file.close(); configuration = JSON.parse(conf_file.readAsString()),
parent = Worker.parent,
let parent = Worker.parent,
user_me = { user_me = {
//for debugging purpose //for debugging purpose
fdopen: std.fdopen, fdopen: std.fdopen,
...@@ -39,12 +44,12 @@ import { Worker } from "os"; ...@@ -39,12 +44,12 @@ import { Worker } from "os";
//required to fly //required to fly
triggerParachute: triggerParachute, triggerParachute: triggerParachute,
drone_dict: {}, drone_dict: {},
exit: function(exit_code) { exit: function (exit_code) {
parent.postMessage({type: "exited", exit: exit_code}); parent.postMessage({type: "exited", exit: exit_code});
parent.onmessage = null; parent.onmessage = null;
}, },
getAltitudeAbs: getAltitude, getAltitudeAbs: getAltitude,
getCurrentPosition: function() { getCurrentPosition: function () {
return { return {
x: getLatitude(), x: getLatitude(),
y: getLongitude(), y: getLongitude(),
...@@ -56,25 +61,25 @@ import { Worker } from "os"; ...@@ -56,25 +61,25 @@ import { Worker } from "os";
id: configuration.id, id: configuration.id,
landed: landed, landed: landed,
loiter: loiter, loiter: loiter,
sendMsg: function(msg, id = -1) { sendMsg: function (msg, id) {
if (id === undefined) { id = -1; }
setMessage(JSON.stringify({ content: msg, dest_id: id })); setMessage(JSON.stringify({ content: msg, dest_id: id }));
}, },
setAirspeed: setAirspeed, setAirspeed: setAirspeed,
setAltitude: setAltitude, setAltitude: setAltitude,
setTargetCoordinates: setTargetCoordinates setTargetCoordinates: setTargetCoordinates
}; };
conf_file.close();
function loadUserScript(path) { function loadUserScript(path) {
let script_content = std.loadFile(path); var script_content = std.loadFile(path);
if (script_content === null) { if (script_content === null) {
console.log("Failed to load user script " + path); console.log("Failed to load user script " + path);
std.exit(1); std.exit(1);
} }
try { try {
std.evalScript( std.evalScript(
"function execUserScript(from, me) {" + "function execUserScript(from, me) {" + script_content + "};"
script_content +
"};"
); );
} catch (e) { } catch (e) {
console.log("Failed to evaluate user script", e); console.log("Failed to evaluate user script", e);
...@@ -102,19 +107,16 @@ import { Worker } from "os"; ...@@ -102,19 +107,16 @@ import { Worker } from "os";
loadUserScript(evt.data.path); loadUserScript(evt.data.path);
parent.postMessage({type: "loaded"}); parent.postMessage({type: "loaded"});
} else if (type === "update") { } else if (type === "update") {
for (const [id, drone] of Object.entries(user_me.drone_dict)) { Object.entries(user_me.drone_dict).forEach(function ([id, drone]) {
message = drone.message message = drone.message;
if (message.length > 0) { if (user_me.id !== Number(id) && message.length > 0) {
message = JSON.parse(message); message = JSON.parse(message);
if (user_me.id === id) {
continue;
}
if (user_me.hasOwnProperty("onGetMsg") && if (user_me.hasOwnProperty("onGetMsg") &&
[-1, user_me.id].includes(message.dest_id)) { [-1, user_me.id].includes(message.dest_id)) {
user_me.onGetMsg(message.content); user_me.onGetMsg(message.content);
} }
} }
} });
// Call the drone onStart function // Call the drone onStart function
if (user_me.hasOwnProperty("onUpdate")) { if (user_me.hasOwnProperty("onUpdate")) {
if (configuration.isADrone && isInManualMode()) { if (configuration.isADrone && isInManualMode()) {
...@@ -138,4 +140,7 @@ import { Worker } from "os"; ...@@ -138,4 +140,7 @@ import { Worker } from "os";
std.exit(1); std.exit(1);
} }
}; };
}(console, Worker)); }(console, getAltitude, getAltitudeRel, getInitialAltitude, getLatitude,
getLongitude, getYaw, initPubsub, isInManualMode, landed, loiter, setAirspeed,
setAltitude, setManualControlInput, setMessage, setTargetCoordinates, std,
triggerParachute, Drone, Worker));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment