课堂作业,由super-loop和event-loop实现。
时隔八百年卡尔再次coding。
package case1;
import smart_room.*;
import smart_room.centralized.*;
//import smart_room.distributed.*;
public class case1{
//String lightID="0";
public static double T=0.20;
public static void super_loop(SinglelBoardSimulator S1, double T1) {
// LightDeviceSimulator super_bulb= new LightDeviceSimulator("super_bulb1");
// LuminositySensorSimulator LS_11 = new LuminositySensorSimulator("super_bulb1");
// PresDetectSensorSimulator PD_11 = new PresDetectSensorSimulator("super_bulb1");
System.out.println("super_loop running!");
S1.init();
S1.off();
while(true){
if(S1.presenceDetected()){
//System.out.println(S1.getLuminosity());
if (S1.getLuminosity()<T1)
{
S1.on();
}
else S1.off();
}
else {
S1.off();
}
System.out.println("Pres Det: " + S1.presenceDetected() + " - Light level: " + S1.getLuminosity());
}
};
public static void event_loop(SinglelBoardSimulator S2, double T2) throws Exception {
System.out.println("event_loop running!");
S2.init();
S2.register((Event ev) -> {
System.out.println("New event: " + ev);
});
new Thread(() -> {
while (true) {
try {
if(S2.presenceDetected()&&S2.getLuminosity()<T2){
S2.on();
}
else S2.off();
} catch (Exception ex) {}
}
}).start();
while (true) {
System.out.println("Pres Det: " + S2.presenceDetected() + " - Light level: " + S2.getLuminosity());
Thread.sleep(1000);
}
};
public static void main(String[] args) {
//T = 0.20;
SinglelBoardSimulator Simulator = new SinglelBoardSimulator();
//super_loop(Simulator , T );
try
{
event_loop(Simulator , T );
} catch (Exception e) {}
}
}
case0 : Use both super-loop and event-loop to achieve smart control of the light panel.
All devices are integrated into a single system.
Single centralized software system controlling a single board including all devices.
===================================================
Problems:
Room with a single light source, to be automatically turned on and off depending on the presence of users in the room and the luminosity level.
on()/off() --> light device (L)
getLuminosity() --> level sensor device (LS)
presenceDetected() --> presence detection sensor device (PD)
VERSION 1 : using super-loop
public static void super_loop(SinglelBoardSimulator S1, double T1):
This function implements the solution through super-loop. A super loop is a program structure comprised of an infinite loop, with all the tasks of the system contained in that loop.
S1 and T1 come from the main function, for the panel and room brightness threshold.
After the init, the loop begins. If the luminosity level is < T and someone is present in the room, the light would on. Otherwise the light would be off. (Here I tested several times before I found that the brightness value on the panel is not 1-100, but 0-1. lol.)
VERSION 2 : using super-loop
public static void event_loop(SinglelBoardSimulator S2, double T2) throws Exception
The event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by making a request to some internal or external event provider, then calls the relevant event handler.
After the init, I register an event. Then the thread begins, inside it is a loop just like the super-loop.
website at:
https://blog.csdn.net/oZuoZuo789