Commit d3d0e0b2 authored by Bruno Laurencich's avatar Bruno Laurencich

cleanup

parent 1ee7f9ec
......@@ -7,129 +7,29 @@ bl_info = {
# if it's there, reload everything
if "bpy" in locals():
import imp
imp.reload(mdns)
imp.reload(connection)
imp.reload(panel)
else:
from . import mdns
from . import connection
from . import panel
# pass
# from . import mysphere
import bpy
import threading
from bpy import props
from bpy.types import WindowManager
C = bpy.context
# =====================================================
# = OPERATOR: GET THE NOTOCHORD IP =
# =====================================================
class Chord_Get_Ip(bpy.types.Operator):
"""Tries to get the ip of the notochord"""
bl_idname = "chordata.get_ip"
bl_label = "Chordata: get notochord ip"
bl_options = {'REGISTER'}
# ----------- PROPS -----------
# ----------- MODAL -----------
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.cancel(context)
return {'CANCELLED'}
if event.type == 'TIMER':
context.area.tag_redraw()
if self.query_thread.is_alive():
print("ip query ongoing..")
self.chord_global.state = possible_states()[1][0]
else:
with self.query_lock:
if self.found_ip:
self.chord_global.state = possible_states()[2][0]
else:
self.chord_global.state = possible_states()[0][0]
print("IP found :", self.found_ip)
self.cancel(context)
return {'FINISHED'}
return {'PASS_THROUGH'}
# ----------- DO QUERY (thread handler) -----------
def do_query(self):
print("query thread running")
ip = mdns.get_ip()
while not ip and mdns.timeout < 5:
mdns.set_timeout(mdns.timeout + 2)
print("*** new timeout ", mdns.timeout)
if mdns.timeout >= 5:
mdns.set_nameserver()
ip = mdns.get_ip()
with self.query_lock:
self.found_ip = ip
print("*** Thread terminating..")
return
# ----------- EXECUTE -----------
def execute(self, context):
self.chord_global = context.window_manager.chordata
self.chord_global.state = possible_states()[0][0]
wm = context.window_manager
self._timer = wm.event_timer_add(0.5, context.window)
wm.modal_handler_add(self)
self.query_lock = threading.Lock()
self.found_ip = False
self.query_thread = threading.Thread(target=self.do_query,
name="mdns_query")
self.query_thread.start()
return {'RUNNING_MODAL'}
# ----------- CANCEL -----------
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
print("waiting thread..")
self.query_thread.join()
print("thread joined..")
# ====== End of OPERATOR: GET THE NOTOCHORD IP =======
# =========================================
# = GLOBAL PROPERTIES =
# =========================================
def possible_states():
"""Returns the 'items' to create the states enum property.
The format is a list of tuples like:
[(identifier, name, description, icon, number), ...]
complete doc in:
https://docs.blender.org/api/blender_python_api_2_77_0/bpy.props.html#bpy.props.EnumProperty """
return [
("Disconnected", "no-conn", "", "COLOR_RED", 1),
("Searching..", "search", "", "INLINK", 2),
("Connected", "conn", "", "COLOR_GREEN", 3)
]
# Create a collection to hold all the chordata properies
class Chord_Properties(bpy.types.PropertyGroup):
state = props.EnumProperty(items=possible_states(), options={'SKIP_SAVE'})
"""A collection to hold all the global chordata properties.
can be retrived at bpy.context.window_manager.chordata"""
state = props.EnumProperty(\
items=connection.possible_states(),\
options={'SKIP_SAVE'})
# ====== End of GLOBAL PROPERTIES =======
......
import bpy
import threading
if "mdns" in locals():
import imp
imp.reload(mdns)
else:
from . import mdns
def possible_states(raw = True):
"""Returns the 'items' to create the states enum property.
The format is a list of tuples like:
[(identifier, name, description, icon, number), ...]
complete doc in:
https://docs.blender.org/api/blender_python_api_2_77_0/bpy.props.html#bpy.props.EnumProperty """
return [
("Disconnected", "no-conn", "", "COLOR_RED", 0),
("Connected", "conn", "", "COLOR_GREEN", 1),
("Searching..", "search1", "", "INLINK", 2),
("Searching.", "search2", "", "LINK", 3),
]
# =====================================================
# = OPERATOR: GET THE NOTOCHORD IP =
# =====================================================
class Chord_Get_Ip(bpy.types.Operator):
"""Tries to get the ip of the notochord"""
bl_idname = "chordata.get_ip"
bl_label = "Chordata: get notochord ip"
bl_options = {'REGISTER'}
# ----------- PROPS -----------
counter = 0
# ----------- MODAL -----------
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.report({"WARNING"}, "Connection canceled")
self.cancel(context)
return {'CANCELLED'}
if event.type == 'TIMER':
context.area.tag_redraw()
context.object.rotation_euler.y +=0.3
if self.query_thread.is_alive():
print("ip query ongoing..")
self.chord_global.state = \
possible_states()[self.counter%2+2][0]
else:
with self.query_lock:
if self.found_ip:
self.chord_global.state = possible_states()[1][0]
self.report({"INFO"}, "IP found :" + self.found_ip)
else:
self.chord_global.state = possible_states()[0][0]
self.report({"WARNING"}, "IP NOT found")
print("IP found :", self.found_ip)
self.cancel(context)
return {'FINISHED'}
self.counter += 1
return {'PASS_THROUGH'}
# ----------- DO QUERY (thread handler) -----------
def do_query(self):
print("query thread running")
ip = mdns.get_ip()
while not ip and mdns.timeout < 5:
mdns.set_timeout(mdns.timeout + 2)
print("*** new timeout ", mdns.timeout)
with self.query_lock:
self.report({"INFO"},\
"ip not found, retrying in %d secs" %mdns.timeout)
if mdns.timeout >= 5:
mdns.set_nameserver()
ip = mdns.get_ip()
with self.query_lock:
self.found_ip = ip
print("*** Thread terminating..")
return
# ----------- EXECUTE -----------
def execute(self, context):
self.chord_global = context.window_manager.chordata
self.chord_global.state = possible_states()[0][0]
self.report({"INFO"},"ip query ongoing..")
wm = context.window_manager
self._timer = wm.event_timer_add( 0.8 , context.window)
wm.modal_handler_add(self)
self.query_lock = threading.Lock()
self.found_ip = False
self.query_thread = threading.Thread(target=self.do_query,
name="mdns_query")
self.query_thread.start()
return {'RUNNING_MODAL'}
# ----------- CANCEL -----------
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
print("waiting thread..")
self.query_thread.join()
print("thread joined..")
# ====== End of OPERATOR: GET THE NOTOCHORD IP =======
......@@ -19,7 +19,8 @@ class Chord_tools_panel(bpy.types.Panel):
def draw(self, context):
layout = self.layout
if get_conn_state_attr(context, "value") > 1:
layout.enabled= False
layout.operator("chordata.get_ip", "Connect")
# layout.prop(context.window_manager.chordata, "state", expand=True)
layout.label(context.window_manager.chordata.state,\
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment