|
| 1 | +type GroupType = { |
| 2 | + "radiator":string; |
| 3 | + "contact":string; |
| 4 | +}; |
| 5 | + |
| 6 | +const HamaRadiatorMode = { |
| 7 | + heat: "heat", |
| 8 | + off: "off" |
| 9 | +} |
| 10 | + |
| 11 | +const livingRoom:GroupType = { |
| 12 | + "radiator": 'zigbee.0.2c1165fffeb3dc7b.mode', // Hama Radiator |
| 13 | + "contact": 'zigbee.0.00124b002fbdc865.opened', //open or close |
| 14 | +} ; |
| 15 | + |
| 16 | +const sleepingRoom:GroupType = { |
| 17 | + "radiator": "2", |
| 18 | + "contact": '2', |
| 19 | +}; |
| 20 | + |
| 21 | +const kitchen:GroupType = { |
| 22 | + "radiator": "3", |
| 23 | + "contact": '3', |
| 24 | +}; |
| 25 | + |
| 26 | +const childrensRoom:GroupType = { |
| 27 | + "radiator": "4", |
| 28 | + "contact": '4', |
| 29 | +} ; |
| 30 | + |
| 31 | +const toilet:GroupType = { |
| 32 | + "radiator": "5", |
| 33 | + "contact": '5', |
| 34 | +} ; |
| 35 | + |
| 36 | +const house:GroupType[] = [childrensRoom, kitchen, livingRoom, sleepingRoom, toilet]; |
| 37 | + |
| 38 | +const northGroup:GroupType[] = [ |
| 39 | + childrensRoom, |
| 40 | + sleepingRoom, |
| 41 | + toilet |
| 42 | +]; |
| 43 | +const southGroup:GroupType[] = [ |
| 44 | + livingRoom, |
| 45 | + kitchen |
| 46 | +]; |
| 47 | + |
| 48 | +const checkIsWindowOpen = (room : GroupType):boolean => { |
| 49 | + if (getState(room.contact).val) { |
| 50 | + return true; |
| 51 | + } else { |
| 52 | + return false; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const deactivateAllRadiators = (rooms:GroupType[]) => { |
| 57 | + for (let room of rooms) { |
| 58 | + deactivateRadiator(room); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +const activateAllRadiators = (rooms:GroupType[]) => { |
| 63 | + for (let room of rooms) { |
| 64 | + activateRadiator(room); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +const deactivateRadiator = (room:GroupType) => { |
| 69 | + setState(room.radiator, HamaRadiatorMode.off); |
| 70 | +} |
| 71 | + |
| 72 | +const activateRadiator = (room:GroupType) => { |
| 73 | + setState(room.radiator, HamaRadiatorMode.heat); |
| 74 | +} |
| 75 | + |
| 76 | +const checkGroup = (group:GroupType[]):boolean => { |
| 77 | + let returnValue:boolean = false; |
| 78 | + |
| 79 | + for (let room of group) { |
| 80 | + if (checkIsWindowOpen(room)) { |
| 81 | + returnValue = true; |
| 82 | + } |
| 83 | + } |
| 84 | + return returnValue; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +const checkAllWindows = () => { |
| 89 | + |
| 90 | + let northOpen = checkGroup(northGroup); |
| 91 | + let southOpen = checkGroup(southGroup); |
| 92 | + if (northOpen && southOpen) { |
| 93 | + deactivateAllRadiators(house); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (!northOpen && !southOpen) { |
| 97 | + activateAllRadiators(house); |
| 98 | + return; |
| 99 | + } |
| 100 | + for (let room of house) { |
| 101 | + if (checkIsWindowOpen(room)) { |
| 102 | + deactivateRadiator(room); |
| 103 | + } else { |
| 104 | + activateRadiator(room); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +on ({id: livingRoom.contact}, (obj) =>{ |
| 112 | + checkAllWindows(); |
| 113 | +}); |
| 114 | + |
| 115 | +on ({id: childrensRoom.contact}, (obj) =>{ |
| 116 | + checkAllWindows(); |
| 117 | +}); |
| 118 | +on ({id: kitchen.contact}, (obj) =>{ |
| 119 | + checkAllWindows(); |
| 120 | +}); |
| 121 | + |
| 122 | +on ({id: sleepingRoom.contact}, (obj) =>{ |
| 123 | + checkAllWindows(); |
| 124 | +}); |
0 commit comments