Beispiel #1
0
 /// <summary>
 /// Handler for Elapsed event of sync timer
 /// </summary>
 /// <param name="sender">Timer instance that triggered this event</param>
 /// <param name="e">Event arguments</param>
 private void Update_timer_Elapsed(object sender, ElapsedEventArgs e)
 {
     UpdateTimer.Stop();
     Locked = true;
     // Iterate storage and sync all values
     foreach (char addr in value_tracker.Keys)
     {
         ApplicationPdu pdu = new ApplicationPdu
         {
             Address = addr,
             Value   = value_tracker[addr]
         };
         ServerBroadcastValue(pdu);
     }
     Locked = false;
     UpdateTimer.Start();
     return;
 }
Beispiel #2
0
 /// <summary>
 /// Event handler for OnMessageReceived event of link
 /// </summary>
 /// <param name="sender">Link layer instance</param>
 /// <param name="msg">Event parameters</param>
 private void Link_OnMessageReceived(LinkLayer sender, LinkMessage msg)
 {
     if (!IsServer)
     {
         char addr = msg.Data[0];
         if (value_tracker.Keys.Contains(addr))
         {
             ApplicationPdu pdu = new ApplicationPdu()
             {
                 Address = addr,
                 Value   = Convert.ToInt32(msg.Data.Substring(1))
             };
             value_tracker[pdu.Address] = pdu.Value;
             Debug.WriteLine("APP-ADDR:" + pdu.Address + "-VAL:" + msg.Data.Substring(1));
             TriggerApplicationUpdate(pdu);
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// Handler for application layer notifications
        /// </summary>
        /// <param name="sender"><c>ApplicationLayer</c> instance</param>
        /// <param name="pdu">Application protocol data unit</param>
        private void ProtocolHandler_OnApplicationUpdate(ApplicationLayer sender, ApplicationPdu pdu)
        {
            switch (pdu.Address)
            {
            case 'A':
                // Update trackbar value
                if (pdu.Value <= trckAnalog.Maximum)
                {
                    Invoke(new Action(() =>
                    {
                        trckAnalog.Value = pdu.Value;
                    }));
                }
                break;

            case 'D':
                // Update button color
                bool value = Convert.ToBoolean(pdu.Value);
                if (value)
                {
                    Invoke(new Action(() =>
                    {
                        bttnDigitial.BackColor = Color.Green;
                    }));
                }
                else
                {
                    Invoke(new Action(() =>
                    {
                        bttnDigitial.BackColor = Color.Gray;
                    }));
                }
                break;

            default:
                break;
            }
        }
Beispiel #4
0
 /// <summary>
 /// Send application protocol data unit to lower layer
 /// </summary>
 /// <param name="pdu">Application protocol data unit to send</param>
 /// <returns>true if success, false if error</returns>
 private bool ServerBroadcastValue(ApplicationPdu pdu)
 {
     if (IsServer)
     {
         try
         {
             LinkMessage msg = new LinkMessage()
             {
                 Data = pdu.Address + pdu.Value.ToString()
             };
             link.Send(msg);
         }
         catch
         {
             return(false);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #5
0
 /// <summary>
 /// Method for triggering OnApplicationUpdate event
 /// </summary>
 /// <param name="pdu"></param>
 protected virtual void TriggerApplicationUpdate(ApplicationPdu pdu)
 {
     OnApplicationUpdate?.Invoke(this, pdu);
 }