Skip to content

Commit

Permalink
resol: fixed an issue preventing the plugin from being restartable
Browse files Browse the repository at this point in the history
  • Loading branch information
aschwith committed Jun 29, 2024
1 parent 1f1d1b5 commit 86a2576
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
55 changes: 34 additions & 21 deletions resol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

class Resol(SmartPlugin):

PLUGIN_VERSION = '1.0.7' # (must match the version specified in plugin.yaml)
PLUGIN_VERSION = '1.1.0' # (must match the version specified in plugin.yaml)


def __init__(self, sh):
"""
Initalizes the plugin.
Initializes the plugin.
"""

Expand All @@ -44,26 +44,28 @@ def __init__(self, sh):
self._cycle = self.get_parameter_value('cycle')
self._password = self.get_parameter_value('password')
self._to_do = True
self.socket = None
self._last_request_successfull = False
#self._client = Tcp_client(name=name, host=self._ip, port=self._port, binary=True, autoreconnect=True, connect_cycle=5, retry_cycle=30)


def run(self):
self.alive = True
self.scheduler_add('PollData', self.sock, prio=5, cycle=self._cycle, offset=2)
self.scheduler_add('PollData', self.pull_socket, prio=5, cycle=self._cycle, offset=2)
# if you want to create child threads, do not make them daemon = True!
# They will not shutdown properly. (It's a python bug)

def stop(self):
self.scheduler_remove('PollData')

try:
if self.sock:
self.sock.shutdown(0)
self.sock.close()
if self.socket:
self.socket.shutdown(0)
self.socket.close()
except:
pass

self.sock = None
self.socket = None
self.alive = False

def parse_item(self, item):
Expand All @@ -90,18 +92,21 @@ def update_item(self, item, caller=None, source=None, dest=None):
# do nothing if items are changed from outside the plugin
pass

def sock(self):
def pull_socket(self):
if not self.alive:
self._last_request_successfull = False
return
self.logger.info("1) Starting sock function")
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.logger.info("2) Connecting socket")
try:
self.sock.connect((self._ip, self._port))
self.socket.connect((self._ip, self._port))
except socket.timeout as e:
self.logger.warning("Timeout exception during socket connect: %s" % str(e))
self._last_request_successfull = False
return
except Exception as e:
self._last_request_successfull = False
self.logger.error("Exception during socket connect: %s" % str(e))
return
self.logger.info("3) Logging in")
Expand All @@ -110,16 +115,17 @@ def sock(self):
self.load_data()
else:
self.logger.warning("Cannot login")
self._last_request_successfull = False
self.logger.info("5) Shutting down socket")
try:
if self.sock:
self.sock.shutdown(0)
self.sock.close()
if self.socket:
self.socket.shutdown(0)
self.socket.close()
except Exception as e:
self.logger.warning("Exception during shutdown socket command: {0}".format(e))
pass
self.logger.info("6) Ending socket function")
self.sock = None
self.socket = None

# Logs in onto the DeltaSol BS Plus over LAN. Also starts (and maintains) the
# actual stream of data.
Expand Down Expand Up @@ -158,14 +164,16 @@ def load_data(self):

if not dat:
self.logger.warning("Could not receive data via socket")
self._last_request_successfull = False
return

self.logger.info("Response to data: " + str(dat))

#Check if device is ready to send Data
if not dat.startswith("+OK"):
self.logger.warning("Vbus Lan is not ready, reply: " + str(dat))
return
self.logger.warning("Vbus Lan is not ready, reply: " + str(dat))
self._last_request_successfull = False
return
buf = self.readstream()

#self.logger.warning("Readstream {0} bytes as asci: {1}".format(len(buf),str(buf)))
Expand All @@ -182,6 +190,7 @@ def load_data(self):
#self.logger.warning("Readstream hex {0} bytes: {1}".format(len(buf), s.hex()))

if not buf:
self._last_request_successfull = False
return

msgs = self.splitmsg(buf)
Expand All @@ -195,12 +204,12 @@ def load_data(self):

# Receive 1024 bytes from stream
def recv(self):
if not self.sock:
if not self.socket:
self.logger.error("Error during data reception: Socket is not valid")
return None
self.sock.settimeout(5)
self.socket.settimeout(5)
try:
dat = self.sock.recv(1024).decode('Cp1252')
dat = self.socket.recv(1024).decode('Cp1252')
except socket.timeout:
self.logger.info("Exception in recv(): Socket reception timeout")
return None
Expand All @@ -212,9 +221,9 @@ def recv(self):

# Sends given bytes over the stream. Adds debug
def send(self, dat):
if not self.sock:
if not self.socket:
return
self.sock.send(dat.encode('utf-8'))
self.socket.send(dat.encode('utf-8'))

# Read Data until minimum 1 message is received
def readstream(self):
Expand Down Expand Up @@ -323,6 +332,7 @@ def parse_payload_pv1(self, msg):

if not self.check_header_crc(msg):
self.logger.warning("Header crc error")
self._last_request_successfull = False
return

command = self.get_command(msg)
Expand All @@ -339,7 +349,10 @@ def parse_payload_pv1(self, msg):

if payload == '':
self.logger.warning("Payload is empty")
self._last_request_successfull = False
return

self._last_request_successfull = True

for item in self._items:
parentItem = item.return_parent()
Expand Down
6 changes: 3 additions & 3 deletions resol/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ plugin:
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/919242-neues-plugin-resol-vbus-cosmo-multi-solarthermie-logging/page4

# Following entries are for Smart-Plugins:
version: 1.0.7 # Plugin version
sh_minversion: '1.7.2' # minimum shNG version to use this plugin
version: 1.1.0 # Plugin version
sh_minversion: 1.7.2 # minimum shNG version to use this plugin
# sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
multi_instance: True
restartable: unknown
restartable: True
classname: Resol # class containing the plugin

parameters:
Expand Down

0 comments on commit 86a2576

Please sign in to comment.