I made a plugin to activate or deactivate motion record from Domoticz.
You need to have a Unifi server and activate API access from user information.
In attachements to zip files for cam logo.
Code: Select all
# UnifiVideo Plugin
# Author: Gysmo, 2017
# Version: 0.1
#
# v0.1 : Initial release
"""
<plugin key="UVC" version="0.1" name="UVC_Plugin" author="gysmo" wikilink="http://www.domoticz.com/wiki/Plugins/UnifiVideo.html" externallink="http://video.ubnt.com">
<params>
<param field="Address" label="IP Address" width="200px" required="true" />
<param field="Port" label="Port" width="200px" required="true" default="7080" />
<param field="Mode1" label="APIKey" width="200px" required="true" />
<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 Domoticz
import time
import base64
import json
from urllib.parse import urlencode
from urllib.request import Request, urlopen, HTTPError
unifiCameras = []
class BasePlugin:
enabled = False
def __init__(self):
#self.var = 123
return
def onStart(self):
Domoticz.Log("onStart called")
if ("myPlugin" not in Images):
Domoticz.Image('cam_g3.zip').Create()
if ("myPluginAlt1" not in Images):
Domoticz.Image('cam_g3_dome.zip').Create()
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
self.unifiInitInfos()
if (len(Devices) == 0):
# Create Camera motion switch
for cam in unifiCameras:
Domoticz.Log("Creating camera " + cam['name'])
OptionsMode = {"LevelActions": "|","LevelNames": "Off|Record on motion","LevelOffHidden": "false","SelectorStyle": "1"}
if (cam['model'] == 'UVC G3'):
model = 'UVC'
elif (cam['model'] == 'UVC G3 Dome'):
model = 'UVCDome'
Domoticz.Device(Name=cam['name'], Unit=cam['dom_id'], TypeName="Selector Switch", Image=Images[model].ID, Options=OptionsMode, Used=1).Create()
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Data, Status, Extra):
Domoticz.Log("onMessage called")
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
if (Level == 0):
self.unifiSetRecording("Off",Unit)
Devices[Unit].Update(0, str(0))
elif (Level == 10):
self.unifiSetRecording("Motion",Unit)
Devices[Unit].Update(1, str(10))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self):
Domoticz.Log("onDisconnect called")
def onHeartbeat(self):
Domoticz.Log("onHeartbeat called")
# Function define for script
def unifiSetRecording(self,mode,domID):
camID = ""
for cam in unifiCameras:
if (cam['dom_id'] == domID):
camID = cam['unifi_id']
name = cam['name']
url = "http://"+Parameters["Address"]+":"+Parameters["Port"]+"/api/2.0/camera/"+camID+"?apiKey="+Parameters["Mode1"]
headers = {'Content-Type': 'application/json'}
if (mode == "Off"):
post_fields = bytes('{"name":"'+name+'","recordingSettings":{"motionRecordEnabled":"false"}}','utf-8')
elif (mode == "Motion"):
post_fields = bytes('{"name":"'+name+'","recordingSettings":{"motionRecordEnabled":"true","channel":"0"}}','utf-8')
request = Request(url=url,data=post_fields,headers=headers,method='PUT')
try:
nvrResponse = urlopen(request)
dataResponse = nvrResponse.read().decode()
jsonResponse = json.loads(dataResponse)
Domoticz.Debug("Update camera OK")
except HTTPError as e:
Domoticz.Debug("Update camera Error: " + str(e))
return True
def unifiInitInfos(self):
url = "http://"+Parameters["Address"]+":"+Parameters["Port"]+"/api/2.0/camera/?apiKey="+Parameters["Mode1"]
headers = {'Content-Type': 'application/json'}
request = Request(url=url)
try:
nvrResponse = urlopen(request)
except HTTPError as e:
Domoticz.Debug("Error code" + str(e))
dataResponse = nvrResponse.read().decode()
jsonResponse = json.loads(dataResponse)
Domoticz.Debug("Found "+ str(len(jsonResponse["data"]))+" camera(s)")
#INIT CAMERAS
dom_id = 1
for cam in jsonResponse["data"]:
unifiCamera = {}
unifiCamera['name'] = cam["name"]
unifiCamera['unifi_id'] = cam["_id"]
unifiCamera['dom_id'] = dom_id
unifiCamera['model'] = cam["model"]
unifiCameras.append(unifiCamera)
dom_id = dom_id + 1
return True
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Status, Description):
global _plugin
_plugin.onConnect(Status, Description)
def onMessage(Data, Status, Extra):
global _plugin
_plugin.onMessage(Data, Status, Extra)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect():
global _plugin
_plugin.onDisconnect()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
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.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return
Fred