Skip to content

Commit

Permalink
Merge pull request vmware-archive#148 from winniex1/get_property
Browse files Browse the repository at this point in the history
Property process improvement:
  • Loading branch information
winniex1 authored Nov 2, 2017
2 parents 44d7513 + fca0d54 commit fb2009d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/liota.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ log_path = /var/log/liota
dev_file_path = /usr/lib/liota/devs
entity_file_path = /usr/lib/liota/entity
iotcc_path = /usr/lib/liota/iotcc.json
enable_reboot_getprop = False
iotcc_load_retry = 3

[CRL_PATH]
Expand Down
1 change: 1 addition & 0 deletions liota-lite/requirements.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ aenum==1.4.5
linux-metrics==0.1.4
paho-mqtt==1.3.1
pint==0.7.2
uptime==3.0.1
74 changes: 68 additions & 6 deletions liota/dccs/iotcc.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import ConfigParser
import os
import Queue
import datetime
from time import gmtime, strftime
from uptime import boottime
from threading import Lock

from liota.dccs.dcc import DataCenterComponent, RegistrationFailure
Expand Down Expand Up @@ -73,8 +75,11 @@ def __init__(self, con):
time.sleep(0.5)
self._iotcc_json = self._create_iotcc_json()
self._iotcc_json_load_retry = int(read_liota_config('IOTCC_PATH', 'iotcc_load_retry'))
self.enable_reboot_getprop = read_liota_config('IOTCC_PATH', 'enable_reboot_getprop')
self.counter = 0
self.recv_msg_queue = self.comms.userdata
self.boottime = boottime()

self.dev_file_path = self._get_file_storage_path("dev_file_path")
# Liota internal entity file system path special for iotcc
self.entity_file_path = self._get_file_storage_path("entity_file_path")
Expand Down Expand Up @@ -134,7 +139,7 @@ def on_response(msg):
return RegisteredEntity(entity_obj, self, self.reg_entity_id)

def _check_version(self, json_msg):
if json_msg["version"] != self._version:
if json_msg["version"] != self._version:
raise Exception("CLIENT SERVER VERSION MISMATCH. CLIENT VERSION IS:" + self._version + ". SERVER VERSION IS:" + json_msg["version"])

def unregister(self, entity_obj):
Expand Down Expand Up @@ -234,6 +239,16 @@ def _properties(self, msg_id, entity_type, entity_id, entity_name, timestamp, pr
msg["body"]["property_data"].append({"propertyKey": key, "propertyValue": value})
return msg

def _get_properties(self, msg_id, res_uuid):
return {
"transactionID": msg_id,
"version": self._version,
"type": "get_properties_request",
"body": {
"uuid": res_uuid
}
}

def _format_data(self, reg_metric):
met_cnt = reg_metric.values.qsize()
if met_cnt == 0:
Expand Down Expand Up @@ -419,7 +434,7 @@ def write_entity_json_file(self, prop_dict, attribute_list, uuid, remove):
if prop_dict is not None:
for key in prop_dict.iterkeys():
value = prop_dict[key]
if key == 'entity type' or key == 'name' or key == 'device type':
if key == 'entity type' or key == 'name' or key == 'device type' or key == 'Entity_Timestamp':
continue
attribute_list.append({key: value})
attribute_list.append({"LastSeenTimestamp": strftime("%Y-%m-%dT%H:%M:%S", gmtime())})
Expand Down Expand Up @@ -489,6 +504,7 @@ def store_device_info(self, uuid, name, dev_type, prop_dict, remove_device):

def write_entity_file(self, prop_dict, res_uuid):
file_path = self.entity_file_path + '/' + res_uuid + '.json'
prop_dict.update({"Entity_Timestamp": datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")})
try:
with open(file_path, "w") as json_file:
if (prop_dict is not None):
Expand All @@ -507,10 +523,22 @@ def read_entity_file(self, res_uuid):
log.error('Read file error')
return prop_dict

def merge_prop_dict_list(self, prop_dict, prop_list):
# prop_dict: new property dictionary
# prop_list: list of dictionary items
if (prop_list is None):
return prop_dict
if (prop_dict is None):
prop_dict = {}
for item in prop_list:
prop_dict.update(item)
# get updated dict
return prop_dict

def store_reg_entity_attributes(self, entity_type, entity_name, reg_entity_id,
dev_type, prop_dict):
log.debug('store_reg_entity_attributes\n {0}:{1}:{2}:{3}'.format(entity_type,
entity_name, reg_entity_id, prop_dict))
log.debug('store_reg_entity_attributes {0}:{1}:{2}:{3}'.format(entity_type,
entity_name, reg_entity_id, prop_dict))

### Update IOTCC local entity file first
# look for uuid.json file first, if not, first time to write
Expand All @@ -532,8 +560,17 @@ def store_reg_entity_attributes(self, entity_type, entity_name, reg_entity_id,
new_prop_dict = tmp_dict
else:
tmp_dict = self.read_entity_file(reg_entity_id)
if ((tmp_dict["entity type"] == entity_type) and (tmp_dict["name"] == entity_name)
and (tmp_dict["device type"] == dev_type)):
# check Entity_Timestamp of entity_file: if < boottime, get properties from cloud
if (self.enable_reboot_getprop == "True") and ('Entity_Timestamp' in tmp_dict):
last_dtime = datetime.datetime.strptime(tmp_dict["Entity_Timestamp"], "%Y-%m-%dT%H:%M:%S")
if (last_dtime <= self.boottime):
list_prop = self.get_properties(reg_entity_id)
# merge property info from get_properties() into our local entity record
tmp_dict = self.merge_prop_dict_list(tmp_dict, list_prop)
if ((('entity type' in tmp_dict) and (tmp_dict["entity type"] == entity_type)) and
(('name' in tmp_dict) and (tmp_dict["name"] == entity_name)) and
(('device type' in tmp_dict) and ((tmp_dict["device type"] == dev_type) or
((tmp_dict["device type"] == '') and (dev_type == None))))):
# the same entity
if (tmp_dict is not None) and (prop_dict is not None):
new_prop_dict = dict(tmp_dict.items() + prop_dict.items())
Expand Down Expand Up @@ -610,3 +647,28 @@ def _unregistration(self, msg_id, ref_entity):
"name": ref_entity.name
}
}

def get_properties(self, resource_uuid):
""" get list of properties with resource uuid """
log.info("Get properties defined with IoTCC for resource {0}".format(resource_uuid))
self.prop_list = None

def on_response(msg):
try:
log.debug("Received msg: {0}".format(msg))
json_msg = json.loads(msg)
log.debug("Processing msg: {0}".format(json_msg["type"]))
self._check_version(json_msg)
if json_msg["type"] == "get_properties_response" and json_msg["body"]["uuid"] != "null" and \
json_msg["body"]["uuid"] == resource_uuid:
log.info("FOUND PROPERTY LIST: {0}".format(json_msg["body"]["propertyList"]))
self.prop_list = json_msg["body"]["propertyList"]
else:
log.info("Waiting for getting properties")
on_response(self.recv_msg_queue.get(True, timeout))
except:
log.exception("Exception while getting properties")

self.comms.send(json.dumps(self._get_properties(self.next_id(), resource_uuid)))
on_response(self.recv_msg_queue.get(True, timeout))
return self.prop_list
1 change: 1 addition & 0 deletions requirements.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mock==2.0.0
paho-mqtt==1.3.1
pint==0.7.2
websocket-client==0.37.0
uptime==3.0.1

0 comments on commit fb2009d

Please sign in to comment.