Skip to content

Commit

Permalink
fix memory leak in jeedom_com & small code quality improvment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mips2648 committed Dec 19, 2024
1 parent 08c971c commit c746c58
Show file tree
Hide file tree
Showing 2 changed files with 324 additions and 353 deletions.
110 changes: 53 additions & 57 deletions resources/demond/demond.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,62 @@
# along with Jeedom. If not, see <http://www.gnu.org/licenses/>.

import logging
import string
import sys
import os
import time
import datetime
import traceback
import re
import signal
from optparse import OptionParser
from os.path import join
import json
import argparse

try:
from jeedom.jeedom import *
except ImportError:
print("Error: importing module jeedom.jeedom")
sys.exit(1)
from jeedom.jeedom import jeedom_socket, jeedom_utils, jeedom_serial, jeedom_com, JEEDOM_SOCKET_MESSAGE

def read_socket():
global JEEDOM_SOCKET_MESSAGE
if not JEEDOM_SOCKET_MESSAGE.empty():
logging.debug("Message received in socket JEEDOM_SOCKET_MESSAGE")
message = json.loads(jeedom_utils.stripped(JEEDOM_SOCKET_MESSAGE.get()))
if message['apikey'] != _apikey:
logging.error("Invalid apikey from socket: %s", message)
return
try:
print ('read')
except Exception as e:
logging.error('Send command to demon error: %s' ,e)
global JEEDOM_SOCKET_MESSAGE
if not JEEDOM_SOCKET_MESSAGE.empty():
logging.debug("Message received in socket JEEDOM_SOCKET_MESSAGE")
message = json.loads(jeedom_utils.stripped(JEEDOM_SOCKET_MESSAGE.get()))
if message['apikey'] != _apikey:
logging.error("Invalid apikey from socket: %s", message)
return
try:
print ('read')
except Exception as e:
logging.error('Send command to demon error: %s' ,e)

def listen():
jeedom_socket.open()
try:
while 1:
time.sleep(0.5)
read_socket()
except KeyboardInterrupt:
shutdown()
my_jeedom_socket.open()
try:
while 1:
time.sleep(0.5)
read_socket()
except KeyboardInterrupt:
shutdown()

# ----------------------------------------------------------------------------

def handler(signum=None, frame=None):
logging.debug("Signal %i caught, exiting...", int(signum))
shutdown()
logging.debug("Signal %i caught, exiting...", int(signum))
shutdown()

def shutdown():
logging.debug("Shutdown")
logging.debug("Removing PID file %s", _pidfile)
try:
os.remove(_pidfile)
except:
pass
try:
jeedom_socket.close()
except:
pass
try:
jeedom_serial.close()
except:
pass
logging.debug("Exit 0")
sys.stdout.flush()
os._exit(0)
logging.debug("Shutdown")
logging.debug("Removing PID file %s", _pidfile)
try:
os.remove(_pidfile)
except:
pass
try:
my_jeedom_socket.close()
except:
pass
# try: # if you need jeedom_serial
# my_jeedom_serial.close()
# except:
# pass
logging.debug("Exit 0")
sys.stdout.flush()
os._exit(0)

# ----------------------------------------------------------------------------

Expand All @@ -97,13 +88,13 @@ def shutdown():
parser.add_argument("--loglevel", help="Log Level for the daemon", type=str)
parser.add_argument("--callback", help="Callback", type=str)
parser.add_argument("--apikey", help="Apikey", type=str)
parser.add_argument("--cycle", help="Cycle to send event", type=str)
parser.add_argument("--cycle", help="Cycle to send event", type=float)
parser.add_argument("--pid", help="Pid file", type=str)
parser.add_argument("--socketport", help="Port for Zigbee server", type=str)
parser.add_argument("--socketport", help="Port for socket server", type=int)
args = parser.parse_args()

if args.device:
_device = args.device
_device = args.device
if args.loglevel:
_log_level = args.loglevel
if args.callback:
Expand All @@ -115,7 +106,7 @@ def shutdown():
if args.cycle:
_cycle = float(args.cycle)
if args.socketport:
_socketport = args.socketport
_socket_port = args.socketport

_socket_port = int(_socket_port)

Expand All @@ -133,10 +124,15 @@ def shutdown():
signal.signal(signal.SIGTERM, handler)

try:
jeedom_utils.write_pid(str(_pidfile))
jeedom_socket = jeedom_socket(port=_socket_port,address=_socket_host)
listen()
jeedom_utils.write_pid(str(_pidfile))
my_jeedom_com = jeedom_com(apikey=_apikey, url=_callback, cycle=_cycle)
if not my_jeedom_com.test():
logging.error('Network communication issues. Please fixe your Jeedom network configuration.')
shutdown()
# my_jeedom_serial = jeedom_serial(device=_device) # if you need jeedom_serial
my_jeedom_socket = jeedom_socket(port=_socket_port,address=_socket_host)
listen()
except Exception as e:
logging.error('Fatal error: %s', e)
logging.info(traceback.format_exc())
shutdown()
logging.error('Fatal error: %s', e)
logging.info(traceback.format_exc())
shutdown()
Loading

0 comments on commit c746c58

Please sign in to comment.