예제 #1
0
 public void UpdateData(ZomBDataLookup data)
 {
     while (communists.Count>0)
     {
         communists.Dequeue().Foreground = Brushes.Black;
     }
     var ks = from d in data where d.Key.StartsWith("dbg-") select new { Name = d.Key.Substring(4), Data = d.Value };
     foreach (var item in ks)
     {
         if (!elms.ContainsKey(item.Name))
         {
             elms.Add(item.Name, new TextBlock());
             var lbl = new TextBlock();
             lbl.HorizontalAlignment = HorizontalAlignment.Right;
             lbl.Text = item.Name+": ";
             Children.Add(lbl);
             Children.Add(elms[item.Name]);
         }
         if (elms[item.Name].Text != item.Data)
         {
             elms[item.Name].Foreground = Brushes.Red;
             communists.Enqueue(elms[item.Name]);
         }
         elms[item.Name].Text = item.Data;
     }
 }
        /// <summary>
        /// The background worker. Will exit after 10 consectutive errors
        /// </summary>
        private void TickleClientWorker()
        {
            //TCP==tickle
            //Person[] ticklishPeople = People.GetTicklish();
            //foreach (Person poorSoul in ticklishPeople)
            //{
            //    poorSoul.Apply(Objects.Get("Feather"), Person.Location.Foot);
            //}

            //number of errors
            int nume = 0;
            //config
            bool implicitSend = false;
            bool longNames = false;
            NetworkStream zb = null;
            while (isrunning)
            {
                try
                {
                    cRIOConnection.Connect(IPAddress, Port);
                    zb = cRIOConnection.GetStream();
                    break;
                }
                catch (SocketException)
                {
                    //Nohbdy is around
                }
                catch (ThreadAbortException)
                {
                    isrunning = false;
                    return;
                }
                catch (Exception ex)
                {
                    nume++;
                    DoError(ex);
                    if (nume > 10)
                    {
                        isrunning = false;
                        DoError(new Exception("10 consecutive errors were encountered, stopping DashboardDataHub"));
                        isrunning = false;
                        return;
                    }
                }
            }
            try
            {
                //config
                int last;
                do
                {
                    last = zb.ReadByte();
                    switch (last)
                    {

                        case 0x45:
                            implicitSend = false;
                            break;
                        case 0x49:
                            implicitSend = true;
                            break;
                        case 0x4C:
                            longNames = true;
                            break;
                        case -1:
                            return;
                        default:
                            break;
                    }
                }
                while (last != 0x00);
                //end config
                int namel, valuel;
                while (isrunning)
                {
                    try
                    {
                        last = zb.ReadByte();
                        //Fire! on 0
                        if (last == 0x00 || implicitSend)
                        {
                            if (kys.Count > 0)
                            {
                                //fire events
                                if (DataRecieved != null)
                                    DataRecieved(this, new EventArgs());
                                if (NewDataRecieved != null)
                                    NewDataRecieved(this, new NewDataRecievedEventArgs(kys));
                                kys = new ZomBDataLookup();
                            }
                            if (last == 0x00)
                                continue;
                        }
                        if (last == -1)
                            return;//stream closed

                        //last byte was name lenght
                        namel = last;

                        //Read the value
                        if (longNames)
                        {
                            valuel = (zb.ReadByte() << 8) + zb.ReadByte();
                        }
                        else
                        {
                            valuel = zb.ReadByte();
                        }
                        if (valuel < 0)
                            return;//stream closed

                        //Make the buffers
                        byte[] buf = new byte[namel], vbuf = new byte[valuel];

                        //Read the name
                        while (cRIOConnection.Available < namel)
                            Thread.Sleep(1);
                        zb.Read(buf, 0, namel);

                        //and the value
                        while (cRIOConnection.Available < valuel)
                            Thread.Sleep(1);
                        zb.Read(vbuf, 0, valuel);

                        string nom = Encoding.UTF8.GetString(buf), val = Encoding.UTF8.GetString(vbuf);
                        //if peeking, send
                        if (peeking)
                        {
                            dp.Invoke(cb, nom);
                        }
                        else
                        {
                            //otherwise
                            //Add the value
                            if (kys.ContainsKey(nom))
                                kys[nom] = new ZomBDataObject(val, ZomBDataTypeHint.Unknown);
                            else
                                kys.Add(nom, new ZomBDataObject(val, ZomBDataTypeHint.Unknown));
                        }

                        //Decrease error
                        if (nume > 0)
                            nume--;
                    }
                    catch (ThreadAbortException)
                    {
                        isrunning = false;
                        return;
                    }
                    catch (Exception ex)
                    {
                        nume++;
                        DoError(ex);
                        if (nume > 10)
                        {
                            isrunning = false;
                            DoError(new Exception("10 consecutive errors were encountered, stopping DashboardDataHub"));
                            isrunning = false;
                            return;
                        }
                    }
                }
            }
            catch { }//Don't let em leak!
            finally
            {
                if (zb != null)
                    zb.Dispose();
                try
                {
                    cRIOConnection.Close();
                }
                catch { }
                cRIOConnection = null;
            }
        }
 void IZomBMonitor.UpdateData(ZomBDataLookup data)
 {
     //Nothing important happens here
 }
 private void AddLookupData(string prefix, ZomBDataLookup datal)
 {
     if (saving)
     {
         lock (dataq)
         {
             var scl = new StringCollection();
             foreach (var item in datal)
             {
                 if (item.Value.Value is ZomBDataLookup)
                 {
                     AddLookupData(prefix + item.Key + ".", item.Value.Value as ZomBDataLookup);
                     continue;
                 }
                 if (!header.Contains(prefix + item.Key))
                 {
                     header.Add(prefix + item.Key);
                 }
                 int hidx = header.IndexOf(prefix + item.Key);
                 while (scl.Count <= hidx)
                 {
                     scl.Add(null);
                 }
                 scl[hidx] = item.Value.ToString();
             }
             if (dataq.Count > 0 && GetTime() - lasttime < 50)//less than 50ms
             {
                 var mmine = new string[scl.Count];
                 scl.CopyTo(mmine, 0);
                 if (Mergable(ref scl))
                 {
                     dataq[dataq.Count - 1] = scl;
                 }
                 else
                 {
                     scl = new StringCollection();
                     scl.AddRange(mmine);
                     dataq.Add(scl);
                 }
             }
             else
             {
                 dataq.Add(scl);
                 lasttime = GetTime();
             }
         }
     }
 }
 /// <summary>
 /// IZomBMonitor: Updates the data
 /// </summary>
 /// <param name="data">The new data</param>
 public void UpdateData(ZomBDataLookup data)
 {
     if (!saving)
     {
         Start();
     }
     try
     {
         AddLookupData("", data);
         if (dataq.Count > 100 || lasttime - savetime > 10000)
             WriteBuffer();
     }
     catch { }
 }
 /// <summary>
 /// Convert the name=value|n=v form to a Dictionary of name and values
 /// </summary>
 /// <param name="Output"></param>
 /// <returns></returns>
 private ZomBDataLookup SplitParams(string Output)
 {
     //Split the main string
     string[] s = Output.Split(':');
     ZomBDataLookup k = new ZomBDataLookup(1);
     string ky, val;
     ky = s[0];
     val = s[1].Split('\r','\n','\b','#')[0];
     k[ky] = new ZomBDataObject(val, ZomBDataTypeHint.Unknown);
     return k;
 }
        /// <summary>
        /// The background worker. Will exit after 10 consectutive errors
        /// </summary>
        private void DoWork()
        {
            //number of errors
            int nume = 0;
            killer.Open();
            while (isrunning)
            {
                try
                {
                    string line = killer.ReadLine();
                    if (line[0] != ' ' && line[0] != ';' && line[0] != '/' && line[0] != '#')
                    {
                        //Get the items in a dictionary
                        try
                        {
                            ZomBDataLookup vals = SplitParams(line);
                            if (peeking)
                            {
                                foreach (var keys in vals)
                                {
                                    dp.Invoke(cb, keys.Key);
                                }
                            }
                            else
                            {
                                kys = vals;

                                //Fire events
                                if (DataRecieved != null)
                                    DataRecieved(this, new EventArgs());
                                if (NewDataRecieved != null)
                                    NewDataRecieved(this, new NewDataRecievedEventArgs(vals));
                            }
                        }
                        catch { }
                    }
                    if (nume > 0)
                        nume--;
                }
                catch (ThreadAbortException)
                {
                    isrunning = false;
                    killer.Close();
                    return;
                }
                catch (Exception ex)
                {
                    nume++;
                    DoError(ex);
                    if (nume > 10)
                    {
                        isrunning = false;
                        killer.Close();
                        DoError(new Exception("10 consecutive errors were encountered, stopping DashboardDataHub"));
                        isrunning = false;
                        return;
                    }
                }
            }
        }
 private void worker()
 {
     char c;
     ushort us;
     int iv;
     while (Running)
     {
         try
         {
             c = (char)redbit.ReadByte();
             switch (c)
             {
                 case 'T':
                     iv = (((int)redbit.ReadByte()) << 8) + redbit.ReadByte();
                     while (Running && iv > 1)//takes some time to read the next one
                     {
                         Thread.Sleep(2);
                         iv -= 2;
                     }
                     break;
                 case 'S':
                     sts = new FRCDSStatus();
                     sts.Status = new StatusBitField(redbit.ReadByte());
                     sts.Error = new ErrorBitField(redbit.ReadByte());
                     sts.DigitalIn = new DIOBitField(redbit.ReadByte());
                     sts.DigitalOut = new DIOBitField(redbit.ReadByte());
                     sts.PacketNumber = (ushort)((((int)redbit.ReadByte()) << 8) + redbit.ReadByte());
                     sts.Battery = redbit.ReadByte() + (((int)redbit.ReadByte()) / 100);
                     if (NewStatusRecieved != null)
                         NewStatusRecieved(this, new NewStatusRecievedEventArgs(sts));
                     break;
                 case 'D':
                     iv = (((int)redbit.ReadByte()) << 8) + redbit.ReadByte();
                     byte[] buffer = new byte[iv];
                     us = 0;
                     us = (ushort)redbit.Read(buffer, 0, iv);
                     while (us != iv)
                     {
                         us += (ushort)redbit.Read(buffer, us, iv - us);
                     }
                     vls = SplitParams(UTF8Encoding.UTF8.GetString(buffer));
                     if (DataRecieved != null)
                         DataRecieved(this, new EventArgs());
                     if (NewDataRecieved != null)
                         NewDataRecieved(this, new NewDataRecievedEventArgs(vls));
                     break;
                 case 'Q':
                     return;
                 default:
                     if (OnError != null)
                         OnError(this, new ErrorEventArgs(new Exception("missed command statements")));
                     break;
             }
         }
         catch (Exception ex)
         {
             if (OnError != null)
                 OnError(this, new ErrorEventArgs(ex));
         }
     }
 }
 /// <summary>
 /// Convert the name=value|n=v form to a Dictionary of name and values
 /// </summary>
 /// <param name="Output"></param>
 /// <returns></returns>
 private ZomBDataLookup SplitParams(string Output)
 {
     //Split the main string
     string[] s = Output.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
     ZomBDataLookup k = new ZomBDataLookup(s.Length);
     foreach (string item in s)
     {
         //split and add each item to the Dictionary
         string ky, val;
         ky = item.Split('=')[0];
         val = item.Split('=')[1];
         k[ky] = new ZomBDataObject(val, ZomBDataTypeHint.Unknown);//Latter will overwrite
     }
     return k;
 }
        /// <summary>
        /// The background worker. Will exit after 10 consectutive errors
        /// </summary>
        private void DoWork()
        {
            //number of errors
            int nume = 0;
            while (isrunning)
            {
                try
                {
                    IPEndPoint RIPend = null;
                    //Recieve the data
                    while (cRIOConnection.Available < 1018 && isrunning) { Thread.Sleep(2); }
                    if (!isrunning)
                    {
                        cRIOConnection.Close();
                        return;
                    }
                    byte[] buffer = cRIOConnection.Receive(ref RIPend);

                    //Check first
                    if (!VerifyPacket(buffer))
                    {
                        if (InvalidPacketRecieved != null && !peeking)
                        {
                            //Create our e
                            InvalidPacketRecievedEventArgs e = new InvalidPacketRecievedEventArgs(buffer, ddh.InvalidPacketAction == InvalidPacketActions.AlwaysContinue || ddh.InvalidPacketAction == InvalidPacketActions.Continue);
                            InvalidPacketRecieved(this, e);
                            if ((int)ddh.InvalidPacketAction < 3)//1-4
                            {
                                if (!e.ContinueAnyway)
                                    break;
                            }
                            else if (ddh.InvalidPacketAction == InvalidPacketActions.AlwaysIgnore)
                                break;
                        }
                    }

                    //DS Status
                    cStat = ParseDSBytes(buffer);

                    //Get a stream
                    MemoryStream Output = new MemoryStream(buffer);
                    Output.Seek(27, SeekOrigin.Begin);
                    EBinaryReader binladen = new EBinaryReader(Output);

                    //get length
                    int totallength = (binladen.ReadUInt16() << 16);
                    totallength += binladen.ReadUInt16();

                    //clear last loop's controls
                    kys = new ZomBDataLookup();

                    //loop all controls
                    while (totallength > (Output.Position - 27))
                    {
                        byte b = binladen.ReadByte();
                        switch (b)
                        {
                            case 0://Announce
                                {
                                    int id = binladen.ReadByte();
                                    SmartDataTypes type = (SmartDataTypes)binladen.ReadByte();
                                    int len = binladen.ReadUInt16();
                                    byte[] name = new byte[len];
                                    binladen.Read(name, 0, len);
                                    if (peeking)
                                    {
                                        dp.Invoke(cb, Encoding.UTF8.GetString(name));
                                        nametable[id] = new SmartInfo { Name = Encoding.UTF8.GetString(name), Type = type };
                                    }
                                    else
                                    {
                                        nametable[id] = new SmartInfo { Name = Encoding.UTF8.GetString(name), Type = type };
                                    }
                                    break;
                                }
                            case 1://Update
                                {
                                    int id = binladen.ReadByte();
                                    if (nametable.ContainsKey(id))
                                    {
                                        var info = nametable[id];
                                        string value = info.Parse(binladen);
                                        kys[info.Name] = new ZomBDataObject(value, info.ZomBTypeHint);
                                    }
                                    else
                                    {
                                        b = binladen.ReadByte();//get length
                                        binladen.Read(new byte[b], 0, b);//skip it
                                    }
                                    break;
                                }
                            case 2://GUI Announce, not impl
                                {
                                    System.Windows.Forms.MessageBox.Show("Dashboard selection via Protocol has not been implemented in ZomB, try WPF tabs instead");
                                    break;
                                }
                            default:
                                throw new Exception("Bin ("+b+") not a 1 or zero, exiting");
                        }
                    }

                    if (kys.Count > 0)//Empty keys in empty packet, "Smart" Dashboard can be dumb
                        //All in favor of chaging the name to DumbDashboard say "aye!" "sqrt(-1)!"
                    {
                        //Fire events
                        if (DataRecieved != null)
                            DataRecieved(this, new EventArgs());
                        if (NewStatusRecieved != null)
                            NewStatusRecieved(this, new NewStatusRecievedEventArgs(cStat));
                        if (NewDataRecieved != null)
                            NewDataRecieved(this, new NewDataRecievedEventArgs(kys));
                    }

                    if (nume > 0)
                        nume--;
                }
                catch (ThreadAbortException)
                {
                    isrunning = false;
                    cRIOConnection.Close();
                    return;
                }
                catch (Exception ex)
                {
                    nume++;
                    DoError(ex);
                    if (nume > 10)
                    {
                        isrunning = false;
                        DoError(new Exception("10 consecutive errors were encountered, stopping Smart"));
                        isrunning = false;
                        try
                        {
                            cRIOConnection.Close();
                        }
                        catch { }
                        return;
                    }
                }
            }
        }
