Bemasoft
Posts: 13
Joined: Sun Apr 07, 2013 6:37 pm

Listen to vehicle variable change

Tue Apr 09, 2013 2:51 pm

I'm trying to develope a plugin that registers a new column in vehicle list adding a button "Stop power".

This button sends a command to the device to cut the power system and the device sends back continuosly a signal notifiying its state.

I need to know if it's possible to "listen" any event signaling a change in a device variable in order to enable/disable the button in function of the state of device.

Thanks and Regards.

kalmangabriel
Posts: 385
Joined: Wed Feb 25, 2009 12:36 am
Location: Romania
Contact: Website Skype

RE: Listen to vehicle variable change

Tue Apr 09, 2013 6:14 pm

yes... it is
Cheers,
Gabriel

<font color="red">Please note that my reply IS NOT from GpsGate!!!</font id="red">
------------------------------------------
<font size="1">Email: tech@scorpiontrack.ro Skype: gabrielkalman
Internet: It doesn't make you more stupid, it just makes your stupidity more accessible to others</font id="size1">

Bemasoft
Posts: 13
Joined: Sun Apr 07, 2013 6:37 pm

RE: Listen to vehicle variable change

Tue Apr 09, 2013 6:31 pm

I know I can listen to "onevent" for alarms and "onvehicleschanged" for changes in vehicle attributes but, how can I listen to a variable change without creating an event rule? That is my question.

User avatar
Mattias
GpsGate
Posts: 135
Joined: Wed Dec 16, 2009 3:59 pm
Location: Sweden

RE: Listen to vehicle variable change

Tue Apr 09, 2013 7:28 pm

Hi,

There is currently no signal ready to use for vehicle variable change-detection. The best solution is probably to create an event rule, and listen to the "onevent" signal.

Try the following:

Code: Select all

MochiKit.Signal.connect(Franson.Messaging, 'onevent', function(events) {
	// "events" contains new events, look for events created by your variable changed-rule here
	var disabled = true; //TODO: check signal
	// update button state, see column example below
	jQuery('#btnStopPower_' + VEHICLEID).prop('disabled', disabled); //TODO: get vehicle id from event object
});
Example on how to add custom columns to the vehicle list:

Code: Select all

// add column
vtMain._tableControl.registerColumn({
	id: 'colStopPower',
	label: 'Stop power',
	value: function(vehicle) {
		var hasDevice = vehicle.devices.length > 0;

		function createDOM(onClick) {
			// to avoid circular ref
			return jQuery('<button>Stop power</button>')
				.attr('id', 'btnStopPower_' + vehicle.id)
				.prop('disabled', !hasDevice) // disable button if there is no device
				.on('click', onClick);
		}

		return createDOM(function(e) {
			e.stopPropagation();

			// on click, send command
			if (hasDevice) {
				var device = vehicle.devices[0];
				GpsGate.Server.Command.Send(device.id, '_CommandName').addCallbacks(
				//OR: GpsGate.Server.Command.SendWithArgs(device.id, '_CommandName(arg1=val1, arg2=val2)').addCallbacks(
					function(response) {
						log(response);
					},
					function(error) {
						logError(error);
					}
				);
			}
		});
	},
	compare: function(a, b) {
		return compare(a.name, b.name);
	},
	visible: true
});

// remove column
vtMain._tableControl.unregisterColumn('colStopPower');

Regards,
Mattias

User avatar
Mattias
GpsGate
Posts: 135
Joined: Wed Dec 16, 2009 3:59 pm
Location: Sweden

RE: Listen to vehicle variable change

Tue Apr 09, 2013 7:35 pm

Oops, I took too long to reply :)

But I don't think there is any other good way to do this with acceptable performance.
_Maybe_ you could use the GpsGate.Server.Directory.GetLatestGateRecords service method.
The problem is that you need to do this for all vehicles, continuosly.

Use the service method like so:

Code: Select all

var vehicles = Context.getApp().getVehicles();

for (var i = 0; i < vehicles.length; ++i) {
	var vehicle = vehicles[i];

	GpsGate.Server.Directory.GetLatestGateRecords(vehicle.id, true).addCallback(function(response) {
		// check response.gateMessages
		// setup polling to do this request again in 10-30 seconds maybe?
	});
});
Regards,
Mattias

Return to “GpsGate for Developers”