mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-15 07:43:50 +00:00
1. Updated metadata for linux ipcSocket
2, Added linux-acpi, and attached events to power-monitor for linux
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -484,11 +484,27 @@ duk_ret_t ILibDuktape_net_socket_connect(duk_context *ctx)
|
|||||||
duk_push_true(ptrs->ctx); // [socket][connecting]
|
duk_push_true(ptrs->ctx); // [socket][connecting]
|
||||||
duk_put_prop_string(ptrs->ctx, -2, "connecting"); // [socket]
|
duk_put_prop_string(ptrs->ctx, -2, "connecting"); // [socket]
|
||||||
duk_pop(ptrs->ctx); // ...
|
duk_pop(ptrs->ctx); // ...
|
||||||
|
|
||||||
|
if (duk_is_object(ptrs->ctx, 0))
|
||||||
|
{
|
||||||
|
if (duk_has_prop_string(ptrs->ctx, 0, "metadata"))
|
||||||
|
{
|
||||||
|
duk_size_t len;
|
||||||
|
char *tmp = (char*)duk_push_sprintf(ptrs->ctx, "net.ipcSocket, %s", (char*)Duktape_GetStringPropertyValueEx(ptrs->ctx, 0, "metadata", "", &len));
|
||||||
|
char *tmp2 = (char*)ILibMemory_SmartAllocate(len + 16);
|
||||||
|
memcpy_s(tmp2, ILibMemory_Size(tmp2), tmp, ILibMemory_Size(tmp2) - 1);
|
||||||
|
ILibChain_Link_SetMetadata(ptrs->socketModule, tmp2);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ILibChain_Link_SetMetadata(ptrs->socketModule, "net.ipcSocket");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (duk_is_number(ctx, 0))
|
if (duk_is_number(ctx, 0))
|
||||||
{
|
{
|
||||||
// This is a PORT number
|
// This is a PORT number
|
||||||
@@ -807,7 +823,6 @@ void ILibDuktape_net_server_OnConnect(ILibAsyncServerSocket_ServerModule AsyncSe
|
|||||||
{
|
{
|
||||||
ILibDuktape_net_server *ptr = (ILibDuktape_net_server*)((void**)ILibMemory_GetExtraMemory(AsyncServerSocketModule, ILibMemory_ASYNCSERVERSOCKET_CONTAINERSIZE))[0];
|
ILibDuktape_net_server *ptr = (ILibDuktape_net_server*)((void**)ILibMemory_GetExtraMemory(AsyncServerSocketModule, ILibMemory_ASYNCSERVERSOCKET_CONTAINERSIZE))[0];
|
||||||
ILibDuktape_net_server_session *session;
|
ILibDuktape_net_server_session *session;
|
||||||
int isIPC = 0;
|
|
||||||
#ifndef MICROSTACK_NOTLS
|
#ifndef MICROSTACK_NOTLS
|
||||||
int isTLS = ILibAsyncSocket_IsUsingTls(ConnectionToken);
|
int isTLS = ILibAsyncSocket_IsUsingTls(ConnectionToken);
|
||||||
#else
|
#else
|
||||||
|
|||||||
47
modules/linux-acpi.js
Normal file
47
modules/linux-acpi.js
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2020 Intel Corporation
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
function linux_acpi()
|
||||||
|
{
|
||||||
|
this._ObjectID = 'linux-acpi';
|
||||||
|
require('events').EventEmitter.call(this, true)
|
||||||
|
.createEvent('acpi');
|
||||||
|
Object.defineProperty(this, "supported", { value: require('fs').existsSync('/var/run/acpid.socket') });
|
||||||
|
if(this.supported)
|
||||||
|
{
|
||||||
|
this._client = require('net').createConnection({ path: '/var/run/acpid.socket', metadata: 'linux-acpi' }, function ()
|
||||||
|
{
|
||||||
|
this.on('data', function (chunk)
|
||||||
|
{
|
||||||
|
var blocks;
|
||||||
|
var ubuffer = null;
|
||||||
|
var tokens = chunk.toString().split('\n');
|
||||||
|
if (tokens.length == 1) { this.unshift(chunk); }
|
||||||
|
if (tokens.peek() != '') { ubuffer = Buffer.from(tokens.pop()); }
|
||||||
|
else { tokens.pop(); }
|
||||||
|
for (var i in tokens)
|
||||||
|
{
|
||||||
|
blocks = tokens[i].split(' ');
|
||||||
|
this.ret.emit('acpi', { name: blocks[0], type: Buffer.from(blocks[2], 'hex').readUInt32BE(), value: Buffer.from(blocks[3], 'hex').readUInt32BE() });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
this._client.ret = this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = new linux_acpi();
|
||||||
@@ -24,11 +24,79 @@ function powerMonitor()
|
|||||||
.createEvent('acdc')
|
.createEvent('acdc')
|
||||||
.createEvent('display');
|
.createEvent('display');
|
||||||
|
|
||||||
|
this._ACState = 1;
|
||||||
|
this._BatteryLevel = -1;
|
||||||
|
|
||||||
|
if (process.platform == 'win32')
|
||||||
|
{
|
||||||
|
// These must be registered BEFORE newListener is hooked up
|
||||||
|
this.on('batteryLevel', function (level) { this._BatteryLevel = level; });
|
||||||
|
this.on('acdc', function (m) { this._ACState = (m == 'AC' ? 1 : 0); });
|
||||||
|
}
|
||||||
|
|
||||||
|
this.on('newListener', function (name, callback)
|
||||||
|
{
|
||||||
|
if (name == 'acdc') { callback.call(this, this._ACState == 1 ? 'AC' : 'BATTERY'); }
|
||||||
|
if (name == 'batteryLevel') { callback.call(this, this._BatteryLevel); }
|
||||||
|
});
|
||||||
|
|
||||||
this._i = setImmediate(function (self)
|
this._i = setImmediate(function (self)
|
||||||
{
|
{
|
||||||
require('user-sessions'); // This is needed because this is where the Windows Messages are processed for these events
|
require('user-sessions'); // This is needed because this is where the Windows Messages are processed for these events
|
||||||
delete self._i;
|
delete self._i;
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
|
if(process.platform == 'linux')
|
||||||
|
{
|
||||||
|
this._ACPath = null;
|
||||||
|
this._BatteryPath = [];
|
||||||
|
|
||||||
|
var devices = require('fs').readdirSync('/sys/class/power_supply');
|
||||||
|
for (var i in devices)
|
||||||
|
{
|
||||||
|
if (require('fs').readFileSync('/sys/class/power_supply/' + devices[i] + '/type').toString().trim() == 'Mains')
|
||||||
|
{
|
||||||
|
this._ACPath = '/sys/class/power_supply/' + devices[i] + '/';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var i in devices)
|
||||||
|
{
|
||||||
|
if (require('fs').readFileSync('/sys/class/power_supply/' + devices[i] + '/type').toString().trim() == 'Battery')
|
||||||
|
{
|
||||||
|
this._BatteryPath.push('/sys/class/power_supply/' + devices[i] + '/');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(this._ACPath != null)
|
||||||
|
{
|
||||||
|
this._ACState = parseInt(require('fs').readFileSync(this._ACPath + 'online').toString().trim());
|
||||||
|
}
|
||||||
|
if(this._BatteryPath.length>0)
|
||||||
|
{
|
||||||
|
this._getBatteryLevel = function _getBatteryLevel()
|
||||||
|
{
|
||||||
|
var sum = 0;
|
||||||
|
var i;
|
||||||
|
for (i in this._BatteryPath)
|
||||||
|
{
|
||||||
|
sum += parseInt(require('fs').readFileSync(this._BatteryPath[i] + 'capacity').toString().trim());
|
||||||
|
}
|
||||||
|
sum = Math.floor(sum / this._BatteryPath.length);
|
||||||
|
return (sum);
|
||||||
|
}
|
||||||
|
this._BatteryLevel = this._getBatteryLevel();
|
||||||
|
}
|
||||||
|
this._acpiSink = function _acpiSink(acpiEvent)
|
||||||
|
{
|
||||||
|
if(acpiEvent.name == 'ac_adapter')
|
||||||
|
{
|
||||||
|
_acpiSink.self._ACState = acpiEvent.value;
|
||||||
|
_acpiSink.self.emit('acdc', acpiEvent.value == 1 ? 'AC' : 'BATTERY');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this._acpiSink.self = this;
|
||||||
|
require('linux-acpi').on('acpi', this._acpiSink);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = new powerMonitor();
|
module.exports = new powerMonitor();
|
||||||
Reference in New Issue
Block a user