more G-Labs products

Author Topic: Python Program stopped working after conditional change  (Read 650 times)

November 09, 2016, 05:32:27 AM
Read 650 times

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
I am writing a python program to work with my Minimote to control a few lights.  I wrote a simple python program to detect when a button is pressed and it was working until I changed the conditional for the parameter value from != to >.  I am seeing the button presses in the log file as well as in the popup box on the screen.  Here is the code snipit:

while (hg.Program.IsEnabled):
  genVal = hg.Modules.WithName('MiniMote').Get().Parameter('Sensor.Generic').Value;
  print "genVal ="+genVal;
  if genVal > '0':
    print "genVal > 0"
  time.sleep(1)

When I changed "if genVal != '0' " to "if genVal > '0' " the program stopped detecting the change in the Sensor.Generic value.  Here is a snipit from my log file:

2016-11-08 23:15:39.9526 Debug 01-0A-00-04-00-09-04-2B-01-01-00-D7
2016-11-08 23:15:39.9526 Debug ZWaveMessage (RawData=01-0A-00-04-00-09-04-2B-01-01-00-D7)
2016-11-08 23:15:39.9526 Debug ZWaveMessage (Direction=Inbound, Header=SOF, NodeId=9, Type=Request, Function=ApplicationCommandHandler, CommandClass=SceneActivation)
2016-11-08 23:15:39.9537 Debug 06
2016-11-08 23:15:39.9537 Debug NodeUpdated (NodeId=9, Parameter=SensorGeneric, Value=1)
2016-11-08 23:15:39.9551 Info HomeAutomation.ZWave   9   ZWave Node   Sensor.Generic   1
genVal =0genVal =0
genVal =0genVal =0
genVal =0genVal =0
genVal =0genVal =0

I have tried to delete and recreate the program but no luck.  Yes I did try putting back the != but that does't work either.  I also restarted homegenie on my raspberry pi which did not work and then tried rebooting the raspberry pi but still no luck.  By the way I am running HomeGenie Version 1.1 beta r525

Any ideas are welcome.
« Last Edit: November 09, 2016, 12:36:13 PM by jpscuba »

November 10, 2016, 02:05:20 PM
Reply #1

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
So no one has any debugging suggestions or knows why I would be missing events in the program.  Just to add to the problem I even generated a program using the "Wizard" and that will not catch the button events.  Some wizard.

Any help is appreciated.

November 12, 2016, 10:51:20 PM
Reply #2

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
Ok, so I deleted all the programs that were suppose to work with the Minimotes and rebooted the raspberry pi.  I then wrote a new program to work with the two remotes that I have and it seems to be receiving the minimote events.  I am now getting the following error and the program stops:

Collection was modified; enumeration operation may not execute.

I tried to write a program that was thread safe so I am guessing the failing line is occuring when I press the remote button and so the value is changing while I am trying to read it.  The failing point is on one of the two lines that reads the minimote module into the module variable (remote1Module or remote2Module).  It fails on the line associated with the remote that I am currently using.  Can anyone suggest how to read the module variable in a HomeGenie thread safe manor?  Any help is appreciated.

---------------------- program start -------------------------
import time

buttonPressed = False;

while (hg.Program.IsEnabled):
    remote1Module = hg.Modules.WithName('Minimote').Get()
    remote2Module = hg.Modules.WithName('Minimote2').Get()
   
    time.sleep(1)
   
    if (remote1Module.Parameter('Sensor.Generic').Value == '1') or (remote2Module.Parameter('Sensor.Generic').Value == '1'):
      hg.Modules.WithName('BRLampModule').Toggle()
      buttonPressed = True;
     
    if (remote1Module.Parameter('Sensor.Generic').Value == '3') or (remote2Module.Parameter('Sensor.Generic').Value == '3'):
      hg.Modules.WithName('FRLampModule').Toggle()
      buttonPressed = True;

    if (remote1Module.Parameter('Sensor.Generic').Value == '5') or (remote2Module.Parameter('Sensor.Generic').Value == '5'):
      hg.Modules.WithName('KitchenLightModule').Toggle()
      buttonPressed = True;
     
    if buttonPressed == True:
      remote1Module.Parameter('Sensor.Generic').Value = '0'
      remote2Module.Parameter('Sensor.Generic').Value = '0'
      buttonPressed = False;
     
    time.sleep(1)
---------------------------- program end --------------------------


November 14, 2016, 10:51:19 PM
Reply #3

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
So I haven't had any replies from anyone that knows HG well but I have a program that seems to be working without the threading conflict.  I looked in the documentation and the forum but could not find any HG mutex references and I am not sure if the module for the minimote would be using it anyway.  The final version of my program that seems to work is shown below.  I am guessing that the issue was when I pressed a button on the minimote the module software would access the minimote module object and since my program was trying to get the same object pointer there would be a conflict and the "collection was modified" error would be reported.  To solve this I get the pointer to the object once before entering my while loop so then I am not interfering with the module that is trying to update the button status.  Hopefully this might help someone else that is having a similar issue.

---------------------- program start -------------------------
import time

buttonPressed = False;

remote1Module = hg.Modules.WithName('Minimote').Get()
remote2Module = hg.Modules.WithName('Minimote2').Get()

while (hg.Program.IsEnabled):
   
    if (remote1Module.Parameter('Sensor.Generic').Value == '1') or (remote2Module.Parameter('Sensor.Generic').Value == '1'):
      hg.Modules.WithName('BRLampModule').Toggle()
      buttonPressed = True;
     
    if (remote1Module.Parameter('Sensor.Generic').Value == '3') or (remote2Module.Parameter('Sensor.Generic').Value == '3'):
      hg.Modules.WithName('FRLampModule').Toggle()
      buttonPressed = True;

    if (remote1Module.Parameter('Sensor.Generic').Value == '5') or (remote2Module.Parameter('Sensor.Generic').Value == '5'):
      hg.Modules.WithName('KitchenLightModule').Toggle()
      buttonPressed = True;
     
    if buttonPressed == True:
      remote1Module.Parameter('Sensor.Generic').Value = '0'
      remote2Module.Parameter('Sensor.Generic').Value = '0'
      buttonPressed = False;
     
    time.sleep(1)
---------------------------- program end --------------------------


December 02, 2016, 09:04:25 PM
Reply #4

jpscuba

  • **
  • Information
  • Jr. Member
  • Posts: 25
Ok, so this solution ran for a while but I am still getting the access error once in a while when I press a button on the remote.  Does anyone know how to access the minimote resource from a program without having threading access issue where the program is trying to read the status of the button while the minimote module is updating the status from a button press?  I can't be the first one to have this type of problem.

December 03, 2016, 09:32:26 PM
Reply #5

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
Hi,
I use a different way to use my keyfob :
Code: [Select]
When.ModuleParameterChanged( (module, parameter) =>
{
  if ( module.Is("Minimote") ) {
if (parameter.DecimalValue == 1.0) {
                }
if (parameter.DecimalValue == 2.0) {
                }
if (parameter.DecimalValue == 3.0) {
                }
if (parameter.DecimalValue == 4.0) {
                }
if (parameter.DecimalValue == 5.0) {
                }
if (parameter.DecimalValue == 6.0) {
                }
if (parameter.DecimalValue == 7.0) {
                }
if (parameter.DecimalValue == 8.0) {
                }
        }

    }
return true;
});
Program.GoBackground();
Perhaps, that can help you ...