###############################################################################
##
##  Copyright (C) 2014, Tavendo GmbH and/or collaborators. All rights reserved.
##
##  Redistribution and use in source and binary forms, with or without
##  modification, are permitted provided that the following conditions are met:
##
##  1. Redistributions of source code must retain the above copyright notice,
##     this list of conditions and the following disclaimer.
##
##  2. Redistributions in binary form must reproduce the above copyright notice,
##     this list of conditions and the following disclaimer in the documentation
##     and/or other materials provided with the distribution.
##
##  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
##  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
##  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
##  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
##  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
##  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
##  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
##  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
##  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
##  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
##  POSSIBILITY OF SUCH DAMAGE.
##
###############################################################################

from __future__ import print_function
from os import environ

from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.util import sleep

from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
from twisted.logger import Logger

from threading import Timer

class AppSession(ApplicationSession):

    log = Logger()
    timeout = 5

    def __init__(self, config = None):
        ApplicationSession.__init__(self, config)
        print("component created")

    def onConnect(self):
         print("transport connected")
         self.join(self.config.realm)

    def onChallenge(self, challenge):
         print("authentication challenge received")

    @inlineCallbacks
    def onJoin(self, details):
        #self.lastMsg = 0
        probotid = None
        lastBat = None
        
        def receive_id(probot_id):
        
            if "B-" in probot_id:
                probotid=probot_id.split('-')[1]
                self.log.info("initializing subscribers for id {probotid}", probotid=probotid)
                topic = "probot-bat-{}".format(probotid)
                                
                def battery_timeout():
                    self.log.info("battery not received from probot {probotid}", probotid=probotid)
                    self.publish('bridge-topic', probotid, 0, "BATTERY TIMEOUT") # para publicar na bridge
                    print("BATTERY TIMEOUT")
                    self.publish(topic, "error")
                    
                self.bat_alive_timer = Timer(30, battery_timeout,())
                self.bat_alive_timer.start()
                               
                if probotid != None:                
                    def receive_bat(bat_value):
                        #global lastBat
                        #lastBat = bat_value.split('-')[1]
                        print(bat_value)
                        #print(lastBat)
                        #self.log.info("last battery from {topic}: {lastBat}", topic=topic, lastBat=lastBat)
                        #self.publish('bridge-topic', probotid, lastBat) # para publicar na bridge
                        self.log.info("last battery from {topic}: {bat_value}", topic=topic, bat_value=bat_value)
                        self.publish('bridge-topic', probotid, bat_value, "UPDATE") # para publicar na bridge
                        self.bat_alive_timer.cancel()
                        self.bat_alive_timer = Timer(30, battery_timeout,())
                        self.bat_alive_timer.start()                        
                    self.subscribe(receive_bat, topic)                                                   
                            
            else:
                #if not "B-" in probot_id:
                self.log.info("initializing subscribers for id {probotid} (controls)", probotid=probot_id)
                probotid=probot_id
                topic = "probot-topic-{}".format(probotid)
                keepalive_topic = "keepalive-{}".format(probotid)
                if probotid != None:
                    print(probotid)
                    def receive_msg(msg):
                        self.log.info("event from {topic} received: {msg}", topic=topic, msg=msg)
                self.subscribe(receive_msg, topic)    
        
                def webclient_timeout():
                    self.log.info("keepalive not received from probot {probotid}", probotid=probotid)
                    #global lastBat
                    #if lastBat != None:
                    self.publish('bridge-topic', probotid, 0, "WEB TIMEOUT") # para publicar na bridge
                    print("WEB TIMEOUT")
                    #else:
                        #self.publish('bridge-topic', probotid, "error")
        
                self.keep_alive_timer = Timer(30, webclient_timeout,())
                self.keep_alive_timer.start()
        
                def reset_timer():
                    self.keep_alive_timer.cancel()
                    self.keep_alive_timer = Timer(30, webclient_timeout,())
                    self.keep_alive_timer.start()
            
                self.subscribe(reset_timer, keepalive_topic)
            
        self.subscribe(receive_id, "general-topic")
        
        yield sleep(1)	
        #self.publish('bridge-topic', probot_id) # para publicar na bridge

		
        #self.log.info("subscribed to topic 'probot2beagle'")

       ## PUBLISH and CALL every second .. forever
        #while True:

            ## PUBLISH an event
            #self.publish('blabla', msg)
            #self.log.info("published an probot2Web from python")

            #yield sleep(1)
	    
    def onLeave(self, details):
         print("session left")

    def onDisconnect(self):
         print("transport disconnected")

if __name__ == '__main__':
        runner = ApplicationRunner(
            environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"),
                    u"realm1",
                    extra=dict(
                               max_events=5,  # [A] pass in additional configuration
                    ),
            )
        runner.run(AppSession , auto_reconnect=True)



@@ -1,1576 +1,4 @@
-###############################################################################%0A##%0A##  Copyright (C) 2014, Tavendo GmbH and/or collaborators. All rights reserved.%0A##%0A##  Redistribution and use in source and binary forms, with or without%0A##  modification, are permitted provided that the following conditions are met:%0A##%0A##  1. Redistributions of source code must retain the above copyright notice,%0A##     this list of conditions and the following disclaimer.%0A##%0A##  2. Redistributions in binary form must reproduce the above copyright notice,%0A##     this list of conditions and the following disclaimer in the documentation%0A##     and/or other materials provided with the distribution.%0A##%0A##  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22%0A##  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE%0A##  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE%0A##  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE%0A##  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR%0A##  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF%0A##  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS%0A##  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN%0A##  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)%0A##  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE%0A##  POSSIBILITY OF SUCH DAMAGE.%0A##%0A###############################################################################%0A
 %0Afro
@@ -733,34 +733,8 @@
 s):%0A
-        #self.lastMsg = 0%0A
     
@@ -1319,32 +1319,33 @@
 OUT%22) # 
-para
+to
  publi
-car na
+sh on the
  bridge%0A
@@ -1734,371 +1734,24 @@
     
-#global lastBat%0A                        #lastBat = bat_value.split('-')%5B1%5D%0A                        print(bat_value)%0A                        #print(lastBat)%0A                        #self.log.info(%22last battery from %7Btopic%7D: %7BlastBat%7D%22, topic=topic, lastBat=lastBat)%0A                        #self.publish('bridge-topic', probotid, lastBat) # para publicar na bridge
+print(bat_value)
 %0A   
@@ -1947,32 +1947,33 @@
 ATE%22) # 
-para
+to
  publi
-car na
+sh on the
  bridge%0A
@@ -2337,51 +2337,8 @@
 se:%0A
-                #if not %22B-%22 in probot_id:%0A
     
@@ -3024,85 +3024,8 @@
 id)%0A
-                    #global lastBat%0A                    #if lastBat != None:%0A
     
@@ -3099,32 +3099,33 @@
 OUT%22) # 
-para
+to
  publi
-car na
+sh on the
  bridge%0A
@@ -3169,108 +3169,8 @@
 T%22)%0A
-                    #else:%0A                        #self.publish('bridge-topic', probotid, %22error%22)%0A
     
@@ -3689,391 +3689,8 @@
 1)%09%0A
-        #self.publish('bridge-topic', probot_id) # para publicar na bridge%0A%0A%09%09%0A        #self.log.info(%22subscribed to topic 'probot2beagle'%22)%0A%0A       ## PUBLISH and CALL every second .. forever%0A        #while True:%0A%0A            ## PUBLISH an event%0A            #self.publish('blabla', msg)%0A            #self.log.info(%22published an probot2Web from python%22)%0A%0A            #yield sleep(1)%0A
 %09   

Update socket_server.py