我们如何保持机器清醒?

时间:2020-03-05 18:50:20  来源:igfitidea点击:

我有一个用Java编写的类似于服务器的软件,可以在Windows和OS X上运行。(它不是在服务器上运行,而是在普通用户的PC上像洪流客户端。)我希望该软件发出信号操作系统以在机器处于活动状态时使其保持唤醒状态(防止其进入睡眠模式)。

当然,我不希望有一个跨平台的解决方案,但我希望我的应用程序可以生成一些非常小的C程序/脚本,以通知OS保持清醒。

有任何想法吗?

解决方案

回答

在计时器内运行命令,例如对服务器执行ping操作。

回答

在服务器上禁用电源管理会更容易吗?可能有人争辩说服务器不应该进入省电模式?

回答

我有一种非常强力的技术,可以将鼠标在x方向上移动1点,然后每3分钟返回一次。

也许我有一个更优雅的解决方案,但这是一个快速解决方案。

回答

在Windows上,使用SystemParametersInfo函数。这是瑞士军队风格的功能,可让我们获取/设置各种系统设置。

例如,要禁用屏幕关闭功能:

SystemParametersInfo( SPI_SETPOWEROFFACTIVE, 0, NULL, 0 );

只要确保完成后将其设置回...

回答

我只是要做一个移动鼠标的功能(或者下载免费的应用程序)。优雅,但容易。

回答

我使用此代码来防止工作站锁定。目前仅将其设置为每分钟移动一次鼠标,不过我们可以轻松进行调整。

这是一个hack,不是一个优雅的解决方案。

import java.awt.*;
import java.util.*;
public class Hal{

    public static void main(String[] args) throws Exception{
        Robot hal = new Robot();
        Random random = new Random();
        while(true){
            hal.delay(1000 * 60);
            int x = random.nextInt() % 640;
            int y = random.nextInt() % 480;
            hal.mouseMove(x,y);
        }
    }
}

回答

来回移动鼠标的所有建议是否会使用户疯狂?我知道我会在隔离后立即删除所有会执行此操作的应用程序。

回答

我已经使用pmset来控制Mac上的睡眠模式已有一段时间了,并且很容易集成。这是一个粗略的示例,说明了如何从Java调用该程序以禁用/启用睡眠模式。请注意,我们需要root特权才能运行pmset,因此需要它们来运行此程序。

import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * Disable sleep mode (record current setting beforehand), and re-enable sleep
 * mode. Works with Mac OS X using the "pmset" command.
 */
public class SleepSwitch {

    private int sleepTime = -1;

    public void disableSleep() throws IOException {
        if (sleepTime != -1) {
            // sleep time is already recorded, assume sleep is disabled
            return;
        }

        // query pmset for the current setting
        Process proc = Runtime.getRuntime().exec("pmset -g");
        BufferedInputStream is = new BufferedInputStream(proc.getInputStream());
        StringBuffer output = new StringBuffer();
        int c;
        while ((c = is.read()) != -1) {
            output.append((char) c);
        }
        is.close();

        // parse the current setting and store the sleep time
        String outString = output.toString();
        String setting = outString.substring(outString.indexOf(" sleep\t")).trim();
        setting = setting.substring(7, setting.indexOf(" ")).trim();
        sleepTime = Integer.parseInt(setting);

        // set the sleep time to zero (disable sleep)
        Runtime.getRuntime().exec("pmset sleep 0");
    }

    public void enableSleep() throws IOException {
        if (sleepTime == -1) {
            // sleep time is not recorded, assume sleep is enabled
            return;
        }

        // set the sleep time to the previously stored value
        Runtime.getRuntime().exec("pmset sleep " + sleepTime);

        // reset the stored sleep time
        sleepTime = -1;
    }
}

回答

我听说过有人得到大号低音炮,然后用导管将盒盖拍到顶部的故事。然后,我们可以将鼠标放在框中并调出音乐。最好是带有很多低音的东西,可以使鼠标四处移动。

回答

我们可以使用Caffeine caffiene程序使工作站保持清醒状态。我们可以通过os X中的open命令运行该程序。