Plugin Help

Posted on
Thu Sep 12, 2013 2:44 pm
jd10884 offline
Posts: 39
Joined: Feb 18, 2012

Plugin Help

Good day,

I am looking for a bit of help with an error please and thanks,

The code below is just a portion to show where I am running into a problem. def startup(self) will open the url fine, def update(self) gives an error like so;

File "plugin.py", line 157, in update
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/socket.py", line 304, in read
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/httplib.py", line 529, in read
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/socket.py", line 328, in read
<class 'socket.error'>: (54, 'Connection reset by peer')

I tried to comment out the def startup(self) block of code and I still get the error. If the url is being called with the same code in each method, why is it failing in the update(self) method yet works in the startup(self) method?

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import serial, time, os, decimal, csv, urllib2, socket
import xml.etree.cElementTree as ET
from xml.etree.cElementTree import parse

class Plugin(indigo.PluginBase):
owd= '{http://schema/owserver}owd_DS18B20'
h = '{http:///schema/owserver}Health'
socket.setdefaulttimeout(60)
url = "http://169.254.1.1/details.xml"
f= urllib2.urlopen(url)
def startup(self):
for x in parse(self.f).findall(Plugin.owd):

dataA = x.find(Plugin.id)
def update(self):
for x in parse(self.f).findall(Plugin.owd):
dataB = x.find(Plugin.h)


Thank you,
Jeff

Posted on
Thu Sep 12, 2013 5:41 pm
jay (support) offline
Site Admin
User avatar
Posts: 18261
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Plugin Help

I've reformatted the code so that 1) it will compile and 2) it's more readable:

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import serial, time, os, decimal, csv, urllib2, socket
import xml.etree.cElementTree as ET
from xml.etree.cElementTree import parse

class Plugin(indigo.PluginBase):
   owd= '{http://schema/owserver}owd_DS18B20'
   h = '{http:///schema/owserver}Health'
   socket.setdefaulttimeout(60)
   url = "http://169.254.1.1/details.xml"
   f= urllib2.urlopen(url)

   def startup(self):
      for x in parse(self.f).findall(Plugin.owd):
      dataA = x.find(Plugin.id)
      
   def update(self):
      for x in parse(self.f).findall(Plugin.owd):
      dataB = x.find(Plugin.h)


A couple of things:

  1. You shouldn't open the URL in the default initialization section of your class like that. You'll most likely want to open it just before you read from it and then close it (though with URLs it's not strictly required).
  2. The error you're getting implies that the socket connection is eventually resetting - which is likely related to the above.

Maybe this is closer:

Code: Select all
class Plugin(indigo.PluginBase):
   owd= '{http://schema/owserver}owd_DS18B20'
   h = '{http:///schema/owserver}Health'
   socket.setdefaulttimeout(60)
   url = "http://169.254.1.1/details.xml"

   def startup(self):
      f= urllib2.urlopen(url)
      for x in parse(self.f).findall(Plugin.owd):
         dataA = x.find(Plugin.id)
      f.close()

      
   def update(self):
      f= urllib2.urlopen(url)
      for x in parse(self.f).findall(Plugin.owd):
         dataB = x.find(Plugin.h)
      f.close()


Of course, you're going to get potential exceptions from the url open (timeouts, server unavailable, etc) that you'll want to catch, but you can add that later.

Unfortunately, there's no context so it's really hard to diagnose further. If you're building a plugin that will implement known device types like dimmers or relays I'd suggest starting from the Example plugin for those types in the SDK - it has a good skeleton to start from and will help guide you.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests