0

Iam doing django project using django channels. In my project I need to integrate and communicate between C++ code. In between i have to send some values to C++. I checked without django channels and it is working fine,Can any one help me out how can I implement below code in consumers.py of django channels.

proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoContinuousFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,preexec_fn=os.setsid,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

1 Answers1

0

I have found the answer using Syncconsumer with synchronous function

class CameraParallelAsync(SyncConsumer):
    def websocket_connect(self,event):
        print("connected",self.channel_name)
        print('Data after delay')
        self.channel_group_name = "task_single_snap"
        self.send({
            "type":"websocket.accept"
        })

    def websocket_receive(self,event):
        print("receive",event)
        channel_data = event.get('text',None)
        user_name = self.scope['url_route']['kwargs']['user']
        team_name = self.scope['url_route']['kwargs']['team']
        media_path = settings.MEDIA_ROOT
        channel_name  = self.channel_name
        if channel_data is not None:
            if channel_data =='CAMERA_ON':
                self.scope['session']['camera'] = True
                print('Camera Enabled')
                global proc
                os.environ['LD_LIBRARY_PATH'] = "/usr/local/lib/Libraries"    
                import subprocess
                proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoContinuousFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,preexec_fn=os.setsid,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
       
                self.send({
                    "type":"websocket.send",
                    "text":"Camera Enabled"
                })
            elif channel_data == 'CAMERA_OFF':
               print('Camera disabled')
               self.scope['session']['camera'] = False
               import signal
               os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
            elif channel_data == 'SINGLE_SNAP':
               self.send({
                   "type":"websocket.send",
                   "text":"We are processing your request"
               })
               print('Single Snap')
               task = tasks.camera_single_task.delay(user_name,team_name,media_path,channel_name)
               print(task.status,'status')

            elif channel_data == 'GROUP_SNAP':
                print('Group Snap')
                os.environ['LD_LIBRARY_PATH'] = "/usr/local/lib/Libraries"
                import subprocess
                proc = subprocess.Popen(["/home/dev/integrated_module/front-face-camera/rest_project/CameraApplication/MakoGroupFrameGrabberConsoleApplication" + ' ' + team_name + ' ' + user_name + ' ' + media_path],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
                print(proc.communicate())
                self.send({
                       "type":"websocket.send",
                       "text":"Group snap Snaptured"
                   })
            else:
               print('No detection')



    def websocket_disconnect(self,event):
        print("disconnected",event)

        # Receive message from room group  
    def task_message(self, event):
        message = event['message']
        print('hello')
        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))