Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py	(revision 2908)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/lib/task_scheduler.py	(working copy)
@@ -1962,17 +1962,52 @@
 '''
 start check device status
 '''
+
+from hive.model.query import mark_expire_one
+from hive.model.loading import get_model
 
 def set_device_version(device_id, version):
     db = DB.get_connected_db()
-    update_sql = "UPDATE device SET version='%s' WHERE id='%s'" % (version, device_id)
-    db.execute_sql(update_sql)
+    
+    # Check if it's device2
+    is_device2 = db.fetchone("SELECT id FROM device2 WHERE id='%s'" % device_id)
+    if is_device2:
+        update_sql = "UPDATE device2 SET version='%s' WHERE id='%s'" % (version, device_id)
+        db.execute_sql(update_sql)
+        try:
+            # Need to expire cache for CMDevice
+            mark_expire_one(get_model('cm', ['device_mgmt', 'cm_device', 'CMDevice']), device_id)
+        except Exception, e:
+            logger.error('Failed to expire cache for CMDevice %s: %s' % (device_id, str(e)))
+    else:
+        update_sql = "UPDATE device SET version='%s' WHERE id='%s'" % (version, device_id)
+        db.execute_sql(update_sql)
+        try:
+             mark_expire_one(get_model('cm', ['device_mgmt', 'device', 'Device']), device_id)
+        except Exception, e:
+             logger.error('Failed to expire cache for Device %s: %s' % (device_id, str(e)))
+             
     db.close()
 
 def set_device_status(device_id, status):
     db = DB.get_connected_db()
-    update_sql = "UPDATE device SET connection='%s' WHERE id='%s'" % (status, device_id)
-    db.execute_sql(update_sql)
+    
+    is_device2 = db.fetchone("SELECT id FROM device2 WHERE id='%s'" % device_id)
+    if is_device2:
+        update_sql = "UPDATE device2 SET connection='%s' WHERE id='%s'" % (status, device_id)
+        db.execute_sql(update_sql)
+        try:
+            mark_expire_one(get_model('cm', ['device_mgmt', 'cm_device', 'CMDevice']), device_id)
+        except Exception, e:
+            logger.error('Failed to expire cache for CMDevice %s: %s' % (device_id, str(e)))
+    else:
+         update_sql = "UPDATE device SET connection='%s' WHERE id='%s'" % (status, device_id)
+         db.execute_sql(update_sql)
+         try:
+             mark_expire_one(get_model('cm', ['device_mgmt', 'device', 'Device']), device_id)
+         except Exception, e:
+             logger.error('Failed to expire cache for Device %s: %s' % (device_id, str(e)))
+
     db.close()
 
 def set_device_snmp(device_id, snmp_general):
@@ -1997,23 +2032,51 @@
                 time.sleep(2)
         else:
             try:
-                new_url = modify_url('/rest/device_type/system/SystemInfo/version', data['type'])
-                rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'])
+                new_url = '/rest/apv/system/SystemInfo/version'
+                rest_response_data = send_https_rest_request('GET', new_url, '', data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], lock=False)
             except Exception, e:
                 failed_time -= 1
-                logger.error('Get device<%s>(%s) version failed. %s' % (data['name'], data['ip'], str(e)))
+                logger.error('Get device<%s>(%s) version failed. Exception: %s' % (data['name'], data['ip'], str(e)))
                 time.sleep(2)
             else:
