To work with Deepsy rtc service it is necessary to use DeepsyRtcManager located in com.gmv.its.deepsy.rtc for getting the initial RtcManager object.
RtcManager rtcManager = DeepsyRtcManager.getInstance();
DeepsyRtcManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
RtcManager rtcManager = DeepsyRtcManager.getInstance("192.168.1.10");
RtcManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a Rtc object:
try (RtcManager rtcManager = DeepsyRtcManager.getInstance()){
Rtc rtc = rtcManager.getRtc();
} catch (GeofencingNotAvailableException e) {
e.printStackTrace();
} catch (GeofencingLowLevelException e) {
e.printStackTrace();
}
If this object is not inside a try with resources block, it is recommended to call to rtcManager.close() in order to finalize properly;
To detect service availability the DeepsyRtcManager implements Manager interface which provides methods for getting availability and subscribing to availability events.
class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() +
" in "+
event.getManager().getIp() +
" "+
event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (RtcManager rtcManager = DeepsyRtcManager.getInstance()){
rtcManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}
Get Rtc get config values
import java.time.Instant;
public class App {
public static void main(String[] args) {
RtcManager rtcManager = DeepsyRtcManager.getInstance();
try {
Rtc rtc = rtcManager.getRtc();
ParamConfig config = rtc.getConfigValues();
System.out.println(config.getSyncTimeSeconds());
System.out.println(config.getMaxDiffTimeSeconds());
} catch (RtcNotAvailableException e) {
e.printStackTrace();
} catch (RtcLowLevelException e) {
e.printStackTrace();
}
rtcManager.close();
}
}
Get Rtc date and time
package com.gmv.its.dpyjavaexamples.rtc;
import java.time.Instant;
public class GetRtcDateTimeApp {
public static void main(String[] args) {
try (RtcManager rtcManager = DeepsyRtcManager.getInstance()) {
Rtc rtc = rtcManager.getRtc();
System.out.println(date.toString());
} catch (RtcNotAvailableException e) {
e.printStackTrace();
} catch (RtcLowLevelException e) {
e.printStackTrace();
}
}
}
Set Rtc synchronism time period in seconds
package com.gmv.its.dpyjavaexamples.rtc;
import java.time.Instant;
public class SetRtcSyncTimePeriodApp {
public static void main(String[] args) {
try (RtcManager rtcManager = DeepsyRtcManager.getInstance()) {
Rtc rtc = rtcManager.getRtc();
rtc.setSyncTime(5);
} catch (RtcNotAvailableException e) {
e.printStackTrace();
} catch (RtcLowLevelException e) {
e.printStackTrace();
}
}
}
Add a new date source that will be used to obtain a valid date an time for the system
package com.gmv.its.dpyjavaexamples.rtc;
import java.time.Instant;
public class AddRtcDateSourceApp {
public static void main(String[] args) {
try (RtcManager rtcManager = DeepsyRtcManager.getInstance()) {
Rtc rtc = rtcManager.getRtc();
String source_name = "gnss1";
rtc.addNewDateSource(source_name, new RtcDateSource(RtcDateSource.DateSourceType.GNSS,2,"127.0.0.1",false));
System.out.println(
"Date Source added correctly");
} catch (RtcNotAvailableException e) {
e.printStackTrace();
} catch (RtcLowLevelException e) {
e.printStackTrace();
}
}
}