Пример #1
0
        private void button_set_Click(object sender, EventArgs e)
        {
            if (!checkBox_LoadAll.Checked)
            {

                button_set.Enabled = false;

                if (current_command.CompareTo("sensor_sensitivity") == 0)
                {
                    #region Sensitivity

                    string val = info_cmd_value_sensitivity.Text;

                    if (cur_sensitivity.CompareTo(val) != 0)
                    {
                        //sets sensitivity to the selected value
                        // SET_SEN sen = new SET_SEN(val);
                        //((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(sen._Bytes);

                        //System.Threading.Thread.Sleep(200);
                    }

                    info_cmd_value_sensitivity.Text = "";
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(200);

                    //reads the sensor sensitivity
                    Command cmd = new GET_SEN();
                    ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes);

                    #endregion Sensitivity

                    button_set.Enabled = true;
                }
                else if (current_command.CompareTo("transmission_mode") == 0)
                {

                    #region Transmission Mode

                    string trm_val = info_cmd_value_tr_rate.Text;

                    //check if the value changed
                    if (cur_tr_mode.CompareTo(trm_val) != 0)
                    {
                        //sets the appropriate sampling rate for the new transmission mode
                        int sr_value = cur_sampling_rate;
                        bool is_valid = true;

                        #region Set The Appropriate Sampling Rate

                        if (trm_val.CompareTo("continuous") == 0)
                        {
                            if (cur_sampling_rate < 1 || cur_sampling_rate > 126)
                            {  //set cur_sampling_rate to 90Hz
                                sr_value = 90;
                                is_valid = false;
                            }
                        }
                        else if (trm_val.Contains("30"))
                        {
                            if (cur_sampling_rate < 1 || cur_sampling_rate > 80)
                            {  //set cur_sampling_rate to 80Hz
                                sr_value = 80;
                                is_valid = false;
                            }
                        }
                        else if (trm_val.Contains("60"))
                        {
                            if (cur_sampling_rate < 1 || cur_sampling_rate > 40)
                            {  //set cur_sampling_rate to 40Hz
                                sr_value = 40;
                                is_valid = false;
                            }
                        }
                        else if (trm_val.Contains("90"))
                        {
                            if (cur_sampling_rate < 1 || cur_sampling_rate > 30)
                            {  //set cur_sampling_rate to 30Hz
                                sr_value = 30;
                                is_valid = false;
                            }
                        }
                        else if (trm_val.Contains("120"))
                        {
                            if (cur_sampling_rate < 1 || cur_sampling_rate > 20)
                            {  //set cur_sampling_rate to 20Hz
                                sr_value = 20;
                                is_valid = false;
                            }
                        }

                        #endregion

                        //if sampling rate is not valid, change to the right value
                        if (!is_valid)
                        {
                            //clean field
                            info_cmd_value_sampling_rate.Text = "";
                            Application.DoEvents();
                            System.Threading.Thread.Sleep(200);

                            //set sampling rate
                            //SET_SR sr = new SET_SR(sr_value);
                            //((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(sr._Bytes);

                            //wait for response
                            System.Threading.Thread.Sleep(200);

                            //reads the sampling rate
                            Command sr_cmd = new GET_SR();
                            ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(sr_cmd._Bytes);

                        }

                        //sets transmission mode to the selected value
                        //SET_TM tr = new SET_TM(val);
                        //((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(tr._Bytes);

                        //System.Threading.Thread.Sleep(200);
                    }

                    //clean field
                    info_cmd_value_tr_rate.Text = "";
                    Application.DoEvents();

                    //reads the sensor sensitivity
                    Command cmd = new GET_TM();
                    ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes);

                    #endregion Transmission Mode

                    button_set.Enabled = true;
                }
                else if (current_command.CompareTo("sampling_rate") == 0)
                {

                    #region Sampling Rate

                    string sr_val = info_cmd_value_sampling_rate.Text;
                    int value = Int32.Parse(sr_val);

                    if (cur_sampling_rate != value)
                    {
                        string tm_val = info_cmd_label_tr_rate.Text;
                        bool is_valid = false;

                        //verify validity
                        if (tm_val.CompareTo("Continous") == 0)
                        {
                            //set_panel_label_status.Text = "The Wockets is set to continous mode. This mode supports sampling rates between 1Hz and 126Hz. Enter the sampling rate in the command textbox.";
                            if (value > 1 && value <= 126)
                                is_valid = true;
                        }
                        else if (tm_val.Contains("30"))
                        {
                            //set_panel_label_status.Text = "The Wockets is set to burst 30 secs mode. This mode supports sampling rates between 1Hz and 80Hz. Enter the sampling rate in the command textbox.";
                            value = Int32.Parse(sr_val);
                            if (value > 1 && value <= 80)
                                is_valid = true;
                        }
                        else if (tm_val.Contains("60"))
                        {
                            //set_panel_label_status.Text = "The Wockets is set to burst 60 secs mode. This mode supports sampling rates between 1Hz and 40Hz. Enter the sampling rate in the command textbox.";
                            value = Int32.Parse(sr_val);
                            if (value > 1 && value <= 40)
                                is_valid = true;
                        }
                        else if (cur_tr_mode.Contains("90"))
                        {
                            //set_panel_label_status.Text = "The Wockets is set to burst 90 secs mode. This mode supports sampling rates between 1Hz and 30Hz. Enter the sampling rate in the command textbox.";
                            value = Int32.Parse(sr_val);
                            if (value > 1 && value <= 30)
                                is_valid = true;
                        }
                        else if (cur_tr_mode.Contains("120"))
                        {
                            //set_panel_label_status.Text = "The Wockets is set to burst 120 secs mode. This mode supports sampling rates between 1Hz and 20Hz. Enter the sampling rate in the command textbox.";
                            value = Int32.Parse(sr_val);
                            if (value > 1 && value <= 20)
                                is_valid = true;
                        }
                        else if (cur_tr_mode.CompareTo("") == 0)
                        {
                            //set_panel_label_status.Text = "Before setting up the sampling rate, it is necessary specify the transmission mode.";
                        }

                        //sets the sampling rate to the selected value
                        if (is_valid)
                        {
                            SET_SR sr = new SET_SR(value);
                            ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(sr._Bytes);
                        }
                    }

                    //clean field
                    info_cmd_value_sampling_rate.Text = "";
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(200);

                    //reads the sampling rate
                    Command cmd = new GET_SR();
                    ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes);

                    #endregion Sampling Rate

                    button_set.Enabled = true;
                }
                else if (current_command.CompareTo("power_down") == 0)
                {

                    #region PowerDown Timeout

                    string pdt_val = info_cmd_value_pwr_timeout.Text;

                    if (pdt_val.Trim().CompareTo("") != 0)
                    {
                        try
                        {
                            int value = Int32.Parse(pdt_val);

                            if (cur_power_down != value)
                            {
                                //verify validity
                                if (value >= 1 && value <= 127)
                                {   //set the power down timeout  according the permitted range in minutes
                                    SET_PDT pdt = new SET_PDT(value);
                                    ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(pdt._Bytes);
                                }
                                else
                                {
                                    //set_panel_label_status.Text = "The time you entered is out of range. The power down timeout range is between 1 min and 127 minutes.";
                                }
                            }
                        }
                        catch
                        {
                            //TODO
                            //set_panel_label_status.Text = "The time you entered is out of range. The power down timeout range is between 1 min and 127 minutes.";
                        }
                    }

                    //clean field
                    info_cmd_value_pwr_timeout.Text = "";
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(200);

                    //reads the power down timeout
                    Command cmd = new GET_PDT();
                    ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes);

                    #endregion PowerDown Timeout

                    button_set.Enabled = true;
                }
                else if (current_command.CompareTo("calibration") == 0)
                {
                    #region Calibration

                    //setup the calibration panel parameters
                    cal_panel_title.Text = info_cmd_label_calibration.Text;
                    cal_panel_cmd_label.Text = "File";
                    cal_panel_entry_path.Text = "";
                    cal_panel_label_status.Text = "";

                    panel_status_texbox.Text = "";
                    panel_status.Visible = false;

                    cal_panel_values_BTpercent.Visible = false;
                    cal_panel_cal_values.Visible = true;

                    panel_calibration.Visible = true;
                    is_sensordata_valid = false;

                    #endregion Calibration

                }
                else if (current_command.CompareTo("battery_calibration") == 0)
                {
                    #region BAT Calibration
                    //setup the calibration panel parameters
                    cal_panel_title.Text = info_cmd_label_btcalibration.Text;
                    cal_panel_cmd_label.Text = "File";
                    cal_panel_entry_path.Text = "";
                    cal_panel_label_status.Text = "";

                    panel_status_texbox.Text = "";
                    panel_status.Visible = false;

                    cal_panel_values_BTpercent.Visible = true;
                    cal_panel_cal_values.Visible = false;

                    panel_calibration.Visible = true;
                    is_sensordata_valid = false;

                    #endregion BAT Calibration

                }
                else
                {
                    button_set.Enabled = true;
                }

            }
        }
