Skip to content

Commit 55a3777

Browse files
committed
Fixed and added tests asserts
1 parent 80e48ab commit 55a3777

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

CameraEvents.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@
2424
# Python 2.7
2525
from ConfigParser import ConfigParser
2626
import logging
27-
import os
2827
import socket
2928
import pycurl
30-
import json
31-
import time
3229
from paho.mqtt import client as mqtt_client # pip install paho-mqtt
33-
import base64
3430
import DahuaDevice
3531

3632
version = "0.3.0"
@@ -88,7 +84,7 @@ def __init__(self, mqtt_cfg, cameras):
8884
self.homebridge = mqtt_cfg["homebridge"]
8985

9086
self.client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2,"CameraEvents-" + socket.gethostname(), clean_session=True)
91-
if not mqtt_cfg["user"] is None and not mqtt_cfg["user"] == '':
87+
if mqtt_cfg["user"] is not None and not mqtt_cfg["user"] == '':
9288
self.client.username_pw_set(mqtt_cfg["user"], mqtt_cfg["password"])
9389
self.client.on_connect = self.mqtt_on_connect
9490
self.client.on_disconnect = self.mqtt_on_disconnect
@@ -149,7 +145,6 @@ def run(self):
149145
break
150146

151147
Ret = self.CurlMultiObj.select(1.0)
152-
while not self.stopped.is_set():
153148
while not self.stopped.is_set():
154149
# Sleeps to ease load on processor
155150
time.sleep(.05)

DahuaDevice.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515
except:
1616
# Python 2.7
1717
from ConfigParser import ConfigParser
18-
import logging
19-
import os
20-
import socket
21-
import pycurl
2218
import json
23-
import time
2419
#import paho.mqtt.client as paho # pip install paho-mqtt
2520
import base64
2621

Tests/test_devices.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import pytest
3-
import CameraEvents
43
import DahuaDevice
54
import datetime
65
try:
@@ -69,7 +68,6 @@ def read_config():
6968
camera_items = cp.items( "Cameras" )
7069
for key, camera_key in camera_items:
7170
#do something with path
72-
camera_cp = cp.items(camera_key)
7371
camera = {}
7472
#temp = cp.get(camera_key,"host")
7573
camera["host"] = cp.get(camera_key,'host')
@@ -154,4 +152,4 @@ def test_dahua_playback_url():
154152
result = device.CreatePlaybackUrl(1, starttime)
155153

156154
assert result is not None
157-
assert checktime.strftime("%Y-%m-%d%%20%H:%M:%S") in result
155+
assert checktime.strftime("%Y_%m_%d_%H_%M_%S") in result

Tests/test_onreceive.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,21 @@ def test_dahua_receive_alarm_local():
8080
device = Tests.test_devices.create_device()
8181
data = str.encode("--myboundary\r\nContent-Length:37\r\nCode=AlarmLocal;action=Start;index=1")
8282
device.OnReceive(data)
83-
assert device.client.topic == "CameraEvents/AlarmLocal/1" and device.client.payload == 'ON'
83+
assert "CameraEvents/AlarmLocal/1" in device.client.messages and device.client.messages["CameraEvents/AlarmLocal/1"] == 'ON'
8484
data = str.encode("--myboundary\r\nContent-Length:37\r\nCode=AlarmLocal;action=Stop;index=1")
8585
device.OnReceive(data)
86-
assert device.client.topic == "CameraEvents/AlarmLocal/1" and device.client.payload == 'OFF'
86+
assert "CameraEvents/AlarmLocal/1" in device.client.messages and device.client.messages["CameraEvents/AlarmLocal/1"] == 'OFF'
87+
8788

8889
def test_dahua_receive_alarm_local_Index_mismatch():
8990
device = Tests.test_devices.create_device()
9091
data = str.encode("--myboundary\r\nContent-Length:37\r\nCode=AlarmLocal;action=Start;index=5")
9192
device.OnReceive(data)
92-
assert device.client.topic == "CameraEvents/AlarmLocal/5" and device.client.payload == 'ON'
93+
assert "CameraEvents/AlarmLocal/5" in device.client.messages and device.client.messages["CameraEvents/AlarmLocal/5"] == 'ON'
9394
data = str.encode("--myboundary\r\nContent-Length:37\r\nCode=AlarmLocal;action=Stop;index=5")
9495
device.OnReceive(data)
95-
assert device.client.topic == "CameraEvents/AlarmLocal/5" and device.client.payload == 'OFF'
96-
96+
assert "CameraEvents/AlarmLocal/5" in device.client.messages and device.client.messages["CameraEvents/AlarmLocal/5"] == 'OFF'
97+
9798

9899
def test_dahua_receive_crossRegion():
99100
device = Tests.test_devices.create_device()
@@ -146,6 +147,8 @@ def test_dahua_receive_crossRegion():
146147
device.OnReceive(data)
147148

148149
assert device is not None
150+
assert "CameraEvents/Camera:1/event" in device.client.messages and device.client.messages["CameraEvents/Camera:1/event"] == 'ON'
151+
assert "CameraEvents/IVS/Camera:1" in device.client.messages and device.client.messages["CameraEvents/IVS/Camera:1"] == 'CrossRegionDetection With Human in Enter direction for Courtyard region'
149152

150153
def test_dahua_receive_crossRegion_createSnapshot():
151154
device = Tests.test_devices.create_device()
@@ -198,6 +201,8 @@ def test_dahua_receive_crossRegion_createSnapshot():
198201
device.OnReceive(data)
199202

200203
assert device is not None
204+
assert "CameraEvents/Camera:1/event" in device.client.messages and device.client.messages["CameraEvents/Camera:1/event"] == 'ON'
205+
assert "CameraEvents/IVS/Camera:1" in device.client.messages and device.client.messages["CameraEvents/IVS/Camera:1"] == 'CrossRegionDetection With Human in Enter direction for Courtyard region'
201206

202207
def test_dahua_receive_crossRegion_NoDirection():
203208
device = Tests.test_devices.create_device()
@@ -247,6 +252,9 @@ def test_dahua_receive_crossRegion_NoDirection():
247252
'"UTCMS" : 633 ' \
248253
'} ' )
249254
device.OnReceive(data)
255+
assert device is not None
256+
assert "CameraEvents/Camera:1/event" in device.client.messages and device.client.messages["CameraEvents/Camera:1/event"] == 'ON'
257+
assert "CameraEvents/IVS/Camera:1" in device.client.messages and device.client.messages["CameraEvents/IVS/Camera:1"] == 'CrossRegionDetection With Human in unknown direction for Courtyard region'
250258

251259
def test_dahua_receive_crossRegion_NoName():
252260
device = Tests.test_devices.create_device()
@@ -297,10 +305,12 @@ def test_dahua_receive_crossRegion_NoName():
297305
device.OnReceive(data)
298306

299307
assert device is not None
308+
assert "CameraEvents/Camera:1/event" in device.client.messages and device.client.messages["CameraEvents/Camera:1/event"] == 'ON'
309+
assert "CameraEvents/IVS/Camera:1" in device.client.messages and device.client.messages["CameraEvents/IVS/Camera:1"] == 'CrossRegionDetection'
300310

301311
def test_dahua_receive_crossLine():
302-
device = Tests.test_devices.create_device()
303-
data = str.encode('Code=CrossLineDetection;action=Start;index=0;data={'\
312+
device = Tests.test_devices.create_device()
313+
data = str.encode('Code=CrossLineDetection;action=Start;index=0;data={'\
304314
'"Class" : "Normal",'\
305315
'"CountInGroup" : 1,'\
306316
'"DetectLine" : ['\
@@ -336,6 +346,8 @@ def test_dahua_receive_crossLine():
336346
'"UTC" : 1549080650.0,'\
337347
'"UTCMS" : 702'\
338348
'} ')
339-
device.OnReceive(data)
349+
device.OnReceive(data)
340350

341-
assert device is not None
351+
assert device is not None
352+
assert "CameraEvents/Camera/event" in device.client.messages and device.client.messages["CameraEvents/Camera/event"] == 'ON'
353+
assert "CameraEvents/IVS/Camera" in device.client.messages and device.client.messages["CameraEvents/IVS/Camera"] == 'CrossLineDetection With Smoke in RightToLeft direction for Gate region'

0 commit comments

Comments
 (0)