Пример #1
0
    def __getitem__(self, key):
        """
        Get a log entry
        """
        if isinstance(key, int):
            # Special case -- allow this to be used as a dict or a list
            # Get all, convert list to iterator
            logsUrl = 'api/v0002/device/types/%s/devices/%s/diag/logs' % (
                self.typeId, self.deviceId)

            r = self._apiClient.get(logsUrl)
            if r.status_code == 200:
                if key > len(r.json()):
                    self.__missing__(key)
                return DeviceLog(**r.json()[key])
            else:
                raise ApiException(r)
        else:
            logUrl = 'api/v0002/device/types/%s/devices/%s/diag/logs/%s' % (
                self.typeId, self.deviceId, key)

            r = self._apiClient.get(logUrl)
            if r.status_code == 200:
                return DeviceLog(**r.json())
            elif r.status_code == 404:
                self.__missing__(key)
            else:
                raise ApiException(r)
Пример #2
0
    def __delitem__(self, key):
        """
        Delete a log
        """
        if isinstance(key, int):
            # Special case -- allow this to be used as a dict or a list
            logsUrl = 'api/v0002/device/types/%s/devices/%s/diag/logs' % (
                self.typeId, self.deviceId)

            r = self._apiClient.get(logsUrl)
            if r.status_code == 200:
                if key > len(r.json()):
                    self.__missing__(key)
                key = r.json()[key]["id"]
            else:
                raise ApiException(r)

        logUrl = 'api/v0002/device/types/%s/devices/%s/diag/logs/%s' % (
            self.typeId, self.deviceId, key)

        r = self._apiClient.delete(logUrl)
        if r.status_code == 404:
            self.__missing__(key)
        elif r.status_code != 204:
            raise ApiException(r)
Пример #3
0
    def create(self, devices):
        """
        Register one or more new devices, each request can contain a maximum of 512KB.
        The response body will contain the generated authentication tokens for all devices.
        You must make sure to record these tokens when processing the response.
        We are not able to retrieve lost authentication tokens
        
        It accepts accepts a list of devices (List of Dictionary of Devices), or a single device
        
        If you provide a list as the parameter it will return a list in response
        If you provide a singular device it will return a singular response
        """
        if not isinstance(devices, list):
            listOfDevices = [devices]
            returnAsAList = False
        else:
            listOfDevices = devices
            returnAsAList = True

        r = self._apiClient.post('api/v0002/bulk/devices/add', listOfDevices)

        if r.status_code in [201, 202]:
            if returnAsAList:
                responseList = []
                for entry in r.json():
                    responseList.append(DeviceCreateResponse(**entry))
                return responseList
            else:
                return DeviceCreateResponse(**r.json()[0])
        else:
            raise ApiException(r)
Пример #4
0
    def __delitem__(self, key):
        """
        delete a device type
        """
        url = 'api/v0002/device/types/%s' % (key)

        r = self._apiClient.delete(url)
        if r.status_code != 204:
            raise ApiException(r)
Пример #5
0
    def setLocation(self, value):
        r = self._apiClient.put(
            'api/v0002/device/types/%s/devices/%s/location' %
            (self.typeId, self.deviceId), value)

        if r.status_code == 200:
            return DeviceLocation(**r.json())
        else:
            raise ApiException(r)
Пример #6
0
    def __len__(self):
        # Get all, convert list to iterator
        logsUrl = 'api/v0002/device/types/%s/devices/%s/diag/errorCodes' % (
            self.typeId, self.deviceId)

        r = self._apiClient.get(logsUrl)
        if r.status_code == 200:
            return len(r.json())
        else:
            raise ApiException(r)
Пример #7
0
    def clear(self):
        # Get all, convert list to iterator
        ecUrl = 'api/v0002/device/types/%s/devices/%s/diag/errorCodes' % (
            self.typeId, self.deviceId)

        r = self._apiClient.delete(ecUrl)
        if r.status_code == 204:
            return True
        else:
            raise ApiException(r)
Пример #8
0
    def create(self, deviceType):
        """
        Register one or more new device types, each request can contain a maximum of 512KB.
        """

        r = self._apiClient.post('api/v0002/device/types', deviceType)

        if r.status_code == 201:
            return DeviceType(self._apiClient, r.json())
        else:
            raise ApiException(r)
