[PlugIn] : BLE Beacon Devices

Python and python framework

Moderator: leecollings

Post Reply
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

[PlugIn] : BLE Beacon Devices

Post by emme »

Ciao,

I'm working in a plugin that would (in my intention) handle a presence based on on BLE beacon devices or, if needed, keep trak of all BLE devices availabe in the network (TVs, remote, apple devices and a really lot of stuff I've seen, is using BLE for beaconing!!!)

In my intention here's the deal

WHAT THE PLUG-IN WILL DO
  • Discover the network and grab al MACs
  • Create a switch devices for each new Mac available (*)(**)
  • Set a state change for each device based on its discoverability (discovered=on, not discovered=off, but check its state fefore)
(*) due to limitation of the framework, the devices will be shown in the "IN USE" page instead of the unsed device tab)
(**) due to limitation of the framework and mostly in my python knowledge the name of each device MUST end with the MAC address, otherwise the device will be recreated


WHAT THE PLUG-IN WILL DO in next release
  • Get Battery level (if provided)
  • Put the battery data in the BatteryLevel of each device
by that time notification about low battery are system handled, so the plugin will not need to perform it.

WHERE I'M STUCK
I'm using Python3 and gattlib to grab BLE data
but the library seems to be unabailable under domoticz.... :cry:

Code: Select all

2017-03-06 16:53:09.107 Error: (BLE Beacon) failed to load 'plugin.py', Python Path used was '/home/pi/domoticz/plugins/BLE-BEACON/:/usr/lib/python3.4/:/usr/lib/python3.4/plat-arm-linux-gnueabihf:/usr/lib/python3.4/lib-dynload'.
2017-03-06 16:53:09.107 Error: (net_Ble) Module Import failed, exception: 'ImportError'
2017-03-06 16:53:09.107 Error: (net_Ble) Module Import failed: ' Name: gattlib'
[/size]
How could I include this library?!
if I run another script from ssh (sudo python3 ble_discovery.py) that include the same library it runs correctly...

PLEASE NOTE:
the attached PlugIn is NOT finalized and should be NOT used into a production environment
Since I cannot test its functionality I have NO IDEA of the inpact on domoticz yet :P :P :P
Attachments
BLEDiscovery.zip
plugin
(1.18 KiB) Downloaded 59 times
The most dangerous phrase in any language is:
"We always done this way"
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by zak45 »

Nice project... afraid will not run windows :-(

(*) due to limitation of the framework, the devices will be shown in the "IN USE" page instead of the unsed device tab)

--> on the last beta, you can put device in unused tab.

if you want to import some modules, you need to indicate the path, something like that :

# Required to import samsungctl, path is OS dependent
# Python framework in Domoticz do not include OS dependent path
#
import sys
import os

if sys.platform.startswith('linux'):
# linux specific code here
sys.path.append(os.path.dirname(os.__file__) + '/site-packages')
elif sys.platform.startswith('darwin'):
# mac
sys.path.append(os.path.dirname(os.__file__) + '/site-packages')
elif sys.platform.startswith('win32'):
# win specific
sys.path.append(os.path.dirname(os.__file__) + '\site-packages')
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by emme »

Thank you so much!!! :D :D

...I move forward to this step.. ut actually I have another problem....
looks like my plugin simply does..... nothing....
not even the onStart() callback seems to work....

heres the cose....

Code: Select all

#                  BLE Beacon Plugin
#          Presence Detection Module
#                  Author:       MrEmme 2017
#
#  NOTE: Since there are no other ways to index in a unique manner
#        the BLEs, you MUST leave the MAC Address in the name of the 
#        Created Switch (last 12 digits)

"""
<plugin key="ble-beacon" name="BLE Beacon Devices" author="mr.emme" externallink="http://domoticz.com/forum/viewtopic.php?f=65&t=16$
        <params>
                <param field="Mode1" label="HCI Interface" width="150px" required="true" default="hci0"/>
                <param field="Mode2" label="Discovery Interval (secs)" width="150px" required="true" default="10"/>
                <param field="Mode3" label="Battery Service Handle ID" width="150px" required="true" default="0x2a19"/>
                <param field="Mode6" label="Debug" width="75px">
                        <options>
                                <option label="True" value="Debug"/>
                                <option label="False" value="Normal"  default="true" />
                        </options>
                </param>
        </params>
</plugin>

"""

import sys
import os
import Domoticz
sys.path.append('/usr/local/lib/python3.4/dist-packages/')

from gattlib import DiscoveryService
from gattlib import GATTRequester

class BasePlugin:

   def onStart(self):
      if Parameters["Mode6"] == "Debug":
         Domoticz.Debugging(1)
         DumpConfigToLog
      discDevices
      checkDevices
      return True

   def onHeartbeat():
      discDevices
      checkDevices
      return True

   def onStop(self):
      return True

def discDevices():
   global SVCdev
   BLEsvc = DiscoveryService(Mode1)
   SVCdev = service.discover(int(Mode2))
   if Parameters["Mode6"] == "Debug" :
      for address, name in SVCdev.items():
         Domoticz.Debug("[BLE PLUGIN] MAC Address: {}, Name: {}".format(address, name))
   return
def checkDevices():
   global SVCdev
   for address, name in SVCdev.items():
      dzNewDev = True
      for dzDevList in Devices:
         if address != (Devices[dzDevList].Name[-12]) :
            dzNewDev = False
      if dzNewDev == True :
         Domoticz.Device(Name="NEW BLE Beacon " + address, Unit=(len(Devices))+1, TypeName="Switch").Create()
         Devices.Refresh()
         Device["NEW BLE Beacon " + address].Update(nValue=1,sValue="On")
   Devices.Refresh()

   for dzDevList in Devices:
      dzDevAvail = False
      for address, name in SVCdev.items():
         if (address != (Devices[dzDevList].Name[-12])) and Devices[dzDevList].sValue=="Off" :
            Device[dzDevList].Update(nValue=1,sValue="On")
            dzDevNotAvail == True

      if dzDevAvail == False :
         Device[dzDevList].Update(nValue=0,sValue="Off")

   Devices.Refresh()
   return

def DumpConfigToLog():
   for x in Parameters:
      if Parameters[x] != "":
         Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
      Domoticz.Debug("Device count: " + str(len(Devices)))
      for x in Devices:
         Domoticz.Log("Device:           " + str(x) + " - " + str(Devices[x]))
         Domoticz.Log("Device ID:        " + str(Devices[x].ID) + "'")
         Domoticz.Log("Device Name:      " + Devices[x].Name + "'")
         Domoticz.Log("Device nValue:    " + str(Devices[x].nValue))
         Domoticz.Log("Device sValue:    " + Devices[x].sValue + "'")
         Domoticz.Log("Device LastLevel: " + str(Devices[x].LastLevel))
   return

global SVCdev
global _plugin
_plugin = BasePlugin()


def onStart():
   DumpConfigToLog
   Domoticz.Log("PlugIn BLE Started!")
   global _plugin
   _plugin.onStart()

def onStop():
   global _plugin
   _plugin.onStop()

def onHeartbeat():
   global _plugin
   _plugin.onHearbeat()
