public BlynkController(IConfiguration config) { this.deviceIDs = this.PopulateDevicesFromConfig(config); this.SetupPinMappingFromConfig(config); var token = config["blynk:token"]; if (token == null) { throw new ArgumentException("Blynk API token is not configured"); } var serverAddress = config["blynk:server-address"] ?? "blynk-cloud.com"; var serverPort = 8442; if (int.TryParse(config["blynk:server-port"], out var value)) { serverPort = value; } this.blynk = new BlynkLibrary.Blynk(token, serverAddress, serverPort); this.blynk.VirtualPinReceived += this.BlynkVirtualPinReceived; this.blynk.Connect(); }
private async void BlynkVirtualPinReceived(BlynkLibrary.Blynk b, BlynkLibrary.VirtualPinEventArgs e) { this.log.LogDebug($"Virtual pin received: {e.Data.Pin}={e.Data.Value[0].ToString()}"); if (!int.TryParse(e.Data.Value[0].ToString(), out int value)) { this.log.LogWarning("Non-integer value received, ignoring"); return; } var pin = e.Data.Pin; if (pin == this.pinConfig.DeviceSelector) { this.SelectDevice(value - 1); } else if (pin == this.pinConfig.SwitchDevice && value == 1) { SelectNextDevice(); } else { foreach (var p in typeof(PinConfiguration).GetProperties()) { if (!int.TryParse(p.GetValue(this.pinConfig).ToString(), out int result)) { continue; } if (result != pin) { continue; } var pinAttributes = p.GetCustomAttributes(typeof(PinAttribute), false); if (pinAttributes.Count() == 0) { continue; } var attribute = pinAttributes.First() as PinAttribute; // Read-only pin is used for updating gauges and labels if (attribute.IsReadOnly) { break; } int binaryValue = value > 0 ? 1 : 0; await this.SetDeviceParameter( attribute.DeviceParamName, attribute.IsBinary?binaryValue : (value - attribute.ValueOffset)); break; } } }
public BlynkController(IConfiguration config) { this.deviceIDs = this.PopulateDevicesFromConfig(config); this.SetupPinMappingFromConfig(config); var token = config["blynk:token"]; if (token == null) { throw new ArgumentException("Blynk API token is not configured"); } this.blynk = new BlynkLibrary.Blynk(token, "blynk-cloud.com", 8442); this.blynk.VirtualPinReceived += this.BlynkVirtualPinReceived; this.blynk.Connect(); }