ADDING CUSTOM LIBRARY (store.js) TO EMBER

here is the quick snippet.
first install the library with bower. bower install store-js --save

now edit ember-cli-build.js file and import the library there.

var EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function(defaults) {
  ...
  // Use app.import to add additional libraries to the generated
  // output files.

  //Javascript files.
  app.import("bower_components/store-js/dist/store.legacy.min.js");

  return app.toTree();
};

now you can use the library anywhere inside ember by its namespace. in store.js case, it is store.

import Ember from 'ember';

const StorageHandler = Ember.Object.extend({

  get(name) {
    console.log('store get will be called');
    return store.get(name);
  }
}

CREATE YOUR OWN SMS GATEWAY

here i am gonna show you how to setup a simple sms gateway from a 3G USB dongle using python. the work-flow here is very simple. but before that let’s go through with some basics of 3G USB dongle.

in our gateway once a valid request from the internet is received to our gateway, it should communicate with the dongle and send the message included in the request to the relevant number in the request. so the most important part in this would be communicating with the dongle when the gateway receives some request.

basically your 3G dongle consists with a 3G modem and USB to serial converter. 3G modem is something like below image. to communicate with the modem, it needs a serial (RS-232) connection and this is where USB to serial converter comes to play.
as all modern laptops are consists of USBs this converter is also integrated inside the dongle so we can easily plug it and communicate. so the catch from here is that we can use RS-232 / serial to communicate with our dongle.

GSM / 3G modem module. a tiny compact module is used in a dongle
GSM / 3G modem module. a tiny compact module is used in a dongle

first let’s try to communicate with our dongle with the terminal and if it succeed we can automate that with python to use in our SMS gateway.

GSM / 3G module communication flow.
GSM / 3G module communication flow. source: http://www.engineersgarage.com/articles/gsm-gprs-modules

testing on terminal

first plug the dongle (assuming that you have inserted a sim card with credit in it πŸ˜› ) and find out the port it opened. open terminal and issue dmesg | grep tty command to find out connected USB devices.
you will obtain an output like below.

[   14.124879] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB0
[   14.126676] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB1
[   14.127795] usb 1-1.4: GSM modem (1-port) converter now attached to ttyUSB2

now, let’s try to communicate with the dongle with the serial connection (RS 232). here we have to use AT commands to communicate. you can find the complete list of AT command set GSM/ 3G modems support from here.

we will be using picocom terminal program to communicate with the dongle via serial. install it as below.

sudo apt-get install picocom

now open the serial port. make sure the substitute relevant port of the dongle.

picocom /dev/ttyUSB0 -b 115200 -l

picocom v1.7

port is        : /dev/gsmmodem
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : yes
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

try issuing AT command first. you should get an output like following if everything so far alright. πŸ™‚ issue following AT commands as well to make enable text mode for SMS.

AT
OK

AT+CMGF=1
OK

now let’s try to send a message. enter message in following format.
AT+CMGS="phonenumber"<CR>the message to send<ctrl+z>
after the number and the end of double quote hit enter, then type the message. hit ctrl + z.

it’ll give u a OK response and in a while you should receive a text message to your specified number. if all is good, its time to write our gateway piece. πŸ˜€

building the gateway

i’ll be using flask for the REST API and a python module called python-messaging for encoding text messages to PDU format.

first install those packages by:

pip install flask
pip install git+https://github.com/pmarti/python-messaging.git

now, lets get into coding. open a new file, say gateway.py. lets first write a function to open the serial port and issue relevant AT command to send the message.

from messaging.sms import SmsSubmit
import serial


def send_text(number, text, path='/dev/ttyUSB0'):
    sms = SmsSubmit(number, text)
    pdu = sms.to_pdu()[0]

    # print len(sms.to_pdu())

    # open the modem port (assumes Linux)
    ser = serial.Serial(path, timeout=1)
    # write the PDU length and wait 1 second till the
    # prompt appears (a more robust implementation
    # would wait till the prompt appeared)
    ser.write('AT+CMGS=%d\r' % pdu.length)
    print ser.readlines()
    # write the PDU and send a ctrl+z escape
    ser.write('%s\x1a' % pdu.pdu)
    ser.close()

now, let’s attach this as a response to a valid POST request. we’ll use HTTP basic auth for authentication.
1. first let’s write a simple function to check user name and password.
2. then basic auth handler to be used in POST request
3. finally the POST request

from flask import Flask, jsonify, request
from functools import wraps
app = Flask(__name__)

def check_auth(username, password):
    """check username / pass"""
    return username == 'user' and password == 'mypassword'


def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return jsonify({'message': 'not logged in'}), 404
        return f(*args, **kwargs)
    return decorated


@app.route("/")
def hello():
    return "python sms gateway :)"

# POST request handler
@app.route('/send', methods=['POST'])
@requires_auth
def create_task():
    if not request.json or not 'number' in request.json:
        return jsonify({'message': 'no number provided'}), 400

    send_text(request.json['number'], request.json['text'])
    return jsonify({'message': 'message queued'}), 201

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=4000)

so the full code for the gateway will look like below.

gateway.py

from messaging.sms import SmsSubmit
import serial
from flask import Flask, jsonify, request
from functools import wraps
app = Flask(__name__)


def check_auth(username, password):
    """check username / pass"""
    return username == 'user' and password == 'mypassword'


def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        auth = request.authorization
        if not auth or not check_auth(auth.username, auth.password):
            return jsonify({'message': 'not logged in'}), 404
        return f(*args, **kwargs)
    return decorated


