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 / Devices.module @ 465

History | View | Annotate | Download (29.3 kB)

1 2 riemers
' Gambas module file
2 2 riemers
3 2 riemers
' Description:
4 2 riemers
' Devices.module
5 98 rdnzl
' Contains code which is device related.
6 2 riemers
7 2 riemers
' Development Status:
8 98 rdnzl
' Working, maybe needs more error checking.
9 2 riemers
10 2 riemers
' DomotiGa - an open source home automation program.
11 307 rdnzl
' Copyright(C) 2008-2010 Ron Klinkien
12 2 riemers
13 25 rdnzl
' Read file called COPYING for license details.
14 2 riemers
15 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 279 rdnzl
' control device via linked interface
17 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 2 riemers
PUBLIC SUB SetDevice(sDeviceName AS String, sValue AS String)
19 2 riemers
20 433 rdnzl
  DIM iInterface, iDeviceId AS Integer
21 2 riemers
  DIM sText, sAddress AS String
22 2 riemers
23 2 riemers
  iInterface = FindInterfaceForDevice(sDeviceName)
24 2 riemers
  sAddress = FindAddressForDevice(sDeviceName)
25 2 riemers
26 2 riemers
  SELECT CASE iInterface
27 199 rdnzl
    CASE 0
28 245 rdnzl
      Main.WriteDebugLog(("[Devices] Something is wrong, cannot query interface for this device!"))
29 203 rdnzl
    CASE 1 ' RFXCom Receiver
30 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] RFXCom Receiver can't control anything!"))
31 2 riemers
    CASE 2 ' Xanura CTX35
32 203 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Xanura CTX35: " & sAddress & " " & sValue)
33 2 riemers
      IF Main.hCTX35 THEN
34 174 rdnzl
        IF Upper(sValue) <> "STOP" THEN
35 174 rdnzl
          Main.hCTX35.sCommandToSend = sAddress & " " & sValue
36 174 rdnzl
          Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
37 245 rdnzl
          sText = ("Switched ") & sDeviceName & " " & sValue
38 174 rdnzl
        END IF
39 2 riemers
      ELSE
40 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because CTX35 is disabled!"))
41 2 riemers
      END IF
42 203 rdnzl
    CASE 3, 4, 5, 6, 8
43 245 rdnzl
      Main.WriteDebugLog(("[Devices] Error, read-only interface used for switching!"))
44 203 rdnzl
    CASE 7 ' Heyu X10
45 98 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Heyu X10")
46 203 rdnzl
      IF Main.bHeyuEnabled THEN
47 245 rdnzl
        sText = ("Switched ") & sDeviceName & " " & sValue
48 203 rdnzl
        Heyu.SendCommand(sAddress, sValue)
49 205 rdnzl
        Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
50 203 rdnzl
      ELSE
51 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because Heyu is disabled!"))
52 203 rdnzl
      END IF
53 2 riemers
    CASE 9 ' Weeder WTSSR
54 174 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Weeder WTSSR: " & sAddress & " " & sValue)
55 188 rdnzl
      IF Main.hWeeder THEN
56 433 rdnzl
        iDeviceId = Find(sAddress, iInterface)
57 433 rdnzl
        IF iDeviceId THEN
58 433 rdnzl
          IF InStr(FindTypeNameForDevice(iDeviceId), "ITHO ECO Fan") THEN
59 188 rdnzl
            IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] ITHO ECO Fan")
60 188 rdnzl
            Main.hWeeder.ControlITHO(sAddress, sValue)
61 188 rdnzl
            Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
62 188 rdnzl
          END IF
63 2 riemers
        ELSE
64 245 rdnzl
          Main.WriteDebugLog(("[Devices] ITHO ECO Fan not enabled!"))
65 2 riemers
        END IF
66 188 rdnzl
      ELSE
67 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because Weeder is disabled!"))
68 2 riemers
      END IF
69 40 rdnzl
    CASE 10 ' Plugwise
70 174 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Plugwise Stick: " & sAddress & " " & sValue)
71 40 rdnzl
      IF Main.hPlugwise THEN
72 40 rdnzl
        Main.hPlugwise.SendCommand(sAddress, sValue)
73 98 rdnzl
        Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
74 245 rdnzl
        sText = ("Switched ") & sDeviceName & " " & sValue
75 40 rdnzl
      ELSE
76 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because Plugwise is disabled!"))
77 40 rdnzl
      END IF
78 203 rdnzl
    CASE 12 ' RFXCom Transmitter
79 203 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] RFXCom Transmitter: " & sAddress & " " & sValue)
80 203 rdnzl
      IF Main.hRFXComTX THEN
81 203 rdnzl
        IF sValue <> "STOP" THEN
82 203 rdnzl
          Main.hRFXComTX.SendCommand(sAddress, sValue)
83 203 rdnzl
          Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
84 245 rdnzl
          sText = ("Switched ") & sDeviceName & " " & sValue
85 203 rdnzl
        END IF
86 203 rdnzl
      ELSE
87 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because RFXCom Transmitter is disabled!"))
88 203 rdnzl
      END IF
89 174 rdnzl
    CASE 13 ' KNX/EIB Interface
90 174 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] KNX/EIB Interface: " & sAddress & " " & sValue)
91 174 rdnzl
      IF Main.hEIB THEN
92 174 rdnzl
        Main.hEIB.SendCommand(sAddress, sValue)
93 174 rdnzl
      ELSE
94 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because KNX/EIB is disabled!"))
95 174 rdnzl
      END IF
96 379 rdnzl
    CASE 15 ' Z-Wave Controller
