Showing posts with label Trigger. Show all posts
Showing posts with label Trigger. Show all posts

Last Updated: January 11, 2017

Introduction to Internet of things platform Octoblu

Description: 

In this post I'm gonna describe very helpful platform for building some stuff that involves 'Internet of thing'/ Internet of apis.
 
So lets get started.




Octoblu - It's an platform to build an amazing iot/iota projects that involve connection of  'things' with variety of tools involved in it. The 'things' could be sensors/led lights/apis from social networks like G+ or twitter etc.


Few key terms
  1. Things - Entity that you need to integrate. E.g: Twitter, G+, Http Api,Sensors etc.
  2. Tools - Connectors to connect the things. E,g: Less than, Greater than operators, Timer.
  3. Flow - Actual Work space which contains THINGS and TOOLS
  4. Bluprints - Final Shared FLOW is Bluprint.
Note: 1. For the sake simplicity we're gonna build a Flow which triggers Twitter post after 5 mins.
          2. DONOT spam your timeline. This may result about your account may get blocked.

Step 1: Create flow

 After successful signup create an Flow and enter name and description as per your choice from  "Flow Inspector" as shown  in fig.1

Flow Inspector
fig.1


Step 2: Add things.

After creating dummy flow its time to add "Things". For our demo we're gonna add "Twitter" as our thing from the Things tab.

Select the endpoint as "Post Tweet" and add a message for post.
Add status as "Hello world"

 As shown in the fig.2.

Note: You can also search through the things and simply drag and drop. You have to add your twitter account for testing.
Coderconsole
fig.2

Step 3: Add trigger.

After adding "Things" its time to add trigger to initiate our twitter post. You can find triggers within the "Tool" tabs as shown in the fig.3

Trigger
fig.3

Step 4: Connection


Now comes the best part "Connections" .Octoblu connections are seamless. We have two points for each "trigger" and "things" simple drag from one point to another as shown in fig4.

Connections
fig.4

Step 5: Lets wrap everything up.


Now simply run the flow created above as shown in fig.5 this will make sure your flow is ready to get triggered

Note: You have to run the flow every time whenever you have made any changes

fig.5




Step 6: Trigger the flow 

Simple click the play icon on the trigger. fig.3. This is trigger your twitter post with the text as "Hello world" on your twitter profile page.

Thats It !!!

How to debug?

This will help to figureout whats happening whenever the trigger  is initiated.

To debug the flow

  1. You have to enabled "DEBUG" mode from "Thing Inspectors" by  first clicking the things on the flow first.

How To Automate?

1. To automate you can use a tool called as "INTERVAL" and can assign the triggering time as 5 minutes


So finally we have automated our twitter post using "Octoblu". It has got immense power and can come handy for demo projects involving 'Internet of things/Internet of APIS'.

For more updates follow us on -  Twitter


Last Updated: February 24, 2015

Android Sqlite Trigger Demo


Description:

Triggers are some structured code that are executed  automatically when certain events occurred in our database. Events can be like INSERT, DELETE, UPDATE.

Example: Consider a database of any University. So if any Student record is added in student table , new row(tuple) is added automatically in  library section or canteen section etc.

So by writting a simple trigger we can automatically insert new records in other sections avoiding boiler plate code.




Schema:
 CREATE TABLE student (sid INTEGER PRIMARY KEY, sname TEXT)  
 CREATE TABLE canteen (cid , sid )  
 CREATE TABLE library (lid INTEGER PRIMARY KEY, sid TEXT)  

Trigger to automatically add records in library and canteen table:
 CREATE TRIGGER if not exists add_student   
   AFTER INSERT  
 ON[student]  
   for each row  
     BEGIN  
        insert into library values (2 , new.sid );  
        insert into canteen values (3 , new.sid);  
     END;  

Explanation:The concept here is to create a trigger ,which insert the values in canteen and library based on new student id.

Trigger to delete records from library and canteen table:
 CREATE TRIGGER if not exists delete_student   
   AFTER DELETE   
 ON[student]  
  for each row  
    BEGIN  
        delete from library where sid = old.sid;  
        delete from library where sid = old.sid;  
    END;  

Explanation:The concept here is to  delete a record from student which thereby delete the values from library and canteen with old id of student.



CODE:
 public class MainActivity extends ActionBarActivity {  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     DatabaseHelper databaseHelper = new DatabaseHelper(this , DatabaseHelper.DB_NAME , null , DatabaseHelper.version);  
     databaseHelper.insertIntoStudent("100" , "Lionel Messi");  
     //Similarly if we delete any record the trigger get fired:e.g  
     //databaseHelper.deleteFromStudent("100");  
   }  
   public class DatabaseHelper extends SQLiteOpenHelper {  
     static final public String DB_NAME = "trigger_demo";  
     static final public int version =1;  
     public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {  
       super(context, name, null, version);  
     }  
     @Override  
     public void onCreate(SQLiteDatabase sqLiteDatabase) {  
       sqLiteDatabase.execSQL("CREATE TABLE student (sid INTEGER PRIMARY KEY, sname TEXT)");  
       sqLiteDatabase.execSQL("CREATE TABLE canteen (cid INTEGER PRIMARY KEY, sid TEXT)");  
       sqLiteDatabase.execSQL("CREATE TABLE library (lid INTEGER PRIMARY KEY, sid TEXT)");  
       sqLiteDatabase.execSQL(insertRecordTrigger()); // create trigger  
       sqLiteDatabase.execSQL(deleteRecordTrigger()); // delete trigger  
     }  
     @Override  
     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {  
       sqLiteDatabase.execSQL("DROP table student");  
       sqLiteDatabase.execSQL("DROP table student");  
       sqLiteDatabase.execSQL("DROP table student");  
       sqLiteDatabase.execSQL("DROP trigger add_student"); // Drop trigger  
       sqLiteDatabase.execSQL("DROP trigger delete_student"); // Drop trigger  
       onCreate(sqLiteDatabase);  
     }  
     public String deleteRecordTrigger(){  
       String deleteRecord = "CREATE TRIGGER if not exists delete_student " +  
           " AFTER DELETE " +  
           " ON[student] " +  
           " for each row " +  
           " BEGIN " +  
           "  delete from library where sid = old.sid; " +  
           "  delete from library where sid = old.sid; " +  
           " END; ";  
       return deleteRecord;  
     }  
     public String insertRecordTrigger(){  
       String insertRecord = "CREATE TRIGGER if not exists add_student "  
           + " AFTER INSERT "  
           + " ON[student] "  
           + " for each row "  
           + " BEGIN "  
           + " insert into library values (2 , new.sid );"  
           + " insert into canteen values (3 , new.sid);"  
           + " END;";  
       return insertRecord;  
     }  
     /**  
      * Insert new student record into student table which eventually fire trigger and insert record into canteen and library  
      */  
     public void insertIntoStudent(String sid , String sname){  
       ContentValues insertValues = new ContentValues();  
       insertValues.put("sid", sid);  
       insertValues.put("sname", sname);  
       SQLiteDatabase db = getWritableDatabase();  
       db.insert("student", null, insertValues);  
     }  
     /**  
      * Delete student record from student table which eventually fire trigger and delete record from canteen and library  
      */  
     public void deleteFromStudent(String sid){  
       SQLiteDatabase db = getWritableDatabase();  
       db.execSQL("delete from student where sid = '"+sid+"'");  
     }  
   }  
 }  



Output: