C8yManagedObjectsService

public class C8yManagedObjectsService : JcConnectionRequest<C8yCumulocityConnection>

Principal access point for all Cumulocity data represented as ManagedObjects such as devices and groups and implemented through the API endpoint \/inventory/managedObjects.

Use this class to fetch, create, update and delete assets in c8y

Refer to the c8y API documentation for more information

  • Undocumented

    See more

    Declaration

    Swift

    public enum ManagedObjectNotFoundError : Error
  • Used when fetching C8yManagedObjects to determines the maximum number allowed in a single request, default is 50

    Notes:

    Managed Objects are grouped into pages via the C8yPagedManagedObjects class, successive pages can be fetched by invoking the appropriate get method with the specified page number. The C8yPagedManagedObjects references the current page, size and total via the property statistics, which is defined by C8yPageStatistics.

    Declaration

    Swift

    public var pageSize: Int
  • Fetch the managed object C8yManagedObject using the cumulocity internal id

    # Notes: #

    The id is only known by c8y, you will need to first fetch a list of managed objects or reference the managed object via an external id.

    # Example: #

       C8yPagedManagedObjectsService(conn).get(pageNum: 0) { (response) in
    
          if (response.status == .SUCCESS) {
              print("\(String(describing: response.content!.owner))")
          }
       }
    

    Declaration

    Swift

    public func get(_ id: String) -> AnyPublisher<JcRequestResponse<C8yManagedObject>, APIError>
  • Fetch the managed object C8yManagedObject using an external id

    # Notes: # The object associated with the given external id, i.e. assigned via method post(object:withExternalId:ofType:). The values will show under the identity tab of the given device in c8y Device Management

    Declaration

    Swift

    public func get(forExternalId: String, ofType: String, deviceOnly: Bool = false) -> AnyPublisher<JcRequestResponse<C8yManagedObject>, APIError>
  • Returns all managed objects in c8y restricted to the given page with the page size specified by the pageSize property of your C8yPagedManagedObjectsService instance, default is 50 items per page.

    # Notes: #

    Invoke this method for successive page whilst incrementing the pageNum You will get a empty list once you go past the last page.

    # Example #

       let service = C8yPagedManagedObjectsService(conn)
       service.pageSize = 10
    
       service.get(pageNum: 0) { (response) in
    
          if (response.status == .SUCCESS) {
    
              print("page \(response.content!.statistics.currentPage) of \(response.content!.statistics.totalPages), size \(response.content!.statistics.pageSize)")
    
              for object in response.content!.objects {
                  print("\(String(describing: object.id))")
              }
          }
       }
    

    Declaration

    Swift

    public func get(pageNum: Int) -> AnyPublisher<JcRequestResponse<C8yPagedManagedObjects>, APIError>

    Return Value

    Publisher for resulting page C8yPagedManagedObjects of C8yManagedObject objects, if any encapsulated in JcRequestResponse wrapper defining result

  • Returns all managed objects in c8y restricted for the given type and page number with the page size specified by the *pageSize*
    property of your `C8yManagedObjectService` instance, default is 50 items per page
    

    Notes:

    Invoke this method for successive page whilst incrementing the pageNum
    You will get a empty list once you go past the last page.
    
    - parameter forType Identifies the type of managed objects to be fetched e.g. c8y_Device or c8y_DeviceGroup
    

    Declaration

    Swift

    public func get(forType type: String, pageNum: Int) -> AnyPublisher<JcRequestResponse<C8yPagedManagedObjects>, APIError>
  • Allows managed objects to be fetched based on set of query parameters C8yManagedObjectQuery and grouped into pages via C8yPagedManagedObjects

    # Notes: #

      All queries must apply (ANDED) if more than query is specified.
    

    # Example: #

       let query = C8yManagedObjectQuery()
                  .add("type", C8yManagedObjectQuery.Operator.eq, "c8y_DeviceGroup")
                  .add("bygroupid", nil, "123456")
    
       let service = C8yPagedManagedObjectsService(conn).get(forQuery: query, pageNum: 0) { (response) in
    
          if (response.status == .SUCCESS) {
    
              print("page \(response.content!.statistics.currentPage) of \(response.content!.statistics.totalPages), size \(response.content!.statistics.pageSize)")
    
              for object in response.content!.objects {
                  print("\(String(describing: object.id))")
              }
          }
       }
    

    Declaration

    Swift

    public func get(forQuery: C8yManagedObjectQuery, pageNum: Int) -> AnyPublisher<JcRequestResponse<C8yPagedManagedObjects>, APIError>
  • Adds the new managed object to your cumulocity tenant.
    The internal id generated by Cumulocity is included in the updated object returned by the Publisher
    
    # Notes: #
    
    Not all elements of your managed object can be posted, refer to the *REST API Guide* - (https://cumulocity.com/guides/reference/inventory/#managed-object)
    for more details
    
    - parameter object a `ManagedObject` created locally for which the id attribute will be null
    - returns Publisher indicating success/failure and an updated `C8yManagedObject` including the  internal id attributed by cumulocity.
    - throws Error if the object is missing required fields
    

    See also

    seeAlso get(forExternalId:ofType:)

    Declaration

    Swift

    public func post(_ object: C8yManagedObject) throws -> AnyPublisher<JcRequestResponse<C8yManagedObject>, APIError>
  • Adds the new managed object to your cumulocity tenant, incuding a reference to the external id provided here. The internal id generated by Cumulocity is included in the updated object returned by the Publisher.

    # Notes: #

    You can in turn fetch the managed object using the external id
    
    Not all elements of your managed object can be updated in c8y, refer to the [REST API Guide](https://cumulocity.com/guides/reference/inventory/#managed-object)
    for more details
    

    Throws

    throws Error if the object is missing required fields

    Requires

    requires valid ManagedObject reference without id

    See also

    seeAlso get(forExternalId:ofType:)

    Declaration

    Swift

    public func post(_ object: C8yManagedObject, withExternalId externalId: String, ofType type: String) throws -> AnyPublisher<JcRequestResponse<C8yManagedObject>, APIError>
  • Updates the managed object in your cumulocity tenant. You do not have to specify all atributes in your C8yManagedObject only those that have changed. Use one of the C8yManagedObject constructors to updated speficific properties such as response Interval, notes or other properties.

    # Notes: #

    Not all elements of your managed object can be posted, refer to the REST API Guide - (https://cumulocity.com/guides/reference/inventory/#managed-object) for more details

    Throws

    throws Error if the object is missing c8y id

    Declaration

    Swift

    public func put(_ object: C8yManagedObject) throws -> AnyPublisher<JcRequestResponse<C8yManagedObject>, APIError>
  • Ensures a existing C8yManagedObject can be retrieved with the given external id

    Declaration

    Swift

    public func register(externalId: String, ofType type: String, forId id: String) throws -> AnyPublisher<JcRequestResponse<String>, APIError>
  • Retrieves the list of external id’s associated for the C8yManagedObject for the given c8y internal id

    Notes:

    Define an external id for your managed object at creation time using the method post(object:withExternalId:ofType:) Alternatively you can register as many external id’s as you want after the object has been created using register(externalId:ofType:forId:)

    Declaration

    Swift

    public func externalIDsForManagedObject(_ id: String) -> AnyPublisher<JcRequestResponse<C8yExternalIds>, APIError>
  • Associates the given managed object with the identified group. This is most often used to add a device ‘c8y_Device’ to a group ‘c8y_DeviceGroup’

    Declaration

    Swift

    public func assignToGroup(child: String, parentId: String) -> AnyPublisher<JcRequestResponse<Bool>, APIError>
  • Deletes the given managed object

    Declaration

    Swift

    public func delete(id: String) -> AnyPublisher<JcRequestResponse<Bool>, APIError>