Blocking Call in Linux Client


#1

When consuming a thinger event, i.e. switch on/off toggle and logic is applied that may run several minutes, all other calls to the Client Lib is blocked until the running process completes.

The example in my case, is that I have an irrigation system that spans across 16 valves - when I toggle the program to tun, each valve is opened for about 15 minutes, whereafter the next is activated. This means that the program cycle will run for about 240 minutes. During this time any other calls to the Client fail, such as data collections of sensors.

  1. Can anyone recommend a implementation to avoid such a scenario?
  2. How can I update other “things” as a progress through each valve - i.e. I have a switch in the dashboard for each valve - but I want to set its value as I progress as those items in the dashboard does not automatically refresh like other widgets.

Here with a sample of my code:

thing["program_switch"] <<[](pson & in)	
{
	if (in.is_empty())
	{
		in = (bool) vProgIndicator;
	}
	else
	{
		vProgIndicator = in;
		if (vProgIndicator == 1)
		{
			//Start Pump 
			digitalWrite(relay1, LOW);
			
			int micro = 1000000;

			usleep(micro*(vProgTime*60));

			digitalWrite(relay2, LOW);
			vProgProgress = vProgProgress + 1;
			usleep(micro*(vProgTime*60));
			digitalWrite(relay2, HIGH);

			digitalWrite(relay3, LOW);
			vProgProgress = vProgProgress + 1;
			usleep(micro*(vProgTime*60));
			digitalWrite(relay3, HIGH);

			digitalWrite(relay4, LOW);
			vProgProgress = vProgProgress + 1;
			usleep(micro*(vProgTime*60));
			digitalWrite(relay4, HIGH);

			digitalWrite(relay5, LOW);
			vProgProgress = vProgProgress + 1;
			usleep(micro*(vProgTime*60));
			digitalWrite(relay5, HIGH);

			//Stop Pump
			digitalWrite(relay1, HIGH);

			//Continue with the rest of the relays here...
		}
		else
		{
			vProgProgress = 0;
			initRelays();
		}
		vProgIndicator = 0;
	}
};

#2

I haven’t coded thinger clients in linux, but I guess that the function usleep stops the execution of functions, that’s why it doesn’t execute something else while is in any of these lines.

I don’t know if you can define functions somewhere else, not in thinger “thing” due that when you call this “thing” the device wont be able to receive other calls while is running all the code in this “thing” (if “usleep” is like “delay” function in arduino).

I use to program arduino, I dont know if linux is similar, I handle timers and this kind of stuff in main loop, and program "things’ with the minimun code possible, just input and output variables, and the control to turn on and off pins (for example), is in main loop, according the control logic established and controled by the input variables, I dont know if I explained myself.


#3

Thank you Ega - i will surely try this suggestion