Guest User

Domoticz_Lua_Verlichting

a guest
Oct 2nd, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. t = os.date("*t")
  2.  
  3. commandArray = {}
  4.  
  5. m_testmodus = false
  6.  
  7. function isVeranderingIemandThuis()
  8.     local a = devicechanged["IemandThuis"] == "On" or devicechanged["IemandThuis"] == "Off"
  9.  
  10.     return a
  11. end
  12.  
  13. function isVeranderingDonkerBinnen()
  14.     local a = devicechanged["DonkerBinnen"] == "On" or devicechanged["DonkerBinnen"] == "Off"
  15.  
  16.     return a
  17. end
  18.  
  19. function isVeranderingDonker()
  20.     local a = devicechanged["Donker"] == "On" or devicechanged["Donker"] == "Off"
  21.  
  22.     return a
  23. end
  24.  
  25. function isVeranderingBedtijd()
  26.     local a = devicechanged["Bedtijd"] == "On" or devicechanged["Bedtijd"] == "Off"
  27.  
  28.     return a
  29. end
  30.  
  31. function scriptMagWordenUitgevoerd()
  32.     local a, b, c, d, e = false, false, false, false
  33.    
  34.     if (devicechanged ~= nil) then
  35.         a = isVeranderingDonker()
  36.         b = isVeranderingDonkerBinnen()
  37.         c = isVeranderingBedtijd()
  38.         d = isVeranderingIemandThuis()
  39.     else
  40.         e = true
  41.     end
  42.  
  43.     print('scriptMagWordenUitgevoerd() A: '..tostring(a)..' B: '..tostring(b)..' C: '..tostring(c)..' D: '..tostring(d))
  44.     return a or b or c or d
  45. end
  46.  
  47. if (scriptMagWordenUitgevoerd() == false) then
  48.     print('Geen geldige input voor script. Afgebroken.')
  49.     return commandArray
  50. end
  51.  
  52. -- enums
  53. local VerlichtingUitgeschakeld, NietThuisStand, VerlichtingIngeschakeld, Ongedefinieerd = 0, 1, 2, 3
  54.  
  55. function toToestandString(toestand)
  56.  
  57.     if (toestand == VerlichtingUitgeschakeld) then
  58.         return "Toestand: Verlichting Uitgeschakeld"
  59.     elseif (toestand == NietThuisStand) then
  60.         return "Toestand: Niet thuis"
  61.     elseif (toestand == VerlichtingIngeschakeld) then
  62.         return "Toestand: Verlichting Ingeschakeld"
  63.     elseif (toestand == Ongedefinieerd) then
  64.         return "Toestand: Ongedefinieerd"
  65.     end
  66. end
  67.  
  68. function iemandThuis()
  69.     a = devicechanged ~= nil and devicechanged["IemandThuis"] == "On"
  70.     b = otherdevices["IemandThuis"] == "On"
  71.  
  72.     print('iemandThuis() C: '..tostring(a)..' O: '..tostring(b))
  73.     return a or b
  74. end
  75.  
  76. -- Controleer of uitschakelen van de verlichting is toegestaan
  77. function isUitschakelenToegestaan()
  78.     local a = devicechanged ~= nil and devicechanged["DonkerBinnen"] == "Off"
  79.  
  80.     local b = isVeranderingIemandThuis() == true and iemandThuis() == false
  81.  
  82.     local c = devicechanged ~= nil and devicechanged["Bedtijd"] == "On"
  83.  
  84.     print('isUitschakelenToegestaan() A: '..tostring(a)..' B: '..tostring(b)..' C: '..tostring(c))
  85.     return a or b or c
  86. end
  87.  
  88. function isInschakelenToegestaan()
  89.     local a = devicechanged ~= nil and devicechanged["DonkerBinnen"] == "On"
  90.  
  91.     local b = isVeranderingIemandThuis() == true and iemandThuis() == true
  92.    
  93.     local c = devicechanged ~= nil and devicechanged["Bedtijd"] == "Off"
  94.  
  95.     print('isInschakelenToegestaan() A: '..tostring(a)..' B: '..tostring(b)..' C: '..tostring(c))
  96.     return a or b or c
  97. end
  98.  
  99. function isHetDonkerBinnen()
  100.     local a = devicechanged ~= nil and devicechanged["DonkerBinnen"] == "On"
  101.     local b = otherdevices["DonkerBinnen"] == "On"
  102.  
  103.     print('isHetDonkerBinnen() A: '..tostring(a)..' B: '..tostring(b))
  104.     return a or b
  105. end
  106.  
  107. function bepaalHuidigeToestand()
  108.  
  109.         for i, v in pairs(otherdevices) do print(i, v) end
  110.  
  111.         if (
  112.             otherdevices["Staande lamp bank rechts"]    == "Off" and
  113.             otherdevices["Staande lamp bank links"]     == "Off" and
  114.             otherdevices["Staande lamp dressoir links"] == "Off" and
  115.             otherdevices["Lamp dressoir rechts"]        == "Off"
  116.             ) then
  117.             return VerlichtingUitgeschakeld
  118.     elseif (
  119.             otherdevices["Staande lamp bank rechts"]    == "On" and
  120.             otherdevices["Staande lamp bank links"]     == "On" and
  121.             otherdevices["Staande lamp dressoir links"] == "On"
  122.             -- and
  123.             -- otherdevices["Lamp dressoir rechts"]        == "On"
  124.             ) then
  125.             return VerlichtingIngeschakeld
  126.     elseif (
  127.             otherdevices["Staande lamp bank rechts"]    == "On" and
  128.             otherdevices["Staande lamp bank links"]     == "Off" and
  129.             otherdevices["Staande lamp dressoir links"] == "On" and
  130.             otherdevices["Lamp dressoir rechts"]        == "Off"
  131.             ) then
  132.             return NietThuisStand
  133.     end
  134.  
  135.     return Ongedefinieerd
  136.  
  137. end
  138.  
  139. function isOvergangToegestaan(van, naar)
  140.     local a = naar == VerlichtingIngeschakeld and m_maglichtinschakelen == true
  141.     local b = naar == VerlichtingUitgeschakeld and m_maglichtuitschakelen == true
  142.     local c = naar == NietThuisStand
  143.    
  144.     print('isOvergangToegestaan() A: '..tostring(a)..' B: '..tostring(b)..'C: '..tostring(c))
  145.  
  146.     return a or b or c
  147. end
  148.  
  149. function stelToestandIn(naar)
  150.     print(toToestandString(naar)..' wordt ingesteld.')
  151.  
  152.     if (m_testmodus == true) then
  153.         return false
  154.     end
  155.     if (naar == VerlichtingIngeschakeld) then
  156.         commandArray["Group:Woonkamer"] = "On"
  157.     elseif (naar == VerlichtingUitgeschakeld) then
  158.         commandArray["Group:Woonkamer"] = "Off"
  159.     elseif (naar == NietThuisStand) then
  160.         commandArray["Staande lamp bank rechts"] = "On"
  161.         commandArray["Staande lamp bank links"] = "Off"
  162.         commandArray["Staande lamp dressoir links"] = "On"
  163.         commandArray["Lamp dressoir rechts"] = "Off"
  164.     end
  165. end
  166.  
  167. -- Controleer of er behoefte is aan verlichting.
  168. function lichtbehoefteBinnen()
  169.     if ( isHetDonkerBinnen() == true and iemandThuis() == true ) then
  170.         print('Er is behoefte aan licht.')
  171.         return true
  172.     elseif ( isHetDonkerBinnen() == false or iemandThuis() == false ) then
  173.         print('Er is geen behoefte aan licht.')
  174.         return false
  175.     end
  176. end
  177.  
  178. if ( otherdevices["Donker"] == "On" and otherdevices["Buitenlamp schuur"] == "Off" ) then
  179.     print('De buitenverlichting wordt ingeschakeld.')
  180.     commandArray["Group:Buitenverlichting"] = "On"
  181. elseif ( otherdevices["Donker"] == "Off" and otherdevices["Buitenlamp schuur"] == "On" ) then
  182.     print('De buitenverlichting wordt uitgeschakeld.')
  183.     commandArray["Group:Buitenverlichting"] = "Off"
  184. end
  185.  
  186.  
  187.  
  188. local m_huidigeToestand = bepaalHuidigeToestand()
  189. local m_gewensteToestand = m_huidigeToestand
  190.  
  191. m_donker = isHetDonkerBinnen()
  192. m_lichtbehoefte = lichtbehoefteBinnen()
  193. m_iemandthuis = iemandThuis()
  194. m_maglichtuitschakelen = isUitschakelenToegestaan()
  195. m_maglichtinschakelen = isInschakelenToegestaan()
  196.  
  197. -- debug
  198. -- m_lichtbehoefte = false
  199. -- m_iemandthuis = false
  200. -- m_maglichtuitschakelen = false
  201.  
  202. print('m_huidigeToestand '..toToestandString(m_huidigeToestand))
  203. print('m_gewensteToestand '..toToestandString(m_gewensteToestand))
  204. print('m_donker '..tostring(m_donker))
  205. print('m_lichtbehoefte '..tostring(m_lichtbehoefte))
  206. print('m_iemandthuis '..tostring(m_iemandthuis))
  207. print('m_maglichtuitschakelen '..tostring(m_maglichtuitschakelen))
  208. print('m_maglichtinschakelen '..tostring(m_maglichtinschakelen))
  209.  
  210. -- local VerlichtingUitgeschakeld, NietThuisStand, VerlichtingIngeschakeld, Ongedefinieerd = 0, 1, 2, 3
  211.  
  212. function gaNaarGewensteToestand()
  213.     if ( m_donker == true ) then
  214.         if ( m_lichtbehoefte == false ) then
  215.             m_gewensteToestand = NietThuisStand
  216.         else
  217.             if ( otherdevices["Bedtijd"] == "On" ) then
  218.                 m_gewensteToestand = VerlichtingUitgeschakeld
  219.             else
  220.                 m_gewensteToestand = VerlichtingIngeschakeld
  221.             end
  222.         end
  223.     else
  224.         m_gewensteToestand = VerlichtingUitgeschakeld
  225.     end
  226.  
  227.     print('m_huidigeToestand '..toToestandString(m_huidigeToestand))
  228.     print('m_gewensteToestand '..toToestandString(m_gewensteToestand))
  229.     if (m_huidigeToestand ~= m_gewensteToestand) then
  230.         print('Huidige toestand is niet de gewenste!')
  231.         if (isOvergangToegestaan(m_huidigeToestand, m_gewensteToestand) == true) then
  232.             stelToestandIn(m_gewensteToestand)
  233.         else
  234.             print('Overgang is niet toegestaan.')
  235.         end
  236.     else
  237.         print('Huidige toestand is de gewenste!')
  238.     end
  239. end
  240.  
  241. gaNaarGewensteToestand()
  242.  
  243. return commandArray
Advertisement
Add Comment
Please, Sign In to add comment