svgui/svgui_server.py
changeset 1736 7e61baa047f0
parent 1730 64d8f52bc8c8
child 1740 b789b695b5c6
equal deleted inserted replaced
1735:c02818d7e29f 1736:7e61baa047f0
    30 svgfile = '%(svgfile)s'
    30 svgfile = '%(svgfile)s'
    31 
    31 
    32 svguiWidgets = {}
    32 svguiWidgets = {}
    33 
    33 
    34 currentId = 0
    34 currentId = 0
       
    35 
       
    36 
    35 def getNewId():
    37 def getNewId():
    36     global currentId
    38     global currentId
    37     currentId += 1
    39     currentId += 1
    38     return currentId
    40     return currentId
       
    41 
    39 
    42 
    40 class SvguiWidget:
    43 class SvguiWidget:
    41 
    44 
    42     def __init__(self, classname, id, **kwargs):
    45     def __init__(self, classname, id, **kwargs):
    43         self.classname = classname
    46         self.classname = classname
    81     def InterfaceRefreshed(self, result):
    84     def InterfaceRefreshed(self, result):
    82         self.inhibit = False
    85         self.inhibit = False
    83         if self.changed:
    86         if self.changed:
    84             self.RefreshInterface()
    87             self.RefreshInterface()
    85 
    88 
       
    89 
    86 def get_object_init_state(obj):
    90 def get_object_init_state(obj):
    87     # Convert objects to a dictionary of their representation
    91     # Convert objects to a dictionary of their representation
    88     attrs = obj.attrs.copy()
    92     attrs = obj.attrs.copy()
    89     attrs.update(obj.inputs)
    93     attrs.update(obj.inputs)
    90     d = { '__class__': obj.classname,
    94     d = { '__class__': obj.classname,
    91           'id': obj.id,
    95           'id': obj.id,
    92           'kwargs': json.dumps(attrs),
    96           'kwargs': json.dumps(attrs),
    93           }
    97           }
    94     return d
    98     return d
    95 
    99 
       
   100 
    96 def get_object_current_state(obj):
   101 def get_object_current_state(obj):
    97     # Convert objects to a dictionary of their representation
   102     # Convert objects to a dictionary of their representation
    98     d = { '__class__': obj.classname,
   103     d = { '__class__': obj.classname,
    99           'id': obj.id,
   104           'id': obj.id,
   100           'kwargs': json.dumps(obj.outputs),
   105           'kwargs': json.dumps(obj.outputs),
   101           }
   106           }
   102     return d
   107     return d
       
   108 
   103 
   109 
   104 class SVGUI_HMI(website.PLCHMI):
   110 class SVGUI_HMI(website.PLCHMI):
   105     jsClass = u"LiveSVGPage.LiveSVGWidget"
   111     jsClass = u"LiveSVGPage.LiveSVGWidget"
   106 
   112 
   107     docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
   113     docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[
   121         return None
   127         return None
   122 
   128 
   123     def setattr(self, id, attrname, value):
   129     def setattr(self, id, attrname, value):
   124         svguiWidgets[id].setinput(attrname, value)
   130         svguiWidgets[id].setinput(attrname, value)
   125 
   131 
       
   132 
   126 def createSVGUIControl(*args, **kwargs):
   133 def createSVGUIControl(*args, **kwargs):
   127     id = getNewId()
   134     id = getNewId()
   128     gad = SvguiWidget(args[0], id, **kwargs)
   135     gad = SvguiWidget(args[0], id, **kwargs)
   129     svguiWidgets[id] = gad
   136     svguiWidgets[id] = gad
   130     gadget = [unicode(json.dumps(gad, default=get_object_init_state, indent=2), 'ascii')]
   137     gadget = [unicode(json.dumps(gad, default=get_object_init_state, indent=2), 'ascii')]
   131     interface = website.getHMI()
   138     interface = website.getHMI()
   132     if isinstance(interface, SVGUI_HMI) and interface.initialised:
   139     if isinstance(interface, SVGUI_HMI) and interface.initialised:
   133         interface.callRemote('init', gadget)
   140         interface.callRemote('init', gadget)
   134     return id
   141     return id
   135 
   142 
       
   143 
   136 def setAttr(id, attrname, value):
   144 def setAttr(id, attrname, value):
   137     gad = svguiWidgets.get(id, None)
   145     gad = svguiWidgets.get(id, None)
   138     if gad is not None:
   146     if gad is not None:
   139         gad.setoutput(attrname, value)
   147         gad.setoutput(attrname, value)
       
   148 
   140 
   149 
   141 def updateAttr(id, **kwargs):
   150 def updateAttr(id, **kwargs):
   142     gad = svguiWidgets.get(id, None)
   151     gad = svguiWidgets.get(id, None)
   143     if gad is not None:
   152     if gad is not None:
   144         gad.updateoutput(**kwargs)
   153         gad.updateoutput(**kwargs)
   145 
   154 
       
   155 
   146 def getAttr(id, attrname, default=None):
   156 def getAttr(id, attrname, default=None):
   147     gad = svguiWidgets.get(id, None)
   157     gad = svguiWidgets.get(id, None)
   148     if gad is not None:
   158     if gad is not None:
   149         return gad.getinput(attrname, default)
   159         return gad.getinput(attrname, default)
   150     return default
   160     return default