Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package fhffm.iot.gateway;
  2.  
  3. import org.eclipse.californium.core.CoapServer;
  4.  
  5.  
  6. import static org.eclipse.leshan.LwM2mId.*;
  7. import static org.eclipse.leshan.client.object.Security.*;
  8.  
  9. import java.net.InetAddress;
  10. import java.net.UnknownHostException;
  11. import java.util.List;
  12.  
  13. import org.eclipse.californium.core.network.config.NetworkConfig;
  14. import org.eclipse.leshan.client.californium.LeshanClient;
  15. import org.eclipse.leshan.client.californium.LeshanClientBuilder;
  16. import org.eclipse.leshan.client.object.Server;
  17. import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
  18. import org.eclipse.leshan.client.resource.ObjectsInitializer;
  19. import org.eclipse.leshan.core.model.LwM2mModel;
  20. import org.eclipse.leshan.core.model.ObjectLoader;
  21. import org.eclipse.leshan.core.model.ObjectModel;
  22. import org.eclipse.leshan.core.request.BindingMode;
  23. import org.eclipse.leshan.util.Hex;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26.  
  27.  
  28. import fhffm.iot.gateway.Collector.UltrasonicResource;
  29.  
  30. public class Gateway{
  31.  
  32.  
  33. //Declaration for Leshan client
  34. private static final Logger LOG = LoggerFactory.getLogger(Gateway.class);
  35.  
  36. private final static String[] modelPaths = new String[] { "3303.xml" };
  37.  
  38. private static final int OBJECT_ID_DISTANCE_SENSOR = 3303;
  39. private final static String DEFAULT_ENDPOINT = "Ultrasonic HC-SR04";
  40. // private final static String LeshanServerURI = "coaps://192.168.50.31:5684";
  41. private final static String LeshanServerURI = "coaps://leshan.eclipse.org:5684";
  42. private final static String pskIdentityDefault = "mypskid";
  43. private final static String pskKeyDefault = "aabbccdd";
  44.  
  45. public static void main(String[] args) {
  46. byte[] pskId = null;
  47. byte[] pskKey = null;
  48. String endpoint = null;
  49. String serverURI = null;
  50.  
  51. // //Start CoAP server for collecting data from device
  52. // //Initiate CoAP server on 5683 port
  53. // CoapServer server = new CoapServer();
  54. //
  55. // //Create new resource
  56. // server.add(new UltrasonicResource());
  57. //
  58. // //Start the server
  59. // server.start();
  60. //
  61.  
  62.  
  63. //Start Leshan client to send data to server
  64. try {
  65. endpoint = InetAddress.getLocalHost().getHostName();
  66. } catch (UnknownHostException e) {
  67. endpoint = DEFAULT_ENDPOINT;
  68. }
  69. serverURI = LeshanServerURI;
  70. pskId = pskIdentityDefault.getBytes();
  71. pskKey = Hex.decodeHex(pskKeyDefault.toCharArray());
  72.  
  73.  
  74. // Initialize model
  75. createAndStartLeshanClient(endpoint, serverURI, pskId, pskKey);
  76. }
  77.  
  78. public static void createAndStartLeshanClient(String endpoint, String serverURI, byte[] pskId, byte[] pskKey) {
  79.  
  80. // Initialize LWM2M model
  81. List<ObjectModel> models = ObjectLoader.loadDefault();
  82. models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));
  83.  
  84. // Initialize object list
  85. ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(models));
  86. initializer.setInstancesForObject(SECURITY, psk(serverURI, 123, pskId, pskKey));
  87. initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
  88.  
  89. initializer.setClassForObject(DEVICE, DeviceLwm2mObject.class);
  90. initializer.setInstancesForObject(OBJECT_ID_DISTANCE_SENSOR, new UltrasonicLwm2mObject());
  91. List<LwM2mObjectEnabler> enablers = initializer.create(SECURITY, SERVER, DEVICE, OBJECT_ID_DISTANCE_SENSOR);
  92. // Create client
  93. LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
  94. builder.setObjects(enablers);
  95. builder.setNetworkConfig(NetworkConfig.getStandard());
  96. final LeshanClient client = builder.build();
  97.  
  98. // Start the client
  99. client.start();
  100.  
  101. // De-register on shutdown and stop client.
  102. Runtime.getRuntime().addShutdownHook(new Thread() {
  103. @Override
  104. public void run() {
  105. client.destroy(true); // send de-registration request before destroy
  106. }
  107. });
  108. }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement