From 40d21944480b108debf49fa6bc497534d7f046cf Mon Sep 17 00:00:00 2001 From: marcus Date: Fri, 29 May 2015 18:23:57 +0200 Subject: [PATCH] Fixed bug controlling the error output, Added socket timeout handling to avoid a large number of pending sockets if TCP timeout applies and the interval is shorter than the TCP timeout --- README.md | 6 +++++- package.json | 2 +- solarview.coffee | 13 +++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0a6d82f..a66501a 100644 --- a/README.md +++ b/README.md @@ -113,4 +113,8 @@ History * Bug fix: destroy socket on error to release socket descriptor * 20150526, V0.0.8 * Reduced error log output. If "debug" is not set on the plugin, only new error states will be logged - * Minor changes \ No newline at end of file + * Minor changes +* 20150529, V0.0.9 + * Fixed bug controlling the error output + * Added socket timeout handling to avoid a large number of pending sockets if TCP timeout applies and the + interval is shorter than the TCP timeout \ No newline at end of file diff --git a/package.json b/package.json index 9baa443..1ff6463 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "device-config-schema.coffee", "LICENSE" ], - "version": "0.0.8", + "version": "0.0.9", "homepage": "https://github.com/mwittig/pimatic-solarview", "keywords": [ "pimatic", diff --git a/solarview.coffee b/solarview.coffee index 1572906..2403544 100644 --- a/solarview.coffee +++ b/solarview.coffee @@ -47,14 +47,15 @@ module.exports = (env) -> class SolarViewInverterBaseDevice extends env.devices.Device # Initialize device by reading entity definition from middleware constructor: (@config, @plugin) -> + @debug = plugin.config.debug; env.logger.debug("SolarViewInverterBaseDevice Initialization") if @debug @host = plugin.config.host @port = plugin.config.port @id = config.id @name = config.name @interval = 1000 * (config.interval or plugin.config.interval) + @timeout = Math.min @interval, 20000 @inverterId = config.inverterId - @debug = plugin.config.debug; @_lastError = "" super() @@ -69,6 +70,7 @@ module.exports = (env) -> fetchData: (host, port, inverterId) -> socket = net.createConnection port, host socket.setNoDelay true + socket.setTimeout @timeout socket.on 'connect', (() => env.logger.debug("Opened connection to #{host}:#{port}.") if @debug @@ -88,15 +90,22 @@ module.exports = (env) -> socket.end() ) - socket.on 'error', (error) -> + socket.on 'error', (error) => if error.code == 'ETIMEDOUT' newError = "Timeout fetching SolarView data" else newError = "Error fetching SolarView data: " + error.toString() + env.logger.error newError if @_lastError isnt newError or @debug @_lastError = newError socket.destroy() + socket.on 'timeout', () => + newError = "Timeout fetching SolarView data" + env.logger.error newError if (@_lastError isnt newError) or @debug + @_lastError = newError + socket.destroy() + # poll device according to interval requestUpdate: ->