class AliObjectModel(CloudObjectModel):
"""This class is aliyun object model
This class extend CloudObjectModel.
Attribute:
events:
Attribute:
- object model event
- attribute value data format
{
"sos_alert": {
local_time: 1651136994000
}
}
properties:
Attribute:
- object model property
- attribute value data format
{
"GeoLocation": {
"Longtitude": 0.0,
"Latitude": 0.0,
"Altitude": 0.0,
"CoordinateSystem": 0
}
}
"""
def __init__(self, om_file="/usr/aliyun_object_model.json"):
super().__init__(om_file)
self.init()
def __init_value(self, om_type):
if om_type in ("int", "enum", "date"):
om_value = 0
elif om_type in ("float", "double"):
om_value = 0.0
elif om_type == "bool":
om_value = True
elif om_type == "text":
om_value = ""
elif om_type == "array":
om_value = []
elif om_type == "struct":
om_value = {}
else:
om_value = None
return om_value
def __get_property(self, om_item):
om_item_key = om_item["identifier"]
om_item_type = om_item["dataType"]["type"]
om_item_val = self.__init_value(om_item_type)
if om_item_type == "struct":
om_item_struct = om_item["dataType"]["specs"]
om_item_val = {i["identifier"]: self.__init_value(i["dataType"]["type"]) for i in om_item_struct}
return om_item_key, om_item_val
def __init_properties(self, om_properties):
for om_property in om_properties:
om_property_key, om_property_val = self.__get_property(om_property)
setattr(self.properties, om_property_key, {om_property_key: om_property_val})
def __init_events(self, om_events):
for om_event in om_events:
om_event_key = om_event["identifier"]
om_event_out_put = om_event.get("outputData", [])
om_event_val = {}
if om_event_out_put:
for om_property in om_event_out_put:
om_property_key, om_property_val = self.__get_property(om_property)
om_property_val[om_property_key] = om_property_val
setattr(self.events, om_event_key, {om_event_key: om_event_val})
def init(self):
with open(self.om_file, "rb") as f:
cloud_object_model = ujson.load(f)
self.__init_properties(cloud_object_model.get("properties", []))
self.__init_events(cloud_object_model.get("events", []))