iot tp3 coap

This commit is contained in:
JOLIMAITRE Matthieu 2024-04-11 18:14:35 +02:00
parent 0bb5ae732f
commit 1395fdd566
23 changed files with 575 additions and 0 deletions

3
iot/tp3/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
out
/resources.zip
/sujet_CoAP.pdf

6
iot/tp3/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{
"editor.formatOnType": true,
"[commonlisp]": {
"editor.wordSeparators": "`|;:'\",()"
}
}

29
iot/tp3/occitanium-client/.gitignore vendored Normal file
View file

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
iot/tp3/occitanium-client/.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/occitanium-client.iml" filepath="$PROJECT_DIR$/occitanium-client.iml" />
</modules>
</component>
</project>

6
iot/tp3/occitanium-client/.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>

View file

@ -0,0 +1,37 @@
#Californium CoAP Properties file
#Wed Apr 10 19:25:41 CEST 2024
ACK_RANDOM_FACTOR=1.5
ACK_TIMEOUT=2000
ACK_TIMEOUT_SCALE=2
CROP_ROTATION_PERIOD=2000
DEDUPLICATOR=DEDUPLICATOR_MARK_AND_SWEEP
DEFAULT_BLOCK_SIZE=512
DEFAULT_COAP_PORT=5683
DEFAULT_ENDPOINT_THREAD_COUNT=1
DEFAULT_LEISURE=5000
EXCHANGE_LIFECYCLE=247000
HTTP_CACHE_RESPONSE_MAX_AGE=86400
HTTP_CACHE_SIZE=32
HTTP_PORT=8080
HTTP_SERVER_SOCKET_BUFFER_SIZE=8192
HTTP_SERVER_SOCKET_TIMEOUT=100000
MARK_AND_SWEEP_INTERVAL=10000
MAX_MESSAGE_SIZE=1024
MAX_RETRANSMIT=4
MAX_TRANSMIT_WAIT=93000
NOTIFICATION_CHECK_INTERVAL=86400000
NOTIFICATION_CHECK_INTERVAL_COUNT=100
NOTIFICATION_MAX_AGE=128000
NOTIFICATION_REREGISTRATION_BACKOFF=2000
NSTART=1
PROBING_RATE=1.0
SERVER_THRESD_NUMER=8
UDP_CONNECTOR_DATAGRAM_SIZE=2000
UDP_CONNECTOR_LOG_PACKETS=false
UDP_CONNECTOR_OUT_CAPACITY=2147483647
UDP_CONNECTOR_RECEIVER_THREAD_COUNT=1
UDP_CONNECTOR_RECEIVE_BUFFER=0
UDP_CONNECTOR_SENDER_THREAD_COUNT=1
UDP_CONNECTOR_SEND_BUFFER=0
USE_RANDOM_MID_START=true
USE_RANDOM_TOKEN_START=true

View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/californium-core-1.0.0-SNAPSHOT.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/element-connector-1.0-SNAPSHOT.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View file

@ -0,0 +1,39 @@
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapHandler;
import org.eclipse.californium.core.CoapResponse;
public class Main {
public static void main(String[] args) throws InterruptedException {
var light_client = new CoapClient("coap://localhost:5683/light");
var lamp_client = new CoapClient("coap://localhost:5683/light/lamp");
while (true) {
var relation = light_client.observe(new LampUpdateHandler(lamp_client));
relation.wait();
}
}
}
class LampUpdateHandler implements CoapHandler {
static String ON = "On";
static String OFF = "Off";
CoapClient lamp_client;
public LampUpdateHandler(CoapClient lamp_client_) {
lamp_client = lamp_client_;
}
@Override
public void onLoad(CoapResponse coapResponse) {
var content = coapResponse.getResponseText();
var level = Integer.parseInt(content);
System.out.println("Light at " + level);
var state = OFF;
if (level < 5) state = ON;
System.out.println("Sending state " + state);
}
@Override
public void onError() {
System.err.println("Owno :s");
}
}

29
iot/tp3/occitanium-server/.gitignore vendored Normal file
View file

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
iot/tp3/occitanium-server/.idea/.gitignore generated vendored Normal file
View file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/occitanium-server.iml" filepath="$PROJECT_DIR$/occitanium-server.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
iot/tp3/occitanium-server/.idea/vcs.xml generated Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
</component>
</project>

View file

@ -0,0 +1,37 @@
#Californium CoAP Properties file
#Wed Apr 10 19:09:21 CEST 2024
ACK_RANDOM_FACTOR=1.5
ACK_TIMEOUT=2000
ACK_TIMEOUT_SCALE=2
CROP_ROTATION_PERIOD=2000
DEDUPLICATOR=DEDUPLICATOR_MARK_AND_SWEEP
DEFAULT_BLOCK_SIZE=512
DEFAULT_COAP_PORT=5683
DEFAULT_ENDPOINT_THREAD_COUNT=1
DEFAULT_LEISURE=5000
EXCHANGE_LIFECYCLE=247000
HTTP_CACHE_RESPONSE_MAX_AGE=86400
HTTP_CACHE_SIZE=32
HTTP_PORT=8080
HTTP_SERVER_SOCKET_BUFFER_SIZE=8192
HTTP_SERVER_SOCKET_TIMEOUT=100000
MARK_AND_SWEEP_INTERVAL=10000
MAX_MESSAGE_SIZE=1024
MAX_RETRANSMIT=4
MAX_TRANSMIT_WAIT=93000
NOTIFICATION_CHECK_INTERVAL=86400000
NOTIFICATION_CHECK_INTERVAL_COUNT=100
NOTIFICATION_MAX_AGE=128000
NOTIFICATION_REREGISTRATION_BACKOFF=2000
NSTART=1
PROBING_RATE=1.0
SERVER_THRESD_NUMER=8
UDP_CONNECTOR_DATAGRAM_SIZE=2000
UDP_CONNECTOR_LOG_PACKETS=false
UDP_CONNECTOR_OUT_CAPACITY=2147483647
UDP_CONNECTOR_RECEIVER_THREAD_COUNT=1
UDP_CONNECTOR_RECEIVE_BUFFER=0
UDP_CONNECTOR_SENDER_THREAD_COUNT=1
UDP_CONNECTOR_SEND_BUFFER=0
USE_RANDOM_MID_START=true
USE_RANDOM_TOKEN_START=true

View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/californium-core-1.0.0-SNAPSHOT.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/element-connector-1.0-SNAPSHOT.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View file

@ -0,0 +1,157 @@
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.server.resources.CoapExchange;
public class Main {
public static void main(String[] args) {
var server = new CoapServer(5683);
server.add(new HelloWorldResource());
server.add(new TimeResource());
server.add(new SubPathResource());
server.add(new ModifiableResource());
server.add(new DeletableResource());
server.add(new LightResource());
server.start();
}
}
class HelloWorldResource extends CoapResource {
public HelloWorldResource() {
super("hello-world");
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.CONTENT, "hello world");
}
}
class TimeResource extends CoapResource {
public TimeResource() {
super("time");
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.CONTENT, Long.toString(System.currentTimeMillis()));
}
}
class SubPathResource extends CoapResource {
public SubPathResource() {
super("subpath");
add(new AnotherResource());
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.CONTENT, "subpath");
}
}
class AnotherResource extends CoapResource {
public AnotherResource() {
super("another");
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.CONTENT, "another");
}
}
class ModifiableResource extends CoapResource {
private String value;
public ModifiableResource() {
super("modifiable");
value = "Start";
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.CONTENT, value);
}
@Override
public void handlePUT(CoapExchange exchange) {
value = exchange.getRequestText();
exchange.respond(CoAP.ResponseCode.CHANGED, value);
}
}
class DeletableResource extends CoapResource {
public DeletableResource() {
super("deletable");
}
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.DELETED, "Deleted resource.");
}
}
class LightResource extends CoapResource {
public LightResource() {
super("Light");
brightness = 6;
add(new LampResource());
setObservable(true);
}
Integer brightness;
@Override
public void handleGET(CoapExchange exchange) {
System.out.println("access detected");
exchange.respond(CoAP.ResponseCode.CONTENT, brightness.toString());
}
@Override
public void handlePUT(CoapExchange exchange) {
brightness = Integer.parseInt(exchange.getRequestText());
exchange.respond(CoAP.ResponseCode.CHANGED, brightness.toString());
}
}
enum LampState {
On("On"),
Off("Off");
private String text;
private LampState(String text_) {
text = text_;
}
@Override
public String toString() {
return text;
}
public static LampState fromString(String text_) {
if (text_.equals("On")) return LampState.On;
if (text_.equals("Off")) return LampState.Off;
return null;
}
}
class LampResource extends CoapResource {
private LampState state = LampState.On;
public LampResource() {
super("lamp");
}
@Override
public void handlePUT(CoapExchange exchange) {
var input = LampState.fromString(exchange.getRequestText());
if (input == null) return;
exchange.respond(CoAP.ResponseCode.CHANGED, state.toString());
}
}