1

I am creating an IOT device where the user can set a particular time to trigger an action by an IOT device. For eg: At 01:00 PM, the Air Conditioner starts automatically. I am using Google Cloud Platform. I checked the Cron job in GCP, which triggers a particular URL at particular time or a specified interval. Since my trigger time is stored in the datastore so either i've to query the database after every minute using cron job where i can write the logic, to trigger the action if the time matches. but there will always be 59 seconds lag in worst case else i can call the URL after every second. Calling a ulr after every second will be very costly since i need to query the database every 1 second.. Is there any other efficient way to achieve this?

Sandy
  • 79
  • 2
  • 8
  • Possible duplicate of [High frequency data refresh with Google App Engine](https://stackoverflow.com/questions/35206365/high-frequency-data-refresh-with-google-app-engine) – Dan Cornilescu Mar 12 '18 at 15:07

1 Answers1

1

Just to be sure I'm understanding, the use-case you're trying to avoid, is a user setting the AC to go on at, say, 1PM, and set it at 13:00:01 (so they'd have to wait until 13:01:00 for the AC to turn on)?

Assuming that's the case, then, if you're using Firebase/Firestore, you can use a GCF (Google Cloud Function) to actually trigger the AC turning on (send an IoT Core config message to the device, for example, not sure how you're actually physically turning on the AC). The GCF can be triggered by either the CRON job which fires every minute, or have it respond (it's one of the event triggers) to a database value change in Firebase.

So the user sets the time for the AC to come on, which sets a value in the Firebase database, which triggers the GCF to check the value and see if it should be turning on the AC or not. That way you're not relying on the CRON job alone to turn on the AC, you also have a data-changed event to check the time as well. That should eliminate the 59 second wait time in your case.

Gabe Weiss
  • 3,134
  • 1
  • 12
  • 15
  • Thanks!, My usecase is very simple. I am giving an option in my mobile app so that user can set a particular time when he wants AC to be switched ON. Eg: User can put in the mobile that he wants to switch ON the AC at 01:00 PM. I am not using Firebase/Firestore.. so not sure if i can follow what you have suggested.. Any other alternate approach? – Sandy Mar 12 '18 at 18:27
  • You could do it yourself with JUST a Cloud Function if you want as well. Where the function does two things: 1) It sets the preferred time on whatever system you're using, and 2) If does a test to see if the "on" time is current and if so, just triggers it immediately with a push. To do that though, you'll need to secure the device to the outside world in some way. Cloud IoT Core on GCP manages that using TLS 1.2 and authentication using SSL key pairs. – Gabe Weiss Mar 12 '18 at 19:41
  • Could you please elaborate more on the Cloud Functions? specially point number 2. For a function to check if the "on" time equals to current time then it has to run forever.. because "on" time can be anytime user picks.. the cost of the function is based on the duration its being executed as per the pricing instructions of function.. don't you think it will be costly? – Sandy Mar 13 '18 at 17:27
  • Another solution i am thinking of is using push queues... but the limitation here is that 1 task in a push queue can only be executed maximum for 10 minutes.. oh my god ! this sounded like a very simple requirement but never thought that the solution will be so complex... any help ? – Sandy Mar 13 '18 at 17:32
  • Cloud Functions are relatively cheap. 2M free invocations per month. And compute time/memory time is cheap as long as you don't go nuts in the function. So in terms of checking the time, I mean, user sets the time they want AC to go on. As long as they don't set "now" or "in the past" as the time, whatever you have checking every minute will kick in and pick up the right time to turn on the AC. So what the Cloud Function would do, is when the user sets the time, it would check that time against "Now". So `if (setTime <= time.time() { turnOnAc(); }`. – Gabe Weiss Mar 13 '18 at 22:30
  • Can cloud function only be triggered from Cloud storage and Pub/Sub? Also is there any language other than node.js that we can write the cloud function? I am not very familiar with Node.js – Sandy Mar 15 '18 at 05:41
  • You can also call them directly: https://cloud.google.com/functions/docs/calling/direct Basically it becomes an HTTP endpoint you can directly call if you want. As for other languages, not yet. It's being actively worked on, but it's not generally available just yet. – Gabe Weiss Mar 15 '18 at 15:16
  • Given that you are having a lengthy conversation here, I would suggest using the [StackOverflow chat](https://chat.stackoverflow.com/) to continue dealing with the issue. In the meantime, @Sandy, if you have more questions, please ask a new question in this platform. – Rodrigo C. Mar 19 '18 at 09:32