Commit 8e898979 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Device component:

- Added logic to auto-enable devices when Automation component is not deployed
- Updated retrieval of device controller
parent 089e08f7
Loading
Loading
Loading
Loading
+12 −2
Original line number Diff line number Diff line
@@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import grpc, logging, time
import grpc, logging, os, time
from typing import Dict
from prometheus_client import Histogram
from common.Constants import ServiceNameEnum
from common.Settings import ENVVAR_SUFIX_SERVICE_HOST, get_env_var_name
from common.method_wrappers.Decorator import MetricTypeEnum, MetricsPool, safe_and_metered_rpc_method
from common.method_wrappers.ServiceExceptions import NotFoundException, OperationFailedException
from common.proto.context_pb2 import (
@@ -121,7 +123,15 @@ class DeviceServiceServicerImpl(DeviceServiceServicer):

            t9 = time.time()

            automation_service_host = get_env_var_name(ServiceNameEnum.AUTOMATION, ENVVAR_SUFIX_SERVICE_HOST)
            environment_variables = set(os.environ.keys())
            if automation_service_host in environment_variables:
                # Automation component is deployed; leave devices disabled. Automation will enable them.
                device.device_operational_status = DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_DISABLED
            else:
                # Automation is not deployed; assume the device is ready while onboarding and set them as enabled.
                device.device_operational_status = DeviceOperationalStatusEnum.DEVICEOPERATIONALSTATUS_ENABLED

            device_id = context_client.SetDevice(device)

            t10 = time.time()
+13 −10
Original line number Diff line number Diff line
@@ -79,11 +79,13 @@ def check_no_endpoints(device_endpoints) -> None:
                        'interrogation of the physical device.')

def get_device_controller_uuid(device : Device) -> Optional[str]:
    for config_rule in device.device_config.config_rules:
        if config_rule.WhichOneof('config_rule') != 'custom': continue
        if config_rule.custom.resource_key != '_controller': continue
        device_controller_id = json.loads(config_rule.custom.resource_value)
        return device_controller_id['uuid']
    controller_uuid = device.controller_id.device_uuid.uuid
    if len(controller_uuid) > 0: return controller_uuid
    #for config_rule in device.device_config.config_rules:
    #    if config_rule.WhichOneof('config_rule') != 'custom': continue
    #    if config_rule.custom.resource_key != '_controller': continue
    #    device_controller_id = json.loads(config_rule.custom.resource_value)
    #    return device_controller_id['uuid']
    return None

def populate_endpoints(
@@ -142,11 +144,12 @@ def populate_endpoints(
            # Sub-devices should not have a driver assigned. Instead, they should have
            # a config rule specifying their controller.
            #_sub_device.device_drivers.extend(resource_value['drivers'])        # pylint: disable=no-member
            controller_config_rule = _sub_device.device_config.config_rules.add()
            controller_config_rule.action = ConfigActionEnum.CONFIGACTION_SET
            controller_config_rule.custom.resource_key = '_controller'
            controller = {'uuid': device_uuid, 'name': device_name}
            controller_config_rule.custom.resource_value = json.dumps(controller, indent=0, sort_keys=True)
            #controller_config_rule = _sub_device.device_config.config_rules.add()
            #controller_config_rule.action = ConfigActionEnum.CONFIGACTION_SET
            #controller_config_rule.custom.resource_key = '_controller'
            #controller = {'uuid': device_uuid, 'name': device_name}
            #controller_config_rule.custom.resource_value = json.dumps(controller, indent=0, sort_keys=True)
            _sub_device.controller_id.device_uuid.uuid = device_uuid

            new_sub_devices[_sub_device_uuid] = _sub_device