# 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(&#039;ember-cli/lib/broccoli/ember-app&#039;);

module.exports = function(defaults) {
...
// output files.

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

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 &#039;ember&#039;;

const StorageHandler = Ember.Object.extend({

get(name) {
console.log(&#039;store get will be called&#039;);
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.

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.

## 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
imap is        :
omap is        :
emap is        : crcrlf,delbs,

```

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 the 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, let’s get into coding. open a new file, say `gateway.py`. Let’s 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)
# write the PDU and send a ctrl+z escape
ser.write('%s\x1a' % pdu.pdu)
ser.close()
```

Next, 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

def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
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
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 functools import wraps

def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
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)
# 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
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. 😛

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.

# PORT ROLL-A-BALL UNITY TUTORIAL TO ANDROID

As unity 3d is now available for Linux, I was playing with it since then. The first tutorial you will find as a beginner in unity official site is a 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 general 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 the Unity grid system for explanations.

```float moveHorizontal= Input.GetAxis("Horizontal");
float moveVertical= Input.GetAxis("Vertical");

Vector3 movement= new Vector3(moveHorizontal, 0.0f, moveVertical);
```

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 a force to our player object. If u think in simple physics we added a force to an object which is a multiple of a vector and scaler. Does it ring a bell?? 😛

$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 the 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);
```

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 set up 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

# FIX FONT RENDERING IN NEW VERSIONS OF JETBRAINS 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.

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.

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. 🙂

references: