Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,349 Location: Tampa, FL Thanks: 28 times Was thanked: 416 time(s) in 354 post(s)
|
S+ is already loading so many things that I would say if you have S+ running already, you should just move all of your automations into it. The same would apply for any other app, really. Once you have one engine that can handle automation, you're just wasting resources by loading a separate one! I haven't tested the actual timer and functionality, only tested the type/API binding and direct call to see that the values are coming back. Global Actions > Load/Unload > Load ScriptCode:// only create the types once
if(!NativeModules.PowerStatus)
{
var ByteT = host.typeOf(System.Byte);
var UintT = host.typeOf(System.UInt32);
var BooleanT = host.typeOf(System.Boolean);
var typesTB = sp.NativeModule().CreateType("PowerStatusTypes", "Class,Public,SequentialLayout,Serializable");
typesTB.DefineNestedStruct("SYSTEM_POWER_STATUS",
"ACLineStatus,BatteryFlag,BatteryLifePercent,SystemStatusFlag,BatteryLifeTime,BatteryFullLifeTime",
[ByteT,ByteT,ByteT,ByteT,UintT,UintT]).Create();
var PowerStatusTB = sp.NativeModule().DefineType("PowerStatus", "Class,Public,SequentialLayout,Serializable");
var SYSTEM_POWER_STATUS_refT = host.typeOf(NativeModules.PowerStatusTypes.SYSTEM_POWER_STATUS).MakeByRefType();
//Define the platform invoke calls
//From: https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getsystempowerstatus
PowerStatusTB.DefinePInvokeMethod("GetSystemPowerStatus",
"kernel32.dll",
[SYSTEM_POWER_STATUS_refT],
BooleanT,
"PreserveSig");
//Create the type (finalize, now made an official C# class)
PowerStatusTB.Create();
}
//Create timer to check battery
StrokesPlus.Timer.New("Battery",
0,
30000,
`var SYSTEM_POWER_STATUS = host.newVar(NativeModules.PowerStatusTypes.SYSTEM_POWER_STATUS);
NativeModules.PowerStatus.GetSystemPowerStatus(SYSTEM_POWER_STATUS.ref);
if(SYSTEM_POWER_STATUS.ACLineStatus == 1) {
// Plugged in, check battery
// If 94% or more and message hasn't been shown, show message
if(SYSTEM_POWER_STATUS.BatteryLifePercent >= 94) {
if(!StrokesPlus.StoredValues.Booleans.Get("BatteryMessageShown")) {
StrokesPlus.UI.Alert("Battery at " + SYSTEM_POWER_STATUS.BatteryLifePercent + "%", "Battery Level");
StrokesPlus.StoredValues.Booleans.Set("BatteryMessageShown", true);
}
}
} else {
// Not plugged in, clear var
StrokesPlus.StoredValues.Booleans.Dispose("BatteryMessageShown");
}
`);
Example/testing code, logs to User tab in Console windowCode://Create type to receive data from API call
var SYSTEM_POWER_STATUS = host.newVar(NativeModules.PowerStatusTypes.SYSTEM_POWER_STATUS);
//Call API
var result = NativeModules.PowerStatus.GetSystemPowerStatus(SYSTEM_POWER_STATUS.ref);
//Show result: True = successfully called API
StrokesPlus.Console.Log(result);
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-system_power_status
//AC power status: 1 = Plugged in, 0 = not plugged in
StrokesPlus.Console.Log(SYSTEM_POWER_STATUS.ACLineStatus);
//Current battery percentage
StrokesPlus.Console.Log(SYSTEM_POWER_STATUS.BatteryLifePercent);
Edited by user Sunday, November 28, 2021 10:52:33 PM(UTC)
| Reason: Not specified
|