do you have an idea....where am I worng?! :(
Last edited by emme on Tuesday 07 March 2017 8:23, edited 1 time in total.
The most dangerous phrase in any language is:
"We always done this way"
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by Dnpwwo »

@emme,

The onStart inside the class needs to take a "self" parameter. Have a look at some of the examples, all the class callbacks need it. It's a Python thing.

Interested to talk about the mechanisms you could use to not create duplicate devices. You could create a local file to hold a mapping or I could enhance the framework. Domoticz doesn't really have a natural place to store MAC addresses but maybe you could use it as the device ID. Currently I generate one but I could allow it to be specified.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by emme »

Thank you Dnpwwo
the (self) was there before, I've removed just to try to generate an error to see if it working or not.
I have place them back.. but... simply nothing happend.. no messages, no errors, no warning... nothing :roll: :?

even if the first thing is to dump the configuration (specified in the onStart out the class)
(I've updated the script on my previous post adding the (self) :P

about indexing...
It would be ok to use IDX, I'm also thinking to use Type & subType (by splitting the mac in 2 halfs) but it would be not a good idea since those are not available in the Devices table for quering

I have no big experience in the file handling in python, but... I could learn it!
the file would have a structure like MAC - IDX (maybe a JSON that would be easier to import!)
The most dangerous phrase in any language is:
"We always done this way"
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by Dnpwwo »

@emme,

Type and subtype can't be used because they control how the device is displayed. Device ID is not used for much and can be set to whatever the hardware defines as the unique identifier (I think). It can certainly hold 8 hex characters.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by emme »

we can cut the first 4 chars of the mac... how many possibility we could have to have 2 different vendor with the same serial ?! :P :P :P


There is something wired with my environment :x
I've tried to installa the Basic Plugin included in the exabple... and it does not run either...
2017-03-07 08:52:30.788 Error: plug_test hardware (54) thread seems to have ended unexpectedly
The most dangerous phrase in any language is:
"We always done this way"
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by Dnpwwo »

@emme,

Are you running on Windows with the language set to something other than English? There was another user that said his Python config was messed up until he changed language to English.

Can code looks okay, no obvious reason why it isn't running. Can you include a dump of your log file?

If you have a look here: http://www.domoticz.com/wiki/index.php? ... &section=7 under __init__ I've added a DeviceID parameter when creating devices, there can be up to 25 characters in length. If the MAC Address is the unique identifier for your devices I don't think we are breaking the Domoticz data model using it that way.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by emme »

uh.. no I'm using Raspberry PI3 with Jessie and domoticz installed from sources (not running the Image)

well I have the system in Italian.... not bother to change to English and have it runs that way

I'll check theis evening once back from work the new ID... and will be back here

as now.. THANK YOU very much for your support and effort!! :D


=== EDIT ===
Domoticz should be set to english? or the system (linux) should?
because I have all locales setted to European/Italian (of course the system is english)
The most dangerous phrase in any language is:
"We always done this way"
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by emme »

there's nothing more than this in the log:
2017-03-07 17:39:00.694 Error: plugIn_BLE hardware (53) thread seems to have ended unexpectedly
2017-03-07 17:39:30.701 Error: plugIn_BLE hardware (53) thread seems to have ended unexpectedly
2017-03-07 17:40:02.561 Error: plugIn_BLE hardware (53) thread seems to have ended unexpectedly
2017-03-07 17:40:30.567 Error: plugIn_BLE hardware (53) thread seems to have ended unexpectedly
2017-03-07 17:41:00.574 Error: plugIn_BLE hardware (53) thread seems to have ended unexpectedly
Domoticz moved to English language... no change :(
could it be I messed up python3 or the plugin framework?
is there a way I can try to reinstall domoticz without loosing my config? (I assume: backup, delete, reinstall, restore :P :P)
The most dangerous phrase in any language is:
"We always done this way"
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

Re: [PlugIn] : BLE Beacon Devices

Post by zak45 »

I had, more or less, similar problem with my Samsung TV plugin until Dnpwwo point me to the timeout.
Maybe same case as I presume the Bluetooth function take some time to execute.
what I have discovered is that when Heartbeat < Timeout I got thread error and see nothing running in domoticz log.
What you can try :
set Domoticz.Heartbeat(xx) with xx bigger than time your function need to run.
and/or set timeout in this way:
socket.setdefaulttimeout(xx) ( or similar)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest