示例#1
0
 static private void ReportDevicesFound(Int32?packetindex = null)
 {
     if (packetindex == null)
     {
         DevicesUpdateCallbackPacket returnpck = new DevicesUpdateCallbackPacket();
         lock (DevicesFound) foreach (KeyValuePair <String, Type> _deviceInfo in DevicesFound)
             {
                 returnpck.AddDevice(_deviceInfo.Key, (String)_deviceInfo.Value.GetField("Description").GetValue(null));
             }
         FlashManager.AddResponse(returnpck);
     }
     else
     {
         ScanDevicesResponsePacket returnpck = new ScanDevicesResponsePacket(packetindex ?? 0);
         lock (DevicesFound) foreach (KeyValuePair <String, Type> _deviceInfo in DevicesFound)
             {
                 returnpck.AddDevice(_deviceInfo.Key, (String)_deviceInfo.Value.GetField("Description").GetValue(null));
             }
         FlashManager.AddResponse(returnpck);
     }
 }
示例#2
0
 static public void DeviceDigitalReadCallback(Device _device, Int32 _pin, Logic _value)
 {
     FlashManager.AddResponse(new DigitalReadCallbackPacket(_device.GetIdentifier(), _pin, _value));
 }
示例#3
0
 // Thread Safe Callbacks
 static public void DeviceResetCallback(Device _device)
 {
     FlashManager.AddResponse(new FailCallbackPacket(new ClassifiedException(Error.Device_Restarted)));
 }
示例#4
0
        // Packet Analyser
        static public void PacketAnalyser(RequestPacket newPacket)
        {
            try
            {
                #region Handle Packet Index
                if (newPacket.Index == null)
                {
                    FlashManager.AddResponse(new FailCallbackPacket(new ClassifiedException(Error.Input_NoPacketIndex)));
                    return;
                }
                Int32 packetindex = newPacket.Index ?? 0;
                #endregion

                // Handle Packet Types
                #region scan-devices
                if (newPacket.Tag == "scan-devices")
                {
                    ReportDevicesFound(packetindex);
                }
                #endregion
                #region connect-device
                else if (newPacket.Tag == "connect-device")
                {
                    String packetDeviceID    = newPacket.Args[0].Value;
                    Int32  cachedpacketindex = packetindex;

                    CreateAsync(delegate() {
                        #region Abortable Thread Operation
                        Exception e = null;
                        try
                        {
                            Device targetDevice = GetDeviceByIdentifierWithCreation(packetDeviceID);
                            if (targetDevice == null)
                            {
                                throw new ClassifiedException(Error.Input_Identifier_Invalid);
                            }
                            AddDeviceToConnected(targetDevice);
                            targetDevice.Connect();
                        }
                        catch (Exception _e) { e = _e; }
                        finally {
                            if (e == null)
                            {
                                FlashManager.AddResponse(new GenericSuccessResponsePacket(cachedpacketindex));
                            }
                            else
                            {
                                FlashManager.AddResponse(new GenericFailResponsePacket(cachedpacketindex, e));
                            }
                        }
                        #endregion
                    });
                }
                #endregion
                #region disconnect-device
                else if (newPacket.Tag == "disconnect-device")
                {
                    DisconnectDeviceByIdentifier(newPacket.Args[0].Value);
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region reset-device
                else if (newPacket.Tag == "reset-device")
                {
                    try
                    {
                        AbortAllAsync();
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.ResetPinState();
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region set-digital-pin
                else if (newPacket.Tag == "set-digital-pin")
                {
                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.SetDigitalPin(Int32.Parse(newPacket.Args[1].Value), LogicHelper.Parse(newPacket.Args[2].Value));
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region set-motor-pin
                else if (newPacket.Tag == "set-motor-pin")
                {
                    Int32 value = 0;
                    try { value = (Int32)Math.Round(Double.Parse(newPacket.Args[3].Value)); }
                    catch (Exception) {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, new ClassifiedException(Error.Input_NotNumeric)));
                        return;
                    }

                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.SetMotorPin(newPacket.Args[1].Value, Int32.Parse(newPacket.Args[2].Value), value);
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region set-pwm-pin
                else if (newPacket.Tag == "set-pwm-pin")
                {
                    Int32 value = 0;
                    try { value = (Int32)Math.Round(Double.Parse(newPacket.Args[2].Value)); }
                    catch (Exception) {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, new ClassifiedException(Error.Input_NotNumeric)));
                        return;
                    }

                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.SetPWMPin(Int32.Parse(newPacket.Args[1].Value), value);
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region read-digital-pin
                else if (newPacket.Tag == "read-digital-pin")
                {
                    Int32  cachedpacketindex = packetindex;
                    String packetDeviceID    = newPacket.Args[0].Value;
                    Int32  pin    = Int32.Parse(newPacket.Args[1].Value);
                    Logic  result = Logic.LOW;

                    CreateAsync(delegate() {
                        #region Abortable Thread Operation
                        Exception e = null;
                        try
                        {
                            Device targetDevice = GetDeviceByIdentifier(packetDeviceID);
                            result = targetDevice.ReadDigitalPin(pin);
                        }
                        catch (Exception _e) { e = _e; }
                        finally
                        {
                            if (e == null)
                            {
                                FlashManager.AddResponse(new DigitalReadSuccessResponsePacket(cachedpacketindex, result));
                            }
                            else
                            {
                                FlashManager.AddResponse(new GenericFailResponsePacket(cachedpacketindex, e));
                            }
                        }
                        #endregion
                    });
                }
                #endregion
                #region read-analog-pin
                else if (newPacket.Tag == "read-analog-pin")
                {
                    Int32  cachedpacketindex = packetindex;
                    String packetDeviceID    = newPacket.Args[0].Value;
                    Int32  pin    = Int32.Parse(newPacket.Args[1].Value);
                    Int32  result = 0;

                    CreateAsync(delegate() {
                        #region Abortable Thread Operation
                        Exception e = null;
                        try
                        {
                            Device targetDevice = GetDeviceByIdentifier(packetDeviceID);
                            result = targetDevice.ReadAnalogPin(pin);
                        }
                        catch (Exception _e) { e = _e; }
                        finally
                        {
                            if (e == null)
                            {
                                FlashManager.AddResponse(new AnalogReadSuccessResponsePacket(cachedpacketindex, result));
                            }
                            else
                            {
                                FlashManager.AddResponse(new GenericFailResponsePacket(cachedpacketindex, e));
                            }
                        }
                        #endregion
                    });
                }
                #endregion
                #region read-i2c
                else if (newPacket.Tag == "read-i2c")
                {
                    Int32       cachedpacketindex = packetindex;
                    String      packetDeviceID    = newPacket.Args[0].Value;
                    Byte        i2c_address       = (Byte)Convert.ToInt32(newPacket.Args[1].Value, 16);
                    Byte        i2c_register      = (Byte)Convert.ToInt32(newPacket.Args[2].Value, 16);
                    Int32       i2c_numofbytes    = Int32.Parse(newPacket.Args[3].Value);
                    List <Byte> result            = new List <Byte>();

                    CreateAsync(delegate()
                    {
                        #region Abortable Thread Operation
                        Exception e = null;
                        try
                        {
                            Device targetDevice = GetDeviceByIdentifier(packetDeviceID);
                            result = targetDevice.I2CRead(i2c_address, i2c_register, i2c_numofbytes);
                        }
                        catch (Exception _e) { e = _e; }
                        finally
                        {
                            if (e == null)
                            {
                                FlashManager.AddResponse(new I2CReadSuccessResponsePacket(cachedpacketindex, result));
                            }
                            else
                            {
                                FlashManager.AddResponse(new GenericFailResponsePacket(cachedpacketindex, e));
                            }
                        }
                        #endregion
                    });
                }
                #endregion
                #region write-i2c
                else if (newPacket.Tag == "write-i2c")
                {
                    Byte        i2c_address  = (Byte)Convert.ToInt32(newPacket.Args[1].Value);
                    Byte        i2c_register = (Byte)Convert.ToInt32(newPacket.Args[2].Value);
                    List <Byte> i2c_data     = new List <Byte>();
                    foreach (String _data in newPacket.Args[3].Value.Split(' '))
                    {
                        i2c_data.Add((Byte)Convert.ToInt32(_data));
                    }

                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.I2CWrite(i2c_address, i2c_register, i2c_data);
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region move-servo
                else if (newPacket.Tag == "move-servo")
                {
                    Double value = 0;

                    // TODO: Put into the try for common error handling
                    if (!Double.TryParse(newPacket.Args[2].Value, out value))
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, new ClassifiedException(Error.Input_NotNumeric)));
                    }

                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.SetServoPin(Int32.Parse(newPacket.Args[1].Value), (Int32)Math.Round(value));
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region device-function
                else if (newPacket.Tag == "device-function")
                {
                    Int32 returnvalue = 0;
                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        returnvalue = targetDevice.CallFunction(newPacket.Args[1].Value);
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new FunctionSuccessResponsePacket(packetindex, returnvalue));
                }
                #endregion
                #region send-raw
                else if (newPacket.Tag == "send-raw")
                {
                    List <Byte> rawdata = new List <Byte>();
                    foreach (String _data in newPacket.Args[1].Value.Split(' '))
                    {
                        rawdata.Add((Byte)Convert.ToInt32(_data, 16));
                    }

                    try
                    {
                        Device targetDevice = GetDeviceByIdentifier(newPacket.Args[0].Value);
                        targetDevice.SendRaw(rawdata);
                    }
                    catch (Exception e)
                    {
                        FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, e));
                        return;
                    }
                    FlashManager.AddResponse(new GenericSuccessResponsePacket(packetindex));
                }
                #endregion
                #region Uknown Packet
                else
                {
                    FlashManager.AddResponse(new GenericFailResponsePacket(packetindex, new ClassifiedException(Error.Input_UnkownPacket)));
                }
                #endregion
            } catch (Exception) { }
        }