Пример #2
0
        /// <summary>
        /// Handler for processing application messages
        /// </summary>
        public static void ApplicationHandler()
        {
            int tid = Thread.CurrentThread.ManagedThreadId;
            string commandPath = Core.REGISTRY_REGISTERED_APPLICATIONS_PATH + "\\{" + (string)applicationPaths[tid] + "}";
            NamedEvents namedEvent = new NamedEvents();
            string applicationGuid = ((string)applicationPaths[tid]);
            string channel = applicationGuid + "-kernel";
            //WocketsController lwcontroller = null;
            while (true)
            {
                //ensures prior synchronization
                namedEvent.Receive(channel.ToString());

                lock (terminationLock)
                {
                    kernelLock.WaitOne();
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(commandPath, true);
                    string msg = (string)rk.GetValue("Message");
                    string param = (string)rk.GetValue("Param");
                    rk.Close();

                    //lock the kernel to avoid changing wockets controller at the
                    //same time from multiple applications

                    #region DISCOVER
                    if (msg == KernelCommand.DISCOVER.ToString())
                    {
                        Thread discovery = new Thread(new ThreadStart(Discover));
                        discovery.Start();
                    }
                    #endregion DISCOVER

                    #region CONNECT
                    else if (msg == KernelCommand.CONNECT.ToString())
                    {
                        try
                        {
                            if (!_WocketsRunning)
                            {

                                //load local wockets controller
                                CurrentWockets._Controller = new WocketsController("", "", "");
                                CurrentWockets._Controller._StorageDirectory = Storage.GenerateStoragePath();
                                CurrentWockets._Controller.FromXML(path + "//NeededFiles//Master//SensorData.xml");
                                //CurrentWockets._Controller._Mode = MemoryMode.BluetoothToShared;
                                CurrentWockets._Controller._Mode = MemoryMode.BluetoothToLocal;
                                CurrentWockets._Controller._TMode = TransmissionMode.Bursty60;
                                try
                                {
                                    File.Copy(path + "//NeededFiles//Master//Configuration.xml", CurrentWockets._Controller._StorageDirectory + "\\Configuration.xml");
                                }
                                catch (Exception e)
                                {
                                }

                                CurrentWockets._Configuration = new Wockets.Data.Configuration.WocketsConfiguration();
                                CurrentWockets._Configuration.FromXML(CurrentWockets._Controller._StorageDirectory + "\\Configuration.xml");
                                CurrentWockets._Configuration._MemoryMode = Wockets.Data.Configuration.MemoryConfiguration.SHARED;

                                int index = 0;
                                for (int i = 0; (i < Booter.wcontroller._Sensors.Count); i++)
                                {
                                    if (Booter.wcontroller._Sensors[i]._Loaded)
                                    {
                                        CurrentWockets._Controller._Receivers[index]._ID = index;
                                        ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[index])._Address = ((RFCOMMReceiver)Booter.wcontroller._Receivers[i])._Address;
                                        CurrentWockets._Controller._Decoders[index]._ID = index;
                                        CurrentWockets._Controller._Sensors[index]._ID = index;
                                        CurrentWockets._Controller._Sensors[index]._Receiver = CurrentWockets._Controller._Receivers[index];
                                        CurrentWockets._Controller._Sensors[index]._Decoder = CurrentWockets._Controller._Decoders[index];
                                        CurrentWockets._Controller._Sensors[index]._Loaded = true;
                                        index++;
                                    }
                                }

                                for (int i = CurrentWockets._Controller._Sensors.Count - 1; (i >= 0); i--)
                                {
                                    if (!CurrentWockets._Controller._Sensors[i]._Loaded)
                                    {
                                        CurrentWockets._Controller._Receivers.RemoveAt(i);
                                        CurrentWockets._Controller._Sensors.RemoveAt(i);
                                        CurrentWockets._Controller._Decoders.RemoveAt(i);
                                    }
                                    else
                                        CurrentWockets._Controller._Sensors[i]._RootStorageDirectory = CurrentWockets._Controller._StorageDirectory + "\\data\\raw\\PLFormat\\";
                                }

                                CurrentWockets._Controller._Receivers.SortByAddress();
                                for (int i = 0; (i < CurrentWockets._Controller._Sensors.Count); i++)
                                    ((Wocket)CurrentWockets._Controller._Sensors[i])._Receiver = CurrentWockets._Controller._Receivers[i];

                                if (CurrentWockets._Controller._Sensors.Count > 0)
                                {
                                    rk = Registry.LocalMachine.OpenSubKey(Core.REGISTRY_KERNEL_PATH, true);
                                    rk.SetValue("Storage", CurrentWockets._Controller._StorageDirectory);
                                    rk.Close();

                                    TextWriter tw = new StreamWriter(CurrentWockets._Controller._StorageDirectory + "\\SensorData.xml");
                                    tw.WriteLine(CurrentWockets._Controller.ToXML());
                                    tw.Close();

                                    _WocketsRunning = true;
                                    CurrentWockets._Controller.Initialize();

                                    //Subscribe to all callback events
                                    foreach (Decoder d in CurrentWockets._Controller._Decoders)
                                    {
                                        d.Subscribe(ResponseTypes.BL_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.BP_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.PC_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.SENS_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.CAL_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.SR_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.TM_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.AC_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        d.Subscribe(ResponseTypes.TCT_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                    }

                                }

                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.CONNECTED, guid);
                            }

                        }
                        catch (Exception e)
                        {
                            foreach (string guid in applicationPaths.Values)
                                Send(KernelResponse.CONNECT_FAILED, guid);
                            Logger.Error("Booter.cs:ApplicationHandler: CONNECT:" + e.ToString());
                        }
                    }
                    #endregion CONNECT

                    #region DISCONNECT
                    else if (msg == KernelCommand.DISCONNECT.ToString())
                    {
                        try
                        {
                            if (_WocketsRunning)
                            {
                                CurrentWockets._Controller.Dispose();
                                _WocketsRunning = false;
                            }
                            foreach (string guid in applicationPaths.Values)
                                Send(KernelResponse.DISCONNECTED, guid);

                        }
                        catch (Exception e)
                        {
                            foreach (string guid in applicationPaths.Values)
                                Send(KernelResponse.DISCONNECT_FAILED, guid);
                            Logger.Error("Booter.cs:ApplicationHandler: DISCONNECT:" + e.ToString());
                        }
                    }
                    #endregion DISCONNECT

                    #region SET_WOCKETS
                    else if (msg == KernelCommand.SET_WOCKETS.ToString())
                    {
                        try
                        {
                            if (!_WocketsRunning)
                            {
                                int index = 0;
                                for (int i = 0; (i < 5); i++)
                                {
                                    rk = Registry.LocalMachine.OpenSubKey(Core.REGISTRY_SENSORS_PATH + "\\" + i.ToString("0"));
                                    int status = (int)rk.GetValue("Status");
                                    if (status == 1)
                                    {
                                        string mac = (string)rk.GetValue("MacAddress");

                                        ((RFCOMMReceiver)Booter.wcontroller._Receivers[index])._Address = mac;
                                        Booter.wcontroller._Sensors[index++]._Loaded = true;

                                    }
                                    rk.Close();
                                }
                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.SENSORS_UPDATED, guid);
                            }
                            else
                            {
                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.SENSORS_UPDATED_FAILED, guid);
                            }
                        }
                        catch (Exception e)
                        {
                            foreach (string guid in applicationPaths.Values)
                                Send(KernelResponse.SENSORS_UPDATED_FAILED, guid);
                            Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKETS:" + e.ToString());
                        }
                    }
                    #endregion SET_WOCKETS

                    #region GET_BATTERY_LEVEL
                    else if (msg == KernelCommand.GET_BATTERY_LEVEL.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_BT();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_BATTERY_LEVEL:" + e.ToString());
                            }
                        }
                    }
                    #endregion GET_BATTERY_LEVEL

                    #region GET_BATTERY_PERCENT
                    else if (msg == KernelCommand.GET_BATTERY_PERCENT.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_BP();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }

                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_BATTERY_PERCENT:" + e.ToString());
                            }
                        }
                    }
                    #endregion GET_BATTERY_PERCENT

                    #region GET_PDU_COUNT
                    else if (msg == KernelCommand.GET_PDU_COUNT.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_PC();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_PDU_COUNT:" + e.ToString());
                            }
                        }
                    }
                    #endregion GET_PDU_COUNT

                    #region GET_WOCKET_SENSITIVITY
                    else if (msg == KernelCommand.GET_WOCKET_SENSITIVITY.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_SEN();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_SENSITIVITY:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_WOCKET_SENSITIVITY

                    #region SET_WOCKET_SENSITIVITY
                    else if (msg == KernelCommand.SET_WOCKET_SENSITIVITY.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                string[] tokens = param.Split(new char[] { ':' });
                                Sensitivity sensitivity = (Sensitivity)Enum.Parse(typeof(Sensitivity),
                                   tokens[1], true);
                                Command command = new SET_SEN(sensitivity);
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (tokens[0] == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_SENSITIVITY:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_WOCKET_SENSITIVITY

                    #region GET_WOCKET_CALIBRATION
                    else if (msg == KernelCommand.GET_WOCKET_CALIBRATION.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_CAL();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_CALIBRATION:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_WOCKET_CALIBRATION

                    #region SET_WOCKET_CALIBRATION
                    else if (msg == KernelCommand.SET_WOCKET_CALIBRATION.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                string[] tokens = param.Split(new char[] { ':' });
                                string mac = tokens[0];
                                Calibration calibration = new Calibration();
                                calibration._X1G = (ushort)Convert.ToUInt32(tokens[1]);
                                calibration._XN1G = (ushort)Convert.ToUInt32(tokens[2]);
                                calibration._Y1G = (ushort)Convert.ToUInt32(tokens[3]);
                                calibration._YN1G = (ushort)Convert.ToUInt32(tokens[4]);
                                calibration._Z1G = (ushort)Convert.ToUInt32(tokens[5]);
                                calibration._ZN1G = (ushort)Convert.ToUInt32(tokens[6]);
                                Command command = new SET_CAL(calibration);
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == mac)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_CALIBRATION:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_WOCKET_CALIBRATION

                    #region GET_WOCKET_SAMPLING_RATE
                    else if (msg == KernelCommand.GET_WOCKET_SAMPLING_RATE.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_SR();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_SAMPLING_RATE:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_WOCKET_SAMPLING_RATE

                    #region SET_WOCKET_SAMPLING_RATE
                    else if (msg == KernelCommand.SET_WOCKET_SAMPLING_RATE.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                string[] tokens = param.Split(new char[] { ':' });
                                Command command = new SET_SR(Convert.ToInt32(tokens[1]));
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (tokens[0] == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_SAMPLING_RATE:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_WOCKET_SAMPLING_RATE

                    #region GET_WOCKET_POWERDOWN_TIMEOUT
                    else if (msg == KernelCommand.GET_WOCKET_POWERDOWN_TIMEOUT.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_PDT();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_SAMPLING_RATE:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_WOCKET_POWERDOWN_TIMEOUT

                    #region SET_WOCKET_POWERDOWN_TIMEOUT
                    else if (msg == KernelCommand.SET_WOCKET_POWERDOWN_TIMEOUT.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                string[] tokens = param.Split(new char[] { ':' });
                                Command command = new SET_PDT(Convert.ToInt32(tokens[1]));
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (tokens[0] == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_SAMPLING_RATE:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_WOCKET_POWERDOWN_TIMEOUT

                    #region GET_TRANSMISSION_MODE
                    else if (msg == KernelCommand.GET_TRANSMISSION_MODE.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_TM();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_TRANSMISSION_MODE:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_TRANSMISSION_MODE

                    #region SET_TRANSMISSION_MODE
                    else if (msg == KernelCommand.SET_TRANSMISSION_MODE.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                string[] tokens = param.Split(new char[] { ':' });

                                TransmissionMode mode = (TransmissionMode)Enum.Parse(typeof(TransmissionMode),
                                    tokens[1], true);

                                //If the mode changed only
                                //if (CurrentWockets._Controller._TMode != mode)
                                // {

                                // to switch from bursty mode, update the receiver mode and on next connection
                                // the mode will be set up
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (tokens[0] == "all")
                                        ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._TMode = mode;
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                    {
                                        ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._TMode = mode;
                                        break;
                                    }
                                }

                                if (CurrentWockets._Controller._TMode == TransmissionMode.Continuous)
                                {
                                    Command command = new SET_VTM(mode);
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (tokens[0] == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }

                                CurrentWockets._Controller.Deinitialize();
                                Thread.Sleep(10000);

                                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("\\");
                                System.IO.FileSystemInfo[] fsi = di.GetFileSystemInfos();
                                string firstCard = "";
                                //iterate through them
                                for (int x = 0; x < fsi.Length; x++)
                                {
                                    //check to see if this is a temporary storage card (e.g. SD card)
                                    if ((fsi[x].Attributes & System.IO.FileAttributes.Temporary) == System.IO.FileAttributes.Temporary)
                                    {
                                        firstCard = fsi[x].FullName;
                                        try
                                        {
                                            Directory.CreateDirectory(firstCard + "\\writable");
                                            Directory.Delete(firstCard + "\\writable");
                                        }
                                        catch (Exception)
                                        {
                                            firstCard = "";
                                            continue;
                                        }
                                        //if so, return the path

                                        break;
                                    }
                                }

                                DateTime now = DateTime.Now;
                                string storageDirectory = firstCard + "\\Wockets\\Session-" + now.Month.ToString("00") + "-" + now.Day.ToString("00") + "-" + now.Year.ToString("0000") + "-" + now.Hour.ToString("00") + "-" + now.Minute.ToString("00") + "-" + now.Second.ToString("00");

                                WocketsController wc = new WocketsController("", "", "");
                                CurrentWockets._Controller = wc;
                                wc._StorageDirectory = storageDirectory;
                                wc.FromXML("\\Program Files\\wockets\\NeededFiles\\SensorConfigurations\\SensorDataFahd.xml");
                                try
                                {
                                    File.Copy("\\Program Files\\wockets\\NeededFiles\\SensorConfigurations\\SensorDataFahd.xml", storageDirectory + "\\SensorData.xml");
                                }
                                catch
                                {
                                }

                                wc._Mode = MemoryMode.BluetoothToLocal;

                                if (!File.Exists("test.csv"))
                                {

                                    TextWriter tw = new StreamWriter("test.csv", true);
                                    tw.Write("Index, Time, Battery %, Voltage, Current, Temperature");
                                    for (int i = 0; (i < wc._Sensors.Count); i++)
                                    {
                                        tw.Write(", Received Packets " + i + ", Expected Packet Count" + i + ",Num Succ Connections " + i + ",Num Reconnections " + i + ", Connection Time " + i);
                                    }
                                    tw.WriteLine();
                                    tw.Close();
                                }

                                if (!File.Exists("testsummary.csv"))
                                {

                                    for (int i = 0; (i < wc._Sensors.Count); i++)

                                    {
                                        TextWriter tw1 = new StreamWriter("testsummary" + i + ".csv", true);
                                        tw1.Write("Index, Time,Summary");
                                        tw1.WriteLine();
                                        tw1.Close();
                                    }

                                }

                                wc._TMode = TransmissionMode.Bursty60;
                                wc.Initialize();
                                //reminderDateTime = DateTime.Now.AddSeconds(60);

                               /* CurrentWockets._Controller._StorageDirectory = Storage.GenerateStoragePath();

                                for (int i = 0; (i < CurrentWockets._Controller._Sensors.Count); i++)
                                {
                                    CurrentWockets._Controller._Sensors[i]._Loaded = true;
                                    CurrentWockets._Controller._Sensors[i]._Flush = true;
                                    CurrentWockets._Controller._Sensors[i]._RootStorageDirectory = CurrentWockets._Controller._StorageDirectory + "\\data\\raw\\PLFormat\\";
                                }
                                try
                                {
                                    File.Copy(path + "//NeededFiles//Master//Configuration.xml", CurrentWockets._Controller._StorageDirectory + "\\Configuration.xml");
                                }
                                catch
                                {
                                }
                                try
                                {
                                    TextWriter tw = new StreamWriter(CurrentWockets._Controller._StorageDirectory + "\\SensorData.xml");
                                    tw.WriteLine(CurrentWockets._Controller.ToXML());
                                    tw.Close();
                                }
                                catch
                                {
                                }
                                CurrentWockets._Controller._TMode = mode;
                                CurrentWockets._Controller.Initialize();*/
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_TRANSMISSION_MODE:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_TRANSMISSION_MODE

                    #region GET_MEMORY_MODE
                    else if (msg == KernelCommand.GET_MEMORY_MODE.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_TRANSMISSION_MODE:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_MEMORY_MODE

                    #region SET_MEMORY_MODE
                    else if (msg == KernelCommand.SET_MEMORY_MODE.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                MemoryMode mode = (MemoryMode)Enum.Parse(typeof(MemoryMode),
                                    param.Split(new char[] { ':' })[1], true);
                                CurrentWockets._Controller._Mode = mode;
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_MEMORY_MODE:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_MEMORY_MODE

                    #region GET_WOCKET_TCT
                    else if (msg == KernelCommand.GET_TCT.ToString())
                    {
                        if (_WocketsRunning)
                        {
                            try
                            {
                                Command command = new GET_TCT();
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (param == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: GET_TCT:" + e.ToString());
                            }
                        }
                    }

                    #endregion GET_TCT

                    #region SET_TCT
                    else if (msg == KernelCommand.SET_TCT.ToString())
                    {
                        if (_WocketsRunning)
                        {

                            try
                            {
                                string[] tokens = param.Split(new char[] { ':' });
                                Command command = new SET_TCT(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3]));
                                for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                {
                                    if (tokens[0] == "all")
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                    else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                    {
                                        ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        break;
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.Error("Booter.cs:ApplicationHandler: SET_TCT:" + e.ToString());
                            }
                        }
                    }
                    #endregion SET_TCT

                    kernelLock.Release();
                }
                namedEvent.Reset();
            }
        }
Пример #3
0
        /// <summary>
        /// Handler for processing application messages
        /// </summary>
        public static void ApplicationHandler()
        {
            try
            {
                int tid = Thread.CurrentThread.ManagedThreadId;
                string commandPath = Core.REGISTRY_REGISTERED_APPLICATIONS_PATH + "\\{" + (string)applicationPaths[tid] + "}";
                NamedEvents namedEvent = new NamedEvents();
                string applicationGuid = ((string)applicationPaths[tid]);
                string channel = applicationGuid + "-kernel";
                //WocketsController lwcontroller = null;

                while (true)
                {
                    //ensures prior synchronization
                    namedEvent.Receive(channel.ToString());

                    lock (terminationLock)
                    {
                        kernelLock.WaitOne();

                        RegistryKey rk = Registry.LocalMachine.OpenSubKey(commandPath, true);
                        string msg = (string)rk.GetValue("Message");
                        string param = (string)rk.GetValue("Param");
                        rk.Close();

                        //lock the kernel to avoid changing wockets controller at the
                        //same time from multiple applications

                        #region DISCOVER
                        if (msg == KernelCommand.DISCOVER.ToString())
                        {
                            Thread discovery = new Thread(new ThreadStart(Discover));
                            discovery.Start();
                        }
                        #endregion DISCOVER

                        #region CONNECT
                        else if (msg == KernelCommand.CONNECT.ToString())
                        {
                            try
                            {
                                if (!_WocketsRunning)
                                {

                                    //load local wockets controller
                                    CurrentWockets._Controller = new WocketsController("", "", "");
                                    CurrentWockets._Controller._StorageDirectory = Storage.GenerateStoragePath();
                                    CurrentWockets._Controller.FromXML(path + "//NeededFiles//Master//SensorData.xml");

                                    CurrentWockets._Controller._Mode = MemoryMode.BluetoothToShared;

                                    rk = Registry.LocalMachine.OpenSubKey(commandPath, true);
                                    string tmodevalue = (string)rk.GetValue("TMode");
                                    rk.Close();

                                    TransmissionMode tmode = (TransmissionMode)Enum.Parse(typeof(TransmissionMode), tmodevalue, true);
                                    CurrentWockets._Controller._TMode = tmode;

                                    try
                                    {
                                        File.Copy(path + "//NeededFiles//Master//Configuration.xml", CurrentWockets._Controller._StorageDirectory + "\\Configuration.xml");
                                    }
                                    catch (Exception e)
                                    {
                                    }

                                    CurrentWockets._Configuration = new Wockets.Data.Configuration.WocketsConfiguration();
                                    CurrentWockets._Configuration.FromXML(CurrentWockets._Controller._StorageDirectory + "\\Configuration.xml");
                                    CurrentWockets._Configuration._MemoryMode = Wockets.Data.Configuration.MemoryConfiguration.SHARED;

                                    int index = 0;
                                    for (int i = 0; (i < Booter.wcontroller._Sensors.Count); i++)
                                    {
                                        if (Booter.wcontroller._Sensors[i]._Loaded)
                                        {
                                            CurrentWockets._Controller._Receivers[index]._ID = index;
                                            ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[index])._Address = ((RFCOMMReceiver)Booter.wcontroller._Receivers[i])._Address;
                                            CurrentWockets._Controller._Decoders[index]._ID = index;
                                            CurrentWockets._Controller._Sensors[index]._ID = index;
                                            CurrentWockets._Controller._Sensors[index]._Receiver = CurrentWockets._Controller._Receivers[index];
                                            CurrentWockets._Controller._Sensors[index]._Decoder = CurrentWockets._Controller._Decoders[index];
                                            CurrentWockets._Controller._Sensors[index]._Loaded = true;
                                            index++;
                                        }
                                    }

                                    for (int i = CurrentWockets._Controller._Sensors.Count - 1; (i >= 0); i--)
                                    {
                                        if (!CurrentWockets._Controller._Sensors[i]._Loaded)
                                        {
                                            CurrentWockets._Controller._Receivers.RemoveAt(i);
                                            CurrentWockets._Controller._Sensors.RemoveAt(i);
                                            CurrentWockets._Controller._Decoders.RemoveAt(i);
                                        }
                                        else
                                            CurrentWockets._Controller._Sensors[i]._RootStorageDirectory = CurrentWockets._Controller._StorageDirectory + "\\data\\raw\\PLFormat\\";
                                    }

                                    //CurrentWockets._Controller._Receivers.SortByAddress();
                                    for (int i = 0; (i < CurrentWockets._Controller._Sensors.Count); i++)
                                        ((Wocket)CurrentWockets._Controller._Sensors[i])._Receiver = CurrentWockets._Controller._Receivers[i];

                                    if (CurrentWockets._Controller._Sensors.Count > 0)
                                    {
                                        rk = Registry.LocalMachine.OpenSubKey(Core.REGISTRY_KERNEL_PATH, true);
                                        rk.SetValue("Storage", CurrentWockets._Controller._StorageDirectory);
                                        rk.Close();

                                        TextWriter tw = new StreamWriter(CurrentWockets._Controller._StorageDirectory + "\\SensorData.xml");
                                        tw.WriteLine(CurrentWockets._Controller.ToXML());
                                        tw.Close();

                                        _WocketsRunning = true;
                                        CurrentWockets._Controller.Initialize();

                                        //Subscribe to all callback events
                                        foreach (Decoder d in CurrentWockets._Controller._Decoders)
                                        {
                                            d.Subscribe(ResponseTypes.BL_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.BP_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.PC_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.SENS_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.CAL_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.SR_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.TM_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.AC_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                            d.Subscribe(ResponseTypes.TCT_RSP, new Decoder.ResponseHandler(DecoderCallback));
                                        }

                                    }

                                    foreach (string guid in applicationPaths.Values)
                                        Send(KernelResponse.CONNECTED, guid);
                                }

                            }
                            catch (Exception e)
                            {
                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.CONNECT_FAILED, guid);
                                Logger.Error("Booter.cs:ApplicationHandler: CONNECT:" + e.ToString());
                            }
                        }
                        #endregion CONNECT

                        #region DISCONNECT
                        else if (msg == KernelCommand.DISCONNECT.ToString())
                        {
                            try
                            {
                                if (_WocketsRunning)
                                {
                                    CurrentWockets._Controller.Dispose();
                                    _WocketsRunning = false;
                                    CurrentWockets._Controller = null;
                                }
                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.DISCONNECTED, guid);

                            }
                            catch (Exception e)
                            {
                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.DISCONNECT_FAILED, guid);
                                Logger.Error("Booter.cs:ApplicationHandler: DISCONNECT:" + e.ToString());
                            }
                        }
                        #endregion DISCONNECT

                        #region SET_WOCKETS
                        else if (msg == KernelCommand.SET_WOCKETS.ToString())
                        {
                            try
                            {
                                if (!_WocketsRunning)
                                {
                                    int index = 0;
                                    for (int i = 0; (i < 5); i++)
                                    {
                                        rk = Registry.LocalMachine.OpenSubKey(Core.REGISTRY_SENSORS_PATH + "\\" + i.ToString("0"));
                                        int status = (int)rk.GetValue("Status");
                                        if (status == 1)
                                        {
                                            string mac = (string)rk.GetValue("MacAddress");

                                            ((RFCOMMReceiver)Booter.wcontroller._Receivers[index])._Address = mac;
                                            Booter.wcontroller._Sensors[index++]._Loaded = true;

                                        }
                                        rk.Close();
                                    }
                                    foreach (string guid in applicationPaths.Values)
                                        Send(KernelResponse.SENSORS_UPDATED, guid);
                                }
                                else
                                {
                                    foreach (string guid in applicationPaths.Values)
                                        Send(KernelResponse.SENSORS_UPDATED_FAILED, guid);
                                }
                            }
                            catch (Exception e)
                            {
                                foreach (string guid in applicationPaths.Values)
                                    Send(KernelResponse.SENSORS_UPDATED_FAILED, guid);
                                Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKETS:" + e.ToString());
                            }
                        }
                        #endregion SET_WOCKETS

                        #region GET_BATTERY_LEVEL
                        else if (msg == KernelCommand.GET_BATTERY_LEVEL.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_BT();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_BATTERY_LEVEL:" + e.ToString());
                                }
                            }
                        }
                        #endregion GET_BATTERY_LEVEL

                        #region GET_BATTERY_PERCENT
                        else if (msg == KernelCommand.GET_BATTERY_PERCENT.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_BP();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }

                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_BATTERY_PERCENT:" + e.ToString());
                                }
                            }
                        }
                        #endregion GET_BATTERY_PERCENT

                        #region GET_PDU_COUNT
                        else if (msg == KernelCommand.GET_PDU_COUNT.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_PC();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_PDU_COUNT:" + e.ToString());
                                }
                            }
                        }
                        #endregion GET_PDU_COUNT

                        #region GET_WOCKET_SENSITIVITY
                        else if (msg == KernelCommand.GET_WOCKET_SENSITIVITY.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_SEN();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_SENSITIVITY:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_WOCKET_SENSITIVITY

                        #region SET_WOCKET_SENSITIVITY
                        else if (msg == KernelCommand.SET_WOCKET_SENSITIVITY.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    string[] tokens = param.Split(new char[] { ':' });
                                    Sensitivity sensitivity = (Sensitivity)Enum.Parse(typeof(Sensitivity),
                                       tokens[1], true);
                                    Command command = new SET_SEN(sensitivity);
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (tokens[0] == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_SENSITIVITY:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_WOCKET_SENSITIVITY

                        #region GET_WOCKET_CALIBRATION
                        else if (msg == KernelCommand.GET_WOCKET_CALIBRATION.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_CAL();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_CALIBRATION:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_WOCKET_CALIBRATION

                        #region SET_WOCKET_CALIBRATION
                        else if (msg == KernelCommand.SET_WOCKET_CALIBRATION.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    string[] tokens = param.Split(new char[] { ':' });
                                    string mac = tokens[0];
                                    Calibration calibration = new Calibration();
                                    calibration._X1G = (ushort)Convert.ToUInt32(tokens[1]);
                                    calibration._XN1G = (ushort)Convert.ToUInt32(tokens[2]);
                                    calibration._Y1G = (ushort)Convert.ToUInt32(tokens[3]);
                                    calibration._YN1G = (ushort)Convert.ToUInt32(tokens[4]);
                                    calibration._Z1G = (ushort)Convert.ToUInt32(tokens[5]);
                                    calibration._ZN1G = (ushort)Convert.ToUInt32(tokens[6]);
                                    Command command = new SET_CAL(calibration);
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == mac)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_CALIBRATION:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_WOCKET_CALIBRATION

                        #region GET_WOCKET_SAMPLING_RATE
                        else if (msg == KernelCommand.GET_WOCKET_SAMPLING_RATE.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_SR();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_SAMPLING_RATE:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_WOCKET_SAMPLING_RATE

                        #region SET_WOCKET_SAMPLING_RATE
                        else if (msg == KernelCommand.SET_WOCKET_SAMPLING_RATE.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    string[] tokens = param.Split(new char[] { ':' });
                                    Command command = new SET_SR(Convert.ToInt32(tokens[1]));
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (tokens[0] == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_SAMPLING_RATE:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_WOCKET_SAMPLING_RATE

                        #region GET_WOCKET_POWERDOWN_TIMEOUT
                        else if (msg == KernelCommand.GET_WOCKET_POWERDOWN_TIMEOUT.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_PDT();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_WOCKET_SAMPLING_RATE:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_WOCKET_POWERDOWN_TIMEOUT

                        #region SET_WOCKET_POWERDOWN_TIMEOUT
                        else if (msg == KernelCommand.SET_WOCKET_POWERDOWN_TIMEOUT.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    string[] tokens = param.Split(new char[] { ':' });
                                    Command command = new SET_PDT(Convert.ToInt32(tokens[1]));
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (tokens[0] == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_WOCKET_SAMPLING_RATE:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_WOCKET_POWERDOWN_TIMEOUT

                        #region GET_TRANSMISSION_MODE
                        else if (msg == KernelCommand.GET_TRANSMISSION_MODE.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_TM();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_TRANSMISSION_MODE:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_TRANSMISSION_MODE

                        #region SET_TRANSMISSION_MODE
                        else if (msg == KernelCommand.SET_TRANSMISSION_MODE.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    string[] tokens = param.Split(new char[] { ':' });

                                    TransmissionMode mode = (TransmissionMode)Enum.Parse(typeof(TransmissionMode),
                                        tokens[1], true);

                                    //If the mode changed only
                                    //if (CurrentWockets._Controller._TMode != mode)
                                    // {

                                    // to switch from bursty mode, update the receiver mode and on next connection
                                    // the mode will be set up
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (tokens[0] == "all")
                                            ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._TMode = mode;
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                        {
                                            ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._TMode = mode;
                                            break;
                                        }
                                    }

                                    if (CurrentWockets._Controller._TMode == TransmissionMode.Continuous)
                                    {
                                        Command command = new SET_VTM(mode);
                                        for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                        {
                                            if (tokens[0] == "all")
                                                ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                            {
                                                ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                                break;
                                            }
                                        }
                                    }

                                    CurrentWockets._Controller.Deinitialize();
                                    Thread.Sleep(10000);

                                    CurrentWockets._Controller._StorageDirectory = Storage.GenerateStoragePath();

                                    for (int i = 0; (i < CurrentWockets._Controller._Sensors.Count); i++)
                                    {
                                        CurrentWockets._Controller._Sensors[i]._Loaded = true;
                                        CurrentWockets._Controller._Sensors[i]._Flush = true;
                                        CurrentWockets._Controller._Sensors[i]._RootStorageDirectory = CurrentWockets._Controller._StorageDirectory + "\\data\\raw\\PLFormat\\";
                                    }
                                    try
                                    {
                                        File.Copy(path + "//NeededFiles//Master//Configuration.xml", CurrentWockets._Controller._StorageDirectory + "\\Configuration.xml");
                                    }
                                    catch
                                    {
                                    }
                                    try
                                    {
                                        TextWriter tw = new StreamWriter(CurrentWockets._Controller._StorageDirectory + "\\SensorData.xml");
                                        tw.WriteLine(CurrentWockets._Controller.ToXML());
                                        tw.Close();
                                    }
                                    catch
                                    {
                                    }
                                    CurrentWockets._Controller._TMode = mode;
                                    CurrentWockets._Controller.Initialize();
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_TRANSMISSION_MODE:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_TRANSMISSION_MODE

                        #region GET_MEMORY_MODE
                        else if (msg == KernelCommand.GET_MEMORY_MODE.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_TRANSMISSION_MODE:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_MEMORY_MODE

                        #region SET_MEMORY_MODE
                        else if (msg == KernelCommand.SET_MEMORY_MODE.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    MemoryMode mode = (MemoryMode)Enum.Parse(typeof(MemoryMode),
                                        param.Split(new char[] { ':' })[1], true);
                                    CurrentWockets._Controller._Mode = mode;
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_MEMORY_MODE:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_MEMORY_MODE

                        #region GET_WOCKET_TCT
                        else if (msg == KernelCommand.GET_TCT.ToString())
                        {
                            if (_WocketsRunning)
                            {
                                try
                                {
                                    Command command = new GET_TCT();
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (param == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == param)
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: GET_TCT:" + e.ToString());
                                }
                            }
                        }

                        #endregion GET_TCT

                        #region SET_TCT
                        else if (msg == KernelCommand.SET_TCT.ToString())
                        {
                            if (_WocketsRunning)
                            {

                                try
                                {
                                    string[] tokens = param.Split(new char[] { ':' });
                                    Command command = new SET_TCT(Convert.ToInt32(tokens[1]), Convert.ToInt32(tokens[2]), Convert.ToInt32(tokens[3]));
                                    for (int i = 0; (i < CurrentWockets._Controller._Receivers.Count); i++)
                                    {
                                        if (tokens[0] == "all")
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                        else if (((RFCOMMReceiver)CurrentWockets._Controller._Receivers[i])._Address == tokens[0])
                                        {
                                            ((SerialReceiver)CurrentWockets._Controller._Receivers[i]).Write(command._Bytes);
                                            break;
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    Logger.Error("Booter.cs:ApplicationHandler: SET_TCT:" + e.ToString());
                                }
                            }
                        }
                        #endregion SET_TCT

                        kernelLock.Release();
                    }
                    namedEvent.Reset();
                }

            }
            catch(Exception e)
            {
                Logger.Error("Booter.cs:an exception occurred in the ApplicationHandler" + e.ToString());
            }
        }