Announcement

Collapse
No announcement yet.

Looping Poll in a plugin - right or wrong way?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looping Poll in a plugin - right or wrong way?

    Hi all,

    A quick question on something I'm doing in a plugin and just want to make sure it's the normal "correct" way to do it and I'm not doing something dodgy.

    I have a plugin that needs to poll a device over IP using hs.GetURL and then update the status of several devices. I've done this by adding a line inside the main.vb section of the plugin which calls all my updating subroutines. Is this standard or should I be achieving this another way (if so please detail)? It does seem to work fine though...

    Thankyou.

    The sample plugin has this in main.vb:
    Code:
            Try
                ' create the user object that is the real plugin, accessed from the pluginAPI wrapper
                callback = callback
                hs = host
                plugin.OurInstanceFriendlyName = Instance
                ' connect to HS so it can register a callback to us
                host.Connect(IFACE_NAME, Instance)
                Console.WriteLine("Connected, waiting to be initialized...")
                Do
                    Threading.Thread.Sleep(10)
                Loop While client.CommunicationState = HSCF.Communication.Scs.Communication.CommunicationStates.Connected And Not HSPI.bShutDown
                Console.WriteLine("Connection lost, exiting")
                ' disconnect from server for good here
                client.Disconnect()
                clientCallback.Disconnect()
                wait(2)
                End
            Catch ex As Exception
                Console.WriteLine("Cannot connect(2): " & ex.Message)
                wait(2)
                End
                Return
            End Try
    I've modified it to do this:
    Code:
    Try
                ' create the user object that is the real plugin, accessed from the pluginAPI wrapper
                callback = callback
                hs = host
                ' connect to HS so it can register a callback to us
                host.Connect(IFACE_NAME, "")
                Console.WriteLine("Connected, waiting to be initialized...")
    
                '5 Second delay to allow device initialisation
                Threading.Thread.Sleep(5000)
                Do
                    'Poll and update devices
                    UpdateDevices()
                    Threading.Thread.Sleep(PollInterval * 1000)
                Loop While client.CommunicationState = HSCF.Communication.Scs.Communication.CommunicationStates.Connected And Not bShutDown
                If Not bShutDown Then
                    oPlugin.ShutdownIO()
                    Console.WriteLine("Connection lost, exiting")
                Else
                    Console.WriteLine("Shutting down plugin")
                End If
                ' disconnect from server for good here
                client.Disconnect()
                clientCallback.Disconnect()
                wait(2)
                End
            Catch ex As Exception
                Console.WriteLine("Cannot connect(2): " & ex.Message)
                wait(2)
                End
                Return
            End Try

  • #2
    Is this a plugin with triggers/conditions/web pages or a remote application you are connecting to HS? If the former I am not sure I would do it this way (although if you see nothing wrong then guess nothing to say you can't), I would be starting a timer and working off a timer elapsed routine...
    My Plugins:

    Pushover 3P | DoorBird 3P | Current Cost 3P | Velleman K8055 3P | LAMetric 3P | Garadget 3P | Hive 3P |
    Yeelight 3P | Nanoleaf 3P

    Comment


    • #3
      No triggers or conditions - it simply builds devices on first run, then within the loop above it reads in some strings from URLs, parses data from them, and writes values and statuses to the devices. Separately it can post back to URLs via device buttons and the SetIOMulti Sub. Basically all I need is a regular loop (3-5 sec) to allow my plugin to poll inputs and update devices and want to make sure I'm doing it the best way possible.

      Although what I've done seems to work fine, I still feel I'm not doing this the right way and I might be about to break something else next time I add functionality or try to write a new plugin based off this one. What is this other method you describe? Do you have an example? Should I simply be creating my own loop at the end of InitIO or something?
      Cheers
      Matt.

      Comment


      • #4
        I don't know if this is the right way or not, but in my plugins where I need it to loop over and do some polling I have used a timer as well. At the start during plugin start once it has connected I enable and start the timer, then within the timer I call various routines and let it go from there.

        If that is the way you go, just make sure you put in some error handling should the connection to the HS server be lost as that may cause errors and issues.
        HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

        Facebook | Twitter | Flickr | Google+ | Website | YouTube

        Comment

        Working...
        X