97 213 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Z-Wave Interface: " & sAddress & " " & sValue)
98 213 rdnzl
      IF Main.hZWave THEN
99 213 rdnzl
        IF sValue <> "STOP" THEN
100 213 rdnzl
          Main.hZWave.SendCommand(sAddress, sValue)
101 213 rdnzl
          Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
102 245 rdnzl
          sText = ("Switched ") & sDeviceName & " " & sValue
103 213 rdnzl
        END IF
104 213 rdnzl
      ELSE
105 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because Z-Wave interface is disabled!"))
106 213 rdnzl
      END IF
107 398 rdnzl
    CASE 16 ' PLCBUS Interface
108 398 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] PLCBUS Interface: " & sAddress & " " & sValue)
109 398 rdnzl
      IF Main.hPLCBUS THEN
110 398 rdnzl
        IF sValue <> "STOP" THEN
111 398 rdnzl
          Main.hPLCBUS.SendCommand(sAddress, sValue)
112 398 rdnzl
          Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
113 398 rdnzl
          sText = ("Switched ") & sDeviceName & " " & sValue
114 398 rdnzl
        END IF
115 398 rdnzl
      ELSE
116 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because PLCBUS interface is disabled!"))
117 398 rdnzl
      END IF
118 415 rdnzl
    CASE 20 ' EZcontrol Interface
119 415 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] EZcontrol Interface: " & sAddress & " " & sValue)
120 415 rdnzl
      IF Main.hEZcontrol THEN
121 415 rdnzl
        IF sValue <> "STOP" THEN
122 433 rdnzl
          iDeviceId = Find(sAddress, iInterface)
123 433 rdnzl
          IF iDeviceId THEN
124 433 rdnzl
            Main.hEZcontrol.SendCommand(sAddress, sValue, iDeviceId)
125 415 rdnzl
            Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
126 415 rdnzl
            sText = ("Switched ") & sDeviceName & " " & sValue
127 415 rdnzl
          END IF
128 415 rdnzl
        END IF
129 415 rdnzl
      ELSE
130 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because EZcontrol interface is disabled!"))
131 415 rdnzl
      END IF
132 433 rdnzl
    CASE 22 ' PwrCtrl Interface
133 433 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] PwrCtrl Interface: " & sAddress & " " & sValue)
134 433 rdnzl
      IF Main.hPwrCtrl THEN
135 433 rdnzl
        IF sValue <> "STOP" THEN
136 433 rdnzl
            Main.hPwrCtrl.SendCommand(sAddress, sValue)
137 433 rdnzl
            Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
138 433 rdnzl
            sText = ("Switched ") & sDeviceName & " " & sValue
139 433 rdnzl
        END IF
140 433 rdnzl
      ELSE
141 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because PwrCtrl interface is disabled!"))
142 433 rdnzl
      END IF
143 433 rdnzl
    CASE 23 ' Denon Interface
144 433 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Denon Interface: " & sAddress & " " & sValue)
145 433 rdnzl
      IF Main.hDenon THEN
146 433 rdnzl
        IF sValue <> "STOP" THEN
147 433 rdnzl
            Main.hDenon.SendCommand(sAddress, sValue)
148 433 rdnzl
            Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
149 433 rdnzl
            sText = ("Switched ") & sDeviceName & " " & sValue
150 433 rdnzl
        END IF
151 433 rdnzl
      ELSE
152 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because Denon interface is disabled!"))
153 433 rdnzl
      END IF
154 433 rdnzl
    CASE 24 ' Onkyo Interface
155 433 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog("[Devices] Onkyo Interface: " & sAddress & " " & sValue)
156 433 rdnzl
      IF Main.hOnkyo THEN
157 433 rdnzl
        IF sValue <> "STOP" THEN
158 433 rdnzl
            Main.hOnkyo.SendCommand(sAddress, sValue)
159 433 rdnzl
            Devices.ValueUpdate(Find(sAddress, iInterface), sValue, "", "", "")
160 433 rdnzl
            sText = ("Switched ") & sDeviceName & " " & sValue
161 433 rdnzl
        END IF
162 433 rdnzl
      ELSE
163 433 rdnzl
        Main.WriteDebugLog(("[Devices] Can't control device '") & sDeviceName & ("' because Onkyo interface is disabled!"))
164 433 rdnzl
      END IF
165 2 riemers
    CASE -1
166 245 rdnzl
      Main.WriteDebugLog(("[Devices] Interface for device '") & sDeviceName & ("' does not support Write!"))
167 125 rdnzl
  CASE ELSE
168 433 rdnzl
    Main.WriteDebugLog(("[Devices] Unknown Interface type ") & Str(iInterface) & (" for device '") & sDeviceName & "'!")
169 2 riemers
  END SELECT
170 2 riemers
171 433 rdnzl
  IF sText AND IF iInterface THEN
172 433 rdnzl
    iDeviceId = Find(sAddress, iInterface)
173 433 rdnzl
    IF iDeviceId AND IF FindLogSpeakForDevice(iDeviceId) THEN VoiceText.Speak(sText)
174 433 rdnzl
  END IF
175 2 riemers
176 2 riemers
END
177 2 riemers
178 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179 2 riemers
' find interface with name sInterfaceName, return it's id.
180 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181 2 riemers
PUBLIC SUB FindInterface(sInterfaceName AS String) AS Integer
182 2 riemers
183 2 riemers
  DIM rInterface AS Result
184 2 riemers
185 2 riemers
  TRY rInterface = Main.hDB.Exec("SELECT * FROM interfaces WHERE name = &1", sInterfaceName)
