/// <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(); } }
private void Load_button_Click(object sender, EventArgs e) { button_load.Enabled = false; Application.DoEvents(); if (checkBox_LoadAll.Checked && (current_command.CompareTo("all") == 0)) { info_panel_clean_text_fields(); Application.DoEvents(); //LoadWocketsParameters(); } else { Command cmd; switch (current_command) { case "hw_version": info_cmd_value_hwversion.Text = ""; Application.DoEvents(); cmd = new GET_HV(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "sw_version": info_cmd_value_swversion.Text = ""; Application.DoEvents(); cmd = new GET_FV(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "packet_count": info_cmd_value_pkt_count.Text = ""; Application.DoEvents(); //cmd = new GET_PC(); //((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "battery_level": info_cmd_value_battery_level.Text = ""; Application.DoEvents(); cmd = new GET_BT(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "battery_percent": info_cmd_value_btpercent.Text = ""; Application.DoEvents(); cmd = new GET_BP(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "battery_calibration": info_cmd_value_btcalibration.Text = ""; Application.DoEvents(); cmd = new GET_BTCAL(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "calibration": info_cmd_value_calibration.Text = ""; Application.DoEvents(); cmd = new GET_CAL(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "sensor_sensitivity": info_cmd_value_sensitivity.Text = ""; Application.DoEvents(); cmd = new GET_SEN(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "transmission_mode": info_cmd_value_tr_rate.Text = ""; Application.DoEvents(); cmd = new GET_TM(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "sampling_rate": info_cmd_value_sampling_rate.Text = ""; Application.DoEvents(); cmd = new GET_SR(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; case "power_down": info_cmd_value_pwr_timeout.Text = ""; Application.DoEvents(); cmd = new GET_PDT(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); break; } } button_load.Enabled = true; }
private void Poll() { #region Poll All Wockets and MITes and Decode Data if ((this._Mode == MemoryMode.BluetoothToLocal) || (this._Mode == MemoryMode.BluetoothToShared)) { bool allWocketsDisconnected = true; bool bluetoothIsReset = false; Receiver currentReceiver = null; Sensor sensor = null; int[] batteryPoll = new int[this._Sensors.Count]; int[] alive = new int[this._Sensors.Count]; GET_BT GET_BT_CMD = new GET_BT(); ALIVE ALIVE_CMD = new ALIVE(); int pollCounter = 0; System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); System.Reflection.AssemblyName an = a.GetName(); Logger.Warn("Version " + an.Version.ToString() +" Date:"+ DateTime.Now.ToString("f")); this.StartTime = WocketsTimer.GetUnixTime(); while (true) { if (!polling) { this.waitToPollEvent.Set(); for (int i = 0; (i < this._Sensors.Count); i++) { this._Sensors[i]._ReceivedPackets = 0; this._Sensors[i]._SavedPackets = 0; this._Receivers[i].Dispose(); } this.pollingEvent.WaitOne(); } allWocketsDisconnected = true; pollCounter++; for (int i = 0; (i < this._Sensors.Count); i++) { sensor = this._Sensors[i]; if (sensor._Loaded) { currentReceiver = sensor._Receiver; try { if (this._TMode== TransmissionMode.Bursty60) { int expectedPackets = ((Wockets.Decoders.Accelerometers.WocketsDecoder)sensor._Decoder)._ExpectedBatchCount; //skip if got everything if ((expectedPackets > 0) && (sensor._ReceivedPackets == expectedPackets)) continue; else currentReceiver.Update(); } else currentReceiver.Update(); if (currentReceiver._Status == ReceiverStatus.Connected) { Decoder decoder = sensor._Decoder; int numDecodedPackets = 0; int tail = currentReceiver._Buffer._Tail; int head = currentReceiver._Buffer._Head; int dataLength = tail - head; //((RFCOMMReceiver)currentReceiver).bluetoothStream._Tail - currentReceiver._Head; if (dataLength < 0) dataLength = currentReceiver._Buffer._Bytes.Length - head + tail;//((RFCOMMReceiver)currentReceiver).bluetoothStream._Buffer.Length - currentReceiver._Head + ((RFCOMMReceiver)currentReceiver).bluetoothStream._Tail; //test if all wockets are disconnected if (sensor._Class == SensorClasses.Wockets) { if (bluetoothIsReset) bluetoothIsReset = false; if (allWocketsDisconnected) allWocketsDisconnected = false; } if (dataLength > 0) { if (currentReceiver._Type == ReceiverTypes.HTCDiamond) { numDecodedPackets = decoder.Decode(sensor._ID, currentReceiver._Buffer, head, tail); sensor._ReceivedPackets += numDecodedPackets; } else if (sensor._Class == SensorClasses.Wockets) { #region Write Data #region Battery Query batteryPoll[i] -= 1; if (batteryPoll[i] <= 0) { ((SerialReceiver)currentReceiver).Write(GET_BT_CMD._Bytes); batteryPoll[i] = 6000 + i * 200; } #endregion Battery Query #region Alive alive[i] -= 1; if (alive[i] <= 0) { ((SerialReceiver)currentReceiver).Write(ALIVE_CMD._Bytes); alive[i] = 200; } #endregion Alive #endregion Write Data #region Read Data numDecodedPackets = decoder.Decode(sensor._ID, currentReceiver._Buffer, head, tail); //((RFCOMMReceiver)currentReceiver).bluetoothStream._Buffer, head, tail); currentReceiver._Buffer._Head = tail; sensor._ReceivedPackets += numDecodedPackets; #endregion Read Data } } if (pollCounter > 2000) { //Logger.Warn("Receiver " + sensor._Receiver._ID + " decoded:" + sensor._ReceivedPackets + ",saved:" + sensor._SavedPackets + ", tail=" + tail + ",head=" + head); pollCounter = 0; } } } catch (Exception ex) { alive[i] = 200;//10 in sniff//200 in continuous worked well Logger.Error(ex.Message + " \nTrace:" + ex.StackTrace); currentReceiver.Dispose(); } } } //reset bluetooth stack once if all wockets are disconnected if ((this._TMode== TransmissionMode.Continuous) && (!bluetoothIsReset) && (allWocketsDisconnected)) { try { //if (CurrentWockets._Configuration._SoftwareMode == Wockets.Data.Configuration.SoftwareConfiguration.DEBUG) // Logger.Debug("All Wockets Disconnected. BT Reset."); NetworkStacks._BluetoothStack.Dispose(); NetworkStacks._BluetoothStack.Initialize(); bluetoothIsReset = true; } catch { } } Thread.Sleep(10); } } #if (PocketPC) //Read data from shared memory and populate the decoder else if (this._Mode == MemoryMode.SharedToLocal) { MemoryMappedFileStream[] sdata = null; MemoryMappedFileStream[] shead = null; byte[] head = new byte[4]; int sdataSize = 0; int numSensors = CurrentWockets._Controller._Sensors.Count; int[] decoderTails; byte[] timestamp = new byte[sizeof(double)]; byte[] acc = new byte[sizeof(short)]; sdata = new MemoryMappedFileStream[numSensors]; shead = new MemoryMappedFileStream[numSensors]; sdataSize = (int)Decoder._DUSize * Wockets.Decoders.Accelerometers.WocketsDecoder.BUFFER_SIZE; decoderTails = new int[numSensors]; for (int i = 0; (i < numSensors); i++) { sdata[i] = new MemoryMappedFileStream("\\Temp\\wocket" + i + ".dat", "wocket" + i, (uint)sdataSize, MemoryProtection.PageReadWrite); shead[i] = new MemoryMappedFileStream("\\Temp\\whead" + i + ".dat", "whead" + i, sizeof(int), MemoryProtection.PageReadWrite); sdata[i].MapViewToProcessMemory(0, sdataSize); shead[i].MapViewToProcessMemory(0, sizeof(int)); shead[i].Read(head, 0, 4); int currentHead = BitConverter.ToInt32(head, 0); decoderTails[i] = currentHead; shead[i].Seek(0, System.IO.SeekOrigin.Begin); sdata[i].Seek((currentHead * (sizeof(double) + 3 * sizeof(short))), System.IO.SeekOrigin.Begin); } while (true) { try { for (int i = 0; (i < CurrentWockets._Controller._Sensors.Count); i++) { int tail = decoderTails[i]; int currentHead = tail; shead[i].Read(head, 0, 4); currentHead = BitConverter.ToInt32(head, 0); shead[i].Seek(0, System.IO.SeekOrigin.Begin); while (tail != currentHead) { #if (PocketPC) int bufferHead = CurrentWockets._Controller._Decoders[i]._Head; WocketsAccelerationData datum = ((WocketsAccelerationData)CurrentWockets._Controller._Decoders[i]._Data[bufferHead]); datum.Reset(); datum._SensorID = (byte)i; sdata[i].Read(timestamp, 0, sizeof(double)); datum.UnixTimeStamp = BitConverter.ToDouble(timestamp, 0); sdata[i].Read(acc, 0, sizeof(short)); datum._X = BitConverter.ToInt16(acc, 0); sdata[i].Read(acc, 0, sizeof(short)); datum._Y = BitConverter.ToInt16(acc, 0); sdata[i].Read(acc, 0, sizeof(short)); datum._Z = BitConverter.ToInt16(acc, 0); datum._Type = Data.SensorDataType.UNCOMPRESSED_DATA_PDU; CurrentWockets._Controller._Decoders[i].TotalSamples++; //copy raw bytes //for (int i = 0; (i < bytesToRead); i++) // datum.RawBytes[i] = this.packet[i]; //datum.RawBytes[0] = (byte)(((datum.RawBytes[0])&0xc7)|(sourceSensor<<3)); if (bufferHead >= (CurrentWockets._Controller._Decoders[i]._BufferSize - 1)) bufferHead = 0; else bufferHead++; CurrentWockets._Controller._Decoders[i]._Head = bufferHead; #endif if (tail >= (Wockets.Decoders.Accelerometers.WocketsDecoder.BUFFER_SIZE - 1)) { tail = 0; #if (PocketPC) sdata[i].Seek(0, System.IO.SeekOrigin.Begin); #endif } else tail++; } decoderTails[i] = currentHead; } } catch { } Thread.Sleep(100); } } #endif #endregion Poll All Wockets and MITes and Decode Data }
private void LoadWocketsParameters() { Command cmd; //----- Read the commands when the form is loaded ------- cmd = new GET_FV(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_HV(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); //cmd = new GET_PC(); //((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_BT(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_BP(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_BTCAL(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_CAL(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_SEN(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_TM(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_SR(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); cmd = new GET_PDT(); ((RFCOMMReceiver)CurrentWockets._Controller._Receivers[0]).Write(cmd._Bytes); }
/// <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()); } }