LJ/libs3/plugins.py

278 lines
7.7 KiB
Python
Raw Permalink Normal View History

2020-09-19 12:28:56 +00:00
#!/usr/bin/python3
2019-03-10 22:06:04 +00:00
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
LJ Laser Server v0.8.1
Plugins Handler.
'''
2020-09-19 12:28:56 +00:00
from OSC3 import OSCServer, OSCClient, OSCMessage
2019-03-10 22:06:04 +00:00
from websocket_server import WebsocketServer
2020-09-19 12:28:56 +00:00
from libs3 import gstt
2023-06-03 12:43:53 +00:00
# ?
from libs3 import commands
2019-03-10 22:06:04 +00:00
import os
import subprocess
import sys
2020-10-10 17:29:07 +00:00
from socket import *
2019-03-10 22:06:04 +00:00
def Init(wserver):
global WSserver
WSserver = wserver
def sendWSall(message):
2023-06-03 12:43:53 +00:00
print("WS sending %s" % (message))
2019-03-10 22:06:04 +00:00
#if gstt.debug >0:
#print("WS sending %s" % (message))
WSserver.send_message_to_all(message)
2023-06-03 12:43:53 +00:00
if message[:7] == '/status':
commands.sendOSCUI('/status', message[8:])
print("plugins WSall", message)
if message[:5] == '/line1':
commands.sendOSCUI('/line1', message[6:])
print("plugins WSall", message)
2019-03-10 22:06:04 +00:00
# What is plugin's OSC port ?
def Port(name):
data = gstt.plugins.get(name)
return data.get("OSC")
2020-09-19 12:28:56 +00:00
def Ping(name):
sendWSall("/"+ name + "/start 0")
return OSCsend(name,"/ping",1)
#return True
2019-03-10 22:06:04 +00:00
# How to start the plugin ?
def Command(name):
data = gstt.plugins.get(name)
return data.get("command")
# Get all plugin current state
def Data(name):
return gstt.plugins.get(name)
2020-09-19 12:28:56 +00:00
def Kill(name):
#data = Data(name)
print("Killing", name, "...")
OSCsend(name,"/quit")
'''
if data["process"] != None:
print name, "plugin is owned by LJ."
print "Killing plugin", name
OSCsend(name,"/quit")
#data["process"].terminate()
sendWSall("/status Killing "+ name +".")
else:
print "Killing asked but plugin is not owned by LJ"
sendWSall("/status Not own plugin")
'''
def Restart(name):
Kill(name)
Start(name)
2019-08-06 01:08:54 +00:00
# See LJ.conf data
2019-03-10 22:06:04 +00:00
def Start(name):
# get Plugin configuration.
2023-07-09 14:09:18 +00:00
print("plugins starting ",name)
2019-03-10 22:06:04 +00:00
command = Command(name)
2019-08-06 01:08:54 +00:00
sendWSall("/status Starting "+name+"...")
# Get LJ path
2020-09-19 12:28:56 +00:00
#ljpath = r'%s' % os.getcwd().replace('\\','/')
print("")
print("LJ is starting plugin :", name)
2019-08-06 01:08:54 +00:00
# Construct the command with absolute path.
PluginPath = command.split(" ")
# Launch as a subprocess
2020-09-19 12:28:56 +00:00
print("launch :", PluginPath[0], gstt.ljpath + "/" + PluginPath[1])
# without argument
if len(PluginPath) < 3:
PluginProcess = subprocess.Popen( [PluginPath[0], gstt.ljpath + "/" + PluginPath[1] ], env=os.environ)
# with 1 argument
else:
PluginProcess = subprocess.Popen( [PluginPath[0], gstt.ljpath + "/" + PluginPath[1] + " " + PluginPath[2]], env=os.environ)
#PluginProcess = os.execv([PluginPath[0], ljpath + "/" + PluginPath[1]])
2019-08-06 01:08:54 +00:00
if gstt.debug >0:
2023-06-03 12:43:53 +00:00
print("LJ path :", gstt.ljpath)
2020-09-19 12:28:56 +00:00
print("New process pid for ", name, ":", PluginProcess.pid)
2019-08-06 01:08:54 +00:00
'''
2019-03-10 22:06:04 +00:00
# Maybe it's not fully started
data = Data(name)
if command != "" and "pid" not in data :
2019-08-06 01:08:54 +00:00
sendWSall("/status Starting "+name+"...")
2019-03-10 22:06:04 +00:00
# Get LJ path
ljpath = r'%s' % os.getcwd().replace('\\','/')
2019-03-17 03:19:57 +00:00
2019-03-10 22:06:04 +00:00
print ""
2019-03-17 03:19:57 +00:00
print "LJ is starting plugin :", name
2019-03-10 22:06:04 +00:00
# Construct the command with absolute path.
PluginPath = command.split(" ")
# Launch as a subprocess
PluginProcess = subprocess.Popen([PluginPath[0], ljpath + "/" + PluginPath[1]])
2019-03-17 03:19:57 +00:00
if gstt.debug >0:
print "LJ path :", ljpath
print "New process pid for ", name, ":", PluginProcess.pid
2019-03-10 22:06:04 +00:00
data = Data(name)
data["pid"] = PluginProcess.pid
data["process"] = PluginProcess
# Process can be terminated with :
# PluginProcess.terminate()
2019-08-06 01:08:54 +00:00
'''
2019-03-10 22:06:04 +00:00
def OSCsend(name, oscaddress, oscargs =''):
#print "OSCsend in plugins got for", name, ": oscaddress", oscaddress, "oscargs :", oscargs
2019-03-10 22:06:04 +00:00
PluginPort = Port(name)
#sendWSall("/status Checking "+ name + "...")
2019-03-10 22:06:04 +00:00
osclientplugin = OSCClient()
2023-06-03 12:43:53 +00:00
osclientplugin.connect((gstt.RediServerIP, PluginPort))
2019-03-10 22:06:04 +00:00
oscmsg = OSCMessage()
oscmsg.setAddress(oscaddress)
oscmsg.append(oscargs)
try:
if gstt.debug > 0:
2023-06-03 12:43:53 +00:00
print("Plugins manager : OSCsending", oscmsg,"to plugin", name, "at", gstt.RediServerIP, ":", PluginPort)
2023-06-03 12:43:53 +00:00
osclientplugin.sendto(oscmsg, (gstt.RediServerIP, PluginPort))
2019-03-10 22:06:04 +00:00
oscmsg.clearData()
2019-08-06 01:08:54 +00:00
if gstt.debug >0:
2020-09-19 12:28:56 +00:00
print(oscaddress, oscargs, "was sent to",name)
2019-03-10 22:06:04 +00:00
return True
except:
2019-03-17 03:19:57 +00:00
if gstt.debug > 0:
2023-06-03 12:43:53 +00:00
print('OSCSend : Connection to plugin IP', gstt.RediServerIP ,':', PluginPort,'refused : died ?')
2019-03-10 22:06:04 +00:00
#sendWSall("/status No plugin.")
#sendWSall("/status " + name + " is offline")
#sendWSall("/" + name + "/start 0")
2019-03-10 22:06:04 +00:00
#PluginStart(name)
return False
2020-10-10 17:29:07 +00:00
def sendbroadcast():
if gstt.debug > 0:
print("Sending broadcast")
cs = socket(AF_INET, SOCK_DGRAM)
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
cs.sendto("LJ 0.8".encode(), ("255.255.255.255", 54545))
2019-03-10 22:06:04 +00:00
2020-09-19 12:28:56 +00:00
# for each plugin will automatically add /pluginame before oscpath to send like /aurora/scim 1, if oscpath = "/scim 1"
def SendAll(oscpath):
2019-03-10 22:06:04 +00:00
2020-10-06 01:11:02 +00:00
print("Sending to all plugins...")
2019-03-10 22:06:04 +00:00
2020-09-19 12:28:56 +00:00
for plugin in list(gstt.plugins.keys()):
if gstt.debug > 0:
2023-07-09 14:09:18 +00:00
print("plugin",plugin)
print('oscpath', oscpath)
2020-10-06 01:11:02 +00:00
print("sending /"+plugin+oscpath,"to", plugin)
print("sending /"+plugin+oscpath[0],"to", plugin)
2020-09-19 12:28:56 +00:00
#sendWSall("/"+ plugin + "/start 0")
2020-10-06 01:11:02 +00:00
Send(plugin, ["/"+plugin+oscpath[0], oscpath[1]])
2019-08-06 01:08:54 +00:00
2019-03-10 22:06:04 +00:00
# Send a command to given plugin. Will also start it if command contain /start 1
def Send(name, oscpath):
2019-03-10 22:06:04 +00:00
2020-10-06 01:11:02 +00:00
print("OSC is sending to", name,":", oscpath)
#if oscpath.find(name) != -1:
2019-03-10 22:06:04 +00:00
if oscpath[0].find(name) != -1:
2020-10-06 01:11:02 +00:00
print("pinging..", name)
2019-03-10 22:06:04 +00:00
# Plugin is online ?
if Ping(name):
# Light up the plugin button
#sendWSall("/" + name + "/start 1")
#sendWSall("/status " + name + " online")
if gstt.debug > 0:
2020-09-19 12:28:56 +00:00
print('')
print("Plugins manager got", oscpath, "for plugin", name, "currently online.")
2019-03-10 22:06:04 +00:00
# If start 0, try to kill plugin
if oscpath[0].find("start") != -1 and oscpath[1] == "0":
if gstt.debug >0:
2020-09-19 12:28:56 +00:00
print("start 0, so killing", name, "...")
2019-03-10 22:06:04 +00:00
Kill(name)
# Send osc command
elif len(oscpath) == 1:
OSCsend(name, oscpath[0], oscargs='noargs')
2019-08-06 01:08:54 +00:00
elif len(oscpath) == 2:
OSCsend(name, oscpath[0], oscargs=oscpath[1])
2019-08-06 01:08:54 +00:00
elif len(oscpath) == 3:
OSCsend(name, oscpath[0], oscargs=(oscpath[1], oscpath[2]))
2020-09-19 12:28:56 +00:00
elif name == "trckr":
#print("To trckr", name, oscpath, len(oscpath))
OSCsend(name, oscpath[0], oscpath[1:])
elif name == "aurora":
2020-10-06 01:11:02 +00:00
#print("To Aurora", oscpath)
#OSCsend(name, oscpath[:-2], oscpath[-1] )
print("To Aurora", oscpath, len(oscpath))
2020-09-19 12:28:56 +00:00
OSCsend(name, oscpath[0], oscpath[1:])
2019-03-10 22:06:04 +00:00
return True
# Plugin not online..
else:
if gstt.debug >0:
2020-09-19 12:28:56 +00:00
print("Plugin manager send says plugin " + name + " is offline.")
2019-03-10 22:06:04 +00:00
2020-10-06 01:11:02 +00:00
#sendWSall("/redstatus Plugin " + name + " offline")
#sendWSall("/"+ name + "/start 0")
2019-03-10 22:06:04 +00:00
# Try to Start it if /start 1
if oscpath[0].find("start") != -1 and oscpath[1] == "1":
if gstt.debug >0:
2020-09-19 12:28:56 +00:00
print("Plugin Manager Trying to start", name, "...")
2019-03-10 22:06:04 +00:00
Start(name)
return False