svgui/svgui_server.py
author Laurent Bessard
Thu, 15 Nov 2012 23:00:53 +0100
changeset 883 235a9ec83b95
parent 728 e0424e96e3fd
child 1511 91538d0c242c
permissions -rw-r--r--
Adding support for defining specific global variables for ConfTreeNodes
Fix bug when defining function block instances as global variables
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     1
#!/usr/bin/python
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     3
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     4
import os
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     5
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     6
from nevow import rend, appserver, inevow, tags, loaders, athena
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     7
import simplejson as json
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     8
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
     9
svgfile = '%(svgfile)s'
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    10
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    11
svguiWidgets = {}
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    12
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    13
currentId = 0
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    14
def getNewId():
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    15
    global currentId
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    16
    currentId += 1
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    17
    return currentId
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    18
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    19
class SvguiWidget:
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    20
    
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    21
    def __init__(self, classname, id, **kwargs):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    22
        self.classname = classname
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    23
        self.id = id
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    24
        self.attrs = kwargs.copy()
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    25
        self.inputs = {}
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    26
        self.outputs = {}
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    27
        self.inhibit = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    28
        self.changed = False
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    29
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    30
    def setinput(self, attrname, value):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    31
        self.inputs[attrname] = value
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    32
        
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    33
    def getinput(self, attrname, default=None):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    34
        if not self.inputs.has_key(attrname):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    35
            self.inputs[attrname] = default
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    36
        return self.inputs[attrname]
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    37
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    38
    def setoutput(self, attrname, value):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    39
        if self.outputs.get(attrname) != value:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    40
            self.outputs[attrname] = value
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    41
            self.changed = True
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    42
            self.RefreshInterface()
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    43
        
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    44
    def updateoutputs(self, **kwargs):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    45
        for attrname, value in kwargs.iteritems():
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    46
            if self.outputs.get(attrname) != value:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    47
                self.outputs[attrname] = value
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    48
                self.changed = True
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    49
        self.RefreshInterface()
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    50
        
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    51
    def RefreshInterface(self):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    52
        interface = website.getHMI()
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    53
        if isinstance(interface, SVGUI_HMI) and self.changed and not self.inhibit:
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    54
            self.changed = False
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    55
            d = interface.sendData(self)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    56
            if d is not None:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    57
                self.inhibit = True
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    58
                d.addCallback(self.InterfaceRefreshed)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    59
    
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    60
    def InterfaceRefreshed(self, result):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    61
        self.inhibit = False
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    62
        if self.changed:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    63
            self.RefreshInterface()
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    64
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    65
def get_object_init_state(obj):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    66
    # Convert objects to a dictionary of their representation
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    67
    attrs = obj.attrs.copy()
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    68
    attrs.update(obj.inputs)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    69
    d = { '__class__': obj.classname,
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    70
          'id': obj.id,
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    71
          'kwargs': json.dumps(attrs),
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    72
          }
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    73
    return d
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    74
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    75
def get_object_current_state(obj):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    76
    # Convert objects to a dictionary of their representation
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    77
    d = { '__class__': obj.classname,
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    78
          'id': obj.id,
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    79
          'kwargs': json.dumps(obj.outputs),
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    80
          }
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    81
    return d
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    82
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    83
class SVGUI_HMI(website.PLCHMI):
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    84
    jsClass = u"LiveSVGPage.LiveSVGWidget"
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    85
    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    86
    docFactory = loaders.stan(tags.div(render=tags.directive('liveElement'))[                                    
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    87
                                         tags.xml(loaders.xmlfile(os.path.join(WorkingDir, svgfile))),
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
    88
                                         ])
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    89
    
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    90
    def HMIinitialisation(self):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    91
        gadgets = []
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    92
        for gadget in svguiWidgets.values():
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    93
            gadgets.append(unicode(json.dumps(gadget, default=get_object_init_state, indent=2), 'ascii'))
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    94
        d = self.callRemote('init', gadgets)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    95
        d.addCallback(self.HMIinitialised)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    96
    
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    97
    def sendData(self,data):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    98
        if self.initialised:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
    99
            return self.callRemote('receiveData',unicode(json.dumps(data, default=get_object_current_state, indent=2), 'ascii'))
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   100
        return None
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   101
        
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   102
    def setattr(self, id, attrname, value):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   103
        svguiWidgets[id].setinput(attrname, value)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   104
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   105
def createSVGUIControl(*args, **kwargs):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   106
    id = getNewId()
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   107
    gad = SvguiWidget(args[0], id, **kwargs)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   108
    svguiWidgets[id] = gad
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   109
    gadget = [unicode(json.dumps(gad, default=get_object_init_state, indent=2), 'ascii')]
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   110
    interface = website.getHMI()
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   111
    if isinstance(interface, SVGUI_HMI) and interface.initialised:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   112
        interface.callRemote('init', gadget)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   113
    return id
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   114
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   115
def setAttr(id, attrname, value):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   116
    gad = svguiWidgets.get(id, None)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   117
    if gad is not None:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   118
        gad.setoutput(attrname, value)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   119
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   120
def updateAttr(id, **kwargs):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   121
    gad = svguiWidgets.get(id, None)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   122
    if gad is not None:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   123
        gad.updateoutput(**kwargs)
371
b7cb57a2da08 Adding new svgui support using twisted website HMI
laurent
parents:
diff changeset
   124
381
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   125
def getAttr(id, attrname, default=None):
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   126
    gad = svguiWidgets.get(id, None)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   127
    if gad is not None:
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   128
        return gad.getinput(attrname, default)
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   129
    return default
5c0f34a9ab00 Improving support for svgui, separating setting end getting attributes functions from creating function.
laurent
parents: 371
diff changeset
   130