Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TurnTouch Python Library
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anton Sarukhanov
TurnTouch Python Library
Commits
b6c6de3b
Commit
b6c6de3b
authored
6 years ago
by
Anton Sarukhanov
Browse files
Options
Downloads
Patches
Plain Diff
Refactor all scanning stuff into one function.
parent
3fb59626
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
turntouch/__init__.py
+34
-36
34 additions, 36 deletions
turntouch/__init__.py
with
34 additions
and
36 deletions
turntouch/__init__.py
+
34
−
36
View file @
b6c6de3b
...
...
@@ -4,47 +4,11 @@ from bluepy import btle
from
typing
import
List
TT_DEVICE_NAME
=
'
Turn Touch Remote
'
TT_SHORT_DEVICE_NAME
=
'
Turn Touch Rem
'
BLE_SHORT_DEVICE_NAME
=
0x08
BLE_COMPLETE_DEVICE_NAME
=
0x09
logger
=
logging
.
getLogger
(
'
TurnTouch
'
)
class
DeviceFoundException
(
Exception
):
"""
Exception thrown when a device is found to abort further scanning.
"""
pass
class
ScanDelegate
(
btle
.
DefaultDelegate
):
"""
Handle callbacks for devices discovered by a BLE scan.
"""
only_one
=
False
def
__init__
(
self
,
only_one
=
False
,
*
args
,
**
kwargs
):
self
.
only_one
=
only_one
super
(
ScanDelegate
,
self
).
__init__
(
*
args
,
**
kwargs
)
def
handleDiscovery
(
self
,
device
,
is_new_device
,
is_new_data
):
"""
When a Turn Touch device is discovered, log a message.
If only searching for one device, short-circuit the scan by raising
a DeviceFoundException once we
'
ve found a Turn Touch device.
"""
if
is_new_device
and
is_turn_touch
(
device
):
logger
.
info
(
"
Discovered device {address}
"
.
format
(
address
=
device
.
addr
))
if
self
.
only_one
:
raise
DeviceFoundException
()
class
TurnTouch
(
btle
.
Peripheral
):
"""
A Turn Touch smart home remote.
"""
pass
def
is_turn_touch
(
device
:
btle
.
ScanEntry
)
->
bool
:
"""
Determine whether a device found by btle.Scanner is a Turn Touch.
"""
short_name
=
device
.
getValueText
(
BLE_SHORT_DEVICE_NAME
)
name
=
device
.
getValueText
(
BLE_COMPLETE_DEVICE_NAME
)
return
(
name
==
TT_DEVICE_NAME
or
short_name
==
TT_SHORT_DEVICE_NAME
)
def
scan
(
device_index
:
int
=
0
,
...
...
@@ -55,6 +19,40 @@ def scan(device_index: int = 0,
:param timeout float: Scanning timeout, in seconds
:param only_one float: Stop scanning after one Turn Touch is found
:return list: Found TurnTouch devices
"""
TT_DEVICE_NAME
=
'
Turn Touch Remote
'
TT_SHORT_DEVICE_NAME
=
'
Turn Touch Rem
'
BLE_SHORT_DEVICE_NAME
=
0x08
BLE_COMPLETE_DEVICE_NAME
=
0x09
class
DeviceFoundException
(
Exception
):
"""
Exception thrown when a device is found.
"""
pass
class
ScanDelegate
(
btle
.
DefaultDelegate
):
"""
Handle callbacks for devices discovered by a BLE scan.
"""
only_one
=
False
def
__init__
(
self
,
only_one
=
False
,
*
args
,
**
kwargs
):
self
.
only_one
=
only_one
super
(
ScanDelegate
,
self
).
__init__
(
*
args
,
**
kwargs
)
def
handleDiscovery
(
self
,
device
,
is_new_device
,
is_new_data
):
"""
When a Turn Touch device is discovered, log a message.
If only searching for one device, short-circuit the scan by raising
a DeviceFoundException once we
'
ve found a Turn Touch device.
"""
if
is_new_device
and
is_turn_touch
(
device
):
logger
.
info
(
"
Discovered device {address}
"
.
format
(
address
=
device
.
addr
))
if
self
.
only_one
:
raise
DeviceFoundException
()
def
is_turn_touch
(
device
:
btle
.
ScanEntry
)
->
bool
:
"""
Determine whether a ScanEntry (device) is a Turn Touch.
"""
short_name
=
device
.
getValueText
(
BLE_SHORT_DEVICE_NAME
)
name
=
device
.
getValueText
(
BLE_COMPLETE_DEVICE_NAME
)
return
name
==
TT_DEVICE_NAME
or
short_name
==
TT_SHORT_DEVICE_NAME
scanner
=
btle
.
Scanner
(
device_index
)
try
:
scanner
.
withDelegate
(
ScanDelegate
(
only_one
)).
scan
(
timeout
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment