public void PacketReceived(Packet packet) { Device device = null; if (devices.ContainsKey(packet.DeviceId)) { device = devices[packet.DeviceId]; } else { Console.WriteLine("NOTICE: New sensor: " + packet.DeviceId + ""); device = new Device(packet.DeviceId, true); devices.Add(packet.DeviceId, device); } device.ParentAddress = packet.ParentAddress; device.AddSamples(packet.Samples); }
public void NewPacket(Packet packet) { foreach (ushort v in packet.Pir) { const float weight = 0.99f; const float latch = 50.0f; average = weight * average + (1.0f - weight) * v; if (count == 0) { average = v; } count++; float diff = Math.Abs(average - v); bool newSignal = (diff > latch); //Console.WriteLine("MOTION: #" + Id + " = " + v + " - " + average + " = " + diff + " (" + latch + ") == " + newSignal + ""); if (newSignal && !signal && count > 48) { count = 1; // reset try { if (Task.ToLower().EndsWith(".wav")) { SoundPlayer simpleSound = new SoundPlayer(Task); simpleSound.Play(); } else { Console.Error.WriteLine("WARNING: Unhandled task: " + Task); } } catch (Exception e) { Console.Error.WriteLine("ERROR: Problem running task: " + e.Message); } } signal = newSignal; } }
private Packet Receive() { Packet packet = null; if (socket == null || socketReader == null) { return null; } try // [dgj] { packet = new Packet(); packet.command = socketReader.ReadLine(); // MESSAGE, ERROR or RECEIPT //return if command =~ /\A\s*\Z/ // [dgj] Fix if (packet.command == "") { packet.command = socketReader.ReadLine(); } string line; while ((line = socketReader.ReadLine()) != "") { if (line == null) { return null; } string[] split = line.Split(new char[] { ':' }, 2); packet.headers[split[0]] = split.Length > 1 ? split[1] : null; } StringBuilder body = new StringBuilder(); int nextChar; while ((nextChar = socketReader.Read()) != 0) { body.Append((char)nextChar); } packet.body = body.ToString().TrimEnd('\r', '\n'); //Console.Out.WriteLine(packet); return packet; } catch (ObjectDisposedException e) { Console.Error.WriteLine(e.Message); return null; } catch (IOException e) { Console.Error.WriteLine(e.Message); return null; } }
private void TeddiPacketReceived(Packet packet) { if (logStream != null) { logStream.WriteLine(packet.ToString()); } lock (manager) { manager.PacketReceived(packet); } if (motionRun != null) { if (motionRun.ContainsKey(packet.DeviceId)) { MotionTracker motionTracker = motionRun[packet.DeviceId]; motionTracker.NewPacket(packet); } } SendOscPacket(packet); if (graphPanel.Visible) { graphPanel.Invalidate(); } if (mapPanel.Visible) { mapPanel.Invalidate(); } }
public void SendOscPacket(Packet packet) { if (oscTransmitter != null) { OscBundle oscBundle = Packet.PacketToOscBundle(packet, oscTopic, oscCooked); if (oscBundle != null) { oscTransmitter.Send(oscBundle); } } }
/* public static string PacketToStomp(Packet packet) { Hashtable sourceData = new Hashtable(); object[] samples = new object[packet.Samples.Length]; for (int i = 0; i < packet.Samples.Length; i++) { Sample sample = packet.Samples[i]; object[] sampleArray = new object[5]; sampleArray[0] = sample.Timestamp.ToBinary(); sampleArray[1] = sample.Index; sampleArray[2] = sample[0]; sampleArray[3] = sample[1]; sampleArray[4] = sample[2]; samples[i] = sampleArray; } sourceData.Add("Timestamp", packet.Timestamp.ToBinary()); sourceData.Add("DeviceId", packet.DeviceId); sourceData.Add("Version", packet.Version); sourceData.Add("Battery", packet.Battery); sourceData.Add("AdcSample", packet.AdcSample); sourceData.Add("SequenceId", packet.SequenceId); sourceData.Add("Format", packet.Format); sourceData.Add("SampleCount", packet.SampleCount); sourceData.Add("Samples", samples); string json = Json.JsonEncode(sourceData); return json; } */ /* public static Packet PacketFromOscBundle(OscBundle oscBundle, string topic) { List<Sample> sampleData = new List<Sample>(); ushort deviceId = 0xffff; ulong timestamp = oscBundle.Timestamp; foreach (OscData oscData in oscBundle.Parts) { OscMessage oscMessage = oscData as OscMessage; if (oscMessage == null) { continue; } string t = (topic == null ? "/teddi" : topic); if (oscMessage.Address != t) { continue; } if (oscMessage.Arguments.Length < 5) { continue; } uint index; short x, y, z; deviceId = (ushort)(int)oscMessage.Arguments[0]; index = (uint)(int)oscMessage.Arguments[1]; x = (short)((float)oscMessage.Arguments[2] * 256.0f); y = (short)((float)oscMessage.Arguments[3] * 256.0f); z = (short)((float)oscMessage.Arguments[4] * 256.0f); Sample waxSample = new Sample(OscMessage.DateTimeFromTimestamp(timestamp), index, x, y, z); Console.WriteLine(waxSample.ToString()); sampleData.Add(waxSample); } byte version = 0; byte battery = 0; ushort adcSample = 0; ushort sequenceId = 0xffff; // TODO: Fix this to something sensible byte format = 0; byte sampleCount = (byte)sampleData.Count; return new Packet(OscMessage.DateTimeFromTimestamp(timestamp), deviceId, version, battery, adcSample, sequenceId, format, sampleCount, sampleData.ToArray()); } */ public static OscBundle PacketToOscBundle(Packet packet, string topic, bool cooked) { if (topic == null || topic.Length == 0) { topic = cooked ? "/teddi-cooked/@" : "/teddi/@"; } Sample[] samples = packet.Samples; OscMessage[] messages = new OscMessage[samples.Length]; for (int i = 0; i < samples.Length; i++) { string t = topic; t = t.Replace("@", "" + packet.DeviceId); ushort rawPir = (ushort)samples[i].V; ushort rawAudio = (ushort)samples[i].X; if (cooked) { float temp = Sample.ConvertC((ushort)packet.Temp); float light = Sample.ConvertLux((ushort)packet.Light); float battery = Sample.ConvertV((ushort)packet.Battery); float humidity = Sample.ConvertPercentage((ushort)packet.Humidity); float pir = Sample.ConvertV(rawPir); float audio = Sample.ConvertV(rawAudio); messages[i] = new OscMessage(t, packet.DeviceId, temp, light, battery, humidity, pir, audio, samples[i].Index); } else { messages[i] = new OscMessage(t, packet.DeviceId, packet.Temp, packet.Light, packet.Battery, packet.Humidity, rawPir, rawAudio, samples[i].Index); } } OscBundle bundle = new OscBundle(OscBundle.TIMESTAMP_NOW, messages); return bundle; }
// Method to return a Packet, or null if invalid byte array public static Packet PacketFromBinary(byte[] buffer, DateTime timestamp) { Packet packet = null; if (buffer != null && buffer.Length > 0) { if (buffer.Length >= 5 && buffer[0] == 0x12 && buffer[1] == 0x54) // USER_REPORT_TYPE && TEDDI (ASCII 'T') { /* unsigned char reportType; // @0 [1] = 0x12 (USER_REPORT_TYPE) unsigned char reportId; // @1 [1] = 0x54 (ASCII 'T') unsigned short deviceId; // @2 [2] = Short device identifier (16-bit) [doesn't have to be part of the payload, but this format is the same as the WAX] unsigned char version; // @4 [1] = (0x02 = format [seq/temp/ldr/audio/pir : short]) */ ushort deviceId = (ushort)(buffer[2] | (((ushort)buffer[3]) << 8)); byte version = buffer[4]; if (((version & 0x0f) == 0x03 || (version & 0x0f) >= 0x04) && buffer.Length >= 18) { /* unsigned char reportType; // @ 0 [1] USER_REPORT_TYPE (0x12) unsigned char reportId; // @ 1 [1] Report identifier (0x54, ASCII 'T') unsigned short deviceId; // @ 2 [2] Device identifier (16-bit) unsigned char version; // @ 4 [1] Low nibble = packet version (0x3), high nibble = config (0x0) unsigned char sampleCount; // @ 5 [1] Sample count (default config is at 250 msec interval with an equal number of PIR and audio samples; 20 = 5 seconds) unsigned short sequence; // @ 6 [2] Sequence number (16-bit) unsigned short unsent; // @ 8 [2] Number of unsent samples (default config is in 250 msec units) unsigned short temp; // @10 [2] Temperature (0.2 Hz) unsigned short light; // @12 [2] Light (0.2 Hz) unsigned short battery; // @14 [2] Battery (0.2 Hz) unsigned short humidity; // @16 [2] Humidity [V4] (0.2 Hz) -- or [V3] 16-bit checksum to make packet zero-sum unsigned char data[BITPACK10_SIZEOF(DATA_MAX_INTERVAL * 2)]; // @18 [50] PIR and audio energy (4 Hz, 20x 2x 10-bit samples) unsigned short parentAddress; // @ADDITIONAL_OFFSET+0 [2] (optional) Parent address unsigned short parentAltAddress; // @ADDITIONAL_OFFSET+2 [2] (optional) Parent alt. address */ byte config = (byte)(buffer[4] >> 4); byte sampleCount = (byte)(buffer[5]); ushort sequence = (ushort)(buffer[6] | (((ushort)buffer[7]) << 8)); ushort unsent = (ushort)(buffer[8] | (((ushort)buffer[9]) << 8)); ushort temp = (ushort)(buffer[10] | (((ushort)buffer[11]) << 8)); ushort light = (ushort)(buffer[12] | (((ushort)buffer[13]) << 8)); ushort battery = (ushort)(buffer[14] | (((ushort)buffer[15]) << 8)); ushort humidity = (ushort)(buffer[16] | (((ushort)buffer[17]) << 8)); ushort[] pir = new ushort[sampleCount]; ushort[] audio = new ushort[sampleCount]; ushort parentAddress = 0xffff; ushort parentAltAddress = 0xffff; if ((version & 0x0f) == 0x03) { humidity = 0; } int[] teddiFrequency = new int[] { 4, 8, 16, 32, 64, 128, 256, 512, 1, 1, 1, 1, 1, 1, 1, 2 }; ushort sampleInterval = (ushort)(1000 / teddiFrequency[config]); try { // Unpack PIR for (int i = 0; i < sampleCount; i++) { pir[i] = BitUnpack_uint10(buffer, i, 18); } // Unpack Audio for (int i = 0; i < sampleCount; i++) { audio[i] = BitUnpack_uint10(buffer, sampleCount + i, 18); } // For V3 data, divide temp/light/battery measurement down if ((version & 0x0f) <= 0x03 && sampleCount > 0) { temp /= sampleCount; light /= sampleCount; battery /= sampleCount; humidity /= sampleCount; } // Check for additional data int additionalIndex = 18 + BITPACK10_SIZEOF(sampleCount * 2); if (additionalIndex + 4 <= buffer.Length) { parentAddress = (ushort)(buffer[additionalIndex + 0] | (((ushort)buffer[additionalIndex + 1]) << 8)); parentAltAddress = (ushort)(buffer[additionalIndex + 2] | (((ushort)buffer[additionalIndex + 3]) << 8)); Console.WriteLine("SENSOR: #" + deviceId + ", parent " + parentAddress + " (" + parentAltAddress + " = #" + Device.RouterAltId(parentAltAddress) + ")."); } // Create samples from the measurement data: deviceId, temp, light, humidity, pir[], audio[] Sample[] sampleData = new Sample[sampleCount]; for (uint i = 0; i < sampleCount; i++) { sampleData[i] = new Sample(timestamp - TimeSpan.FromMilliseconds((unsent + sampleCount - 1 - i) * sampleInterval), (ushort)(sequence * sampleCount) + i, (short)pir[i], (short)temp, (short)light, (short)humidity, audio[i], battery); } packet = new Packet(timestamp - TimeSpan.FromMilliseconds((unsent + sampleCount - 1) * sampleInterval), deviceId, 1, 0, 0, (ushort)(sequence * sampleCount), 0, sampleCount, timestamp, unsent, temp, light, humidity, pir, audio, sampleData, parentAddress, parentAltAddress); } catch (IndexOutOfRangeException ex) { Console.Error.Write("EXCEPTION: " + ex + " while parsing packet."); } } else { Console.Error.Write("[T?]"); // Unknown TEDDI packet type } } else { //Console.Error.Write("[?]"); // Unknown packet type } } return packet; }