Tuesday, May 12, 2015

Google Summer of Code 2015 : FOSSASIA

I've got selected to Google Summer of Code 2015. This time I'm developing a library to support knitting machines. It's great to work with FOSSASIA team under awesome mentors Mario Behling Eddie von Altenberga , Christian Obersteiner and Aruna Herath

This is the Initial suggestions for the project.

Library to support knitting machines

Basic abstraction of the library

I would like to create library as separate layers

  • Serial communication : open, configure, read and write to serial port
  • Machine definitions : machine initialization, load to machine, save from machine
  • Image handling functions : memory allocation, read image file, setter and getter for image pixels
  • File handling functions : read / write access
  • Common utility functions

Add dependencies for the library

There are several dependency libraries which ayab has such as pillow, pyserial, wsgiref, fysom and yapsy with relevant versions. These dependencies should be added to the library. 

Functions for the library

For access hardware directly
       * Identify existing libraries to interface with hardware
       * Use libraries to deal with serial ports
       * Ability to get all the functions via library (for an example functions in ayabControl.py and add more functions to support in more machines)
       * Ability to send QT signals

Emulate file formats

       * Create functions to emulate file format

Add Arduino support


Installation should be well documented in step by step. Additionally I suggest to add GUI based installation with usability improvements with standard installation procedures. 

* Installation on Linux (32bit, 64bit)
       * Prerequisites
       * Setup
* Installation on Windows (32bit, 64bit)
       * Prerequisites
       * Setup

Tests on the library

Use a python test framework like unittest to test the python library. It supports test automation and aggregation of the tests into collection.

Documentation of the library 

Documenting Class definitions, methods definitions and links to the given source codes. 


Source Code

Thursday, February 12, 2015

Library Management System: Database Project - Part V - Finalize Database

  Library Management System for Imaginary University
  2nd year Database Project

In order to maximize the use of system resources to perform work as efficiently and rapidly as possible the views and indices to be used were confirmed and some of the views were decided to materialize.

In the application a user can search a book based on its authors, title and subject as well as ISBN number. Therefore by creating indices on tilte, authors and subjects the database can access relevant entries more efficiently. Since the table book_details is not updated frequently this will not cause for any low performance of the database.

create index book_title on book_details(title);
create index book_publisher on book_details(publisher);
create index book_category on book_details(category);

create index cd_title on cd_details(title);
create index cd_publisher on cd_details(publisher);
create index cd_category on cd_details(category);

create index member_index on borrows(member_index);

According to the design of our database it was required to join several tables in order to fulfill search requests. For example when finding a book by its ISBN number we happened to join book_details table with authors table to get details of the book. In addition, to find the availability of that book it was necessary to join item table and book table with previous tables making total number of tables to be joined is four. Searching a book is one of main functionalities and that is done frequently, joining tables at each search is highly expensive execution. Therefore it was more convenient to make a view joining necessary tables as follows and materialize the view since it is not updated very frequently.

CREATE VIEW bookSearch AS SELECT items.id, bd.ISBN, bd.title, bd.edition, bd.publisher, bd.category, authors.author_name_1, authors.author_name_2, authors.author_name_3, items.borrowed
FROM items, books, authors, book_details AS bd
WHERE items.id = books.item_id
AND books.ISBN = bd.ISBN
AND authors.ISBN = bd.ISBN;

Similarly a view to read data in joined tables, item, cd and cd_details was created

CREATE VIEW cdSearch AS SELECT items.id, cd.ISSN, cd.title, cd.edition, cd.publisher, cd.category,items.borrowed
FROM items, cds,cd_details AS cd
WHERE items.id = cds.item_id
AND cds.ISSN = cd.ISSN ;

Following views were created to search member details efficiently

CREATE VIEW memberSearch AS SELECT s.student_index, m.first_name, m.last_name, m.email,
PASSWORD , s.batch, m.department, s.registered_date, m.number_of_books_allowed, s.total_fine
FROM students AS s, members AS m
WHERE s.student_index = m.member_index;

CREATE VIEW staffSearch AS SELECT s.staff_index, m.first_name, m.last_name, m.email,
PASSWORD ,s.position, m.department, s.year_of_appoinment, m.number_of_books_allowed
FROM staff AS s, members AS m
WHERE s.staff_index = m.member_index;

To ensure the persistence and security of data backup capabilities were added to the software.

Book Search Window

Wednesday, February 11, 2015

Library Management System: Database Project - Part IV - Database Implementation

  Library Management System for Imaginary University
  2nd year Database Project

Data Definition Language (DDL) of the database.

create table items
     id                  INT PRIMARY KEY,
     borrowed            boolean,
     received_as         varchar(50)    
create table book_details
     ISBN                varchar(15) PRIMARY KEY,
     title               varchar(50) not null,
     edition             varchar(15),
     publisher           varchar(50),
     category            varchar(50),
     price               float(7,2)
create table books
     item_id         int PRIMARY KEY,
     ISBN            varchar(15) ,   
     foreign key (item_id) references items(id),
     foreign key (ISBN) references book_details(ISBN)

create table cd_details
     ISSN                 varchar(15) PRIMARY KEY,
     title                varchar(50) not null,
     edition              varchar(15),
     publisher            varchar(50),
     category             varchar(50),
     price                float(7,2)

create table cds
     item_id           int PRIMARY KEY,
     ISSN              varchar(15) ,
     foreign key (item_id) references items(id),
     foreign key (ISSN) references cd_details(ISSN)

create table members
     member_index       varchar(10)  PRIMARY KEY,
     first_name         varchar(50) not null,
     last_name          varchar(50) not null,
     department         varchar(20) not null,
     password           varchar(15) not null,
     email              varchar(50),
     number_of_books_allowed int

create table students
     student_index      varchar(10)  PRIMARY KEY,
     batch              varchar(5) not null,
     total_fine         int,
     registered_date   date,
     foreign key (student_index) references members(member_index)
create table staff
     staff_index           varchar(10)  PRIMARY KEY,
     year_of_appoinment    DATE,
     position              varchar(20) not null,
     foreign key (staff_index) references members(member_index)
create table authors
    ISBN                  varchar(15)  PRIMARY KEY,
    author_name_1         varchar(20) not null,
    author_name_2         varchar(20),
    author_name_3         varchar(20),
    author_name_4         varchar(20),
    foreign key (ISBN)    references book_details(ISBN)

create table borrows
     member_index         varchar(10),
     item_id              int,
     borrowed_date        DATE not null,
     fine                 float(7,2),
     remarks              varchar(50),
     foreign key (member_index) references members(member_index),
     foreign key (item_id) references items(id),
     primary key (member_index,item_id)
create table locations
     id                INT PRIMARY KEY,
     shelve_ID         varchar(6),
     row_number        int,
     section           varchar(15) not null
create table item_is_at
     ISBN             varchar(15) PRIMARY KEY,
     location_id      int,   
     foreign key (location_id) references locations(id),
     foreign key (ISBN) references book_details(ISBN)
