Skip to content

Commit

Permalink
Merge pull request #1 from Ayokas/master
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
Ayokas authored Aug 9, 2022
2 parents 11eeb09 + 6c5ed31 commit 3ee23c7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
54 changes: 54 additions & 0 deletions DCSEnergyManagement.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- Updates Windows Energy Management based on playercount. Boosts to "High Performance" when players connect.
-- If no players are connected (IDLE) sets windows energy plan to balanced to save power.
--
-- Author: Ayokas (August.2022)
-- See GitHub: https://github.com/Ayokas/DCS-Energy-Management

-- GUIDs for powercfg tool, get GUIDs for you system with cmd or powershell: powercfg /L
-- Balanced GUID, e.g. "381b4222-f694-41f0-9685-ff5bb260df2e"
local balancedGUID = "<insert blanced GUID here>"

-- High Performance GUID, e.g. "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"
local highPerformanceGUID = "<insert high performance GUID here>"


-- Leave this as it is =)

local energyHandler = {}
local clients = 0


function energyHandler.log(str)
net.log(string.format("[EnergyManagement] %s", str))
end

energyHandler.onPlayerConnect = function(id)
clients = clients + 1
energyHandler.log(string.format("%s connected, total clients %d", net.get_player_info(id, 'name'), clients))

if clients == 2 then
energyHandler.log("Player connected - setting to high performance")
-- execute os command via os.execute: http://www.lua.org/manual/5.4/manual.html#pdf-os.execute
os.execute(string.format("powercfg /S %s", highPerformanceGUID))
end

-- if server starts set to energy saving, just to be sure
if clients == 1 then
energyHandler.log("No players connected - setting to energy saving / balanced")
os.execute(string.format("powercfg /S %s", balancedGUID))
end
end


energyHandler.onPlayerDisconnect = function(id)
clients = clients - 1
energyHandler.log(string.format("Player disconnected, total clients %d", clients))

if clients == 1 then
energyHandler.log("No players connected - setting to energy saving / balanced")
os.execute(string.format("powercfg /S %s", balancedGUID))
end
end

DCS.setUserCallbacks(energyHandler)
energyHandler.log("Loaded and ready!")
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
# DCS-Energy-Management
DCS Lua-Hook for Windows Energy Plan
This server hook sets windows power plans depending if players are connected or not. It sets the system energy plan to "High Performance" when players are connected to eliminate stutters. When all players are disconnected it will set the "Balanced" plan, so the system consumes less energy and reduces your electricity bill =)

## Installation
Copy the `DCSEnergyManagement.lua` file into your servers `%SAVED_GAMES%\DCS.openbeta_server\Scripts\Hooks\` directory.

Use **powershell** or **CMD** to get the GUIDs for you powerplan (as they might be different).

The output will look like this (currently in german, will provide in english as soon as I can but it will be obvious which values you need):
```powershell
PS C:\Users\Administrator> powercfg /L
Bestehende Energieschemen (* Aktiv)
-----------------------------------
GUID des Energieschemas: 381b4222-f694-41f0-9685-ff5bb260df2e (Ausbalanciert) *
GUID des Energieschemas: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c (Höchstleistung)
GUID des Energieschemas: a1841308-3541-4fab-bc81-f71556f20b4a (Energiesparmodus)
```

After that edit the configuration lines of the script:
``` lua
-- GUIDs for powercfg tool, get GUIDs for you system with cmd or powershell: powercfg /L
-- Balanced GUID, e.g. "381b4222-f694-41f0-9685-ff5bb260df2e"
local balancedGUID = "<insert blanced GUID here>"

-- High Performance GUID, e.g. "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"
local highPerformanceGUID = "<insert high performance GUID here>"
```

Save and restart you DCS server. Now this script is active.

## Remarks
- Please make sure your energy plans are set correctly.
- If this hook is loaded properly you can look into the dcs.log file and search for the lines with `[EnergyManagement]`.
- The server admin user (that connects by default) is ignored.

### Log-examples
``` log
2022-08-09 18:48:08.272 INFO LuaNET: [EnergyManagement] Loaded and ready!
2022-08-09 18:48:08.673 INFO LuaNET: [EnergyManagement] Admin connected, total clients 1
2022-08-09 18:48:08.673 INFO LuaNET: [EnergyManagement] No players connected - setting to energy saving / balanced
2022-08-09 18:48:51.917 INFO LuaNET: [EnergyManagement] Sword 1-4 | Yokas connected, total clients 2
2022-08-09 18:48:51.918 INFO LuaNET: [EnergyManagement] Player connected - setting to high performance
2022-08-09 18:49:55.550 INFO LuaNET: [EnergyManagement] Player disconnected, total clients 1
2022-08-09 18:49:55.550 INFO LuaNET: [EnergyManagement] No players connected - setting to energy saving / balanced
```

## GitHub
You will find this hook / script in the newest version over at [GitHub](https://github.com/Ayokas/DCS-Energy-Management).

0 comments on commit 3ee23c7

Please sign in to comment.