Skip to content

socket io don't send emit in subclasses call by pydispatcher #1248

@tetrew88

Description

@tetrew88

hello, I have a home automation server using openzwave to manipulate home automation elements. openzwave allows me to capture events when, for example, a light bulb turns on, etc. my server contains a zwave network subclass which takes care of retrieving events via pydispatch connection, so I passed the socketio server as an attribute to my zwave class in order to emit an event to the client when openzwave detects the ignition or other of a light bulb.

my problem is that I can send the events in the run of my server but not in my zwave class and what's more it interrupts the ping/pong of the server and clients

here is some code:

server:
`
mport eventlet
eventlet.monkey_patch(os=True,select=True,socket=True,thread=True,time=True)

import socketio
import sys
import os

import threading
import time

sys.path.append("..")

import json

socketIoServer = socketio.Server(async_mode='eventlet', cors_allowed_origins="*", ping_timeout=60, logger=True, engineio_logger=True)
app = socketio.WSGIApp(socketIoServer, object)

from homeAutomationServer.homeAutomationEngine.classes.homeAutomationEngine import *

class HomeAutomationServer(socketio.Namespace):
"""
class representing the home automation server:
property:

        methods:

        server event:
"""

homeAutomationEngine = False
running = False


def __init__(self, scriptPath):
    self.scriptPath = scriptPath
    self.configFilePath = scriptPath + "/configs/homeAutomationServerConfig.json"
    
    self.load_home_automation_engine(self)

    socketio.Namespace.__init__(self, '/HomeAutomationServer')



@staticmethod
def load_home_automation_engine(self):
    HomeAutomationServer.homeAutomationEngine = HomeAutomationEngine(self.scriptPath + '/homeAutomationEngine', socketIoServer)

###BASE METHODS###
def start(self):
succes = False

    if self.serverConfigured == True:
        if self.start_engine():
            succes = True
        else:
            succes = False
    else:
        succes = False

    if succes:
        self.running = True

    return succes

def run(self):
listen_clients = threading.Thread(target=self.listen_clients)
listen_clients.start()

    while True:
        #HomeAutomationServer.homeAutomationEngine.zWaveNetwork.send_light_controller_color_updated_event(False)
        time.sleep(0.1)

###CLIENTS REQUESTS###
"""CONNECTION EVENT"""
@socketIoServer.event(namespace='/HomeAutomationServer')
def connect(sid, environ):
print('client connecté ', sid)
`

zwave network method sending the event:
`

def send_light_controller_color_updated_event(self, event):
succes = False

    if self.server != False:
        try:
            self.server.emit('light_controller_color_updated', {}, namespace='/HomeAutomationServer')
            
            try:
                self.server.sleep(0.1)
            except:
                pass

            succes = True
        except:
            succes = False
    else:
        succes = False

    if succes:
        print('iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii')

    return succes

`

client code:
`
import eventlet
eventlet.monkey_patch(os=True,select=True,socket=True,thread=True,time=True)

import threading
import socketio

import sys
import os

import json

import time

sys.path.append("..")
from homeAutomationSystem.homeAutomationSystem.classes.homeAutomationSystem import *

homeAutomationSystemSocket = socketio.Server(cors_allowed_origins="*")
app = socketio.WSGIApp(homeAutomationSystemSocket, object)

homeAutomationServerSocket = socketio.Client(logger=True, engineio_logger=True)

class HomeAutomationSystemServer(socketio.Namespace):

homeAutomationSystem = False

def __init__(self, scriptPath):
    self.scriptPath = scriptPath
    self.configFilePath = scriptPath + '/configs/homeAutomationSystemConfig.json'
    
    self.running = False

    socketio.Namespace.__init__(self, '/HomeAutomationSystem')

def listen_home_automation_server(self):
homeAutomationServerSocket.wait()

    #listenClient.join()

@homeAutomationServerSocket.event(namespace='/HomeAutomationServer')
def light_controller_color_updated(data):
print("yesssssssssssssssssss")
`

for all the code of the program: https://github.com/tetrew88/homeAutomation
the server is in homeAutomationServer
and client in homeAutomationServer

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions