Reactive programming之bulb panel

本文介绍了如何使用super-loop和event-loop来实现一个智能灯板控制系统。该系统根据房间内人员存在情况和光线亮度自动开关灯。在super-loop版本中,系统在无限循环中检查光线亮度和人员存在状态来控制灯的开闭。而在event-loop版本中,通过注册事件监听,等待并处理相关事件来控制灯的状态。
摘要由CSDN通过智能技术生成

课堂作业,由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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值