Last active
January 26, 2016 13:22
-
-
Save btgoodwin/c3e4248bdd0ee969c78d to your computer and use it in GitHub Desktop.
Start of some generic factory extensions on top of those in client/redhawk-services.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * This file is protected by Copyright. Please refer to the COPYRIGHT file | |
| * distributed with this source distribution. | |
| * | |
| * This file is part of fei-factories. | |
| * | |
| * REDHAWK redhawk-web-app is free software: you can redistribute it and/or modify it | |
| * under the terms of the GNU Lesser General Public License as published by the | |
| * Free Software Foundation, either version 3 of the License, or (at your | |
| * option) any later version. | |
| * | |
| * REDHAWK redhawk-web-app is distributed in the hope that it will be useful, but WITHOUT | |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
| * for more details. | |
| * | |
| * You should have received a copy of the GNU Lesser General Public License | |
| * along with this program. If not, see http://www.gnu.org/licenses/. | |
| */ | |
| // Author: Thomas Goodwin | |
| // Company: Geon Technologies LLC, 20156 | |
| angular.module('redhawk', ['FEIDevice']) | |
| /* Geon's GPS Wrapper Example (supports 'dummy' movable device) | |
| * Provides a convenience interface for UI development with FEI GPS devices that have a Provides GPS port. | |
| * | |
| * Instantiate the factory using the extra argument to the new redhawk-services Domain object: | |
| * var myGPS = Redhawk.getDomain(domainId).getDevice(deviceId, managerId, 'GenericGPS'); | |
| * | |
| * The resulting object can be subscribed to (addListener(callback)) to drive UI updates anytime | |
| * the refresh() method is called allowing multiple UI elements to be driven by the same method. | |
| */ | |
| .factory('GenericGPS', ['FEIDevice', | |
| function(FEIDevice) { | |
| // Define and extend from the base factory, FEIDevice | |
| var GenericGPS = function() { | |
| var self = this; | |
| FEIDevice.apply(self, arguments); | |
| // List of callbacks to fire during refresh() | |
| self._feiListeners = []; | |
| // NOTE: Position is only updated after getPosition query completes. | |
| // It is better to pass a callback to getPosition to receive | |
| // timely updates. | |
| self.position = {latitude: 0.0, longitude: 0.0}; | |
| self.positionIsValid = false; | |
| self.feiPort = undefined; | |
| self.isMovable = false; | |
| // If the configurable 'position' property struct exists, we can move | |
| // this "dummy" GPS FEI stand-in. | |
| self.setPosition = function(lat, lon) { | |
| if (self.isMovable) { | |
| self.configure([{ | |
| id: 'position', | |
| value: [ | |
| { id: 'position::latitude', value: lat }, | |
| { id: 'position::longitude', value: lon } | |
| ] | |
| }]); | |
| } | |
| } | |
| // Async refresh of position. | |
| // Pass a callback for when the update completes. | |
| // To have several entities update, add the callbacks | |
| // using addListener and have a single entity call refresh. | |
| // Callback receives a structure | |
| // { latitude: float, | |
| // longitude: float } | |
| self.getPosition = function(callback) { | |
| if (self.feiPort) { | |
| self.feiQuery(self.feiPort.name) | |
| .then( | |
| // "Success" | |
| function(response) { | |
| self.position.latitude = self.feiPort.gps_time_pos.position.lat; | |
| self.position.longitude = self.feiPort.gps_time_pos.position.lon; | |
| self.positionIsValid = self.feiPort.gps_time_pos.position.valid; | |
| return response; | |
| }, | |
| // "Exception" Mark position as invalid | |
| function (err) { | |
| self.positionIsValid = false; | |
| return err; | |
| }) | |
| .finally( | |
| // "Finally" Emit position regardless of update | |
| function(fin) { | |
| if (callback) | |
| callback(self.position); | |
| return fin; | |
| } | |
| ); | |
| } | |
| else { | |
| console.warn('GPS FEI Port handle not found; Unable to update position.'); | |
| } | |
| } | |
| // One-time refresh method, emits position to any attached listeners | |
| self.refresh = function () { | |
| self.getPosition(function() { | |
| var i = self._feiListeners.length; | |
| // Notify and prune any stale listeners | |
| while (i--) { | |
| if (self._feiListeners[i]) | |
| self._feiListeners[i](self.position); | |
| else | |
| self._feiListeners.splice(i, 1); | |
| } | |
| }); | |
| } | |
| // Attach a callback for when a position update has been requested (by entity) | |
| self.addListener = function(callback) { | |
| self._feiListeners.push(callback); | |
| } | |
| self.removeListener = function(callback) { | |
| var i = self._feiListeners.length; | |
| while (i--) { | |
| if (self._feiListeners[i] == callback) { | |
| self._feiListeners.splice(i, 1); | |
| break; | |
| } | |
| } | |
| } | |
| // Add a run-many updateFinished that persistently updates isMovable. | |
| // Update isMovable. The presence of the 'position' property structure | |
| // indicates this GPS can be reconfigured to a different location (i.e., demo dummy). | |
| self.updateFinished.push(function () { | |
| self.isMovable = (UtilityFunctions.findPropId(self.properties, 'position')) ? true : false; | |
| }); | |
| // Add a run-once updateFinished that gives us a | |
| // convenience property, feiPort, to use | |
| self.updateFinished.push(function() { | |
| angular.forEach(self.ports, function(port) { | |
| if (('FRONTEND' == port.idl.namespace) && | |
| ('GPS' == port.idl.type) && | |
| ('Provides' == port.direction)) { | |
| self.feiPort = port; | |
| } | |
| }); | |
| self.refresh(); | |
| return false; // Run once | |
| }); | |
| } | |
| GenericGPS.prototype = Object.create(FEIDevice.prototype); | |
| GenericGPS.prototype.constructor = GenericGPS; | |
| return GenericGPS; | |
| }]) | |
| ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment