@@ -81,7 +81,7 @@ class DahuaDevice():
81
81
82
82
83
83
84
- def __init__ (self , name , device_cfg , client , basetopic , homebridge ):
84
+ def __init__ (self , name , device_cfg , client , basetopic , homebridge , publishImages ):
85
85
if device_cfg ["channels" ]:
86
86
self .channels = device_cfg ["channels" ]
87
87
else :
@@ -104,6 +104,7 @@ def __init__(self, name, device_cfg, client, basetopic, homebridge):
104
104
self .basetopic = basetopic
105
105
self .homebridge = homebridge
106
106
self .snapshotoffset = device_cfg .get ("snapshotoffset" )
107
+ self .publishImages = publishImages
107
108
108
109
#generate the event url
109
110
self .url = self .EVENT_TEMPLATE .format (
@@ -171,7 +172,7 @@ def channelIsMine(self,channelname="",channelid=-1):
171
172
return - 1
172
173
173
174
174
- def SnapshotImage (self , channel , channelName , message ,nopublish = False ):
175
+ def SnapshotImage (self , channel , channelName , message ,publishImages = False ):
175
176
"""Takes a snap shot image for the specified channel
176
177
channel (index number starts at 1)
177
178
channelName if known for messaging
@@ -199,28 +200,29 @@ def SnapshotImage(self, channel, channelName, message,nopublish=False):
199
200
#construct image payload
200
201
#{{ \"message\": \"Motion Detected: {0}\", \"imagebase64\": \"{1}\" }}"
201
202
imagepayload = (base64 .encodebytes (image )).decode ("utf-8" )
202
- msgpayload = json .dumps ({"message" :message ,"imagebase64" :imagepayload })
203
+ if publishImages :
204
+ msgpayload = json .dumps ({"message" :message ,"imagebase64" :imagepayload })
205
+ else :
206
+ msgpayload = json .dumps ({"message" :message ,"imagurl" :imageurl })
203
207
#msgpayload = "{{ \"message\": \"{0}\", \"imagebase64\": \"{1}\" }}".format(message,imgpayload)
204
208
205
- if not nopublish :
206
- self . client . publish ( self . basetopic + "/{0}/Image" . format ( channelName ), msgpayload )
209
+ self . client . publish ( self . basetopic + "/{0}/Image" . format ( channelName ), msgpayload )
210
+
207
211
except Exception as ex :
208
212
_LOGGER .error ("Error sending image: " + str (ex ))
209
213
try :
210
214
211
215
with open ("default.png" , 'rb' ) as thefile :
212
216
imagepayload = thefile .read ().encode ("base64" )
213
217
msgpayload = json .dumps ({"message" :"ERR:" + message , "imagebase64" : imagepayload })
214
- if not nopublish :
215
- self .client .publish (self .basetopic + "/{0}/Image" .format (channelName ),msgpayload )
216
218
except :
217
219
pass
218
220
219
221
220
222
return image
221
223
222
224
223
- def SearchImages (self ,channel ,starttime , endtime , events ,nopublish = True , message = '' ,delay = 0 ):
225
+ def SearchImages (self ,channel ,starttime , endtime , events ,publishImages = False , message = '' ,delay = 0 ):
224
226
"""Searches for images for the channel
225
227
channel is a numerical channel index (starts at 1)
226
228
starttime is the start search time
@@ -364,13 +366,12 @@ def SearchImages(self,channel,starttime, endtime, events,nopublish=True, message
364
366
365
367
if image is not None :
366
368
imagepayload = (base64 .encodebytes (image )).decode ("utf-8" )
367
- msgpayload = json .dumps ({"message" :message ,"imagebase64" :imagepayload })
369
+ if publishImages :
370
+ msgpayload = json .dumps ({"message" :message ,"imagebase64" :imagepayload })
371
+ else :
372
+ msgpayload = json .dumps ({"message" :message ,"imageurl" :loadurl })
368
373
#msgpayload = "{{ \"message\": \"{0}\", \"imagebase64\": \"{1}\" }}".format(message,imgpayload)
369
374
370
- if not nopublish :
371
- _LOGGER .debug ("SearchImages: Publishing " + filepath )
372
- self .client .publish (self .basetopic + "/{0}/Image" .format (channelName ),msgpayload )
373
-
374
375
#imageio.mimsave('movie.gif',images, duration=1.5)
375
376
#try:
376
377
# slack = Slacker(self.token)
@@ -407,7 +408,7 @@ def SearchImages(self,channel,starttime, endtime, events,nopublish=True, message
407
408
return ""
408
409
#def SearchImages(self,channel,starttime, endtime, events,nopublish=True, message='',delay=0):
409
410
410
- def SearchClips (self ,channel ,starttime , endtime , events ,nopublish = True , message = '' ,delay = 0 ):
411
+ def SearchClips (self ,channel ,starttime , endtime , events ,publishImages = False , message = '' ,delay = 0 ):
411
412
"""Searches for clips for the channel
412
413
channel is a numerical channel index (starts at 1)
413
414
starttime is the start search time
@@ -537,7 +538,7 @@ def SearchClips(self,channel,starttime, endtime, events,nopublish=True, message=
537
538
loadurl = MEDIA_LOADFILE .format (host = self .host ,protocol = self .protocol ,port = self .port ,file = filepath )
538
539
_LOGGER .debug ("SearchClips: LoadUrl: " + loadurl )
539
540
#result = s.get(loadurl,auth=auth,cookies=cookies,stream=True)
540
- if not nopublish :
541
+ if not publishImages :
541
542
_LOGGER .debug ("SearchClips: Publishing " + filepath )
542
543
self .client .publish (self .basetopic + "/{0}/Clip" .format (channelName ),loadurl )
543
544
#result = s.get(loadurl,auth=auth,cookies=cookies)
@@ -550,7 +551,7 @@ def SearchClips(self,channel,starttime, endtime, events,nopublish=True, message=
550
551
loadurl = MEDIA_LOADFILE .format (host = self .host ,protocol = self .protocol ,port = self .port ,file = filepath )
551
552
_LOGGER .debug ("SearchImages: LoadUrl: " + loadurl )
552
553
#result = s.get(loadurl,auth=auth,cookies=cookies,stream=True)
553
- if not nopublish :
554
+ if not publishImages :
554
555
_LOGGER .debug ("SearchClips: Publishing " + filepath )
555
556
self .client .publish (self .basetopic + "/{0}/Clip" .format (channelName ),loadurl )
556
557
#result = s.get(loadurl,auth=auth,cookies=cookies)
@@ -658,19 +659,19 @@ def OnReceive(self, data):
658
659
if self .alerts :
659
660
#possible new process:
660
661
#http://192.168.10.66/cgi-bin/snapManager.cgi?action=attachFileProc&Flags[0]=Event&Events=[VideoMotion%2CVideoLoss]
661
- process = threading .Thread (target = self .SnapshotImage ,args = (index + self .snapshotoffset ,Alarm ["channel" ],"Motion Detected: {0}" .format (Alarm ["channel" ])))
662
+ process = threading .Thread (target = self .SnapshotImage ,args = (index + self .snapshotoffset ,Alarm ["channel" ],"Motion Detected: {0}" .format (Alarm ["channel" ]), self . publishImages ))
662
663
process .daemon = True # Daemonize thread
663
664
process .start ()
664
665
else : #if Alarm["action"] == "Start":
665
666
eventStart = False
666
667
self .client .publish (self .basetopic + "/" + Alarm ["Code" ] + "/" + Alarm ["channel" ] ,"ON" )
667
668
self .client .publish (self .basetopic + "/" + Alarm ["channel" ] + "/event" ,"OFF" )
668
- _LOGGER .info ("ReceiveData: calling search Clips" )
669
- starttime = datetime .datetime .now () - datetime .timedelta (minutes = 1 )
670
- endtime = datetime .datetime .now ()
671
- process2 = threading .Thread (target = self .SearchClips ,args = (index + self .snapshotoffset ,starttime ,endtime ,'' ,False ,'Search Clips VideoMotion' ,30 ))
672
- process2 .daemon = True # Daemonize thread
673
- process2 .start ()
669
+ # _LOGGER.info("ReceiveData: calling search Clips")
670
+ # starttime = datetime.datetime.now() - datetime.timedelta(minutes=1)
671
+ # endtime = datetime.datetime.now()
672
+ # process2 = threading.Thread(target=self.SearchClips,args=(index+self.snapshotoffset,starttime,endtime,'',False,'Search Clips VideoMotion',30,self.publishImages ))
673
+ # process2.daemon = True # Daemonize thread
674
+ # process2.start()
674
675
elif Alarm ["Code" ] == "AlarmLocal" :
675
676
_LOGGER .info ("Alarm Local received: " + Alarm ["name" ] + " Index: " + str (index ) + " Code: " + Alarm ["Code" ])
676
677
# Start action reveived, turn alarm on.
@@ -706,18 +707,18 @@ def OnReceive(self, data):
706
707
if self .alerts :
707
708
#possible new process:
708
709
#http://192.168.10.66/cgi-bin/snapManager.cgi?action=attachFileProc&Flags[0]=Event&Events=[VideoMotion%2CVideoLoss]
709
- process = threading .Thread (target = self .SnapshotImage ,args = (index + self .snapshotoffset ,Alarm ["channel" ],"IVS: {0}: {1}" .format (Alarm ["channel" ],regionText )))
710
+ process = threading .Thread (target = self .SnapshotImage ,args = (index + self .snapshotoffset ,Alarm ["channel" ],"IVS: {0}: {1}" .format (Alarm ["channel" ],regionText , self . publishImages )))
710
711
process .daemon = True # Daemonize thread
711
712
process .start ()
712
713
else : #if Alarm["action"] == "Start":
713
714
eventStart = False
714
715
self .client .publish (self .basetopic + "/" + Alarm ["channel" ] + "/event" ,"OFF" )
715
- _LOGGER .info ("ReceiveData: calling search Clips" )
716
- starttime = datetime .datetime .now () - datetime .timedelta (minutes = 1 )
717
- endtime = datetime .datetime .now ()
718
- process2 = threading .Thread (target = self .SearchClips ,args = (index + self .snapshotoffset ,starttime ,endtime ,'' ,False ,'Search Clips IVS' ,30 ))
719
- process2 .daemon = True # Daemonize thread
720
- process2 .start ()
716
+ # _LOGGER.info("ReceiveData: calling search Clips")
717
+ # starttime = datetime.datetime.now() - datetime.timedelta(minutes=1)
718
+ # endtime = datetime.datetime.now()
719
+ # process2 = threading.Thread(target=self.SearchClips,args=(index+self.snapshotoffset,starttime,endtime,'',False,'Search Clips IVS',30,self.publishImages ))
720
+ # process2.daemon = True # Daemonize thread
721
+ # process2.start()
721
722
722
723
else : #if Alarm["Code"] == "VideoMotion": - unknown event
723
724
_LOGGER .info ("dahua_event_received: " + Alarm ["name" ] + " Index: " + Alarm ["channel" ] + " Code: " + Alarm ["Code" ])
@@ -813,7 +814,9 @@ def __init__(self, mqtt, cameras):
813
814
814
815
for device_cfg in cameras :
815
816
816
- device = DahuaDevice (device_cfg .get ("name" ), device_cfg , self .client ,self .basetopic , self .homebridge )
817
+ device = DahuaDevice (device_cfg .get ("name" ), device_cfg , self .client ,self .basetopic , self .homebridge , mqtt ["mqttimages" ])
818
+
819
+ _LOGGER .info ("Device %s created on Url %s. Alert Status: %s" % (device .Name , device .host , device .alerts ))
817
820
818
821
self .Devices .append (device )
819
822
@@ -911,9 +914,12 @@ def mqtt_on_connect(self, client, userdata, flags, rc):
911
914
#else:
912
915
# state = "OFF"
913
916
914
- #for device in self.Devices:
915
- # device.alerts = state
916
- # self.client.publish(self.basetopic +"/" + device.Name + "/alerts/state",state,qos=0,retain=True)
917
+ for device in self .Devices :
918
+ if device .alerts :
919
+ state = "ON"
920
+ else :
921
+ state = "OFF"
922
+ self .client .publish (self .basetopic + "/" + device .Name + "/alerts/state" ,state ,qos = 0 ,retain = True )
917
923
self .client .subscribe (self .basetopic + "/+/picture" )
918
924
self .client .subscribe (self .basetopic + "/+/alerts" )
919
925
@@ -943,7 +949,7 @@ def mqtt_on_picture_message(self,client, userdata, msg):
943
949
datestring = (msg .payload ).decode ()
944
950
d = datetime .datetime .strptime (datestring , "%Y-%m-%dT%H:%M:%S" )
945
951
#def SearchImages(self,channel,starttime, endtime, events):
946
- device .SearchImages (channel + device .snapshotoffset ,d ,d + datetime .timedelta (minutes = 2 ),'' ,nopublish = False ,message = "Snap Shot Image" )
952
+ device .SearchImages (channel + device .snapshotoffset ,d ,d + datetime .timedelta (minutes = 2 ),'' ,publishImages = device . publishImages ,message = "Snap Shot Image" )
947
953
except Exception as searchI :
948
954
_LOGGER .warn ("Error searching images: " + str (searchI ))
949
955
d = None
@@ -1002,14 +1008,14 @@ def mqtt_on_cross_message(self,client, userdata, msg):
1002
1008
#temp = cp.get(camera_key,"host")
1003
1009
camera ["host" ] = cp .get (camera_key ,'host' )
1004
1010
camera ["protocol" ] = cp .get (camera_key ,'protocol' )
1005
- camera ["isNVR" ] = cp .get (camera_key ,'isNVR' )
1011
+ camera ["isNVR" ] = cp .getboolean (camera_key ,'isNVR' , fallback = False )
1006
1012
camera ["name" ] = cp .get (camera_key ,'name' )
1007
1013
camera ["port" ] = cp .getint (camera_key ,'port' )
1008
1014
camera ["user" ] = cp .get (camera_key ,'user' )
1009
1015
camera ["pass" ] = cp .get (camera_key ,'pass' )
1010
1016
camera ["auth" ] = cp .get (camera_key ,'auth' )
1011
1017
camera ["events" ] = cp .get (camera_key ,'events' )
1012
- camera ["alerts" ] = cp .get (camera_key ,"alerts" ,fallback = True )
1018
+ camera ["alerts" ] = cp .getboolean (camera_key ,"alerts" ,fallback = True )
1013
1019
channels = {}
1014
1020
if cp .has_option (camera_key ,'channels' ):
1015
1021
try :
@@ -1043,8 +1049,8 @@ def mqtt_on_cross_message(self,client, userdata, msg):
1043
1049
mqtt ["basetopic" ] = cp .get ("MQTT Broker" ,"BaseTopic" )
1044
1050
mqtt ["user" ] = cp .get ("MQTT Broker" ,"user" ,fallback = None )
1045
1051
mqtt ["password" ] = cp .get ("MQTT Broker" ,"password" ,fallback = None )
1046
- mqtt ["homebridge" ] = cp .get ("MQTT Broker" ,"HomebridgeEvents" ,fallback = False )
1047
- mqtt ["mqttimages" ] = cp .get ("MQTT Broker" ,"PostImagesToMQTT" ,fallback = True )
1052
+ mqtt ["homebridge" ] = cp .getboolean ("MQTT Broker" ,"HomebridgeEvents" ,fallback = False )
1053
+ mqtt ["mqttimages" ] = cp .getboolean ("MQTT Broker" ,"PostImagesToMQTT" ,fallback = True )
1048
1054
dahua_event = DahuaEventThread (mqtt ,cameras )
1049
1055
1050
1056
dahua_event .start ()
0 commit comments