public Boolean AddIfDoesNotExist(BlinkStick led) { Boolean newRecord = true; foreach (BlinkStickDeviceSettings current in Devices) { if (current.Serial == led.Serial) { current.Touched = true; if (current.Led == null) { current.Led = led; current.Led.OpenDevice(); } newRecord = false; } } if (newRecord) { BlinkStickDeviceSettings settings = new BlinkStickDeviceSettings(led); settings.Touched = true; Devices.Add(settings); led.OpenDevice(); } return(newRecord); }
protected void OnPatternSend(int channel, int firstLed, int lastLed, BlinkStickDeviceSettings device, Pattern pattern, int repeat, int duration) { if (PatternSend != null) { PatternSend(this, new PatternSendEventArgs(channel, firstLed, lastLed, device, pattern, repeat, duration)); } }
public PatternSendEventArgs(int channel, int firstLed, int lastLed, BlinkStickDeviceSettings device, Pattern pattern, int repeat, int duration) { this.Channel = channel; this.FirstLed = firstLed; this.LastLed = lastLed; this.Device = device; this.Pattern = pattern; this.Repeat = repeat; this.Duration = duration; }
public ColorSendEventArgs(int channel, int firstLed, int lastLed, byte r, byte g, byte b, BlinkStickDeviceSettings device) { this.R = r; this.G = g; this.B = b; this.Channel = channel; this.FirstLed = firstLed; this.LastLed = lastLed; this.Device = device; }
public TriggeredEvent(CustomNotification notification, int channel, int firstLed, int lastLed, BlinkStickDeviceSettings device, Pattern pattern, int repeat, int duration) { this.Channel = channel; this.TimeStamp = DateTime.Now; this.Notification = notification; this.NotificationSnapshot = notification.Copy(); this.FirstLed = firstLed; this.LastLed = lastLed; this.Device = device; this.Pattern = pattern; this.Repeat = repeat; this.Duration = duration; }
public TriggeredEvent(PatternNotification notification, String message) { this.TimeStamp = DateTime.Now; this.Notification = notification; this.NotificationSnapshot = ((CustomNotification)notification).Copy(); this.Message = message; this.FirstLed = notification.LedFirstIndex; this.LastLed = notification.LedLastIndex; this.Device = notification.Device; this.Pattern = notification.Pattern; this.Repeat = 1; this.Duration = 0; }
private void NotificationTriggered(object sender, TriggeredEventArgs e) { log.InfoFormat("Notification [{0}] \"{1}\" triggered. Message: {2}", (sender as CustomNotification).GetTypeName(), (sender as CustomNotification).Name, e.Message); PatternNotification notification = sender as PatternNotification; TriggeredEvent ev = new TriggeredEvent(notification, e.Message); if (e.Message != "") { DataModel.TriggeredEvents.Add(ev); } if (notification.Pattern == null) { log.WarnFormat("({0}) Pattern is not assigned", notification.Name); return; } BlinkStickDeviceSettings settings = DataModel.FindBySerial(notification.BlinkStickSerial); if (settings == null) { log.WarnFormat("({0}) BlinkStick with serial {1} not known", notification.Name, notification.BlinkStickSerial); return; } if (settings.Led == null) { log.WarnFormat("({0}) BlinkStick with serial {1} is not connected", notification.Name, notification.BlinkStickSerial); return; } Boolean found = false; lock (settings.EventQueue) { foreach (TriggeredEvent pendingEvent in settings.EventQueue.ToArray()) { if (pendingEvent.Notification == notification) { found = true; break; } } if (found) { log.InfoFormat("Notification {0} already pending to play. Skipping.", notification.Name); } else { settings.EventQueue.Enqueue(ev); } } if (!found) { settings.Start(); } }
public override void Start() { if (Running) { return; } log.InfoFormat("Starting {0}", GetTypeName()); base.Start(); server = new RemoteControlServer(this.ApiBindAddress, this.ApiAccessPort); server.RequestReceived += (object sender, RequestReceivedEventArgs e) => { if (e.Context.Request.Url.AbsolutePath == "/api/v1/test") { server.SendResponseJson(200, new InfoResponse() { name = "BlinkStick Client", version = ApplicationDataModel.ApplicationVersion, versionFull = ApplicationDataModel.ApplicationFullVersion }, e.Context.Response); e.Handled = true; } RouteEnum route = RouteEnum.Unrecognized; Match m = SetColorRegex.Match(e.Context.Request.Url.AbsolutePath); if (m.Success) { route = RouteEnum.SetColor; } else { m = PatternRegex.Match(e.Context.Request.Url.AbsolutePath); if (m.Success) { route = RouteEnum.PlayPattern; } } BlinkStickDeviceSettings ledSettings = null; if (route != RouteEnum.Unrecognized) { String serial = m.Groups[1].ToString(); ledSettings = DataModel.FindBySerial(serial); if (ledSettings == null) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("BlinkStick with serial number {0} not found", serial) }, e.Context.Response); e.Handled = true; return; } else if (ledSettings.Led == null) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("BlinkStick with serial number {0} not connected", serial) }, e.Context.Response); e.Handled = true; return; } } switch (route) { case RouteEnum.SetColor: ProcessSetColorRequest(e, ledSettings); break; case RouteEnum.PlayPattern: ProcessPlayPatternRequest(e, ledSettings); break; default: break; } }; server.Start(); log.DebugFormat("{0} started", GetTypeName()); }
private void ProcessPlayPatternRequest(RequestReceivedEventArgs e, BlinkStickDeviceSettings ledSettings) { Pattern pattern = new Pattern(); int channel = 0; int ledStart = 0; int ledEnd = 0; for (int i = 0; i < e.Context.Request.QueryString.AllKeys.Length; i++) { string key = e.Context.Request.QueryString.AllKeys[i].ToLower(); string value = e.Context.Request.QueryString.GetValues(i)[0]; if (key == "channel") { try { channel = Convert.ToInt32(value); if (channel < 0 || channel > 2) { throw new Exception("not within range of 0..2"); } } catch (Exception ex) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Invalid channel parameter: {0}", ex.Message) }, e.Context.Response); e.Handled = true; return; } } else if (key == "firstled") { try { ledStart = Convert.ToInt32(value); if (ledStart < 0 || ledStart > 63) { throw new Exception("not within range of 0..63"); } } catch (Exception ex) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Invalid ledStart parameter: {0}", ex.Message) }, e.Context.Response); e.Handled = true; return; } } else if (key == "lastled") { try { ledEnd = Convert.ToInt32(value); if (ledEnd < 0 || ledEnd > 63) { throw new Exception("not within range of 0..63"); } } catch (Exception ex) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Invalid ledEnd parameter: {0}", ex.Message) }, e.Context.Response); e.Handled = true; return; } } else if (key == "pattern") { pattern = DataModel.FindPatternByName(value); if (pattern == null) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Pattern {0} not found", value) }, e.Context.Response); e.Handled = true; return; } } } if (pattern == null) { server.SendResponseJson(422, new ErrorResponse() { error = "Missing pattern parameter" }, e.Context.Response); e.Handled = true; return; } try { OnPatternSend(channel, (byte)ledStart, (byte)ledEnd, ledSettings, pattern, 1, 0); } catch (Exception ex) { log.ErrorFormat("Failed to send color {0}", ex); } server.SendResponseJson(200, null, e.Context.Response); e.Handled = true; }
private void ProcessSetColorRequest(RequestReceivedEventArgs e, BlinkStickDeviceSettings ledSettings) { RgbColor color = RgbColor.Black(); int channel = 0; int firstLed = 0; int lastLed = 0; for (int i = 0; i < e.Context.Request.QueryString.AllKeys.Length; i++) { string key = e.Context.Request.QueryString.AllKeys[i].ToLower(); string value = e.Context.Request.QueryString.GetValues(i)[0]; if (key == "channel") { try { channel = Convert.ToInt32(value); if (channel < 0 || channel > 2) { throw new Exception("not within range of 0..2"); } } catch (Exception ex) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Invalid channel parameter: {0}", ex.Message) }, e.Context.Response); e.Handled = true; return; } } else if (key == "firstled") { try { firstLed = Convert.ToInt32(value); if (firstLed < 0 || firstLed > 63) { throw new Exception("not within range of 0..63"); } } catch (Exception ex) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Invalid ledStart parameter: {0}", ex.Message) }, e.Context.Response); e.Handled = true; return; } } else if (key == "lastled") { try { lastLed = Convert.ToInt32(value); if (lastLed < 0 || lastLed > 63) { throw new Exception("not within range of 0..63"); } } catch (Exception ex) { server.SendResponseJson(422, new ErrorResponse() { error = String.Format("Invalid ledEnd parameter: {0}", ex.Message) }, e.Context.Response); e.Handled = true; return; } } else if (key == "color") { try { color = RgbColor.FromString(value); } catch { try { color = RgbColor.FromString("#" + value); } catch { server.SendResponseJson(422, new ErrorResponse() { error = "Invalid color parameter" }, e.Context.Response); e.Handled = true; return; } } } } try { Pattern pattern = new Pattern(); pattern.Animations.Add(new Animation()); pattern.Animations[0].AnimationType = AnimationTypeEnum.SetColor; pattern.Animations[0].DelaySetColor = 0; pattern.Animations[0].Color = color; OnPatternSend(channel, (byte)firstLed, (byte)lastLed, ledSettings, pattern, 1, 0); } catch (Exception ex) { log.ErrorFormat("Failed to send color {0}", ex); } server.SendResponseJson(200, null, e.Context.Response); e.Handled = true; }
protected void OnColorSend(int channel, int firstLed, int lastLed, byte r, byte g, byte b, BlinkStickDeviceSettings settings) { if (ColorSend != null) { ColorSend(this, new ColorSendEventArgs(channel, firstLed, lastLed, r, g, b, settings)); } }