Skip to content

Commit

Permalink
Fixed bug controlling the error output, Added socket timeout handling…
Browse files Browse the repository at this point in the history
… to avoid a large number of pending sockets if TCP timeout applies and the interval is shorter than the TCP timeout
  • Loading branch information
mwittig committed May 29, 2015
1 parent 5e32330 commit 40d2194
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 11 additions & 2 deletions solarview.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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: ->
Expand Down

0 comments on commit 40d2194

Please sign in to comment.