3
ESCUELA TÉCNICA SUPERIOR DE INGENIERÍA INFORMÁTICA
GRADO EN INGENIERÍA DEL SOFTWARE
MODELOS EMERGENTES PARA EL DESARROLLO DE
APLICACIONES MÓVILES SOCIALES: PEOPLE AS A
SERVICE Y SOCIAL DEVICES. PRUEBA DE CONCEPTO.
EMERGING MODELS FOR THE DEVELOPMENT OF SOCIAL
MOBILE APPLICATIONS: PEOPLE AS A SERVICE, AND
SOCIAL DEVICES. A PROOF OF CONCEPT.
Realizado po
JOSÉ ANDRÉS CORDERO BENÍTEZ
Tu o izado po
CARLOS CANAL VELASCO
Depa amen o
LENGUAJES Y CIENCIAS DE LA COMPUTACIÓN
UNIVERSIDAD DE MÁLAGA
MÁLAGA, no iemb e 2014
Fecha de ensa:
El Sec e a io del T ibunal
5
Resumen: Ac ualmen e los disposi i os mó iles han ganado una g an impo ancia
en nues a sociedad y es án p esen es en cualquie luga . Es os disposi i os ienen una
g an can idad de in o mación sob e sus dueños y da os con ex uales de sus
al ededo es, y muchos canales de comunicación. No obs an e con los modelos ac uales
los disposi i os mó iles no pueden ob ene en aja de es a si uación.
Po es os mo i os, han comenzado a su gi ecien emen e nue os modelos de
aplicaciones mó iles. People-as-a-Se ice (PeaaS) pe mi e in e acciones en e
disposi i os mó iles ce canos, usando pe iles sociológicos de sus dueños pa a c ea
comunicaciones en e disposi i os in e esados. Social De ices es un modelo ecien e
que implemen a el concep o de PeaaS, ap o echando que los disposi i os es án casi
siemp e con el usua io c ea una nue a línea de in e acciones en e los disposi i os
ce canos con el obje i o de aumen a las elaciones sociales. Pa a ello cada disposi i o
es capaz de p o ee y consumi se icios de disposi i os ce canos, man eniendo la
in o mación del usua io en el p opio disposi i o y compa iéndola solamen e cuando
es é au o izado. Es e concep o ambién puede in eg a se con ecnologías como
In e ne -de-las-Cosas y la Compu ación Ubicua, pe mi iendo que los disposi i os se
conec en con o os obje os ce canos ales como ascenso es, semá o os o u p opio
ehículo.
En pa icula exis e una pla a o ma que implemen a ambos concep os, y que es el
obje o de es udio de es e abajo: O ches a o JS. La pla a o ma aún se encuen a en
desa ollo y no es demasiado es able, además de no con a con ninguna
documen ación. El obje i o de es e abajo es explo a la pla a o ma y c ea una
documen ación básica que pueda se u ilizada po cualquie a que es é in e esado en
ella, ac uando como pun o de pa ida pa a u u os abajos.
Palab as cla e: Social De ices, People-as-a-Se ice, Coope ación basada en
p oximidad, Compu ación Ubicua, O ches a o JS.
6
Abs ac : Nowadays mobile de ices ha e gained a g ea impo ance in ou socie y
and hey a e p esen e e ywhe e. These de ices ha e a huge amoun o da a abou
hei owne s and con ex da a o hei su oundings, and many communica ion channels.
Ne e heless wi h cu en models mobile de ices canno ake ad an age o his
si ua ion.
Fo hese easons, new models o mobile applica ions ha e appea ed ecen ly.
People-as-a-Se ice (PeaaS) allows in e ac ions be ween mobile de ices in hei
su oundings, using sociological p o iles o hei owne s o c ea e communica ion
be ween in e es ed de ices. Social De ices is a ecen model ha implemen s he
PeaaS concep , aking ad an age he de ices a e almos always wi h he use i c ea es
a new kind o in e ac ions be ween nea by de ices wi h he main a ge o imp o e social
ela ions. Fo his pu pose each de ice is able o p o ide se ices o nea by de ices and
consume hem, keeping he use ’s da a in he de ice and sha ing i only when i is
au ho ized. This concep could be in eg a ed wi h echnologies like In e ne -o -Things
and Ubiqui ous Compu ing, allowing de ices o connec wi h nea by objec s like
ele a o s, a ic ligh s o you own ehicle.
In pa icula , he e is a new pla o m ha implemen s bo h concep s, which is he
s udy objec o his wo k: O ches a o JS. The pla o m is s ill unde de elopmen and is
no oo s able ye , u he mo e he e is no documen a ion a ailable. The main a ge o
his wo k is o explo e he pla o m and o c ea e a basic documen a ion which could be
used o anybody in e es ed on he in, ac ing as a s a ing poin o u u e wo ks.
Keywo ds: Social De ices, People as a Se ice, P oximi y-based Coope a ion,
Ubiqui ous Compu ing, O ches a o JS.
7
Index
INDEX ............................................................................................................................................... 7
INTRODUCTION AND GOALS ........................................................................................................ 9
CHAPTER 1: PEOPLE-AS-A-SERVICE ........................................................................................ 13
CLOUD-ENABLED MOBILE DEVICES ................................................................................................ 13
PEOPLE-AS-A-SERVICE .................................................................................................................. 15
PEAAS ARCHITECTURE ................................................................................................................. 16
CURRENT IMPLEMENTATIONS ......................................................................................................... 18
THE INTERNET OF PEOPLE ............................................................................................................. 22
INTEGRATION WITH SOCIAL DEVICES AND INTERNET OF THINGS ...................................................... 26
CHAPTER 2: SOCIAL DEVICES ................................................................................................... 31
THE ACTION-ORIENTED PROGRAMMING MODEL ............................................................................. 32
REQUIREMENTS AND CHARACTERISTICS OF SOCIAL DEVICES .......................................................... 37
ARCHITECTURE ............................................................................................................................. 39
ORCHESTRATOR.JS ....................................................................................................................... 40
TOOLS .......................................................................................................................................... 41
GETTING THINGS WORK ................................................................................................................. 49
ANDROID CLIENT ........................................................................................................................... 52
CHANGES AND ADDITIONAL FEATURES ............................................................................................ 54
GOALS.......................................................................................................................................... 56
EXAMPLE: THE COFFEE BREAK MACHINE ....................................................................................... 57
CHAPTER 3: RELATED TECHNOLOGIES .................................................................................. 61
ANDROID PROGRAMMING .............................................................................................................. 61
MONGODB ................................................................................................................................... 67
NODE.JS ....................................................................................................................................... 70
CHAPTER 4: GETTING STARTED WITH SOCIAL DEVICES ..................................................... 75
DEPENDENCIES ............................................................................................................................. 75
INSTALLATION ............................................................................................................................... 76
GETTING STARTED ........................................................................................................................ 79
EXECUTING A FIRST ACTION ........................................................................................................... 82
STARTING THE DEVELOPMENT ........................................................................................................ 83
CHAPTER 5: APPLICATION HITHERE! ....................................................................................... 89
CHAPTER 6: APPLICATION EGREETINGS ................................................................................ 97
CHAPTER 7: APPLICATION FOLLOWME ................................................................................. 103
CHAPTER 8: APPLICATION HAVEYOUMET ............................................................................ 109
CHAPTER 9: APPLICATION TREASUREHUNT ........................................................................ 115
CONCLUSIONS AND FUTURE WORK ...................................................................................... 119
CONCLUSIONES Y TRABAJO FUTURO ................................................................................... 121
ANNEX 1: NPM MODULES ......................................................................................................... 123
REFERENCES ............................................................................................................................. 127
8
9
In oduc ion and goals
Nowadays sma de ices ha e an excellen po en ial and connec i i y, and can be
used o mul iple pu poses: en e aining, socializing o sha ing in o ma ion. We ha e
applica ions capable o do e e y hing wi h he help o web se ices and cloud
compu ing. Howe e , we ha e cu en ly some limi a ions.
These days e e y hing is some hing-as-a-se ice. We a e used o So wa e-as-a-
Se ice and Pla o m-as-a-Se ice ha ha e been well accep ed by he indus y. Now,
when he usage o mobile de ices ha e become cus oma y o he majo i y o
popula ion, p obably i is he momen o a new model. Mobile de ices a e becoming he
mos impo an piece in he communica ions, and hey con ain a lo o in o ma ion abou
he owne . We could use his ad an age o c ea e a new ype o communica ion, whe e
de ices in e ac wi h o he de ices as people would do.
The goal o his wo k is o lea n abou a new model, called People-as-a-Se ice
[18][19], whe e mobile de ices can deploy se ices o in e ac wi h o he de ices in hei
su oundings. In pa icula , his wo k s udies he pla o m called Social De ices,
de eloped as a esea ch wo k by he Uni e si y o Tampe e in conjunc ion wi h Nokia
[26]. This pla o m is cu en ly in de elopmen and he e is no documen a ion o u he
in o ma ion a ailable.
The main a ge o his wo k is o analyse he po en ial o he pla o m Social De ices,
o s udy all he capabili ies and c ea e a base documen a ion abou i ha could be used
o u u e wo ks. I ha e c ea ed some case s udies o s udy he po en ial and capabili ies
o he pla o m, which a e comple ely documen ed o p epa e he way o u u e p ojec s.
All he e o has been concen a ed in esea ching and unde s anding he pla o m,
documen ing e e y hing p ope ly o acili a e he wo k o u u e p ojec s using his
pla o m and echnology.
The pla o m is no inished and he e is a long way o in eg a e e e y hing, I ha e
been collabo a ing wi h Niko Mäki alo and his eam in he Uni e si y o Tampe e
imp o ing some aspec s o he pla o m and he And oid clien . The code is public o
e e yone and can be accessed in he nex link:
h ps://gi hub.com/nikkis
This wo k uses NodeJS
1
and Py hon
2
as basic echnologies o de elop new
capabili ies and unc ionali y in he pla o m. The se e se ice, called O ches a o JS is
cu en ly de eloped in NodeJS, and he capabili ies in Py hon. On he o he hand, he
clien is ocused in mobile de ices, so we use Ja a as main echnology o de elop he
clien unc ionali y o And oid de ices.
1
h p://nodejs.o g/
2
h ps://www.py hon.o g/
10
This documen is di ided in o h ee main pa s:
- The i s pa includes he chap e s om 1 o 3, which a e a b ie in oduc ion o
he echnologies used in his wo k: Cloud Compu ing
1
, specially he model
People-as-a-Se ice and he Ac ion-O ien ed P og amming Model [1], which a e
used as basic a chi ec u e o he Social De ices pla o m. The se e is
de eloped using Node.js [35], and connec ed o a MongoDB [32] Da abase.
And oid P og amming [4] is he echnology used in he clien .
- The second pa con ains all he ele an in o ma ion o unde s and he Social
De ices Pla o m and O ches a o JS. I explain in Chap e 4 how he pla o ms
beha es, hei a chi ec u e and he base o knowledge. Also I explain how o
p epa e and ins all he en i onmen o s a he de elopmen .
- The hi d pa includes he chap e s om 5 o 9, one chap e o each case s udy.
I ha e de eloped 5 small applica ions ha can be used as a p oo o concep o
see wha he pla o m can o e . Each chap e con ains all he documen a ion o
use he applica ion and de ails abou i s implemen a ion.
Social De ices has cu en ly wo unc ional pla o ms implemen ing he concep . In
chap e 4 we will see he i s implemen a ion, called Social De ices Pla o m (SDP) as
an in oduc ion o he new pla o m O ches a o .js (OJS) [25].
1
h p://en.wikipedia.o g/wiki/Cloud_compu ing
11
Mo i a ion
This p ojec is aligned wi h he s a egic objec i es o he esea ch g oups SCENIC,
om he Uni e si y o Málaga, and QUERCUS, om Ex emadu a on Mobile Cloud
Compu ing and ela ed wi h he esea ch p ojec s SeaClouds
1
and SOFIA
2
, in which is
cu en ly wo king he Uni e si y o Málaga as many o he uni e si ies ac oss Eu ope.
These esea ch g oups a e ocused in cloud echnologies and mobile compu ing.
Cloud Compu ing is a new concep and he e is a long u u e o i . I educes ime- o-
ma ke and p o ides on-demand scalabili y a a low cos o he business and a new
concep is being de eloped: People-as-a-Se ice.
PeaaS is a new echnology in de elopmen and he e a e only a ew pla o ms ha
in eg a es he concep . The pu pose o his p ojec is o c ea e a p oo o concep o wha
People-as-a-Se ice can b ing o us, ocused in he pla o m Social De ices, cu en ly in
de elopmen as a PhD, by Niko Mäki alo in he Uni e si y o Tampe e.
The a ge o his p ojec is o do he p ope in es iga ion in o he pla o m and
elabo a e a well know-how abou i , explo ing he possibili ies and p epa ing all he
documen a ion o u he esea ch and de elopmen s, as he e is no documen a ion
abou i . The pla o m is cu en ly in de elopmen and is no comple ely s able.
The concep o PeaaS will be also in e es ing o people, because his new
echnology will allow new ways o in e ac ing be ween people and de ices, p o iding
new capabili ies in how we use hem. Combined wi h In e ne o Things, people will be
able o in e ac wi h he su oundings, he de ices will know he con ex da a o he use ,
and change i s beha iou depending on i , making he wo k o he use wi hou o cing
him o send commands o he de ice.
Social De ices is a new pla o m ha can be used o his pu pose, i was in oduced
in 2011 as a join wo k by Nokia Resea ch Cen e , Aal o Uni esi y, and Tampe e
Uni e si y o echnology [26]. Sma phones ha e no only a lo o in o ma ion abou heis
owne s, bu also capabili ies ha enable hem o esemble humans ( hey can ansla e
ex in o speech, o example).
1
h p://www.seaclouds-p ojec .eu/
2
h p://so ia.lcc.uma.es/
12
19
Fo example, an ad e ise can use his pla o m o c ea e an applica ion ha sends
ad e ising o special a ge s, based in hei geog aphical posi ion, age, in e es s, social
s a us, e c.
The nimBees a chi ec u e [Figu e 2] is a specializa ion o he one de ined o he
PeaaS model. Hence, he se e and mobile a chi ec u es a e designed as ollows:
- Ca alogue se e . Based on he concep o he ca alogue se e , a non-mobile
elemen wi h a ixed and well de ined se ice en ypoin whe e e e y new mobile
de ice added o he PeaaS ne wo k mus be connec ed, in o de o au hen ica e
and p o ide in o ma ion abou i sel , and o e ie e in o ma ion abou he es o
connec ed de ices. This ca alogue se e is called The Hi e in nimBees.
- Mobile Se ice En i onmen . They a e basically mobile applica ions ha mus
be ins alled on e e y mobile de ice ha wan s o be pa o a PeaaS ne wo k,
p o iding an execu ion pla o m in which PeasS-designed se ices can be
deployed and execu ed. This is di ided in ou laye s o abs ac ion:
o Se ice Manage : This laye cons i u es he co e o he mobile se ice
en i onmen . I p o ides he capabili ies equi ed o deploying, emo ing,
unning o s opping se ices in he se ice con aine , and a basic
communica ion in e ace o es ablish a link wi h he ca alogue se e . I
also p o ides an in e ace o he use o he de ice o con igu e he mobile
se ice en i onmen p ope ly. In nimBees he in e ence engine is also
included in his le el. I consis s on a componen ha moni o s use
ac i i y o ex ac beha iou al pa e ns ha en ich he pe sonal in o ma ion
p o ile. The in o ma ion gene a ed by his componen is used o de e mine
whe he he owne ma ches he use p o ile equi ed by he ad e ise o
a ce ain se ice execu ion.
o Se ice Con aine : This laye p o ides he execu ion en i onmen whe e
he PeaaS-based se ices a e alloca ed, simila o a i ualiza ion engine.
The li ecycle o he alloca ed se ices is managed by he se ice manage
laye . In nimBees his laye has been implemen ed as a sc ip ing engine
ha p o ides an execu ion en i onmen in which un he nimBees
se ices, which a e w i en using an own sc ip ing language.
o Pe mission Con ol Laye : The access pe missions o he esou ces and
se ices p o ided by he mobile de ice a e e i ied by his laye , he eby
a oiding access o ce ain esou ces by unau ho ized se ices. Use s can
block he access o ce ain esou ces o in o ma ion in e e y momen and
o any se ice.
o Resou ces access Laye : This laye abs ac s PeaaS-based se ices
om pla o m-speci ic implemen a ions o each esou ce and se ice
o e ed by mobile de ices, p o iding a s anda d in e ace o be used
homogeneously by he se ices. In nimBees he access o he de ice’s
20
esou ces is p o ided o he deployed se ices in o m o “sou ces o
in o ma ion”. Such sou ce can be accessed by he sc ip ing language wi h
he p ope syn ax.
In conclusion, nimBees is a new pla o m ha uses he powe o People-as-a-Se ice
concep o p o ide a new communica ion channel o he use s, which can be used o
ad e ising companies o communica e wi h speci ic g oups o use s in a new and mo e
powe ul segmen a ion capabili ies.
In oducing BeeFun
Mobile ma ke ing is a g ea niche o PeaaS, Google and Facebook a e clea
examples o how collec ing sociological in o ma ion om ad e ising a ge s has become
ex emely impo an o companies [10] [16], bu wi h hei model is di icul o gain use
sa is ac ion. They y o c ea e apps as ools o launching pe sonalized
ecommenda ions o de ice owne s, while digi al ad e ise s can ge segmen ed da a
abou he use s and show a ge ed and ele an ad e ising o hem.
Howe e , he sociological p o ile is always cen alized on company se e s, p o iding
no anspa ency o he use and wi hou a p i acy gua an ee abou his da a. He e is
whe e PeaaS p esen s an al e na i e applied o mobile ma ke ing ha could bene i bo h
consume s and ad e ise s.
BeeFun
1
is a mobile app de elope o And oid and iOS ha makes use o he
nimBees messaging in as uc u e. I was de eloped by Gloin and allow use s o in e ac
wi h new kinds o a ge ed messages based in c i e ia such as age, ci y o esidence,
sex, cu en loca ion, e c. I is a po en ial example o his ma ke and he capabili y o
p o ide apid eedback on i s bene i s.
Wha BeeFun o e s is he abili y o ad e ise s o anonymously que y ad e ising
a ge s’ sociological p o iles o e ie e collec i e in o ma ion abou hei p e e ences,
a eas o in e es , and so on. This comple ely segmen ed da a le s ad e ise s e ine hei
ma ke ing s a egies and po en ially inc ease hei campaigns’ e ec i eness.
Fu he mo e, he in e ope a ion possibili ies suppo ed by PeaaS, and consequen ly by
BeeFun, le ad e ise s send ou in e ac i e ads o he app’s use s on he basis o hei
p o iles.
In e ope a ion be ween ad e ise s and a ge s is ca ied ou h ough he se ices
ha ad e ise s can deploy in he de ices. BeeFun is a he momen a me ely a p oo o
concep and he applica ions and se ices ha e o be w i en in his p op ie a y sc ip ing
language, which con ains basic ins uc ions o ask he in e ence engine abou he use ’s
sociological p o iles, eques no i ica ions and isual con en o display on he use
in e ace, and e u n and execu ion esponse o ad e ise s.
1
h p://www.bee unapp.com/
21
All he se ices ha ad e ise s can deploy in mobile de ices a e main ained in an
ex e nal ca alogue o a Hi e sys em, which egis e s all mobile de ices connec ed o he
ne wo k and manages he deploymen and execu ion o se ices and applica ions in he
de ices.
The mos impo an hing o his sys em, and which di e en ia es he PeaaS model
om o he s (like Google o Facebook), is he acco dance wi h he ou h pilla . The
sociological p o iles a e always kep in hei de ices, wi h only some o he in o ma ion,
olun a ily gi en by he use s) sa ed in he hi e. This basic in o ma ion allows he
ad e ise s o in e ac wi h a ge s, because i would be di icul o ind he a ge s wi hou
a cen alized sys em.
The hi e sys em le s he sys em p eselec po en ial a ge s on he basis o his
in o ma ion. Then, he de ices’ in e ence engines s ill mus e alua e he a ge ’s
sui abili y.
Conclusions
Th ough his implemen a ion o PeaaS we can explo e new o ms o mobile
ma ke ing ha allow ad e ise s o each hei a ge s mo e p ecisely. I allows accessing
de ailed sociological p o iles o he a ge s and e ie ing collec i e in o ma ion abou
hem. Ta ge s a e able o p o ide hei opinions abou p oduc s and ads while p ese ing
hei p i acy, wha is an inno a ion in his ield. The echnology allows he gene a ion o
a iew o collec i e in e es s, which could be ele an o ma ke s udies.
By ocusing on people and hei mobile de ices as in o ma ion sou ces, we can
con inue o hink o a new ad anced compu ing models ha p o ide nume ous bene i s.
Mobile-cen ic compu ing a chi ec u es can p o e hei g ea capabili y o gene a e
sociological p o iles h ough applica ions such as BeeFun.
Applying his model in ields wi h a high social impac could be ex emely bene icial
o socie y. PeaaS could le o ganiza ions iden i y pa e ns ha could be con ibu ing o
heal h diso de s among la ge communi ies. They could use his in o ma ion o plan
s a egic ac ions ha could help inc ease indi iduals’ li e expec ancies. In go e nmen ,
PeaaS could allow e ie al o collec i e in o ma ion abou ci izens’ poli ical iews and
p e e ed cou ses o ac ion o enhance li ing s anda ds and p omo e democ acy.
Howe e , he success o PeaaS equi es a wide adop ion by mobile de ice use s,
hey need o ecei e some bene i s in exchange o pa icipa ing. PeaaS gi es use s
mo e con ol o e hei p i acy, bu pe haps hey could ecei e bene i s om he
companies using hei in o ma ion. Fo example, discoun s o people using he PeaaS
pla o m. Addi ionally, a hi d-pa y company ha uses he pla o m o each use s could
mone a ily compensa e hose use s o gi e hem i s p oduc s o se ices o ee o a
discoun ed p ice.
22
The In e ne o People
The e is g owing in e es in de eloping applica ions on he In e ne o Things. The
main objec i e o hese applica ions is o in eg a e echnology in o people’s e e yday li e
in o de o be a se ice o hem. Howe e he e a e many hings o imp o e in he
implemen a ion. Usually, he use needs o se pa ame e s wi hin he applica ion, and e-
con igu e hem when he con ex changes.
Nowadays In e ne is a ailable almos e e ywhe e, connec ing e e yday physical
objec s o he Ne is ge ing e e easie . One o he main goals behind such sma
in eg a ion o de ices is o simpli y people’s li es by ha ing echnology wo k o hem
seamlessly [6]. Fo ins ance, you can emo ely swi ch on you house’s ai -condi ioning
sys em using you sma phone o ge a com o able empe a u e when a i ing home.
Howe e , how IoT echnologies a e cu en ly in eg a ed wi h human s ill lea es much
oom o imp o emen [17]. We need o de elop sui able mechanisms o p ope ly adap
o people’s con ex and make he ecnology able o wo k o people. Cu en ly people
a e o ced o change hei con ex o i echnological equi emen s.
Technology should ake people’s con ex in o accoun , lea n om i , an ake
p oac i e s eps acco ding o hei si ua ion and excep a ions, a oiding use in e en ion
as much as possible. In his desi able scena io, he ai -condi ioning sys em will know
whe e we a e in ou way back home, ins ead o swi ching on a speci ic p econ igu ed
ime.
Enabling such scena ios equi es mo ing om he In e ne o Things o he In e ne
o People (IoP) [30]. In his in as uc u e, sma phones play a cen al ole, e lec ing
hei cu en use as he main in e ace allowing people o be connec ed o he In e ne .
Howe e , he sma phones ha e o mee a se ie o equi emen s and capabili ies [28]:
- The sma phone needs o be capable o lea ning abou i s owne and hei
con ex , cons uc ing a digi al p o ile.
- I needs o anspa en ly nego ia e and p opose in e ac ions wi h o he de ices on
he In e ne , eac ing o s imuli and handling ela ionships.
- I needs o be able o manage digi al p o iles and ac acco dingly, p o iding i s
owne ’s con ex as a se ice o o he s, and scanning o se ices ha migh be
o in e es o i s owne o o upda e he digi al p o ile.
The in as uc u e o suppo he IoP concep can be achie ed wi h a combina ion o
he Social De ices
1
and People-as-a-Se ice pla o ms. Social De ices enhances he
p oac i e capabili ies o sma phones o o ches a e hei in e ac ions wi h o he de ices
connec ed o he IoT. PeaaS p o ides sma phones wi h se ing capabili ies ha allow
people o o e se ices om hei de ices, including hei con ex and sociological
p o ile. The combina ion o hese wo con ibu es o cons uc ing an IoP by allowing
1
h p://socialde ices.gi hub.io/
23
people’s con ex in o ma ion o be included in he coo dina ion and in e ac ion
managemen o IoT de ices. The main bene i is he in eg a ion o people as i s -class
ci izens in he IoT uni e se, opening he way o he de elopmen o new kinds o human-
iendly se ices and applica ions.
As sma phones con ains a lo o in o ma ion abou hei owne s, and senso s o
e ie e con ex ual in o ma ion abou he su oundings o i and, consequen ly, abou he
owne , hey a e he pe ec de ice o be he cen al piece o his new concep [30].
Sma phones can educe he complexi y o use in e ac ion wi h IoT-linked de ices. They
can know he use ’s ci cums ances, as hey a e almos always wi h he use . The
con ex ual da a can be used o know in o ma ion abou he use and o lea n abou him
and o de ec how he pe son beha es when hey a e a ce ain loca ions o su ounded
by ce ain o he people.
One o he mos impo an aspec s o People-as-a-Se ice is he ac ha all he
in o ma ion abou he use is kep in he owne ’s de ice, and is secu ely p o ided as a
se ice o hi d pa ies di ec ly om he sma phone. The use can limi who can access
o ha in o ma ion and know who and when is ecei ing i . The use can con ol his
sociological p o ile and all he in o ma ion easily, jus modi ying o dele ing i om he
de ice.
PeaaS allows a a ie y o in o ma ion o be collec ed, such as he moods, ends,
social s a uses, and heal h habi s o a g oup o people, in o de o de ine hei digi al
p o ile.
Cu en IoT echnology needs people-cen ic enhancemen s. As se ou in (Mi anda
e al., 2014) [30], he goals o any pe asi e compu ing con ex should be:
- Be social: In e ac ions in which hings, de ices and people pa icipa e mus be
social. The IoP should allow o he e ogenei y by suppo ing he di e en ypes o
de ices ha people use, allowing hem o in e ac wi h people and o he de ices.
Use s need o be empowe ed o adjus hei p e e ences and policies abou when
and wi h whom hei de ices a e socializing.
- Be pe sonalized: In e ac ions be ween de ices mus be pe sonalized o he
sociological p o iles and con ex s o hei use s, p o iding a anspa en
mechanism o his cus omiza ion. Use s mus be able o adjus hei p e e ences
in o de o con ol how hei p o ile is used by o he IoP s akeholde s.
- Be p oac i e: The igge ing o in e ac ions mus be p oac i e, no manually
commanded by he use . The IoT should allow o de ice he e ogenei y o allow
hem all o in e ac mo e p oac i ely, wi hou he need o manage all connec ed
hings. Use s mus be empowe ed o adjus hei p e e ences o con ol how
p oac i e hei su ounding hings a e, his is done by p oac i i y policy. I is e y
impo an because can in oduce secu i y isks.
- Be p edic able: In e ac ions mus be p edic able and igge ed acco ding o a
p edic able con ex ha he use had p e iously iden i ied, and wi h a speci ic
24
beha iou de ined. Use s mus be able o iden i y and ag ha con ex , de ine he
expec ed beha iou and se he p i acy policies o sha ing hei in o ma ion. The
use has o know how an in e ac ion can be s opped immedia ely and how o
p e en a misbeha iou in u u e.
In e ne o People Middlewa e a chi ec u e
Figu e 3. A chi ec u al desc ip ion o he IoP middlewa e.
In his a chi ec u e, he cen al componen is he De ice Regis y, who main ains he
in o ma ion abou he di e en de ices managed by he sys em. The de ices a e
classi ied in o Companion and NonCompanion.
Companion de ices a e hose ha main ain con ex ual and sociological in o ma ion
abou hei owne s, while NonCompanions a e “ hings” which can egis e hei
capabili ies and addi ional in o ma ion abou hei s a e.
The Ac ion Reposi o y manages he middlewa e’s se o ac ions. Each ac ion de ines
how he de ices should in e ac wi h each o he and wi h people.
The Applica ion Reposi o y componen s o es he di e en applica ions de ined in he
IoT con ex and managed by he middlewa e.
25
The Applica ion Manage allows people o enable o disable he di e en applica ions
acco ding o whe he hey would like o be p esen and con ibu e wi h hei sociological
p o iles.
The applica ion and ac ion speci ica ions may include eques s o he di e en
Sociological P o ile componen s o he Companion de ices in ol ed, so ha he
speci ied beha iou can be adap ed o he use ’s con ex , pe sonali y o mood.
Sociological P o iles
The Sociological P o ile componen is a undamen al pa o e e y Companion
de ice. This componen main ains he in o ma ion ha has been ga he ed and in e ed
abou people and hei con ex s. This in o ma ion co e s people’s beha iou ,
p e e ences and also con ex ual da a such as loca ion. De ices can also p ocess
con ex ual in o ma ion such as a p oximi y g aph o su ounding people/de ices/ hings.
Companion de ices can also no i y he egis y abou signi ican changes in hei
Sociological P o ile, so ha subsc ibed applica ions will be no i ied when an on-change
e en occu s, allowing hem o e- un he que ies o ge he eshly upda ed in o ma ion.
The componen also allows use s o de ine and adjus he sha ing schema o he
p o ile. Use s should de ine wha in o ma ion will be a ailable o which applica ions,
de ices o use s, and in which con ex he in o ma ion should be p o ided.
Finally, he Sociological P o ile p o ides an in e ace o de ice owne s o pe sonalize
he way in which he de ice beha es unde speci ic ci cums ances.
Conclusion
In e ne o Things is ha ing a success ul g ow h, howe e , people’s in e ac ion wi h
his kind o sys ems is s ill a om iendly. The e m In e ne o People has been used
be o e, bu i e e ed o adi ional Web sys ems designed only o humans o use. Now
he idea o his e m is o b ing he In e ne o Things close o people.
Mos o he p o ile’s cus omizable policies and p e e ences will be suppo ed by a
use - iendly wiza d. The componen will p og essi ely lea n he cus omiza ion du ing
he de ice’s use om he in e ac ions and con ex s in which i is in ol ed, and om he
owne ’s decisions abou how he de ice should beha e in hose in e ac ions.
To cons uc mo e human-like, p edic able, p oac i e and social in e ac ions be ween
people and “ hings”, he IoP middlewa e o e s a Web-based IDE and ools
1
, which we
will discuss in nex chap e .
1
h p://o ches a o js.o g
26
In eg a ion wi h Social De ices and In e ne o Things
People-as-a-Se ice and Social De ices combined p o ides new in e ac ions and
capabili ies o use s when hey a e co-loca ed, bu IoT can be also in eg a ed wi h hem,
p o iding new capabili ies and communica ions wi h e e yday objec s nea he use . In
some yea s 50 billion o de ices could be connec ed [11].
Some IoT- ela ed scena ios ha e been al eady implemen ed and in oduced using
Social De ices, o example The Social Co e Machine [25]. This co ee machine asks
o he use , based on his con ex , i he wan s o ha e a co ee, and hen he machine
in i ed o he people o join he co ee b eak.
Vázquez and López-de-Ipina [50] ha e been wo king on his subjec oo, hey
in oduced he concep o seman ic de ice as a sys em “able o spon aneously disco e ,
exchange and sha e con ex in o ma ion wi h o he ellow seman ic de ices as well as
augmen his con ex in o ma ion ia easoning in o de o be e unde s and he
si ua ion and pe o m he app op ia e eac i e esponse.” [48][49].
They ha e de eloped ou p o o ypes o wha hey call Social De ices, in eg a ed
wi h The In e ne o Things o make some objec s capable o in e ac wi h use s. They
eu ilize he concep o Seman ic Gadge , which was in oduced by Lassila and Adle as
de ices capable o pe o ming “disco e y and u iliza ion o se ices wi hou human
guidance o in e en ion” [24].
This concep emphasizes e en mo e he bene i s o he communica ion wi h o he
de ices in o de o de e mine he app opia e beha iou . In e ne is he enabling
echnology in his case, since he knowledge p o ided by local de ices can be
complemen ed wi h in o ma ion ob ained om he In e ne (which is p o ided by o he
de ices, se ices o humans).
Flexeo’s Senso Map: a wi eless senso ne wo k
This is he i s p o o ype by Vazquez and Lopez-de-Ipina. The goal o his p ojec
was designing a sys em o emo ely moni o ing wi eless senso ne wo ks h ough he
In e ne , and in eg a ing all he da a in o business p ocesses in o de o execu e da a-
mining ope a ions and de e mine co ela ions among da a. Scena ios ela ed o “hea lh
a home” and emo e moni o ing o indus ial acili ies we e designed in o de o es he
esul ing sys em.
As an addi ional ou come, a mash-up o senso collec ed da a and Google Maps was
de eloped, called Senso Map. This subsys em illus a ed he po en ial applica ions o
publishing up- o-da e senso da a on he In e ne in o de o dynamically in eg a e
di e en sou ces o in o ma ion o p o iding highe le el se ices.
The nex [Figu e 4], om [50], shows a senso ized chai wi h wo p essu e senso s
(a he sea and a he back) connec ed o a wi eless senso ne wo k node in o de o
p o ide in o ma ion abou i s use o local and emo e objec s. [Figu e 5] con ains a
27
sc eensho o he Senso Map applica ion showing he s a e o he chai (i someone is
sa on and/o leaned backwa ds).
Figu e 4. P essu e senso s in he chai .
Figu e 5. Flexeo’s Senso Map showing he s a e o a chai .
28
RealWidge s: om he desk op o he eal wo ld
Wi h RealWidge s hey wan ed o embody he unc ional capabili ies o hese digi al
en i ies in o eal wo ld iny wi eless displays, in o de o ha e small “windows” deployed
e e ywhe e opened o he in o ma ion om he In e ne .
The RealWidge is o med by an OLED display, wi h high esolu ion and con as
while small ene gy consump ion, in eg a ed wi h a C ossbowMo e2 wi eless senso
ne wo k node. A compu e ac ed as a ga eway be ween he In e ne and he wi eless
senso ne wo k, unning a RealWidge Managemen Applica ion ha connec ed o he
equi ed si es on he Web, downloaded he in o ma ion, analyzed i and inally sen he
con en o he app op ia e widge as con igu ed by he use . [Figu e 6] illus a es a
RealWidge showing in o ma ion abou he liquid le el in a emo e chemical con aine
ha was moni o ed by a wi eless senso ne wo k o e he In e ne .
Figu e 6. RealWidged ale ing abou he liquid le el in a ank.
Two bu ons we e p o ided o in e ac ing wi h he RealWidge , basically o
managing he ene gy by pos poning he in o ma ion o a la e ime, o immedia ely
disca ding i .
35
Se ice
Desc ip ion
Ac ion egis y
A egis y o all possible ac ions and in e aces ha a e a ailable.
De ice egis y
A egis y o all de ices and hei ins alled capabili ies. This egis y may
also con ain addi ional dynamic in o ma ion abou he de ices, such as
de ice p oximi ies and s a es, as well as con ain in o ma ion abou use s
and de ice owne s.
T igge egis y
A egis y ha consis s o a igge ing able, which holds in o ma ion abou
possible igge s and co esponding ac ions; and o an ac i e igge s able,
which holds in o ma ion abou igge ins ances ha ha e been obse ed,
a e s ill ali e, and ha e no ye been success ully scheduled.
Scheduling
Managemen o he scheduling ask.
Execu ion
Managemen o he ac ion execu ion, ha is, synch oniza ion and
coo dina ion o he de ices.
Table 3. The Se ices in he Ac ion-O ien ed P og amming Model In as uc u e.
Figu e 10. Ac o s, a e ac s and esponsibili ies in he ac ion-o ien ed p og amming model
in as uc u e.
36
The e a e se e al ac o s and hey in e ac wi h he a e ac s in di e en ways [Figu e
10].
- The in as uc u e p o ide deploys and adminis e s he Ac ion-O ien ed
in as uc u e.
- The capabili y de elope is esponsible o implemen ing he capabili ies o he
de ices, acco dingly o he de ined in e aces.
- The ac ion de elope uses he in e aces o execu e he necessa y capabili ies in
each de ice. The de elope doesn’ know he iden i ies o cha ac e is ics o he
de ices, and how he capabili ies a e implemen ed.
- The use ins alls he desi ed capabili ies o he de ice.
- The de ice could be conside ed as an ac o oo, i will egis e i sel o he
in as uc u e a un ime, and cons an ly epo s dynamic in o ma ion abou i s
s a e.
The o de o he illus a ed e en s can be di e en , because he ac ion de elope can
euse exis ing de ice capabili ies ins ead o w i ing code o new de ice capabili ies o
suppo mul iple mobile pla o ms.
The h ee basic s eps o c ea e an Ac ion-O ien ed p og am would be he ollowing:
- De ine o implemen he in e aces o he necessa y capabili ies o he de ice.
- De ine he ac ions o be execu ed, he p og am i sel , and speci y he needed
p econdi ions o s a ing he ac ion.
- De ine he igge s ha ini ia e he ac ions.
37
Requi emen s and cha ac e is ics o Social De ices
Ou de ices ha e a lo o in o ma ion abou us, maybe ou mobile phone knows
whe e we wan o d i e when we en e in he ca , and i can communica e ou des iny
au oma ically o ou GPS sys em. Also a he o ice, employees’ de ices can synch onize
each use calenda o s ablish he day and ime o a mee ing, and hen communica e he
esul o each employee. The concep can be aken beyond, o example, ou de ice
knows we ha e a mee ing in he hi d loo , so i could send ha in o ma ion o he
ele a o when we en e in i .
The key o Social De ices is o u ilize esou ces and capabili ies o mul iple physically
co-loca ed de ices and he p oximi y o he de ices in a use obse able manne . Some
o hese de ices can be s a iona y, like an ele a o o he sc een in a mee ing oom. This
echnology would i well in conjunc ion o o he echnologies like pe asi e compu ing
1
[7] o In e ne o Things [17].
Howe e , Social De ices equi es some de ices capabili ies o be able o wo k in
each de ice. The de ice mus be a compu a ionally capable de ice and be able o
in e ac wi h o he de ices and use s in hei p oximi y, o example ia Blue oo h. This
equi emen is easy o accomplish o de ices like mobile phones, lap ops o compu e s,
bu is di icul o o he kind o de ices like ele a o s, GPS sys ems, ca s, e c.
The capable de ices need o ha e iden i ies, so he pla o m would be able o keep
ack o he de ice iden i ies. De ices collabo a e in a way ha is o en pe sonal o he
use and ela ed o use p e e ences. The e is a cen alized egis y whe e de ices need
o egis e hei iden i y and capabili ies. The pla o m allows o egis e mul iple de ices
o each use and selec which o hem is he p incipal, he mos used.
Ano he equi emen is he need o know which o he de ices a e nea by he de ice.
Social De ices in e ac ions wo k in a co-loca ed way, and equi e ha use s and de ices
a e nea each o he . Thus, he p oximi y o de ices needs o be disco e ed and s o ed in
he Social De ices Pla o m, who main ains p oximi y in o ma ion in a o m o a
ma hema ical g aph, whe e nodes deno e de ices and edges deno e he dis ance o he
de ices. This in o ma ion is acqui ed by he de ices and epo ed o he cen alized
se e , who upda es he p oximi y g aph.
The e is also a need o know wha di e en collabo a i e ac ions a e a ailable
be ween de ices. The pla o m manages ac ion desc ip ions cen ally. As we ha e seen,
an ac ion has a body and a p econdi ion. The body desc ibes wha he ac ion will do,
and he p econdi ion is a se o equi emen o be comply he de ice o be able o
execu e he ac ion. Each de ice has a se o enabled capabili ies ha can be used by
he unning ac ions, ha ing a equi ed capabili y could be a p econdi ion o execu e a
de e mined ac ion. Also we may need o know which kinds o de ices can pa icipa e in
an ac ion, wha will be also cap u ed in he ac ion p econdi ion.
1
Pe asi e compu ing, also called ubiqui ous compu ing, is a concep whe e compu ing is made
o appea e e ywhe e and anywhe e.
38
Capabili ies a e a way o manage pe missions in Social De ices. The use can
enable o disable di e en capabili ies. Fo example, he e is a capabili y able o speak
aloud by syn hesizing ex , i he use doesn’ wan he de ice can speak, he can disable
he capabili y. Howe e he use won’ be able o execu e ac ions ha equi e ha
capabili y.
The condi ion when ac ions a e execu ed needs o be decided au oma ically a
un ime, bu he p econdi ions a e no only abou s a ic capabili ies o de ices, bu hey
may cons ain dynamic p ope ies changing on he de ice. Dynamic condi ions ha
a ec whe he a de ice can pa icipa e in an ac ion a e moni o ed ia de ice s a es. The
de ices con inuously epo hei s a es o he cen al se e , so i has all he necessa y
in o ma ion o decide i he ac ion can ake place o no . S a es can be based on gene al
p e e ences o he use as, o example, i he de ice is in silen mode.
All he condi ions ha e o be e alua ed wi hou use in e ac ion, bu con inuous
sea ching o de ices and e alua ing p econdi ions is compu a ionally ha d. He e is
whe e igge s ake place o ma k he need o ac ion execu ion. Ce ain ac ions a e
a emp ed o be scheduled when a ce ain igge is ecei ed. A igge can be an upda e
in he s a e o he de ice, a p oximi y change o an ex e nal e en . The de ices only
need o iden i y he condi ion o aise a igge , which will be ecei ed by he cen al
se e , who decides whe he and which ac ion mus be ini ia ed.
Also when execu ing an ac ion, one has o decide which conc e e de ices will
pa icipa e in i . The main ask is o ela e he po en ially a ailable de ices o he
p econdi ions s a ed in he ac ion desc ip ion and o decide he oles ha will be
assigned. Some imes he same se o de ices can pa icipa e in he ac ion wi h di e en
oles, o example in a dialog applica ion.
Finally, he ac ion needs o be execu ed, bu each de ice has a di e en so wa e
and ha dwa e ea u es. The ac ion coo dina ion o o ches a ion equi es de ining a
con ol low o each de ice, ega dless he di e en so wa e o ha dwa e speci ica ions.
In he SDP he e is a gene ic sys em whe e a ious coope a ion possibili ies a e
a ailable de ining a pla o m in o which coo dina ion is buil in.
These equi emen s and cha ac e is ics a e necessa y p ima ily o use and p i a e
de ices, because hey mus allow he owne o change hei p e e ences and sa e he
p i acy o he use . Howe e , o public Social De ices, like an ele a o , he e is no need
o e alua e p econdi ions o capabili ies, cause is a simple de ice which only will accep
eques s o nea by de ices and ac acco dingly.
39
A chi ec u e
This is he basic a chi ec u e o Social De ices, he de ices a e egis e ed in he
Con olle componen in he se e , and he use can ins all he in e aces and
capabili ies he wan s. Once connec ed, he de ice will look o nea by de ices and
epo hem o he se e , wha will igge some e en s. The se e hen e alua es i
he e is an ac ion a ailable o be execu ed o his de ice, and o ches a o execu es i .
Figu e 11. The basic a chi ec u e o Social De ices, and connec ions done s ep by s ep.
40
O ches a o .js
We ha e seen a small desc ip ion abou Social De ices Pla o m, who implemen s
he concep o Social De ices. Howe e we ha e seen i as an in oduc ion o he new
pla o m, called O ches a o .js and implemen ed by he same g oup o de elope s. This
new pla o m is he ocus o his wo k, and i is mo e powe ul han SDP.
O ches a o .js (OJS) is a comple ely new middlewa e ha suppo s Social De ices
concep . I is implemen ed as a single cloud se ice ha o e s a comple e se o ools o
implemen and p o o ype Social De ices applica ions. I is specially designed o
implemen in e ac ions be ween de ices and people, and o obse ing si ua ions when
hese in e ac ions should p oac i ely ge igge ed.
Social De ices applica ions di e om adi ional mobile apps, bu O ches a o .js
can be easily hos ed on any egula compu e and be used o quickly de eloping mul i-
de ice applica ions. The main ask o O ches a o .js is o main ain pe sis en connec ion
o de ices, s o e applica ions and con ex ual in o ma ion o de ices, and o e means o
igge ing applica ions.
A chi ec u e
O ches a o .js is based on Node.js
1
and he communica ion is based on Socke .IO
2
P o ocol. This o e s highly e icien inpu /ou pu ope a ions be ween O ches a o .js
se e and i s de ices. The se e is o med by he ollowing componen s:
- The O ches a o Co e c ea es new ac ion ins ances and makes su e ha he
ac ions wo k p ope ly. I main ains he connec ions o de ices and coo dina es
he de ices by handling incoming and ou going e en s based on he logic de ined
in ac ions.
- The Applica ion Manage s o es and execu es he applica ions, allowing use s
o s a /s op hem, and s o es use - ela ed se ings ha may be equi ed by he
applica ion. The applica ions a e like daemons o child p ocesses ha un
cons an ly and moni o changes wi h help o obse e s. When he app de ec s
changes, i uses some scheduling algo i hm de ined by he de elope o
de e mine whe he some in e ac ion should ake place.
- The De ice Regis y main ains con ex ual s a e in o ma ion abou each de ice,
which is s o ed o MongoDB
3
da abase. Mo eo e , he egis y main ains he
p oximi y g aph o he de ices, which is o med based on Blue oo h signal
s eng h alues (RSSI). This allows igge ing applica ions o a se o de ices
ha a e close o each o he .
1
Node.js is a se e -side Ja aSc ip pla o m buil on Ch ome’s Ja aSc ip un ime.
2
Socke .IO is a new non-s anda dized p o ocol o elying e en s be ween de ice and se e .
3
MongoDB is an open-sou ce documen Non-SQL da abase.
41
- The Ac ion Reposi o y con ains he ac ion de ini ions s o ed by he de elope s.
Basically hese de ini ions a e Ja aSc ip iles which each con ains p econdi ion,
e o handling, and he desc ip ion o join beha iou o de ices and in e ac ions
be ween use s by u ilizing de ice capabili ies.
- The Capabili y Reposi o y con ains desc ip ions o each capabili y, like Ja a’s
in e aces. The unc ionali y is no w i en he e, his only de ines wha me hods
he capabili y mus implemen , and wha pa ame e each me hod akes. The
de elope s mus implemen he capabili ies like hey wish and using wha e e
language, as long as hey ollow he capabili y desc ip ion.
Tools
O ches a o JS o e s a web-based use in e ace named Web Console. Use s can
manage hei p o ile and hei de ices, and o s a and s op he applica ions om his
console. I can be used di ec ly om mobile phone’s b owse and also be embedded
inside mobile apps wi h WebView UI componen s.
Figu e 12. Web Console iew whe e he use can add, modi y o emo e his de ices.
F om his iew, use s can add, emo e and manage hei de ices. In [Figu e 12] we
can see 2 de ices egis e ed o my accoun . Each de ice has an iden i ie , composed by
he use name and he de ice name.
We can see all he egis e ed de ices in he De ice Moni o iew [Figu e 13]. In his
iew we ha e all he in o ma ion a ailable by he pla o m o all he de ices. Fo
example, we can see he Blue oo h Add ess, UUID, Owne , Type o de ice, enabled
capabili ies, and he con ex da a.
Only egis e ed de ices can be used in he pla o m, so when a new use wan s no
use he applica ion, he has o egis e he de ice using he Web Console, and enable all
he desi ed capabili ies.
De ices can be modi ied and emo ed, so i he use doesn’ wan o con inue using
he pla o m, he can emo e his egis e ed de ices. All he in o ma ion abou ha de ice
will be o ally emo ed om he pla o m, and won’ be possible o use i anymo e.
42
Figu e 13. De ice Moni o wi h my egis e ed de ices.
All he enabled capabili ies appea in he iew, hey a e codi ied as a colou ed
squa e. We can enable he desi ed capabili ies in he se e -side om he same iew
whe e we egis e new de ices.
Figu e 14. Regis e new De ice iew. F om his iew we can add all he in o ma ion abou he de ice,
and enable he capabili ies we wan o use.
43
The Web Console also o e s a Web-based IDE o implemen ing he applica ions
wi hou ins all any so wa e. De elope s can de ine capabili ies, ac ions and obse e s
di ec ly om hei b owse s, and igge hem manually o a p ede ined se o de ices.
We can see, c ea e and modi y ac ions om he Ac ions
De ini ions menu. We will
see a iew like [Figu e 15]. All he de ined ac ions will appea on he igh menu, also
c ea e a new Ac ion is possible wi h he i s op ion.
On he le we ha e a big edi o whe e we can in oduce he body o he ac ion. This
edi o is e y powe ul, i will colou s he syn ax while we a e coding and show he line
numbe s. This IDE allows us o p og am ac ions en i ely wi hou he need o an ex e nal
so wa e. We can sa e he ac ion di ec ly o he se e p essing he Push bu on.
Figu e 15. Example o he Ac ions De ini ion iew, wi h he Apocalymbics ac ion loaded in he edi o .
We ha e wo ields below he edi o which allows us o execu e he ac ion in a
de ice. Pa ame e s can be de ices o simple da a like S ings, In ege s, e c.
Figu e 16. We can selec de ices o execu e he ac ion di ec ly in he d opdown lis .
44
Capabili ies a e implemen ed wi h he na i e pla o m speci ic ools, like wi h Eclipse
(And oid), howe e , he O ches a o JS pla o m o e s a ool o de ining he capabili y
desc ip ions. This iew [Figu e 17] is p e y simila o he Ac ions De ini ion iew, wi h all
he capabili ies on he igh menu and he edi o on he le .
Figu e 17. Capabili ies in e ace example, in his case wi h he Playe Capabili y loaded.
In his in e ace we can de ine all he unc ionali y o his capabili y. We can add
commen s o explain hem. All o he unc ions de ined in he in e ace mus be
implemen ed in he de ice [Figu e 18].
Figu e 18. Implemen a ion example o he unc ion se Volume o he Playe Capabili y in And oid.
O ches a o JS is a ailable o And oid, iOS, .NET Gadgee e and Py hon using
hese capabili ies. We only ha e o implemen he capabili ies in each de ice in hei
na i e language, and he Pla o m will call he unc ions.
51
I we need o ead in o ma ion om a websi e we can use he class eques . In oking
ac ions is done wi h he h p eques class and calling he REST API o he pla o m.
Figu e 26. Calling an ac ion om an applica ion. In oking an ac ion is achie ed sending he
co esponding POST message o he REST API.
We can also c ea e and manage Fibe s, which will allow us o c ea e pe iodic
ac ions, poll websi es, and check i any use has eques in o ma ion, e c. A Fibe is
basically as a Th ead in any con en ional p og amming language.
In he same way, we can use any unc ion and lib a y in NodeJS. The possibili ies
a e endless: access a da abase, c ea e socke s, connec o a web se ice, e c.
Figu e 27. A basic Fibe p in ing a message each 10 seconds while he applica ion is unning.
52
And oid Clien
Now we unde s and how he se e wo ks, le ’s see how he And oid clien connec s
o he se e , manages capabili ies and execu es he ac ions sen by he pla o m.
The main class in And oid is he O ches a o JSAc i i y.ja a. This class con ains he
basic unc ionali y o he clien : connec ion o he se e , managemen o capabili ies,
in oca ion o ac ions, se ings and con igu a ions. All he classes mus be inside he
main package com.ojs.
The main class will execu e when we
open he applica ion, i has a minimalized
iew wi h wo small bu ons, o connec and
disconnec . The i s ime we execu e he
applica ion i p obably won’ connec o ou
se e because i needs o be con igu ed.
In chap e 4 we will see how o ins all and
con igu e e e y hing.
When he de ice connec s o he
pla o m a small message appea s, i he
connec ion is los i will be also no iced wi h
a small message a he bo om o he
sc een. The de ice will y o load all he
enabled capabili ies.
Capabili ies a e di e en ia ed h ough
packages, each package means a di e en
capabili y.
All he capabili ies mus be in a package called com.ojs.capabili ies.myCapabili y and
hey mus implemen he public me hod ini Capabili y( Con ex applica ionCon ex ).
Figu e 28. ini Capabili y me hod om he hashTagCapabili y.
53
No e: The name o he package and he capabili y class a e e y impo an . The clien
will y o loca e he capabili y in a package wi h he same name as he capabili y in he
se e , bu wi h he i s le e in lowe case. Also he name o he class ha implemen s
he capabili y mus ha e exac ly he same name as in he se e ’s capabili y in e ace.
As we can see in [Figu e 28], he capabili y
name is HashTagCapabili y, which is he name
in he se e ’s capabili y in e ace. The name o
he class implemen ing ha capabili y mus be he same, and he name he package is
hashTagCapabili y, wi h he same le e in lowe case and inside he package
com.ojs.capabili ies.
The capabili y mus also implemen s he me hod ini Capabili y as in he igu e. In one
o hese s eps ail, he capabili y will be au oma ically disabled om he clien
1
.
Once his is accomplished, he capabili y will be able o load in he de ice and i is
eady o execu e he ac ions sen by he se e . The capabili y should implemen all he
unc ions de ined by he se e ’s capabili y in e ace. I one o he unc ions is no
implemen ed, he de ice will send an excep ion o he se e when an ac ion ies o
igge he me hod.
Usually a capabili y will c ea e a new Ac i i y in he de ice, his is no oo much
di e en as c ea ing a no mal ac i i y in a usual And oid applica ion. Me hods in
capabili ies can ha e a gumen s o ecei e in o ma ion om he se e , in his example I
ha e he me hod showNo i ica ion, which ecei es a i le and a message. Bo h
a gumen s a e sen o he new ac i i y h ough an In en .
Figu e 29. Implemen a ion code in Ja a o he me hod showNo i ica ion o No i ica ionCapabili y.
Then, we can ead he In en in he c ea ed ac i i y and use he da a p ope ly.
1
This is one o he ea u es I ha e implemen ed.
54
Figu e 30. No i ica ionCapabili y in e ace deployed in he se e . The capabili y ins alled in he clien
should implemen all o hese unc ions.
Changes and addi ional ea u es
To accomplish he a ge o his p ojec and be able o de elop he i e es
applica ions I ha e o make some changes and de elop new ea u es in he pla o m. I
will explain he e wha hese changes a e and how hey wo k:
- T igge ing obse e s when he a i ing da a is he same: In he o iginal
pla o m when con ex da a a i es o he pla o m, i didn’ igge he obse e
i he con en was exac ly he same as he pla o m al eady had. I ha e
changed his beha iou because some imes I need o igge he obse e .
Fo example, in some applica ions he se e asks he use o in oduce da a,
like he phone numbe . When he use in oduces his da a he applica ion can
s a , howe e , i he use in oduced he same phone numbe as he exis ing
one in he pla o m (wha is e y p obable), he applica ion didn’ igge he
ac ion.
- Changed he RSSI Blue oo h alues: I ha e
c ea ed 5 di e en anges o de e mine he
dis ance o a de ice based in he RSSI Blue oo h
alue. I changed his alues because i would be
be e i we ha e a be e app oxima ion o he
dis ance. Also he o iginal alues had nega i e
alues (and my de ices send a alue be ween 0
and 255), and some imes he p oximi y g aph
didn’ d aw co ec ly because alues we e oo
much high.
- Added suppo o Blue oo h p oximi y in he And oid clien : o iginally when I
s a ed my p ojec he And oid clien had no Blue oo h suppo . I ha e
implemen ed i in my e sion. La e , a Blue oo h se ice was in eg a ed in he
main pla o m. Howe e , bo h se ices can coexis in he applica ion. I had
implemen ed he Blue oo h suppo as a capabili y, wha he use can enable
o disable om he de ice. Se e applica ions call his capabili y when an
upda e o he Blue oo h p oximi y is needed.
55
- Au oma ically disable unimplemen ed capabili ies in he And oid clien : i a
new capabili y in e ace is deployed in he se e , p obably ou ins alled
And oid applica ion has no suppo o i . I ha e changed he beha iou o how
capabili ies a e ac i a ed, so he use won’ be able o enable capabili ies no
implemen ed in he clien . Unins alled capabili ies can’ be selec ed in he
menu [Figu e 31].
- Handle disconnec ion om he se e
o inish capabili ies: in my
implemen a ion I use some
capabili ies as a se ice, his se ices
can, o example, cons an ly upda e
Blue oo h p oximi y o GPS posi ion. I
ha e added a igge when he
connec ion wi h he se e is los , o
allow inish some ope a ions ha
ha e no much sense o be in
execu ion once he clien is
disconnec ed.
- Imp o ed debug messaged and
clean-up: some imes he console
was di icul o ead because some
debug messages appea eally as . I
ha e done small imp o emen s in
his aspec o be able o ead he
debug console in an easie way. I
ha e also added some e o
messages o he And oid clien .
Figu e 31. No ins alled applica ions can’ be enabled.
- Bug ixes: some small ixes and imp o emen s in he pla o m and he clien ,
o a oid c ashes and andom disconnec ions o he use s.
56
Goals
The main goal o OJS is o suppo Social De ices concep , his means ha he
middlewa e is a ge ed o acili a e and inc ease in e ac ions be ween people and
de ices when hey mee ace- o- ace. The objec i e is o coo dina e de ices whe e e
hey a e in he p oximi y o each o he .
Se ice composi ions may be di e en as ano he pla o m, he main sepa a ion is
ha he de ices can independen ly socialize, s a ing o esemble humans, and ge ing
simila cha ac e is ics.
Apa he main goal, he e is a se o ou goals he sys em should mee [8]
(B ons ed, Hansen, & Ings up, Se ice composi ion issues in pe asi e compu ing,
2010):
Con ex Awa eness
The sys em should be able o espond con ex changes by p o iding in o ma ion o
execu ing a command. OJS is specially designed o compose he se ices in an ad hoc
manne as he in e ac ions (ac ions) a e e y momen a y by hei na u e. Fu he mo e,
OJS can epo and s o e he e ogeneous and spa se con ex da a. As we ha e seen, he
pla o m suppo s he implemen a ion o obse e s, which can be used o epo ing and
ecei ing no i ica ions o eac hese changes in he con ex da a. The de elope s can
de ine hei own logic o do ha , and decide how o in o m he use .
Managing Con ingencies
The second goal is o manage con ingencies. OJS ac ions a e Ja aSc ip modules
ha con ain he in e ac ion de ini ion (body), bu also con ain h ee op ional me hods:
p econdi ion, wa ning and e o . I he e o me hod has been de ined i ge s called when
an excep ion is h own in he ac ion body, o in de ice-side code. OJS au oma ically
elays excep ions om de ice o he se e .
- The de elope can use he e o me hod o ace wha happened, and eac
acco dingly, o example in o ming he use . I he e o me hod is no de ined,
he de aul me hod kills he ac ion p ocess.
- The p econdi ion is used o making su e ha he ac ion can ake place, which
helps o p e en he con ingencies al eady be o ehand. Fo example, we can
de ine as a p econdi ion ha a ce ain de ice mus be connec ed, o he
con ex da a has a special alue. So he ac ion won’ be execu ed un il hese
p econdi ions a e no sa is ied.
- The wa ning me hod wo ks simila ly han e o me hod, bu i is used by he
de ice-side de elope o manually in o m he ac ion abou some mino e o
cases. The de aul handle does no kill he ac ion.
57
Le e aging De ice He e ogenei y
Acco ding o he hi d goal, he sys em should dis ibu e esponsibili ies in he
composi ion on he basis o capabili ies o di e en ypes o de ices. One o he main
concep s o he Ac ion-O ien ed P og amming Language a e capabili ies which a e used
o desc ibing wha a de ice can do. This makes i s aigh o wa d o he app de elope
o pick app op ia e de ices o he in e ac ions and se hei oles.
The con ex ual in o ma ion s o ed in egis y, as well as he in o ma ion in social
media and o he pa y se ices, can be accessed wi hin app and in e ac ions. I is
possible o de ine algo i hms o always selec he de ice ha bes i s o each pu pose.
Mo eo e , he cons an ly ex ending suppo o mobile and embedded de ices allows
aking ad an age o many o he e e yday physical objec s o OJS, which also suppo s
he goal as i helps o build cus om de ices o some speci ic pu poses.
Empowe ing Use s
The ou h goal demands ha a pe asi e app should change pu pose and
unc ionali y o e hei li e ime, and also ha he au oma ic solu ions o his do no
always wo k, so he middlewa e should o e means o he use o ecompose he
se ice.
The a chi ec u e o OJS and he concep s o AcOP model allow use s o con ol
in e ac ions in se e al ways. The use can now easily con ol wha applica ion he wan s
o un and wha in e ac ions his phone can ake pa o, by enabling o disabling de ice
capabili ies.
I he use ne e wan s he phone o alk, he can easily disable ha capabili y. I he
de ice is aking pa in an undesi ed in e ac ion, he can easily s op he applica ion.
Example: The Co ee B eak Machine
As we in oduced be o e, he e is an example o Co ee Machine de eloped using he
Social De ices concep , and in eg a ed in he O ches a o .js Pla o m [25]. He e we will
see a small abs ac o how his p o o ype was de eloped. I hink his is a e y
in e es ing p o o ype because i illus a es how he pla o m can be in eg a ed o apply
he In e ne o Things and he In e ne o People models.
In his implemen a ion, Bob is a wo ke ha akes he bus e e y mo ning o his o ice.
His mobile phone de ec s when he is in he bus and ib a es o ask i he wan s o ha e a
co ee when he a i es o he o ice.
The phone sends da a om senso s o De ice Regis y o he O ches a o JS
Pla o m, o example wi h GPS loca ion o nea by WIFI ne wo ks, o know he use is
lea ing home and way o he o ice.
58
Figu e 32. Pa ial code o he Co ee Machine P o o ype.
The use ecei es a no i ica ion asking i he wan s o ha e he co ee, he can always
decline ha ing a co ee, o jus igno e he no i ica ion and i will anish a e a sho
pe iod. The con ex ual da a can be s o ed in any o ma , and hen be p ocessed by he
app. Maybe i ’s necessa y o ansla e he GPS coo dina es o add ess, o calcula e he
dis ance and speed o know when Bob will a i e he o ice.
The De ice Regis y allows s o ing con ex ual da a in any o ma and publishing i
h ough pub/sub API. Co ee B eak app has wo obse e s ha ge no i ied when use ’s
loca ion changes, and when co ee is eady [Figu e 33]. In lines 13 o 18 he applica ion
checks i he use hasn’ ye been asked abou ha ing co ee.
When all he condi ions a e me , he applica ion calls he REST API using he
h p eques lib a y o igge he MakeCo ee ac ion. The second obse e [line 28]
checks when he co eeReady con ex da a a i es om he Co ee Machine, which
indica es ha he co ee is eady. The applica ion will hen igge he In i eFo Co ee
ac ion o in i e Bob and all his colleagues a he o ice o ha e a co ee oge he in he
co ee oom.
59
Figu e 33. O ches a o JS and all he componen s used in he Co ee Machine implemen a ion.
The applica ion was de eloped using he Web IDE and i is a small example o wha
can be done wi h he pla o m. This p o o ype was de eloped using he pla o m
F amewo k o .NET Gadgee e . This amewo k allows o make he echnology
a ailable o di e en kinds o ha dwa e de ices, which can help o in eg a e wi h IoT.
60
67
Ac i i y S a es
An ac i i y can be in one o he di e en s a es du ing he cou se o i s execu ion
wi hin an applica ion:
- Ac i e / Running: The ac i i y is a he op o he Ac i i y S ack and is he
o eg ound ask isible on he de ice sc een, wi h he ocus and in e ac ing wi h
he use . This would be he las ac i i y o be e mina ed in case he de ice is low
o esou ces.
- Paused: The ac i i y is isible o he use , bu does no cu en ly ha e ocus, o
example because he e is a dialog o ano he small ac i i y on op asking o an
ac ion om he use . These paused ac i i ies a e hold in memo y, and can be
quickly es o ed when gains he ocus again.
- S opped: The ac i i y is no isible o he use and i is e ained in memo y like in
paused s a e. Howe e ac i i ies wi h his s a e ha e a highe isk o be
e mina ed in low memo y si ua ions.
- Killed: The ac i i y has been e mina ed by he un ime sys em o ee up
memo y. The ac i i y is no longe p esen in he Ac i i y S ack and has o be
es a ed.
Usually, when he un ime sys em is going o change he s a e o an ac i i y, i sends
a no i ica ion be o e he change. This allows he ac i i y o eac acco dingly. Fo
example, we can sa e all he wo k be o e being killed.
MongoDB
MongoDB [31] is a c oss-pla o m Documen ed-O ien ed Da abase
1
. I is classi ied as
a NoSQL da abase and he da a is sa ed in a s uc u e o JSON-like documen s wi h
dynamic schemas, making he in eg a ion o da a in ce ain ypes o applica ions easie
and as e .
I was de eloped by he so wa e company 10gen (now MongoDB Inc.) in Oc obe
2007 as a componen o a planned Pla o m-as-a-Se ice p oduc . In 2009 i was shi ed
o an open sou ce de elopmen model.
MongoDB ocuses on lexibili y, powe , speed and ease o use [32]:
- Flexibili y: MongoDB s o es da a in JSON documen s, which is se ialized o
BSON (Bina y JSON). JSON p o ides a ich da a model ha seamlessly maps o
na i e p og amming language ypes, and he dynamic schema makes i easie o
e ol e you da a model han wi h a sys em wi h en o ced schemas such as a
Rela ional Da abase.
1
A Documen -O ien ed Da abase is a kind o NoSQL da abase designed o s o ing, e ie ing,
and managing documen -o ien ed in o ma ion, also known as semi-s uc u ed da a [13].
68
- Powe : MongoDB p o ides a lo o he ea u es o a adi ional Rela ional
Da abase Managemen Sys em such as seconda y indexes, dynamic que ies,
so ing, ich upda es, upse s (upda e i exis s, inse i i doesn’ ), and easy
agg ega ion. This gi es he wide unc ionali y om a RDBMS, wi h he lexibili y
and scaling capabili y ha he non- ela ional model allows.
- Speed and scaling: By keeping ela ed da a oge he in documen s, que ies can
be much as e han in a ela ional da abase whe e ela ed da a is sepa a es in o
mul iple ables and hen needs o be joined la e . MongoDB also makes i easy o
scale ou you da abase. Au o-sha ding
1
allows o scale you clus e linea ly by
adding mo e machines. I is possible o inc ease capaci y wi hou any down ime,
which is e y impo an on he web when load can inc ease suddenly and
b inging down he websi e o ex ended main enance can cos you business
la ge amoun s o e enue.
- Ease o use: MongoDB wo ks ha d o be e y easy o ins all, con igu e, main ain,
and use. To his end, MongoDB p o ides ew con igu a ion op ions, and ins ead
ies o au oma ically do he “ igh hing” whene e possible. This means ha
MongoDB wo ks igh ou o he box, and you can di e igh in o de eloping you
applica ion, ins ead o spending a lo o ime ine- uning obscu e da abase
con igu a ions.
Some impo an ea u es ha MongoDB suppo s a e:
- Replica ion: MongoDB p o ides high a ailabili y wi h eplica se s. A eplica se
consis s o wo o mo e copies o he da a. Each eplica se membe may ac in
he ole o p ima y o seconda y eplica a any ime. The p ima y eplica pe o ms
all w i es and eads by de aul . Seconda y eplicas main ain a copy o he da a on
he p ima y using buil -in eplica ion. When a p ima y eplica ails, he eplica se
au oma ically conduc s an elec ion p ocess o de e mine which seconda y should
become he p ima y. Seconda ies can also pe o m ead ope a ions, bu he da a
is e en ually consis en by de aul .
- Agg ega ion: MapReduce can be used o ba ch p ocessing o da a and
agg ega ion ope a ions. The agg ega ion amewo k enables use s o ob ain he
kind o esul s o which he SQL GROUP BY clause is used.
- Se e -side Ja aSc ip execu ion: Ja aSc ip can be used in que ies,
agg ega ion unc ions (such as MapReduce), and sen di ec ly o he da abase o
be execu ed.
- Capped collec ions: MongoDB suppo s ixed-size collec ions called capped
collec ions. This ype o collec ion main ains inse ion o de and, once he
speci ied size has been eached, beha es like a ci cula queue.
1
h p://en.wikipedia.o g/wiki/Sha d_(da abase_a chi ec u e)
69
How i wo ks
MongoDB can be managed in Linux om a command-line e minal. To connec o a
unning MongoDB se e we simply execu e he command:
mongo [--hos localhos ] [--po 21017]
Pa ame e s hos and po a e op ional, i hey a e no speci ied i will au oma ically
connec o a da abase lis ening on po 21017 on he localhos in e ace.
To show a lis o he da abases exis ing in he se e we can execu e he command:
show dbs
We can swi ch o a da abase using he command use and he name o he da abase.
No e ha i he da abase doesn’ exis i will c ea e i au oma ically. Tha ’s why MongoDB
is comple ely lexible DBMS:
use mydb
db
The las command db will show he cu en selec ed da abase. Now you a e using
he new c ea ed da abase called mydb. This new da abase is emp y a his momen , and
i won’ be shown in he lis o da abases e u ned by he show dbs un il any da a is
s o ed on i .
Le ’s inse some da a in JSON (called documen s). C ea ing a new documen is
done in he same way as we would do in Ja aSc ip :
Now we can inse his new documen s in o he da abase. We will inse hem in a
collec ion called myTes :
The show collec ions command will show all he collec ions exis ing in he da abase.
The ind command will ou pu all he documen s exis ing in he speci ied collec ion.
70
We can speci y a gumen s wi h he alue o he keys, so i will e u n only he
documen s ma ching he speci ied sea ch:
Upda ing and dele ing da a is no e y di e en o complica ed. MongoDB p o ides
also mechanisms o i e a e h ough e u ned da a om que ies, called Cu so s.
An impo an example o whe e MongoDB is e y use ul is combined wi h In e ne o
Things [33]. In e ne o Things needs o s o e in o ma ion om huge amoun s o senso s
and objec s, such amoun o da a is oo much o a ela ional da abase.
Bosch has buil i s In e ne o Things sui e on MongoDB, b inging he powe o big
da a o a new ange o Indus ial In e ne applica ions including manu ac u ing,
au omo i e, e ail, ene gy and many o he s.
In e ne o Things is a big challenge o da abases, each new gene a ion o ‘ hings’
comes wi h new senso s, which c ea e new da a and new unc ionali y equi emen s.
Rela ional da abases make i ha d o inco po a e new da a and i e a e on you da a
model. MongoDB can manage da a o any s uc u e, no ma e how o en i changes.
You can ship new unc ionali y wi hou edesigning he da abase, allowing build apps
ha could ne e build be o e.
Also, ela ional da abases a e no designed o handle da a om 40 billion de ices,
while MongoDB is buil o scale ou on commodi y ha dwa e.
Finally, MongoDB can analyse da a o any s uc u e di ec ly wi hin he da abase and
in eal ime. In ela ional da abases you p obably need o p ocess da a and p epa e o
ou pu in he co ec o ma .
Node.js
Node.js [29] is a way o unning Ja aSc ip on he se e , i allows o c ea e Web
applica ions using Ja aSc ip en i ely. Node.js is an in e ace o he V8 Ja aSc ip
un ime, he as Ja aSc ip in e p e e implemen ed by Google ha uns in he Ch ome
b owse .
Con en ional se e s, like Apache, spawns a new h ead o each connec ion o he
se e , bu his doesn’ scale well and i ’s e y easy o quickly lose pe o mance o he
si e goes down. Wi h Node.js his is no he case, you will need o handle he co e se e
a chi ec u e you sel . This can be o e whelming when you s a ou wi h Node.js, bu
he e a e modules o make his easie , and esul will be a high pe o mance web
applica ion.
Ja aSc ip is an e en -based language, so any hing ha happens on he se e
igge s a non-blocking e en . Each new connec ion i es an e en , ecei ed da a om a
o m i es an e en , eques ed da a om he da abase i es an e en . This means a
71
Node.js websi e will ne e lock up and can suppo ens o housands o concu en
use s [9].
In Node.js you don’ ha e a main code ha uns cons an ly in he backg ound, you
don’ ha e o check i some hing has inished o con inue, you will ne e wai o he
esponse o a da abase eques . In Node.js you speci y unc ions ha should be un o
comple ion o some hing else, and ha unc ions will execu e when hey a e igge ed by
he speci ied e en .
Figu e 38. A basic Hello Wo ld p og am in Node.js.
[Figu e 38] shows a Hello Wo ld p og am ha simply p in s he Hello Wo ld! message
when a new connec ion is c ea ed in he po 8080. This basic example can help o
unde s and how asynch onous class wo k.
Node.js is based in an e en d i en model, wha is e en -d i en p og amming? I liked
his desc ip ion om a P ince on Uni e si y Page [15] (E en -d i en p og amming,
2014):
“In compu e p og amming, e en -d i en p og amming o e en -based
p og amming is a p og amming pa adigm in which he low o he p og am is
de e mined by e en s—i.e., senso ou pu s o use ac ions (mouse clicks, key
p esses) o messages om o he p og ams o h eads.
E en -d i en p og amming can also be de ined as an applica ion
a chi ec u e echnique in which he applica ion has a main loop which is clea ly
di ided down o wo sec ions: he i s is e en selec ion (o e en de ec ion), and
he second is e en handling. In embedded sys ems he same may be achie ed
using in e up s ins ead o a cons an ly unning main loop; in ha case he
o me po ion o he a chi ec u e esides comple ely in ha dwa e.
E en -d i en p og ams can be w i en in any language, al hough he ask is
easie in languages ha p o ide high-le el abs ac ions, such as closu es. Some
in eg a ed de elopmen en i onmen s p o ide code gene a ion assis an s ha
au oma e he mos epe i i e asks equi ed o e en handling.”
72
To do a login sys em, we simply call he da abase que y when he use sends he
o m da a, Node.js sends he que y o he da abase, bu i won’ wai o he esponse.
When he esponse om he da abase a i es, we will ha e a di e en unc ion igge ed
when he da a a i es, which will send he esul back o he use . This allows he se e
o execu e ins uc ions ( o ano he use , o example) ins ead o was ing CPU cycles
wai ing o an IO esponse [52].
E en -d i en p og amming is mo e complex han usual p og amming, howe e
Node.js pla o m p o ides abs ac ions and modules ha make i easie o c ea e e en -
d i en applica ions.
Node.js is single h eaded, i can use only 1 CPU a any gi en ime, bu i p o ides
unc ionali y o ha e mul iple p ocesses lis en on he same po . Each indi idual p ocess
can use a single CPU so you can scale ou a mul i-CPU se e by using mul iple
p ocesses. You can also ake ad an age o scaling ac oss mul iple hos by using a
e e se p oxy, load balancing web se e [20].
Figu e 39. How a Node.js applica ion scales on a pe p ocesso basis and ac oss se e s.
Node.js can also ake a huge ad an age o JSON, Ja aSc ip Objec No a ion, which
acili a es da a ansmission be ween he se e and he applica ion wi hou he need o
XML. JSON is ligh weigh , human- eadable and can be pa sed di ec ly in Ja aSc ip .
Node.js is also a good choice o de elop REST ul se ices e y e icien ly and can
handle housands o eques s concu en ly, whe e PHP would jus collapse.
The majo downside o Node.js is ha i is s ill in he ea ly s ages o de elopmen ,
meaning ha some ea u es a e likely o change as de elopmen p og esses. In ac , he
documen a ion includes a s abili y index, which shows how isky use o each ea u e is
cu en ly [Figu e 40].
73
Figu e 40. S abili y index.
In conclusion, Node.js scales much be e han o he pla o ms: consumes less
memo y and is as e . The de elopmen can be complica ed o people no amilia ized
wi h he e en -d i en p og amming model and wi h Ja aSc ip , howe e is a good
al e na i e o de elop small and middle web applica ions wi h housands o connec ed
use s as could be, o example, a REST ul se ice.
In eg a ion wi h MongoDB
Node.js can be easily in eg a ed wi h MongoDB using he package MongoJS, which
le s you access MongoDB using an API ha is ex emely simila o MongoDB’s
Ja aSc ip shell.
The package can be ins alled om NPM simply using he command:
npm ins all mongojs
Le ’s see some examples. Fi s s ep would be he connec ion om Node.js o
MongoDB:
a da abaseU l = "mydb";
a collec ions = ["use s", "messages"];
a db = equi e("mongojs").connec (da abaseU l, collec ions);
This example will c ea e a new connec ion called db connec ed o he da abase
mydb using he de aul hos name and po (localhos and 21017). We can speci y
di e en alues as:
a da abaseU l = "use name:passwo
[email protected]/mydb";
74
The pa ame e collec ions can be used o speci y he se o collec ions ou applica on
uses. This pa ame e is op ional, bu I ecommend o se i because i will allow us o
emula e a MongoDB Ja aSc ip clien like API wi hin ou Node.js applica ion.
Now, o ind a documen he syn ax is e y simila o he MongoDB console:
db.use s. ind({sex: " emale"}, unc ion(e , use s) {
i ( e || !use s) console.log("No emale use s ound");
else use s. o Each( unc ion( emaleUse ) {
console.log( emaleUse );
} );
});
This will e u n all he emale use s. This que y is no like usual que ies in o he
di e en p og amming languages. We a e no wai ing un il he esul o he que y, we a e
sending he que y o he DB and speci ying he unc ion ha will be execu ed when he
da abase e u ns he esul .
Sa ing a new documen in o he MongoDB da abase is also e y simila :
db.use s.sa e({email: “use @email.com”, passwo d: “ es MongoDB”, sex:
“male”}, unc ion(e , sa ed) {
i ( e || !sa ed ) console.log(“Use no sa ed”);
else console.log(“Use sa ed”);
});
Finally, le ’s see how o upda e a documen . In his case we a e changing he
passwo d o he use [email p o ec ed].
db.use s.upda e({email: "use @email.com"}, {$se : {passwo d:
" es Upda e"}}, unc ion(e , upda ed) {
i ( e || !upda ed ) console.log("Use no upda ed");
else console.log("Use upda ed");
});
As you ha e p obably ealized, all he unc ions has a pa ame e e , which will allow
us o de e mine i an e o occu ed, and a pa ame e wi h he esul o he ope a ion. In
he i s que y, he pa ame e use s will include he lis o esul s.
75
Chap e 4
Ge ing s a ed wi h Social De ices
In his chap e we will see how o ins all and con igu e he O ches a o JS Pla o m
and all he needed so wa e o ge he pla o m p epa ed o use and de elop. This guide
explains how o ins all he pla o m and all he equi ed so wa e in Ubun u. I ha e used
a Vi ual Machine o ins all e e y hing, bu i is alid o any e sion and compu e .
No e: I ’s e y impo an o keep he same e sion o some o he dependencies,
especially he npm modules, as he newe e sions ha e changed some names and
unc ions and hey a e no backwa d compa ible.
Dependencies
My es se e uns on Ubun u 14.04 LTS, howe e should be no p oblem in un
ano he Linux Dis ibu ion o e sion. Howe e his u o ial and all he dependencies
included in he CD-ROM a e speci ic o his e sion, and could no be alid in any o he
e sion o dis ibu ion.
- Ubun u 14.04 LTS.
- NodeJS 0.10.25.
- Npm 1.3.10.
- MongoDB Shell e sion 2.4.9.
- O ches a o JS Pla o m om my Gi hub eposi o y
1
.
- And oid Clien o O ches a o JS, which can be downloaded om my eposi o y
2
.
To es he applica ions we would need a leas wo And oid de ices, wi h And oid 4.0
o highe , and Blue oo h capabili ies.
No e: Due he Pla o m is s ill unde de elopmen , some applica ions could s op wo king
i speci ica ions change in he Gi hub eposi o y. I you wan o es hese applica ions
p ope ly I encou age you use my eposi o y ins ead he o icial.
1
h ps://gi hub.com/Jacb667/O ches a o JS.gi
2
h ps://gi hub.com/Jacb667/O ches a o JSAnd oid.gi
76
Ins alla ion
Ins all Node.js
P obably you can ins all i om he eposi o y o you Linux Dis ibu ion, bu i i ’s no
possible he e you ha e one eposi o y you migh use. To add he eposi o y o Ubun u
14.04 you can un a e minal and w i e hese commands:
sudo add-ap - eposi o y ppa:ch is-lea/node.js
sudo ap -ge upda e
Once he eposi o y is success ully added, we can ins all Node.js:
sudo ap -ge ins all py hon-so wa e-p ope ies py hon g++ make nodejs
I you ha e any p oblem o ins all Node.js o you wan o ins all i in a di e en
pla o m you can download and ins all om he Node.js websi e:
h p://nodejs.o g/download/
Ins all MongoDB
As in Node.js, p obably you can ins all i au oma ically om you dis ibu ion’s
eposi o y, i no , you can add his eposi o y o Ubun u and ins all i :
sudo ap -key ad --keyse e hkp://keyse e .ubun u.com:80 -- ec 7F0CEB10
echo ‘deb h p://downloads-dis o.mongodb.o g/ epo/ubun u-ups a dis
10gen’ | sudo ee /e c/ap /sou ces.lis .d/mongodb.lis
sudo ap -ge upda e
sudo ap -ge ins all mongodb-10gen
I you ha e any p oblem you can download and ins all i om he MongoDB websi e:
h p://www.mongodb.o g/downloads
Check i e e y hing is co ec
We can check i all he dependencies was co ec ly ins alled execu ing hese simple
commands in he e minal:
Figu e 41. Te minal sc een in Ubun u showing he e sion o all he ins alled
dependencies.
83
When hese capabili ies a e enabled, we can go o he menu Ac ions
De ini ions
and selec he ac ion called DialogTes . This ac ion [Figu e 47] will send a basic dialog o
he de ice, asking o answe YES o NO. The ques ion will be also said loudly by he
de ice, using he Tex -To-Speech Capabili y called TalkingCapabili y.
The ac ion will hen sleep o one second con inuously while he use answe s he
ques ion, once he use has answe ed, he ac ion lea es he loop and eads he
esponse, which will be ou pu o he se e console. Then, i calls he TalkingCapabili y
again, which will say he chosen op ion o he use .
To execu e he ac ion we can go down o
he a gumen s ield and selec ou de ice in
he lis . This pa ame e will be sen o he
ac ion as he d1 pa ame e o he unc ion.
Then, p essing he bu on igge will send
he ac ion o he de ice.
The de ice will say he ques ion using
he TalkingCapabili y and show he ques ion
and he op ions o choose.
To unde s and how capabili ies wo k, you can go now o he se ings iew in you
de ice and disable he TalkingCapabili y. I you execu e he same ac ion now, i will
show he ques ion and ask he use o an op ion, bu he de ice won’ use he Tex -To-
Speech ea u e.
S a ing he de elopmen
In his sec ion I will explain he mos impo an echnical de ails ha e e y de elope
should know abou he pla o m. I hink his in o ma ion can be a g ea s a ing poin o
any de elope in e es ed in c ea e new applica ions o he O ches a o JS Pla o m.
No e: A he momen o his wo k he e is no u he in o ma ion abou how o de elop
applica ions o he pla o m, all o his in o ma ion is he esul o my in es iga ion, and
could a y in u u e e sions.
Capabili ies
Capabili ies a e he main piece o de elop when s a ing o implemen a new
unc ionali y, as hey a e he only piece ha is implemen ed in he clien de ice, and
hus, he only way o c ea e new ea u es.
We will use he Web IDE o c ea e a new capabili y. To open he IDE, selec he
menu Capabili ies in he main menu o he O ches a o JS main page.
84
[Figu e 48] shows he basic in e ace o a new capabili y called Capabili yName wi h
a unc ion unc ionName.
All he code inside he in e ace should be inside he JSON objec module.expo s.
Inside he objec we can add unc ions in he same way as we c ea e JSON Objec s, in
he o ma unc ionName : unc ion (pa ame e s…).
Figu e 48. Basic code o a new unc ionali y.
We can add as much unc ions as we wan , all o hese unc ions mus be
implemen ed in he clien o a oid p oblems and un ime e o s.
In his example I ha e c ea ed a unc ion called unc ionName wi h 3 pa ame e s:
pa ame e A, pa ame e B, and pa ame e C. The e is no need o speci y he ype o hese
pa ame e s, he clien will map hem au oma ically h ough he name.
Le ’s see how his capabili y would be implemen ed in an And oid clien . Fi s s ep
will be c ea e he igh package o s o e he new
capabili y. This package mus be inside he
package com.ojs.capabili ies and he name o he package mus be capabili yName,
wi h he i s le e in lowe case.
Inside he package we ha e o c ea e he main class o he capabili y. The class has
o be named exac ly he same as he capabili y: Capabili yName.
Figu e 49. Implemen a ion o he me hod unc ionName in Capabili yName.
Pa ame e s o he me hod mus be named as we de ined in he in e ace, bu hey
can be wha e e ype, including a JSONObjec . Also we can speci y a e u n ype. The
e u ned alue will be aken by he se e when execu ing he ac ion, bu he se e
won’ wai o i .
85
We can speci y any hing inside he body o he unc ion. Fo example c ea ing a new
And oid Ac i i y, calling a web se ice, asking an inpu om he use , e c.
The me hod ini Capabili y is manda o y, we ha e o speci y i in he main class o all
he capabili ies. This me hod will be execu ed when he clien connec s o he se e and
i allows o adjus any ini ializa ion se ings he capabili y could need.
[Figu e 50] shows how o send con ex da a o he se e . The con ex da a mus be
a JSONObjec , which can include any kind o da a (JSONA ays, S ings, In ege s,
Booleans, Doubles, Floa s, e c.). This da a needs a name o di e en ia e i in he se e -
side.
Figu e 50. Sending con ex da a o he pla o m.
Ac ions
Le ’s c ea e an ac ion which uses he new capabili y. To c ea e an ac ion om he
Web IDE we can go o he menu Ac ions
De ini ions.
Ac ions ha e o be decla ed also inside he module.expo s objec , and hey a e
eally a new JSON Objec . The body o he ac ion mus be always a unc ion decla ed
wi h he name body.
Addi ional unc ions can be decla ed, o example, an excep ion handle , which will
be execu ed when he de ice h ows an excep ion.
[Figu e 51] shows a small example ac ion which calls he me hod unc ionName om
ou capabili y. I included he lib a y misc.js, which has a sleep me hod ha will be use ul
as he e u n alue om ou capabili y me hod won’ block he ac ion wai ing o he
esul . We need o poll he me hod un il he alue a i es.
I ha e implemen ed an excep ionHandle in his example, his me hod is op ional and
by de aul i will p in he excep ion message in console and s op he ac ion. We can
speci y he e any beha iou o ea he excep ion.
86
Figu e 51. Ac ion example using ou new capabili y Capabili yName.
The body unc ion has a pa ame e called de ice, his will be he de ice o execu e
he ac ion. This pa ame e can be se in he a gumen s lis when igge ing he ac ion.
Applica ions
Finally, we will implemen a simple applica ion which will call ou new Ac ion. Simply
we ha e o go o he Apps menu and selec he bu on NEW APP in he g id. This will
open us he Web IDE wi h an example code o how o implemen basic hings.
An applica ion will basically moni o con ex da a ecei ed om he de ices and
igge ac ions based on ha da a. We can also c ea e h eads (Fibe s) o execu e
pe iodic asks, like poll a websi e o se ice.
[Figu e 53] shows he i s pa o a basic applica ion. The i s we should do is o
include all he equi ed modules om Node.js:
Then, he JSON Objec modules.expo s is decla ed, in he same way o ac ions and
capabili ies, we ha e o de ine he name o he objec and he alue. An applica ion wil
ha e basically wo objec s:
87
- Se ings: his objec will include a lis o pa ame e s equi ed o he applica ion.
This pa ame e s will be eques ed o he use when unning he applica ion
[Figu e 52].
- Logic: his objec con ains
a unc ion wi h he body o
he applica ion.
The se ings de ined can ha e
a de aul alue speci ied.
Figu e 52. When he use uns he applica ion, i will be
asked o in oduce all he pa ame e s de ined in he se ings
objec .
In he example in [Figu e 53] I ha e de ined an obse e [Line 16] which is eading
he online con ex da a. This da a is au oma ically sen o ue when a new de ice is
connec ed o he pla o m.
I he de ice is connec ed (i he con ex Value is se o ue), and he iden i y o he
connec ed de ice is he iden i y he use has speci ied in he pa ame e de iceId, he
p og am will con inue, o he wise i will e u n wi hou execu ing any ins uc ion.
Then, he applica ion will call he REST API using he h p eques module, execu ing
he ac ion Ac ionName on he de ice con ained in he a iable de iceIden i y.
Figu e 53. Example applica ion (pa 1).
88
[Figu e 54] shows he second pa o he code. This pa only c ea es a h ead (Fibe )
ha simply p in s he wo d ick in console e e y 10 seconds.
Figu e 54. Simple example o a Fibe o he applica ion (pa 2).
No e: Always emembe o sa e you code be o e execu ing i . To sa e you code
simply p ess he bu on ‘Push’. I you change some hing in you code and you p ess he
bu on ‘S a ’ o ‘T igge ’ ( o ac ions), i will execu e he code sa ed in he se e , no he
code you a e edi ing in you b owse .
89
Chap e 5
Applica ion hiThe e!
In his chap e I will in oduce he i s s udy applica ion. The applica ion hiThe e! is
he i s o i e applica ions o p o e he po en ial o he pla o m. Also I will explain how
he en i e applica ion, i s ac ions, and i s capabili ies a e de eloped, o help o
unde s and he implemen a ion p ocess.
The goal o his applica ion is o ead he lis o con ac s om he de ice, and
au oma ically send a no i ica ion o he use i one o his con ac s is in he su oundings.
This is achie ed using a Blue oo h de ec ion o nea by de ices.
Each use will be asked o in oduce his mobile phone numbe , his phone numbe
will be mapped o he use ’s iden i y in he pla o m and, consequen ly, o his Blue oo h
MAC add ess, which will be used o de ec he p oximi y.
[Figu e 55] shows how he applica ion a high le el. Now I will explain i s de ails in
deep, as ac ions, capabili ies and con ex da a.
Figu e 55. Ac i i y diag am wi h he beha iou o he applica ion hiThe e!
90
Implemen a ion
I will explain now he mos impo an hings abou he implemen a ion. This
applica ion will use he ac ions: PhoneDialog, Ge Con ac Lis , SendNo i ica ion and
Upda eP oximi y.
The PhoneDialog ac ion will call he me hod askPhoneNumbe om he
phoneNumbe Capabili y.
In he clien his me hod simply s a s a new And oid’s
Ac i i y, which will show a dialog o he use o inpu he
mobile phone numbe . The pla o m sends wo pa ame e s:
a message o show o he use , and a imeou , which
indica es he numbe o seconds o au oma ically close he
Ac i i y i he use doesn’ in oduce he phone numbe .
The Ac i i y will send he phone numbe in oduced by
he use in an asynch onous message, as a con ex da a.
The code in [Figu e 56] shows he me hod o send he
con ex da a o he pla o m.
Figu e 56. F agmen o code ha sends he phoneNumbe in oduced by he use as con ex da a o
he O ches a o JS Pla o m.
91
// De ices wi hou a alid phone numbe canno pa icipa e in his app
pubsub.on( 'phoneNumbe ', unc ion( con ex Value, de iceIden i y )
{
console.log(de iceIden i y + ": " + con ex Value);
phoneMap[de iceIden i y] = con ex Value;
} );
This is he code o he obse e lis ening o he con ex da a called phoneNumbe ,
he applica ion will ecei e he phone numbe in oduced by he use and will s o e i in
an objec indexed wi h he de ice iden i y.
A he end o he applica ion he e is a Fibe ha cons an ly execu es upda ing he
p oximi y o all connec ed de ices. This will i e a e h ough all he connec ed de ices
(de ices which we ha e he phone numbe in he map) and eques hei Blue oo h
p oximi y using he Upda eP oximi y ac ion.
// Upda e Blue oo h isibili y o all connec ed de ices
Fibe ( unc ion()
{
while ( ue )
{
console.log( 'blue oo h ick' );
// I e a e h ough all he de ices and upda e he p oximi y.
Objec .keys(phoneMap). o Each( unc ion(key)
{
console.log( " eques p oximi y on " + key );
a pa ams = {
ac ionName: 'Upda eP oximi y',
pa ame e s: [ 'de ice:' + key ]
};
h p eques ( {
u i: '/api/1/ac ionins ance',
me hod: "POST",
o m: pa ams
}, unc ion( e o , esponse, body ) {} );
});
ools.sleep( 30 );
}
} ). un();
92
Blue oo hCapabili y is as I added Blue oo h suppo o he O ches a o JS Pla o m.
One o he las s e sions o he pla o m has added Blue oo h suppo oo, bu i is
implemen ed as a se ice ha uns cons an ly on he de ice. My capabili y is compa ible
wi h his me hod, bu my capabili y allows he de elope o ask o a Blue oo h upda e
only when i is needed.
This capabili y has some ini ializa ion code in he And oid de ice, ha code is
execu ed when he use connec s o he pla o m. The mos impo an piece o code in
his capabili y is he Blue oo h disco e y:
Figu e 57. Piece o code ha shows how he Blue oo h disco e y is implemen ed.
This Recei e is execu ed o each disco e ed de ice in And oid. Wha I do is s o ing
all he disco e ed de ices in he mDe icesMap, which s o es he Blue oo h MAC
add ess o he de ice and he RSSI, o be used as an app oxima ion o he dis ance
be ween bo h de ices.
The capabili y has a imeou con igu ed by he de elope , a e ha imeou he clien
will send all he disco e ed de ices. In he ac ion Upda eP oximi y I con igu ed his alue
as 10 seconds. Tha means ha he de ice will send all he disco e ed de ices a e 10
seconds, bu i will con inue looking o de ices and when i inishes will send all o hem
again.
The deli e y is done in a simila way as he phone numbe , each de ice is a JSON
A ay wi h he Blue oo h MAC add ess and he RSSI. All he de ices a e s o ed in
ano he JSON A ay, and inally e e y hing is w apped in o a JSON Objec [Figu e 58].
99
I he de ice is disconnec ing om he pla o m, he applica ion will dele e all he
s o ed da a ( he objec s sendedMap and connec edDe ices).
When he use c ea es a new isi ing ca d, i is sen o he pla o m as con ex da a
called isi ingCa d. This da a con ains all he ields and he image in Base64 o ma :
p i a e oid sendVisi ingCa d()
{
S ing name = name.ge Tex (). oS ing();
S ing add ess = add ess.ge Tex (). oS ing();
S ing phone = phone.ge Tex (). oS ing();
S ing desc ip ion = desc ip ion.ge Tex (). oS ing();
S ing email = email.ge Tex (). oS ing();
by e[] b = {};
Bi map bm = Bi mapFac o y.decodeFile(selec edImagePa h);
i (bm == null)
bm = ((Bi mapD awable) image.ge D awable()).ge Bi map();
By eA ayOu pu S eam baos = new By eA ayOu pu S eam();
bm.comp ess(Bi map.Comp essFo ma .JPEG, 50, baos);
b = baos. oBy eA ay();
S ing image = Base64.encodeToS ing(b, Base64.DEFAULT);
y
{
// Objec o be sen
JSONObjec sendDa a = new JSONObjec ();
// Visi ing ca d
JSONObjec isi ingCa d = new JSONObjec ();
isi ingCa d.pu ("name", name);
isi ingCa d.pu ("add ess", add ess);
isi ingCa d.pu ("phone", phone);
isi ingCa d.pu ("email", email);
isi ingCa d.pu ("desc ip ion", desc ip ion);
isi ingCa d.pu ("image", image);
sendDa a.pu (" isi ingCa d", isi ingCa d);
O ches a o JsAc i i y.ojsCon ex Da a(sendDa a);
}
ca ch (JSONExcep ion ex)
{
ex.p in S ackT ace();
Toas .makeTex ( his, "E o !", Toas .LENGTH_SHORT).show();
}
Toas .makeTex ( his, "Visi ing Ca d c ea ed succes ully!",
Toas .LENGTH_SHORT).show();
}
Then, he se e will ecei e his JSON Objec and s o e he isi ing ca d in he
connec edDe ices map, wi h he de ice iden i y as key. This da a is s o ed in he se e
o acili a e he sha e o he in o ma ion be ween de ices wi hou ha ing o eques he
100
in o ma ion om he use each ime. Howe e he da a is emo ed om he se e when
he use disconnec s.
Finally, he las new pa in his code is he p oximi yDe ices. This code wo ks in a
simila way o he code in he las applica ion, howe e in his case he isi ing ca d will
be sen o e e y use (connec ed o he pla o m) in p oximi y. Also I ha e added a
con igu able imeou in se ings, se by de aul o 10 minu es. This means ha he isi ing
ca d will be sen maximum once each 10 minu es.
// Reques da a om p oximi y
pubsub.on( 'p oximi yDe ices', unc ion ( con ex Value, de iceIden i y )
{
i (de iceIden i y in connec edDe ices)
{
// My isi ingCa d o send
a isi ingCa d = connec edDe ices[de iceIden i y];
o ( a i = 0; i < con ex Value.leng h;i++)
{
a de ice = con ex Value[i][0];
i (de ice in connec edDe ices)
{
a now = new Da e().ge Time();
i (sendedMap[de iceIden i y] != null &&
sendedMap[de iceIden i y][de ice] != null &&
sendedMap[de iceIden i y][de ice] > now)
{
console.log("I ha e al eady sen o his de ice!!");
}
else
{
// Don' send o his de ice in a leas 10 minu es
sendedMap[de iceIden i y][de ice] = new Da e().ge Time() +
se ings. imeou ;
a pa ame e s = [ 'de ice:' + de ice, isi ingCa d.name,
isi ingCa d.phone, isi ingCa d.add ess,
isi ingCa d.email, isi ingCa d.desc ip ion,
isi ingCa d.image ]
execu eAc ionDe ice( 'ShowVisi ingCa d', pa ame e s );
No e ha he code has been simpli ied, I ha e emo ed logs and debug in o ma ion
no needed in his example.
101
Ac ions can ecei e only p imi i e da a
(S ings, In ege s, Doubles, Floa s, and
Booleans) as pa ame e s, hey don’ allow
JSON Objec s as pa ame e s, which is he
eason I ha e o send all he ields in he
isi ing ca d one by one ins ead o sending
he en i e objec .
When all he equi emen s a e me he
ac ion ShowVisi ingCa d is execu ed in he
a ge de ice, showing my isi ing ca d o he
o he de ice.
Each de ice will send he isi ing ca d
based on his own p oximi y. This means ha
when wo de ices mee , maybe hey don’
in e change hei isi ing ca ds in he same
momen . Some de ices can ake mo e ime
o disco e nea by de ices.
[Figu e 61] shows he code om
ShowVisi ingCa dAc i i y which akes all he
a gumen s, including he image, and shows
he isi ing ca d o he use .
Figu e 61. Example o code o ead all he a gumen s and show he isi ing ca d o he use . The
image is sen as a S ing codi ied in Base64. This s ing is decoded and showed in an ImageView.
102
Conclusions
This applica ion can be e y use ul o businessmen who ha e a lo o mee ings in a
day. Thei mobile de ices can send he isi ing ca d au oma ically o e e ybody in hei
su oundings. This can be a cheap way o do publici y.
Fo example, he applica ion could un in a small de ice a he en ance o a shop,
and i will send he isi ing ca d o he people who pass in on o he shop. This sys em
could be used o o e discoun coupons o clien s o o make small ad e s.
O cou se, some people can hink his may be dis u bing, howe e hey can disable
he capabili y in any momen and hey will s op ecei ing isi ing ca ds.
103
Chap e 7
Applica ion ollowMe
FollowMe is he hi d applica ion. This basic applica ion asks he use o en e hei
p e e ences in a hash ag way. Then, he applica ion will ge all he use ’s hash ags and
ma ch hem based on he de ices in you p oximi y.
Wi h his applica ion you can easily ind people wi h you in e es s in you
su oundings. When he de ices ind one o mo e common hash ags, hey ask he use s
i hey wan o sha e hei da a wi h he o he use . I bo h use s answe “yes”, hei
social ne wo k da a will be sha ed each o he .
Implemen a ion
When a new de ice connec s o he applica ion, i au oma ically sends wo ac ions.
The i s , Reques hashTags, is a iew o eques he use o in oduce his in e es s. The
second ac ion, Reques SocialDa a, will eques he social ne wo k da a o he use .
Figu e 62. Fi s pa o he ollowMe code applica ion wi h he login obse e .
This da a will be sen by he And oid clien , and ecei e by wo di e en obse e s:
// Obse e o hashTags
pubsub.on( 'hashTags', unc ion( con ex Value, de iceIden i y )
{
de iceHashTags[de iceIden i y] = con ex Value;
console.log("hashTags " + de iceHashTags[de iceIden i y]);
} );
104
// Obse e o socialDa a
pubsub.on( 'socialDa a', unc ion( con ex Value, de iceIden i y )
{
con ac De ails[de iceIden i y] = con ex Value;
console.log("socialDa a " + con ac De ails[de iceIden i y]);
} );
The use has no limi in he numbe o
hash ags he can use. To add a new hash ag he
has o simply p ess he bu on New ag. To
emo e a hash ag he can swipe i om he lis .
Used hash ags a e also sa ed in he And oid
clien , so he use doesn’ ha e o speci y hem
when he disconnec s and connec s again. They
will be he e and he use can send hem back o
he se e o edi hem be o e deli e ing.
Finally, his applica ion also uses he
Upda eP oximi y ac ion, which upda es he
p oximi y o all connec ed de ices each 20
seconds.
This is he code o e ie e he sa ed social
ne wo ks om he And oid de ice:
public oid eques De ails() h ows Excep ion
{
Log.d(O ches a o JsAc i i y.TAG, " eques ing Social Ne wo k da a!");
y
{
JSONObjec sendDa a = new JSONObjec ();
JSONA ay socialDa a = new JSONA ay();
Accoun [] accoun s = Accoun Manage .ge (applica ionCon ex _).ge Accoun s();
o (Accoun accoun : accoun s)
{
i (accoun . ype.equalsIgno eCase("com. wi e .and oid.au h.login"))
{
JSONObjec cuen a = new JSONObjec ();
cuen a.pu (" wi e ", accoun .name);
socialDa a.pu (cuen a);
}
else i (accoun . ype.equalsIgno eCase("com. acebook.au h.login"))
{
JSONObjec cuen a = new JSONObjec ();
cuen a.pu (" acebook", accoun .name);
socialDa a.pu (cuen a);
}
}
sendDa a.pu ("socialDa a", socialDa a);
O ches a o JsAc i i y.ojsCon ex Da a(sendDa a);
105
This code will ead you social ne wo k da a om Twi e and Facebook. Suppo o
mo e accoun s can be p o ided, and an op ion o le he use which o hem he p e e s
o sha e o no .
[Figu e 63] shows he main implemen a ion o he p oximi yDe ices obse e . This is
he main code o he applica ion. This obse e will i e a e all he nea by de ices
epo ed by you de ice, compa ing hei hash ags wi h you s o ind po en ial ma ches.
I has a cool down ime like he las applica ion o a oid sending he no i ica ion
se e al imes o he same use .
Figu e 63. Main implemen a ion o he p oximi yDe ices obse e .
106
Howe e , his applica ion doesn’ use you p oximi y da a o send you in o ma ion
(like he eG ee ings app). In his case, he i s de ice who inds he po en ial hash ag
ma ch will s a he ac ion. The ime will be se in bo h de ices, o a oid o call he ac ion
again when he second de ice’s p oximi y senso inds he cu en de ice.
Once he applica ion has ound all he common hash ags, i will call he ac ion
SendReques In o ma ionDialog wi h 5 pa ame e s: he de ice iden i y I ha e ound, my
own de ice iden i y, he lis o common hash ags, he JSON Objec con e ed o S ing
(S ingi ied) wi h he con ac de ails o he i s de ice, and he con ac de ails o mysel .
[Figu e 64] shows he implemen a ion code o he ac ion. This ac ion will send a
dialog o bo h de ices asking i hey wan o sha e hei con ac de ails wi h each o he .
This is done using he capabili y DialogCapabili y, one o he o iginal capabili ies om
he O ches a o JS Pla o m. This capabili y will c ea e a small dialog wi h he op ions
YES and NO, wi h a imeou o 60 seconds.
I bo h use s answe YES o he sha ing o con ac de ails, bo h de ices will sha e he
in o ma ion each o he and he ac ion will inish.
Figu e 64. Implemen a ion o he ac ion SendReques In o ma ionDialog.
As we ha e seen in p e ious examples, eques ing da a di ec ly om a capabili y is
no synch onized, wha means ha he ac ion won’ wai o he esponse. Tha means
107
ha he ac ion has o poll i cons an ly un il i
ecei es he esponse. This is achie ed
using he sleep me hod om he misc.js
lib a y, which will sleep he unning ac ion o
1 second.
Conclusions
This applica ion can be use ul o mee
new people wi h same in e es s. P obably
e e y day when you a e going o he wo k,
shopping, o home, you pass nea people
wi h you same in e es s.
This sys em can be e y use ul o help
people in he s ee . I someone is looking o
some hing he can con igu e an ale in he
de ice, o example, he is looking o a oom
o en in he neighbo hood. P obably you
a e en ing a oom, so you can con igu e he
ad e in you de ice and when you a e
nea by in he s ee Social De ices will do he es o join you.
108
115
Chap e 9
Applica ion easu eHun
The las applica ion is a small easu e hun ing game whe e he use s can play and
compe e each o he . This applica ion also combines GPS loca ion wi h Blue oo h
p oximi y o know whe e you a e and wha do you ha e in you su oundings.
Each easu e can ha e some in o ma ion like a name, a desc ip ion o hin , he GPS
loca ion, a sco e, e c. The e is also a sco eboa d wi h he sco es o he playe s.
Implemen a ion
T easu es a e s a ic de ices, hey canno mo e and hey can be implemen ed in any
de ice wi h Blue oo h capabili ies. They won’ need o execu e he clien nei he In e ne
connec ion. Fo example, a easu e could be a small A duino o simila de ice wi h
Blue oo h capabili ies.
They a e se in he i s lines o he applica ion:
This applica ion makes use o he ollowing capabili ies:
- Reques Playe P o ile: This is e y simila o he used in he las applica ion, i
eques s da a om he use , like he use name and he a a a ha will be shown
o ano he playe s. This ac ion will call he capabili y loca eCapabili y.
- Reques Loca eT easu e: Ve y simila o he Reques Loca ePe son o he las
applica ion, bu his will show he lis o all nea by easu es, so we can pick one
o s a he hun ing.
- SendPlaye Lis : as he name says, his will be he ac ion esponsible o sending
he sco eboa d o all he playe s. The sco eboa d is sen when a new use
connec s o he applica ion, and each ime a use inds a easu e.
- Se Blue oo hT easu e: simila o Se Blue oo hTa ge , his ac ion will send special
da a like posi ion and Blue oo h MAC add ess o he a ge easu e.
- SendNo i ica ion: basic no i ica ion o in o m he use when some hing was
w ong. Fo example, when he has al eady ound a easu e.
116
Figu e 72. Example o iews. The i s shows all he nea by easu es, he use should selec one o
hem o s a he loca ion. Each easu e shows he dis ance o he cu en posi ion o he use . The
second igu e is an example o he sco eboa d.
The code o he obse e loca eT easu e is simila o he loca ePe son in he las
applica ion, howe e now we ha e o check i he use has al eady ound he selec ed
easu e:
117
Finally, he MapT easu eCapabili yAc i i y will do he same as
MapLoca eCapabili yAc i i y, i will check pe iodically he loca ion o he easu e, gi ing
hin s o he o ien a ion and dis ance. When he easu e is inside he Blue oo h ange,
he sc een will change i s colo acco ding o he dis ance.
When you a e close enough he easu e (abou 2-3 me e s), you will ge you sco e,
and he possibili y o selec ano he easu e o hun . The sco eboa d will be sen o all
he playe s o know you ha e ound a new easu e.
Figu e 73. Code o he obse e oundT easu e.
[Figu e 73] shows he code o he oundT easu e obse e . This obse e will gi e
you he co esponding poin s (5 in his case, bu hey can be con igu ed). Then, i will
ask you o selec a new easu e o hun , and send he
sco eboa d o all he connec e playe s.
You will see a small anima ion [see igu e a igh ]
gi ing you he sco e. This easu e will be au oma ically
disabled o you, so you won’ ge you poin s again.
118
119
Conclusions and u u e wo k
Social De ices is aimed o inc ease, acili a e and en ich social in e ac ions be ween
people in a ious kinds o co-loca ed and ace- o- ace si ua ions. In his wo k I ha e
p esen ed he la es pla o m implemen ing his concep : O ches a o JS. This pla o m
allows o c ea e and manage all he de ices, capabili ies, ac ions and apps h ough he
websi e wi hou he need o ex e nal ools.
Fu he mo e we ha e seen how he concep can be in eg a ed wi h such
echnologies like In e ne o Things and pe asi e compu ing, which a e no he main
a ge o he pla o m, bu has a big po en ial wi h hem.
To acili a e he de elopmen , he pla o m is based in he Ac ion-O ien ed
P og amming Model, which o e s abs ac ions o de elope s ha help hem o s uc u e
pe asi e applica ions. The de elopmen o applica ions is e y lexible and modula
because he main piece a e Ac ions, a small piece o code which can c ea e a small
in e ac ion be ween some de ices. Ac ions a e eusable and easily in eg a ed in any
applica ion.
In his wo k I ha e p esen ed se e al p o o ypes o hi ds using he People-as-a-
Se ice concep o ela ed wi h i . To inish his wo k, I ha e de eloped i e s udy cases
which show he po en ial o he pla o m. These applica ions a e only small examples,
wi h an impo an didac ical objec i e, because a he momen o his wo k he e was no
documen a ion abou how o s a he de elopmen in he pla o m.
In he u u e, he pla o m could ha e new ea u es and capabili ies, ecen ly he
Blue oo h suppo has been added, and p obably GPS suppo will be a ea u e soon.
P obably in he ideogames ield his kind o pla o ms can ha e an impo an u u e,
he e a e wo example ideogames whe e use s can s a he game and in i e people in
hei su oundings o s a playing. Wi h he di e en amewo ks and he REST API, he
echnology can also be ex ended o many di e en ypes o de ices.
Fu he mo e he new Blue oo h 4.0 Low Ene gy can be a pe ec solu ion o a oid he
cu en Blue oo h p oblems, which make he de ices o was e huge amoun s o ene gy
when inding o communica ing wi h nea by de ices. Wi hou Blue oo h Low Ene gy
won’ be possible he deploymen o a echnology like Social De ices.
120
121
Conclusiones y abajo u u o
Social De ices es á o ien ado a aumen a , acili a y en iquece las in e acciones
sociales en e las pe sonas de di e en es o mas coope a i as en si uaciones ca a a
ca a. En es e abajo p esen o la úl ima pla a o ma que implemen a es e concep o:
O ches a o JS. Es a pla a o ma pe mi e c ea y ges iona odos los disposi i os,
capacidades, acciones y aplicaciones desde un si io web sin necesidad de he amien as
ex e nas.
Además hemos is o como el concep o puede se in eg ado con ecnologías como
In e ne de las Cosas y compu ación ubicua, que no son el p incipal obje i o de la
pla a o ma, pe o iene un g an po encial en ellas.
Pa a acili a el desa ollo, la pla a o ma es á basada en el Modelo de P og amación
O ien ado a Acciones, que o ece abs acciones que ayudan a es uc u a aplicaciones
ubicuas. El desa ollo de aplicaciones es muy lexible y modula ya que la pieza
p incipal son las Acciones, un pequeño agmen o de código que capaz de c ea una
pequeña in e acción en e a ios disposi i os. Las acciones son eu ilizables y
ácilmen e in eg ables en cualquie aplicación.
En es e abajo he p esen ado a ios p o o ipos de e ce os u ilizando el concep o de
People-as-a-Se ice o elacionados con él. Pa a e mina es e abajo, he desa ollado
cinco casos de es udio que mues an el po encial de la pla a o ma. Es as aplicaciones
son sólo pequeños ejemplos, con un impo an e obje i o didác ico, ya que en el
momen o de es e abajo no exis e documen ación sob e cómo empeza a desa olla
pa a es a pla a o ma.
En un u u o, la pla a o ma pod ía ene nue as ca ac e ís icas y capacidades,
ecien emen e se ha añadido sopo e Blue oo h, y p obablemen e el sopo e GPS se á
p on o una nue a uncionalidad. P obablemen e en el campo de los ideojuegos es e
ipo de pla a o mas puedan ene un impo an e u u o, exis en dos ideojuegos de
ejemplo donde los usua ios pueden comenza a juga e in i a a pe sonas a su
al ededo pa a juga jun os. Con los di e en es ma cos de abajo y la API REST, la
ecnología puede ambién ex ende se a muchos ipos di e en es de disposi i os.
Además la nue a e sión de Blue oo h 4.0 de baja ene gía puede se la solución
pe ec a pa a e i a los p oblemas del Blue oo h ac ual, que hace que los disposi i os
gas en g andes can idades de ene gía cuando buscan o se comunican con los
disposi i os ce canos. Sin Blue oo h de baja ene gía no se á posible el despliegue de
una ecnología como Social De ices.
122
123
Annex 1: npm modules
Due o he amoun o p oblems can appea because he e a e always new upda es o
he NodeJS lib a ies, I ha e done a dump o all my ins alled modules in npm. The idea is
o keep exac ly he same e sion o all he modules he e appea , o a oid any p oblem.
Fo example, he Socke s.IO lib a y is one o he mos p oblema ic lib a ies, because
om e sion 1.0 i doesn’ o e backwa d compa ibili y and he O ches a o JS Pla o m
won’ wo k as expec ed.
He e is he se o commands o ins all npm modules o he igh e sion. A he
momen hese commands should be enough, bu in he u u e mo e hings could a y.
Howe e he way o ins all old e sions would be he same:
npm ins all colo
[email protected]
npm ins all c [email p o ec ed]
npm ins all ej[email p o ec ed].5
npm ins all exp
[email protected]
npm ins all ibe
[email protected]
npm ins all o e e @0.10.10
npm ins all o e e -moni [email p o ec ed].3
npm ins all
[email protected]
npm ins all
[email protected]
npm ins all mu2exp
[email protected]
npm ins all no[email p o ec ed]
npm ins all no[email p o ec ed].1
npm ins all passpo @0.2.0
npm ins all passpo -loca[email p o ec ed]
npm ins all eques @2.34.0
npm ins all socke .i[email p o ec ed].16
npm ins all socke .io-clien[email p o ec ed]6
npm ins all wins
[email protected]
To ins all he equi ed e sion we only ha e o speci y he name o he module and
he desi ed e sion, sepa a ed wi h ‘@’.
In he nex page is he en i e dump o npm ins alled modules.
124
├── colo
[email protected]
├── c yp
[email protected]
├── [email p o ec ed]
├─┬ exp
[email protected]
│ ├── bu e -[email p o ec ed].1
│ ├── command[email p o ec ed]
│ ├─┬ connec @2.7.11
│ │ ├── by
[email protected]
│ │ ├── cooki[email p o ec ed]
│ │ ├── o mi[email p o ec ed].14
│ │ ├──
[email protected]
│ │ ├──
[email protected]
│ │ └─┬
[email protected]
│ │ └── mim[email p o ec ed]
│ ├──
[email protected]
│ ├── cookie-signa u
[email protected]
│ ├──
[email protected]
│ ├──
[email protected]
│ ├── me
[email protected]
│ ├── mkdi
[email protected]
│ ├── ange-p[email p o ec ed].4
│ └─┬
[email protected]
│ └──
[email protected]
├── ibe
[email protected]
├─┬ o e e @0.10.10
│ ├─┬ cli @0.1.8
│ │ ├──
[email protected]
│ │ └─┬ wins [email p o ec ed]
│ │ ├── asy[email p o ec ed]
│ │ ├── cyc[email p o ec ed]
│ │ ├── pkg[email p o ec ed].3
│ │ ├── eq[email p o ec ed].203
│ │ └── s ack-
[email protected]
│ ├── colo
[email protected]
│ ├─┬ la i o[email p o ec ed]
│ │ ├─┬ b oad[email p o ec ed]
│ │ │ ├── e en emi e [email p o ec ed]
│ │ │ └─┬ win[email p o ec ed].2
│ │ │ ├── a[email p o ec ed].22
│ │ │ ├── c[email p o ec ed].2
│ │ │ ├── e[email p o ec ed]
│ │ │ ├── pkgin
[email protected]
│ │ │ ├── eques @2.9.203
│ │ │ └── s ack- ac[email p o ec ed]
│ │ ├── di ec[email p o ec ed]0
│ │ ├─┬ op im[email p o ec ed]
│ │ │ └── wo [email p o ec ed].2
│ │ └─┬ p omp[email p o ec ed]
│ │ ├─┬ ea[email p o ec ed]
│ │ │ └── mu e-s ea[email p o ec ed]
│ │ ├── e alida o @0.1.5
│ │ └─┬ win[email p o ec ed].2
│ │ ├── a[email p o ec ed].22
│ │ ├── c[email p o ec ed].2
│ │ ├── e[email p o ec ed]
│ │ ├── pkgin
[email protected]
│ │ ├── eques @2.9.203
│ │ └── s ack- ac[email p o ec ed]
│ ├─┬ o e e -moni o @1.2.3
│ │ ├─┬ b oad[email p o ec ed]
│ │ │ ├── e en emi e [email p o ec ed]
│ │ │ ├─┬ nco[email p o ec ed]
│ │ │ │ ├── a[email p o ec ed].9
│ │ │ │ ├── i[email p o ec ed]
│ │ │ │ └─┬ op imis @0.6.0
│ │ │ │ ├── minimis @0.0.5
│ │ │ │ └── wo dw
[email protected]
│ │ │ ├─┬ u i[email p o ec ed]
│ │ │ │ ├── a[email p o ec ed].9
│ │ │ │ ├──
[email protected]
│ │ │ │ ├──
[email protected]
│ │ │ │ ├── m[email p o ec ed].5
│ │ │ │ ├── n[email p o ec ed]
│ │ │ │ └── [email p o ec ed].5
│ │ │ └─┬ win[email p o ec ed].2
│ │ │ ├── a[email p o ec ed].9
│ │ │ ├── c[email p o ec ed].2
│ │ │ ├── e[email p o ec ed]
│ │ │ ├─┬ eques @2.16.6
│ │ │ │ ├──
[email protected]
│ │ │ │ ├── cookie-j[email p o ec ed]
│ │ │ │ ├── o e e -agen @0.2.0
│ │ │ │ ├─┬ o m-da [email p o ec ed]
│ │ │ │ │ └─┬ combined[email p o ec ed].4
│ │ │ │ │ └── delayed-s
[email protected]
│ │ │ │ ├─┬ [email p o ec ed]0.2
│ │ │ │ │ ├──
[email protected]
│ │ │ │ │ ├── c yp i[email p o ec ed].3
│ │ │ │ │ ├──
[email protected]
│ │ │ │ │ └── sn
[email protected]
│ │ │ │ ├── json-s [email p o ec ed]
│ │ │ │ ├── [email p o ec ed].11
│ │ │ │ ├── node-uui[email p o ec ed]
│ │ │ │ ├── oau h-si[email p o ec ed]
│ │ │ │ ├── [email p o ec ed]
│ │ │ │ └── unnel-a[email p o ec ed]
│ │ │ └── s ack- ac[email p o ec ed]
│ │ ├─┬ minim[email p o ec ed].12
│ │ │ ├── l
[email protected]
│ │ │ └── sig[email p o ec ed].0
│ │ ├─┬ ps- [email p o ec ed]
│ │ │ └─┬ e en -s
[email protected]
│ │ │ └─┬ op imis @0.2.8
│ │ │ └── wo dw
[email protected]
│ │ └── wa
[email protected]
│ ├─┬ ncon @0.6.7
│ │ ├──
[email protected]
│ │ ├──
[email protected]
│ │ ├─┬ op im[email p o ec ed]
│ │ │ └── wo [email p o ec ed].2
│ │ └── pkgin[email p o ec ed]
│ ├─┬ nssocke[email p o ec ed]
│ │ ├── e en emi e
[email protected]
│ │ └──
[email protected]
│ ├─┬ op imis[email p o ec ed]
│ │ └── wo dw[email p o ec ed]
│ ├── pkgin
[email protected]
│ ├── imespa[email p o ec ed]