private void event_color(SocketIOClient.Messages.IMessage message) { Debug.WriteLine("\nevent_color:\n" + message.MessageText); EventJson <ColorData> received = JsonConvert.DeserializeObject <EventJson <ColorData> >(message.Json.ToJsonString()); //Validate if (!received.Validate()) { return; } ColorData data = received.args[0].data; ColorTriggered?.Invoke(this, data); }
private void ParseSensorMessage(byte[] data) { if (data[3] == 0x3b && (Type == Types.POWERED_UP_REMOTE || this.GetType() == typeof(RemoteHub))) { // Voltage (PUP Remote) data = PadMessage(data, 6); BatteryVoltage = ((double)BitConverter.ToUInt16(data, 4) / 500); return; } else if (data[3] == 0x3c) { // Voltage data = PadMessage(data, 6); BatteryVoltage = ((double)BitConverter.ToUInt16(data, 4) / 400); double batteryRaw = (double)BitConverter.ToUInt16(data, 4); return; } Port port = GetPortFromPortNumber(data[3]); if (port != null && port.Connected) { switch (port.Device) { case Port.Devices.WEDO2_DISTANCE: { int distance = data[4]; if (data.Length > 5 && data[5] == 1) { distance = data[4] + 255; } if (MainBoard.showColorDebug) { MainBoard.WriteLine($"{Name} - Distance Received: " + distance + " last triggers was " + (Environment.TickCount - port.LastDistanceTick)); } port.LatestDistance = (int)distance; if (Environment.TickCount - port.LastDistanceTick > port.DistanceColorCooldownMs) { DistanceTriggered?.Invoke(this, port, (int)distance); OnDataUpdated(); } break; } case Port.Devices.BOOST_DISTANCE: { if (data.Length >= 8) { double distance = data[5] * 1.5f; double partial = data[7]; double dis2 = distance; if (partial > 0) { dis2 += (1.0 / partial); } port.LatestDistance = (int)distance; // Trigger a distance event if at least 2 seconds have passed if (Environment.TickCount - port.LastDistanceTick > port.DistanceColorCooldownMs) { DistanceTriggered?.Invoke(this, port, (int)distance); } // Should we show any debugs? if (MainBoard.showColorDebug) { MainBoard.WriteLine($"{Name} - Distance: " + distance + " Color: " + data[4] + "(" + (Colors)data[4] + ") -> Partial: " + partial, (partial > 5) ? Color.Green : Color.Black); } // We don't trigger colors unless they are close enough if (partial > 5) { Colors currentColor = (Colors)data[4]; port.LatestColor = currentColor; // Did at least 2 seconds passed if the color is the same as before? if (Environment.TickCount - port.LastColorTick > port.DistanceColorCooldownMs) { ColorTriggered?.Invoke(this, port, currentColor); DataUpdated(); } } } else { MainBoard.WriteLine("ERROR: Issue with Color Packet. It was too short!!", Color.Red); } break; } case Devices.POWERED_UP_REMOTE_BUTTON: { switch (data[4]) { case 0x01: { MainBoard.WriteLine("button " + port.Id + " - BUTTON_PLUS was pressed"); OnRemoteTriggered(this, port, RemoteButtons.BUTTON_PLUS); break; } case 0xff: { MainBoard.WriteLine("button " + port.Id + " - BUTTON_MINUS was pressed"); OnRemoteTriggered(this, port, RemoteButtons.BUTTON_MINUS); break; } case 0x7f: { MainBoard.WriteLine("button " + port.Id + " - BUTTON_STOP was pressed"); OnRemoteTriggered(this, port, RemoteButtons.BUTTON_STOP); break; } case 0x00: { MainBoard.WriteLine("button " + port.Id + " - ButtonState.RELEASED"); OnRemoteTriggered(this, port, RemoteButtons.BUTTON_RELEASED); break; } } break; } } } else { MainBoard.WriteLine($"ERROR: {Name} - Port could not be found or is not connected", Color.Red); } }
internal void OnColorTriggered(Hub hub, Port p, Colors color) { ColorTriggered?.Invoke(hub, p, color); }