001package jmri.jmrix.srcp;
002
003import java.util.Date;
004import jmri.InstanceManager;
005import jmri.implementation.DefaultClockControl;
006
007/**
008 * Class providing SRCP Clock Control to the SRCP client.
009 *
010 * @author Paul Bender Copyright (C) 2014
011 */
012public class SRCPClockControl extends DefaultClockControl {
013
014    SRCPBusConnectionMemo _memo = null;
015    SRCPTrafficController _tc = null;
016
017    public SRCPClockControl(SRCPBusConnectionMemo memo) {
018        _memo = memo;
019        _tc = _memo.getTrafficController();
020    }
021
022    /**
023     * Operational instance variables (not saved between runs)
024     */
025    /**
026     * Get name of hardware clock
027     */
028    @Override
029    public String getHardwareClockName() {
030        return ("SRCP Fast Clock");
031    }
032
033    /**
034     * Get and set the rate of the fast clock Note: The rate is an integer that
035     * multiplies the wall clock For example, a rate of 4 specifies that the
036     * fast clock runs 4 times faster than the wall clock. For the default
037     * implementation, setRate is ignored, and getRate returns the rate of the
038     * internal clock;
039     */
040    @Override
041    public void setRate(double newRate) {
042        String text = "INIT " + _memo.getBus() + " TIME 1 " + newRate;
043        // create and send the message itself
044        _tc.sendSRCPMessage(new SRCPMessage(text), null);
045        return;
046    }
047
048    @Override
049    public double getRate() {
050        // There is no way to request the current rate from the
051        // server, so we need to watch for rate in return messages
052        // and save the value.
053        return InstanceManager.getDefault(jmri.Timebase.class).getRate();
054    }
055
056    /**
057     * Set and get the fast clock time For the default implementation,set time
058     * is ignored and getTime returns the time of the internal clock;
059     */
060    @Override
061    public void setTime(Date now) {
062        // prepare to format the date as <JulDay> <Hour> <Minute> <Seconds>
063        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyDDD hh mm ss");
064        String text = "SET " + _memo.getBus() + " TIME " + sdf.format(now);
065        // create and send the message itself
066        _tc.sendSRCPMessage(new SRCPMessage(text), null);
067        return;
068    }
069
070    @Override
071    public Date getTime() {
072        // this requests the time, but it doesn't actually send
073        // the time from the server to the clock yet.
074        String text = "GET " + _memo.getBus() + " TIME";
075        // create and send the message itself
076        _tc.sendSRCPMessage(new SRCPMessage(text), null);
077
078        return InstanceManager.getDefault(jmri.Timebase.class).getTime();
079    }
080
081    /**
082     * Start and stop hardware fast clock Many hardware fast clocks continue to
083     * run indefinitely. This is provided for the case where the hardware clock
084     * can be stopped and started.
085     */
086    @Override
087    public void startHardwareClock(Date now) {
088        setTime(now);
089        return;
090    }
091
092    @Override
093    public void stopHardwareClock() {
094        return;
095    }
096
097    /**
098     * Initialize the hardware fast clock Note: When the hardware clock control
099     * receives this, it should initialize those clock settings that are
100     * available on the hardware clock. Default implementation is to ignore this
101     * request.
102     */
103    @Override
104    public void initializeHardwareClock(double rate, Date now, boolean getTime) {
105        return;
106    }
107}
108
109