Skip to content

Commit

Permalink
Merge pull request #69 from Mips2648/master
Browse files Browse the repository at this point in the history
fix memory leak in jeedom_com & small code quality improvment
  • Loading branch information
Mips2648 authored Dec 26, 2024
2 parents 08c971c + a3e9318 commit 6a35720
Show file tree
Hide file tree
Showing 2 changed files with 335 additions and 369 deletions.
119 changes: 57 additions & 62 deletions resources/demond/demond.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,73 +14,64 @@
# 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_com, JEEDOM_SOCKET_MESSAGE # jeedom_serial


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)
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 Exception as e:
logging.warning('Error removing PID file: %s', e)
try:
my_jeedom_socket.close()
except Exception as e:
logging.warning('Error closing socket: %s', e)
# try: # if you need jeedom_serial
# my_jeedom_serial.close()
# except Exception as e:
# logging.warning('Error closing serial: %s', e)
logging.debug("Exit 0")
sys.stdout.flush()
os._exit(0)


_log_level = "error"
_socket_port = 55009
Expand All @@ -91,19 +82,18 @@ def shutdown():
_callback = ''
_cycle = 0.3

parser = argparse.ArgumentParser(
description='Desmond Daemon for Jeedom plugin')
parser = argparse.ArgumentParser(description='Desmond Daemon for Jeedom plugin')
parser.add_argument("--device", help="Device", type=str)
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 +105,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 +123,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 6a35720

Please sign in to comment.