I made this bash script so that I can use any IDE for developing imp code. When you run the script, it will build the code and run it on the device/model of your choosing. I have it setup to run automatically everytime I save the device file. WARNING: this will of course overwrite the model of your choosing in the ide, so make sure you have the code for the model in the web ide saved somewhere (you probably already do)
Pre-reqs:
*Firefox
*node.js, npm (apt-get install node, and jsesc (npm install -g jsesc)
*curl (apt-get install curl)
The only reason it requires jsesc is to escape quotes and new lines. You can use sed or another tool of your choosing, this was just easiest for me. The same concept can be adapted to work for windows.
Steps:
- Login to ide.electricimp.com in firefox (you may close it after logging in, just dont log out)
- Set the environment variable FFX_PROFILE to the directory location of your firefox profile (mine is ~/.mozilla/firefox/mwad0hks.default)
- Run this script like so ./impide {model} {device} {agent code file} {device code file}
a) If you refresh the ide page, you will see the code from your local files there!
b) {model} and {device} are integer ids that can be seen in the URL when you have a model/device selected in the ide. - (optional) Run a when-changed script to run this script everytime a file is saved. Here’s one I use: https://github.com/joh/when-changed
Here’s the script:
`
#!/bin/bash
Warning: hack
Get’s around the imp ide. This script will send out the post request that builds
device and agent code and sends it through the electric imp service to be built
and run.
You must first open firefox and login to the ide there. The session cookie is stolen
from firefox! Make sure to have the env var FFX_PROFILE set to the profile dir.
eval ffxProfile=$FFX_PROFILE
model=$1
device=$2
agentCode=cat $3 | jsesc --double-quotes
impCode=cat $4 | jsesc --double-quotes
impToken=sqlite3 $ffxProfile/cookies.sqlite "SELECT value FROM moz_cookies WHERE baseDomain='electricimp.com' and name='imp.token';"
curl “https://ide.electricimp.com/ide/v3/models/$model/code” \
-H “Cookie: imp.token=$impToken” \
-H ‘Origin: https://ide.electricimp.com’ \
-H ‘Accept-Encoding: gzip,deflate,sdch’ \
-H ‘Accept-Language: en-US,en;q=0.8’ \
-H ‘User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36’ \
-H ‘Content-Type: application/json’ \
-H ‘Accept: application/json, text/javascript, /; q=0.01’ \
-H “Referer: https://ide.electricimp.com/ide/models/$model/devices/$device” \
-H ‘X-Requested-With: XMLHttpRequest’ \
-H ‘Connection: keep-alive’ \
–data-binary “{“device_id”:”$device",“imp_code”:"$impCode",“agent_code”:"$agentCode"}" --compressed
`