TimerTask 在午夜后停止 运行

TimerTask stops running after midnight

我有一小部分程序有一个定时器,每 15 分钟通过命令行 (fswebcam) 使用 USB 网络摄像头拍照。

代码是这样的:

public static final String HOME_DIR = System.getProperty("user.home") + "/";
public static final String PGP_DIR = HOME_DIR + "PGP/";

public static final String COLLECTION_DATA_DIR = PGP_DIR + "collectionData/";
public static final String SENSOR_CALIBRATION_DATA = PGP_DIR + "sensorCalibration/";
public static final String PICTURE_DIR = PGP_DIR + "pictures/";
public static final String ALARM_DIR = PGP_DIR + "alarms/";
private class PictureTakerTask extends TimerTask{
        Timer t;
        public void start(){
            if(t != null){
                t.cancel();
                t.purge();
                t = null;
                this.cancel();
            }
            t = new Timer(true);
            t.scheduleAtFixedRate(this, 0, 1000 * 60 * 15); //takes a picture every 15 minutes
        }

        public void stop(){
            if(running) return;
            if(t != null){
                t.cancel();
                t.purge();
                t = null;
            }
            this.cancel();
        }

        @Override
        public void run() {
            
            String filename = getPictureFilename();
            if(filename == null) return;
            Process p;
            try {
                CommIO.printLog("Taking a picture");
                String file;
                light.setGreenPWM(80);
                p = Runtime.getRuntime().exec("fswebcam -r 1920x1080 --no-banner -S 1 " + filename);
                p.waitFor();
                System.out.println ("picture taken with exit value: " + p.exitValue());
                p.destroy();
                light.setGreenPWM(0);
            } catch (Exception e) {
                e.printStackTrace();
                light.setGreenPWM(0);
            }
        }

        private String getPictureFilename(){
            //make folder if it doesn't exist already
            SimpleDateFormat justDate = new SimpleDateFormat("MM-dd-yy");
            String date = justDate.format(new Date());
            String s = PICTURE_DIR + date + "/";
            File picDir = new File(s);
            if(!picDir.exists()){
                if(picDir.mkdir()){
                    Alert alert = new Alert(AlertType.ERROR, "Couldn't make picture directory: " + s, ButtonType.OK);
                }
            }
            //find an unused filename
            String picFileName = s + "plant_01.jpg";
            File tempFile = new File(picFileName);
            int i = 1;
            while (tempFile.exists()) { //finds the next nonexistent name for data spreadsheet
                String num = i < 10 ? "0" + i : "" + i;
                picFileName = s + "plant_" + num + ".jpg";
                i++;
                tempFile = new File(picFileName);
                if(i > 10000) break;
            }
            return picFileName;
        }
    }

线程启动并工作正常,直到午夜。第二天会拍照失败。我一辈子都弄不明白为什么它会停止。如果我停止并重新启动任务,它将再次正常工作。如果我什至通过程序外的终端拍照,它会再次正常工作。它只会在第二天停止拍照。它甚至会创建第二天的文件夹,只是没有图片。没有错误消息(我能找到)。

有人对此有任何想法或经验吗?在你说之前,不,我不能使用 motion 或 Cron,因为我需要与 运行() 函数中的“light”对象同步。

您可以尝试将 11:59:59pm 的时间设置为 12:00:01am。我不确定这是否会解决它。一个 while 循环可以工作。

回答我自己的问题。简单的错误,实际上:

if(picDir.mkdir()){
    Alert alert = new Alert(AlertType.ERROR, "Couldn't make picture directory: " + s, ButtonType.OK);
}

忘了条件的否定。我通常 运行 程序一天不止一次检查其他更改,所以它第一次创建文件夹,使线程崩溃,并且 运行 在接下来的几次启动程序时都很好.但是当第二天到来时,它发出警报并大发脾气,因为它不在 FX 线程上。很难注意到,因为我什至忘了显示警报。

我喜欢 java-fx,但有时它会很挑剔。通过添加否定并将警报放入 Platform.runLater Runnable 来修复。