diff --git a/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar b/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar index 115c1fe..24986e7 100644 Binary files a/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar and b/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar differ diff --git a/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT.jar b/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT.jar index 1789232..a9531b0 100644 Binary files a/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT.jar and b/leshan-bsserver-demo/target/leshan-bsserver-demo-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-client-cf/target/classes/META-INF/MANIFEST.MF b/leshan-client-cf/target/classes/META-INF/MANIFEST.MF index bc9b343..e919d98 100644 --- a/leshan-client-cf/target/classes/META-INF/MANIFEST.MF +++ b/leshan-client-cf/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861529939 +Bnd-LastModified: 1671049511996 Build-Jdk-Spec: 19 Bundle-Description: A transport implementation for leshan client based o n CoAP Californium diff --git a/leshan-client-cf/target/leshan-client-cf-2.0.0-SNAPSHOT.jar b/leshan-client-cf/target/leshan-client-cf-2.0.0-SNAPSHOT.jar index d2a7a9f..2c9ca9c 100644 Binary files a/leshan-client-cf/target/leshan-client-cf-2.0.0-SNAPSHOT.jar and b/leshan-client-cf/target/leshan-client-cf-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-client-core/target/classes/META-INF/MANIFEST.MF b/leshan-client-core/target/classes/META-INF/MANIFEST.MF index 5abdba9..bd501df 100644 --- a/leshan-client-core/target/classes/META-INF/MANIFEST.MF +++ b/leshan-client-core/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861529703 +Bnd-LastModified: 1671049511731 Build-Jdk-Spec: 19 Bundle-Description: A LWM2M client implementation which abstracts transp ort layer. A transport implementation like "leshan-client-cf" is needed diff --git a/leshan-client-core/target/leshan-client-core-2.0.0-SNAPSHOT.jar b/leshan-client-core/target/leshan-client-core-2.0.0-SNAPSHOT.jar index 016ef9c..5673a03 100644 Binary files a/leshan-client-core/target/leshan-client-core-2.0.0-SNAPSHOT.jar and b/leshan-client-core/target/leshan-client-core-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/DemandResponse.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/DemandResponse.java index cc75cd6..ad70ef4 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/DemandResponse.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/DemandResponse.java @@ -42,95 +42,96 @@ public class DemandResponse extends BaseInstanceEnabler { // Static values for resource items private static final int RES_TOTAL_ALLOWED_PEAK_ROOM_POWER = 30005; private static final List supportedResources = - Arrays.asList( - RES_TOTAL_ALLOWED_PEAK_ROOM_POWER - ); + Arrays.asList( + RES_TOTAL_ALLOWED_PEAK_ROOM_POWER + ); // Variables storing current values. - // 0..500 + // 0..500 private int vTotalAllowedPeakRoomPower = 300; - private JLabel glTotalAllowedPeakRoomPower; - private JLabel gvTotalAllowedPeakRoomPower; - private JTextField tfTotalAllowedPeakRoomPower; - private JFrame guiFrame; - - public DemandResponse() { - // Automatically generated GUI code. - guiFrame = new JFrame(); - guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - guiFrame.setTitle("Demand Response"); - - // Total Allowed Peak Room Power - glTotalAllowedPeakRoomPower = new JLabel(); - glTotalAllowedPeakRoomPower.setText("Total Allowed Peak Room Power"); - gvTotalAllowedPeakRoomPower = new JLabel(); - gvTotalAllowedPeakRoomPower.setText(Integer.toString(vTotalAllowedPeakRoomPower)); - tfTotalAllowedPeakRoomPower = new JTextField(); - tfTotalAllowedPeakRoomPower.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - String sValue = (String)tfTotalAllowedPeakRoomPower.getText(); - setTotalAllowedPeakRoomPower(Integer.valueOf(sValue)); - } - }); - - // Create layout of labels, inputs and values. - GridLayout layout = new GridLayout(0,3,10,10); - guiFrame.getContentPane().setLayout(layout); - Container guiPane = guiFrame.getContentPane(); - guiPane.add(glTotalAllowedPeakRoomPower); - guiPane.add(tfTotalAllowedPeakRoomPower); - guiPane.add(gvTotalAllowedPeakRoomPower); - guiFrame.pack(); - // Code to make the frame visible. - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - guiFrame.setVisible(true); - } - }); - } - - @Override - public synchronized ReadResponse read(ServerIdentity identity, int resourceId) { - switch (resourceId) { - case RES_TOTAL_ALLOWED_PEAK_ROOM_POWER: - return ReadResponse.success(resourceId, vTotalAllowedPeakRoomPower); - default: - return super.read(identity, resourceId); + private JLabel glTotalAllowedPeakRoomPower; + private JLabel gvTotalAllowedPeakRoomPower; + private JTextField tfTotalAllowedPeakRoomPower; + private JFrame guiFrame; + + public DemandResponse() { + // Automatically generated GUI code. + guiFrame = new JFrame(); + guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + guiFrame.setTitle("Demand Response"); + + // Total Allowed Peak Room Power + glTotalAllowedPeakRoomPower = new JLabel(); + glTotalAllowedPeakRoomPower.setText("Total Allowed Peak Room Power"); + gvTotalAllowedPeakRoomPower = new JLabel(); + gvTotalAllowedPeakRoomPower.setText(Integer.toString(vTotalAllowedPeakRoomPower)); + tfTotalAllowedPeakRoomPower = new JTextField(); + tfTotalAllowedPeakRoomPower.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + String sValue = (String) tfTotalAllowedPeakRoomPower.getText(); + setTotalAllowedPeakRoomPower(Integer.valueOf(sValue)); + } + }); + + // Create layout of labels, inputs and values. + GridLayout layout = new GridLayout(0, 3, 10, 10); + guiFrame.getContentPane().setLayout(layout); + Container guiPane = guiFrame.getContentPane(); + guiPane.add(glTotalAllowedPeakRoomPower); + guiPane.add(tfTotalAllowedPeakRoomPower); + guiPane.add(gvTotalAllowedPeakRoomPower); + guiFrame.pack(); + // Code to make the frame visible. + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + guiFrame.setVisible(true); + } + }); } - } - @Override - public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { - switch (resourceId) { - default: - return super.write(identity, replace, resourceId,value); + @Override + public synchronized ReadResponse read(ServerIdentity identity, int resourceId) { + switch (resourceId) { + case RES_TOTAL_ALLOWED_PEAK_ROOM_POWER: + return ReadResponse.success(resourceId, vTotalAllowedPeakRoomPower); + default: + return super.read(identity, resourceId); + } } - } - @Override - public synchronized ExecuteResponse execute(ServerIdentity identity, int resourceId, Arguments arguments) { - switch (resourceId) { - default: - return super.execute(identity, resourceId,arguments); + @Override + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { + switch (resourceId) { + default: + return super.write(identity, replace, resourceId, value); + } } - } - - @Override - public List getAvailableResourceIds(ObjectModel model) { - return supportedResources; - } - - // Called before the object is registered, don't fire. - public void configure(int peakroompower) { - vTotalAllowedPeakRoomPower = peakroompower; - gvTotalAllowedPeakRoomPower.setText(Integer.toString(vTotalAllowedPeakRoomPower)); } - - private synchronized void setTotalAllowedPeakRoomPower(int value) { - if (vTotalAllowedPeakRoomPower != value) { - vTotalAllowedPeakRoomPower = value; - gvTotalAllowedPeakRoomPower.setText(Integer.toString(vTotalAllowedPeakRoomPower)); - fireResourceChange(RES_TOTAL_ALLOWED_PEAK_ROOM_POWER); + + @Override + public synchronized ExecuteResponse execute(ServerIdentity identity, int resourceId, Arguments arguments) { + switch (resourceId) { + default: + return super.execute(identity, resourceId, arguments); + } + } + + @Override + public List getAvailableResourceIds(ObjectModel model) { + return supportedResources; + } + + // Called before the object is registered, don't fire. + public void configure(int peakroompower) { + vTotalAllowedPeakRoomPower = peakroompower; + gvTotalAllowedPeakRoomPower.setText(Integer.toString(vTotalAllowedPeakRoomPower)); + } + + private synchronized void setTotalAllowedPeakRoomPower(int value) { + if (vTotalAllowedPeakRoomPower != value) { + vTotalAllowedPeakRoomPower = value; + gvTotalAllowedPeakRoomPower.setText(Integer.toString(vTotalAllowedPeakRoomPower)); + fireResourceChange(RES_TOTAL_ALLOWED_PEAK_ROOM_POWER); + } } - } -} +} \ No newline at end of file diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/Luminaire.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/Luminaire.java index 55de90a..d48ce38 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/Luminaire.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/Luminaire.java @@ -5,14 +5,12 @@ import java.math.BigDecimal; import java.math.RoundingMode; -import java.util.Random; -import java.util.List; -import java.util.Arrays; +import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import java.util.Scanner; +import jdk.internal.net.http.frame.SettingsFrame; import org.eclipse.leshan.client.servers.ServerIdentity; import org.eclipse.leshan.client.resource.BaseInstanceEnabler; import org.eclipse.leshan.core.model.ResourceModel.Type; @@ -45,181 +43,205 @@ public class Luminaire extends BaseInstanceEnabler { private static final int RES_PEAK_POWER = 30003; private static final int RES_DIM_LEVEL = 30004; private static final List supportedResources = - Arrays.asList( - RES_POWER - , RES_TYPE - , RES_PEAK_POWER - , RES_DIM_LEVEL - ); + Arrays.asList( + RES_POWER + , RES_TYPE + , RES_PEAK_POWER + , RES_DIM_LEVEL + ); // Variables storing current values. private boolean vPower = false; // LED, Halogen - private String vType = ""; + private String vType; - private long vPeakPower = 0; + private long vPeakPower; // 0..100 private long vDimLevel = 0; - // - // 2IMN15: TODO : fill in - // - // Add state variables for the user interface. - private JLabel glPower; - private JLabel gvPower; - private JLabel glDimLevel; - private JLabel gvDimLevel; - private JLabel glInstantPower; - private JLabel gvInstantPower; - private JFrame guiFrame; - + /** + * Variables for values in GUI + * Power, Dim Level, Type, Peak Power and frame declaration itself + */ + private final JLabel gvPower; + private final JLabel gvDimLevel; + + private final JLabel gvType; + + private final JLabel gvPeakPower; + private final JFrame guiFrame; + public Luminaire() { - // - // 2IMN15: TODO : fill in - // - // Create an interface to display the luminaire state. - // Options: - // * GUI (see DemandResponse.java for an Swing/AWT example) - // * external application - // * ... - // - // Automatically generated GUI code. - guiFrame = new JFrame(); - guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - guiFrame.setTitle("Luminaire"); - - // Power - glPower = new JLabel(); - glPower.setText("Power"); - gvPower = new JLabel(); - gvPower.setText(Boolean.toString(vPower)); - - //Dim Level - glDimLevel = new JLabel(); - glDimLevel.setText("Dim Level"); - gvDimLevel = new JLabel(); - gvDimLevel.setText(Long.toString(vDimLevel)); - - //Instant Power - glInstantPower = new JLabel(); - glInstantPower.setText("Instant Power"); - gvInstantPower = new JLabel(); - gvInstantPower.setText(Long.toString(vDimLevel * vPeakPower)); - - // Create layout of labels, inputs and values. - GridLayout layout = new GridLayout(0,6,10,10); - guiFrame.getContentPane().setLayout(layout); - Container guiPane = guiFrame.getContentPane(); - guiPane.add(glPower); - guiPane.add(gvPower); - guiPane.add(glDimLevel); - guiPane.add(gvDimLevel); - guiPane.add(glInstantPower); - guiPane.add(gvInstantPower); - - guiFrame.pack(); - // Code to make the frame visible. - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - guiFrame.setVisible(true); - } - }); + // + // 2IMN15: TODO : fill in + // + // Create an interface to display the luminaire state. + // Options: + // * GUI (see DemandResponse.java for an Swing/AWT example) + // * external application + // * ... + // + // Automatically generated GUI code. + guiFrame = new JFrame(); + guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + guiFrame.setTitle("Luminaire"); + + // Power + // + // 2IMN15: TODO : fill in + // + // Add state variables for the user interface. + JLabel glPower = new JLabel(); + + // Power field (TRUE/FALSE corresponding to ON/OFF) + glPower.setText("Power"); + gvPower = new JLabel(); + gvPower.setText(Boolean.toString(vPower)); + + //Dim Level (0-100) + JLabel glDimLevel = new JLabel(); + glDimLevel.setText("Dim Level"); + gvDimLevel = new JLabel(); + gvDimLevel.setText(Long.toString(vDimLevel)); + + //Type of luminaire + JLabel glType = new JLabel(); + glType.setText("Type"); + gvType = new JLabel(); + + //Peak power of luminaire + JLabel glPeakPower = new JLabel(); + glPeakPower.setText("Peak Power"); + gvPeakPower = new JLabel(); + + // Create layout of labels, inputs and values. + GridLayout layout = new GridLayout(0, 8, 10, 10); + guiFrame.getContentPane().setLayout(layout); + Container guiPane = guiFrame.getContentPane(); + guiPane.add(glPower); + guiPane.add(gvPower); + guiPane.add(glDimLevel); + guiPane.add(gvDimLevel); + guiPane.add(glType); + guiPane.add(gvType); + guiPane.add(glPeakPower); + guiPane.add(gvPeakPower); + + guiFrame.pack(); + // Code to make the frame visible. + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + guiFrame.setVisible(true); + } + }); } @Override public synchronized ReadResponse read(ServerIdentity identity, int resourceId) { - switch (resourceId) { - case RES_POWER: - return ReadResponse.success(resourceId, vPower); - case RES_TYPE: - return ReadResponse.success(resourceId, vType); - case RES_PEAK_POWER: - return ReadResponse.success(resourceId, vPeakPower); - case RES_DIM_LEVEL: - return ReadResponse.success(resourceId, vDimLevel); - default: - return super.read(identity, resourceId); - } + switch (resourceId) { + case RES_POWER: + return ReadResponse.success(resourceId, vPower); + case RES_TYPE: + return ReadResponse.success(resourceId, vType); + case RES_PEAK_POWER: + return ReadResponse.success(resourceId, vPeakPower); + case RES_DIM_LEVEL: + return ReadResponse.success(resourceId, vDimLevel); + default: + return super.read(identity, resourceId); + } } - + @Override public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { - switch (resourceId) { - case RES_POWER: - // vPower = (Boolean) value.getValue(); - // fireResourceChange(resourceId); - setPower((Boolean) value.getValue()); - return WriteResponse.success(); - case RES_DIM_LEVEL: - // vDimLevel = (Integer) value.getValue(); - // fireResourceChange(resourceId); - setDimLevel((Long) value.getValue()); - return WriteResponse.success(); - default: - return super.write(identity, replace, resourceId,value); - } + switch (resourceId) { + case RES_POWER: + //vPower = (Boolean) value.getValue(); + //fireResourceChange(resourceId); + setPower((Boolean) value.getValue()); + return WriteResponse.success(); + case RES_DIM_LEVEL: + // vDimLevel = (Integer) value.getValue(); + // fireResourceChange(resourceId); + setDimLevel((Long) value.getValue()); + return WriteResponse.success(); + case RES_TYPE: + // case for luminaire type added by us + setType((String) value.getValue()); + return WriteResponse.success(); + case RES_PEAK_POWER: + // case for peak power added by us + setPeakPower((long) value.getValue()); + System.out.println(value.getValue()); + return WriteResponse.success(); + default: + return super.write(identity, replace, resourceId, value); + } } - + @Override public synchronized ExecuteResponse execute(ServerIdentity identity, int resourceId, Arguments arguments) { - switch (resourceId) { - default: - return super.execute(identity, resourceId,arguments); - } + return super.execute(identity, resourceId, arguments); } - + @Override public List getAvailableResourceIds(ObjectModel model) { - return supportedResources; + return supportedResources; } - + // Configure before registration, don't fire. public void configure(String lumtype, long peakpower) { - vType = lumtype; - vPeakPower = peakpower; - System.out.println(vType + " " + vPeakPower); + vType = lumtype; + // Set text here + gvType.setText(lumtype); + // Set text here + vPeakPower = peakpower; + gvPeakPower.setText(String.valueOf(peakpower)); + System.out.println(vType + " " + vPeakPower); } - + private synchronized void setPower(boolean value) { - if (vPower != value) { - vPower = value; - // - // 2IMN15: TODO : fill in - // - // RoomControl has change the power. - // Update the UI. - gvPower.setText(Boolean.toString(value)); - fireResourceChange(RES_POWER); - } + if (vPower != value) { + vPower = value; + // + // 2IMN15: TODO : fill in + // + // RoomControl has changed the power. + // Update the UI. + gvPower.setText(Boolean.toString(value)); + fireResourceChange(RES_POWER); + } } - + private synchronized void setType(String value) { - if (vType != value) { - vType = value; - fireResourceChange(RES_TYPE); - } + if (!Objects.equals(vType, value)) { + vType = value; + gvType.setText(value); + fireResourceChange(RES_TYPE); + } } private synchronized void setPeakPower(long value) { - if (vPeakPower != value) { - vPeakPower = value; - fireResourceChange(RES_PEAK_POWER); - } + if (vPeakPower != value) { + vPeakPower = value; + gvPeakPower.setText(Long.toString(value)); + fireResourceChange(RES_PEAK_POWER); + } } private synchronized void setDimLevel(long value) { - if (vDimLevel != value) { - vDimLevel = value; - // - // 2IMN15: TODO : fill in - // - // RoomControl has change the dim level. - // Update the UI. - gvDimLevel.setText(Long.toString(value)); - fireResourceChange(RES_DIM_LEVEL); - } + if (vDimLevel != value) { + vDimLevel = value; + // + // 2IMN15: TODO : fill in + // + // RoomControl has changed the dim level. + // Update the UI. + gvDimLevel.setText(Long.toString(value)); + fireResourceChange(RES_DIM_LEVEL); + } } } diff --git a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/PresenceDetector.java b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/PresenceDetector.java index efc1e4f..17502e6 100644 --- a/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/PresenceDetector.java +++ b/leshan-client-demo/src/main/java/org/eclipse/leshan/client/demo/PresenceDetector.java @@ -46,123 +46,134 @@ public class PresenceDetector extends BaseInstanceEnabler { private static final int RES_POWER = 30000; private static final int RES_PRESENCE = 30001; private static final List supportedResources = - Arrays.asList( - RES_POWER - , RES_PRESENCE - ); + Arrays.asList( + RES_POWER + , RES_PRESENCE + ); // Variables storing current values. + /** + * Variable to store the power of the Presence Detecotr + *

+ * Not used in current code + */ private boolean vPower = false; + /** + * Variable to store the status of the presence + * Changes state by user interaction with buttons + */ private boolean vPresence = false; // // 2IMN15: TODO : fill in // // Add state variables for interaction with the user (GUI, CLI, sensor.) - private JFrame guiFrame; - private JButton btnProvokePresence; - private JButton btnCancelPresence; - - + /** + * Declare GUI frame + */ + private final JFrame guiFrame; + + public PresenceDetector() { - // - // 2IMN15: TODO : fill in - // - // Create an interface to enable presence detection - // Options: - // * GUI (see DemandResponse.java for an Swing/AWT example) - // * external application - // * ... - // - // Call "setPresence(bool)" to inform observers. - - // Automatically generated GUI code. - guiFrame = new JFrame(); - guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - guiFrame.setTitle("Presence Detector"); - - // Create button that mimicks human presence detection - btnProvokePresence = new JButton("Provoke Presence"); - btnProvokePresence.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - setPresence(true); - System.out.println("Presence provocked!"); - } - }); - - btnCancelPresence = new JButton("Cancel Presence"); - btnCancelPresence.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - setPresence(false); - System.out.println("Presence cancelled"); - } - }); - - // Create layout of labels, inputs and values. - GridLayout layout = new GridLayout(0,2,10,10); - guiFrame.getContentPane().setLayout(layout); - Container guiPane = guiFrame.getContentPane(); - guiPane.add(btnProvokePresence); - guiPane.add(btnCancelPresence); - guiFrame.pack(); - // Code to make the frame visible. - java.awt.EventQueue.invokeLater(new Runnable() { - public void run() { - guiFrame.setVisible(true); - } - }); + // + // 2IMN15: TODO : fill in + // + // Create an interface to enable presence detection + // Options: + // * GUI (see DemandResponse.java for an Swing/AWT example) + // * external application + // * ... + // + // Call "setPresence(bool)" to inform observers. + + // Automatically generated GUI code. + guiFrame = new JFrame(); + guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + guiFrame.setTitle("Presence Detector"); + + // Create button that mimics human presence detection + JButton btnProvokePresence = new JButton("Provoke Presence"); + // Action listener for button + btnProvokePresence.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + // Set presence to true + setPresence(true); + // Debug info + System.out.println("Presence provoked!"); + } + }); + + // Create button that mimics human presence deetecion + JButton btnCancelPresence = new JButton("Cancel Presence"); + // Action listener for button + btnCancelPresence.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + // set presence to false + setPresence(false); + // Debug info + System.out.println("Presence cancelled"); + } + }); + + // Create layout of labels, inputs and values. + GridLayout layout = new GridLayout(0, 2, 10, 10); + guiFrame.getContentPane().setLayout(layout); + Container guiPane = guiFrame.getContentPane(); + guiPane.add(btnProvokePresence); + guiPane.add(btnCancelPresence); + guiFrame.pack(); + // Code to make the frame visible. + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + guiFrame.setVisible(true); + } + }); } @Override public synchronized ReadResponse read(ServerIdentity identity, int resourceId) { - switch (resourceId) { - case RES_POWER: - return ReadResponse.success(resourceId, vPower); - case RES_PRESENCE: - return ReadResponse.success(resourceId, vPresence); - default: - return super.read(identity, resourceId); - } + switch (resourceId) { + case RES_POWER: + return ReadResponse.success(resourceId, vPower); + case RES_PRESENCE: + return ReadResponse.success(resourceId, vPresence); + default: + return super.read(identity, resourceId); + } } - + @Override public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { - switch (resourceId) { - case RES_POWER: - // vPower = (Boolean) value.getValue(); - // fireResourceChange(resourceId); - setPower((Boolean) value.getValue()); - return WriteResponse.success(); - default: - return super.write(identity, replace, resourceId,value); - } + if (resourceId == RES_POWER) {// vPower = (Boolean) value.getValue(); + // fireResourceChange(resourceId); + setPower((Boolean) value.getValue()); + return WriteResponse.success(); + } + return super.write(identity, replace, resourceId, value); } @Override public synchronized ExecuteResponse execute(ServerIdentity identity, int resourceId, Arguments arguments) { - switch (resourceId) { - default: - return super.execute(identity, resourceId,arguments); - } + return super.execute(identity, resourceId, arguments); } - + @Override public List getAvailableResourceIds(ObjectModel model) { - return supportedResources; + return supportedResources; } private synchronized void setPower(boolean value) { - if (vPower != value) { - vPower = value; - fireResourceChange(RES_POWER); - } + if (vPower != value) { + vPower = value; + fireResourceChange(RES_POWER); + } } - + private synchronized void setPresence(boolean value) { - if (vPresence != value) { - vPresence = value; - fireResourceChange(RES_PRESENCE); - } + if (vPresence != value) { + vPresence = value; + fireResourceChange(RES_PRESENCE); + } } - + } diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/DemandResponse.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/DemandResponse.class index f8b58f8..70b595b 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/DemandResponse.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/DemandResponse.class differ diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire$1.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire$1.class index 40e0059..003ad3f 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire$1.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire$1.class differ diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire.class index 00f1936..ab87711 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/Luminaire.class differ diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$1.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$1.class index 37038d4..5d6b098 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$1.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$1.class differ diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$2.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$2.class index 2429b29..b3f23f8 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$2.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$2.class differ diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$3.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$3.class index 6002e62..d70aa27 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$3.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector$3.class differ diff --git a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector.class b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector.class index 39116df..e76dace 100644 Binary files a/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector.class and b/leshan-client-demo/target/classes/org/eclipse/leshan/client/demo/PresenceDetector.class differ diff --git a/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar b/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar index 22ded82..6aae721 100644 Binary files a/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar and b/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar differ diff --git a/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-sources.jar b/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-sources.jar index 1a37cb8..8ae3f5c 100644 Binary files a/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-sources.jar and b/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT-sources.jar differ diff --git a/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT.jar b/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT.jar index 4f4fdf6..46f6a4d 100644 Binary files a/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT.jar and b/leshan-client-demo/target/leshan-client-demo-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-client-demo/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/leshan-client-demo/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index f253685..e69de29 100644 --- a/leshan-client-demo/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/leshan-client-demo/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1 +0,0 @@ -org/eclipse/leshan/client/demo/Luminaire$1.class diff --git a/leshan-core-cf/target/classes/META-INF/MANIFEST.MF b/leshan-core-cf/target/classes/META-INF/MANIFEST.MF index ed3dc11..d7e7f6f 100644 --- a/leshan-core-cf/target/classes/META-INF/MANIFEST.MF +++ b/leshan-core-cf/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861527863 +Bnd-LastModified: 1671049509739 Build-Jdk-Spec: 19 Bundle-Description: Shared classes between server and client which depen ds on CoAP Californium diff --git a/leshan-core-cf/target/leshan-core-cf-2.0.0-SNAPSHOT.jar b/leshan-core-cf/target/leshan-core-cf-2.0.0-SNAPSHOT.jar index 445026e..5756cf1 100644 Binary files a/leshan-core-cf/target/leshan-core-cf-2.0.0-SNAPSHOT.jar and b/leshan-core-cf/target/leshan-core-cf-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-core-demo/target/classes/leshan.properties b/leshan-core-demo/target/classes/leshan.properties index eab39e6..4eeaef5 100644 --- a/leshan-core-demo/target/classes/leshan.properties +++ b/leshan-core-demo/target/classes/leshan.properties @@ -1,4 +1,4 @@ # value are set at build time, see pom.xml version=2.0.0-SNAPSHOT -commitid=8762056001aa3c1793cf1a27ae84a1b01dea4dd1 -timestamp=1670861530834 \ No newline at end of file +commitid=68b12d2a1990939d5d015bb107f50ea85827cca0 +timestamp=1671049512892 \ No newline at end of file diff --git a/leshan-core-demo/target/leshan-core-demo-2.0.0-SNAPSHOT.jar b/leshan-core-demo/target/leshan-core-demo-2.0.0-SNAPSHOT.jar index 4a0dd35..eefdfc2 100644 Binary files a/leshan-core-demo/target/leshan-core-demo-2.0.0-SNAPSHOT.jar and b/leshan-core-demo/target/leshan-core-demo-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-core/target/classes/META-INF/MANIFEST.MF b/leshan-core/target/classes/META-INF/MANIFEST.MF index 208917f..d37ecdd 100644 --- a/leshan-core/target/classes/META-INF/MANIFEST.MF +++ b/leshan-core/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861526719 +Bnd-LastModified: 1671049508561 Build-Jdk-Spec: 19 Bundle-Description: Core classes for implementing Lightweight M2M Bundle-License: http://www.eclipse.org/legal/epl-v20.html, http://www.ec diff --git a/leshan-core/target/leshan-core-2.0.0-SNAPSHOT-tests.jar b/leshan-core/target/leshan-core-2.0.0-SNAPSHOT-tests.jar index b70ad22..ce36e64 100644 Binary files a/leshan-core/target/leshan-core-2.0.0-SNAPSHOT-tests.jar and b/leshan-core/target/leshan-core-2.0.0-SNAPSHOT-tests.jar differ diff --git a/leshan-core/target/leshan-core-2.0.0-SNAPSHOT.jar b/leshan-core/target/leshan-core-2.0.0-SNAPSHOT.jar index 7dc547e..cabd45b 100644 Binary files a/leshan-core/target/leshan-core-2.0.0-SNAPSHOT.jar and b/leshan-core/target/leshan-core-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-core/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/leshan-core/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index 0b49298..e69de29 100644 --- a/leshan-core/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/leshan-core/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1,25 +0,0 @@ -org/eclipse/leshan/senml/json/jackson/SenMLJsonRecordSerDes$1.class -org/eclipse/leshan/core/node/codec/json/LwM2mNodeJsonDecoder$2.class -org/eclipse/leshan/core/request/argument/Arguments$1.class -org/eclipse/leshan/core/node/codec/tlv/LwM2mNodeTlvEncoder$1.class -org/eclipse/leshan/core/node/codec/senml/LwM2mNodeSenMLDecoder$2.class -org/eclipse/leshan/core/node/codec/cbor/LwM2mNodeCborEncoder$1.class -org/eclipse/leshan/core/node/codec/tlv/LwM2mNodeTlvDecoder$1.class -org/eclipse/leshan/core/node/codec/opaque/LwM2mNodeOpaqueEncoder$1.class -org/eclipse/leshan/core/node/LwM2mResourceInstance$1.class -org/eclipse/leshan/core/node/codec/text/LwM2mNodeTextEncoder$1.class -org/eclipse/leshan/core/node/LwM2mSingleResource$1.class -org/eclipse/leshan/core/request/BindingMode$1.class -org/eclipse/leshan/core/node/LwM2mNodeUtil$1.class -org/eclipse/leshan/core/json/jackson/JsonArrayEntrySerDes$1.class -org/eclipse/leshan/core/util/datatype/LwM2mValueUtil$1.class -org/eclipse/leshan/core/node/LwM2mMultipleResource$1.class -org/eclipse/leshan/core/model/DefaultObjectModelValidator$1.class -org/eclipse/leshan/senml/cbor/upokecenter/SenMLCborPackSerDes$1.class -org/eclipse/leshan/core/tlv/TlvEncoder$1.class -org/eclipse/leshan/core/node/codec/text/LwM2mNodeTextDecoder$1.class -org/eclipse/leshan/core/node/codec/json/LwM2mNodeJsonEncoder$1.class -org/eclipse/leshan/core/node/codec/cbor/LwM2mNodeCborDecoder$1.class -org/eclipse/leshan/core/node/codec/senml/LwM2mNodeSenMLEncoder$1.class -org/eclipse/leshan/core/model/ResourceModel$1.class -org/eclipse/leshan/core/node/TimestampedLwM2mNodes$1.class diff --git a/leshan-server-cf/target/classes/META-INF/MANIFEST.MF b/leshan-server-cf/target/classes/META-INF/MANIFEST.MF index f920e7f..eca4a15 100644 --- a/leshan-server-cf/target/classes/META-INF/MANIFEST.MF +++ b/leshan-server-cf/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861529013 +Bnd-LastModified: 1671049510879 Build-Jdk-Spec: 19 Bundle-Description: A transport implementation for leshan server based o n CoAP Californium diff --git a/leshan-server-cf/target/leshan-server-cf-2.0.0-SNAPSHOT.jar b/leshan-server-cf/target/leshan-server-cf-2.0.0-SNAPSHOT.jar index 0d573ca..e769306 100644 Binary files a/leshan-server-cf/target/leshan-server-cf-2.0.0-SNAPSHOT.jar and b/leshan-server-cf/target/leshan-server-cf-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-server-core-demo/target/leshan-server-core-demo-2.0.0-SNAPSHOT.jar b/leshan-server-core-demo/target/leshan-server-core-demo-2.0.0-SNAPSHOT.jar index 0102e3c..e99c4ac 100644 Binary files a/leshan-server-core-demo/target/leshan-server-core-demo-2.0.0-SNAPSHOT.jar and b/leshan-server-core-demo/target/leshan-server-core-demo-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-server-core/target/classes/META-INF/MANIFEST.MF b/leshan-server-core/target/classes/META-INF/MANIFEST.MF index 784fdb7..09b8686 100644 --- a/leshan-server-core/target/classes/META-INF/MANIFEST.MF +++ b/leshan-server-core/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861528528 +Bnd-LastModified: 1671049510395 Build-Jdk-Spec: 19 Bundle-Description: A LWM2M server implementation which abstracts transp ort layer. A transport implementation like "leshan-server-cf" is needed diff --git a/leshan-server-core/target/leshan-server-core-2.0.0-SNAPSHOT.jar b/leshan-server-core/target/leshan-server-core-2.0.0-SNAPSHOT.jar index cef75f8..e0f6ef2 100644 Binary files a/leshan-server-core/target/leshan-server-core-2.0.0-SNAPSHOT.jar and b/leshan-server-core/target/leshan-server-core-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-server-demo/src/main/java/org/course/RoomControl.java b/leshan-server-demo/src/main/java/org/course/RoomControl.java index 6ea5ef1..85a4535 100644 --- a/leshan-server-demo/src/main/java/org/course/RoomControl.java +++ b/leshan-server-demo/src/main/java/org/course/RoomControl.java @@ -5,15 +5,10 @@ package org.course; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.io.PrintWriter; +import java.util.*; import org.eclipse.leshan.server.californium.LeshanServer; +import org.eclipse.leshan.server.registration.Deregistration; import org.eclipse.leshan.server.registration.Registration; import org.eclipse.leshan.core.response.WriteResponse; import org.eclipse.leshan.core.response.ReadResponse; @@ -21,11 +16,10 @@ import org.eclipse.leshan.core.request.WriteRequest; import org.eclipse.leshan.core.request.ReadRequest; import org.eclipse.leshan.core.request.ObserveRequest; -import org.eclipse.leshan.core.request.WriteRequest.Mode; import org.eclipse.leshan.core.node.LwM2mResource; import org.eclipse.leshan.core.node.LwM2mPath; import org.eclipse.leshan.core.observation.SingleObservation; - +import org.eclipse.leshan.server.registration.RegistrationServiceImpl; public class RoomControl { @@ -39,10 +33,45 @@ public class RoomControl { // // Declare variables to keep track of the state of the room. // - static int maxRoomPeakPower = 0; - static Map powerMap = new HashMap(); - static ArrayList luminaireIDList = new ArrayList<>(); - + /** + * Global variable to keep track of the maximum possible peak power in the room + * Add all the peak powers of each individual luminaire at registration + * And remove it at deregistration + */ + static int maxRoomPeakPower; + /** + * Global variable to keep track of each luminare (by regID) and its associated + * peak power + */ + static Map powerMap; + /** + * Global array list to keep track of all the registrations of luminaires + */ + static ArrayList registrationLuminaire; + /** + * Global variable to keep track of the dim level of each of the luminaires + */ + static float RoomDimLevel = 0; + /** + * Global boolean variable to keep track of the status of the presence detector + */ + static boolean presence; + /** + * Global variable to keep track of the given power budget from DemandResponse + */ + static int powerBudget; + /** + * Global array list to keep track of the allowed types a luminaire can be + * Will be used to check if a luminaire is of a given type + * If a luminaire type is not present here, it will not be registered + */ + static ArrayList allowedType; + + /** + * Function used for initialization of all required variables + * + * @param server the Leshan Server variable + */ public static void Initialize(LeshanServer server) { // Register the LWM2M server object for future use lwServer = server; @@ -50,6 +79,15 @@ public static void Initialize(LeshanServer server) { // 2IMN15: TODO : fill in // // Initialize the state variables. + powerMap = new HashMap(); + registrationLuminaire = new ArrayList<>(); + // Initialize max room peak power to 0 (corresponding to 0 luminaires + // being registered) + maxRoomPeakPower = 0; + allowedType = new ArrayList<>(); + // Allowed types - can be extended + allowedType.add("LED"); + allowedType.add("Halogen"); } @@ -60,37 +98,223 @@ public static void Initialize(LeshanServer server) { // * set the power flag of all luminaires. // * show the status of the room. + /** + * handleDimLevel() function + *

+ * It is used to disseminate for each registered luminare the global dim level + */ + public static void handleDimLevel() { + for (Registration registration : registrationLuminaire) { + writeInteger(registration, + Constants.LUMINAIRE_ID, + 0, + Constants.RES_DIM_LEVEL, + (int) RoomDimLevel); + } + } + + /** + * handlePower() function + *

+ * It is used to disseminate for each registered luminare the global power status + * It is adjusted based on the presence global variable + * That variable can be only true or false, depending on what is being received from + * the presence detector + */ + public static void handlePower() { + for (Registration registration : registrationLuminaire) { + writeBoolean(registration, + Constants.LUMINAIRE_ID, + 0, + Constants.RES_POWER, + presence); + } + } + + /** + * handleType() function + *

+ * Function used to disseminate back to the GUI the received parameter for type + * Function does not take into account the allowed types + * It will show exactly the type that is given in the CLI command + *

+ * NOTE: This is not required and could have been implemented in the GUI directly + * This allows for further functionality of possibly changing the type of the luminaire + * from the GUI of the RoomControl itself + * How useful that would be is up to discussion + * + * @param registration of type Registration + * @param type of type String + */ + public static void handleType(Registration registration, String type) { + System.out.println(type); + writeString(registration, + Constants.LUMINAIRE_ID, + 0, + Constants.RES_TYPE, + type); + } + + /** + * handlePeakPower() function + *

+ * Function used to disseminate back to the GUI the received parameter for peakPower + * Function does not take into account the allowed types + * It will show exactly the type that is given in the CLI command + *

+ * NOTE: This is not required and could have been implemented in the GUI directly + * This allows for further functionality of possibly changing the peak power of the luminaire + * from the GUI of the RoomControl itself + * How useful that would be is up to discussion + * + * @param registration of type Registration + * @param peakPower of type int + */ + public static void handlePeakPower(Registration registration, int peakPower) { + writeInteger(registration, + Constants.LUMINAIRE_ID, + 0, + Constants.RES_PEAK_POWER, + peakPower); + } + + /** + * handleAllowedTypes() function + *

+ * Function used to manage deregistration for types that are not allowed + * + * @param registration of type Registration + * @param type of type String + */ + public static void handleAllowedTypes(Registration registration, String type) { + if (!allowedType.contains(type)) { + // Code according to https://github.com/eclipse/leshan/issues/901 + // How to Deregister a client from Server #901 + RegistrationServiceImpl registrationService = ((RegistrationServiceImpl) lwServer.getRegistrationService()); + final Deregistration deregistration = registrationService.getStore().removeRegistration(registration.getId()); + registrationService.fireUnregistered(deregistration.getRegistration(), deregistration.getObservations(), null); + } + } + + /** + * luminaireRegistrationMonitor() function + *

+ * Used to monitor the state variables for registration and deregistration of luminaires + *

+ * When a luminaire is registered it is added to the array list keeping track of all + * luminaire registrations. Its peak power is added to the max room peak power and + * the hash map is completed with the key,value pair of regID,peakPower + *

+ * When a luminaire is deregistered it is removed from the array list keeping track of all + * luminaire registrations. The hash map is queried for the respective peakPower of the + * current regID. This peakPower is removed from the total max room peak power. The hash map + * is updated by removing the respective key,value pair of regID,peakPower + * + * @param registration of type Registration + * @param peakPower of type Integer + * @param method of type String + */ + public static void luminaireRegistrationMonitor(Registration registration, Integer peakPower, String method) { + if (Objects.equals(method, "ADD LUMINAIRE")) { + registrationLuminaire.add(registration); + maxRoomPeakPower += peakPower; + powerMap.put(registration.getId(), peakPower); + } else if (Objects.equals(method, "REMOVE LUMINAIRE")) { + maxRoomPeakPower -= powerMap.get(registration.getId()); + powerMap.remove(registration.getId()); + registrationLuminaire.remove(registration); + } + + } + /** + * calculateDimLevel() function + *

+ * Function used to calculate the global dim level of all the luminaires + * + * @param newPowerBudget of type int + */ + public static void calculateDimLevel(int newPowerBudget) { + // if the power budget is larger than 500, keep it at 500 + if (newPowerBudget > 500) { + newPowerBudget = 500; + } + // if the power budget is larger or equal than 0, calculate the dim level + // if the power budget is smaller than 0, then the power budget + // has not been updated + if (newPowerBudget >= 0) { + powerBudget = newPowerBudget; + RoomDimLevel = ((float) newPowerBudget / (float) maxRoomPeakPower) * 100; + // If the dim level is larger than 100, set it at 100 (the max possible) + if (RoomDimLevel > 100) { + RoomDimLevel = 100; + } + } + } + + /** + * calculatePresence() function + *

+ * Used to set the global presence state based on the queried presence state + * from the presence detector + * + * @param newPresenceState of type int + */ + public static void calculatePresence(int newPresenceState) { + if (newPresenceState == 1) { + presence = Boolean.TRUE; + } else if (newPresenceState == 0) { + presence = Boolean.FALSE; + } + } + + /** + * Function to handle Registration of all components + * + * @param registration of type Registration + */ public static void handleRegistration(Registration registration) { // Check which objects are available. Map supportedObject = registration.getSupportedObject(); if (supportedObject.get(Constants.PRESENCE_DETECTOR_ID) != null) { - System.out.println("Presence Detector " + registration.getEndpoint()); + System.out.println("Presence Detector " + registration.getEndpoint()); // // 2IMN15: TODO : fill in // - boolean presence = registerPresenceDetector(registration); + // Set the initial global presence state + presence = registerPresenceDetector(registration); } if (supportedObject.get(Constants.LUMINAIRE_ID) != null) { + // Get the type of the luminaire + String type = registerTypeLuminaire(registration); + // check if the type is allowed + handleAllowedTypes(registration, type); + // disseminate the type back to the GUI + handleType(registration, type); + System.out.println("Luminaire " + registration.getEndpoint()); - luminaireIDList.add(registration.getEndpoint()); // // 2IMN15: TODO : fill in // // Process the registration of a new Luminaire. - int peakPower = registerPeakPowerLuminaire(registration); - maxRoomPeakPower += peakPower; - powerMap.put(registration.getId(), peakPower); - System.out.println(maxRoomPeakPower); - String type = registerTypeLuminaire(registration); - boolean power = registerPowerLuminaire(registration); - int dimLevel = registerDimLevelLuminaire(registration); + // Get the peak power of the luminaire + int peakPower = registerPeakPowerLuminaire(registration); + // Disseminate the peak power back to the GUI + handlePeakPower(registration, peakPower); + + // Handle the registration of the luminaire + luminaireRegistrationMonitor(registration, peakPower, "ADD LUMINAIRE"); + // Calculate the dim level + calculateDimLevel(powerBudget); + // Disseminate to the components the global dim level and power state + handleDimLevel(); + handlePower(); } if (supportedObject.get(Constants.DEMAND_RESPONSE_ID) != null) { @@ -99,13 +323,22 @@ public static void handleRegistration(Registration registration) { // The registerDemandResponse() method contains example code // on how handle a registration. // - int powerBudget = registerDemandResponse(registration); + // Get the initial power budget from demand response + powerBudget = registerDemandResponse(registration); + // Calculate the dim level based on this power budget + calculateDimLevel(powerBudget); } // 2IMN15: don't forget to update the other luminaires. } - + /** + * handleDeregistration() function + *

+ * Handles the deregistration of all possible components + * + * @param registration of type Registration + */ public static void handleDeregistration(Registration registration) { // // 2IMN15: TODO : fill in @@ -116,12 +349,18 @@ public static void handleDeregistration(Registration registration) { registration.getSupportedObject(); if (supportedObject.get(Constants.PRESENCE_DETECTOR_ID) != null) { - System.out.println("Presence Detector Left " + registration.getEndpoint()); + System.out.println("Presence Detector Left " + registration.getEndpoint()); } if (supportedObject.get(Constants.LUMINAIRE_ID) != null) { - maxRoomPeakPower -= powerMap.get(registration.getId()); - System.out.println(maxRoomPeakPower); + // Handle the deregistration of a luminaire, along with updating all global variables + luminaireRegistrationMonitor(registration, null, "REMOVE LUMINAIRE"); + // Recalculate the dim level based on the current power budget + calculateDimLevel(powerBudget); + // Handle the dim level and power state upon deregistration of a luminaire + // Used to update both in real time after removal. + handleDimLevel(); + handlePower(); System.out.println("Luminaire Left " + registration.getEndpoint()); } @@ -131,9 +370,17 @@ public static void handleDeregistration(Registration registration) { } + /** + * Observe the response of each of the components + * + * @param observation of type SingleObservation + * @param registration of type Registration + * @param response of type ObserveResponse + * @throws InterruptedException for a RuntimeException caused in EventServlet + */ public static void handleObserveResponse(SingleObservation observation, Registration registration, - ObserveResponse response) { + ObserveResponse response) throws InterruptedException { if (registration != null && observation != null && response != null) { // // 2IMN15: TODO : fill in @@ -145,11 +392,21 @@ public static void handleObserveResponse(SingleObservation observation, // registration.getEndpoint() // observation.getPath() - // For processing an update of the Demand Response object. // It contains some example code. int newPowerBudget = observedDemandResponse(observation, response); + // Calculate dim level based on the new power budget + calculateDimLevel(newPowerBudget); + // Handle the dim level based on the new power budget + handleDimLevel(); + + // Get the new presence state from the presence detector + int newPresenceState = observedPresenceDetector(observation, response); + // Calculate the global presence variable based on the new observation + calculatePresence(newPresenceState); + // Handle the global power of luminaires based on the new presence state + handlePower(); } } @@ -157,7 +414,12 @@ public static void handleObserveResponse(SingleObservation observation, // Support functions for reading and writing resources of // certain types. - // Returns the current power budget. + /** + * Returns the current power budget. + * + * @param registration of type Registration + * @return power budget + */ private static int registerDemandResponse(Registration registration) { int powerBudget = readInteger(registration, Constants.DEMAND_RESPONSE_ID, @@ -183,13 +445,19 @@ private static int registerDemandResponse(Registration registration) { return powerBudget; } + /** + * Function that returns the type of luminaire (what is given in the CLI) + * + * @param registration of type Registration + * @return type of luminare as string + */ private static String registerTypeLuminaire(Registration registration) { String type = readString(registration, Constants.LUMINAIRE_ID, 0, Constants.RES_TYPE); System.out.println("Type of luminaire is " + type); - // Observe the total budget information for updates. + // Observe the type of luminaire for initialization try { ObserveRequest obRequest = new ObserveRequest(Constants.LUMINAIRE_ID, @@ -203,17 +471,24 @@ private static String registerTypeLuminaire(Registration registration) { System.out.println(">>ObserveRequest null << "); } } catch (Exception e) { - System.out.println("Observe request failed for Demand Response."); + System.out.println("Observe request failed for Luminaire Type Response."); } return type; } + + /** + * Function that returns the peak power of luminaire (what is given in the CLI) + * + * @param registration of type Registration + * @return peak power as int + */ private static int registerPeakPowerLuminaire(Registration registration) { int peakPower = readInteger(registration, Constants.LUMINAIRE_ID, 0, Constants.RES_PEAK_POWER); System.out.println("Peak power of luminaire is " + peakPower); - // Observe the total budget information for updates. + // Observe the total peak power of luminaire for registration; try { ObserveRequest obRequest = new ObserveRequest(Constants.LUMINAIRE_ID, @@ -227,66 +502,24 @@ private static int registerPeakPowerLuminaire(Registration registration) { System.out.println(">>ObserveRequest null << "); } } catch (Exception e) { - System.out.println("Observe request failed for Demand Response."); + System.out.println("Observe request failed for Luminaire Peak Power Response."); } return peakPower; } - private static boolean registerPowerLuminaire(Registration registration) { - boolean power = readBoolean(registration, - Constants.LUMINAIRE_ID, - 0, - Constants.RES_POWER); - System.out.println("Power state of luminaire is " + power); - // Observe the total budget information for updates. - try { - ObserveRequest obRequest = - new ObserveRequest(Constants.LUMINAIRE_ID, - 0, - Constants.RES_POWER); - System.out.println(">>ObserveRequest created << "); - ObserveResponse coResponse = - lwServer.send(registration, obRequest, 1000); - System.out.println(">>ObserveRequest sent << "); - if (coResponse == null) { - System.out.println(">>ObserveRequest null << "); - } - } catch (Exception e) { - System.out.println("Observe request failed for Demand Response."); - } - return power; - } - private static int registerDimLevelLuminaire(Registration registration) { - int dimLevel = readInteger(registration, - Constants.LUMINAIRE_ID, - 0, - Constants.RES_DIM_LEVEL); - System.out.println("Dim Level of Luminaire is " + dimLevel); - // Observe the total budget information for updates. - try { - ObserveRequest obRequest = - new ObserveRequest(Constants.LUMINAIRE_ID, - 0, - Constants.RES_DIM_LEVEL); - System.out.println(">>ObserveRequest created << "); - ObserveResponse coResponse = - lwServer.send(registration, obRequest, 1000); - System.out.println(">>ObserveRequest sent << "); - if (coResponse == null) { - System.out.println(">>ObserveRequest null << "); - } - } catch (Exception e) { - System.out.println("Observe request failed for Demand Response."); - } - return dimLevel; - } + /** + * Function that returns the presence status of the presence detector + * + * @param registration of type Registration + * @return presence as boolean + */ private static boolean registerPresenceDetector(Registration registration) { boolean presence = readBoolean(registration, Constants.PRESENCE_DETECTOR_ID, 0, Constants.RES_PRESENCE); System.out.println("Presence Detector is currently in state" + presence); - // Observe the total budget information for updates. + // Observe the presence information of presence detector for updates. try { ObserveRequest obRequest = new ObserveRequest(Constants.PRESENCE_DETECTOR_ID, @@ -300,13 +533,22 @@ private static boolean registerPresenceDetector(Registration registration) { System.out.println(">>ObserveRequest null << "); } } catch (Exception e) { - System.out.println("Observe request failed for Demand Response."); + System.out.println("Observe request failed for Presence Response."); } return presence; } // If the response contains a new power budget, it returns that value. // Otherwise, it returns -1. + + /** + * If the response contains a new power budget, it returns that value. + * Otherwise, it returns -1. + * + * @param observation of type SingleObservation + * @param response of type ObserveResponse + * @return integer + */ private static int observedDemandResponse(SingleObservation observation, ObserveResponse response) { // Alternative code: @@ -318,7 +560,7 @@ private static int observedDemandResponse(SingleObservation observation, String strValue = ((LwM2mResource) response.getContent()).getValue().toString(); try { int newPowerBudget = Integer.parseInt(strValue); - + System.out.println(newPowerBudget); return newPowerBudget; } catch (Exception e) { System.out.println("Exception in reading demand response:" + e.getMessage()); @@ -327,6 +569,34 @@ private static int observedDemandResponse(SingleObservation observation, return -1; } + /** + * If the response contains a new presence state, it returns that boolean (0,1) + * Otherwise, it returns -1. + * + * @param observation of type SingleObservation + * @param response of type ObserveResponse + * @return integer \in {-1,0,1} + */ + private static int observedPresenceDetector(SingleObservation observation, + ObserveResponse response) { + LwM2mPath obsPath = observation.getPath(); + if ((obsPath.getObjectId() == Constants.PRESENCE_DETECTOR_ID) && + (obsPath.getResourceId() == Constants.RES_PRESENCE)) { + String strValue = ((LwM2mResource) response.getContent()).getValue().toString(); + try { + boolean newPresenceState = Boolean.parseBoolean(strValue); + if (newPresenceState) { + return 1; + } else { + return 0; + } + } catch (Exception e) { + System.out.println("Exception in reading presence detection response:" + e.getMessage()); + } + } + return -1; + } + private static int readInteger(Registration registration, int objectId, int instanceId, int resourceId) { try { @@ -368,6 +638,15 @@ private static String readString(Registration registration, int objectId, int in } } + /** + * New function based on readString/readInteger to read a boolean from a resource + * + * @param registration of type Registration + * @param objectId of type int + * @param instanceId of type int + * @param resourceId of type int + * @return boolean + */ private static Boolean readBoolean(Registration registration, int objectId, int instanceId, int resourceId) { try { ReadRequest request = new ReadRequest(objectId, instanceId, resourceId); diff --git a/leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/EventServlet.java b/leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/EventServlet.java index 07d82a3..2c17bad 100644 --- a/leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/EventServlet.java +++ b/leshan-server-demo/src/main/java/org/eclipse/leshan/server/demo/servlet/EventServlet.java @@ -182,9 +182,17 @@ public void onResponse(SingleObservation observation, Registration registration, } if (registration != null) { + /* + THIS HAS BEEN MODIFIED BY US +

+ It includes a try catch block, because the compiler was complaining. + */ // 2IMN15: Inform about observe notification. - RoomControl.handleObserveResponse(observation, registration,response); - + try { + RoomControl.handleObserveResponse(observation, registration,response); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } String data = new StringBuilder("{\"ep\":\"") // .append(registration.getEndpoint()) // .append("\",\"kind\":\"single\"") // diff --git a/leshan-server-demo/target/classes/org/course/RoomControl.class b/leshan-server-demo/target/classes/org/course/RoomControl.class index e7d53e0..538784b 100644 Binary files a/leshan-server-demo/target/classes/org/course/RoomControl.class and b/leshan-server-demo/target/classes/org/course/RoomControl.class differ diff --git a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$3.class b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$3.class index 4f4d445..6516e6a 100644 Binary files a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$3.class and b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$3.class differ diff --git a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$4.class b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$4.class index cb075e0..5b48596 100644 Binary files a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$4.class and b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$4.class differ diff --git a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$ClientCoapListener.class b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$ClientCoapListener.class index 1b5943e..5e60d28 100644 Binary files a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$ClientCoapListener.class and b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$ClientCoapListener.class differ diff --git a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$LeshanEventSource.class b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$LeshanEventSource.class index 4c3442a..5bb892c 100644 Binary files a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$LeshanEventSource.class and b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$LeshanEventSource.class differ diff --git a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$RegUpdate.class b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$RegUpdate.class index 1f7a90a..34deb3a 100644 Binary files a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$RegUpdate.class and b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet$RegUpdate.class differ diff --git a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet.class b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet.class index 41d36c8..791fa4c 100644 Binary files a/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet.class and b/leshan-server-demo/target/classes/org/eclipse/leshan/server/demo/servlet/EventServlet.class differ diff --git a/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar b/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar index c1af139..f55f52a 100644 Binary files a/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar and b/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar differ diff --git a/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-sources.jar b/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-sources.jar index 3a912d4..ead764e 100644 Binary files a/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-sources.jar and b/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT-sources.jar differ diff --git a/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT.jar b/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT.jar index e22a27d..7696c8b 100644 Binary files a/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT.jar and b/leshan-server-demo/target/leshan-server-demo-2.0.0-SNAPSHOT.jar differ diff --git a/leshan-server-redis/target/classes/META-INF/MANIFEST.MF b/leshan-server-redis/target/classes/META-INF/MANIFEST.MF index 7b33a2a..647d9c6 100644 --- a/leshan-server-redis/target/classes/META-INF/MANIFEST.MF +++ b/leshan-server-redis/target/classes/META-INF/MANIFEST.MF @@ -1,5 +1,5 @@ Manifest-Version: 1.0 -Bnd-LastModified: 1670861529273 +Bnd-LastModified: 1671049511167 Build-Jdk-Spec: 19 Bundle-Description: Redis implementation for different stores of Leshan Server diff --git a/leshan-server-redis/target/leshan-server-redis-2.0.0-SNAPSHOT.jar b/leshan-server-redis/target/leshan-server-redis-2.0.0-SNAPSHOT.jar index 1041ec0..69d3994 100644 Binary files a/leshan-server-redis/target/leshan-server-redis-2.0.0-SNAPSHOT.jar and b/leshan-server-redis/target/leshan-server-redis-2.0.0-SNAPSHOT.jar differ