|
| 1 | +# UDP Discovery Example |
| 2 | +# |
| 3 | +# Logs traffic on the Dynamic Device Discovery port |
| 4 | +# |
| 5 | +# Works on Linux, Windows appears to filter out UDP from the network even when the firewall is set to allow it |
| 6 | +# |
| 7 | +# Author: Dnpwwo, 2017 |
| 8 | +# |
| 9 | +""" |
| 10 | +<plugin key="UdpDiscover" name="UDP Discovery Example" author="dnpwwo" version="1.0.0"> |
| 11 | + <params> |
| 12 | + <param field="Address" label="IP Address" width="200px" required="true" default="239.255.250.250"/> |
| 13 | + <param field="Port" label="Port" width="200px" required="true" default="9131"/> |
| 14 | + <param field="Mode6" label="Debug" width="100px"> |
| 15 | + <options> |
| 16 | + <option label="True" value="Debug"/> |
| 17 | + <option label="File Only" value="File" /> |
| 18 | + <option label="False" value="Normal" default="true" /> |
| 19 | + </options> |
| 20 | + </param> |
| 21 | + </params> |
| 22 | +</plugin> |
| 23 | +""" |
| 24 | +import Domoticz |
| 25 | + |
| 26 | +class BasePlugin: |
| 27 | + BeaconConn = None |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + return |
| 31 | + |
| 32 | + def onStart(self): |
| 33 | + if Parameters["Mode6"] == "Debug": |
| 34 | + Domoticz.Debugging(1) |
| 35 | + DumpConfigToLog() |
| 36 | + |
| 37 | + if Parameters["Mode6"] != "Normal": |
| 38 | + logFile = open(Parameters["HomeFolder"]+Parameters["Key"]+".log",'w') |
| 39 | + |
| 40 | + self.BeaconConn = Domoticz.Connection(Name="Beacon", Transport="UDP/IP", Address=Parameters["Address"], Port=str(Parameters["Port"])) |
| 41 | + self.BeaconConn.Listen() |
| 42 | + |
| 43 | + def onMessage(self, Connection, Data): |
| 44 | + try: |
| 45 | + strData = Data.decode("utf-8", "ignore") |
| 46 | + Domoticz.Log("onMessage called from: "+Connection.Address+":"+Connection.Port+" with data: "+strData) |
| 47 | + Domoticz.Debug("Connection detail: : "+str(Connection)) |
| 48 | + |
| 49 | + if Parameters["Mode6"] != "Normal": |
| 50 | + logFile = open(Parameters["HomeFolder"]+Parameters["Key"]+".log",'a') |
| 51 | + logFile.write(Connection.Name+" ("+Connection.Address+"): "+strData+"\n") |
| 52 | + logFile.close() |
| 53 | + |
| 54 | + except Exception as inst: |
| 55 | + Domoticz.Error("Exception in onMessage, called with Data: '"+str(strData)+"'") |
| 56 | + Domoticz.Error("Exception detail: '"+str(inst)+"'") |
| 57 | + raise |
| 58 | + |
| 59 | +global _plugin |
| 60 | +_plugin = BasePlugin() |
| 61 | + |
| 62 | +def onStart(): |
| 63 | + global _plugin |
| 64 | + _plugin.onStart() |
| 65 | + |
| 66 | +def onMessage(Connection, Data): |
| 67 | + global _plugin |
| 68 | + _plugin.onMessage(Connection, Data) |
| 69 | + |
| 70 | + # Generic helper functions |
| 71 | +def DumpConfigToLog(): |
| 72 | + for x in Parameters: |
| 73 | + if Parameters[x] != "": |
| 74 | + Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'") |
| 75 | + Domoticz.Debug("Device count: " + str(len(Devices))) |
| 76 | + for x in Devices: |
| 77 | + Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x])) |
| 78 | + Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'") |
| 79 | + Domoticz.Debug("Device Name: '" + Devices[x].Name + "'") |
| 80 | + Domoticz.Debug("Device nValue: " + str(Devices[x].nValue)) |
| 81 | + Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'") |
| 82 | + Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel)) |
| 83 | + return |
0 commit comments