Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a function to send data elsewhere, API, MQTT, etc. #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions speed-cam.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,20 @@
print("WARN : import Failed. File Not Found %s" % userMotionFilePath)
USER_MOTION_CODE_ON = False

# Check for user_reporting_code.py file to import and error out if not found.
userReportingFilePath = os.path.join(baseDir, "user_reporting_code.py")
USER_REPORTING_CODE_ON = True # Set Flag to run user_reporting_code.py
if os.path.isfile(userReportingFilePath):
try:
import user_reporting_code
except Exception as err_msg:
print("WARN: %s" % err_msg)
# set flag to ignore running user reporting code after succsessful track.
USER_REPORTING_CODE_ON = False
else:
print("WARN : import Failed. File Not Found %s" % userReportingFilePath)
USER_REPORTING_CODE_ON = False

# Import Settings from specified plugin if PLUGIN_ENABLE_ON=True
if PLUGIN_ENABLE_ON: # Check and verify plugin and load variable overlay
pluginDir = os.path.join(baseDir, "plugins")
Expand Down Expand Up @@ -1565,6 +1579,7 @@ def speed_camera():
(image_width / 2)
- (len(image_text) * IM_FONT_SIZE_PX / 3)
)

if text_x < 2:
text_x = 2
cv2.putText(
Expand Down Expand Up @@ -1725,6 +1740,55 @@ def speed_camera():
" SQL - Inserted Data Row into %s", DB_PATH
)

if USER_REPORTING_CODE_ON:
# ===========================================
# Put your user code in userReportingCode() function
# In the File user_reporting_code.py
# ===========================================
try:
reporting_fields = {
"log_time": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"camera": CAMERA.upper(),
"ave_speed": ave_speed,
"speed_units": speed_units,
"filename": filename,
"image_width": image_width,
"image_height": image_height,
"image_multiplier": IM_BIGGER,
"travel_direction": travel_direction,
"plugin_name": plugin_name,
"track_x": track_x,
"track_y": track_y,
"track_w": track_w,
"track_h": track_h,
"m_area": m_area,
"mo_crop_x_left": MO_CROP_X_LEFT,
"mo_crop_x_right": MO_CROP_X_RIGHT,
"mo_crop_y_upper": MO_CROP_Y_UPPER,
"mo_crop_y_lower": MO_CROP_Y_LOWER,
"mo_max_speed_over": MO_MAX_SPEED_OVER,
"mo_min_area_px": MO_MIN_AREA_PX,
"mo_track_event_count": MO_TRACK_EVENT_COUNT,
"cal_obj_px": cal_obj_px,
"cal_obj_mm": cal_obj_mm,
"cam_location": CAM_LOCATION
}

user_reporting_code.userReportingCode(reporting_fields, vs, filename)
except ValueError:
logging.error(
"Problem running userReportingCode function from File %s",
userReportingFilePath,
)
except TypeError as err:
logging.error(
"Problem with file user_reporting_code.py Possibly out of date"
)
logging.error("Err Msg: %s", err)
logging.error(
"Suggest you delete/rename file and perform menubox UPGRADE"
)

# Format and Save Data to CSV Log File
if LOG_DATA_TO_CSV:
log_csv_time = (
Expand Down
47 changes: 47 additions & 0 deletions user_reporting_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# import json
# import requests

"""
This module will be imported into speed-cam.py and will
execute the userReportingCode function after collecting data to report.
The filenamePath will be passed in case you want to process the file as an attachment
or include in a message, Etc. If you need to import other
python modules they can be added to the top of this
module and used in the userReportingCode.
You can also include other functions within this module
as long as they are directly or indirectly called
within the userReportingCode function since that is
the only function that is called in the speed-cam.py
program when reporting is triggered.
"""

#------------------------------------------------------------------------------
def userReportingCode(data, video_stream, filenamePath):
"""
Users can put code here that needs to be run
after speed camera reporting tracking and image taken
Eg Notify or activate something.

Note all functions and variables will be imported.
speed-cam.py will execute this function userReportingCode(filename)
in speed-cam.py per example below

user reporting_code.userReportingCode(data, filename)

"""
# Insert User python code Below
# print("User Code Executing from userReportingCode function")
# print("file path is %s" % filenamePath)

# files = {
# "json": data,
# "file": open(filenamePath, "rb")
# }

# headers = {
# "Authorization": "Bearer shhh it's a secret"
# }

# url = "https://example.com/api/speed"

# requests.post(url, data=data, files=files, headers=headers)