186 140 rdnzl
  IF rInterface.Available THEN
187 248 rdnzl
    IF rInterface.Count = 1 THEN
188 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Interface with name '") & sInterfaceName & ("' has id '") & rInterface!id & "'")
189 2 riemers
      RETURN rInterface!id
190 2 riemers
    ELSE
191 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Interface with name '") & sInterfaceName & ("' not found!"))
192 2 riemers
      RETURN 0
193 2 riemers
    END IF
194 2 riemers
  ELSE
195 245 rdnzl
    IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Cannot get Interface records!"))
196 2 riemers
    RETURN 0
197 2 riemers
  END IF
198 2 riemers
199 2 riemers
END
200 2 riemers
201 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202 256 rdnzl
' find devicetype with type, return it's id.
203 256 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204 256 rdnzl
PUBLIC SUB FindDeviceType(sDeviceType AS String) AS Integer
205 256 rdnzl
206 256 rdnzl
  DIM rDeviceType AS Result
207 256 rdnzl
208 256 rdnzl
  TRY rDeviceType = Main.hDB.Exec("SELECT * FROM devicetypes WHERE type LIKE &1", sDeviceType)
209 256 rdnzl
  IF rDeviceType.Available THEN
210 256 rdnzl
    IF rDeviceType.Count = 1 THEN
211 256 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] DeviceType with type '") & sDeviceType & ("' has id '") & rDeviceType!id & "'")
212 256 rdnzl
      RETURN rDeviceType!id
213 256 rdnzl
    ELSE
214 256 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] DeviceType with type '") & sDeviceType & ("' not found!"))
215 256 rdnzl
      RETURN 0
216 256 rdnzl
    END IF
217 256 rdnzl
  ELSE
218 256 rdnzl
    IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Cannot get DeviceType records!"))
219 256 rdnzl
    RETURN 0
220 256 rdnzl
  END IF
221 256 rdnzl
222 256 rdnzl
END
223 256 rdnzl
224 256 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
225 2 riemers
' return module/devicetype for device with id
226 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227 2 riemers
PUBLIC SUB FindModuleForDevice(iDeviceId AS Integer) AS Integer
228 2 riemers
229 2 riemers
  DIM rDevice AS Result
230 2 riemers
231 2 riemers
  TRY rDevice = Main.hDB.Exec("SELECT module FROM devices WHERE id = &1", iDeviceId)
232 151 rdnzl
  IF rDevice.Available THEN
233 151 rdnzl
    RETURN rDevice!module
234 151 rdnzl
  ELSE
235 151 rdnzl
    RETURN 0
236 151 rdnzl
  END IF
237 2 riemers
238 2 riemers
END
239 2 riemers
240 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
241 2 riemers
' return type for device with id
242 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243 2 riemers
PUBLIC SUB FindTypeForDevice(iDeviceId AS Integer) AS String
244 2 riemers
245 2 riemers
  DIM rDevice AS Result
246 2 riemers
  DIM iDeviceType AS Integer
247 2 riemers
248 2 riemers
  iDeviceType = FindModuleForDevice(iDeviceId)
249 62 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT type FROM devicetypes WHERE id = &1", iDeviceType)
250 2 riemers
  RETURN rDevice!type
251 2 riemers
252 2 riemers
END
253 2 riemers
254 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
255 2 riemers
' return typename for device with id
256 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
257 2 riemers
PUBLIC SUB FindTypeNameForDevice(iDeviceId AS Integer) AS String
258 2 riemers
259 2 riemers
  DIM rDevice AS Result
260 2 riemers
  DIM iDeviceType AS Integer
261 2 riemers
262 2 riemers
  iDeviceType = FindModuleForDevice(iDeviceId)
263 62 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT name FROM devicetypes WHERE id = &1", iDeviceType)
264 2 riemers
  RETURN rDevice!name
265 2 riemers
266 2 riemers
END
267 2 riemers
268 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269 248 rdnzl
' return description for devicetype with iId
270 248 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
271 248 rdnzl
PUBLIC SUB FindDescrForDeviceType(iId AS Integer) AS String
272 248 rdnzl
273 248 rdnzl
  DIM rDevice AS Result
274 248 rdnzl
275 248 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT description FROM devicetypes WHERE id = &1", iId)
276 465 rdnzl
  IF rDevice.Available THEN
277 465 rdnzl
    RETURN rDevice!description
278 465 rdnzl
  ELSE
279 465 rdnzl
    RETURN 0
280 465 rdnzl
  END IF
281 248 rdnzl
282 248 rdnzl
END
283 248 rdnzl
284 248 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285 2 riemers
' return name for device with id
286 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
287 2 riemers
PUBLIC SUB FindNameForDevice(iDeviceId AS Integer) AS String
288 2 riemers
289 2 riemers
  DIM rDevice AS Result
290 2 riemers
291 62 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT name FROM devices WHERE id = &1", iDeviceId)
292 433 rdnzl
  IF NOT ERROR AND IF rDevice.Available THEN
293 433 rdnzl
    RETURN rDevice!name
294 433 rdnzl
  ELSE
295 433 rdnzl
    Main.WriteDebugLog(("[Devices] Cannot find name for device with id ") & iDeviceId)
296 433 rdnzl
    RETURN ""
297 433 rdnzl
  ENDIF
298 2 riemers
299 2 riemers
END
300 2 riemers
301 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
302 307 rdnzl
' return device id for virtual device with address
303 307 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
304 307 rdnzl
PUBLIC SUB FindVirtualDeviceID(sAddress AS String) AS Integer
305 307 rdnzl
306 307 rdnzl
  DIM iId AS Integer
307 307 rdnzl
308 307 rdnzl
  iId = Find(sAddress, FindInterface("Virtual Interface"), "Virtual Device")
309 307 rdnzl
  RETURN iId
310 307 rdnzl
311 307 rdnzl
END
312 307 rdnzl
313 307 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314 48 rdnzl
' return name for interface with id
315 48 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
316 48 rdnzl
PUBLIC SUB FindNameForInterface(iInterfaceId AS Integer) AS String
317 48 rdnzl
318 48 rdnzl
  DIM rInterface AS Result
319 48 rdnzl
320 62 rdnzl
  TRY rInterface = Main.hDB.Exec("SELECT name FROM interfaces WHERE id = &1", iInterfaceId)
321 48 rdnzl
  RETURN rInterface!name
322 48 rdnzl
323 48 rdnzl
END
324 48 rdnzl
325 48 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326 2 riemers
' return log boolean for device with id
327 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328 2 riemers
PUBLIC SUB FindLogForDevice(iDeviceId AS Integer) AS String
329 2 riemers
330 2 riemers
  DIM rDevice AS Result
331 2 riemers
332 62 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT log FROM devices WHERE id = &1", iDeviceId)
333 433 rdnzl
  IF NOT ERROR AND IF rDevice.Available THEN RETURN rDevice!log
334 2 riemers
335 2 riemers
END
336 2 riemers
337 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338 433 rdnzl
' return print boolean for device with id
339 433 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340 433 rdnzl
PUBLIC SUB FindLogDisplayForDevice(iDeviceId AS Integer) AS String
341 433 rdnzl
342 433 rdnzl
  DIM rDevice AS Result
343 433 rdnzl
344 433 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT logdisplay FROM devices WHERE id = &1", iDeviceId)
345 433 rdnzl
  IF NOT ERROR AND IF rDevice.Available THEN RETURN rDevice!logdisplay
346 433 rdnzl
347 433 rdnzl
END
348 433 rdnzl
349 433 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
350 433 rdnzl
' return speak boolean for device with id
351 433 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352 433 rdnzl
PUBLIC SUB FindLogSpeakForDevice(iDeviceId AS Integer) AS String
353 433 rdnzl
354 433 rdnzl
  DIM rDevice AS Result
355 433 rdnzl
356 433 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT logspeak FROM devices WHERE id = &1", iDeviceId)
357 433 rdnzl
  IF NOT ERROR AND IF rDevice.Available THEN RETURN rDevice!logspeak
358 433 rdnzl
359 433 rdnzl
END
360 433 rdnzl
361 433 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
362 2 riemers
' return hide boolean for device with id
363 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
364 2 riemers
PUBLIC SUB FindHideForDevice(iDeviceId AS Integer) AS String
365 2 riemers
366 2 riemers
  DIM rDevice AS Result
367 2 riemers
368 62 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT hide FROM devices WHERE id = &1", iDeviceId)
369 433 rdnzl
  IF NOT ERROR AND IF rDevice.Available THEN RETURN rDevice!hide
370 2 riemers
371 2 riemers
END
372 2 riemers
373 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
374 2 riemers
' find interface for device with sdeviceName.
375 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
376 2 riemers
PUBLIC SUB FindInterfaceForDevice(sDeviceName AS String) AS Integer
377 2 riemers
378 238 rdnzl
  DIM rDevice, rResult AS Result
379 2 riemers
380 238 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT address, name, interface FROM devices WHERE name = &1", sDeviceName)
381 2 riemers
  ' found only one unique entry?
382 2 riemers
  IF rDevice.Count > 1 THEN
383 245 rdnzl
    Main.WriteDebugLog(("[Devices] The following devices have same name:"))
384 2 riemers
    FOR EACH rDevice
385 245 rdnzl
      Main.WriteDebugLog(("[Devices] Device with address '") & rDevice!address & "' is '" & rDevice!name & "'")
386 2 riemers
    NEXT
387 2 riemers
    RETURN 0
388 2 riemers
  ' ok this one seems useful
389 2 riemers
  ELSE IF rDevice.Count = 1 THEN
390 2 riemers
    TRY rResult = Main.hDB.Exec("SELECT mode FROM interfaces WHERE id = &1", rDevice!interface)
391 2 riemers
    IF InStr(rResult!mode, "Write") THEN
392 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Interface for device with name '") & sDeviceName & "' is '" & rDevice!interface & "'")
393 2 riemers
      RETURN rDevice!interface
394 2 riemers
    ELSE
395 48 rdnzl
      ' read-only
396 2 riemers
      RETURN -1
397 2 riemers
    END IF
398 2 riemers
  ' no interface found, something is wrong
399 2 riemers
  ELSE
400 245 rdnzl
    Main.WriteDebugLog(("[Devices] Device with name '") & sDeviceName & ("' not found!"))
401 2 riemers
    RETURN 0
402 2 riemers
  END IF
403 2 riemers
404 2 riemers
END
405 2 riemers
406 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
407 2 riemers
' find address for device with sDeviceName.
408 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
409 2 riemers
PUBLIC SUB FindAddressForDevice(sDeviceName AS String) AS String
410 2 riemers
411 2 riemers
  DIM rDevice AS Result
412 2 riemers
413 2 riemers
  TRY rDevice = Main.hDB.Exec("SELECT address FROM devices WHERE name = &1", sDeviceName)
414 2 riemers
  IF rDevice.Count > 1 THEN
415 245 rdnzl
    Main.WriteDebugLog(("[Devices] The following devices have same name:"))
