public async Task InitAsync() { try { _logger.LogInformation("Waiting for connecting..."); LoggerX.WriteEventLog("Waiting for connecting..."); if (!string.IsNullOrWhiteSpace(IotGateway.Secret)) { _logger.LogInformation($"Registering gateway {IotGateway.GatewayId}"); LoggerX.WriteEventLog($"Registering gateway {IotGateway.GatewayId}"); var gw = new Gateway.API.Abstractions.Gateway { GatewayId = IotGateway.GatewayId, Secret = IotGateway.Secret, Make = "Innotrack", Model = "Vodaworld-RNC", FirmwareVersion = typeof(Gateway1Simulation).Assembly.GetName().Version.ToString(), Devices = new Dictionary <string, Device>() }; gw.Devices = await IotGateway.GetIotDevices(); // gw.Devices.Add("RFID|1:ZONE|ZONE1", new Device() { DeviceName = "RFID|1:ZONE|ZONE1", DeviceType = "ZONE", Properties = new Dictionary<string, DeviceProperty>() { { "TAGS", new DeviceProperty() { PropertyName = "TAGS", DataType = "RFIDDictionary" } } } }); await DapiContext.ExecuteAsync(username : IotGateway.UserName, action : () => _gatewayApi.RegisterGatewayFromGatewayAsync(gw)); if (!string.IsNullOrWhiteSpace(IotGateway.ClientId)) { _logger.LogInformation($"Associating gateway {IotGateway.GatewayId} with client {IotGateway.ClientId}"); LoggerX.WriteEventLog($"Associating gateway {IotGateway.GatewayId} with client {IotGateway.ClientId}"); await DapiContext.ExecuteAsync(username : "******", action : () => _entityApi.AssociateGatewayAndClientAsync(IotGateway.GatewayId, IotGateway.ClientId)); } } connected = true; _logger.LogInformation($"Simulating gateway {IotGateway.GatewayId} started..."); LoggerX.WriteEventLog($"Simulating gateway {IotGateway.GatewayId} started..."); StartIntegration(); } catch (Exception ex) { connected = false; _logger.LogError(ex, "Error initialization simulator"); } }
private async Task RunAsync() { try { var gw = new Gateway.API.Abstractions.Gateway { GatewayId = Program.GatewayId, Secret = Program.SecretKey, Make = "IoT.nxt", Model = "PC Gateway", FirmwareVersion = "1.0.0", Devices = new Dictionary <string, Device> { ["PC"] = new Device { DeviceName = "PC", DeviceType = "Personal Computer", Properties = new Dictionary <string, DeviceProperty> { ["MachineName"] = new DeviceProperty { PropertyName = "MachineName" }, ["ProcessorCount"] = new DeviceProperty { PropertyName = "ProcessorCount" }, ["OSVersion"] = new DeviceProperty { PropertyName = "OSVersion" }, ["ProcessCount"] = new DeviceProperty { PropertyName = "ProcessCount" }, ["CursorPosition"] = new DeviceProperty { PropertyName = "CursorPosition" }, ["TotalMemoryGB"] = new DeviceProperty { PropertyName = "TotalMemoryGB" }, ["SecondsIdle"] = new DeviceProperty { PropertyName = "SecondsIdle" }, ["CPUUsage"] = new DeviceProperty { PropertyName = "CPUUsage" }, ["AvailableRAMGB"] = new DeviceProperty { PropertyName = "AvailableRAMGB" }, ["Execute"] = new DeviceProperty { PropertyName = "Execute" } } }, ["Apps"] = new Device { DeviceName = "Apps", DeviceType = "Applications", Properties = new Dictionary <string, DeviceProperty> { ["SkypeOpen"] = new DeviceProperty { PropertyName = "SkypeOpen" }, ["Notepads"] = new DeviceProperty { PropertyName = "Notepads" }, ["Chromes"] = new DeviceProperty { PropertyName = "Chromes" }, ["VisualStudios"] = new DeviceProperty { PropertyName = "VisualStudios" }, } } } }; await _gatewayApi.RegisterGatewayFromGatewayAsync(gw); } catch (Exception ex) { _logger.LogError(ex, "Error initialization example gateway"); } string tenantId = "t000000019"; string lastCursorPosition = null; var intervalMs = 1000; var lastCursorMove = DateTime.Now; await _redq.SubscribeAsync($"GATEWAY.1.*.{Program.GatewayId}.REQ", "requests", null, queueName : Program.GatewayId, createQueue : true, process : async(q, jo, a, b, c, headers) => { var command = jo["deviceGroups"]["PC"].Value <string>("Execute"); var parts = command.Split("|"); var path = parts.Length > 0 ? parts[0] : ""; var arguments = parts.Length > 1 ? parts[1] : ""; try { new Process { StartInfo = { FileName = path, Arguments = arguments } }.Start(); //Echo the command to confirm execution await _redq.SendGateway1NotificationAsync( tenantId, Program.GatewayId, DateTime.UtcNow, null, null, DateTime.UtcNow, true, false, ("PC", "Execute", command) ); } catch (Exception ex) { _logger.LogError(ex, $"Processing command: {command}"); } return(true); }); while (true) { try { var processes = Process.GetProcesses(); var machineName = Environment.MachineName; var processorCount = Environment.ProcessorCount; var osVersion = Environment.OSVersion; var processCount = processes.Length; var skypeOpen = processes.Any(p => p.ProcessName.ContainsAnyOfNoCase("Skype")); var notepadsOpen = processes.Count(p => p.ProcessName.ContainsAnyOfNoCase("Notepad")); var chromesOpen = processes.Count(p => p.ProcessName.ContainsAnyOfNoCase("Chrome")); var visualStudiosOpen = processes.Count(p => p.ProcessName.ContainsAnyOfNoCase("devenv")); var cursorPosition = $"{Cursor.Position.X},{Cursor.Position.Y}"; var cpuUsage = _cpuCounter.NextValue(); var avalableRamGb = _ramCounter.NextValue() / 1024.0; var computerInfo = new ComputerInfo(); var totalMemoryGb = computerInfo.TotalPhysicalMemory / (1024 * 1024 * 1024); if (cursorPosition != lastCursorPosition) { lastCursorPosition = cursorPosition; lastCursorMove = DateTime.Now; } var lastActivity = lastCursorMove > Program.LastKeystroke ? lastCursorMove : Program.LastKeystroke; var secondsIdle = (DateTime.Now - lastActivity).TotalSeconds; await _redq.SendGateway1NotificationAsync( tenantId, Program.GatewayId, DateTime.UtcNow, null, null, DateTime.UtcNow, true, false, ("PC", "MachineName", machineName), ("PC", "ProcessorCount", processorCount), ("PC", "OSVersion", osVersion.ToString()), ("PC", "ProcessCount", processCount), ("PC", "CursorPosition", cursorPosition), ("PC", "TotalMemoryGB", totalMemoryGb), ("PC", "SecondsIdle", secondsIdle), ("PC", "CPUUsage", cpuUsage), ("PC", "AvailableRAMGB", avalableRamGb), ("Apps", "SkypeOpen", skypeOpen), ("Apps", "Notepads", notepadsOpen), ("Apps", "Chromes", chromesOpen), ("Apps", "VisualStudios", visualStudiosOpen) ); } catch (Exception ex) { _logger.LogError(ex, "Error sending telemetry"); } await Task.Delay(intervalMs); } }
private async Task RunAsync() { //We register a gateway to send data back into the IoT.nxt platform try { var gw = new Gateway.API.Abstractions.Gateway { GatewayId = Program.GatewayId, Secret = Program.SecretKey, Make = "IoT.nxt", Model = "Custom Function Results", FirmwareVersion = "1.0.0", Devices = new Dictionary <string, Device> { ["FUNCTIONRESULTS"] = new Device { DeviceName = "FUNCTIONRESULTS", DeviceType = "FUNCTIONRESULTS", Properties = new Dictionary <string, DeviceProperty> { ["FUNCTIONARESULT"] = new DeviceProperty { PropertyName = "FUNCTIONARESULT" } } } } }; await _gatewayApi.RegisterGatewayFromGatewayAsync(gw); } catch (Exception ex) { _logger.LogError(ex, "Error initialization example function gateway"); } //You can listen to the values from any endpoint (or endpoints) using subscribe. //You have to subscribe to the entity's PublishRoutingKey to gets its values. await _redq.SubscribeAsync("ENDPOINT.1.7fe0f7fc-ecb1-497e-a371-17fc53986afe.NFY", "Functions", null, queueName : "CustomEndpointFunctions." + Program.GatewayId, createQueue : true, process : async(r, msg, a, b, c, d) => { //Get the endpoint's value from the packet. var inputvalue = msg["endPoints"].Value <int>("ec957061-cb78-4483-ae5b-b8010aeac2d1"); //Calculate our output value var outputvalue = "Input was: " + inputvalue; //Send the result back as a new endpoint try { await _redq.SendGateway1NotificationAsync( "T000000019", Program.GatewayId, DateTime.UtcNow, null, null, DateTime.UtcNow, true, false, ("FUNCTIONRESULTS", "FUNCTIONARESULT", outputvalue)); } catch (Exception ex) { _logger.LogError(ex, "Error sending result"); } return(true); }); }