-                if rest_response_data['status'] == 200 and data['connection'] != 'connected':
-                    set_device_status(data['id'], 'connected')
-                try:
-                    rest_body = json.loads(rest_response_data['body'])
-                    set_device_version(data['id'], rest_body['SystemInfo']['version'])
-                except Exception, e:
-                    logger.error('Get device<%s>(%s) version failed.' % (data['name'], data['ip']))
-                break
+                if rest_response_data['status'] == 200:
+                    if data['connection'] != 'connected':
+                        logger.info('Device<%s>(%s) connection recovered. Setting status to connected.' % (data['name'], data['ip']))
+                        set_device_status(data['id'], 'connected')
+
+                        # Re-register device if it is managed by license server (in device2 table)
+                        try:
+                            check_db = DB.get_connected_db()
+                            is_license_device = check_db.fetchone("SELECT id FROM device2 WHERE ip='%s'" % data['ip'])
+                            check_db.close()
+                            
+                            if is_license_device:
+                                logger.info('Device<%s>(%s) is a license managed device. Re-registering to restore activationserver config.' % (data['name'], data['ip']))
+                                reg_url = '/rest/cm/Registration'
+                                reg_body = json.dumps({'port': 8888})
+                                # Use timeout=10 to match usage in some places or default
+                                reg_response = send_https_rest_request('POST', reg_url, reg_body, data['ip'], data['restapi_port'], data['restapi_username'], data['restapi_password'], lock=False)
+                                
+                                if reg_response.get('status') == 200:
+                                    logger.info('Device<%s>(%s) re-registered successfully.' % (data['name'], data['ip']))
+                                else:
+                                    logger.error('Device<%s>(%s) re-registration failed. HTTP %s. Body: %s' % (data['name'], data['ip'], reg_response.get('status'), reg_response.get('body', '')[:100]))
+                        except Exception, e:
+                            logger.error('Device<%s>(%s) re-registration exception: %s' % (data['name'], data['ip'], str(e)))
+
+                    try:
+                        rest_body = json.loads(rest_response_data['body'])
+                        set_device_version(data['id'], rest_body['SystemInfo']['version'])
+                    except Exception, e:
+                        logger.error('Get device<%s>(%s) version failed. JSON parse error. Body: %s' % (data['name'], data['ip'], rest_response_data['body'][:100]))
+                    break
+                else:
+                    logger.error('Get device<%s>(%s) version failed. HTTP Status: %s, Body: %s' % (data['name'], data['ip'], rest_response_data.get('status'), rest_response_data.get('body', '')[:100]))
+                    failed_time -= 1
+                    time.sleep(2)
     if not failed_time:
         if data['connection'] != 'unconnected':
+            logger.warning('Device<%s>(%s) connection lost. Setting status to unconnected.' % (data['name'], data['ip']))
             set_device_status(data['id'], 'unconnected')
     else:
         if os.path.exists(os.path.join(extension_path, "monitoring")):
@@ -2158,6 +2221,10 @@
             db = DB.get_connected_db()        
             select_sql = "SELECT id, name, ip_address, protocol, restapi_port, restapi_username, restapi_password, type, enable_password, connection FROM device"
             data = db.fetchall(select_sql)
+            
+            # Also check device2 (License Managed Devices) - only active ones
+            select_sql2 = "SELECT id, ip, restapi_protocol, restapi_port, restapi_username, restapi_password, connection FROM device2 WHERE status='active'"
+            data2 = db.fetchall(select_sql2)
         finally:
             if db:
                 db.close()
@@ -2166,6 +2233,17 @@
         return
     key = ['id', 'name', 'ip', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'type', 'enable_password', 'connection']
     result = [dict(zip(key, each)) for each in data]
+    
+    # Process device2 data
+    if data2:
+        key2 = ['id', 'ip', 'protocol', 'restapi_port', 'restapi_username', 'restapi_password', 'connection']
+        result2 = [dict(zip(key2, each)) for each in data2]
+        for each in result2:
+            each['name'] = each['ip']
+            each['type'] = 'Array APV' # Default type for license managed devices
+            each['enable_password'] = ''
+            result.append(each)
+
     for each in result:
         device_check_pool.apply_async(check_one_device_status, (each,))
 '''
Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/device_mgmt/device/__init__.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/device_mgmt/device/__init__.py	(revision 2908)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/cm/models/device_mgmt/device/__init__.py	(working copy)
@@ -501,7 +501,10 @@
                         raise ModelQueryException(CLICmdError(__('Register Failed! Please make sure the XML RPC configuration is correct!')))
                 else:
                     if rest_response_data['status'] == 200:
-                        rest_body = json.loads(rest_response_data['body'])
+                        try:
+                            rest_body = json.loads(rest_response_data['body'])
+                        except ValueError:
+                            raise ModelQueryException(CLICmdError(__('Invalid JSON response from device. Body start: %s' % rest_response_data['body'])))
                         data['version'] = rest_body['SystemInfo']['version']
                     elif rest_response_data['status'] == 401:
                         raise ModelQueryException(CLICmdError(__('Register Failed! Please make sure the RESTful API username/password is correct!')))
Index: /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/hive/model/query.py
===================================================================
--- /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/hive/model/query.py	(revision 2908)
+++ /branches/amp_3_7_2/src/webui/webui/htdocs/new/src/hive/model/query.py	(working copy)
@@ -524,7 +524,9 @@
 def mark_expire_one(module, pk):
     if not CACHE_SWITCH:
         return True
-    anerror('hive.debug', 'mark_expire_one: module is %s, pk is %s' % (module, pk))
+    if pk is None:
+        return
+    andebug('hive.debug', 'mark_expire_one: module is %s, pk is %s' % (module, pk))
     ins = get_cache_one(module, pk, False)
     if ins:
         manager_cache.remove_module(ins._meta._model)
