plc.d/accessors now created the tagtype for all defined accessors
[plcapi.git] / PLC / Accessor.py
index 39e7872..fb12f3d 100644 (file)
@@ -21,6 +21,8 @@ This is implemented as a singleton, so we can cache results over time"""
 
     _instance = None
 
+    tag_locators={}
+
     def __init__ (self, api):
         self.api=api
         # 'tagname'=>'tag_id'
@@ -28,7 +30,7 @@ This is implemented as a singleton, so we can cache results over time"""
 
     def has_cache (self,tagname): return self.cache.has_key(tagname)
     def get_cache (self,tagname): return self.cache[tagname]
-    def set_cache (self,tagname,tag_id): self.cache[tagname]=tag_id
+    def set_cache (self,tagname,tag_type): self.cache[tagname]=tag_type
 
     def locate_or_create_tag (self, tagname, category, description, roles):
         "search tag type from tagname & create if needed"
@@ -54,10 +56,26 @@ This is implemented as a singleton, so we can cache results over time"""
                 except:
                     # xxx todo find a more appropriate way of notifying this
                     print "Accessor.locate_or_create_tag: Could not add role %r to tag_type %s"%(role,tagname)
-        tag_type_id = tag_type['tag_type_id']
-        self.set_cache(tagname,tag_type_id)
-        return tag_type_id
+        self.set_cache(tagname,tag_type)
+        return tag_type
+
+    # a locator is a function that retrieves - or creates - a tag_type instance
+    @staticmethod
+    def register_tag_locator (name, tag_locator):
+        Accessor.tag_locators[name]=tag_locator
 
+    @staticmethod
+    def retrieve_tag_locator (name):
+        return Accessor.tag_locators[name]
+    
+    # this is designed to be part of the 'service plc start' sequence
+    # it ensures the creation of all the tagtypes defined 
+    # in the various accessors
+    # it's not easy to have define_accessors do this because at
+    # load-time as we do not have an instance of API yet
+    def run_all_tag_locators (self):
+        for (name, tag_locator) in Accessor.tag_locators.items():
+            tag_locator(self)
 
 ####################
 # make it a singleton so we can cache stuff in there over time