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 * @see <a href="https://srcpd.sourceforge.net/srcp/srcp-084.html#TIME">SRCP TIME documentation</a> 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 } 046 047 @Override 048 public double getRate() { 049 // There is no way to request the current rate from the 050 // server, so we need to watch for rate in return messages 051 // and save the value. 052 return InstanceManager.getDefault(jmri.Timebase.class).getRate(); 053 } 054 055 /** 056 * Set and get the fast clock time For the default implementation,set time 057 * is ignored and getTime returns the time of the internal clock; 058 * Date format sent as yyyyDDD HH mm ss , YearJulDay Hour Minute Seconds 059 */ 060 @Override 061 public void setTime(Date now) { 062 // prepare to format the date as <Year><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 } 068 069 @Override 070 public Date getTime() { 071 // this requests the time, but it doesn't actually send 072 // the time from the server to the clock yet. 073 String text = "GET " + _memo.getBus() + " TIME"; 074 // create and send the message itself 075 _tc.sendSRCPMessage(new SRCPMessage(text), null); 076 077 return InstanceManager.getDefault(jmri.Timebase.class).getTime(); 078 } 079 080 /** 081 * Start and stop hardware fast clock Many hardware fast clocks continue to 082 * run indefinitely. This is provided for the case where the hardware clock 083 * can be stopped and started. 084 */ 085 @Override 086 public void startHardwareClock(Date now) { 087 setTime(now); 088 } 089 090 @Override 091 public void stopHardwareClock() { 092 } 093 094 /** 095 * Initialize the hardware fast clock Note: When the hardware clock control 096 * receives this, it should initialize those clock settings that are 097 * available on the hardware clock. Default implementation is to ignore this 098 * request. 099 */ 100 @Override 101 public void initializeHardwareClock(double rate, Date now, boolean getTime) { 102 } 103}