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.
root / trunk / DomotiGaServer / Events.module @ 239
History | View | Annotate | Download (11.2 kB)
| 1 | ' Gambas module file |
|---|---|
| 2 | |
| 3 | ' Description: |
| 4 | ' Events.module |
| 5 | ' This module provides support for events, triggers and actions. |
| 6 | |
| 7 | ' Development Status: |
| 8 | ' Just started. |
| 9 | |
| 10 | ' DomotiGa - an open source home automation program. |
| 11 | ' Copyright(C) 2008-2009 Ron Klinkien |
| 12 | |
| 13 | ' Read file called COPYING for license details. |
| 14 | |
| 15 | PUBLIC SUB CheckTimeNowEvents() |
| 16 | |
| 17 | DIM rResult, rResultAction AS Result |
| 18 | |
| 19 | rResult = Main.hDB.Exec("SELECT * FROM events, triggers WHERE events.trigger1 = triggers.id AND triggers.type = 1 AND events.enabled AND triggers.param1 = &1 AND triggers.param2 = &2", Format(Main.GlobalVar["Hour"], "00"), Format(Main.GlobalVar["Minute"], "00"))
|
| 20 | IF rResult THEN |
| 21 | IF rResult.Count THEN |
| 22 | FOR EACH rResult |
| 23 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Event '" & rResult["events.name"] & "' is triggered.")
|
| 24 | IF rResult["events.action1"] THEN |
| 25 | rResultAction = Main.hDB.Exec("SELECT * FROM actions WHERE id = &1 ", rResult["events.action1"])
|
| 26 | IF rResultAction THEN |
| 27 | IF RunAction(rResult["events.action1"]) THEN UpdateEvent(rResult["events.id"]) |
| 28 | ELSE |
| 29 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Action '" & rResult["events.name"] & "' no first action defined!")
|
| 30 | END IF |
| 31 | END IF |
| 32 | NEXT |
| 33 | END IF |
| 34 | ELSE |
| 35 | Main.WriteLog("Error: table 'events' not found!")
|
| 36 | RETURN |
| 37 | END IF |
| 38 | |
| 39 | END |
| 40 | |
| 41 | PUBLIC SUB CheckDeviceChangeEvents(iId AS Integer, sField AS String, sValue AS String) |
| 42 | |
| 43 | DIM rResult, rResultCondition AS Result |
| 44 | DIM bTrigger AS Boolean |
| 45 | DIM sDateDiff AS String |
| 46 | |
| 47 | rResult = Main.hDB.Exec("SELECT * FROM events, triggers WHERE events.trigger1 = triggers.id AND triggers.type = 3 AND events.enabled AND triggers.param1 = &1", iId)
|
| 48 | IF rResult THEN |
| 49 | IF rResult.Count THEN |
| 50 | FOR EACH rResult |
| 51 | ' check trigger |
| 52 | IF rResult["triggers.param2"] <> sField THEN |
| 53 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Event '" & rResult["events.name"] & "' " & sField & " changed, but trigger is on field " & rResult["triggers.param2"])
|
| 54 | IF Main.bEventsDebug THEN Main.WriteDebugLog("iId = " & iId & " triggers.param2 = " & rResult["triggers.param2"] & " sField = " & sField)
|
| 55 | CONTINUE |
| 56 | END IF |
| 57 | IF NOT CheckTrigger(sValue, rResult["triggers.param3"], rResult["triggers.param4"]) THEN CONTINUE |
| 58 | ' SELECT rResult["triggers.param3"] |
| 59 | ' CASE "=" |
| 60 | ' IF Main.bEventsDebug THEN Main.WriteDebugLog("triggers.param4 = " & rResult["triggers.param4"] & " = " & sValue)
|
| 61 | ' IF Comp(sValue, rResult["triggers.param4"]) <> 0 THEN CONTINUE |
| 62 | ' CASE "<>" |
| 63 | ' IF sValue == rResult["triggers.param4"] THEN CONTINUE |
| 64 | ' ' CASE ">" |
| 65 | ' ' IF IsDigit(sValue) = FALSE IsDigit(rResult["triggers.param4"]) THEN |
| 66 | ' ' IF sValue == rResult["triggers.param4"] THEN CONTINUE |
| 67 | ' CASE ELSE |
| 68 | ' IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Event '" & rResult["events.name"] & "' unsupported operand found!")
|
| 69 | ' CONTINUE |
| 70 | ' END SELECT |
| 71 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Device Status trigger on '" & rResult["triggers.param2"] & "' " & sValue & " " & rResult["triggers.param3"] & " " & rResult["triggers.param4"])
|
| 72 | ' check rerun condition |
| 73 | IF rResult!rerunenabled = TRUE AND IsDate(rResult!lastrun) THEN |
| 74 | IF NOT CheckReRunCondition(rResult!lastrun, rResult!reruntype, rResult!rerunvalue) THEN |
| 75 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Already ran in the last " & rResult!rerunvalue & " " & rResult!reruntype & " so discarding")
|
| 76 | CONTINUE |
| 77 | END IF |
| 78 | END IF |
| 79 | ' check condition1 |
| 80 | IF rResult["events.condition1"] THEN |
| 81 | rResultCondition = Main.hDB.Exec("SELECT * FROM conditions WHERE id = &1", rResult["events.condition1"])
|
| 82 | IF rResultCondition THEN |
| 83 | SELECT rResultCondition!type |
| 84 | CASE 2 ' variable |
| 85 | IF NOT CheckCondition(Main.GlobalVar[rResultCondition!param1], rResultCondition!param2, rResultCondition!param3) AND IF NOT rResult["events.condition2"] THEN CONTINUE |
| 86 | ' SELECT rResultCondition!param2 |
| 87 | ' CASE "=" |
| 88 | ' IF Main.GlobalVar[rResultCondition!param1] == rResultCondition!param3 THEN |
| 89 | ' bTrigger = TRUE |
| 90 | ' END IF |
| 91 | ' CASE "<>" |
| 92 | ' IF Main.GlobalVar[rResultCondition!param1] <> rResultCondition!param3 THEN |
| 93 | ' bTrigger = TRUE |
| 94 | ' END IF |
| 95 | ' CASE ELSE |
| 96 | ' bTrigger = FALSE |
| 97 | ' END SELECT |
| 98 | CASE ELSE |
| 99 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Event '" & rResult["events.name"] & "' unsupported condition type found!")
|
| 100 | IF NOT rResult["events.condition2"] THEN CONTINUE |
| 101 | END SELECT |
| 102 | END IF |
| 103 | END IF |
| 104 | ' ' check condition2 |
| 105 | ' IF rResult["events.condition2"] THEN |
| 106 | ' IF rResult["events.operand"] == "OR" THEN |
| 107 | ' rResultCondition = Main.hDB.Exec("SELECT * FROM conditions WHERE id = &1", rResult["events.condition2"])
|
| 108 | ' IF rResultCondition THEN |
| 109 | ' IF rResultCondition!type = 2 THEN ' variable |
| 110 | ' SELECT rResultCondition!param2 |
| 111 | ' CASE "=" |
| 112 | ' IF Main.GlobalVar[rResultCondition!param1] == rResultCondition!param3 THEN |
| 113 | ' bTrigger = TRUE |
| 114 | ' END IF |
| 115 | ' CASE "<>" |
| 116 | ' IF Main.GlobalVar[rResultCondition!param1] <> rResultCondition!param3 THEN |
| 117 | ' bTrigger = TRUE |
| 118 | ' END IF |
| 119 | ' CASE ELSE |
| 120 | ' bTrigger = FALSE |
| 121 | ' END SELECT |
| 122 | ' ELSE |
| 123 | ' IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Event '" & rResult["events.name"] & "' unsupported condition type found!")
|
| 124 | ' END IF |
| 125 | ' END IF |
| 126 | ' END IF |
| 127 | ' END IF |
| 128 | ' run action(s) |
| 129 | ' IF bTrigger THEN |
| 130 | Main.WriteDebugLog("[Events] Event '" & rResult["events.name"] & "' is triggered.")
|
| 131 | IF rResult["events.action1"] THEN |
| 132 | IF RunAction(rResult["events.action1"]) THEN UpdateEvent(rResult["events.id"]) |
| 133 | ELSE |
| 134 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Action '" & rResult["events.name"] & "' no first action defined!")
|
| 135 | END IF |
| 136 | IF rResult["events.action2"] THEN |
| 137 | IF RunAction(rResult["events.action2"]) THEN UpdateEvent(rResult["events.id"]) |
| 138 | END IF |
| 139 | IF rResult["events.action3"] THEN |
| 140 | IF RunAction(rResult["events.action3"]) THEN UpdateEvent(rResult["events.id"]) |
| 141 | END IF |
| 142 | ' END IF |
| 143 | ' bTrigger = FALSE |
| 144 | NEXT |
| 145 | END IF |
| 146 | ELSE |
| 147 | Main.WriteLog("Error: table 'events' not found!")
|
| 148 | RETURN |
| 149 | END IF |
| 150 | |
| 151 | END |
| 152 | |
| 153 | PRIVATE SUB CheckReRunCondition(dDate AS Date, sPeriod AS String, iValue AS Integer) AS Boolean |
| 154 | |
| 155 | IF DateDiff(dDate, Now(), Eval(sPeriod)) > iValue THEN |
| 156 | RETURN TRUE |
| 157 | ELSE |
| 158 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Event Rerun condition " & iValue & " " & sPeriod & " is false.")
|
| 159 | RETURN FALSE |
| 160 | END IF |
| 161 | |
| 162 | ' SELECT sPeriod |
| 163 | ' CASE "Seconds" |
| 164 | ' IF DateDiff(dDate, Now(), gb.Second) > iValue THEN RETURN TRUE |
| 165 | ' CASE "Minutes" |
| 166 | ' IF DateDiff(dDate, Now(), gb.Minut) > iValue THEN RETURN TRUE |
| 167 | ' CASE "Hours" |
| 168 | ' IF DateDiff(dDate, Now(), gb.Hour) > iValue THEN RETURN TRUE |
| 169 | ' CASE "Days" |
| 170 | ' IF DateDiff(dDate, Now(), gb.Day) > iValue THEN RETURN TRUE |
| 171 | ' CASE "Weeks" |
| 172 | ' IF DateDiff(dDate, Now(), gb.Week) > iValue THEN RETURN TRUE |
| 173 | ' CASE "Weekdays" |
| 174 | ' IF DateDiff(dDate, Now(), gb.WeekDay) > iValue THEN RETURN TRUE |
| 175 | ' CASE "Months" |
| 176 | ' IF DateDiff(dDate, Now(), gb.Month) > iValue THEN RETURN TRUE |
| 177 | ' CASE "Quarters" |
| 178 | ' IF DateDiff(dDate, Now(), gb.Quarter) > iValue THEN RETURN TRUE |
| 179 | ' CASE "Years" |
| 180 | ' IF DateDiff(dDate, Now(), gb.Year) > iValue THEN RETURN TRUE |
| 181 | ' END SELECT |
| 182 | ' RETURN FALSE |
| 183 | |
| 184 | END |
| 185 | |
| 186 | PUBLIC SUB RunAction(iAction AS Integer) AS Boolean |
| 187 | |
| 188 | DIM rResultAction AS Result |
| 189 | DIM bOk AS Boolean |
| 190 | |
| 191 | rResultAction = Main.hDB.Exec("SELECT * FROM actions WHERE id = &1 ", iAction)
|
| 192 | IF rResultAction.Available THEN |
| 193 | SELECT rResultAction!type |
| 194 | CASE 1 ' device change |
| 195 | IF rResultAction!param1 AND IF rResultAction!param2 AND IF rResultAction!param3 THEN |
| 196 | Devices.SetDevice(Devices.FindNameForDevice(rResultAction!param1), rResultAction!param3) |
| 197 | bOk = TRUE |
| 198 | END IF |
| 199 | CASE ELSE |
| 200 | bOk = FALSE |
| 201 | END SELECT |
| 202 | END IF |
| 203 | |
| 204 | IF bOk THEN |
| 205 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Action '" & rResultAction!name & "' executed!")
|
| 206 | RETURN TRUE |
| 207 | ELSE |
| 208 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Action '" & rResultAction!name & "' failed to execute!")
|
| 209 | RETURN FALSE |
| 210 | END IF |
| 211 | |
| 212 | END |
| 213 | |
| 214 | PUBLIC SUB UpdateEvent(iId AS Integer) |
| 215 | |
| 216 | DIM rResult AS Result |
| 217 | |
| 218 | rResult = Main.hDB.Exec("SELECT * FROM events where id = &1", iId)
|
| 219 | IF rResult.Available THEN |
| 220 | IF rResult.Count = 1 THEN |
| 221 | IF rResult!firstrun = "00:00:00" OR rResult!firstrun = "" THEN |
| 222 | rResult = Main.hDB.Exec("UPDATE events SET firstrun = &1 WHERE id = &2", Format(Now(), "yyyy-mm-dd hh:nn:ss"), iId)
|
| 223 | END IF |
| 224 | END IF |
| 225 | rResult = Main.hDB.Exec("UPDATE events SET lastrun = &1 WHERE id = &2", Format(Now(), "yyyy-mm-dd hh:nn:ss"), iId)
|
| 226 | END IF |
| 227 | |
| 228 | END |
| 229 | |
| 230 | PRIVATE SUB CheckTrigger(sValue AS Variant, sOperand AS String, sCond AS Variant) AS Boolean |
| 231 | |
| 232 | DIM bReturn AS Boolean |
| 233 | |
| 234 | IF IsBoolean(sCond) THEN sCond = CBool(sValue) |
| 235 | |
| 236 | SELECT sOperand |
| 237 | CASE "=" |
| 238 | IF Comp(sValue, sCond) = 0 THEN bReturn = TRUE |
| 239 | CASE "<>" |
| 240 | IF sValue <> sCond THEN bReturn = TRUE |
| 241 | CASE ">" |
| 242 | IF IsDigit(sValue) AND IF IsDigit(sCond) = FALSE THEN |
| 243 | IF Val(sValue) > Val(sCond) THEN bReturn = TRUE |
| 244 | ENDIF |
| 245 | CASE "<" |
| 246 | IF IsDigit(sValue) AND IF IsDigit(sCond) = FALSE THEN |
| 247 | IF Val(sValue) < Val(sCond) THEN bReturn = TRUE |
| 248 | ENDIF |
| 249 | CASE ELSE |
| 250 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Unsupported operand found!")
|
| 251 | END SELECT |
| 252 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Check trigger " & sCond & " " & sOperand & " " & sValue & " = " & Main.DisplayBool(bReturn))
|
| 253 | RETURN bReturn |
| 254 | |
| 255 | END |
| 256 | |
| 257 | PRIVATE SUB CheckCondition(sValue AS Variant, sOperand AS String, sCond AS Variant) AS Boolean |
| 258 | |
| 259 | DIM bReturn AS Boolean |
| 260 | |
| 261 | IF IsBoolean(sCond) THEN sCond = CBool(sValue) |
| 262 | |
| 263 | SELECT sOperand |
| 264 | CASE "=" |
| 265 | IF Comp(sValue, sCond) = 0 THEN bReturn = TRUE |
| 266 | CASE "<>" |
| 267 | IF sValue <> sCond THEN bReturn = TRUE |
| 268 | CASE ">" |
| 269 | IF IsDigit(sValue) AND IF IsDigit(sCond) = FALSE THEN |
| 270 | IF Val(sValue) > Val(sCond) THEN bReturn = TRUE |
| 271 | ENDIF |
| 272 | CASE "<" |
| 273 | IF IsDigit(sValue) AND IF IsDigit(sCond) = FALSE THEN |
| 274 | IF Val(sValue) < Val(sCond) THEN bReturn = TRUE |
| 275 | ENDIF |
| 276 | CASE ELSE |
| 277 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Unsupported operand found!")
|
| 278 | END SELECT |
| 279 | IF Main.bEventsDebug THEN Main.WriteDebugLog("[Events] Check condition " & sCond & " " & sOperand & " " & sValue & " = " & Main.DisplayBool(bReturn))
|
| 280 | RETURN bReturn |
| 281 | |
| 282 | END |
