Toshiba / Mbed OS mbed-os-example-pelion
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers observation.py Source File

observation.py

00001 """
00002 Copyright 2019 ARM Limited
00003 Licensed under the Apache License, Version 2.0 (the "License");
00004 you may not use this file except in compliance with the License.
00005 You may obtain a copy of the License at
00006 
00007     http://www.apache.org/licenses/LICENSE-2.0
00008 
00009 Unless required by applicable law or agreed to in writing, software
00010 distributed under the License is distributed on an "AS IS" BASIS,
00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 See the License for the specific language governing permissions and
00013 limitations under the License.
00014 """
00015 
00016 # pylint: disable=missing-docstring,useless-super-delegation
00017 # pylint: disable=line-too-long,method-hidden,relative-import
00018 
00019 import time
00020 from icetea_lib.bench import TestStepFail
00021 from mbed_cloud.exceptions import CloudApiException
00022 from pelion_helper import PelionBase
00023 
00024 
00025 class Testcase(PelionBase):
00026     def __init__(self):
00027         PelionBase.__init__(self,
00028                             name="observation",
00029                             title="Example application can respond to resource subscription)",
00030                             status="released",
00031                             type="acceptance",
00032                             component=["mbed_cloud_client_example"])
00033 
00034     def setup(self):
00035         super(Testcase, self).setup()
00036 
00037     def case(self):
00038         # Test resource subscription
00039         resource_path = '/3201/0/5853'
00040 
00041         # 1) Add initial resource value
00042         self.logger.info("PUT %s with value %s", resource_path, self.pattern_value1)
00043         try:
00044             self.connect_api.set_resource_value(device_id=self.device_id,
00045                                                 resource_path=resource_path,
00046                                                 resource_value=self.pattern_value1,
00047                                                 timeout=self.restTimeout)
00048         except CloudApiException as error:
00049             raise TestStepFail("PUT request failed with %d and msg %s" % (error.status, error.message))
00050 
00051         # Create resource subscription for custom resource (/3201/0/5853)
00052         self.logger.info("Testing subscription for %s", resource_path)
00053         self.connect_api.start_notifications()
00054 
00055         try:
00056             self.connect_api.add_resource_subscription_async(device_id=self.device_id,
00057                                                              resource_path=resource_path,
00058                                                              callback_fn=self._callback_fn)
00059         except CloudApiException as error:
00060             raise TestStepFail("Subscription request failed with %d and msg %s" % (error.status, error.message))
00061 
00062         # 2) Update resource value
00063         self.logger.info("PUT %s with value %s", resource_path, self.pattern_value2)
00064         try:
00065             self.connect_api.set_resource_value(device_id=self.device_id,
00066                                                 resource_path=resource_path,
00067                                                 resource_value=self.pattern_value2,
00068                                                 timeout=self.restTimeout)
00069         except CloudApiException as error:
00070             raise TestStepFail("PUT request failed with %d and msg %s" % (error.status, error.message))
00071 
00072         self.logger.info("Waiting for resource notification")
00073         notif_timeout = time.time() + self.restTimeout
00074 
00075         # 3) Wait until we receive some value or timeout
00076         while time.time() < notif_timeout:
00077             if self.notified_value != "":
00078                 break
00079 
00080         if self.notified_value != self.pattern_value2:
00081             self.logger.error("Web-application received %s as notification, expected %s", self.notified_value, self.pattern_value2)
00082             raise TestStepFail("Incorrect/No notification received")
00083         else:
00084             self.logger.info("Web-application received %s as notification.", self.notified_value)
00085 
00086         # 4) Remove subscription
00087         try:
00088             self.connect_api.delete_resource_subscription(device_id=self.device_id)
00089         except CloudApiException as error:
00090             raise TestStepFail("Subscription removal failed with %d and msg %s" % (error.status, error.message))
00091 
00092         # 5) Update resource value to trigger reset from server
00093         # This is needed to clean the client subscription status for any follow-up tests
00094         self.logger.info("PUT %s with value %s", resource_path, self.pattern_value3)
00095         try:
00096             self.connect_api.set_resource_value(device_id=self.device_id,
00097                                                 resource_path=resource_path,
00098                                                 resource_value=self.pattern_value3,
00099                                                 timeout=self.restTimeout)
00100         except CloudApiException as error:
00101             raise TestStepFail("PUT request failed with %d and msg %s" % (error.status, error.message))
00102 
00103     def teardown(self):
00104         # Remove subscription
00105         try:
00106             self.connect_api.delete_resource_subscription(device_id=self.device_id)
00107         except CloudApiException as error:
00108             raise TestStepFail("Subscription removal failed with %d and msg %s" % (error.status, error.message))
00109 
00110         self.connect_api.stop_notifications()