416 2 riemers
    FOR EACH rDevice
417 245 rdnzl
      Main.WriteDebugLog(("[Devices] Device with address '") & rDevice!address & ("' is '") & rDevice!name & "'")
418 2 riemers
    NEXT
419 2 riemers
    RETURN 0
420 2 riemers
  ELSE IF rDevice.Count = 1 THEN
421 245 rdnzl
    IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Address for device with name '") & sDeviceName & ("' is '") & rDevice!address & "'")
422 2 riemers
    RETURN rDevice!address
423 2 riemers
  ELSE
424 245 rdnzl
    Main.WriteDebugLog(("[Devices] Device with name '") & sDeviceName & ("' not found!"))
425 2 riemers
    RETURN 0
426 2 riemers
  END IF
427 2 riemers
428 2 riemers
END
429 2 riemers
430 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
431 2 riemers
' find device with sName, return it's current value
432 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
433 441 rdnzl
PUBLIC SUB GetValueForDevice(sDeviceName AS String, OPTIONAL iValue AS Integer) AS String
434 2 riemers
435 2 riemers
  DIM rDevice AS Result
436 2 riemers
437 441 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT value, value2, value3, value4 address FROM devices WHERE name = &1", sDeviceName)
438 2 riemers
  IF rDevice.Count > 1 THEN
439 245 rdnzl
    Main.WriteDebugLog(("[Devices] The following devices have same name:"))
440 2 riemers
    FOR EACH rDevice
441 245 rdnzl
      Main.WriteDebugLog(("[Devices] Device with address '") & rDevice!address & ("' is '") & rDevice!name & "'")
442 2 riemers
    NEXT
443 2 riemers
    RETURN 0
444 2 riemers
  ELSE IF rDevice.Count = 1 THEN
445 441 rdnzl
    IF ivalue THEN
446 441 rdnzl
      SELECT CASE iValue
447 441 rdnzl
        CASE 1
448 441 rdnzl
          RETURN rDevice!value
449 441 rdnzl
        CASE 2
450 441 rdnzl
          RETURN rDevice!value2
451 441 rdnzl
        CASE 3
452 441 rdnzl
          RETURN rDevice!value3
453 441 rdnzl
        CASE 4
454 441 rdnzl
          RETURN rDevice!value4
455 441 rdnzl
        CASE ELSE
456 441 rdnzl
          RETURN rDevice!value
457 441 rdnzl
      END SELECT
458 441 rdnzl
    ELSE
459 441 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Value of device with name '") & sDeviceName & ("' is ") & rDevice!value)
460 441 rdnzl
      RETURN rDevice!value
461 441 rdnzl
    ENDIF
462 2 riemers
  ELSE
463 245 rdnzl
    Main.WriteDebugLog(("[Devices] Device with name '") & sDeviceName & ("' not found!"))
464 2 riemers
    RETURN 0
465 2 riemers
  END IF
466 2 riemers
467 2 riemers
END
468 2 riemers
469 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
470 465 rdnzl
' find device with iId, return it's lastchanged value
471 465 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
472 465 rdnzl
PUBLIC SUB GetLastChangedForDevice(iId AS Integer) AS String
473 465 rdnzl
474 465 rdnzl
  DIM rDevice AS Result
475 465 rdnzl
476 465 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT lastchanged FROM devices WHERE id = &1", iId)
477 465 rdnzl
  IF rDevice.Count THEN
478 465 rdnzl
    RETURN rDevice!lastchanged
479 465 rdnzl
  END IF
480 465 rdnzl
481 465 rdnzl
END
482 465 rdnzl
483 465 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
484 2 riemers
' find device with iId, return it's current value
485 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
486 2 riemers
PUBLIC SUB GetCurrentValueForDevice(iId AS Integer) AS String
487 2 riemers
488 2 riemers
  DIM rDevice AS Result
489 2 riemers
490 2 riemers
  TRY rDevice = Main.hDB.Exec("SELECT value FROM devices WHERE id = &1", iId)
491 2 riemers
  IF rDevice.Count THEN
492 2 riemers
    RETURN rDevice!value
493 2 riemers
  END IF
494 2 riemers
495 2 riemers
END
496 2 riemers
497 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
498 2 riemers
' find device with iId, return it's current value2
499 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
500 2 riemers
PUBLIC SUB GetCurrentValue2ForDevice(iId AS Integer) AS String
501 2 riemers
502 2 riemers
  DIM rDevice AS Result
503 2 riemers
504 2 riemers
  TRY rDevice = Main.hDB.Exec("SELECT value2 FROM devices WHERE id = &1", iId)
505 2 riemers
  IF rDevice.Count THEN
506 2 riemers
    RETURN rDevice!value2
507 2 riemers
  END IF
508 2 riemers
509 2 riemers
END
510 2 riemers
511 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512 40 rdnzl
' find device with iId, return it's current value3
513 40 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
514 40 rdnzl
PUBLIC SUB GetCurrentValue3ForDevice(iId AS Integer) AS String
515 40 rdnzl
516 40 rdnzl
  DIM rDevice AS Result
517 40 rdnzl
518 40 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT value3 FROM devices WHERE id = &1", iId)
519 40 rdnzl
  IF rDevice.Count THEN
520 40 rdnzl
    RETURN rDevice!value3
521 40 rdnzl
  END IF
522 40 rdnzl
523 40 rdnzl
END
524 40 rdnzl
525 40 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
526 98 rdnzl
' find device with iId, return it's current value4
527 98 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
528 98 rdnzl
PUBLIC SUB GetCurrentValue4ForDevice(iId AS Integer) AS String
529 98 rdnzl
530 98 rdnzl
  DIM rDevice AS Result
