High Level Access

Convenience classes that abstract out the complexity of the c8y API to provide easy access to your groups and devices. Of course you have less flexibility in using these classes, but you use these in conjuction with the c8y API services too.

  • Class to allow you to manage an abritary collection of groups or devices from the users device e.g.

    let myCollection = C8yAssetCollection()
    
    do {
        try myCollection.load(conn, c8yReferencesToLoad: ["9403", "9102", "2323"], includeSubGroups: true)
            .receive(on: RunLoop.main)
            .sink(receiveCompletion: { (completion) in
    
                    print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> SINK LOAD ENDED")
                }, receiveValue: { (objects) in
    
                    // both devices and group, which can also be referenced via myColletion.objects, which in turn is a Published attribute and hence can
                    // be referenced directly from a swiftui View if myCollection is defined either as a @StateObject or @ObservedObject
    
                    let devicesAndGroup: [AnyC8yObject] = objects
    
                   ...
            })
    } catch {
        print ("load failed \(error.localizedDescription)")
    }
    
    

    Devices and groups can be added to the collection using either the add(_:) or addGroupReference(c8yId:includeSubGroups:completionHandler:) methods e.g.

    myCollection.add(device)
    myCollection.addGroupReference(c8yId: "4343", includeSubgroups: true) { (group, error) in
    
        if (group != nil) {
            // success, group represents the managed object that has been fetched from c8y and added to your collection
            ...
        } else {
            // lookup failed, probably due to invalid reference, refer to error object for precision
            ...
    }
    

    You can reference the devices and groups via the objects attribute directly from your SwiftUI views. Ensure that you prefix the reference to you collection attribute with @ObservedObject or @StateObject to ensure that the view automatically updates if your collection changes e.g.

    struct MyAssetsView: View {
    
        @Binding var conn: C8yCumulocityConnection
        @Binding var referenecs: [String]
    
        @StateObject var myCollection:C8yAssetCollection = C8yAssetCollection()
        @State var isLoading: Bool = false
    
        var body some View {
            VStack {
                ForEach(myCollection.objects) { r in
                    Text("asset is \(r.name)")
                }
            }.onAppear {
                myCollection.load(self.conn, c8yReferencesToLoad: self.references, includeSubGroups: true)
                    .sink(receiveCompletion: { (completion) in
                        self.isLoading = false
                }
            }
        }
    }
    
    See more

    Declaration

    Swift

    public class C8yAssetCollection : ObservableObject
  • Encapsulates a c8y C8yManagedObject managed object and treats it as a group exposing attributes and methods typically attributed to managing a group.

    Also includes a number of custom atributes to better categorise devices such as groupCategory, info etc.

    See more

    Declaration

    Swift

    public struct C8yGroup : C8yObject
  • Encapsulates a c8y C8yManagedObject managed object and treats it as a device exposing attributes and methods typically attributed to a device such as serialNumber, model

    Also includes a number of custom atributes to better categorise devices such as deviceCategory, attachments and relayState

    See more

    Declaration

    Swift

    public struct C8yDevice : C8yObject
  • Presents a C8Device device that can be observed for changed within in a SwiftUI View directly. Static device data is still available via the wrapped device attribute. In addition it provides dynamic data such as gps position, battery level, measurements etc that can be observed for changes

    Use the EditableDevice class if you want to provide a view to allow a user to edit a device.

    See more

    Declaration

    Swift

    public class C8yMutableDevice : ObservableObject
  • Use this class directly from a SwiftUI Form View to allow the wrapped device to be edited.

    Changes to fields are published to the attribute onChange and can be acted on in your View with the following code. Duplicates are removed and changes are debounced into 1 event every 3 seconds, this means you could automatically persist changes to Cumulocity via the method C8yAssetCollection.saveObject(_) without having it called on each key press made by the user.

    VStack {
        ...
    }.onReceive(self.editableDevice.onChange) { editableDevice in
    
        do {
            try self.assetCollection.saveObject(editableDevice.toDevice()) { success, error in
    
            }
        } catch {
            print("error \(error.localizedDescription)")
        }
    }
    
    See more

    Declaration

    Swift

    public class C8yEditableDevice : ObservableObject, Equatable
  • Use this class directly from a SwiftUI Form View to allow the wrapped group to be edited.

    Changes to fields are published to the attribute onChange and can be acted on in your View with the following code. Duplicates are removed and changes are debounced into 1 event every 3 seconds, this means you could automatically persist changes to Cumulocity via the method C8yAssetCollection.saveObject(_) without having it called on each key press made by the user.

    VStack {
        ...
    }.onReceive(self.editableGroup.onChange) { editableGroup in
    
        do {
            try self.assetCollection.saveObject(editableGroup.toGroup()) { success, error in
    
            }
        } catch {
            print("error \(error.localizedDescription)")
        }
    }
    
    See more

    Declaration

    Swift

    public class C8yEditableGroup : ObservableObject
  • Wrapper to allow objects deviced from C8yObject to managed as a collection. Swift does not allow Arrays or Dictionaries to reference protocol types, hence why this is needed.

    This class is used by C8yAssetCollection to allow both C8yDevice and C8yGroup objects to managed together.

    See more

    Declaration

    Swift

    public struct AnyC8yObject : Identifiable, Equatable, Hashable
  • Protocol identifying common features for all cumulocity assets managed via a C8yManagedObject

    Currently only CyGroup and C8yDevice have been defined

    See more

    Declaration

    Swift

    public protocol C8yObject : Equatable
  • Undocumented

    See more

    Declaration

    Swift

    public class C8yNetworks : ObservableObject