1. class AliObjectModel(CloudObjectModel):
    2. """This class is aliyun object model
    3. This class extend CloudObjectModel.
    4. Attribute:
    5. events:
    6. Attribute:
    7. - object model event
    8. - attribute value data format
    9. {
    10. "sos_alert": {
    11. local_time: 1651136994000
    12. }
    13. }
    14. properties:
    15. Attribute:
    16. - object model property
    17. - attribute value data format
    18. {
    19. "GeoLocation": {
    20. "Longtitude": 0.0,
    21. "Latitude": 0.0,
    22. "Altitude": 0.0,
    23. "CoordinateSystem": 0
    24. }
    25. }
    26. """
    27. def __init__(self, om_file="/usr/aliyun_object_model.json"):
    28. super().__init__(om_file)
    29. self.init()
    30. def __init_value(self, om_type):
    31. if om_type in ("int", "enum", "date"):
    32. om_value = 0
    33. elif om_type in ("float", "double"):
    34. om_value = 0.0
    35. elif om_type == "bool":
    36. om_value = True
    37. elif om_type == "text":
    38. om_value = ""
    39. elif om_type == "array":
    40. om_value = []
    41. elif om_type == "struct":
    42. om_value = {}
    43. else:
    44. om_value = None
    45. return om_value
    46. def __get_property(self, om_item):
    47. om_item_key = om_item["identifier"]
    48. om_item_type = om_item["dataType"]["type"]
    49. om_item_val = self.__init_value(om_item_type)
    50. if om_item_type == "struct":
    51. om_item_struct = om_item["dataType"]["specs"]
    52. om_item_val = {i["identifier"]: self.__init_value(i["dataType"]["type"]) for i in om_item_struct}
    53. return om_item_key, om_item_val
    54. def __init_properties(self, om_properties):
    55. for om_property in om_properties:
    56. om_property_key, om_property_val = self.__get_property(om_property)
    57. setattr(self.properties, om_property_key, {om_property_key: om_property_val})
    58. def __init_events(self, om_events):
    59. for om_event in om_events:
    60. om_event_key = om_event["identifier"]
    61. om_event_out_put = om_event.get("outputData", [])
    62. om_event_val = {}
    63. if om_event_out_put:
    64. for om_property in om_event_out_put:
    65. om_property_key, om_property_val = self.__get_property(om_property)
    66. om_property_val[om_property_key] = om_property_val
    67. setattr(self.events, om_event_key, {om_event_key: om_event_val})
    68. def init(self):
    69. with open(self.om_file, "rb") as f:
    70. cloud_object_model = ujson.load(f)
    71. self.__init_properties(cloud_object_model.get("properties", []))
    72. self.__init_events(cloud_object_model.get("events", []))