Esempio n. 1
0
from msb_client.DataType import DataType
from msb_client.Event import Event
from msb_client.Function import Function
from msb_client.MsbClient import MsbClient


def printMsg(msg):
    print(str(msg["dataObject"]))


if __name__ == "__main__":

    myMsbClient = MsbClient()

    myMsbClient.enableDebug(True)
    myMsbClient.disableHostnameVerification(True)
    myMsbClient.disableEventCache(False)

    event1 = Event("event1", "Event Name", "Event Description", DataType.FLOAT)
    myMsbClient.addEvent(event1)

    function1 = Function("function1", "Function Name", "Function Description",
                         DataType.FLOAT, printMsg, False, None)
    myMsbClient.addFunction(function1)

    print(myMsbClient.objectToJson(myMsbClient.getSelfDescription()))

    myMsbClient.connect()

    myMsbClient.register()
            "https://cdd.iec.ch/cdd/iec61360/iec61360.nsf/Units/0112-2---62720%23UAA033",
            "/AMBIENT_TEMPERATURE",
        ))
    myMsbClient.addEvent(e_temperature)

    def sendParticleData():
        print("Method stub for data sending")

    def startReadFineParticle():
        print("Method stub for particle reading")

    f_start_fp_detection = Function(
        "START_FP_DETECTION",
        "Start fine particle measurement",
        "Starts the Process of fine particle measurements",
        DataType.BOOLEAN,
        startReadFineParticle,
        False,
        ["PARTICLE_CONCENTRATION"],
    )
    f_start_fp_detection.addMetaData(
        CustomMetaData(
            "Funktion_Temperatur",
            "Funktion_Umgebungstemperatur",
            TypeDescription(
                TypeDescriptor.CDD,
                "0112/2///61987#ABT514#001",
                "https://cdd.iec.ch/cdd/iec61987/iec61987.nsf/ListsOfUnitsAllVersions/0112-2---61987%23ABT514",
            ),
            "/START_FP_DETECTION",
        ))
                            "Response Information Message", "Information",
                            DataType.STRING, 1)

    myMsbClient.addEvent(event1)

    myMsbClient.addEvent(response_event1)

    # define the function which will be passed to the function description
    def printMsg(msg):
        print(str(msg["dataObject"]))

    function1 = Function(
        "PRINT_MSG",
        "Print Message",
        "Print Message",
        DataType.STRING,
        printMsg,
        False,
        ["RESPONSE_INFO_MESSAGE"],
    )

    myMsbClient.addFunction(function1)

    def set_interval(func, sec):
        def func_wrapper():
            set_interval(func, sec)
            func()

        t = threading.Timer(sec, func_wrapper)
        t.start()
        return t
        print("##############################################################")
        binstring = str(
            bin(
                int.from_bytes(
                    str(
                        myMsbClient.objectToJson(
                            myMsbClient.getSelfDescription())).encode(),
                    'big')))
        myMsbClient.publish("SELFDESCRIPTION_DATA", binstring, 1, False, None,
                            SO_UUID)

    function1 = Function(
        "PRINT_MSG",
        "Print Message",
        "Print Message",
        DataType.STRING,
        printMsg,
        False,
        ["RESPONSE_INFO_MESSAGE"],
    )

    myMsbClient.addFunction(function1)

    f_getSelfDescription = Function(
        "SEND_SELFDESCRIPTION",
        "Send Selfdescription",
        "Send Selfdescription",
        DataType.STRING,
        sendSelfDescription,
        False,
        ["SELFDESCRIPTION_DATA"],
def setup_msbclient(verification_token):

    logging.debug("Setup MSB Client")

    # Init msb client
    global myMsbClient
    myMsbClient = MsbClient(
        "SmartObject", SO_UUID, SO_NAME, SO_DESCRIPTION, verification_token
    )
    myMsbClient.enableDebug(True)
    myMsbClient.enableTrace(False)
    myMsbClient.enableDataFormatValidation(True)
    myMsbClient.disableEventCache(False)
    myMsbClient.setEventCacheSize(1000)
    myMsbClient.disableAutoReconnect(False)
    myMsbClient.setReconnectInterval(10000)
    myMsbClient.disableHostnameVerification(True)
    myMsbClient.enableThreadAsDaemon(True)

    # add a configuration parameter to the self description
    config_param_name_1 = "testParam1"
    config_param_value_1 = True
    config_param_datatype_1 = bool

    config_param_name_2 = "testParam2"
    config_param_value_2 = "StringValue"
    config_param_datatype_2 = str

    config_param_name_3 = "testParam3"
    config_param_value_3 = 1000
    config_param_datatype_3 = "int32"

    config_param_name_4 = "testParam3_2"
    config_param_value_4 = 2000
    config_param_datatype_4 = int

    config_param_name_5 = "testParam5"
    config_param_value_5 = 3.3
    config_param_datatype_5 = float

    config_param_name_6 = "testParam6"
    config_param_value_6 = 3.3
    config_param_datatype_6 = "float"

    config_param_name_7 = "testParam7"
    config_param_value_7 = datetime.datetime.now()
    config_param_datatype_7 = "date-time"

    myMsbClient.addConfigParameter(config_param_name_1, config_param_value_1, config_param_datatype_1)
    myMsbClient.addConfigParameter(config_param_name_2, config_param_value_2, config_param_datatype_2)
    myMsbClient.addConfigParameter(config_param_name_3, config_param_value_3, config_param_datatype_3)
    myMsbClient.addConfigParameter(config_param_name_4, config_param_value_4, config_param_datatype_4)
    myMsbClient.addConfigParameter(config_param_name_5, config_param_value_5, config_param_datatype_5)
    myMsbClient.addConfigParameter(config_param_name_6, config_param_value_6, config_param_datatype_6)
    myMsbClient.addConfigParameter(config_param_name_7, config_param_value_7, config_param_datatype_7)

    logging.debug("Self Description - added config params")
    # add events to the client: as single param
    myMsbClient.addEvent(
        "SIMPLE_EVENT1_STRING",
        "Simple event 1",
        "Simple event with string",
        DataType.STRING,
        "LOW",
        False,
    )
    myMsbClient.addEvent(
        "SIMPLE_EVENT2_INTEGER_ARRAY",
        "Simple event 2",
        "Simple event with integer array",
        DataType.INT32,
        0,
        True
    )
    myMsbClient.addEvent(
        "SIMPLE_EVENT3_JSONDATAFORMAT",
        "Simple event 3",
        "Simple event with JSON stringified dataformat",
        '{ "type": "number",  "format": "float" }',
        2,
        False
    )
    myMsbClient.addEvent(
        "SIMPLE_EVENT4_NOPAYLOAD",
        "Simple event 4",
        "Simple event with no payload",
        None,
        0,
        False
    )
    myMsbClient.addEvent(
        "DATE_EVENT",
        "Date Event",
        "Simple event with datetime",
        DataType.DATETIME,
        0,
        False
    )
    myMsbClient.addEvent(
        "arrayev",
        "Array Event",
        "Array Event for testing",
        DataType.STRING,
        "LOW",
        True
    )

    # add events to the client: as event object
    event1 = Event(
        "EVENT1",
        "Event 1",
        "Event with string",
        DataType.STRING,
        "LOW",
        False
    )
    myMsbClient.addEvent(event1)
    event1 = Event(
        "EVENT2",
        "Event 2",
        "Event with number",
        DataType.FLOAT,
        "LOW",
        False
    )
    myMsbClient.addEvent(event1)

    logging.debug("Self Description - added simple events")

    # define properties for complex data format to be used in an event
    complexObject_name_1 = "ComplexObject1"
    complexObject_property_name_1 = "megaprop"
    complexObject_isArray_1 = False
    complexObject_name_2 = "ComplexObject2"
    complexObject_property_name_2 = "superprop"
    complexObject_isArray_2 = True
    complexObject_name_3 = "ComplexObject3"
    complexObject_property_name_3 = "mediumprop"
    complexObject_isArray_3 = True
    complexObject_name_4 = "ComplexObject4"
    complexObject_property_name_4 = "prop"
    complexObject_datatype_4 = "int32"
    complexObject_isArray_4 = True
    # initialize the complex data format
    complexObject_1 = ComplexDataFormat(complexObject_name_1)
    complexObject_2 = ComplexDataFormat(complexObject_name_2)
    complexObject_3 = ComplexDataFormat(complexObject_name_3)
    complexObject_4 = ComplexDataFormat(complexObject_name_4)
    # add properties to the nested complex data formats
    complexObject_4.addProperty(
        complexObject_property_name_4,
        complexObject_datatype_4,
        complexObject_isArray_4
    )
    complexObject_3.addProperty(
        complexObject_property_name_3,
        complexObject_4,
        complexObject_isArray_3
    )
    complexObject_2.addProperty(
        complexObject_property_name_2,
        complexObject_3,
        complexObject_isArray_2
    )
    complexObject_1.addProperty(
        complexObject_property_name_1,
        complexObject_2,
        complexObject_isArray_1
    )
    # add the event with the complex data format
    myMsbClient.addEvent(
        "EVENT3_COMPLEX",
        "Event 3 Complex Data Format",
        "Event wit a 4-level complex data format",
        complexObject_1,
        0,
        True,
    )

    # the final data format can be provided as a valid JSON object
    myMsbClient.addEvent(
        "COMPLEX_JSON_EVENT",
        "JSON based event",
        "JSON based event description",
        {
            "Member" : {
                "type" : "object",
                "properties" : {
                    "name" : {
                        "type" : "string"
                    },
                    "status" : {
                        "enum" : [ "present", "absent" ],
                        "type" : "string"
                    }
                }
            },
            "Team" : {
                "type" : "object",
                "properties" : {
                    "staff" : {
                        "type" : "array",
                        "items" : {
                            "$ref" : "#/definitions/Member"
                        }
                    }
                }
            },
            "dataObject" : {
                "$ref" : "#/definitions/Team"
            }
        },
        0,
        False,
    )

    logging.debug("Self Description - added complex events")

    # define the function which will be passed to the function description
    def printMsg(msg):
        print(str(msg["dataObject"]))

    # add functions to the client: as single param
    myMsbClient.addFunction(
        "FUNCTION1",
        "Function 1",
        "Description function 1",
        "int32",
        printMsg,
        False,
        ["EVENT1", "EVENT2"],
    )

    # add functions to the client: as function object
    function2 = Function(
        "FUNCTION2",
        "Function 2",
        "Description function 2",
        str,
        printMsg,
        False,
        ["EVENT1", "EVENT2"],
    )
    myMsbClient.addFunction(function2)

    # the final data format can be provided as a valid JSON object
    myMsbClient.addFunction(
        "COMPLEX_JSON_FUNCTION",
        "Function JSON based",
        "Description function JSON based",
        {
            "Member" : {
                "type" : "object",
                "properties" : {
                    "name" : {
                        "type" : "string"
                    },
                    "status" : {
                        "enum" : [ "present", "absent" ],
                        "type" : "string"
                    }
                }
            },
            "Team" : {
                "type" : "object",
                "properties" : {
                    "staff" : {
                        "type" : "array",
                        "items" : {
                            "$ref" : "#/definitions/Member"
                        }
                    }
                }
            },
            "dataObject" : {
                "$ref" : "#/definitions/Team"
            }
        },
        printMsg,
        False,
        ["EVENT1", "EVENT2"],
    )

    # define the function which will be passed to the function description
    def arrayfun_implementation(msg):
        logging.debug("Array Function has been called, message: " + str(msg["a"]))
        global receivedArrayEvIndexCheck
        receivedArrayEvIndexCheck = True
        receivedArrayEvIndexCheck = str(msg["a"][0]) == "Hello"
        receivedArrayEvIndexCheck = str(msg["a"][1]) == "World"
        receivedArrayEvIndexCheck = str(msg["a"][2]) == "!"
        logging.debug(
            "Array Function has been called, correlationId: " + msg["correlationId"]
        )
        global receivedArrayEvWithCorrectCorrelationId
        receivedArrayEvWithCorrectCorrelationId = (
            str(msg["correlationId"]) == CORRELATIOON_ID_FOR_TEST
        )
        global receivedArrayEv
        receivedArrayEv = True

    # add the function to be tested in integration flow
    function_arrayfun = Function(
        "/arrayfun",
        "Array Function",
        "Array Function for testing",
        str,
        arrayfun_implementation,
        True,
        None,
    )
    myMsbClient.addFunction(function_arrayfun)

    logging.debug("Self Description - added functions")
        "REQUEST",
        DataType.STRING,
        1,
        False,
    )

    e_result = Event(
        "RESULT",
        "RESULT",
        "RESULT",
        DataType.STRING,
        1,
        False,
    )

    f_authenticate = Function("AUTHENTICATE", "AUTHENTICATE", "AUTHENTICATE",
                              DataType.STRING, f_func1, False)

    f_authenticate.addMetaData(
        CustomMetaData("CPU_SPEED", "Verify CPU_SPEED measurement array",
                       TypeDescription(TypeDescriptor.CUSTOM, "CPU_SPEED", ""),
                       "/F_AUTHENTICATE", "CPU_SPEED", DataType.FLOAT, True))

    myMsbClient.addEvent(e_auth)
    myMsbClient.addEvent(e_result)

    myMsbClient.addFunction(f_authenticate)

    # myMsbClient.addMetaData(
    #     CustomMetaData(
    #         "SEN",
    #         "Sensor - device which, when excited by a physical phenomenon, produces an electric signal characterizing the physical phenomenon",
    #     )

    # create new function object
    # This example has no response events.
    # parameter 1 (str:‘FUNCTION1’): internal function name reference (inside program code)
    # parameter 2 (str:‘Function1’): MSB function name (visible in MSB GUI)
    # parameter 3 (str:'Function1_description’): description which shows up in MSB GUI
    # parameter 4 (DataType:DataType.STRING): type of data the function will handle
    # parameter 5 (fnPointer:printMsg): pointer to the function implementation
    # parameter 6 (bool:optional): True if payload is an array of parameter 4
    # parameter 7 (list:optional): array of response events e.g. ['RESPONSE_EVENT1']
    function1 = Function(
        "FUNCTION1",
        "Function1",
        "Function1_description",
        DataType.STRING,
        printMsg,
        True,
        [],
    )
    # add function objects to MSB client
    myMsbClient.addFunction(function1)

    function1.addMetaData(
        CustomMetaData(
            "Funktion_Temperatur",
            "Funktion_Umgebungstemperatur",
            TypeDescription(
                TypeDescriptor.CUSTOM,
                "0112/2///61987#ABT514#001",
                "https://cdd.iec.ch/cdd/iec61987/iec61987.nsf/ListsOfUnitsAllVersions/0112-2---61987%23ABT514",
        # don't forget to pass the parameter name for the correlationId if you're not passing all parameters
        myMsbClient.publish(
            "RESPONSE_EVENT1", msg["dataObject"], correlationId=msg["correlationId"]
        )

    # create new function object
    # This example has no response events.
    # parameter 1 (str:‘FUNCTION1’): internal function name reference (inside program code)
    # parameter 2 (str:‘Function1’): MSB function name (visible in MSB GUI)
    # parameter 3 (str:'Function1_description’): description which shows up in MSB GUI
    # parameter 4 (DataType:DataType.STRING): type of data the function will handle
    # parameter 5 (fnPointer:printMsg): pointer to the function implementation
    # parameter 6 (bool:optional): True if payload is an array of parameter 4
    # parameter 7 (list:optional): array of response events e.g. ['RESPONSE_EVENT1']
    function1 = Function(
        "FUNCTION1", "Function1", "Function1_description", DataType.STRING, printMsg, True, []
    )
    # add function objects to MSB client
    myMsbClient.addFunction(function1)

    # optionally, add function directly in line
    # this example has one response event.
    myMsbClient.addFunction(
        "FUNCTION2",
        "Function2",
        "Function2_description",
        DataType.STRING,
        sendResponseEventExample,
        False,
        ["RESPONSE_EVENT1"],
    )
    def requestSelfdescription(serviceUuid):
        myMsbClient.publish("SELFDESCRIPTION_REQUEST", "", 1, False, None,
                            serviceUuid)

    e_selfdescriptionRequest = Event(
        "SELFDESCRIPTION_REQUEST",
        "Request for selfdescription",
        "Request for selfdescription",
        DataType.STRING,
        1,
        False,
    )
    myMsbClient.addEvent(e_selfdescriptionRequest)

    f_idSelfdescription = Function("ID_SELFDESCRIPTION",
                                   "Identify Entity by its selfdescription",
                                   "Identify Entity", DataType.STRING,
                                   identifyEntity, False)
    myMsbClient.addFunction(f_idSelfdescription)

    print(myMsbClient.objectToJson(myMsbClient.getSelfDescription()))

    myMsbClient.connect(msb_url)
    myMsbClient.register()

    # def runMsbClient():
    #     myMsbClient.connect(msb_url)
    #     myMsbClient.register()

    # wst = threading.Thread(target=runMsbClient)
    # # wst.setDaemon(True)
    # wst.start()