Пример #9
0
    def getMgmt(self):
        r = self._apiClient.get('api/v0002/device/types/%s/devices/%s/mgmt' %
                                (self.typeId, self.deviceId))

        if r.status_code == 200:
            return r.json()
        if r.status_code == 404:
            # It's perfectly valid for a device to not have a location set, if this is the case, set response to None
            return None
        else:
            raise ApiException(r)
Пример #10
0
    def list(self):
        """
        List all device management extension packages
        """
        url = 'api/v0002/mgmt/custom/bundle'
        r = self._apiClient.get(url)

        if r.status_code == 200:
            return r.json()
        else:
            raise ApiException(r)
Пример #11
0
 def getStatus(self, requestId, typeId=None, deviceId=None):
     """
     Get a list of device management request device statuses.
     Get an individual device mangaement request device status.
     """
     if typeId is None or deviceId is None:
         url = MgmtRequests.mgmtRequestStatus % (requestId)
         r = self._apiClient.get(url)
         
         if r.status_code == 200:
             return r.json()
         else:
             raise ApiException(r)
     else:
         url = MgmtRequests.mgmtRequestSingleDeviceStatus % (requestId, typeId, deviceId)
         r = self._apiClient.get(url)
         
         if r.status_code == 200:
             return r.json()
         else:
             raise ApiException(r)
Пример #12
0
    def create(self, dmeData):
        """
        Create a new device management extension package
        In case of failure it throws APIException
        """
        url = 'api/v0002/mgmt/custom/bundle'
        r = self._apiClient.post(url, dmeData)

        if r.status_code == 201:
            return r.json()
        else:
            raise ApiException(r)
Пример #13
0
 def list(self):
     """
     Gets a list of device management requests, which can be in progress or recently completed.
     In case of failure it throws APIException
     """
     url = MgmtRequests.mgmtRequests
     r = self._apiClient.get(url)
     
     if r.status_code == 200:
         return r.json()
     else:
         raise ApiException(r)
Пример #14
0
 def initiate(self, request):
     """
     Initiates a device management request, such as reboot.
     In case of failure it throws APIException
     """
     url = MgmtRequests.mgmtRequests
     r = self._apiClient.post(url, request)
     
     if r.status_code == 202:
         return r.json()
     else:
         raise ApiException(r)
Пример #15
0
    def serviceStatus(self):
        """
        Retrieve the organization-specific status of each of the services offered by the IBM Watson IoT Platform.
        In case of failure it throws APIException
        """

        r = self._apiClient.get('api/v0002/service-status')

        if r.status_code == 200:
            return r.json()
        else:
            raise ApiException(r)
Пример #16
0
    def append(self, item=None, **kwargs):
        # Get all, convert list to iterator
        logsUrl = 'api/v0002/device/types/%s/devices/%s/diag/logs' % (
            self.typeId, self.deviceId)

        if item is None:
            item = DeviceLog(**kwargs)
        r = self._apiClient.post(logsUrl, item)
        if r.status_code == 201:
            return True
        else:
            raise ApiException(r)
Пример #17
0
    def get(self, bundleId):
        """
        Get a specific device management extension package
        It accepts bundleId (string) as parameters
        In case of failure it throws APIException
        """
        url = 'api/v0002/mgmt/custom/bundle/%s' % (bundleId)
        r = self._apiClient.get(url)

        if r.status_code == 200:
            return r.json()
        else:
            raise ApiException(r)
Пример #18
0
 def get(self, requestId):
     """
     Gets details of a device management request.
     It accepts requestId (string) as parameters
     In case of failure it throws APIException
     """
     url = MgmtRequests.mgmtSingleRequest % (requestId)
     r = self._apiClient.get(url)
     
     if r.status_code == 200:
         return r.json()
     else:
         raise ApiException(r)
Пример #19
0
    def __getitem__(self, key):
        """
        get a device type from the registry
        """
        url = 'api/v0002/device/types/%s' % (key)

        r = self._apiClient.get(url)
        if r.status_code == 200:
            return DeviceType(self._apiClient, r.json())
        elif r.status_code == 404:
            self.__missing__(key)
        else:
            raise ApiException(r)
Пример #20
0
    def update(self, bundleId, dmeData):
        """
        Update a device management extension package
        It accepts bundleId (string) as parameters
        In case of failure it throws APIException
        """
        url = 'api/v0002/mgmt/custom/bundle/%s' % (bundleId)
        r = self._apiClient.put(url, dmeData)

        if r.status_code == 200:
            return r.json()
        else:
            raise ApiException(r)
Пример #21
0
    def delete(self, bundleId):
        """
        Delete a device management extension package
        It accepts bundleId (string) as parameters
        In case of failure it throws APIException
        """
        url = 'api/v0002/mgmt/custom/bundle/%s' % (bundleId)
        r = self._apiClient.delete(url)

        if r.status_code == 204:
            return True
        else:
            raise ApiException(r)
Пример #22
0
    def getConnectionLogs(self):
        r = self._apiClient.get('api/v0002/logs/connection',
                                parameters={
                                    "typeId": self.typeId,
                                    "deviceId": self.deviceId
                                })

        if r.status_code == 200:
            responseList = []
            for entry in r.json():
                responseList.append(LogEntry(**entry))
            return responseList
        else:
            raise ApiException(r)
Пример #23
0
    def __getitem__(self, index):
        """
        Get a log entry
        """
        ecUrl = 'api/v0002/device/types/%s/devices/%s/diag/errorCodes' % (
            self.typeId, self.deviceId)

        r = self._apiClient.get(ecUrl)
        if r.status_code == 200:
            if index > len(r.json()):
                self.__missing__(index)
            return DeviceErrorCode(**r.json()[index])
        else:
            raise ApiException(r)
Пример #24
0
    def __contains__(self, key):
        """
        get a device type from the registry
        """

        url = 'api/v0002/device/types/%s' % (key)

        r = self._apiClient.get(url)
        if r.status_code == 200:
            return True
        elif r.status_code == 404:
            return False
        else:
            raise ApiException(r)
Пример #25
0
    def update(self, typeId, description=None, deviceInfo=None, metadata=None):
        devicetypeUrl = 'api/v0002/device/types/%s' % (typeId)

        data = {
            'description': description,
            'deviceInfo': deviceInfo,
            'metadata': metadata
        }

        r = self._apiClient.put(devicetypeUrl, data)
        if r.status_code == 200:
            return DeviceType(self._apiClient, r.json())
        else:
            raise ApiException(r)
Пример #26
0
    def __contains__(self, key):
        """
        Does a log exist?
        """
        logUrl = 'api/v0002/device/types/%s/devices/%s/diag/logs/%s' % (
            self.typeId, self.deviceId, key)

        r = self._apiClient.get(logUrl)
        if r.status_code == 200:
            return True
        elif r.status_code == 404:
            return False
        else:
            raise ApiException(r)
Пример #27
0
    def dataTransfer(self, start, end, detail=False):
        """
        Retrieve the organization-specific status of each of the services offered by the IBM Watson IoT Platform.
        In case of failure it throws APIException
        """

        r = self._apiClient.get(
            'api/v0002/usage/data-traffic?start=%s&end=%s&detail=%s' %
            (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'), detail))

        if r.status_code == 200:
            return DataTransferSummary(**r.json())
        else:
            raise ApiException(r)
Пример #28
0
 def delete(self, requestId):
     """
     Clears the status of a device management request.
     You can use this operation to clear the status for a completed request, or for an in-progress request which may never complete due to a problem.
     It accepts requestId (string) as parameters
     In case of failure it throws APIException
     """
     url = MgmtRequests.mgmtSingleRequest % (requestId)
     r = self._apiClient.delete(url)
     
     if r.status_code == 204:
         return True
     else:
         raise ApiException(r)
Пример #29
0
    def __delitem__(self, key):
        """
        Delete a device
        """
        if self.typeId is None:
            (classIdentifier, orgId, typeId, deviceId) = key.split(":")
            deviceUrl = 'api/v0002/device/types/%s/devices/%s' % (typeId, deviceId)
        else:
            deviceUrl = 'api/v0002/device/types/%s/devices/%s' % (self.typeId, key)

        r = self._apiClient.delete(deviceUrl)
        if r.status_code == 404:
            self.__missing__(key)
        elif r.status_code != 204:
            raise ApiException(r)
Пример #30
0
 def get(self, deviceUid, eventId):
     """
     Retrieves the last cached message for specified event from a specific device.
     """
     
     if not isinstance(deviceUid, DeviceUid) and isinstance(deviceUid, dict):
         deviceUid = DeviceUid(**deviceUid)
     
     url = 'api/v0002/device/types/%s/devices/%s/events/%s' % (deviceUid.typeId, deviceUid.deviceId, eventId)
     r = self._apiClient.get(url)
     
     if r.status_code == 200:
         return LastEvent(**r.json())
     else:
         raise ApiException(r)