The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.

This URL has Read-Only access.

Statistics
| Revision:

root / trunk / DomotiGa / EventLoop.module @ 851

History | View | Annotate | Download (5.3 kB)

1
' Gambas module file
2
3
' Description:
4
' EventLoop.module
5
' This is a program loop where all events run which are not started by a timer event.
6
' It will be run 4 times per second (every 250mS)
7
8
' Development Status:
9
' This currently holds hardcodes events, these should be places inside the database instead and
10
' should be configurable via a GUI.
11
12
' DomotiGa - an open source home automation program.
13
' Copyright(C) 2008-2009 Ron Klinkien
14
15
' Read file called COPYING for license details.
16
17
PUBLIC SUB Run()
18
19
  DIM sPlantTalk, sFrom, sTo, sSql AS String
20
  DIM rResult AS Result
21
22
  ' create new log files if needed
23
  IF Main.bNew_Month THEN
24
    Main.Setup_Logfiles()
25
  END IF
26
27
  ' put things that can be triggered on a minute boundary here
28
  ' this should be placed in a database table instead, with nice gui to manage. ;-)
29
30
  IF Main.bNew_Minute THEN
31
32
    Events.CheckTimeNowEvents()
33
34
    Devices.CheckRepeatDevice()
35
    Devices.CheckResetDevice()
36
37
    ' update DSC virtual LCD screen
38
    IF Main.hDSC THEN Main.hDSC.DSC_LCDText()
39
40
    ' store energy usage in database and rrd files every 5 minutes
41
    IF Main.GlobalVar["Minute"] MOD 5 = 0 THEN
42
      Energy.Log() ' write energy usage to *_usage tables
43
    ENDIF
44
45
    ' mail list of devices with low battery
46
    ' and fetch random quote for this day
47
    IF Main.bNew_Minute THEN
48
      ' Devices.MailBatteryStatus("all")
49
      Main.GlobalVar["Tagline"] = Main.Random_Text("remarks_tags")
50
    END IF
51
52
    ' generate plant watering messages
53
    IF Events.TimeCron("30 12,16,18 * * 0") THEN
54
      sPlantTalk = Main.Random_Text("remarks_plants")
55
      VoiceText.Speak(("Your plants want to be watered. They gave me the following message: ") & sPlantTalk)
56
      Twitter.PostTweet(("Your plants wrote this msg: ") & sPlantTalk)
57
    END IF
58
59
    ' if in normal/house mode, auto-go to mute mode
60
    IF Events.TimeCron("30 22 * * 0-4") AND InStr(Main.GlobalVar["House_Mode"], "normal") THEN
61
      VoiceText.Speak(("I am going to mute mode now. Nite nite."))
62
      Main.ChangeMuteMode(TRUE)
63
      Main.SetGlobalVar("Mode_Set", "auto")
64
    END IF
65
66
    ' if I auto-went to mute mode, go to normal mode (don't mess with manually overides)
67
    IF Events.TimeCron("0 7 * * 1-5") AND InStr(Main.GlobalVar["Mode_Set"], "auto") THEN
68
      Main.ChangeMuteMode(FALSE)
69
      VoiceText.Speak(("Gooooodmorning."))
70
    END IF
71
72
    ' home simulation
73
    IF Main.GlobalVar["Simulation"] = "On" THEN
74
      sFrom = Format(Date(Now() - 7), "yyyy-mm-dd") & " " & Format(Time(), "hh:nn")
75
      sTo = sFrom & ":59"
76
      sFrom = sFrom & ":00"
77
      sSql = "SELECT devices.name, devices_log. * FROM devices, devices_log where groups LIKE '%|Simulation|%' AND enabled = -1 AND Log = -1 AND (switchable = -1 OR dimable = -1) AND devices.id = deviceid AND devices_log.lastchanged >= '" & sFrom & "' and devices_log.lastchanged <='" & sTo & "'"
78
      rResult = Main.hDB.Exec(sSql)
79
      IF rResult.Count THEN
80
        IF Main.bEventsDebug THEN Main.WriteDebugLog(("[Simulate] Got ") & rResult.Count & (" result(s)."))
81
        FOR EACH rResult
82
          IF Main.bEventsDebug THEN Main.WriteDebugLog(("[Simulate] Setting device '") & rResult!name & "' to '" & rResult!value & "'")
83
          Devices.SetDevice(rResult!name, rResult!value)
84
        NEXT
85
      ENDIF
86
    ENDIF
87
  ENDIF
88
89
END
90
91
PUBLIC SUB DeviceChanged(iId AS Integer, sField AS String, sValue AS String, sLabel AS String)
92
93
  ' log device status changes who are not hidden and have log changes enabled
94
  IF Devices.FindLogDisplayForDevice(iId) AND NOT Devices.FindHideForDevice(iId) THEN
95
    Main.WriteLog(Devices.FindNameForDevice(iId) & " status changed to " & sValue & IIf(sLabel, " " & sLabel, "") & ".")
96
  END IF
97
98
  Events.CheckDeviceChangeEvents(iId, sField, sValue)
99
100
  ' detect postal mail delivery
101
  IF Devices.FindNameForDevice(iId) = "Mailbox Sensor" AND IF sValue == "open" THEN
102
    ' grab video image with frontdoor camera
103
    IF Main.bVideoServerEnabled THEN
104
      VideoServer.Grab(0, ("Mailbox Status"), ("Someone just delivered you postal mail!"), Main.sEmailToAddress)
105
    END IF
106
    'Mail.SendMail(Application.Name & ": Mailbox Status", "Someone just delivered you postal mail!", Main.sEmailToAddress)
107
    ' Mail.SendCaptureByMail(Application.Name & ": Mailbox Status", "Someone just delivered you postal mail!", 0, Main.sEmailToAddress)
108
    Main.WriteLog(("Mail delivered at front door, sending an e-mail."))
109
    Twitter.PostTweet(("Mail is just delivered at the front door."))
110
  END IF
111
112
  ' detect doorbell ring
113
  IF Devices.FindNameForDevice(iId) = "Doorbell" AND IF sValue == "on" THEN
114
    ' grab video image with frontdoor camera
115
    IF Main.bVideoServerEnabled THEN
116
      VideoServer.Grab(0, ("Doorbell Status"), ("Someone just rang your doorbell!"), Main.sEmailToAddress)
117
    END IF
118
    Main.WriteLog(("Someone just rang the Doorbell, sending an e-mail."))
119
    Twitter.PostTweet(("Someone just rang the Doorbell."))
120
  END IF
121
122
  ' detect front door opening
123
  IF Devices.FindNameForDevice(iId) = "Front Door Sensor" AND IF sValue == "open" THEN
124
    ' grab video image with frontdoor camera
125
    IF Main.bVideoServerEnabled THEN
126
      VideoServer.Grab(0, ("Front Door Status"), ("Someone just opened the Front Door!"), Main.sEmailToAddress)
127
    END IF
128
    Main.WriteLog(("Someone just opened the front door, sending an e-mail."))
129
    Twitter.PostTweet(("Someone just opened the front door."))
130
  END IF
131
132
END