Overview
Senquip devices have the ability to run custom scripts, allowing extra flexibility. The scripting language is called mJS and is essentially a restricted JavaScript engine.
mJS was designed for small embedded systems with limited resources. mJS implements a strict subset of ES6 (JavaScript version 6):
Any valid mJS code is a valid ES6 code.
Any valid ES6 code is not necessarily a valid mJS code.
Restrictions
The mJS scripting environment has the following restrictions:
No standard library. No String, Number, RegExp, Date, Function, etc.
JSON.parse() and JSON.stringify() are available.
No closures, only lexical scoping (i.e. nested functions are allowed).
No exceptions.
No new. In order to create an object with a custom prototype, use Object.create(), which is available.
Strict mode only.
No var, only let.
No for..of, =>, destructors, generators, proxies, promises.
No getters, setters, valueOf, prototypes, classes, template strings.
No == or !=, only === and !==.
mJS strings are byte strings, not Unicode strings: ‘ы’.length === 2, ‘ы’[0] === ‘xd1’, ‘ы’[1] === ‘x8b’. mJS string can represent any binary data chunk.
Risks
Warning
The scripting environment is a powerful tool. If incorrectly used the scripting functions may cause the device to malfunction, or even permanently brick the device.
The mJS scripting environment provides low level access to the device’s memory and CPU. Running a script on the device is not without risk, some of which include:
Permanent filesystem damage rendering the device unable to boot.
Malfunction of other functions, such as regular reporting of data to the portal.
Memory corruption, causing incorrect reporting of data.
Memory leaks, which may present intermittently or after a period of time.
Loss of remote control over the device, if your Portal account is compromised.
A good understanding of the environment, language and best practices will help avoid or mitigate these risks.
Timing
The mJS scripting environment is single-threaded. The thread is also shared with the device’s main functions, such as data collection and communication with the Senquip Portal. Scripts should be written to be non-blocking, and should not run for long periods of time.
Because mJS is an interpreted language, it is very slow. Typically about 10-100 times slower than native code. Any computationally intensive tasks should be broken down into smaller tasks, and run from a timed state machine.
Access
Scripts can be written and securely downloaded to the device from the Senquip Portal. Your Portal account must have ADMIN or USER permissions for the device.
You can access the scripting page by clicking on the scripting icon in the top toolbar from the device’s data page.