Master : Master service call
This module is the EdgerOS master service module. This module is available in EdgerOS 1.6.0 and later.
This module is the asynchronous mode of the master
module. User can use the following code to import the master
module.
var master = require('async/master');
Support
The following shows master
module APIs available for each permissions.
User Mode | Privilege Mode | |
---|---|---|
master.log | ● | ● |
master.find | ● | ● |
master.cloud | ● | ● |
master.startup | ● | ● |
master.machine | ● | ● |
master.wallpaper | ● | ● |
master.alarmAdd | ● | ● |
master.alarmDelete | ● | ● |
master.alarmDeleteById | ● | ● |
Master Object
async master.log(file)
file
{String} Local file name.- Returns: {String} The argument
file
in when calling this function.
Each application can inform the EdgerOS operating system of the desired log mode through a description file (desc.json
). They include: 'file'
, 'console'
, 'null'
(fast mode), 'file'
mode means that all console
output of the application will be saved in the system log file, 'console'
mode means directly observe the application output through the EdgerOS IDE plug-in, and 'null'
mode means no log will be saved. When the application selects the 'file'
mode, the system will record the latest log output within 64-128KB. The system can call master.log()
function to notify EdgerOS to copy the log to the file specified by file
. EdgerOS will copy the current application log in append form. At this file, EdgerOS will erase the original log files after success, ensuring that the logs copied by each application request are not duplicated.
Example
async function getLog() {
return await master.log('./log.log');
}
When the application crashes, you can use this log to analyze uncaught exceptions.
async master.find(eapid)
eapid
{String} EdgerOS App package ID (Bundle ID: eg.'com.acoinfo.app'
).- Returns: {Object} App information.
The return object contains the following members:
vendor
{Object} App vendor information.id
{String} Vendor ID.name
{String} Vendor name.
version
{Array} App version, typical format:[x.y.z]
.
This method can be used to query whether the specified application is installed in current EdgerOS system. The EdgerOS application package ID is the application unique bundle name. This method is mostly used to share information with other known applications.
Example
master.find('com.acoinfo.app').then(info => {
console.log(JSON.stringify(info));
}).catch(() => console.log('Can not found this App.'));
async master.cloud()
- Returns: {Boolean} Whether to connect with EdgerOS Cloud.
This method can detect whether the current machine is connected to the cloud, or it can be used to detect whether the current device has an Internet connection.
Example
async function isDock() {
return await master.cloud();
}
async master.startup()
- Returns: {Boolean} Whether current App is auto startup.
Get whether current App is auto startup when EdgerOS boot on.
async master.machine()
- Returns: {Object} Machine information.
The return object contains the following members:
mname
{String} Machine name set by the administrator.machine
{Object} Machine information.product
{Object} Basic product info.vendor
{Object} Machine vendor information.version
{Array} EdgerOS version[x.x.x]
.
Get current machine information.
Example
master.machine().then(info => {
console.log(JSON.stringify(info));
}).catch(console.error);
async master.wallpaper(acoid, chunkOrPath[, option])
acoid
{String} User ID.chunkOrPath
{Buffer} | {String} Wallpaper bitmap buffer or picture file path.option
{Object} Set options.- Returns: {Boolean} Whether the operation was successful.
Set the EdgerOS wallpaper, please make sure you have the wallpaper
permission. Supported image formats include png
, jpeg
, the maximum image size does not exceed 4096 x 4096, and the size does not exceed 6 MBytes. option
parameter reserved.
Example
master.wallpaper(req.eos.user.acoid, './wallpaper.png').then(() => {
console.log('OK');
}).catch(console.error);
// Or:
var chunk = fs.readFile('./wallpaper.png');
master.wallpaper(req.eos.user.acoid, chunk).then(() => {
console.log('OK');
}).catch(console.error);
async master.alarmAdd(time, topic, msg[, extra])
time
{Date} | {Integer} | {String} Some time in the future.topic
{String} Topic that generates a message when the alarm expires.msg
{String} The content of the message when the alarm expires.extra
{Object} Extra Information.- Returns: {Integer} Alarm ID.
Create an alarm. Regardless of whether the application is closed or not, a preset push message will be generated when the alarm expires. time
is a UTC time, You have to guarantee that it is sometime in the future.
extra
can have the following members:
accounts
{Array} EdgerOS acoid array, specify the list of members to receive.
Example
var master = require('master');
var now = new Date();
var time = new Date(now.getTime() + 60 * 1000);
// Push this message after 1 minute, regardless of whether this application is closed.
master.alarmAdd(time, 'Test', 'This is message').then(id => {
console.log('New alarm id:', id);
}).catch(console.error);
async master.alarmDelete(start[, end])
start
{Date} | {Integer} | {String} Start time.end
{Date} | {Integer} | {String} End time. default: all future time from start.- Returns: {Boolean} Whether the operation was successful.
Delete the timer for the current app installation that expires in a future time period.
Example
// Delete all alarms created by the current app.
master.alarmDelete(new Date()).then(() => {
console.log('Delete All Alarms!');
}).catch(console.error);
async master.alarmDeleteById(alarmid)
alarmid
{Integer} Alarm ID, greater than 0 is valid.- Returns: {Boolean} Whether the operation was successful.
Delete alarm with specified alarmid
.
Master Events
The master
object will emit specific events in some situations.
state
This event will be generated when the application state changes, such as switching from the foreground to the background, returning to the foreground from the background, etc.
Example
var master = require('master');
master.on('state', function(state) {
if (state.foreground) {
console.log('Foreground!');
} else {
console.log('Background!');
}
});