To work with Deepsy ecu service it is necessary to use DeepsyEcuManager located in com.gmv.its.deepsy.ecu for getting the initial EcuManager object.
EcuManager ecuManager = DeepsyEcuManager.getInstance();
DeepsyEcuManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
EcuManager ecuManager = DeepsyEcuManager.getInstance("192.168.1.10");
EcuManager 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 an Ecu object:
try (EcuManager ecuManager = DeepsyEcuManager.getInstance()){
Ecu ecu = ecuManager.getEcu();
} catch (EcuNotAvailableException e) {
e.printStackTrace();
} catch (EcuLowLevelException e) {
e.printStackTrace();
}
If this object is not inside a try with resources block, it is recommended to call to ecuManager.close() in order to finalize properly;
To detect service availability the DeepsyEcuManager 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 (EcuManager ecuManager = DeepsyEcuManager.getInstance()){
ecuManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}
Get Ecu protocol
package com.gmv.its.dpyjavaexamples.ecu;
import java.util.List;
import java.util.Arrays;
public class GetProtocolApp {
public static void main(String[] args) {
try (EcuManager ecuManager = DeepsyEcuManager.getInstance()){
Ecu ecu = ecuManager.getEcu();
EcuProtocol protocol = ecu.getCurrentProtocol();
System.out.println(protocol.getName());
} catch (EcuNotAvailableException e) {
e.printStackTrace();
} catch (EcuLowLevelException e) {
e.printStackTrace();
}
}
}
Get EPIDs parameters
package com.gmv.its.dpyjavaexamples.ecu;
import java.util.Arrays;
public class GetEPIDsApp {
public static void main(String[] args) {
try (EcuManager ecuManager = DeepsyEcuManager.getInstance()){
Ecu ecu = ecuManager.getEcu();
List<String> EPIDs = Arrays.asList("Engine_Temp");
List<EcuParameter> params = ecu.getParameter(EPIDs);
for (EcuParameter param : params) {
System.out.println(
"Timestamp: " + param.getTimestamp());
System.out.println(
"Id: " + param.getId());
System.out.println(
"Value: " + param.getValue());
System.out.println(
"Units: " + param.getUnits());
}
} catch (EcuNotAvailableException e) {
e.printStackTrace();
} catch (EcuLowLevelException e) {
e.printStackTrace();
}
}
}
Get Ecu available EPIDs
package com.gmv.its.dpyjavaexamples.ecu;
public class GetAvailableEPIDsApp {
public static void main(String[] args) {
try (EcuManager ecuManager = DeepsyEcuManager.getInstance()){
Ecu ecu = ecuManager.getEcu();
List<String> pids = ecu.getAvailableEPIDs();
for (String pid : pids) {
}
} catch (EcuNotAvailableException e) {
e.printStackTrace();
} catch (EcuLowLevelException e) {
e.printStackTrace();
}
}
}
Get Ecu Information
public class App {
public static void main(String[] args) {
EcuManager ecuManager = DeepsyEcuManager.getInstance();
try {
Ecu ecu = ecuManager.getEcu();
EcuAlarm alarm = new EcuAlarm(EcuAlarm.AlarmType.STATE_CHANGE,"Vehicle_Speed",50);
int id = ecu.setAlarm(alarm);
} catch (EcuNotAvailableException e) {
e.printStackTrace();
} catch (EcuLowLevelException e) {
e.printStackTrace();
}
ecuManager.close();
}
}
Subscribe to Ecu alarms
class MyEcuObserver extends EcuObserver {
@Override
public void notifyEcuAlarm(EcuAlarmEvent event) {
EcuAlarm alarm = ((EcuAlarmEvent) event).getAlarm();
System.out.println(
"EVENT EcuAlarmEvent: ->" + alarm.getType() +
", " + alarm.getParameterId() +
", " + alarm.getThreshold());
}
}
public class App {
public static void main(String[] args) {
EcuManager ecuManager = DeepsyEcuManager.getInstance();
try {
Ecu ecu = ecuManager.getEcu();
ecu.subscribe(new MyEcuObserver());
} catch (EcuNotAvailableException e) {
e.printStackTrace();
} catch (EcuLowLevelException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ecuManager.close();
}
}