create table operator
    (id               varchar(10) PRIMARY KEY,
     username         varchar(50) not null,
     password         varchar(15) not null,
     email            Varchar(128) not null,
     position         varchar(64)    

Library Management System: Database Project - Part III - Database Design

  Library Management System for Imaginary University
  2nd year Database Project

There are Four main entities

Item : a book or as CD which can be borrow from the library
Member : a registered member of the library who will be a student or a staff member
Location : the physical location where an item is stored in the library
Operator : a library staff member who will interact with members of the library. That is they will add/update data about members, transactions and items in the library

Entity Relationship Diagram (ER Diagram)

ER diagram

There can be many copies of the same item (Book or CD). However since we need to identify each item separately so that each of them are given a unique id which is the 'item_id'.
Storing common details of the same copies of items make redundancy, the details of items are decomposed into separate tables named 'book_details' and 'CD_details'.

A member can borrow four books (or CDs) at a time and an item can be borrowed by only one member at a time. Therefore the relation between items and members is many to one.

The relationship, borrows, will record details of a transaction and stored as the history of each transaction. The attribute, returned date of the relationship borrows will be null until the item is returned.

Books and CD’s are stored physically in the place designated by the location. A location can be uniquely identified by the location_id which includes shelf number, row number and section number. Therefore the combination of those three attributes will be the primary key for the entity, location_id. However since there can be about 10-20 books in a section of a shelf the relationship, item_location is many to one from item to location.

Operator entity is used in authenticating operators when using the system, which is essential to confirm security of the system. That does not have any relationship with other entities.

All the entities are checked and confirmed to be normalized in the first and third normal forms.

Database Schema

Database schema

Library Management System: Database Project - Part II - Functional & Non Functional Requirements

  Library Management System for Imaginary University
  2nd year Database Project

Functional Requirements

  • Add/delete/update details of a member for the library staff.
  • Add/remove/change privileges of library staff to access the system
  • All functionalities of an operator ( mentioned below )
  • Add/delete/update book details (location)
  • Issue/accept lent books
  • Issue fine receipts
  • Search books (for location, availability, other related books, etc.)
  • Check user history
  •  only allowed to view data about resources available in the library

Non Functional Requirements

Data validation
Data validation is having valid data for a given type of variable. Data must be validated to ensure the consistency of data.

The security to the data base from unauthorized access is prevented by proper authentication giving access privileges for users. In addition security from SQL injection is prevented at the implementation using HTML methods and php (eg: using php function like mysql_real_escape_string(trim($variable) )

Both the throughput and low response time is achieved by making proper views and indices. They are explained later in the report.

User friendly interfaces are provided. Also in order to prevent mistakes from users proper warnings and alerts are implemented.

Library Management System: Database Project - Part I - Introduction

  Library Management System for Imaginary University
  2nd year Database Project

Database management is a wide area of computer science which is essential in most of the applications in order to store and handle data proper and efficient way. Library management system is one of a most frequently used application of database management systems where there is a chunk of information to store and record daily.


University of ABC is a imaginary university of about 12000 students and 1000 of staff. The library system of the university facilitates all students, academic and non academic staff by providing access to the library. Anyone of the university can read books or other documents at the library, but in order to borrow them one should register and get the membership. The membership is valid only for a year and that should be updated afterwards. After getting the membership, a member can borrow 4 books at a time and maximum burrowed period is 2 weeks. Users must return books on or before due date, otherwise they are fined. The fine is rs.5.00 per book for the first week and rs.10.00 per day for further delays. In order to facilitate its users to the fullest University of ABC has decided to keep a database and automate the library system.


Main purpose of managing a database system is to facilitate the users of the library in an efficient way. There are three main requirements of the University for the library system.
  • Store information about books and users
  • Search books
  • Reporting member profiles

Monday, February 9, 2015

Powering my home by Solar Energy

I am interesting in renewable energy area from my childhood. Sri Lankan government introduced grid solar power system for residential few years ago. I was able to install grid solar power system at my home in 2014  with my uncle's help who has great knowledge about electric systems.

Mainly there are two types of Solar Systems i.e off grid and grid tied.

DC off grid system

rarely use [most electric components are capable only for Alternative Current (AC)]


AC off grid system

Inverter converts Direct Current (DC) to Alternative Current (AC). You can use a generator if you want to get power when battery bank is dead.

grid system with batteries

With a Battery bank you can have current to your home when distribution line power cut. you cant do that without a battery bank. because extra power should be store somewhere (in a battery bank).

grid system with batteries

I am using this system (without a battery bank). At Day time we are using the solar power and rest sends to the distribution line (export). If the solar power is low then get the current from normal distribution line (import).  

Lets say you have exported 300 units at the end of month and imported 400 units, so you have to only pay for 100 units. Otherwise (export = 400 , import = 300) then you have extra 100 units in your hand and you can use that extra units at other circumstances (some months have rainy season so import units can be higher)

There are 24 solar panels at my house's roof, each panel produces 185W to power up my home by solar energy. setup is not much difficult. It is similar to normal battery setup. Lets say you have two packs of 12 batteries. 12 batteries of each pack is connected series. 2 Packs gives powers separately.

grid solar system circuit diagram at my home

Be carefull
There are two types of solar panels in market i.e. monocrystalline and polycrystalline. Make sure to buy monocrystalline solar panels though it has high initial cost.

Currently I am getting 20 units average (20 kwh) for a good sunny day. For a rainy day its reduced to 10 units.

In my home roof is faced to East and West. For this kind of scenario best way is to have two sides are separately to produce power. Morning to 12'o clock Right hand side produce much energy than left also 12'o clock noon to Evening left produce much than right. If we put all sets as one it will drop the production.


Lets say Morning to 12'o clock Right hand side produce average of 1900 W and left is 1000 W, total production is 2900 W (1900+1000).

if we set both side as one, total production will be reduced to the minimum side

production = minimum {Left , Right} * 2 = 1000 W * 2= 2000 W

Wednesday, January 28, 2015

AngularJS for very Beginners

"AngularJS" also referred to as "Angular" is an open source structural framework for dynamic web apps.

AngularJS is created by Google and the community of individual developers. AngularJS is a MVC based framework which reduces much of the code by using dependency injection and data binding. 

To use Angular you need to download its framework from here. Get the stable and minified version of the framework. This will be the reference for the script we are going to create.

1.    <html ng-app>

2.    <script src="angular.min.js"></script>

3.    Text : <input ng-model="textName">
4.    <br/>

5.    {{textName}}

6.    </html>

Explanation of the code

1.   "ng-app": saying html to use angular

2.   set reference for the script (angular.min.js is included in same folder of test.html)

3.   "ng-model": Model to the input

4.   "{{textName}}": View of the input 



We looked at Model and View architecture, Lets look at Controller in action !

1.   <html ng-app>
2.   <script src="angular.min.js"></script>

3.   <button ng-click="num=num+1" ng-init="num=0">Add One</button>

4.   Number: {{num}}
5.   </html>

Explanation of the code

3.   "ng-click": click handler for Angular   ::  "ng-init": initial value of num


Tuesday, January 27, 2015

What is Dependency Injection & Polymorphism

" Dependency injection is the way to decouple conventional dependency relationships between objects. "

What is meant by Dependency ?

Suppose there are two classes A and B. A can not carry out works without B and A can not be reused without re using B. For this kind of scenario we said " A depends on B "

A – Dependant
B – Dependency

What Is meant by Decouple ?

A and B classes are decoupled if A can be use without B and and B can be used without A.

Lets assume creating a drawing application with Square , Triangle and Application classes. Square and Triangle class has same method name called draw() which draws square for a Square object and triangle for Triangle object.

Polymorphism to rescue !

The Greek meaning of the word "polymorphism" is "having different forms". Same object having different object behaviors ! 

Added interface called Shape and the common method draw() for both Triangle and Square. Square and Triangular classes implements Shape interface.

Shape triangular = new Triangular();
Shape square = new Square();

Lets Inject Dependencies !

Created a new class called DrawingTool and added a private member of Shape type (shape) and a setter for that. Created a method called drawShape(). Note that in DrawingTool class does not initiate any object. DrawingTool assumes that It will be provided a initiated object. 

Advantage !

By using this method we can separate dependency from a whole class (DrawingTool) which does not know what to draw. Lets say if we want to draw a triangle we don't have to modify the DrawingTool class. Only thing to do is pass a triangular to the setter. 

Dependency to the triangle that the DrawingTool class has injected by the Application class. This is the way of injecting dependency.

Basic git commands for beginners with examples

Git is a popular version control system (VCS). Wait , what is a Version Control System ?

" Version Control System keeps track of the records changes to a set of files over time " 

Why we need Git or any other VCS ? Only to keep the code in a remote repository as a backup ? Is VCS a waste of time ? Lets find answers for these questions.

When we are creating individual projects we may not want to use a VCS. But VCS is very useful for team projects.

Imagine you are working on a team project without using a VCS. Each of your team members given individual tasks and plan is to combine those tasks at the end. But practically it would be a very difficult. There can be lots of redundant of codes, redundant of libraries,  sometimes combining costs lots of time and even may be unable to combine.

Imagine a scenario in which everybody can work under a same framework, every one do there part and add to a common repository so that different individuals can see who has committed (added the code),  when it is included, what are the changes etc. Life is so easy !

This is precisely what Version Control System capable for.

You always have a updated local repository (repository in your computer) of the remote repository (repository in VCS network) (pull). Changes you've made is first effects in your local repository. Then push(add) those changes to remote repository. 

Basic git commands

git branch : show which branch you are currently working

list down branches working on. start symbol shows the current branch working on

git status : current status of your local repository (modified files)

modified file with the path is shown red color

git diff path-to-file : show what are the changes of modified file

removed line of the file is shown with red color

git reset --hard HEAD : reset local repository

HEAD is now reset to current fix AAERESAA-19915

less .git/config : configure git configurations

git pull : update local repository from remote repository (pull changes from  remote repository to local)

git checkout – –track path-to-branch : changes the working path

git reset – –soft shaID : reset the state until given shaID

git add path-to-file : stage the file (ready the file to commit)

git commit -m “commit-message” : commit the file

git push origin branch-name : push to remote repository

gitk : shows the git repository browser