예제 #1
0
    def initOPCUA(self):
        # OPC-UA server
        self.server = opcua.Server()
        endpoint = "opc.tcp://{}:{}/".format(HOST, PORT)
        self.server.set_endpoint(endpoint)
        self.server.set_server_name("ROS ua Server")
        self.server.start()
        print('OPC-UA server listening on {}'.format(endpoint))

        # setup our own namespaces, this is expected
        self.uri_topics = "http://ros.org/topics"
        # two different namespaces to make getting the correct node easier for get_node (otherwise had object for service and topic with same name
        self.uri_services = "http://ros.org/services"
        self.uri_actions = "http://ros.org/actions"

        self.idx_topics = self.server.register_namespace(self.uri_topics)
        self.idx_services = self.server.register_namespace(self.uri_services)
        self.idx_actions = self.server.register_namespace(self.uri_actions)
        # get Objects node, this is where we should put our custom stuff
        objectsNode = self.server.get_objects_node()
        # one object per type we are watching
        self.topics_object = objectsNode.add_object(self.idx_topics,
                                                    "ROS-Topics")
        self.services_object = objectsNode.add_object(self.idx_services,
                                                      "ROS-Services")
        self.actions_object = objectsNode.add_object(self.idx_actions,
                                                     "ROS-Actions")
예제 #2
0
    def init_server(self):
        custom_iserver = CustomInternalServer(self)
        server = opcua.Server(iserver=custom_iserver)
        server.set_endpoint("opc.tcp://0.0.0.0:4840/freeopcua/server/")
        os.chdir("..")
        server.import_xml(
            os.path.abspath(os.curdir) + '/nodeset/onem2m-opcua.xml'
        )  # server.iserver.dump_address_space(os.path.dirname(__file__) + 'dump')

        self.server = server
예제 #3
0
 def run(self):
     self.srv = opcua.Server()
     self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num1)
     self.srv.start()
     self.started.set()
     while not self._exit.is_set():
         time.sleep(0.1)
         if not self._queue.empty():
             ev = self._queue.get()
             self.srv.trigger_event(ev)
     self.srv.stop()
예제 #4
0
 def run(self):
     self.srv = opcua.Server()
     self.srv.load_cpp_addressspace(True)
     self.srv.set_endpoint("opc.tcp://localhost:48410")
     self.srv.start()
     self.started.set()
     while not self._exit.is_set():
         time.sleep(0.1)
     print("Stopping server")
     self.srv.stop()
     print("Server stopped")
예제 #5
0
 def setUpClass(self):
     self.srv = opcua.Server()
     self.srv.set_endpoint('opc.tcp://localhost:%d' % port_num2)
     self.srv.start()
     self.opc = self.srv
예제 #6
0
from IPython import embed
import opcua


class SubHandler(opcua.SubscriptionHandler):
    def data_change(self, handle, node, val, attr):
        print("New data change event", handle, node, val, attr)

    def event(self, handle, event):
        print("Python: New event", handle, event)


if __name__ == "__main__":
    # create our server, the argument is for debugging
    server = opcua.Server(True)
    server.set_endpoint("opc.tcp://localhost:4841/freeopcua/server/")
    server.set_server_name("FreeOpcUa Example Server")

    # start the server
    server.start()

    try:
        # setup our own namespace, this is expected
        uri = "http://examples.freeopcua.github.io"
        idx = server.register_namespace(uri)

        # get Objects node, this is where we should put our custom stuff
        objects = server.get_objects_node()
        print("I got objects folder: ", objects)
def main():

    # create instance/object of type opcua.Server
    myserver = opcua.Server()
    print("Server instance created")

    # create instance of kukacontroller with unique identifier
    global kuka  # use kuka as a global variable
    kuka = KUKABridge("5915524839")  # do not change!
    print("KUKA Bridge instance created")

    # Load XML model into server
    # TODO: find the method to 'import' nodes defined in 'XML'. to find it check the documentation https://python-opcua.readthedocs.io/en/latest/server.html
    # TODO: In the line below replace the underscores with the method you found earlier. This will call the method from the myserver object.
    # TODO: As the fuction argument provide the path/name of the XML file you were given
    myserver.import_xml("kuka_kr10.xml")
    print("Adress space model imported into server")

    # Assemble server endpoint/URL
    ep_protocol = "opc.tcp"  # OPC UA protocol
    ep_ip = "localhost"  # do not change, localhost
    ep_port = "9090"  # TODO: chose a free unblocked port (for the assignment chose a random number between 9000 and 10000)
    ep_path = "freeopcua/server/group03"  # TODO: add your group number. Path on the server where the address space is accesible. One server can host multiple address spaces
    endpoint = ep_protocol+"://"+ep_ip+":" + \
        ep_port+"/"+ep_path  # assemble the string

    # Set endpoint for address space on the server
    myserver.set_endpoint(endpoint)

    # Get reference to the Objects node of the server.
    # TODO: start the 'opcua-modeler' software as outlined in the assignment documents.
    # TODO: open the provided XML to inspect it, so you can understand how the address space is navigated and references to nodes obtained
    objects = myserver.get_objects_node()

    # Get references to the robot and position nodes in the server
    node_kuka = objects.get_child("0:KUKA_KR10")
    # 'Axis_Act' is the robot's position. It is an array with 7 elements. 6 robot axes and 1 linear track.
    axis_act = node_kuka.get_child("0:Axis_Act")
    # There is no data from the KUKA controller available right now
    axis_act.set_value('unavailable')

    # Get reference to the controller node
    # TODO: Get a reference to the 'controller' node. Have a look at the previous four lines of code, and the address space in the xml file.
    node_controller = node_kuka.get_child("0:Controller")

    # Get reference to the status node
    machinestatus = node_controller.get_child(
        "0:Status")  # TODO: Get a reference to the 'status' node
    machinestatus.set_value(
        'unavailable')  # TODO: Set the machinestatus to 'unavailable'

    print("OPC UA variable nodes linked against KUKA controller")

    # In the following callback function the value of the nodes will be updated with the data from the KUKA controller
    # Callback method that gets called when the KUKA controller has new axisdata
    def NewAxisData(axis_data, status):

        # Write position data from KUKA controller into OPC UA nodes
        axis_act.set_value(axis_data)
        machinestatus.set_value(status)

    # Register above callback method with the KUKA controller
    kuka.register_callback(NewAxisData)

    print("Callback with KUKA controller registered")

    # Get references to the OPC UA Controller methods
    controller_loadprogram = node_controller.get_child(
        "0:Load")  # TODO get reference to 'Load' method node
    controller_start = node_controller.get_child(
        "0:Start")  # TODO get reference to 'Start' method node
    controller_stop = node_controller.get_child(
        "0:Stop")  # TODO get reference to 'Stop' method node

    # Link OPC UA methods with Python methods
    myserver.link_method(controller_loadprogram, LoadProgram)
    myserver.link_method(controller_start,
                         Start)  # TODO add code to link start method
    myserver.link_method(controller_stop,
                         Stop)  # TODO add code link stop method

    print("OPC UA methods nodes linked against KUKA controller")

    # starting! The server will continue running
    myserver.start()
    print("Server up and running")
예제 #8
0
 def setUpClass(self):
     self.srv = opcua.Server()
     self.srv.load_cpp_addressspace(True)
     self.srv.set_endpoint("opc.tcp://localhost:48430")
     self.srv.start()
     self.opc = self.srv
예제 #9
0
import sys
sys.path.append(".")

from IPython import embed
import opcua

class SubClient(opcua.SubscriptionClient):
    def data_change(self, handle, node, val, attr):
        print("New data change event", handle, node, val, attr)


if __name__ == "__main__":
    server = opcua.Server(False)
    server.set_endpoint("opc.tcp://localhost:4841")
    #s.add_xml_address_space("standard_address_space.xml")
    #s.add_xml_address_space("user_address_space.xml")
    server.start()
    try:
        root = server.get_root_node()
        print("I got root folder: ", root)
        objects = server.get_objects_node()
        print("I got objects folder: ", objects)

        #Now adding some object to our addresse space from server side
        test = objects.add_folder("testfolder")
        myvar = test.add_variable("myvar", [16, 56])
        myprop = test.add_property("myprop", 9.9)
       
        # uncomment next lines to subscribe to changes on server side
        #sclt = SubClient()
예제 #10
0
    def __init__(self,
                 opcua_url,
                 server_name,
                 api_key,
                 place,
                 frequency_of_fetching=30,
                 address='localhost',
                 port=80):
        """
        :param opcua_url: url of the server endpoint to expose
        :type opcua_url: string
        :param server_name: name of the server
        :type server_name: string
        :param api_key: api key of the open_weather API,
        needs to be obtained via registration of the user on open weather maps platform
        :type api_key: string
        :param place: localization in format "city,country code" e. g. "London,gb"
        :type place: string
        :param frequency_of_fetching: frequency of refreshing of weather measurements in seconds
        :type frequency_of_fetching: int
        """
        self.weather_fetcher = open_weather.Client(api_key, place)
        self.address = address
        self.port = port

        self.server = opcua.Server()
        self.server.set_endpoint(opcua_url)
        self.server.set_server_name(server_name)
        idx = self.server.register_namespace("namespace")

        days, time_of_last_fetching = self.fetch_prediction()
        time_node = self.server.nodes.objects.add_object(idx, "time")
        self.time_holder = time_node.add_variable(idx, "last_fetching",
                                                  time_of_last_fetching)

        # todo: make server implementation of method work
        # time_node.add_method(idx, "update_predictions_uamethod", update_predictions_uamethod)
        self.object_variables = {}
        for d in days.keys():
            idx = idx + 1
            obj = self.server.nodes.objects.add_object(idx, d)
            self.object_variables[d] = {}
            for k in days[d].keys():
                variable = obj.add_variable(idx, k, days[d][k])
                self.object_variables[d][k] = variable

        self.frequency_of_fetching = frequency_of_fetching

        # todo - it is work around not working opcua method
        update_predictions = self.update_prediction

        class HTTPRequestHandler(BaseHTTPRequestHandler):
            def do_POST(self):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()

                update_predictions()

                self.wfile.write(b'Success!!!')

        self.handler = HTTPRequestHandler
예제 #11
0
def AddToNodeCount(nAdds):
    tnods = groot_totalnodes.get_data_value()
    currNodeCount = tnods.Value.Value
    totNodeCount = nAdds + currNodeCount
    groot_totalnodes.set_data_value(totNodeCount, ua.VariantType.UInt32)


if __name__ == "__main__":

    # setup our server
    import socket
    hostname = socket.gethostname()
    IPAddr = socket.gethostbyname(hostname)
    #IPAddr = "172.16.10.66"
    IPAddr = "LocalHost"
    server = opcua.Server()
    server.set_server_name("KUAServer")
    server.set_application_uri("urn:" + socket.gethostname() + ":KUAServer")
    endPnt = "opc.tcp://" + IPAddr + ":4846"
    print("EndPoint:", endPnt)
    server.set_endpoint(endPnt)

    # setup our own namespace
    uri = "http://opcfoundation.org/UA/KUAServer/"
    idx = server.register_namespace(uri)

    # get objects folder
    objects = server.get_objects_node()

    # starting!
    server.start()