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 / DomotiGaServer / Bluetooth.module @ 307

History | View | Annotate | Download (5.7 kB)

1
' Gambas module file
2
3
' Description:
4
' Bluetooth.module
5
' Support for Bluetooth proximity, see if a user is around by checking his bluetooth device(s).
6
7
' Development Status:
8
' Just build, bugs around, maybe need to reorganize.
9
10
' DomotiGa - an open source home automation program.
11
' Copyright(C) 2008-2010 Ron Klinkien
12
13
' Read file called COPYING for license details.
14
15
PUBLIC tBluetooth AS Timer
16
PRIVATE sScan AS String
17
18
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19
' start timer for polling
20
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21
PUBLIC FUNCTION Run()
22
23
  ' start poll timer for bluetooth
24
  tBluetooth = NEW Timer AS "tBluetooth"
25
  tBluetooth.Delay = Main.iBluetoothPollTime * 1000 ' multiply for seconds
26
  tBluetooth.Start
27
28
END
29
30
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
' start one of two scan types depending on Threshold setting
32
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
PUBLIC SUB tBluetooth_Timer()
34
35
  IF Main.iBluetoothThreshold = 255 THEN
36
    ScanBluetooth()
37
  ELSE
38
    CheckBluetooth()
39
  END IF
40
41
END
42
43
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
' scan for BT devices, don't calculate RSSI
45
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
PRIVATE SUB CheckBluetooth()
47
48
  DIM rResult, rUser AS Result
49
  DIM iRSSI AS Integer
50
  DIM sRSSI, sProx, sOutput AS String
51
52
  ' get all enabled devices of type blutooth
53
  rResult = Main.hDB.Exec("SELECT * FROM devices WHERE interface = &1 AND enabled is TRUE", Devices.FindInterface("Bluetooth Dongle"))
54
  IF rResult.Available THEN
55
    IF rResult.Count >= 1 THEN
56
      ' check each device
57
      FOR EACH rResult
58
        IF Main.bBluetoothDebug THEN Main.WriteDebugLog(("[Bluetooth] Checking device named '") & rResult!name & ("' with address '") & rResult!address & "'.")
59
        ' check device for signal strenght
60
        sRSSI = GetRSSI(rResult!address)
61
        IF Main.bBluetoothDebug THEN Main.WriteDebugLog("[Bluetooth] " & Replace(sRSSI, gb.NewLine, ""))
62
        IF IsInteger(Val(sRSSI)) THEN
63
          iRSSI = IsInteger(Val(sRSSI))
64
          IF iRSSI > Main.iBluetoothThreshold THEN
65
            sProx = ("Near")
66
          ELSE
67
            sProx = ("Far")
68
          END IF
69
          Devices.ValueUpdate(rResult!id, sProx, Val(sRSSI), "", "")
70
          IF Main.bBluetoothDebug THEN Main.WriteDebugLog(("[Bluetooth] Got RSSI value ") & iRSSI)
71
          ' if signal strenght is within set threshold
72
          IF iRSSI > Main.iBluetoothThreshold THEN
73
            ' is there a user linked to this device?
74
            IF rResult!user THEN
75
              rUser = Main.hDB.Exec("SELECT * FROM users WHERE id = &1", rResult!user)
76
              IF rUser.Available THEN Main.WriteLog(("Bluetooth Proximity detected that '") & rUser!fullname & ("' is home!"))
77
            END IF
78
          END IF
79
        ELSE
80
          Devices.ValueUpdate(rResult!id, ("Away"), "", "", "")
81
        END IF
82
      NEXT
83
    ELSE
84
      Main.WriteLog(("Bluetooth: No Bluetooth device(s) found in device table!"))
85
    END IF
86
  END IF
87
88
END
89
90
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91
' scan bluetooth network
92
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93
PRIVATE FUNCTION ScanBluetooth() AS String
94
95
  SHELL "hcitool -i " & Main.sBluetoothDevice & " scan 2>&1" FOR READ AS "ScanBT"
96
97
END
98
99
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
' got output, save it
101
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
PUBLIC SUB ScanBT_Read()
103
104
  DIM sBuffer AS String
105
106
  READ #LAST, sBuffer, -256
107
  sScan &= sBuffer
108
109
END
110
111
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
' scan bluetooth has finished, process it's output
113
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114
PUBLIC SUB ScanBT_Kill()
115
116
  DIM sLine AS String
117
  DIM aScan AS String[]
118
  DIM iDeviceId AS Integer
119
  DIM rResult, rUser AS Result
120
121
  rResult = Main.hDB.Exec("SELECT * FROM devices WHERE interface = &1 AND enabled is TRUE", Devices.FindInterface("Bluetooth Dongle"))
122
  IF rResult.Available THEN
123
    IF rResult.Count >= 1 THEN
124
      ' check each device
125
      FOR EACH rResult
126
        IF Main.bBluetoothDebug THEN Main.WriteDebugLog(("[Bluetooth] Checking device named '") & rResult!name & ("' with address '") & rResult!address & "'.")
127
        IF InStr(sScan, rResult!address) THEN
128
          Devices.Find(rResult!address, Devices.FindInterface("Bluetooth Dongle"), "Bluetooth Device")
129
          Devices.ValueUpdate(rResult!id, "Home", "", "", "")
130
          ' is there a user linked to this device?
131
          IF rResult!user THEN
132
            rUser = Main.hDB.Exec("SELECT * FROM users WHERE id = &1", rResult!user)
133
            IF rUser.Available THEN Main.WriteLog(("Bluetooth Proximity detected that '") & rUser!fullname & ("' is home!"))
134
          END IF
135
        ELSE
136
          Devices.Find(rResult!address, Devices.FindInterface("Bluetooth Dongle"), "Bluetooth Device")
137
          Devices.ValueUpdate(rResult!id, ("Away"), "", "", "")
138
        END IF
139
      NEXT
140
    ELSE
141
      Main.WriteLog(("Bluetooth: No Bluetooth device(s) found in device table!"))
142
    END IF
143
  END IF
144
145
  ' parse each line
146
  FOR EACH sLine IN Split(sScan, "\n")
147
    ' check for sensor data
148
    IF InStr(sLine, "Scanning") THEN CONTINUE
149
    aScan = Scan(sLine, " * *")
150
    IF aScan.Count = 2 THEN
151
      IF Main.bBluetoothDebug THEN Main.WriteDebugLog(("[Bluetooth] Found device with address '") & aScan[0] & ("' and name '") & aScan[1] & "'.")
152
    END IF
153
  NEXT
154
  sScan = ""
155
156
END
157
158
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
159
' return BT field strenght info for sAddress
160
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
PUBLIC FUNCTION GetRSSI(sAddress AS String) AS String
162
163
  DIM sStrength AS String
164
165
  EXEC ["hcitool", "rssi", sAddress, "2>&1"] TO sStrength
166
  RETURN Replace$(sStrength, "RSSI return value: ", "")
167
168
END