def send_text(number, text, path='/dev/ttyUSB0'):
    sms = SmsSubmit(number, text)
    pdu = sms.to_pdu()[0]

    # print len(sms.to_pdu())

    # open the modem port (assumes Linux)
    ser = serial.Serial(path, timeout=1)
    # write the PDU length and wait 1 second till the
    # prompt appears (a more robust implementation
    # would wait till the prompt appeared)
    ser.write('AT+CMGS=%d\r' % pdu.length)
    print ser.readlines()
    # write the PDU and send a ctrl+z escape
    ser.write('%s\x1a' % pdu.pdu)
    ser.close()




@app.route("/")
def hello():
    return "python sms gateway :)"


@app.route('/send', methods=['POST'])
@requires_auth
def create_task():
    if not request.json or not 'number' in request.json:
        return jsonify({'message': 'no number provided'}), 400

    send_text(request.json['number'], request.json['text'])
    return jsonify({'message': 'message queued'}), 201

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=4000)

now run the gateway by issuing python gateway.py. our gateway server now would be running on http://localhost:4000 in the host machine. next let’s use postman to send a test request and ensure that our gateway is working. download postman from chrome web store if you don’t have it installed.

format the request as below. πŸ˜›

postman_1

postman_3

if all is good, you should now receive a text from the dongle.

you can find the complete source code of the project from https://github.com/wathmal/sms-gateway.

references:
* https://myraspberryandme.wordpress.com/2013/09/13/short-message-texting-sms-with-huawei-e220/
* https://github.com/pmarti/python-messaging/blob/master/doc/tutorial/sms.rst

PORT ROLL-A-BALL UNITY TUTORIAL TO ANDROID

as unity 3d is now available for linux <3 i was playing with it sice then. the first tutorial you will find as a beginner in unity official site is roll-a-ball game which you can find in here. i assume you have completed this and willing to port this to android or ios which is the main intention of this post.

you can build and run this on pc or mobile devices. but if you try to run this on android or a mobile device your player object won’t move as you moved it in pc. this is because we want to configure our player object to use accelerometer or gyroscope data from the mobile device as inputs.

what we did on the player controller script is we took the keyboard input of horizontal and vertical axis and created a 3d vector with having 0 as y component. one thing to make a note here is that in generally in vectors we are considering z axis as the axis which stands vertically from xy plane which governs up/ down movements. but in unity 3d y axis is the one which governs up/down movements. from here on i will use unity grid system for explanations.

unity axis vs cartesian axis
difference between unity axis and cartesian axis
float moveHorizontal= Input.GetAxis("Horizontal");
float moveVertical= Input.GetAxis("Vertical");

Vector3 movement= new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement* speed);

what we did here was first we generated a 3d vector from keyboard inputs for x and z movements. we made y axis as 0 as we don’t want our player object to move vertically or up/down. then we add the vector multiplied by a scalar as force to our player object. if u think in simple physics we added a force to an object which is a multiply of a vector and scaler. does it ring a bell?? πŸ˜›

\large F = m.\dot{a}

we created an acceleration vector from keyboard inputs, and multiplied by the mass and add that force to the player object. the more you increase the mass more force is added, so the rate of change of object’s position will increase.

so the only thing we have to do here is get the acceleration from the mobile device and add it to our acceleration vector. unity’s interface to the input system provides this natively. you can get the acceleration vector from Input.acceleration. as we want to make y axis acceleration zero we can do following.

Vector3 movement= new Vector3(Input.acceleration.x, 0.0f, Input.acceleration.y);
rb.AddForce(movement* speed);

note that here in Input.acceleration.y provides the linear acceleration of y axis in cartesian grid system. we are all set. switch build platform to android and hit build and run. you may need to setup a bundle identifier for the app if you are switching platforms for the first time.

now you can play the game by tilting your phone. πŸ™‚
references:
https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial
http://answers.unity3d.com/answers/887453/view.html

FIX FONT RENDERING IN NEW VERSIONS OF JETBRAIN IDE UBUNTU

if you try to install new versions of any jetbrain’s IDEΒ s you may have experience that fonts are not rendering well like previous versions. you may see fonts are rendered in ubuntu’s default fonts and not in jetbrain’s font set.

jetbrains ide clion font not rendered
jetbrains ide clion font not rendered

root permission method

if you try to run the IDE as root this won’t be happen, the fonts will render perfectly. so the solution here is to edit the desktop entity of the IDE to run as root. for that we need gksuso installed first.

sudo apt-get install gksu

if you haven’t already created a desktop entity create a one through configure -> create desktop entity. then we have to edit the created .desktop file in order to run the IDE as root everything you run in from unity dash. typically .desktop files are stored in /usr/share/applications or in ~/.local/share/applications. check whether your corresponding desktop files lies in there. if so open it with gedit or your preferred text editor.

add gksudo after Exec= entry and save your .desktop file. edited file may looks like following.

[Desktop Entry]
Version=1.0
Type=Application
Name=CLion
Icon=/opt/clion-1.0.3/bin/clion.svg
Exec=gksudo "/opt/clion-1.0.3/bin/clion.sh" %f
Comment=Develop with pleasure!
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-clion

now every time you launch the IDE you will be prompted to enter you root password and you can see the fonts are rendered so much perfect than before. hope entering your password one time won’t be a pain as now you can work with your IDE more nicely than before.

font perfectly shown in jetbrains clion ide
font perfectly shown in jetbrains clion ide

update 2015-09-01

instead of adding root permissions to the IDE you can change the IDE’s font to default font which is used by all jetbrains IDEs. default font used by the IDE in ubuntu is dejavu-sans 12px. goto settings -> appearance, check override default fonts and select dejavu-sans 12 px as below and you are ready to go. πŸ™‚

override font to dejavu-sans 12px to fix font rendering isse in new jetbrain ides
override font to dejavu-sans 12px

references:
http://askubuntu.com/a/484413/378106