예제 #11
0
 /// <summary>
 /// IZomBMonitor: Updates the data
 /// </summary>
 /// <param name="data">The new data</param>
 public void UpdateData(ZomBDataLookup data)
 {
     //TODO: Test
     if (saving)
     {
         StringBuilder sb = new StringBuilder();
         sb.Append("D00");//hog space for the size
         foreach (var item in data)
         {
             sb.Append(item.Key);
             sb.Append("=");
             sb.Append(item.Value);
             sb.Append("|");
         }
         byte[] bit = UTF8Encoding.UTF8.GetBytes(sb.ToString());
         bit[1] = (byte)(sb.Length >> 8);
         bit[2] = (byte)(sb.Length);
         AddValue(bit);
     }
 }
        /// <summary>
        /// The background worker. Will exit after 10 consectutive errors
        /// </summary>
        private void DoWork()
        {
            //number of errors
            int nume = 0;
            while (isrunning)
            {
                try
                {
                    IPEndPoint RIPend = null;
                    //Recieve the data
                    while (cRIOConnection.Available < 1018 && isrunning) { Thread.Sleep(2); }
                    if (!isrunning)
                    {
                        cRIOConnection.Close();
                        return;
                    }
                    byte[] buffer = cRIOConnection.Receive(ref RIPend);
                    string Output;

                    //Convert
                    Output = UTF8Encoding.UTF8.GetString(buffer);

                    //Find segment of data
                    if (Output.Contains("@@ZomB:|") && Output.Contains("|:ZomB@@") && !Output.Contains("@@ZomB:|:ZomB@@"))
                    {
                        Output = Output.Substring(Output.IndexOf("@@ZomB:|") + 8, (Output.IndexOf("|:ZomB@@") - (Output.IndexOf("@@ZomB:|") + 8)));
                        if (Output != "")
                        {
                            //Check first
                            if (!VerifyPacket(buffer))
                            {
                                if (InvalidPacketRecieved != null && !peeking)
                                {
                                    //Create our e
                                    InvalidPacketRecievedEventArgs e = new InvalidPacketRecievedEventArgs(buffer, ddh.InvalidPacketAction == InvalidPacketActions.AlwaysContinue || ddh.InvalidPacketAction == InvalidPacketActions.Continue);
                                    InvalidPacketRecieved(this, e);
                                    if ((int)ddh.InvalidPacketAction < 3)//1-4
                                    {
                                        if (!e.ContinueAnyway)
                                            break;
                                    }
                                    else if (ddh.InvalidPacketAction == InvalidPacketActions.AlwaysIgnore)
                                        break;
                                }
                            }

                            //Get the items in a dictionary
                            ZomBDataLookup vals = SplitParams(Output);
                            if (peeking)
                            {
                                foreach (var keys in vals)
                                {
                                    dp.Invoke(cb, keys.Key);
                                }
                            }
                            else
                            {
                                FRCDSStatus status = ParseDSBytes(buffer);
                                cStat = status;
                                kys = vals;

                                //Fire events
                                if (DataRecieved != null)
                                    DataRecieved(this, new EventArgs());
                                if (NewStatusRecieved != null)
                                    NewStatusRecieved(this, new NewStatusRecievedEventArgs(status));
                                if (NewDataRecieved != null)
                                    NewDataRecieved(this, new NewDataRecievedEventArgs(vals));
                            }
                        }
                    }
                    if (nume > 0)
                        nume--;
                }
                catch (ThreadAbortException)
                {
                    isrunning = false;
                    cRIOConnection.Close();
                    return;
                }
                catch (Exception ex)
                {
                    nume++;
                    DoError(ex);
                    if (nume > 10)
                    {
                        isrunning = false;
                        DoError(new Exception("10 consecutive errors were encountered, stopping DashboardDataHub"));
                        isrunning = false;
                        try
                        {
                            cRIOConnection.Close();
                        }
                        catch { }
                        return;
                    }
                }
            }
        }
        private void DoWork()
        {
            while (saving)
            {
                //create a new object so we don't clobber the old data
                kys = new ZomBDataLookup();

                var str = ins.ReadLine();
                if (str == null)
                {
                    ins.BaseStream.Seek(0, SeekOrigin.Begin);
                    str = ins.ReadLine();
                    str = ins.ReadLine();
                    if (str == null)
                    {
                        saving = false;
                        return;
                    }
                }
                var strs = str.Split(',');
                for (int i = 0; i < Math.Min(header.Count, strs.Length); i++)
                {
                    kys.Add(header[i], new ZomBDataObject(strs[i], ZomBDataTypeHint.Unknown));
                }
                Thread.Sleep(20);
                if (peeking)
                {
                    foreach (var item in kys)
                    {
                        dp.Invoke(cb, item.Key);
                    }
                }

                //Fire events
                if (DataRecieved != null)
                    DataRecieved(this, new EventArgs());
                if (NewDataRecieved != null)
                    NewDataRecieved(this, new NewDataRecievedEventArgs(kys));
            }
        }