def Read(self,IPH_inOptions_outOptions): ''' Return a dummy value for all read requests ''' newIPH = ItemPairHolder() for inItem, outItem in IPH: key = mkItemKey(inItem) if self.OPCItemDict.has_key(key): newIPH.append(inItem, outItem = copy.deepcopy(self.OPCItemDict[key])) else: newIPH.append(inItem, ItemContainer(ResultID=\ self.OPC_E_UNKNOWNITEMNAME, ErrorText=\ 'No such OPC Item')) return super(BasicXDAServer,self).Read((newIPH,inOptions,outOptions))
class BasicXDAServer(XDAServer): ''' Class that implements a very basic XMLDA OPC Server The server accepts every itemname/path and returns a default value ''' OPCServerAddress = '/' SupportedLocaleIDs = ('en-us', ) # Status specifics StatusInfo = 'This is a very basic XMLDA OPC server for testing' # All items in "OPCItems" are accessible in the server # The format is: ((ItemContainer(),(Properties)),(ItemContainer(),...)) # The following options can/should be set on the ItemContainer: # # ValueTypeQualifer # Timestamp # ResultID, DiagnosticInfo, ErrorText # QualityField, LimitField, VendorField # ReadDelay, WriteDelay # # Properties of any kind can be added to the item OPCItems = () def __init__(self, *kl, **kd): ''' Initialize Test Server ''' # Predefined Item Values self.OPCItemDict = self.mkItems(self.OPCItems) super(BasicXDAServer, self).__init__(self, *kl, **kd) def mkItems(self, kl): ''' Make a dictionary of Items ''' d = {} for item, properties in kl: item.addProperties(properties) item.IsEmpty = False d[mkItemKey(item)] = copy.deepcopy(item) return d ######################### OPC Operations ####################### def Read(self, (IPH, inOptions, outOptions)): ''' Return a dummy value for all read requests ''' newIPH = ItemPairHolder() for inItem, outItem in IPH: key = mkItemKey(inItem) if self.OPCItemDict.has_key(key): outItem = copy.deepcopy(self.OPCItemDict[key]) newIPH.append(inItem, outItem) else: newIPH.append(inItem, ItemContainer(ResultID=\ self.OPC_E_UNKNOWNITEMNAME, ErrorText=\ 'No such OPC Item')) return super(BasicXDAServer, self).Read( (newIPH, inOptions, outOptions))
def Read(self, IPH_inOptions_outOptions): ''' Return a dummy value for all read requests ''' newIPH = ItemPairHolder() for inItem, outItem in IPH: key = mkItemKey(inItem) if self.OPCItemDict.has_key(key): newIPH.append(inItem, outItem=copy.deepcopy(self.OPCItemDict[key])) else: newIPH.append(inItem, ItemContainer(ResultID=\ self.OPC_E_UNKNOWNITEMNAME, ErrorText=\ 'No such OPC Item')) return super(BasicXDAServer, self).Read( (newIPH, inOptions, outOptions))
def BuildOPCArgs(self, inIClist, inOptions): IPH = ItemPairHolder() for inItem in inIClist: IPH.append(inItem=inItem, outItem=inItem) outOptions = {} return (IPH, inOptions, outOptions)
def _errWrite(self, failure, inItem, outItem): ''' Fill errorneous property ''' outItem.ResultID = self.ESD_E_WRITE outItem.ErrorText = str(failure.value.args) self.write_results.append((inItem.Sequence, inItem, outItem)) newIPH = ItemPairHolder()
d.addCallback(self._cbReadESD, inItem, outItem) d.addErrback(self._errRead, inItem, outItem) return d def _cbReadESD(self, (NodeAddress, encoding, data), inItem, outItem): ''' Handle result of ESD Read ''' outItem.Value = data outItem.QualityField = 'good' outItem.Timestamp = datetime.datetime.now() self.read_results.append((inItem.Sequence, inItem, outItem)) def _cbRead(self, result, (IPH, inOptions, outOptions)): ''' Assemble IPH and return it ''' # Sort results so that sequence is correct again self.read_results.sort() rIPH = ItemPairHolder() for i, inItem, outItem in self.read_results: rIPH.append(inItem, outItem) return super(ESDProxy, self).Read((rIPH, inOptions, outOptions)) def _errRead(self, failure, inItem, outItem): ''' Fill errorneous property ''' outItem.ResultID = self.ESD_E_READ outItem.ErrorText = str(failure.value.args) self.read_results.append((inItem.Sequence, inItem, outItem)) newIPH = ItemPairHolder() def Write(self, (IPH, inOptions, outOptions)): ''' Write to the item dictionary ''' # Here all results will be stored self.write_results = []