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(_:)oraddGroupReference(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
objectsattribute 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.
See morestruct 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 } } } }Declaration
Swift
public class C8yAssetCollection : ObservableObject -
Encapsulates a c8y
C8yManagedObjectmanaged 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
See moregroupCategory,infoetc.Declaration
Swift
public struct C8yGroup : C8yObject -
Encapsulates a c8y
C8yManagedObjectmanaged object and treats it as a device exposing attributes and methods typically attributed to a device such asserialNumber,modelAlso includes a number of custom atributes to better categorise devices such as
See moredeviceCategory,attachmentsandrelayStateDeclaration
Swift
public struct C8yDevice : C8yObject -
Presents a
C8Devicedevice that can be observed for changed within in a SwiftUI View directly. Static device data is still available via the wrappeddeviceattribute. In addition it provides dynamic data such as gps position, battery level, measurements etc that can be observed for changesUse the
See moreEditableDeviceclass if you want to provide a view to allow a user to edit a device.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
onChangeand 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 methodC8yAssetCollection.saveObject(_)without having it called on each key press made by the user.
See moreVStack { ... }.onReceive(self.editableDevice.onChange) { editableDevice in do { try self.assetCollection.saveObject(editableDevice.toDevice()) { success, error in } } catch { print("error \(error.localizedDescription)") } }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
onChangeand 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 methodC8yAssetCollection.saveObject(_)without having it called on each key press made by the user.
See moreVStack { ... }.onReceive(self.editableGroup.onChange) { editableGroup in do { try self.assetCollection.saveObject(editableGroup.toGroup()) { success, error in } } catch { print("error \(error.localizedDescription)") } }Declaration
Swift
public class C8yEditableGroup : ObservableObject -
Wrapper to allow objects deviced from
C8yObjectto 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
See moreC8yAssetCollectionto allow bothC8yDeviceandC8yGroupobjects to managed together.Declaration
Swift
public struct AnyC8yObject : Identifiable, Equatable, Hashable -
Protocol identifying common features for all cumulocity assets managed via a
C8yManagedObjectCurrently only
See moreCyGroupandC8yDevicehave been definedDeclaration
Swift
public protocol C8yObject : Equatable -
Undocumented
See moreDeclaration
Swift
public class C8yNetworks : ObservableObject
View on GitHub
High Level Access Reference