public void LoadResources() { if (Directory.Exists(RESOURCES_CACHE)) { foreach (string directory in Directory.GetDirectories(RESOURCES_CACHE)) { string id = Path.GetFileName(directory); int objectID; if (int.TryParse(id, out objectID)) { LWM2MResources resources = null; switch (objectID) { case 0: resources = _SecurityResources; break; case 1: resources = _ServerResources; break; case 3: resources = new DeviceResources(); break; case 4: resources = new ConnectivityMonitoringResources(); break; case 5: resources = new FirmwareUpdateResources(); break; case 6: resources = new LocationResources(); break; case 7: resources = new ConnectivityStatisticsResources(); break; case 15: resources = new DeviceCapabilityResources(); break; case 20000: resources = new FlowObjectResources(); break; case 20001: resources = new FlowAccessResources(); break; case 20005: resources = new FlowCommandResources(); EventHandler<ChildCreatedEventArgs> handler = (s, e) => { FlowCommandResource flowCommandResource = e.Resource as FlowCommandResource; if (flowCommandResource != null) { flowCommandResource.Updated += new EventHandler(FlowCommand_Updated); FlowCommand_Updated(flowCommandResource, null); } }; resources.ChildCreated += handler; break; default: break; } if (resources != null) { foreach (string fileName in Directory.GetFiles(directory, "*.tlv")) { LWM2MResource resource = null; switch (objectID) { case 0: resource = new SecurityResource(Path.GetFileNameWithoutExtension(fileName)); break; case 1: resource = new ServerResource(Path.GetFileNameWithoutExtension(fileName)); break; case 3: resource = new DeviceResource(); break; case 4: resource = new ConnectivityMonitoringResource(); break; case 5: resource = new FirmwareUpdateResource(); break; case 6: resource = new LocationResource(); break; case 7: resource = new ConnectivityStatisticsResource(); break; case 15: resource = new DeviceCapabilityResource(Path.GetFileNameWithoutExtension(fileName)); break; case 20000: resource = new FlowObjectResource(); resource.Updated += new EventHandler(FlowObject_Updated); break; case 20001: resource = new FlowAccessResource(); break; case 20005: resource = new FlowCommandResource(Path.GetFileNameWithoutExtension(fileName)); resource.Updated += new EventHandler(FlowCommand_Updated); break; default: break; } if (resource != null) { using(Stream stream = File.OpenRead(fileName)) { TlvReader reader = new TlvReader(stream); if (resource.Deserialise(reader)) resources.Add(resource); } if (objectID == 0) { SecurityResource securityResource = resource as SecurityResource; if (securityResource.SecurityMode == TSecurityMode.PreSharedKey) { UsePSK(System.Text.Encoding.UTF8.GetString(securityResource.ClientPublicKey), StringUtils.HexString(securityResource.SecretKey)); } } else if (objectID == 20000) { FlowObjectResource flowObjectResource = resource as FlowObjectResource; if (flowObjectResource != null) flowObjectResource.TenantHash = null; } } } this.AddResources(resources); } } } } else LoadDefaultResources(); }
public void LoadDefaultResources() { DeviceResources deviceResources = new DeviceResources(); IntegerResources availablePowerSources = new IntegerResources(string.Empty); availablePowerSources.Add(new IntegerResource("0") { Value = 1 }); availablePowerSources.Add(new IntegerResource("1") { Value = 5 }); IntegerResources powerSourceVoltage = new IntegerResources(string.Empty); powerSourceVoltage.Add(new IntegerResource("0") { Value = 3800 }); powerSourceVoltage.Add(new IntegerResource("1") { Value = 5000 }); IntegerResources powerSourceCurrent = new IntegerResources(string.Empty); powerSourceCurrent.Add(new IntegerResource("0") { Value = 3800 }); powerSourceCurrent.Add(new IntegerResource("1") { Value = 5000 }); IntegerResources errorCode = new IntegerResources(string.Empty); errorCode.Add(new IntegerResource("0") { Value = 0 }); deviceResources.Add(new DeviceResource() { Manufacturer = "Open Mobile Alliance", ModelNumber = "Lightweight M2M Client", SerialNumber = "345000123", FirmwareVersion = "1.0", AvailablePowerSources = availablePowerSources, PowerSourceVoltages = powerSourceVoltage, PowerSourceCurrents = powerSourceCurrent, BatteryLevel = 100, MemoryFree = 15, ErrorCodes = errorCode, CurrentTime = DateTime.UtcNow, UTCOffset = "+12:00", SupportedBindingandModes = "U" }); ConnectivityMonitoringResources connectivityMonitoringResources = new ConnectivityMonitoringResources(); IntegerResources availableNetworkBearer = new IntegerResources(string.Empty); availableNetworkBearer.Add(new IntegerResource("0") { Value = 0 }); StringResources ipAddresses = new StringResources(string.Empty); ipAddresses.Add(new StringResource("0") { Value = "192.168.148.18" }); StringResources routerIPAddresse = new StringResources(string.Empty); routerIPAddresse.Add(new StringResource("0") { Value = "192.168.148.1" }); StringResources apn = new StringResources(string.Empty); apn.Add(new StringResource("0") { Value = "internet" }); connectivityMonitoringResources.Add(new ConnectivityMonitoringResource() { NetworkBearer = 0, AvailableNetworkBearers = availableNetworkBearer, RadioSignalStrength = 92, LinkQuality = 2, IPAddresses = ipAddresses, RouterIPAddresses = routerIPAddresse, LinkUtilization = 5, APNs = apn }); this.AddResources(deviceResources); this.AddResources(connectivityMonitoringResources); this.AddResources(new FirmwareUpdateResources()); FlowObjectResources flowObjectResources = new FlowObjectResources(); FlowObjectResource flowObjectResource = new FlowObjectResource() { DeviceType = "EVOKE Flow", TenantID = 1 }; flowObjectResource.Updated += new EventHandler(FlowObject_Updated); flowObjectResources.Add(flowObjectResource); this.AddResources(flowObjectResources); this.AddResources(new FlowAccessResources()); FlowCommandResources flowCommandResources = new FlowCommandResources(); EventHandler<ChildCreatedEventArgs> handler = (s, e) => { FlowCommandResource flowCommandResource = e.Resource as FlowCommandResource; if (flowCommandResource != null) { flowCommandResource.Updated += new EventHandler(FlowCommand_Updated); FlowCommand_Updated(flowCommandResource, null); } }; flowCommandResources.ChildCreated += handler; this.AddResources(flowCommandResources); LocationResources locationResources = new LocationResources(); locationResources.Add(new LocationResource() { Latitude = "-41.0", Longitude = "174.0", Altitude = "150", Uncertainty = "1.0", Velocity = new byte[] { 0, 45, 0, 0 }, Timestamp = DateTime.UtcNow }); this.AddResources(locationResources); DeviceCapabilityResources deviceCapabilityResources = new DeviceCapabilityResources(); deviceCapabilityResources.Add(new DeviceCapabilityResource("0") { Attached = true, Enabled = true, Group = 0, Description = "Temp", Property = "2;3" }); deviceCapabilityResources.Add(new DeviceCapabilityResource("1") { Attached = true, Enabled = true, Group = 1, Description = "Control", Property = "1;2" }); deviceCapabilityResources.Add(new DeviceCapabilityResource("2") { Attached = true, Enabled = false, Group = 2, Description = "Bluetooth", Property = "0" }); deviceCapabilityResources.Add(new DeviceCapabilityResource("3") { Attached = false, Enabled = false, Group = 2, Description = "WiFi", Property = "2" }); this.AddResources(deviceCapabilityResources); }