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