private void PeriodicTimerCallback(ThreadPoolTimer timer) { if (_cancelRequested == false) { // get exclusive use of port _adapter.beginExclusive(true); // open a path to the temp device _path.open(); // check if present if (_owc.Present) { // read the temp device byte[] state = _tc.readDevice(); _tc.doTemperatureConvert(state); state = _tc.readDevice(); Debug.WriteLine("Temperature of " + _address + " is " + _tc.getTemperature(state) + " C"); } else { Debug.WriteLine("Device " + _address + " not present so stopping thread"); } // close the path to the device _path.close(); // release exclusive use of port _adapter.endExclusive(); } else { _periodicTimer.Cancel(); var settings = ApplicationData.Current.LocalSettings; var key = _taskInstance.Task.Name; // // Write to LocalSettings to indicate that this background task ran. // settings.Values[key] = "Canceled with reason: " + _cancelReason.ToString(); Debug.WriteLine("Background " + _taskInstance.Task.Name + settings.Values[key]); // // Indicate that the background task has completed. // _deferral.Complete(); } }
/// <summary> /// Performs a search of the 1-Wire network, with branch searching /// </summary> /// <param name="arrivals"> A vector of Long objects, represent new arrival addresses. </param> /// <param name="departures"> A vector of Long objects, represent departed addresses. </param> public override void search(List <long> arrivals, List <long> departures) { lock (sync_flag) { try { // aquire the adapter adapter.beginExclusive(true); // setup the search adapter.setSearchAllDevices(); adapter.targetAllFamilies(); adapter.Speed = DSPortAdapter.SPEED_REGULAR; // close any opened branches for (int j = 0; j < paths.Count; j++) { try { ((OWPath)paths[j]).close(); } catch (System.Exception) { ; } } // search through all of the paths for (int i = 0; i < paths.Count; i++) { // set searches to not use reset adapter.setNoResetSearch(); // find the first device on this branch bool search_result = false; OWPath path = (OWPath)paths[i]; try { // try to open the current path path.open(); } catch (System.Exception) { // if opening the path failed, continue on to the next path continue; } search_result = adapter.findFirstDevice(); // loop while devices found while (search_result) { // get the 1-Wire address long longAddress = adapter.AddressAsLong; // check if the device allready exists in our hashtable if (!deviceAddressHash.ContainsKey(longAddress)) { OneWireContainer owc = getDeviceContainer(adapter, longAddress); // check to see if it's a switch and if we are supposed // to automatically search down branches if (this.branchAutoSearching && (owc is SwitchContainer)) { SwitchContainer sc = (SwitchContainer)owc; byte[] state = sc.readDevice(); for (int j = 0; j < sc.getNumberChannels(state); j++) { OWPath tmp = new OWPath(adapter, path); tmp.add(owc, j); if (!paths.Contains(tmp)) { paths.Add(tmp); } } } lock (devicePathHash) { devicePathHash[longAddress] = path; } if (arrivals != null) { arrivals.Add(longAddress); } } // check if the existing device moved else if (!path.Equals(devicePathHash[longAddress])) { lock (devicePathHash) { devicePathHash[longAddress] = path; } if (departures != null) { departures.Add(longAddress); } if (arrivals != null) { arrivals.Add(longAddress); } } // update count deviceAddressHash[longAddress] = max_state_count; // find the next device on this branch path.open(); search_result = adapter.findNextDevice(); } } } finally { adapter.endExclusive(); } // remove any devices that have not been seen foreach (var address in deviceAddressHash.Keys.Where(kv => deviceAddressHash[kv] <= 0).ToList()) { // device entry is stale, should be removed deviceAddressHash.Remove(address); if (departures != null) { departures.Add(address); } } foreach (var address in deviceAddressHash.Keys.Where(kv => deviceAddressHash[kv] > 0).ToList()) { // device entry isn't stale, it stays deviceAddressHash[address] -= 1; } // fire notification events if (departures != null && departures.Count > 0) { fireDepartureEvent(adapter, departures); } if (arrivals != null && arrivals.Count > 0) { fireArrivalEvent(adapter, arrivals); } } }
/// <summary> /// Search a given path for an XML file. /// </summary> /// <param name="currentPathIndex"> index into the 'paths' Vector that /// indicates what 1-Wire path to search for XML files </param> /// <returns> true if the current path provided has been /// completely checked for XML files. false if /// if this current path should be searched further </returns> public static bool pathXMLSearchComplete(int currentPathIndex) { OneWireContainer owd = null, check_owd = null; ParseContainer pc = null, check_pc = null; OWFileInputStream file_stream = null; bool rslt = true; bool xml_parsed = false; try { main_Renamed.Status = "Waiting for 1-Wire available"; // get exclusive use of adapter adapter.beginExclusive(true); main_Renamed.Status = "Exclusive 1-Wire aquired"; // open the current path to the device OWPath owpath = (OWPath)paths[currentPathIndex]; owpath.open(); main_Renamed.Status = "Path opened: " + owpath.ToString(); // setup search adapter.setSearchAllDevices(); adapter.targetAllFamilies(); adapter.Speed = DSPortAdapter.SPEED_REGULAR; // find all devices, update parseLog and get a device filesystem to check for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();) { owd = (OneWireContainer)owd_enum.Current; long key = owd.AddressAsLong; main_Renamed.Status = "Device Found: " + owd.AddressAsString; // check to see if this is in the parseLog, add if not there pc = null; parseLog.TryGetValue(key, out pc); if (pc == null) { main_Renamed.Status = "Device is new to parse: " + owd.AddressAsString; pc = new ParseContainer(owd); parseLog.Add(key, pc); } else { main_Renamed.Status = "Device " + owd.AddressAsString + " with count " + pc.attemptCount; } // if attemptCount is low then check it later for XML files if (pc.attemptCount < ParseContainer.MAX_ATTEMPT) { check_owd = owd; check_pc = pc; } } // check if there is anything to open if (check_owd != null) { // result is false because found something to try and open rslt = false; main_Renamed.Status = "Attempt to open file TAGX.000"; // attempt to open a 'TAGX.000' file, (if fail update parse_log) try { file_stream = new OWFileInputStream(check_owd, "TAGX.0"); main_Renamed.Status = "Success file TAGX.000 opened"; } catch (OWFileNotFoundException) { file_stream = null; check_pc.attemptCount++; main_Renamed.Status = "Could not open TAGX.000 file"; } } // try to parse the file, (if fail update parse_log) if (file_stream != null) { // create the tagParser // (this should not be necessary but the parser currently holds state) parser = new TAGParser(adapter); // attempt to parse it, on success max out the attempt if (parseStream(parser, file_stream, owpath, true)) { xml_parsed = true; check_pc.attemptCount = ParseContainer.MAX_ATTEMPT; } else { check_pc.attemptCount++; } // close the file try { file_stream.close(); } catch (IOException) { main_Renamed.Status = "Could not close TAGX.000 file"; } } // close the path owpath.close(); main_Renamed.Status = "Path closed"; // update the main paths listbox if XML file found if (xml_parsed) { // add the paths to the main window main_Renamed.clearPathList(); for (int p = 0; p < paths.Count; p++) { main_Renamed.addToPathList(((OWPath)paths[p]).ToString()); } } } catch (OneWireException e) { Debug.WriteLine(e); main_Renamed.Status = e.ToString(); } finally { // end exclusive use of adapter adapter.endExclusive(); main_Renamed.Status = "Exclusive 1-Wire aquired released"; } return(rslt); }