531 98 rdnzl
532 98 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT value4 FROM devices WHERE id = &1", iId)
533 98 rdnzl
  IF rDevice.Count THEN
534 98 rdnzl
    RETURN rDevice!value4
535 98 rdnzl
  END IF
536 98 rdnzl
537 98 rdnzl
END
538 98 rdnzl
539 98 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
540 436 rdnzl
' find device with sAddress and iInterface, return id if found.
541 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
542 159 rdnzl
PUBLIC SUB Find(sAddress AS String, iInterface AS Integer, OPTIONAL sDevice AS String) AS Integer
543 2 riemers
544 125 rdnzl
  DIM rDevice, rResult AS Result
545 2 riemers
546 436 rdnzl
  IF iInterface THEN
547 436 rdnzl
    TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE address LIKE &1 AND interface = &2 AND enabled is TRUE", sAddress, iInterface)
548 436 rdnzl
    IF rDevice.Count > 1 THEN
549 436 rdnzl
      Main.WriteDebugLog(("[Devices] The following devices have same address:"))
550 436 rdnzl
      FOR EACH rDevice
551 436 rdnzl
        Main.WriteDebugLog(("[Devices] Device with address '") & sAddress & ("' is '") & rDevice!name & "'")
552 436 rdnzl
      NEXT
553 2 riemers
      RETURN 0
554 436 rdnzl
    ELSE IF rDevice.Count = 1 THEN
555 436 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device with address '") & sAddress & ("' is '") & rDevice!name & "'")
556 436 rdnzl
      IF rDevice!firstseen = "00:00:00" OR IF rDevice!firstseen = "" THEN
557 436 rdnzl
        IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device with address '") & sAddress & ("' and '") & rDevice!name & ("' is a new device"))
558 436 rdnzl
        rResult = Main.hDB.Exec("UPDATE devices SET firstseen = &1 WHERE id = &2", Format(Now(), "yyyy-mm-dd hh:nn:ss"), rDevice!id)
559 436 rdnzl
      END IF
560 436 rdnzl
      ' if it's hidden we return 0, else we return it's id
561 436 rdnzl
      IF rDevice!hide THEN
562 436 rdnzl
        IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device with address '") & sAddress & ("' and name '") & rDevice!name & ("' is set to hide"))
563 436 rdnzl
        RETURN 0
564 159 rdnzl
      ELSE
565 436 rdnzl
        RETURN rDevice!id
566 159 rdnzl
      END IF
567 159 rdnzl
    ELSE
568 436 rdnzl
      IF NOT IsBlacklisted(sAddress, iInterface) THEN
569 159 rdnzl
        IF sDevice THEN
570 436 rdnzl
          Main.WriteDebugLog(("[Devices] Device '") & sDevice & ("' with address '") & sAddress & ("' and interface '") & FindNameForInterface(iInterface) & ("' not found!"))
571 159 rdnzl
        ELSE
572 436 rdnzl
          Main.WriteDebugLog(("[Devices] Device with address '") & sAddress & ("' and interface '") & FindNameForInterface(iInterface) & ("' not found!"))
573 159 rdnzl
        END IF
574 436 rdnzl
        RETURN 0
575 436 rdnzl
      ELSE
576 436 rdnzl
        IF Main.bDevicesDebug THEN
577 436 rdnzl
          IF sDevice THEN
578 436 rdnzl
            Main.WriteDebugLog(("[Devices] Device '") & sDevice & ("' with address '") & sAddress & ("' and interface '") & FindNameForInterface(iInterface) & ("' is blacklisted!"))
579 436 rdnzl
          ELSE
580 436 rdnzl
            Main.WriteDebugLog(("[Devices] Device with address '") & sAddress & ("' and interface '") & FindNameForInterface(iInterface) & ("' is blacklisted!"))
581 436 rdnzl
          END IF
582 436 rdnzl
        END IF
583 159 rdnzl
      END IF
584 48 rdnzl
    END IF
585 436 rdnzl
  ELSE
586 436 rdnzl
    Main.WriteDebugLog(("[Devices] Interface for device '") & sDevice & ("' with address '") & sAddress & ("' not found!"))
587 436 rdnzl
    RETURN 0
588 436 rdnzl
  ENDIF
589 2 riemers
590 2 riemers
END
591 2 riemers
592 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
593 307 rdnzl
' find out if an device has firstseen filled
594 307 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
595 307 rdnzl
PUBLIC SUB CheckFirstSeen(iId AS Integer)
596 307 rdnzl
597 307 rdnzl
  DIM rResult, rDevice AS Result
598 307 rdnzl
599 307 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE id = &1", iId)
600 307 rdnzl
  IF rDevice.Count THEN
601 307 rdnzl
    IF rDevice!firstseen = "00:00:00" OR IF rDevice!firstseen = "" THEN
602 307 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device with address '") & rDevice!address & ("' and '") & rDevice!name & ("' is a new device!"))
603 307 rdnzl
      rResult = Main.hDB.Exec("UPDATE devices SET firstseen = &1 WHERE id = &2", Format(Now(), "yyyy-mm-dd hh:nn:ss"), rDevice!id)
604 307 rdnzl
    END IF
605 307 rdnzl
  END IF
606 307 rdnzl
607 307 rdnzl
END
608 307 rdnzl
609 307 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
610 2 riemers
' find out if address is blacklisted and return true
611 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
612 199 rdnzl
PUBLIC SUB IsBlacklisted(sAddress AS String, iInterface AS Integer) AS Boolean
613 2 riemers
614 2 riemers
  DIM rDevice AS Result
