schedule.java

来自「这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统」· Java 代码 · 共 667 行 · 第 1/2 页

JAVA
667
字号
                                                int yesterday = ((today - 1) + 7) % 7;                                                SortedSet sy = getEvents(yesterday);                                                current = (ScheduleEvent)sy.last();                    }                                        if ( current == null ) {                                            current = e;                    }                                        return current;                }            }                        //complain(LOG_NOTICE, CH_SCHEDULE, ts.getName() + ": exhausted");                        return current;                    } finally {                    if ( current == null ) {                            // VT: NOTE: This case has hopefully been taken care of, if                // it ever surfaces, has to be fixed                                throw new IllegalStateException("Unable to resolve the current event");            }        }    }        /**     * Check if there is a scheduling conflict.     *     * <p>     *     * When the schedule event is changed, there is a possibility that the     * new start time will be the same as the start time for other existing     * event. In this case, we get a problem when the events are run     * (undefined order) and next time the GUI starts (because the scheduler     * panel displays the events based on unique time).     *     * <p>     *     * In order to avoid this, the event start times must be unique. There's     * not too many events per day (four), and the solution is just to skip     * the time that is already occupied.     *     * @param start New desired start time.     *     * @param se Event being considered for rescheduling. It has to be     * excluded from consideration.     *     * @return true if there is a conflict, false if not.     */    public boolean checkConflict(long start, ScheduleEvent se) {            for ( Iterator i = eventSet[se.getDayOffset()].iterator(); i.hasNext(); ) {                    ScheduleEvent e = (ScheduleEvent)i.next();                        if ( e == se ) {                            continue;            }                        if ( e.getStartTime() == start ) {                            return true;            }        }            return false;    }        /**     * Verify the changes made to the schedule and reorder the events, if     * necessary.     */    public void reschedule() {            scheduler.reschedule();                // Now, there's a good chance that the order of events has changed        // since the reschedule was requested. Let's regenerate the event        // sets.                // VT: FIXME: See if this needs to be synchronized                for ( int day = 0; day < 7; day++ ) {                    SortedSet copy = new TreeSet();                        for ( Iterator i = eventSet[day].iterator(); i.hasNext(); ) {                            Object next = i.next();                //complain(LOG_WARNING, CH_SCHEDULE, "eventSet[" + day + "] next: " + next);                if ( !copy.add(next) ) {                                    for ( Iterator ci = copy.iterator(); ci.hasNext(); ) {                                            Object c = ci.next();                        complain(LOG_ERR, CH_SCHEDULE, "eventSet[" + day + "] copy: " + c);                    }                    throw new IllegalStateException("Copy contains the object: " + next);                }            }                        eventSet[day] = copy;        }    }        /**     * Save the changes to persistent configuration.     */    public synchronized void sync() {            String cfrootBase = getConfigurationRoot();                persistentConf = new TextConfiguration();        persistentConf.setDefaultConfiguration(getConfiguration());                for ( int day = 0; day < 7; day++ ) {                    SortedSet s = eventSet[day];            String cfrootDay = cfrootBase + ".day." + days[day];            String periods = "";                        for ( Iterator i = s.iterator(); i.hasNext(); ) {                            ScheduleEvent e = (ScheduleEvent)i.next();                String eventName = e.getName();                String cfroot = cfrootDay + ".period." + eventName;                                if ( !"".equals(periods) ) {                                    periods += ",";                }                                periods += eventName;                                persistentConf.put(cfroot + ".start_time", e.getStartTimeAsString());                persistentConf.put(cfroot + ".setpoint", Double.toString(e.getSetpoint()));                persistentConf.put(cfroot + ".setpoint.enabled", e.isOn() ? "true" : "false");                persistentConf.put(cfroot + ".dump_priority", Integer.toString(e.getDumpPriority()));            }                        persistentConf.put(cfrootDay + ".period", periods);        }                try {                    new ConfigurationFactory().storeText(persistentConf, scheduleURL);            complain(LOG_INFO, CH_SCHEDULE, "Stored settings to " + scheduleURL);                    } catch ( Throwable t ) {                    complain(LOG_ERR, CH_SCHEDULE, "Can't store persistent configuration to "            	+ scheduleURL            	+ ", cause:", t);        }    }        /**     * Copy the schedule for the day.     *     * @param source Day offset to copy from.     *     * @param target Day offset to copy to.     */    public synchronized void copy(int source, int target) {            if ( source == target ) {                    // No point in doing anything at all                        return;        }            SortedSet sourceSet = eventSet[source];        SortedSet targetSet = eventSet[target];                // Remove the target day's events from the schedule                for ( Iterator i = targetSet.iterator(); i.hasNext(); ) {                    ScheduleEvent se = (ScheduleEvent)i.next();            scheduler.remove(se);            i.remove();        }                for ( Iterator i= sourceSet.iterator(); i.hasNext(); ) {                    ScheduleEvent se = (ScheduleEvent)i.next();            ScheduleEvent targetEvent = new ScheduleEvent(target,                                                          se.getName(),                                                          se.getStartTime(),                                                          se.getSetpoint(),                                                          se.isOn(),                                                          se.isVoting(),                                                          se.getDumpPriority());                        targetSet.add(targetEvent);                        Task t = new Command(ts, targetEvent, targetEvent.getName());                        try {                            // VT: NOTE: Running a late task results in a mess                // when the periods from the different days overlap.                                scheduler.schedule(t);                            } catch ( Throwable x ) {                            complain(LOG_ERR, CH_SCHEDULE, x.getMessage());            }        }                if ( target == new GregorianCalendar().get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY ) {                    // That's today, have to rerun the schedule                        getCurrentEvent().run(ts);        }    }        /**     * Copy the schedule from this object to the target.     *     * Don't forget to retain the command target that the target schedule     * has.     */    public synchronized void copyTo(Schedule target) {            for ( int day = 0; day < 7; day++ ) {                    SortedSet targetSet = target.eventSet[day];                        // Remove the target events                        for ( Iterator i = targetSet.iterator(); i.hasNext(); ) {                            target.scheduler.remove((TaskDescriptor)i.next());                i.remove();            }                        // Now the target event set for the day is empty, and we can            // repopululate it                        for ( Iterator i = eventSet[day].iterator(); i.hasNext(); ) {                            ScheduleEvent se = (ScheduleEvent)i.next();                ScheduleEvent targetEvent = new ScheduleEvent(day,                                                              se.getName(),                                                              se.getStartTime(),                                                              se.getSetpoint(),                                                              se.isOn(),                                                              se.isVoting(),                                                              se.getDumpPriority());                                targetSet.add(targetEvent);                                Task t = new Command(target.ts, targetEvent, targetEvent.getName());                                try {                                    // VT: NOTE: Running a late task results in a mess                    // when the periods from the different days overlap.                                        target.scheduler.schedule(t);                                    } catch ( Throwable x ) {                                    complain(LOG_ERR, CH_SCHEDULE, x.getMessage());                }            }        }                target.sync();        target.getCurrentEvent().run(target.ts);    }        public void addListener(TaskListener tl) {            scheduler.addListener(tl);    }        public void removeListener(TaskListener tl) {            scheduler.removeListener(tl);    }        protected class EventComparator implements Comparator {            public int compare(Object o1, Object o2) {                    if ( o1 instanceof ScheduleEvent && o2 instanceof ScheduleEvent ) {                            return (int)(((ScheduleEvent)o1).getStartOffset() - ((ScheduleEvent)o2).getStartOffset());                        } else {                            throw new ClassCastException("Expecting ScheduleEvent");            }        }    }    protected class Command extends Task {            Thermostat ts;            Command(Thermostat ts, TaskDescriptor td, String message) {                    super(td, message);                        this.ts = ts;        }                 public void run() {                     ScheduleEvent e = (ScheduleEvent)getDescriptor();                        e.run(ts);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?