274 lines
7.0 KiB
Python
274 lines
7.0 KiB
Python
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
# -*- mode: Python -*-
|
|
|
|
'''
|
|
tosimu
|
|
exporter to simulocal page in www.
|
|
v0.1b
|
|
|
|
'''
|
|
from __future__ import print_function
|
|
import websocket
|
|
import time
|
|
import argparse
|
|
import traceback
|
|
import sys
|
|
from websocket_server import WebsocketServer
|
|
|
|
try:
|
|
import thread
|
|
except ImportError:
|
|
import _thread as thread
|
|
|
|
name = "exports::tosimu"
|
|
|
|
def debug(*args, **kwargs):
|
|
if( verbose == False ):
|
|
return
|
|
print(*args, file=sys.stderr, **kwargs)
|
|
|
|
|
|
argsparser = argparse.ArgumentParser(description="toWS v0.1b help mode")
|
|
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
|
|
argsparser.add_argument("-s","--server",help="WS server IP (127.0.0.1 by default)", type=str)
|
|
argsparser.add_argument("-p","--port",help="WS port to bind to (9001 by default)", type=str)
|
|
argsparser.add_argument("-k","--key",help="Redis key to update",default="0",type=str)
|
|
args = argsparser.parse_args()
|
|
|
|
key = args.key
|
|
|
|
if args.verbose:
|
|
verbose = True
|
|
else:
|
|
verbose = False
|
|
|
|
if args.server:
|
|
serverIP = args.server
|
|
else:
|
|
serverIP = "127.0.0.1"
|
|
|
|
if args.port:
|
|
wsPORT = args.port
|
|
else:
|
|
wsPORT = 9001
|
|
|
|
debug("")
|
|
debug("tosimu v0.1b")
|
|
|
|
points0 = "[(150.0, 230.0, 65280), (170.0, 170.0, 65280), (230.0, 170.0, 65280), (210.0, 230.0, 65280), (150.0, 230.0, 65280)]"
|
|
points1 = "[(180.0, 230.0, 65280), (200.0, 170.0, 65280), (180.0, 230.0, 65280)]"
|
|
points2 = "[(170.0, 190.0, 65280), (200.0, 170.0, 65280), (230.0, 190.0, 65280), (230.0, 200.0, 65280), (170.0, 230.0, 65280), (230.0, 230.0, 65280)]"
|
|
points3 = "[(170.0, 170.0, 65280), (200.0, 170.0, 65280), (230.0, 190.0, 65280), (200.0, 200.0, 65280), (230.0, 210.0, 65280), (200.0, 230.0, 65280), (170.0, 230.0, 65280)]"
|
|
points = [points0, points1, points2, points3]
|
|
|
|
LaserNumber = 1
|
|
SceneNumber = 0
|
|
Laser = 0
|
|
|
|
|
|
# Change current laser.
|
|
def NoteOn(note):
|
|
debug("NoteOn", note)
|
|
|
|
# Change laser client
|
|
if note < 8:
|
|
|
|
Laser = note
|
|
debug("Current Laser switched to", Laser)
|
|
sendWSall("/status Laser " + str(Laser))
|
|
sendWSall("/simul" +" "+ points[Laser])
|
|
|
|
# Change PL displayed on webui
|
|
if note > 23 and note < 32:
|
|
if note - 24 > LaserNumber -1:
|
|
debug("Only",LaserNumber,"lasers asked, you dum ass !")
|
|
sendWSall("/redstatus No Laser"+str(note-24))
|
|
sendWSall("/laser "+str(LaserNumber-1))
|
|
sendWSall("/simul" +" "+ points[LaserNumber-1])
|
|
else:
|
|
Laser = note -24
|
|
debug("Current Laser switched to", Laser)
|
|
sendWSall("/status Laser " + str(Laser))
|
|
sendWSall("/simul" +" "+ points[Laser])
|
|
|
|
#
|
|
# Websocket server
|
|
#
|
|
|
|
|
|
# Called for every WS client connecting (after handshake)
|
|
def new_client(client, wserver):
|
|
|
|
|
|
debug("WS server got new client connected and was given id %d" % client['id'])
|
|
sendWSall("/status Hello " + str(client['id']))
|
|
sendWSall("/laser "+str(0))
|
|
sendWSall("/lack/" + str(0) + " 3")
|
|
sendWSall("/lstt/" + str(0) + " 3")
|
|
sendWSall("/simul" +" "+ points[0])
|
|
|
|
|
|
# Called for every WS client disconnecting
|
|
def client_left(client, wserver):
|
|
|
|
try:
|
|
debug("WS server had client(%d) disconnected" % client['id'])
|
|
|
|
except:
|
|
debug("Something weird is coming from",client,"on the wire...")
|
|
pass
|
|
|
|
|
|
|
|
# Called for each WS received message.
|
|
def message_received(client, wserver, message):
|
|
|
|
#if len(message) > 200:
|
|
# message = message[:200]+'..'
|
|
|
|
oscpath = message.split(" ")
|
|
#print "WS Client", client['id'], "said :", message, "splitted in an oscpath :", oscpath
|
|
#if( verbose == True ):
|
|
# debug("WS server's client", client['id'], "said :", message)
|
|
|
|
if message[0:5] == "/simu":
|
|
debug("WS server sending to simu page :",message)
|
|
sendWSall(message)
|
|
|
|
if message[0:7] == "/noteon":
|
|
oscpath = message.split(" ")
|
|
debug("WS server got new laser", int(oscpath[1]))
|
|
NoteOn(int(oscpath[1]))
|
|
|
|
|
|
def sendWSall(message):
|
|
if ( verbose == True ):
|
|
debug("WS server is sending to all : %s" % (message))
|
|
wserver.send_message_to_all(message)
|
|
|
|
|
|
def LaunchServer(*args):
|
|
global wserver
|
|
|
|
try:
|
|
|
|
# Websocket server
|
|
wserver = WebsocketServer(wsPORT,host=serverIP)
|
|
|
|
debug("Launching Websocket server...")
|
|
debug("at", serverIP, "port",wsPORT)
|
|
wserver.set_fn_new_client(new_client)
|
|
wserver.set_fn_client_left(client_left)
|
|
wserver.set_fn_message_received(message_received)
|
|
debug("LJ local server running...")
|
|
debug("")
|
|
|
|
# websocket server loop
|
|
wserver.run_forever()
|
|
|
|
|
|
except EOFError:
|
|
debug("break")# no more information
|
|
|
|
finally:
|
|
|
|
debug("sendWS terminating...")
|
|
|
|
|
|
|
|
#
|
|
# CLI websocket client -> WS server (nanoLJ) -> webpage
|
|
#
|
|
|
|
def on_message(ws, message):
|
|
pass
|
|
#debug("CLI WS client received and dropped "+message)
|
|
|
|
def on_error(ws, error):
|
|
debug("CLI WS client got error :"+error)
|
|
|
|
def on_close(ws):
|
|
debug("### CLI WS client WS closed ###")
|
|
|
|
def on_open(ws):
|
|
|
|
def run(*args):
|
|
|
|
try:
|
|
while True:
|
|
line = sys.stdin.readline()
|
|
if line == "":
|
|
time.sleep(0.01)
|
|
|
|
#debug("CLI string", line)
|
|
line = line.rstrip('\n')
|
|
line=line[1:-1]
|
|
line = line.replace("[",'(')
|
|
line = line.replace("]",')')
|
|
#debug(line)
|
|
line = "[{}]".format(line)
|
|
debug("CLI proccess sending : /simul" +" "+ line)
|
|
#sendWSall("/simul" +" "+ str(points[laserid].decode('ascii')))
|
|
ws.send("/simul "+line)
|
|
#debug("exports::tosimuCLIent "+str(key)+" "+line)
|
|
|
|
except EOFError:
|
|
debug("tosimu break")# no more information
|
|
|
|
finally:
|
|
ws.close()
|
|
debug("tosimu WS terminating...")
|
|
|
|
|
|
thread.start_new_thread(run, ())
|
|
|
|
def handle_timeout(self):
|
|
self.timed_out = True
|
|
|
|
|
|
|
|
|
|
#
|
|
# Launch WS server and CLI client
|
|
#
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
|
|
|
# Websocket server
|
|
thread.start_new_thread(LaunchServer, ())
|
|
|
|
# CLI Websocket client
|
|
debug("Launching tosimu CLI websocket client...")
|
|
#websocket.enableTrace(True)
|
|
websocket.enableTrace(False)
|
|
ws = websocket.WebSocketApp("ws://"+str(serverIP)+":"+str(wsPORT),
|
|
on_message = on_message,
|
|
on_error = on_error,
|
|
on_close = on_close)
|
|
ws.on_open = on_open
|
|
ws.run_forever()
|
|
|
|
except Exception:
|
|
debug("tosimu Exception")
|
|
traceback.print_exc()
|
|
|
|
'''
|
|
if __name__ == "__main__":
|
|
websocket.enableTrace(True)
|
|
ws = websocket.WebSocketApp("ws://"+str(serverIP)+":"+str(wsPORT),
|
|
on_message = on_message,
|
|
on_error = on_error,
|
|
on_close = on_close)
|
|
ws.on_open = on_open
|
|
ws.run_forever()
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|