615 2 riemers
616 2 riemers
  TRY rDevice = Main.hDB.Exec("SELECT * FROM deviceblacklist WHERE address = &1", sAddress)
617 2 riemers
  IF rDevice.Count THEN
618 199 rdnzl
    IF rDevice!id THEN
619 199 rdnzl
      IF iInterface = rDevice!id THEN
620 245 rdnzl
        IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Address '") & sAddress & ("' with Interface '") & FindNameForInterface(iInterface) & ("' is blacklisted, so I ignore it."))
621 199 rdnzl
        RETURN TRUE
622 199 rdnzl
      ELSE
623 199 rdnzl
        RETURN FALSE
624 199 rdnzl
      END IF
625 199 rdnzl
    END IF
626 245 rdnzl
    IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Address '") & sAddress & ("' is blacklisted, so I ignore it."))
627 2 riemers
    RETURN TRUE
628 2 riemers
  END IF
629 2 riemers
  RETURN FALSE
630 2 riemers
631 2 riemers
END
632 2 riemers
633 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
634 208 rdnzl
' update device with iId with vValue, vValue2 etc
635 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
636 98 rdnzl
PUBLIC SUB ValueUpdate(iId AS Integer, vValue AS Variant, vValue2 AS Variant, vValue3 AS Variant, vValue4 AS Variant) AS Boolean
637 2 riemers
638 2 riemers
  DIM rResult, rDevice AS Result
639 2 riemers
  DIM bUpdated AS Boolean
640 433 rdnzl
  DIM vValUpdated AS NEW Collection
641 433 rdnzl
  DIM rDeviceLog AS Result
642 433 rdnzl
  DIM vVal AS Variant
643 98 rdnzl
  DIM sLabel, sLabel2, sLabel3, sLabel4, sValue, sValue2, sValue3, sValue4, sSql AS String
644 2 riemers
645 208 rdnzl
  IF vValue = "Dim 100" THEN vValue = "On"
646 208 rdnzl
  IF vValue = "Dim 0" THEN vValue = "Off"
647 208 rdnzl
648 433 rdnzl
  rDevice = Main.hDB.Exec("SELECT name, label, label2, label3, label4, value, value2, value3, value4, log FROM devices WHERE id = &1", iId)
649 98 rdnzl
  IF rDevice.Count = 1 THEN
650 2 riemers
    sLabel = rDevice!label
651 2 riemers
    sLabel2 = rDevice!label2
652 98 rdnzl
    sLabel3 = rDevice!label3
653 98 rdnzl
    sLabel4 = rDevice!label4
654 98 rdnzl
    sValue = rDevice!value
655 98 rdnzl
    sValue2 = rDevice!value2
656 98 rdnzl
    sValue3 = rDevice!value3
657 98 rdnzl
    sValue4 = rDevice!value4
658 433 rdnzl
  ENDIF
659 53 rdnzl
660 245 rdnzl
  IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] ValueUpdate() called for device with id '") & iId & ("' and name '") & rDevice!name & "'")
661 98 rdnzl
  IF vValue THEN
662 98 rdnzl
    IF Comp(vValue, sValue, gb.Text) <> 0 THEN
663 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device change triggered for device with id '") & iId & ("' and value1 '") & vValue & "'")
664 98 rdnzl
      EventLoop.DeviceChanged(iId, "Value", vValue, sLabel)
665 98 rdnzl
      bUpdated = TRUE
666 433 rdnzl
      IF rDevice!log THEN
667 433 rdnzl
        vValUpdated["value"] = vValue
668 433 rdnzl
      ENDIF
669 98 rdnzl
      rResult = Main.hDB.Exec("UPDATE devices SET lastchanged = &1, lastseen = &2, value = &3 WHERE id = &4", Format(Now(), "yyyy-mm-dd hh:nn:ss"), Format(Now(), "yyyy-mm-dd hh:nn:ss"), vValue, iId)
670 433 rdnzl
    ENDIF
671 433 rdnzl
  ENDIF
672 2 riemers
  IF vValue2 THEN
673 98 rdnzl
    IF Comp(vValue2, sValue2, gb.Text) <> 0 THEN
674 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device change triggered for device with id '") & iId & ("' and value2 '") & vValue2 & "'")
675 98 rdnzl
      EventLoop.DeviceChanged(iId, "Value2", vValue2, sLabel2)
676 2 riemers
      bUpdated = TRUE
677 433 rdnzl
      IF rDevice!log THEN
678 433 rdnzl
        vValUpdated["value2"] = vValue2
679 433 rdnzl
      ENDIF
680 98 rdnzl
      rResult = Main.hDB.Exec("UPDATE devices SET lastchanged = &1, lastseen = &2, value2 = &3 WHERE id = &4", Format(Now(), "yyyy-mm-dd hh:nn:ss"), Format(Now(), "yyyy-mm-dd hh:nn:ss"), vValue2, iId)
681 433 rdnzl
    ENDIF
682 433 rdnzl
  ENDIF
683 98 rdnzl
  IF vValue3 THEN
684 98 rdnzl
    IF Comp(vValue3, sValue3, gb.Text) <> 0 THEN
685 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device change triggered for device with id '") & iId & ("' and value3 '") & vValue3 & "'")
686 98 rdnzl
      EventLoop.DeviceChanged(iId, "Value3", vValue3, sLabel3)
687 2 riemers
      bUpdated = TRUE
688 433 rdnzl
      IF rDevice!log THEN
689 433 rdnzl
        vValUpdated["value3"] = vValue3
690 433 rdnzl
      ENDIF
691 98 rdnzl
      rResult = Main.hDB.Exec("UPDATE devices SET lastchanged = &1, lastseen = &2, value3 = &3 WHERE id = &4", Format(Now(), "yyyy-mm-dd hh:nn:ss"), Format(Now(), "yyyy-mm-dd hh:nn:ss"), vValue3, iId)
692 433 rdnzl
    ENDIF
693 433 rdnzl
  ENDIF
694 98 rdnzl
  IF vValue4 THEN
695 98 rdnzl
    IF Comp(vValue4, sValue4, gb.Text) <> 0 THEN
696 245 rdnzl
      IF Main.bDevicesDebug THEN Main.WriteDebugLog(("[Devices] Device change triggered for device with id '") & iId & ("' and value4 '") & vValue4 & "'")
697 98 rdnzl
      EventLoop.DeviceChanged(iId, "Value4", vValue4, sLabel4)
698 98 rdnzl
      bUpdated = TRUE
699 433 rdnzl
      IF rDevice!log THEN
700 433 rdnzl
        vValUpdated["value4"] = vValue4
701 433 rdnzl
      ENDIF
702 98 rdnzl
      rResult = Main.hDB.Exec("UPDATE devices SET lastchanged = &1, lastseen = &2, value4 = &3 WHERE id = &4", Format(Now(), "yyyy-mm-dd hh:nn:ss"), Format(Now(), "yyyy-mm-dd hh:nn:ss"), vValue4, iId)
703 433 rdnzl
    ENDIF
704 433 rdnzl
  ENDIF
705 98 rdnzl
  IF NOT bUpdated THEN
706 98 rdnzl
    rResult = Main.hDB.Exec("UPDATE devices SET lastseen = &1 WHERE id = &2", Format(Now(), "yyyy-mm-dd hh:nn:ss"), iId)
707 433 rdnzl
  ELSE
708 433 rdnzl
    IF rDevice!log THEN
709 433 rdnzl
      Main.hDB.Begin()
710 433 rdnzl
      rDeviceLog = Main.hDB.Create("devices_log")
711 433 rdnzl
      rDeviceLog!deviceid = iId
712 433 rdnzl
      rDeviceLog!lastchanged = Now()
713 433 rdnzl
      FOR EACH vVal IN vValUpdated
714 433 rdnzl
        rDeviceLog[vValUpdated.Key] = vVal
715 433 rdnzl
      NEXT
716 433 rdnzl
      rDeviceLog.Update()
717 433 rdnzl
      Main.hDB.Commit()
718 433 rdnzl
    ENDIF
719 433 rdnzl
  ENDIF
720 2 riemers
721 261 rdnzl
  Main.RefreshDeviceList()
722 219 rdnzl
723 40 rdnzl
  IF rResult THEN
724 40 rdnzl
    RETURN TRUE
725 40 rdnzl
  ELSE
726 40 rdnzl
    RETURN FALSE
727 433 rdnzl
  ENDIF
728 40 rdnzl
729 40 rdnzl
END
730 40 rdnzl
731 40 rdnzl
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
732 2 riemers
' update battery status for device with iId
733 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
734 2 riemers
PUBLIC SUB Battery(iId AS Integer, vValue AS Variant) AS Boolean
735 2 riemers
736 2 riemers
  DIM rResult AS Result
737 2 riemers
738 2 riemers
  rResult = Main.hDB.Exec("UPDATE devices SET batterystatus = &1 WHERE id = &2", vValue, iId)
739 2 riemers
740 2 riemers
  IF rResult.Count THEN
741 2 riemers
    RETURN TRUE
742 2 riemers
  ELSE
743 2 riemers
    RETURN FALSE
744 2 riemers
  END IF
745 2 riemers
746 2 riemers
END
747 2 riemers
748 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
749 98 rdnzl
' mail battery status for device with battery status set
750 2 riemers
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
751 2 riemers
PUBLIC FUNCTION MailBatteryStatus()
752 2 riemers
753 2 riemers
  DIM rDevice AS Result
754 2 riemers
  DIM sMessageBody AS String
755 140 rdnzl
  DIM bFound AS Boolean
756 2 riemers
757 373 rdnzl
  TRY rDevice = Main.hDB.Exec("SELECT name, address, batterystatus, lastseen FROM devices WHERE enabled is TRUE")
758 140 rdnzl
  IF rDevice.Available THEN
759 140 rdnzl
    IF rDevice.Count THEN
760 140 rdnzl
      FOR EACH rDevice
761 140 rdnzl
        IF rDevice!batterystatus THEN
762 373 rdnzl
          sMessageBody = sMessageBody & "\r" & rDevice!address & " - " & rDevice!name & "  " & rDevice!batterystatus & "  " & Replace$(Str$(Format(rDevice!lastseen, "yyyy-mm-dd hh:nn:ss")), Format(Date(), "yyyy-mm-dd") & " ", "")
763 140 rdnzl
          bFound = TRUE
764 140 rdnzl
        END IF
765 140 rdnzl
      NEXT
766 140 rdnzl
      ' sent e-mail with lowbatt status
767 245 rdnzl
      IF bFound THEN Mail.SendMail(Application.Name & (": Battery Status"), ("These Devices have their Battery status set: \n") & sMessageBody, Main.sEmailToAddress)
768 140 rdnzl
    END IF
769 2 riemers
  END IF
770 2 riemers
771 2 riemers
END