/** * Calculate the number of hour to wait until the first scheduled update check. This will only be * called if daily checking for config updates is enabled * * @return The number of hours to wait */ private int calcHoursToWait() { // The hours to wait is the number of hours until midnight tonight (24 // minus the current hour) plus the hour that the config says updates // should be return 24 - Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + configuration.getInt(CHECK_FOR_UPDATES_DAILY_TIME_PROP, 0); }
public TaskWrite(Map map1, Map m_max, Map m_min, DBCollection coll, ModbusSlaveSet slave) { try { synchronized (slave) { Calendar calener = Calendar.getInstance(); Date d1 = calener.getTime(); Date d2 = new Date(calener.getTime().getTime() - 5000); BasicDBObject b2 = new BasicDBObject(); b2.put("$gte", d2); b2.put("$lte", d1); DBCursor cursor = coll.find(new BasicDBObject("_id", b2)).sort(new BasicDBObject("_id", -1)).limit(1); while (cursor.hasNext()) { DBObject dbo = cursor.next(); Set set1 = map1.entrySet(); Iterator it1 = set1.iterator(); while (it1.hasNext()) { Map.Entry<String, Map<String, String>> entry = (Map.Entry<String, Map<String, String>>) it1.next(); Map<String, String> map2 = entry.getValue(); for (Iterator it2 = map2.entrySet().iterator(); it2.hasNext(); ) { Map.Entry<String, String> entry2 = (Map.Entry<String, String>) it2.next(); String name = entry2.getKey().toString(); String paramAddr = entry2.getValue().toString(); int fun = (int) (Double.parseDouble(paramAddr)); if (paramAddr.substring(0, 1).equals("4")) { double value = Double.parseDouble(dbo.get(name).toString()); double dmax = (Double) m_max.get(name); double dmin = (Double) m_min.get(name); double dValue = 0; if (value > dmax || value < dmin) { if (value >= 0) { dValue = 32000 * (int) (value / dmax); } if (value < 0) { dValue = -32000 * (int) (value / dmin); } // slave.getProcessImage(3).setInputRegister(fun % 10000, (short) dValue); } else { /// 参数超限报警 JOptionPane.showMessageDialog(null, "参数超限"); slave.stop(); } } if (paramAddr.substring(0, 1).equals("3")) { double value = Double.parseDouble(dbo.get(name).toString()); double dmax = (Double) m_max.get(name); double dmin = (Double) m_min.get(name); double dValue = 0; if (value > dmax || value < dmin) { if (value >= 0) { dValue = 32000 * (int) (value / dmax); } if (value < 0) { dValue = -32000 * (int) (value / dmin); } // slave.getProcessImage(3).setHoldingRegister(fun % 10000, (short) dValue); } else { /// 参数超限报警 JOptionPane.showMessageDialog(null, "参数超限"); slave.stop(); } ; } if (paramAddr.substring(0, 1).equals("2")) { String value = dbo.get(name).toString(); /// slave.getProcessImage(4).setInput(fun % 10000, Boolean.valueOf(value)); } if (paramAddr.substring(0, 1).equals("1")) { String value = dbo.get(name).toString(); // slave.getProcessImage(4).setCoil(fun % 10000, Boolean.valueOf(value)); } } } } } } catch (Exception ex) { } }
public TaskRead(Map map1, Map m_max, Map m_min, DBCollection coll, ModbusSlaveSet slave) { try { //// 此处只要点击开始就能保存数据 /// 方案:利用数据源发送单个布尔型变量(1,0)交替,链接成功,先判断是否链接成功,然后在保存数据 synchronized (slave) { Map map = new HashMap(); Set set1 = map1.entrySet(); Iterator it1 = set1.iterator(); while (it1.hasNext()) { Map.Entry<String, Map<String, String>> entry = (Map.Entry<String, Map<String, String>>) it1.next(); Map<String, String> map2 = entry.getValue(); for (Iterator it2 = map2.entrySet().iterator(); it2.hasNext(); ) { Map.Entry<String, String> entry2 = (Map.Entry<String, String>) it2.next(); String name = entry2.getKey().toString(); String paramAddr = entry2.getValue().toString(); int fun = (int) (Double.parseDouble(paramAddr)); if (paramAddr.substring(0, 1).equals("4")) { Short d4 = slave.getProcessImage(1).getInputRegister(fun % 10000); double dmax = (Double) m_max.get(name); double dmin = (Double) m_min.get(name); double dValue = 0; if (d4 >= 0) { dValue = dmax * d4 / 32000; } else { dValue = dmin * d4 / (-32000); } map.put(name, dValue); } if (paramAddr.substring(0, 1).equals("3")) { Short d3 = slave.getProcessImage(1).getHoldingRegister(fun % 10000); double dmax = (Double) m_max.get(name); double dmin = (Double) m_min.get(name); double dValue = 0; if (d3 >= 0) { dValue = dmax * d3 / 32000; } else { dValue = dmin * d3 / (-32000); } map.put(name, dValue); } if (paramAddr.substring(0, 1).equals("2")) { map.put(name, slave.getProcessImage(2).getInput(fun % 10000)); } if (paramAddr.substring(0, 1).equals("1")) { Boolean a = slave.getProcessImage(2).getCoil(fun % 10000 - 1); map.put(name, a); } } } Calendar calendar = Calendar.getInstance(); Date dd = calendar.getTime(); BasicDBObject doc = new BasicDBObject(); doc.put("_id", dd); Set set = map.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry<String, String> entry1 = (Map.Entry<String, String>) it.next(); doc.put(entry1.getKey(), entry1.getValue()); } coll.insert(doc); } } catch (Exception ex) { } }
public class GreenhouseScheduler { @SuppressWarnings("unused") private volatile boolean light = false; @SuppressWarnings("unused") private volatile boolean water = false; private String thermostat = "Day"; public synchronized String getThermostat() { return thermostat; } public synchronized void setThermostat(String value) { thermostat = value; } ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10); public void schedule(Runnable event, long delay) { scheduler.schedule(event, delay, TimeUnit.MILLISECONDS); } public void repeat(Runnable event, long initialDelay, long period) { scheduler.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MILLISECONDS); } class LightOn implements Runnable { public void run() { // Put hardware control code here to // physically turn on the light. System.out.println("Turning on lights"); light = true; } } class LightOff implements Runnable { public void run() { // Put hardware control code here to // physically turn off the light. System.out.println("Turning off lights"); light = false; } } class WaterOn implements Runnable { public void run() { // Put hardware control code here. System.out.println("Turning greenhouse water on"); water = true; } } class WaterOff implements Runnable { public void run() { // Put hardware control code here. System.out.println("Turning greenhouse water off"); water = false; } } class ThermostatNight implements Runnable { public void run() { // Put hardware control code here. System.out.println("Thermostat to night setting"); setThermostat("Night"); } } class ThermostatDay implements Runnable { public void run() { // Put hardware control code here. System.out.println("Thermostat to day setting"); setThermostat("Day"); } } class Bell implements Runnable { public void run() { System.out.println("Bing!"); } } class Terminate implements Runnable { public void run() { System.out.println("Terminating"); scheduler.shutdownNow(); // Must start a separate task to do this job, // since the scheduler has been shut down: new Thread() { public void run() { for (DataPoint d : data) System.out.println(d); } }.start(); } } // New feature: data collection static class DataPoint { final Calendar time; final float temperature; final float humidity; public DataPoint(Calendar d, float temp, float hum) { time = d; temperature = temp; humidity = hum; } public String toString() { return time.getTime() + String.format(" temperature: %1$.1f humidity: %2$.2f", temperature, humidity); } } private Calendar lastTime = Calendar.getInstance(); { // Adjust date to the half hour lastTime.set(Calendar.MINUTE, 30); lastTime.set(Calendar.SECOND, 00); } private float lastTemp = 65.0f; private int tempDirection = +1; private float lastHumidity = 50.0f; private int humidityDirection = +1; private Random rand = new Random(47); List<DataPoint> data = Collections.synchronizedList(new ArrayList<DataPoint>()); class CollectData implements Runnable { public void run() { System.out.println("Collecting data"); synchronized (GreenhouseScheduler.this) { // Pretend the interval is longer than it is: lastTime.set(Calendar.MINUTE, lastTime.get(Calendar.MINUTE) + 30); // One in 5 chances of reversing the direction: if (rand.nextInt(5) == 4) tempDirection = -tempDirection; // Store previous value: lastTemp = lastTemp + tempDirection * (1.0f + rand.nextFloat()); if (rand.nextInt(5) == 4) humidityDirection = -humidityDirection; lastHumidity = lastHumidity + humidityDirection * rand.nextFloat(); // Calendar must be cloned, otherwise all // DataPoints hold references to the same lastTime. // For a basic object like Calendar, clone() is OK. data.add(new DataPoint((Calendar) lastTime.clone(), lastTemp, lastHumidity)); } } } public static void main(String[] args) { GreenhouseScheduler gh = new GreenhouseScheduler(); gh.schedule(gh.new Terminate(), 5000); // Former "Restart" class not necessary: gh.repeat(gh.new Bell(), 0, 1000); gh.repeat(gh.new ThermostatNight(), 0, 2000); gh.repeat(gh.new LightOn(), 0, 200); gh.repeat(gh.new LightOff(), 0, 400); gh.repeat(gh.new WaterOn(), 0, 600); gh.repeat(gh.new WaterOff(), 0, 800); gh.repeat(gh.new ThermostatDay(), 0, 1400); gh.repeat(gh.new CollectData(), 500, 500); } } /* (Execute to see output) */ // :~
{ // Adjust date to the half hour lastTime.set(Calendar.MINUTE, 30); lastTime.set(Calendar.SECOND, 00); }
public String toString() { return time.getTime() + String.format(" temperature: %1$.1f humidity: %2$.2f", temperature, humidity); }
public JdbcLoader(Config config) { this.config = config; cal = Calendar.getInstance(); cal.setTimeZone(TimeZone.getTimeZone("EST")); logger = LoggerFactory.getLogger(JdbcLoader.class); }