Skip to content

Commit

Permalink
Merge branch 'MQTT-update' into MQTT-update-topic
Browse files Browse the repository at this point in the history
  • Loading branch information
bobjacobsen committed Mar 30, 2019
2 parents de77db5 + 3424e80 commit 9d33e4d
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions jython/SetMqttParser.py
Expand Up @@ -8,13 +8,14 @@
#
# Author: Bob Jacobsen, copyright 2019
# Author: Gert Jim Muller, copyright 2019
# Author: Dave McMorran, copyright 2019#
# Author: Dave McMorran, copyright 2019
#

import jmri
import java
import json

PUBLISHJSON = True # True:publish JSON, False:just the state
PUBLISHJSON = False # True:publish JSON, False:just the state
RUNAFTERPANELS = True # False to speed up (ms), but need script before panels
# True needed if Tunrouts are already loadedin the table

Expand All @@ -26,7 +27,7 @@ def beanFromPayload( self, bean, payload, topic ):
print "from Broker. Bean", bean
print "with topic", topic

# Try JSON first
# Try JSON first
try:
json_payload = json.loads( payload )
except:
Expand All @@ -41,9 +42,9 @@ def beanFromPayload( self, bean, payload, topic ):

# THROWN/CLOSED is the default behavior, here so you can modify it as desired
if ( payload_state == "THROWN" ) or ( payload_state == "1" ) or ( payload_state == 1 ):
bean.newKnownState( THROWN )
bean.newKnownState( CLOSED if bean.getInverted( ) else THROWN )
elif ( payload_state == "CLOSED" ) or ( payload_state == "0" ) or ( payload_state == 0 ):
bean.newKnownState( CLOSED )
bean.newKnownState( THROWN if bean.getInverted( ) else CLOSED )
else:
bean.newKnownState( INCONSISTENT )
return
Expand All @@ -55,42 +56,55 @@ def payloadFromBean( self, bean, newState ) :
print "with state", newState

# JSON, if set to True
if PUBLISHJSON:
data = {}
if ( ( newState & jmri.Turnout.CLOSED ) != 0 ^ bean.getInverted( ) ):
data['state'] = "CLOSED"
else:
data['state'] = "THROWN"
if PUBLISHJSON :
data = {}
if ( not bean.getInverted( ) ) :
if ( newState & jmri.Turnout.CLOSED ) :
data['state'] = "CLOSED"
else:
data['state'] = "THROWN"
else :
if ( newState & jmri.Turnout.CLOSED ) :
data['state'] = "THROWN"
else:
data['state'] = "CLOSED"

json_data = json.dumps( data )
return json_data

# Without JSON - this is the same as the default behavior, here so you can modify it as desired
else:
if ( ( newState & jmri.Turnout.CLOSED ) != 0 ^ bean.getInverted( ) ):
return "CLOSED"
if ( not bean.getInverted( ) ) :
if ( newState & jmri.Turnout.CLOSED ) :
return "CLOSED"
else :
return "THROWN"
else :
return "THROWN"
if ( newState & jmri.Turnout.CLOSED ) :
return "THROWN"
else :
return "CLOSED"

# Find the MqttTurnoutManager
m = jmri.InstanceManager.getNullableDefault( jmri.jmrix.mqtt.MqttTurnoutManager )
print "From InstanceManager as MqttTurnoutManager: ", m
if( m is None ):
if ( m is None ) :
# Might only have one connection,
m = jmri.InstanceManager.getDefault( jmri.TurnoutManager ).getManagerList( )
m = m[1] # 1 is only appropriate if the MQTT connection is first configured in JMRI
m = m[1] # 1 is only appropriate if the MQTT connection is configured first in JMRI
print "From InstanceManager as TurnoutManager: ", m

# Install the Parser
if( m is not None ):
if ( m is not None ) :
p = ParserReplacement( )

# install in Manager for all future-defined turnouts
m.setParser( p )
print( "ParserReplacement installed" )

# install in any turnouts that have already been created
if RUNAFTERPANELS:
for turnout in m.getNamedBeanSet( ):
if RUNAFTERPANELS :
for turnout in m.getNamedBeanSet( ) :
turnout.setParser( p )
else:
print( "ParserReplacement not installed" )

0 comments on commit 9d33e4d

Please sign in to comment.