designiot.mobi 280 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406
  1. <div id="TOC">
  2. <ul>
  3. <li><a href="#前言"><span class="toc-section-number">1</span> 前言</a>
  4. <ul>
  5. <li><a href="#目标读者"><span class="toc-section-number">1.1</span> 目标读者</a></li>
  6. <li><a href="#不适合人群"><span class="toc-section-number">1.2</span> 不适合人群</a></li>
  7. <li><a href="#介绍"><span class="toc-section-number">1.3</span> 介绍</a>
  8. <ul>
  9. <li><a href="#为什么没有c"><span class="toc-section-number">1.3.1</span> 为什么没有C ?</a></li>
  10. <li><a href="#为什么不是java"><span class="toc-section-number">1.3.2</span> 为什么不是JAVA ?</a></li>
  11. </ul></li>
  12. <li><a href="#如何阅读"><span class="toc-section-number">1.4</span> 如何阅读</a></li>
  13. </ul></li>
  14. <li><a href="#无处不在的html"><span class="toc-section-number">2</span> 无处不在的HTML</a>
  15. <ul>
  16. <li><a href="#html的helloworld"><span class="toc-section-number">2.1</span> html的hello,world</a>
  17. <ul>
  18. <li><a href="#调试helloworld"><span class="toc-section-number">2.1.1</span> 调试hello,world</a></li>
  19. <li><a href="#说说helloworld"><span class="toc-section-number">2.1.2</span> 说说hello,world</a></li>
  20. <li><a href="#想用中文"><span class="toc-section-number">2.1.3</span> 想用中文?</a></li>
  21. </ul></li>
  22. <li><a href="#其他html标记"><span class="toc-section-number">2.2</span> 其他html标记</a>
  23. <ul>
  24. <li><a href="#美妙之处"><span class="toc-section-number">2.2.1</span> 美妙之处</a></li>
  25. <li><a href="#更多"><span class="toc-section-number">2.2.2</span> 更多</a></li>
  26. </ul></li>
  27. </ul></li>
  28. <li><a href="#无处不在的javascript"><span class="toc-section-number">3</span> 无处不在的Javascript</a>
  29. <ul>
  30. <li><a href="#javascript的helloworld"><span class="toc-section-number">3.1</span> Javascript的Hello,world</a></li>
  31. <li><a href="#更js一点"><span class="toc-section-number">3.2</span> 更js一点</a>
  32. <ul>
  33. <li><a href="#从数学出发"><span class="toc-section-number">3.2.1</span> 从数学出发</a></li>
  34. </ul></li>
  35. <li><a href="#设计和编程"><span class="toc-section-number">3.3</span> 设计和编程</a>
  36. <ul>
  37. <li><a href="#函数"><span class="toc-section-number">3.3.1</span> 函数</a></li>
  38. <li><a href="#重新设计"><span class="toc-section-number">3.3.2</span> 重新设计</a></li>
  39. <li><a href="#object和函数"><span class="toc-section-number">3.3.3</span> object和函数</a></li>
  40. <li><a href="#面向对象"><span class="toc-section-number">3.3.4</span> 面向对象</a></li>
  41. </ul></li>
  42. <li><a href="#其他"><span class="toc-section-number">3.4</span> 其他</a></li>
  43. <li><a href="#美妙之处-1"><span class="toc-section-number">3.5</span> 美妙之处</a></li>
  44. </ul></li>
  45. <li><a href="#无处不在的css"><span class="toc-section-number">4</span> 无处不在的CSS</a>
  46. <ul>
  47. <li><a href="#css"><span class="toc-section-number">4.1</span> CSS</a></li>
  48. <li><a href="#关于css"><span class="toc-section-number">4.2</span> 关于CSS</a></li>
  49. <li><a href="#代码结构"><span class="toc-section-number">4.3</span> 代码结构</a></li>
  50. <li><a href="#样式与目标"><span class="toc-section-number">4.4</span> 样式与目标</a>
  51. <ul>
  52. <li><a href="#选择器"><span class="toc-section-number">4.4.1</span> 选择器</a></li>
  53. </ul></li>
  54. <li><a href="#更有趣的css"><span class="toc-section-number">4.5</span> 更有趣的CSS</a></li>
  55. </ul></li>
  56. <li><a href="#无处不在的三剑客"><span class="toc-section-number">5</span> 无处不在的三剑客</a>
  57. <ul>
  58. <li><a href="#hellogeek"><span class="toc-section-number">5.1</span> Hello,Geek</a></li>
  59. <li><a href="#从源码学习"><span class="toc-section-number">5.2</span> 从源码学习</a></li>
  60. <li><a href="#浏览器渲染过程"><span class="toc-section-number">5.3</span> 浏览器渲染过程</a>
  61. <ul>
  62. <li><a href="#html"><span class="toc-section-number">5.3.1</span> HTML</a></li>
  63. </ul></li>
  64. <li><a href="#dom树形结构图"><span class="toc-section-number">5.4</span> DOM树形结构图</a>
  65. <ul>
  66. <li><a href="#javascript"><span class="toc-section-number">5.4.1</span> javascript</a></li>
  67. <li><a href="#css-1"><span class="toc-section-number">5.4.2</span> CSS</a></li>
  68. </ul></li>
  69. <li><a href="#css盒模型图"><span class="toc-section-number">5.5</span> CSS盒模型图</a></li>
  70. <li><a href="#笔记"><span class="toc-section-number">5.6</span> 笔记</a></li>
  71. </ul></li>
  72. <li><a href="#gnulinux-强大且free"><span class="toc-section-number">6</span> GNU/Linux 强大且Free</a>
  73. <ul>
  74. <li><a href="#什么是linux"><span class="toc-section-number">6.1</span> 什么是Linux</a></li>
  75. <li><a href="#操作系统"><span class="toc-section-number">6.2</span> 操作系统</a>
  76. <ul>
  77. <li><a href="#linux架构图"><span class="toc-section-number">6.2.1</span> Linux架构图</a></li>
  78. <li><a href="#shell"><span class="toc-section-number">6.2.2</span> Shell</a></li>
  79. <li><a href="#gcc"><span class="toc-section-number">6.2.3</span> GCC</a></li>
  80. <li><a href="#启动引导程序"><span class="toc-section-number">6.2.4</span> 启动引导程序</a></li>
  81. </ul></li>
  82. <li><a href="#从编译开始"><span class="toc-section-number">6.3</span> 从编译开始</a>
  83. <ul>
  84. <li><a href="#开始之前"><span class="toc-section-number">6.3.1</span> 开始之前</a></li>
  85. <li><a href="#编译nginx"><span class="toc-section-number">6.3.2</span> 编译Nginx</a></li>
  86. <li><a href="#其他-1"><span class="toc-section-number">6.3.3</span> 其他</a></li>
  87. </ul></li>
  88. <li><a href="#包管理"><span class="toc-section-number">6.4</span> 包管理</a></li>
  89. <li><a href="#ubuntu-lnmp"><span class="toc-section-number">6.5</span> Ubuntu LNMP</a>
  90. <ul>
  91. <li><a href="#update软件包列表"><span class="toc-section-number">6.5.1</span> Update软件包列表</a></li>
  92. <li><a href="#安装mysql"><span class="toc-section-number">6.5.2</span> 安装MySQL</a></li>
  93. <li><a href="#安装nginx"><span class="toc-section-number">6.5.3</span> 安装Nginx</a></li>
  94. <li><a href="#安装php"><span class="toc-section-number">6.5.4</span> 安装PHP</a></li>
  95. </ul></li>
  96. </ul></li>
  97. <li><a href="#arduino-极客的玩具"><span class="toc-section-number">7</span> Arduino 极客的玩具</a>
  98. <ul>
  99. <li><a href="#极客的玩具"><span class="toc-section-number">7.1</span> 极客的玩具</a></li>
  100. <li><a href="#硬件熟悉"><span class="toc-section-number">7.2</span> 硬件熟悉</a></li>
  101. <li><a href="#开发环境"><span class="toc-section-number">7.3</span> 开发环境</a></li>
  102. <li><a href="#点亮一个led"><span class="toc-section-number">7.4</span> 点亮一个LED</a></li>
  103. <li><a href="#串口通信"><span class="toc-section-number">7.5</span> 串口通信</a>
  104. <ul>
  105. <li><a href="#关于arduino-setup"><span class="toc-section-number">7.5.1</span> 关于Arduino Setup()</a></li>
  106. </ul></li>
  107. </ul></li>
  108. <li><a href="#python-代码如散文"><span class="toc-section-number">8</span> Python 代码如散文</a>
  109. <ul>
  110. <li><a href="#代码与散文"><span class="toc-section-number">8.1</span> 代码与散文</a>
  111. <ul>
  112. <li><a href="#开始之前-1"><span class="toc-section-number">8.1.1</span> 开始之前</a></li>
  113. <li><a href="#python的helloworld"><span class="toc-section-number">8.1.2</span> Python的Hello,World</a></li>
  114. <li><a href="#我们想要的helloworld"><span class="toc-section-number">8.1.3</span> 我们想要的Hello,World</a></li>
  115. </ul></li>
  116. <li><a href="#算法"><span class="toc-section-number">8.2</span> 算法</a></li>
  117. <li><a href="#实用主义哲学"><span class="toc-section-number">8.3</span> 实用主义哲学</a></li>
  118. <li><a href="#包管理-1"><span class="toc-section-number">8.4</span> 包管理</a>
  119. <ul>
  120. <li><a href="#python-requests"><span class="toc-section-number">8.4.1</span> python requests</a></li>
  121. </ul></li>
  122. </ul></li>
  123. <li><a href="#raspberry-pi"><span class="toc-section-number">9</span> Raspberry Pi</a>
  124. <ul>
  125. <li><a href="#geek的盛宴"><span class="toc-section-number">9.1</span> Geek的盛宴</a></li>
  126. <li><a href="#raspberry-pi-初始化"><span class="toc-section-number">9.2</span> Raspberry Pi 初始化</a></li>
  127. <li><a href="#raspberry-pi-gpio"><span class="toc-section-number">9.3</span> Raspberry Pi GPIO</a></li>
  128. </ul></li>
  129. <li><a href="#server-一切皆为服务"><span class="toc-section-number">10</span> Server 一切皆为服务</a>
  130. <ul>
  131. <li><a href="#服务器"><span class="toc-section-number">10.1</span> 服务器</a></li>
  132. <li><a href="#web服务器"><span class="toc-section-number">10.2</span> Web服务器</a></li>
  133. <li><a href="#lnmp"><span class="toc-section-number">10.3</span> LNMP</a></li>
  134. </ul></li>
  135. <li><a href="#web服务"><span class="toc-section-number">11</span> Web服务</a>
  136. <ul>
  137. <li><a href="#soap-vs-restful"><span class="toc-section-number">11.1</span> SOAP VS RESTful</a></li>
  138. </ul></li>
  139. <li><a href="#http-熟悉陌生"><span class="toc-section-number">12</span> HTTP 熟悉&amp;陌生</a>
  140. <ul>
  141. <li><a href="#你所没有深入的http"><span class="toc-section-number">12.1</span> 你所没有深入的HTTP</a>
  142. <ul>
  143. <li><a href="#打开网页时发生了什么"><span class="toc-section-number">12.1.1</span> 打开网页时发生了什么</a></li>
  144. <li><a href="#url组成"><span class="toc-section-number">12.1.2</span> URL组成</a></li>
  145. </ul></li>
  146. <li><a href="#一次http-get请求"><span class="toc-section-number">12.2</span> 一次HTTP GET请求</a>
  147. <ul>
  148. <li><a href="#http响应"><span class="toc-section-number">12.2.1</span> HTTP响应</a></li>
  149. </ul></li>
  150. </ul></li>
  151. <li><a href="#设计restful-api"><span class="toc-section-number">13</span> 设计RESTful API</a>
  152. <ul>
  153. <li><a href="#资源"><span class="toc-section-number">13.0.1</span> 资源</a></li>
  154. </ul></li>
  155. <li><a href="#设计restful-api-1"><span class="toc-section-number">14</span> 设计RESTful API</a>
  156. <ul>
  157. <li><a href="#rest关键目标"><span class="toc-section-number">14.1</span> REST关键目标</a></li>
  158. <li><a href="#判断是否是-restful的约束条件"><span class="toc-section-number">14.2</span> 判断是否是 RESTful的约束条件</a></li>
  159. <li><a href="#json"><span class="toc-section-number">14.3</span> JSON</a></li>
  160. </ul></li>
  161. <li><a href="#环境准备"><span class="toc-section-number">15</span> 环境准备</a>
  162. <ul>
  163. <li><a href="#laravel"><span class="toc-section-number">15.1</span> Laravel</a>
  164. <ul>
  165. <li><a href="#为什么是-laravel"><span class="toc-section-number">15.1.1</span> 为什么是 Laravel</a></li>
  166. </ul></li>
  167. <li><a href="#安装-laravel"><span class="toc-section-number">15.2</span> 安装 Laravel</a>
  168. <ul>
  169. <li><a href="#gnulinux安装composer"><span class="toc-section-number">15.2.1</span> GNU/Linux安装Composer</a></li>
  170. </ul></li>
  171. <li><a href="#mysql"><span class="toc-section-number">15.3</span> MySQL</a>
  172. <ul>
  173. <li><a href="#安装mysql-1"><span class="toc-section-number">15.3.1</span> 安装MySQL</a></li>
  174. <li><a href="#配置mysql"><span class="toc-section-number">15.3.2</span> 配置MySQL</a></li>
  175. </ul></li>
  176. </ul></li>
  177. <li><a href="#创建rest服务"><span class="toc-section-number">16</span> 创建REST服务</a>
  178. <ul>
  179. <li><a href="#数据库迁移"><span class="toc-section-number">16.1</span> 数据库迁移</a>
  180. <ul>
  181. <li><a href="#创建表"><span class="toc-section-number">16.1.1</span> 创建表</a></li>
  182. <li><a href="#数据库迁移-1"><span class="toc-section-number">16.1.2</span> 数据库迁移</a></li>
  183. </ul></li>
  184. <li><a href="#创建restful"><span class="toc-section-number">16.2</span> 创建RESTful</a></li>
  185. <li><a href="#laravel-resources"><span class="toc-section-number">16.3</span> Laravel Resources</a>
  186. <ul>
  187. <li><a href="#修改create"><span class="toc-section-number">16.3.1</span> 修改Create()</a></li>
  188. <li><a href="#创建表单"><span class="toc-section-number">16.3.2</span> 创建表单</a></li>
  189. <li><a href="#编辑模板"><span class="toc-section-number">16.3.3</span> 编辑模板</a></li>
  190. </ul></li>
  191. </ul></li>
  192. <li><a href="#前端显示"><span class="toc-section-number">17</span> 前端显示</a>
  193. <ul>
  194. <li><a href="#库与车轮子"><span class="toc-section-number">17.1</span> 库与车轮子</a></li>
  195. <li><a href="#库"><span class="toc-section-number">17.2</span> 库</a>
  196. <ul>
  197. <li><a href="#jquery"><span class="toc-section-number">17.2.1</span> jQuery</a></li>
  198. <li><a href="#jquery-mobile"><span class="toc-section-number">17.2.2</span> jQuery Mobile</a></li>
  199. </ul></li>
  200. <li><a href="#网站前台显示"><span class="toc-section-number">17.3</span> 网站前台显示</a>
  201. <ul>
  202. <li><a href="#highcharts"><span class="toc-section-number">17.3.1</span> Highcharts</a></li>
  203. <li><a href="#实时数据"><span class="toc-section-number">17.3.2</span> 实时数据</a></li>
  204. </ul></li>
  205. </ul></li>
  206. <li><a href="#restful的coap协议"><span class="toc-section-number">18</span> RESTful的CoAP协议</a>
  207. <ul>
  208. <li><a href="#coap-嵌入式系统的rest"><span class="toc-section-number">18.1</span> CoAP: 嵌入式系统的REST</a></li>
  209. <li><a href="#coap-命令行工具"><span class="toc-section-number">18.2</span> CoAP 命令行工具</a>
  210. <ul>
  211. <li><a href="#node-coap-cli"><span class="toc-section-number">18.2.1</span> Node CoAP CLI</a></li>
  212. <li><a href="#libcoap"><span class="toc-section-number">18.2.2</span> libcoap</a></li>
  213. <li><a href="#firefox-copper"><span class="toc-section-number">18.2.3</span> Firefox Copper</a></li>
  214. </ul></li>
  215. <li><a href="#coap-helloworld"><span class="toc-section-number">18.3</span> CoAP Hello,World</a></li>
  216. <li><a href="#coap-数据库查询"><span class="toc-section-number">18.4</span> CoAP 数据库查询</a>
  217. <ul>
  218. <li><a href="#node-module"><span class="toc-section-number">18.4.1</span> Node Module</a></li>
  219. <li><a href="#node-sqlite3"><span class="toc-section-number">18.4.2</span> Node-Sqlite3</a></li>
  220. <li><a href="#查询数据"><span class="toc-section-number">18.4.3</span> 查询数据</a></li>
  221. </ul></li>
  222. <li><a href="#coap-block"><span class="toc-section-number">18.5</span> CoAP Block</a>
  223. <ul>
  224. <li><a href="#coap-post"><span class="toc-section-number">18.5.1</span> CoAP POST</a></li>
  225. <li><a href="#coap-content-types"><span class="toc-section-number">18.5.2</span> CoAP Content Types</a></li>
  226. </ul></li>
  227. <li><a href="#coap-json"><span class="toc-section-number">18.6</span> CoAP JSON</a></li>
  228. <li><a href="#使用iot-coap构建物联网"><span class="toc-section-number">18.7</span> 使用IoT-CoAP构建物联网</a></li>
  229. </ul></li>
  230. <li><a href="#简单物联网"><span class="toc-section-number">19</span> 简单物联网</a>
  231. <ul>
  232. <li><a href="#硬件通信"><span class="toc-section-number">19.1</span> 硬件通信</a>
  233. <ul>
  234. <li><a href="#串口通信-1"><span class="toc-section-number">19.1.1</span> 串口通信</a></li>
  235. </ul></li>
  236. <li><a href="#硬件"><span class="toc-section-number">19.2</span> 硬件</a>
  237. <ul>
  238. <li><a href="#arduino"><span class="toc-section-number">19.2.1</span> Arduino</a></li>
  239. <li><a href="#继电器"><span class="toc-section-number">19.2.2</span> 继电器</a></li>
  240. </ul></li>
  241. </ul></li>
  242. <li><a href="#android简单示例"><span class="toc-section-number">20</span> Android简单示例</a>
  243. <ul>
  244. <li><a href="#调用web-services-get"><span class="toc-section-number">20.1</span> 调用Web Services GET</a>
  245. <ul>
  246. <li><a href="#创建restclient"><span class="toc-section-number">20.1.1</span> 创建RESTClient</a></li>
  247. </ul></li>
  248. <li><a href="#使用rest-client获取结果"><span class="toc-section-number">20.2</span> 使用REST Client获取结果</a></li>
  249. </ul></li>
  250. <li><a href="#尾声"><span class="toc-section-number">21</span> 尾声</a>
  251. <ul>
  252. <li><a href="#路"><span class="toc-section-number">21.1</span> 路</a></li>
  253. <li><a href="#其他-2"><span class="toc-section-number">21.2</span> 其他</a></li>
  254. </ul></li>
  255. </ul>
  256. </div>
  257. <p>本作品采用<a href="http://creativecommons.org/licenses/by-nc/4.0/">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可。<img src="./images/88x31.png" alt="cc" /></p>
  258. <p>© 2014 <a href="http://www.phodal.com">Phodal Huang</a>.</p>
  259. <h1 id="前言"><span class="header-section-number">1</span> 前言</h1>
  260. <p>设计物联网系统是件有意思的事情,它需要考虑到软件、硬件、通讯等多个不同方面。通过探索不同的语言,不同的框架,从而形成不同的解决方案。</p>
  261. <p>在这里,我们将对设计物联网系统有一个简单的介绍,并探讨如何设计一个最小的物联网系统。</p>
  262. <h2 id="目标读者"><span class="header-section-number">1.1</span> 目标读者</h2>
  263. <p>目标读者: 初入物联网领域,希望对物联网系统有一个大概的认识和把握,并学会掌握一个基础的物联网系统的设计。</p>
  264. <ul>
  265. <li>硬件开发人员,对物联网有兴趣。</li>
  266. <li>没有web开发经验</li>
  267. <li>几乎为0的linux使用经验</li>
  268. <li>想快速用于生产环境<br /></li>
  269. <li>对硬件了解有限的开发人员。</li>
  270. <li>没接触过51、ARM、Arduino<br /></li>
  271. <li>想了解以下内容:</li>
  272. <li>RESTful与IOT</li>
  273. <li>CoAP协议</li>
  274. <li>MQTT</li>
  275. </ul>
  276. <p>本文档对一些概念(如)只做了一些基本介绍,以方便读者理解。如果您想进一步了解这些知识,会列出一些推荐书目,以供参考。</p>
  277. <h2 id="不适合人群"><span class="header-section-number">1.2</span> 不适合人群</h2>
  278. <ul>
  279. <li>如果你是在这方面已经有了丰富经验的开发者。</li>
  280. <li>不是为了学习而学习这方面的知识。</li>
  281. </ul>
  282. <h2 id="介绍"><span class="header-section-number">1.3</span> 介绍</h2>
  283. <p>关于内容的选择,这是一个有意思的话题,因为我们很难判断不同的开发者用的是怎样的语言,用的是怎样的框架。</p>
  284. <p>于是我们便自作主张地选择了那些适合于理论学习的语言、框架、硬件,去除掉其他一些我们不需要考虑的因素,如语法,复杂度等等。当然,这些语言、框架、硬件也是最流行的。</p>
  285. <ul>
  286. <li>Arduino: 如果你从头开始学过硬件的话,那么你会爱上它的。</li>
  287. <li>Raspberry PI: 如果你从头编译过GNU/Linux的话,我想你会爱上她的。</li>
  288. <li>Python: 简单地来说,你可以方便地使用一些扩展,同时代码就表达了你的想法。</li>
  289. <li>PHP : 这是一门容易部署的语言,我想你只需要在你的Ubuntu机器上,执行一下脚本就能完成安装了。而且,如果你是一个硬件开发者的话,你会更容易地找到其他开发者。</li>
  290. <li>Javascript : 考虑到javascript这门语言已经无处不在了,而且会更加流行。所以,在这里CoAP、MQTT等版本是基于Nodejs的。</li>
  291. <li>HTML、CSS : 这是必须的,同样,他们仍然无处不在。</li>
  292. <li>GNU/Linux: 作为部署到服务器的一部分——你需要掌握他。当然如果你要用WAMP也是可以的。</li>
  293. <li>CoAP: 用NodeJS构建IOT CoAP物联网</li>
  294. </ul>
  295. <h3 id="为什么没有c"><span class="header-section-number">1.3.1</span> 为什么没有C ?</h3>
  296. <p>如果你还想用C学理论的话,烦请出门左拐。我想,C并不适合于学习相关硬件的理论。</p>
  297. <h3 id="为什么不是java"><span class="header-section-number">1.3.2</span> 为什么不是JAVA ?</h3>
  298. <p>大有以下两个原因</p>
  299. <ul>
  300. <li>学习JAVA的人很多,但是它不适合我们集中精力构建与学习,因为无关的代码太多了。</li>
  301. <li>之前以及现在,我还是不喜欢JAVA (我更喜欢脚本语言,可以提高工作效率)。</li>
  302. </ul>
  303. <h2 id="如何阅读"><span class="header-section-number">1.4</span> 如何阅读</h2>
  304. <p>这只是一个小小的建议,仅针对于在选择阅读上没有经验的读者。</p>
  305. <table>
  306. <thead>
  307. <tr class="header">
  308. <th align="left">当前状态</th>
  309. <th align="left">建议</th>
  310. </tr>
  311. </thead>
  312. <tbody>
  313. <tr class="odd">
  314. <td align="left">软件初学者</td>
  315. <td align="left">从头阅读</td>
  316. </tr>
  317. <tr class="even">
  318. <td align="left">硬件开发者</td>
  319. <td align="left">从头阅读</td>
  320. </tr>
  321. <tr class="odd">
  322. <td align="left">没有web经验的开发者</td>
  323. <td align="left">从第二部分开始</td>
  324. </tr>
  325. </tbody>
  326. </table>
  327. <p>我们会在前面十章里简单介绍一些必要的基础知识,这些知识将会在后面我们构建物联网系统时用到。</p>
  328. <p>某一天,正走在回学校的路上的我突然想到:“未来将会是一个科技的时代——虽然现在也是——只是在未来,科技将会无处不在。如果我们依旧对周围这些无处不在的代码一无所知的话,或许我们会成为黑客帝国之中被控制的普通人。”于是开始想着,有一天人们会像学习一门语言一样开始学习编程,直到又有一天我看到了学习编程如同学习一门语言的说法。这又恰好在我做完最小物联网系统之后,算是一个有趣的时间点,我开始想着像之前做最小物联网系统的那些步骤一样,写一个简单的入门。也可以补充好之前在这个最小物联网系统缺失的那些东西,给那些正在开始试图去解决编程问题的人。</p>
  329. <p>让我们先从身边的语言下手,也就是现在无处不在的html+javascript+css。</p>
  330. <h1 id="无处不在的html"><span class="header-section-number">2</span> 无处不在的HTML</h1>
  331. <p>之所以从html开始,是因为我们不需要配置一个复杂的开发环境,也许你还不知道开发环境是什么东西,不过这也没关系,毕竟这些知识需要慢慢的接触才能有所了解,尤其是对于普通的业余爱好者来说,当然,对于专业选手言自然不是问题。HTML是Web的核心语言,也算是比较基础的语言。</p>
  332. <h2 id="html的helloworld"><span class="header-section-number">2.1</span> html的hello,world</h2>
  333. <p>Hello,world是一个传统,所以在这里也遵循这个有趣的传统,我们所要做的事情其实很简单,虽然也有一点点hack的感觉。——让我们先来新建一个文并命名为“helloworld.html”。</p>
  334. <p>(PS:大部分人应该都是在windows环境下工作的,所以你需要新建一个文本,然后重命名,或者你需要一个编辑器,在这里我们推荐用<strong>sublime text</strong>。破解不破解,注册不注册都不会对你的使用有太多的影响。)</p>
  335. <ol>
  336. <li><p>新建文件</p></li>
  337. <li><p>输入</p>
  338. <pre><code>hello,world</code></pre></li>
  339. <li><p>保存为-&gt;“helloworld.html”,</p></li>
  340. <li><p>双击打开这个文件。 正常情况下都应该是用你的默认浏览器打开。只要是一个正常工作的现代浏览器,都应该可以看到上面显示的是“Hello,world”。</p></li>
  341. </ol>
  342. <p>这才是最短的hello,world程序,但是呢?在ruby中会是这样子的</p>
  343. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">2.0.0-p353</span> :001 <span class="kw">&gt;</span> p <span class="st">&quot;hello,world&quot;</span>
  344. <span class="st">&quot;hello,world&quot;</span>
  345. =<span class="kw">&gt;</span> <span class="st">&quot;hello,world&quot;</span>
  346. <span class="kw">2.0.0-p353</span> :002 <span class="kw">&gt;</span></code></pre>
  347. <p>等等,如果你了解过html的话,会觉得这一点都不符合语法规则,但是他工作了,没有什么比安装完Nginx后看到It works!更让人激动了。</p>
  348. <p>遗憾的是,它可能无法在所有的浏览器上工作,所以我们需要去调试其中的bug。</p>
  349. <h3 id="调试helloworld"><span class="header-section-number">2.1.1</span> 调试hello,world</h3>
  350. <p>我们会发现我们的代码在浏览器中变成了下面的代码,如果你和我一样用的是chrome,那么你可以右键浏览器中的空白区域,点击审查元素,就会看到下面的代码。</p>
  351. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  352. <span class="kw">&lt;head&gt;&lt;/head&gt;</span>
  353. <span class="kw">&lt;body&gt;</span>hello,world<span class="kw">&lt;/body&gt;</span>
  354. <span class="kw">&lt;/html&gt;</span></code></pre>
  355. <p>这个才是真正能在大部分浏览器上工作的代码,所以复制它到编辑器里吧。</p>
  356. <h3 id="说说helloworld"><span class="header-section-number">2.1.2</span> 说说hello,world</h3>
  357. <p>我很不喜欢其中的&lt;*&gt;超文本标记语言</p>
  358. <p>所以我们可以发现其中的关键词是标记——markup,也就是说html是一个markup,head是一个markup,body也是一个markup。</p>
  359. <p>然而,我们真正工作的代码是在body里面,至于为什么是在这里面,这个问题就太复杂了。打个比方来说:</p>
  360. <ol>
  361. <li><p>我们所使用的汉语是人类用智慧创造的,我们所正在学的这门语言同样也是人类创造的。</p></li>
  362. <li><p>我们在自己的语言里遵循着<strong>桌子是桌子,凳子是凳子</strong>的原则,很少有人会问为什么。</p></li>
  363. </ol>
  364. <h3 id="想用中文"><span class="header-section-number">2.1.3</span> 想用中文?</h3>
  365. <p>所以我们也可以把计算机语言与现实世界里用于交流沟通的语言划上一个等号。而我们所要学习的语言,并不是我们最熟悉的汉语语言,所以我们便觉得这些很复杂,但是如果我们试着用汉语替换掉上面的代码的话</p>
  366. <pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>语言&gt;
  367. <span class="er">&lt;</span>头&gt;<span class="er">&lt;</span>结束头&gt;
  368. <span class="er">&lt;</span>身体&gt;你好,世界<span class="er">&lt;</span>结束身体&gt;
  369. <span class="er">&lt;</span>结束语言&gt;</code></pre>
  370. <p>这看上去很奇怪,只是因为是直译过去的原因,也许你会觉得这样会好理解一点,但是输入上可就一点儿也不方便,因为这键盘本身就不适合我们去输入汉字,同时也意味着可能你输入的会有问题。</p>
  371. <p>让我们把上面的代码代替掉原来的代码然后保存,打开浏览器会看到下面的结果</p>
  372. <pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>语言&gt; <span class="er">&lt;</span>头&gt;<span class="er">&lt;</span>结束头&gt; <span class="er">&lt;</span>身体&gt;你好,世界<span class="er">&lt;</span>结束身体&gt; <span class="er">&lt;</span>结束语言&gt;</code></pre>
  373. <p>更不幸的结果可能是</p>
  374. <pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>璇█&gt; <span class="er">&lt;</span>澶�&gt;<span class="er">&lt;</span>缁撴潫澶�&gt; <span class="er">&lt;</span>韬綋&gt;浣犲ソ锛屼笘鐣�<span class="er">&lt;</span>缁撴潫韬綋&gt; <span class="er">&lt;</span>缁撴潫璇█&gt;</code></pre>
  375. <p>这是一个编码问题,对中文支持不友好。</p>
  376. <p>我们把上面的代码改为和标记语言一样的结构</p>
  377. <pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>语言&gt;
  378. <span class="er">&lt;</span>头&gt;<span class="er">&lt;</span>/头&gt;
  379. <span class="er">&lt;</span>身体&gt;你好,世界<span class="er">&lt;</span>/身体&gt;
  380. <span class="er">&lt;</span>结束语言&gt;</code></pre>
  381. <p>于是我们看到的结果便是</p>
  382. <pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>语言&gt; <span class="er">&lt;</span>头&gt; <span class="er">&lt;</span>身体&gt;你好,世界</code></pre>
  383. <p>被chrome浏览器解析成什么样了?</p>
  384. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;</span><span class="er">&lt;</span>语言&gt;
  385. <span class="er">&lt;</span>头&gt;<span class="co">&lt;!--头--&gt;</span>
  386. <span class="er">&lt;</span>身体&gt;你好,世界<span class="co">&lt;!--身体--&gt;</span>
  387. <span class="co">&lt;!--语言--&gt;</span>
  388. <span class="kw">&lt;/body&gt;&lt;/html&gt;</span> </code></pre>
  389. <p>以</p>
  390. <p>结尾的是注释,写给人看的代码,不是给机器看的,所以机器不会去理解这些代码。</p>
  391. <p>但是当我们把代码改成</p>
  392. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;whatwewanttosay&gt;</span>你好世界<span class="kw">&lt;/whatwewanttosay&gt;</span></code></pre>
  393. <p>浏览器上面显示的内容就变成了</p>
  394. <pre class="sourceCode html"><code class="sourceCode html">你好世界</code></pre>
  395. <p>或许你会觉得很神奇,但是这一点儿也不神奇,虽然我们的中文语法也遵循着标记语言的标准,但是我们的浏览器不支持中文标记。</p>
  396. <p>结论:</p>
  397. <ol>
  398. <li>浏览器对中文支持不友好。</li>
  399. <li>浏览器对英文支持友好。</li>
  400. </ol>
  401. <p>刚开始的时候不要对中文编程有太多的想法,这是很不现实的:</p>
  402. <ol>
  403. <li>现有的系统都是基于英语语言环境构建的,对中文支持不是很友好。</li>
  404. <li>中文输入的速度在某种程度上来说没有英语快。</li>
  405. </ol>
  406. <p>我们离开话题已经很远了,但是这里说的都是针对于那些不满于英语的人来说的,只有当我们可以从头构建一个中文系统的时候才是可行的,而这些就要将cpu、软件、硬件都包含在内,甚至我们还需要考虑重新设计cpu的结构,在某种程度上来说会有些不现实。或许,需要一代又一代人的努力。忘记那些吧,师夷长之技以治夷。</p>
  407. <h2 id="其他html标记"><span class="header-section-number">2.2</span> 其他html标记</h2>
  408. <p>添加一个标题,</p>
  409. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  410. <span class="kw">&lt;head&gt;</span>
  411. <span class="kw">&lt;title&gt;</span>标题<span class="kw">&lt;/title&gt;</span>
  412. <span class="kw">&lt;/head&gt;</span>
  413. <span class="kw">&lt;body&gt;</span>hello,world<span class="kw">&lt;/body&gt;</span>
  414. <span class="kw">&lt;/html&gt;</span></code></pre>
  415. <p>我们便可以在浏览器的最上方看到“标题”二字,就像我们常用的淘宝网,也包含了上面的东西,只是还包括了更多的东西,所以你也可以看懂那些我们可以看到的淘宝的标题。</p>
  416. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  417. <span class="kw">&lt;head&gt;</span>
  418. <span class="kw">&lt;title&gt;</span>标题<span class="kw">&lt;/title&gt;</span>
  419. <span class="kw">&lt;/head&gt;</span>
  420. <span class="kw">&lt;body&gt;</span>
  421. hello,world
  422. <span class="kw">&lt;h1&gt;</span>大标题<span class="kw">&lt;/h1&gt;</span>
  423. <span class="kw">&lt;h2&gt;</span>次标题<span class="kw">&lt;/h2&gt;</span>
  424. <span class="kw">&lt;h3&gt;</span>...<span class="kw">&lt;/h3&gt;</span>
  425. <span class="kw">&lt;ul&gt;</span>
  426. <span class="kw">&lt;li&gt;</span>列表1<span class="kw">&lt;/li&gt;</span>
  427. <span class="kw">&lt;li&gt;</span>列表2<span class="kw">&lt;/li&gt;</span>
  428. <span class="kw">&lt;/ul&gt;</span>
  429. <span class="kw">&lt;/body&gt;</span>
  430. <span class="kw">&lt;/html&gt;</span></code></pre>
  431. <p>更多的东西可以在一些书籍上看到,这边所要说的只是一次简单的语言入门,其他的东西都和这些类似。</p>
  432. <h3 id="美妙之处"><span class="header-section-number">2.2.1</span> 美妙之处</h3>
  433. <p>我们简单地上手了一门不算是语言的语言,浏览器简化了这其中的大部分过程,虽然没有C和其他语言来得有专业感,但是我们试着去开始写代码了。我们可能在未来的某一篇中可能会看到类似的语言,诸如python,我们所要做的就是</p>
  434. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">python</span> file.py
  435. =<span class="kw">&gt;hello</span>,world</code></pre>
  436. <p>然后在终端上返回结果。只是因为在我看来学会html是有意义的,简单的上手,然后再慢慢地深入,如果一开始我们就去理解指针,开始去理解类。我们甚至还知道程序是怎么编译运行的时候,在这个过程中又发生了什么。虽然现在我们也没能理解这其中发生了什么,但是至少展示了</p>
  437. <ol>
  438. <li>中文编程语言在当前意义不大,不现实,效率不高兼容性差</li>
  439. <li>语言的语法是固定的。(ps:虽然我们也可以进行扩充,我们将会在后来支持上述的中文标记。)</li>
  440. <li>已经开始写代码,而不是还在配置开发环境。</li>
  441. <li>随身的工具才是最好的,最常用的code也才是实在的。</li>
  442. </ol>
  443. <h3 id="更多"><span class="header-section-number">2.2.2</span> 更多</h3>
  444. <p>我们还没有试着去解决“某商店里的糖一颗5块钱,小明买了3颗糖,小明一共花了多少钱”的问题。也就是说我们学会的是一个还不能解决实际问题的语言,于是我们还需要学点东西,比如javascript,css。我们可以将Javascript理解为解决问题的语言,html则是前端显示,css是配置文件,这样的话,我们会在那之后学会成为一个近乎专业的程序员。我们刚刚学习了一下怎么在前端显示那些代码的行为,于是我们还需要Javascript。</p>
  445. <h1 id="无处不在的javascript"><span class="header-section-number">3</span> 无处不在的Javascript</h1>
  446. <p>Javascript现在已经无处不在了,也许你正打开的某个网站,他便可能是node.js+json+javascript+mustache.js完成的,虽然你还没理解上面那些是什么,也正是因为你不理解才需要去学习更多的东西。但是你只要知道Javascript已经无处不在了,它可能就在你手机上的某个app里,就在你浏览的网页里,就运行在你IDE中的某个进程里。</p>
  447. <h2 id="javascript的helloworld"><span class="header-section-number">3.1</span> Javascript的Hello,world</h2>
  448. <p>这里我们还需要有一个helloworld.html,Javascript是专为网页交互而设计的脚本语言,所以我们一点点来开始这部分的旅途,先写一个符合标准的helloworld.html</p>
  449. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  450. <span class="kw">&lt;html&gt;</span>
  451. <span class="kw">&lt;head&gt;&lt;/head&gt;</span>
  452. <span class="kw">&lt;body&gt;&lt;/body&gt;</span>
  453. <span class="kw">&lt;/html&gt;</span></code></pre>
  454. <p>然后开始融入我们的javascript,向HTML中插入Javascript的方法,就需要用到html中的&lt;script&gt;标签,我们先用页面嵌入的方法来写helloworld。</p>
  455. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  456. <span class="kw">&lt;html&gt;</span>
  457. <span class="kw">&lt;head&gt;</span>
  458. <span class="kw">&lt;script&gt;</span>
  459. <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">&#39;hello,world&#39;</span>);
  460. &lt;<span class="ot">/script&gt;</span>
  461. <span class="ot"> &lt;/head</span>&gt;
  462. &lt;body&gt;&lt;<span class="ot">/body&gt;</span>
  463. <span class="ot">&lt;/html</span>&gt;</code></pre>
  464. <p>按照标准的写法,我们还需要声明这个脚本的类型</p>
  465. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  466. <span class="kw">&lt;html&gt;</span>
  467. <span class="kw">&lt;head&gt;</span>
  468. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="kw">&gt;</span>
  469. <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">&#39;hello,world&#39;</span>);
  470. &lt;<span class="ot">/script&gt;</span>
  471. <span class="ot"> &lt;/head</span>&gt;
  472. &lt;body&gt;&lt;<span class="ot">/body&gt;</span>
  473. <span class="ot">&lt;/html</span>&gt;</code></pre>
  474. <p>没有显示hello,world?试试下面的代码</p>
  475. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  476. <span class="kw">&lt;html&gt;</span>
  477. <span class="kw">&lt;head&gt;</span>
  478. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="kw">&gt;</span>
  479. <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">&#39;hello,world&#39;</span>);
  480. &lt;<span class="ot">/script&gt;</span>
  481. <span class="ot"> &lt;/head</span>&gt;
  482. &lt;body&gt;
  483. &lt;noscript&gt;
  484. disable Javascript
  485. &lt;<span class="ot">/noscript&gt;</span>
  486. <span class="ot"> &lt;/body</span>&gt;
  487. &lt;<span class="ot">/html&gt;</span></code></pre>
  488. <h2 id="更js一点"><span class="header-section-number">3.2</span> 更js一点</h2>
  489. <p>我们需要让我们的代码看上去更像是js,同时是以js结尾。就像C语言的源码是以C结尾的,我们也同样需要让我们的代码看上去更正式一点。于是我们需要在helloworld.html的同一文件夹下创建一个app.js文件,在里面写着</p>
  490. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="st">&#39;hello,world&#39;</span>);</code></pre>
  491. <p>同时我们的helloworld.html还需要告诉我们的浏览器js代码在哪里</p>
  492. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  493. <span class="kw">&lt;html&gt;</span>
  494. <span class="kw">&lt;head&gt;</span>
  495. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;app.js&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  496. <span class="kw">&lt;/head&gt;</span>
  497. <span class="kw">&lt;body&gt;</span>
  498. <span class="kw">&lt;noscript&gt;</span>
  499. disable Javascript
  500. <span class="kw">&lt;/noscript&gt;</span>
  501. <span class="kw">&lt;/body&gt;</span>
  502. <span class="kw">&lt;/html&gt;</span></code></pre>
  503. <h3 id="从数学出发"><span class="header-section-number">3.2.1</span> 从数学出发</h3>
  504. <p>让我们回到第一章讲述的小明的问题,<strong>从实际问题下手编程,更容易学会编程</strong>。小学时代的数学题最喜欢这样子了——某商店里的糖一个5块钱,小明买了3个糖,小明一共花了多少钱。在编程方面,也许我们还算是小学生。最直接的方法就是直接计算3x5=?</p>
  505. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="dv">3</span>*<span class="dv">5</span>);</code></pre>
  506. <p>document.write实际也我们可以理解为输出,也就是往页面里写入3*5的结果,在有双引号的情况下会输出字符串。我们便会在浏览器上看到15,这便是一个好的开始,也是一个糟糕的开始。</p>
  507. <h2 id="设计和编程"><span class="header-section-number">3.3</span> 设计和编程</h2>
  508. <p>对于实际问题,如果我们只是止于所要得到的结果,很多年之后,我们就成为了code monkey。对这个问题进行再一次设计,所谓的设计有些时候会把简单的问题复杂化,有些时候会使以后的扩展更加简单。这一天因为这家商店的糖价格太高了,于是店长将价格降为了4块钱。</p>
  509. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="dv">3</span>*<span class="dv">4</span>);</code></pre>
  510. <p>于是我们又得到了我们的结果,但是下次我们看到这些代码的时候没有分清楚哪个是糖的数量,哪个是价格,于是我们重新设计了程序</p>
  511. <pre class="sourceCode javascript"><code class="sourceCode javascript">tang=<span class="dv">4</span>;
  512. num=<span class="dv">3</span>;
  513. <span class="ot">document</span>.<span class="fu">write</span>(tang*num);</code></pre>
  514. <p>这才能叫得上是程序设计,或许你注意到了“;”这个符号的存在,我想说的是这是另外一个标准,我们不得不去遵守,也不得不去fuck。</p>
  515. <h3 id="函数"><span class="header-section-number">3.3.1</span> 函数</h3>
  516. <p>记得刚开始学三角函数的时候,我们会写</p>
  517. <pre><code>sin 30=0.5</code></pre>
  518. <p>而我们的函数也是类似于此,换句话说,因为很多搞计算机的先驱都学好了数学,都把数学世界的规律带到了计算机世界,所以我们的函数也是类似于此,让我们做一个简单的开始。</p>
  519. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">hello</span>(){
  520. <span class="kw">return</span> <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">&quot;hello,world&quot;</span>);
  521. }
  522. <span class="fu">hello</span>();</code></pre>
  523. <p>当我第一次看到函数的时候,有些小激动终于出现了。我们写了一个叫hello的函数,它返回了往页面中写入hello,world的方法,然后我们调用了hello这个函数,于是页面上有了hello,world。</p>
  524. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">sin</span>(degree){
  525. <span class="kw">return</span> <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">Math</span>.<span class="fu">sin</span>(degree));
  526. }
  527. <span class="fu">sin</span>(<span class="dv">30</span>);</code></pre>
  528. <p>在这里degree就称之为变量。 于是输出了-0.9880316240928602,而不是0.5,因为这里用的是弧度制,而不是角度制。</p>
  529. <pre><code>sin(30)</code></pre>
  530. <p>的输出结果有点类似于sin 30。写括号的目的在于,括号是为了方便解析,这个在不同的语言中可能是不一样的,比如在ruby中我们可以直接用类似于数学中的表达:</p>
  531. <pre class="sourceCode ruby"><code class="sourceCode ruby"><span class="fl">2.0</span>.<span class="dv">0</span>-p353 :<span class="dv">004</span> &gt; <span class="dt">Math</span>.sin <span class="dv">30</span>
  532. =&gt; -<span class="fl">0.9880316240928618</span>
  533. <span class="fl">2.0</span>.<span class="dv">0</span>-p353 :<span class="dv">005</span> &gt;</code></pre>
  534. <p>我们可以在函数中传入多个变量,于是我们再回到小明的问题,就会这样去编写代码。</p>
  535. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">calc</span>(tang,num){
  536. result=tang*num;
  537. <span class="ot">document</span>.<span class="fu">write</span>(result);
  538. }
  539. <span class="fu">calc</span>(<span class="dv">3</span>,<span class="dv">4</span>);</code></pre>
  540. <p>但是从某种程度上来说,我们的calc做了计算的事又做了输出的事,总的来说设计上有些不好。</p>
  541. <h3 id="重新设计"><span class="header-section-number">3.3.2</span> 重新设计</h3>
  542. <p>我们将输出的工作移到函数的外面,</p>
  543. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">calc</span>(tang,num){
  544. <span class="kw">return</span> tang*num;
  545. }
  546. <span class="ot">document</span>.<span class="fu">write</span>(<span class="fu">calc</span>(<span class="dv">3</span>,<span class="dv">4</span>));</code></pre>
  547. <p>接着我们用一种更有意思的方法来写这个问题的解决方案</p>
  548. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">calc</span>(tang,num){
  549. <span class="kw">return</span> tang*num;
  550. }
  551. <span class="kw">function</span> <span class="fu">printResult</span>(tang,num){
  552. <span class="ot">document</span>.<span class="fu">write</span>(<span class="fu">calc</span>(tang,num));
  553. }
  554. <span class="fu">printResult</span>(<span class="dv">3</span>, <span class="dv">4</span>)</code></pre>
  555. <p>看上去更专业了一点点,如果我们只需要计算的时候我们只需要调用calc,如果我们需要输出的时候我们就调用printResult的方法。</p>
  556. <h3 id="object和函数"><span class="header-section-number">3.3.3</span> object和函数</h3>
  557. <p>我们还没有说清楚之前我们遇到过的document.write以及Math.sin的语法为什么看上去很奇怪,所以让我们看看他们到底是什么,修改app.js为以下内容</p>
  558. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> document);
  559. <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> Math);</code></pre>
  560. <p>typeof document会返回document的数据类型,就会发现输出的结果是</p>
  561. <pre class="sourceCode javascript"><code class="sourceCode javascript">object object</code></pre>
  562. <p>所以我们需要去弄清楚什么是object。对象的定义是</p>
  563. <blockquote>
  564. 无序属性的集合,其属性可以包含基本值、对象或者函数。
  565. </blockquote>
  566. <p>创建一个object,然后观察这便是我们接下来要做的</p>
  567. <pre class="sourceCode javascript"><code class="sourceCode javascript">store={};
  568. <span class="ot">store</span>.<span class="fu">tang</span>=<span class="dv">4</span>;
  569. <span class="ot">store</span>.<span class="fu">num</span>=<span class="dv">3</span>;
  570. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">store</span>.<span class="fu">tang</span>*<span class="ot">store</span>.<span class="fu">num</span>);</code></pre>
  571. <p>我们就有了和document.write一样的用法,这也是对象的美妙之处,只是这里的对象只是包含着基本值,因为</p>
  572. <pre><code>typeof story.tang=&quot;number&quot;</code></pre>
  573. <p>一个包含对象的对象应该是这样子的。</p>
  574. <pre class="sourceCode javascript"><code class="sourceCode javascript">store={};
  575. <span class="ot">store</span>.<span class="fu">tang</span>=<span class="dv">4</span>;
  576. <span class="ot">store</span>.<span class="fu">num</span>=<span class="dv">3</span>;
  577. <span class="ot">document</span>.<span class="fu">writeln</span>(<span class="ot">store</span>.<span class="fu">tang</span>*<span class="ot">store</span>.<span class="fu">num</span>);
  578. <span class="kw">var</span> wall=<span class="kw">new</span> <span class="fu">Object</span>();
  579. <span class="ot">wall</span>.<span class="fu">store</span>=store;
  580. <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> <span class="ot">wall</span>.<span class="fu">store</span>);</code></pre>
  581. <p>而我们用到的document.write和上面用到的document.writeln都是属于这个无序属性集合中的函数。</p>
  582. <p>下面代码说的就是这个无序属性集中中的函数。</p>
  583. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> IO=<span class="kw">new</span> <span class="fu">Object</span>();
  584. <span class="kw">function</span> <span class="fu">print</span>(result){
  585. <span class="ot">document</span>.<span class="fu">write</span>(result);
  586. };
  587. <span class="ot">IO</span>.<span class="fu">print</span>=print;
  588. <span class="ot">IO</span>.<span class="fu">print</span>(<span class="st">&quot;a obejct with function&quot;</span>);
  589. <span class="ot">IO</span>.<span class="fu">print</span>(<span class="kw">typeof</span> <span class="ot">IO</span>.<span class="fu">print</span>);</code></pre>
  590. <p>我们定义了一个叫IO的对象,声明对象可以用</p>
  591. <pre><code>var store={};</code></pre>
  592. <p>又或者是</p>
  593. <pre><code>var store=new Object{};</code></pre>
  594. <p>两者是等价的,但是用后者的可读性会更好一点,我们定义了一个叫print的函数,他的作用也就是document.write,IO中的print函数是等价于print()函数,这也就是对象和函数之间的一些区别,对象可以包含函数,对象是无序属性的集合,其属性可以包含基本值、对象或者函数。</p>
  595. <p>复杂一点的对象应该是下面这样的一种情况。</p>
  596. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person={<span class="dt">name</span>:<span class="st">&quot;phodal&quot;</span>,<span class="dt">weight</span>:<span class="dv">50</span>,<span class="dt">height</span>:<span class="dv">166</span>};
  597. <span class="kw">function</span> <span class="fu">dream</span>(){
  598. future;
  599. };
  600. <span class="ot">Person</span>.<span class="fu">future</span>=dream;
  601. <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> Person);
  602. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">Person</span>.<span class="fu">future</span>);</code></pre>
  603. <p>而这些会在我们未来的实际编程过程中用得更多。</p>
  604. <h3 id="面向对象"><span class="header-section-number">3.3.4</span> 面向对象</h3>
  605. <p>开始之前先让我们简化上面的代码,</p>
  606. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">Person</span>.<span class="fu">future</span>=<span class="kw">function</span> <span class="fu">dream</span>(){
  607. future;
  608. }</code></pre>
  609. <p>看上去比上面的简单多了,不过我们还可以简化为下面的代码。。。</p>
  610. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person=<span class="kw">function</span>(){
  611. <span class="kw">this</span>.<span class="fu">name</span>=<span class="st">&quot;phodal&quot;</span>;
  612. <span class="kw">this</span>.<span class="fu">weight</span>=<span class="dv">50</span>;
  613. <span class="kw">this</span>.<span class="fu">height</span>=<span class="dv">166</span>;
  614. <span class="kw">this</span>.<span class="fu">future</span>=<span class="kw">function</span> <span class="fu">dream</span>(){
  615. <span class="kw">return</span> <span class="st">&quot;future&quot;</span>;
  616. };
  617. };
  618. <span class="kw">var</span> person=<span class="kw">new</span> <span class="fu">Person</span>();
  619. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">person</span>.<span class="fu">name</span>+<span class="st">&quot;&lt;br&gt;&quot;</span>);
  620. <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> person+<span class="st">&quot;&lt;br&gt;&quot;</span>);
  621. <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> <span class="ot">person</span>.<span class="fu">future</span>+<span class="st">&quot;&lt;br&gt;&quot;</span>);
  622. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">person</span>.<span class="fu">future</span>()+<span class="st">&quot;&lt;br&gt;&quot;</span>);</code></pre>
  623. <p>只是在这个时候Person是一个函数,但是我们声明的person却变成了一个对象<strong>一个Javascript函数也是一个对象,并且,所有的对象从技术上讲也只不过是函数。</strong>这里的“&lt;br&gt;”是HTML中的元素,称之为DOM,在这里起的是换行的作用,我们会在稍后介绍它,这里我们先关心下this。this关键字表示函数的所有者或作用域,也就是这里的Person。</p>
  624. <p>上面的方法显得有点不可取,换句话说和一开始的</p>
  625. <pre><code>document.write(3*4);</code></pre>
  626. <p>一样,不具有灵活性,因此在我们完成功能之后,我们需要对其进行优化,这就是程序设计的真谛——解决完实际问题后,我们需要开始真正的设计,而不是解决问题时的编程。</p>
  627. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person=<span class="kw">function</span>(name,weight,height){
  628. <span class="kw">this</span>.<span class="fu">name</span>=name;
  629. <span class="kw">this</span>.<span class="fu">weight</span>=weight;
  630. <span class="kw">this</span>.<span class="fu">height</span>=height;
  631. <span class="kw">this</span>.<span class="fu">future</span>=<span class="kw">function</span>(){
  632. <span class="kw">return</span> <span class="st">&quot;future&quot;</span>;
  633. };
  634. };
  635. <span class="kw">var</span> phodal=<span class="kw">new</span> <span class="fu">Person</span>(<span class="st">&quot;phodal&quot;</span>,<span class="dv">50</span>,<span class="dv">166</span>);
  636. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">name</span>+<span class="st">&quot;&lt;br&gt;&quot;</span>);
  637. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">weight</span>+<span class="st">&quot;&lt;br&gt;&quot;</span>);
  638. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">height</span>+<span class="st">&quot;&lt;br&gt;&quot;</span>);
  639. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">future</span>()+<span class="st">&quot;&lt;br&gt;&quot;</span>);</code></pre>
  640. <p>于是,产生了这样一个可重用的Javascript对象,this关键字确立了属性的所有者。</p>
  641. <h2 id="其他"><span class="header-section-number">3.4</span> 其他</h2>
  642. <p>Javascript还有一个很强大的特性,也就是原型继承,不过这里我们先不考虑这些部分,用尽量少的代码及关键字来实际我们所要表达的核心功能,这才是这里的核心,其他的东西我们可以从其他书本上学到。</p>
  643. <p>所谓的继承,</p>
  644. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Chinese=<span class="kw">function</span>(){
  645. <span class="kw">this</span>.<span class="fu">country</span>=<span class="st">&quot;China&quot;</span>;
  646. }
  647. <span class="kw">var</span> Person=<span class="kw">function</span>(name,weight,height){
  648. <span class="kw">this</span>.<span class="fu">name</span>=name;
  649. <span class="kw">this</span>.<span class="fu">weight</span>=weight;
  650. <span class="kw">this</span>.<span class="fu">height</span>=height;
  651. <span class="kw">this</span>.<span class="fu">futrue</span>=<span class="kw">function</span>(){
  652. <span class="kw">return</span> <span class="st">&quot;future&quot;</span>;
  653. }
  654. }
  655. <span class="ot">Chinese</span>.<span class="fu">prototype</span>=<span class="kw">new</span> <span class="fu">Person</span>();
  656. <span class="kw">var</span> phodal=<span class="kw">new</span> <span class="fu">Chinese</span>(<span class="st">&quot;phodal&quot;</span>,<span class="dv">50</span>,<span class="dv">166</span>);
  657. <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">country</span>);</code></pre>
  658. <p>完整的Javascript应该由下列三个部分组成:</p>
  659. <ul>
  660. <li>核心(ECMAScript)——核心语言功能</li>
  661. <li>文档对象模型(DOM)——访问和操作网页内容的方法和接口</li>
  662. <li>浏览器对象模型(BOM)——与浏览器交互的方法和接口</li>
  663. </ul>
  664. <p>我们在上面讲的都是ECMAScript,也就是语法相关的,但是JS真正强大的,或者说我们最需要的可能就是对DOM的操作,这也就是为什么jQuery等库可以流行的原因之一,而核心语言功能才是真正在哪里都适用的,至于BOM,真正用到的机会很少,因为没有完善的统一的标准。</p>
  665. <p>一个简单的DOM示例,</p>
  666. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  667. <span class="kw">&lt;html&gt;</span>
  668. <span class="kw">&lt;head&gt;</span>
  669. <span class="kw">&lt;/head&gt;</span>
  670. <span class="kw">&lt;body&gt;</span>
  671. <span class="kw">&lt;noscript&gt;</span>
  672. disable Javascript
  673. <span class="kw">&lt;/noscript&gt;</span>
  674. <span class="kw">&lt;p</span><span class="ot"> id=</span><span class="st">&quot;para&quot;</span><span class="ot"> style=</span><span class="st">&quot;color:red&quot;</span><span class="kw">&gt;</span>Red<span class="kw">&lt;/p&gt;</span>
  675. <span class="kw">&lt;/body&gt;</span>
  676. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;app.js&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  677. <span class="kw">&lt;/html&gt;</span></code></pre>
  678. <p>我们需要修改一下helloworld.html添加</p>
  679. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;p</span><span class="ot"> id=</span><span class="st">&quot;para&quot;</span><span class="ot"> style=</span><span class="st">&quot;color:red&quot;</span><span class="kw">&gt;</span>Red<span class="kw">&lt;/p&gt;</span></code></pre>
  680. <p>同时还需要将script标签移到body下面,如果没有意外的话我们会看到页面上用红色的字体显示Red,修改app.js。</p>
  681. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para=<span class="ot">document</span>.<span class="fu">getElementById</span>(<span class="st">&quot;para&quot;</span>);
  682. <span class="ot">para</span>.<span class="ot">style</span>.<span class="fu">color</span>=<span class="st">&quot;blue&quot;</span>;</code></pre>
  683. <p>接着,字体就变成了蓝色,有了DOM我们就可以对页面进行操作,可以说我们看到的绝大部分的页面效果都是通过DOM操作实现的。</p>
  684. <h2 id="美妙之处-1"><span class="header-section-number">3.5</span> 美妙之处</h2>
  685. <p>这里说到的Javascript仅仅只是其中的一小小部分,忽略掉的东西很多,只关心的是如何去设计一个实用的app,作为一门编程语言,他还有其他强大的内制函数,要学好需要一本有价值的参考书。这里提到的只是其中的不到20%的东西,其他的80%或者更多会在你解决问题的时候出现。</p>
  686. <ul>
  687. <li>我们可以创建一个对象或者函数,它可以包含基本值、对象或者函数。</li>
  688. <li>我们可以用Javascript修改页面的属性,虽然只是简单的示例。</li>
  689. <li>我们还可以去解决实际的编程问题。</li>
  690. </ul>
  691. <h1 id="无处不在的css"><span class="header-section-number">4</span> 无处不在的CSS</h1>
  692. <p>或许你觉得CSS一点儿也不重要,而事实上,如果说HTML是建筑的框架,CSS就是房子的装修。那么Javascript呢,我听到的最有趣的说法是小三——还是先让我们回到代码上来吧。</p>
  693. <h2 id="css"><span class="header-section-number">4.1</span> CSS</h2>
  694. <p>下面就是我们之前说到的代码,css将Red三个字母变成了红色。</p>
  695. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  696. <span class="kw">&lt;html&gt;</span>
  697. <span class="kw">&lt;head&gt;</span>
  698. <span class="kw">&lt;/head&gt;</span>
  699. <span class="kw">&lt;body&gt;</span>
  700. <span class="kw">&lt;p</span><span class="ot"> id=</span><span class="st">&quot;para&quot;</span><span class="ot"> style=</span><span class="st">&quot;color:red&quot;</span><span class="kw">&gt;</span>Red<span class="kw">&lt;/p&gt;</span>
  701. <span class="kw">&lt;/body&gt;</span>
  702. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;app.js&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  703. <span class="kw">&lt;/html&gt;</span></code></pre>
  704. <p>只是,</p>
  705. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para=<span class="ot">document</span>.<span class="fu">getElementById</span>(<span class="st">&quot;para&quot;</span>);
  706. <span class="ot">para</span>.<span class="ot">style</span>.<span class="fu">color</span>=<span class="st">&quot;blue&quot;</span>;</code></pre>
  707. <p>将字体变成了蓝色,CSS+HTML让页面有序的工作着,但是Javascript却打乱了这些秩序,有着唯恐世界不乱的精彩,也难怪被冠以小三之名了——或许终于可以理解,为什么以前人们对于Javascript没有好感了——不过这里要讲的是正室,也就是CSS,这时还没有Javascript。</p>
  708. <div class="figure">
  709. <img src="./images/redfonts.png" alt="Red Fonts" />
  710. <p>Red Fonts</p>
  711. </div>
  712. <h2 id="关于css"><span class="header-section-number">4.2</span> 关于CSS</h2>
  713. <p>这不是一篇专业讲述CSS的书籍,所以我不会去说CSS是怎么来的,有些东西我们既然可以很容易从其他地方知道,也就不需要花太多时间去重复。诸如重构等这些的目的之一也在于去除重复的代码,不过有些重复是不可少的,也是有必要的,而通常这些东西可能是由其他地方复制过来的。</p>
  714. <p>到目前为止我们没有依赖于任何特殊的硬件或者是软件,对于我们来说我们最基本的需求就是一台电脑,或者可以是你的平板电脑,当然也可以是你的智能手机,因为他们都有个浏览器,而这些都是能用的,对于我们的CSS来说也不会有例外的。</p>
  715. <p>CSS(Cascading Style Sheets),到今天我也没有记得他的全称,CSS还有一个中文名字是层叠式样式表,事实上翻译成什么可能并不是我们关心的内容,我们需要关心的是他能做些什么。作为三剑客之一,它的主要目的在于可以让我们方便灵活地去控制Web页面的外观表现。我们可以用它做出像淘宝一样复杂的界面,也可以像我们的书本一样简单,不过如果要和我们书本一样简单的话,可能不需要用到CSS。HTML一开始就是依照报纸的格式而设计的,我们还可以继续用上面说到的编辑器,又或者是其他的。如果你喜欢DreamWeaver那也不错,不过一开始使用IDE可无助于我们写出良好的代码。</p>
  716. <p>忘说了,CSS也是有版本的,和windows,Linux内核等等一样,但是更新可能没有那么频繁,HTML也是有版本的,JS也是有版本的,复杂的东西不是当前考虑的内容。</p>
  717. <h2 id="代码结构"><span class="header-section-number">4.3</span> 代码结构</h2>
  718. <p>对于我们的上面的Red示例来说,如果没有一个好的结构,那么以后可能就是这样子。</p>
  719. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  720. <span class="kw">&lt;html&gt;</span>
  721. <span class="kw">&lt;head&gt;</span>
  722. <span class="kw">&lt;/head&gt;</span>
  723. <span class="kw">&lt;body&gt;</span>
  724. <span class="kw">&lt;p</span><span class="ot"> style=</span><span class="st">&quot;font-size: 22px;color:#f00;text-align: center;padding-left: 20px;&quot;</span><span class="kw">&gt;</span>如果没有一个好的结构<span class="kw">&lt;/p&gt;</span>
  725. <span class="kw">&lt;p</span><span class="ot"> style=</span><span class="st">&quot; font-size:44px;color:#3ed;text-indent: 2em;padding-left: 2em;&quot;</span><span class="kw">&gt;</span>那么以后可能就是这样子。。。。<span class="kw">&lt;/p&gt;</span>
  726. <span class="kw">&lt;/body&gt;</span>
  727. <span class="kw">&lt;/html&gt;</span></code></pre>
  728. <p>虽然我们看到的还是一样的:</p>
  729. <div class="figure">
  730. <img src="./images/nostyle.png" alt="No Style" />
  731. <p>No Style</p>
  732. </div>
  733. <p>于是我们就按各种书上的建议重新写了上面的代码</p>
  734. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  735. <span class="kw">&lt;html&gt;</span>
  736. <span class="kw">&lt;head&gt;</span>
  737. <span class="kw">&lt;title&gt;</span>CSS example<span class="kw">&lt;/title&gt;</span>
  738. <span class="kw">&lt;style</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="kw">&gt;</span>
  739. <span class="fl">.para</span><span class="kw">{</span>
  740. <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
  741. <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
  742. <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
  743. <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
  744. <span class="kw">}</span>
  745. <span class="fl">.para2</span><span class="kw">{</span>
  746. <span class="kw">font-size:</span><span class="dt">44px</span><span class="kw">;</span>
  747. <span class="kw">color:</span><span class="dt">#3ed</span><span class="kw">;</span>
  748. <span class="kw">text-indent:</span> <span class="dt">2em</span><span class="kw">;</span>
  749. <span class="kw">padding-left:</span> <span class="dt">2em</span><span class="kw">;</span>
  750. <span class="kw">}</span>
  751. <span class="kw">&lt;/style&gt;</span>
  752. <span class="kw">&lt;/head&gt;</span>
  753. <span class="kw">&lt;body&gt;</span>
  754. <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">&quot;para&quot;</span><span class="kw">&gt;</span>如果没有一个好的结构<span class="kw">&lt;/p&gt;</span>
  755. <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">&quot;para2&quot;</span><span class="kw">&gt;</span>那么以后可能就是这样子。。。。<span class="kw">&lt;/p&gt;</span>
  756. <span class="kw">&lt;/body&gt;</span>
  757. <span class="kw">&lt;/html&gt;</span></code></pre>
  758. <p>总算比上面好看也好理解多了,这只是临时的用法,当文件太大的时候,正式一点的写法应该如下所示:</p>
  759. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  760. <span class="kw">&lt;html&gt;</span>
  761. <span class="kw">&lt;head&gt;</span>
  762. <span class="kw">&lt;title&gt;</span>CSS example<span class="kw">&lt;/title&gt;</span>
  763. <span class="kw">&lt;style</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;style.css&quot;</span><span class="kw">&gt;&lt;/style&gt;</span>
  764. <span class="kw">&lt;/head&gt;</span>
  765. <span class="kw">&lt;body&gt;</span>
  766. <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">&quot;para&quot;</span><span class="kw">&gt;</span>如果没有一个好的结构<span class="kw">&lt;/p&gt;</span>
  767. <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">&quot;para2&quot;</span><span class="kw">&gt;</span>那么以后可能就是这样子。。。。<span class="kw">&lt;/p&gt;</span>
  768. <span class="kw">&lt;/body&gt;</span>
  769. <span class="kw">&lt;/html&gt;</span></code></pre>
  770. <p>我们需要</p>
  771. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  772. <span class="kw">&lt;html&gt;</span>
  773. <span class="kw">&lt;head&gt;</span>
  774. <span class="kw">&lt;title&gt;</span>CSS example<span class="kw">&lt;/title&gt;</span>
  775. <span class="kw">&lt;link</span><span class="ot"> href=</span><span class="st">&quot;./style.css&quot;</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span> <span class="kw">/&gt;</span>
  776. <span class="kw">&lt;/head&gt;</span>
  777. <span class="kw">&lt;body&gt;</span>
  778. <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">&quot;para&quot;</span><span class="kw">&gt;</span>如果没有一个好的结构<span class="kw">&lt;/p&gt;</span>
  779. <span class="kw">&lt;p</span><span class="ot"> class=</span><span class="st">&quot;para2&quot;</span><span class="kw">&gt;</span>那么以后可能就是这样子。。。。<span class="kw">&lt;/p&gt;</span>
  780. <span class="kw">&lt;/body&gt;</span>
  781. <span class="kw">&lt;/html&gt;</span></code></pre>
  782. <p>然后我们有一个像app.js一样的style.css放在同目录下,而他的内容便是</p>
  783. <pre class="sourceCode css"><code class="sourceCode css"><span class="fl">.para</span><span class="kw">{</span>
  784. <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
  785. <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
  786. <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
  787. <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
  788. <span class="kw">}</span>
  789. <span class="fl">.para2</span><span class="kw">{</span>
  790. <span class="kw">font-size:</span><span class="dt">44px</span><span class="kw">;</span>
  791. <span class="kw">color:</span><span class="dt">#3ed</span><span class="kw">;</span>
  792. <span class="kw">text-indent:</span> <span class="dt">2em</span><span class="kw">;</span>
  793. <span class="kw">padding-left:</span> <span class="dt">2em</span><span class="kw">;</span>
  794. <span class="kw">}</span></code></pre>
  795. <p>这代码和JS的代码有如此多的相似</p>
  796. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para={
  797. <span class="dt">font_size</span>:<span class="st">&#39;22px&#39;</span>,
  798. <span class="dt">color</span>:<span class="st">&#39;#f00&#39;</span>,
  799. <span class="dt">text_align</span>:<span class="st">&#39;center&#39;</span>,
  800. <span class="dt">padding_left</span>:<span class="st">&#39;20px&#39;</span>,
  801. }</code></pre>
  802. <p>而22px、20px以及#f00都是数值,因此:</p>
  803. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para={
  804. <span class="dt">font_size</span>:22px,
  805. <span class="dt">color</span>:#f00,
  806. <span class="dt">text_align</span>:center,
  807. <span class="dt">padding_left</span>:20px,
  808. } </code></pre>
  809. <p>目测差距已经尽可能的小了,至于这些话题会在以后讨论到,如果要让我们的编译器更正确的工作,那么我们就需要非常多这样的符号,除非你乐意去理解:</p>
  810. <pre class="lisp"><code>(dotimes (i 4) (print i))</code></pre>
  811. <p>总的来说我们减少了符号的使用,但是用lisp便带入了更多的括号,不过这是一种简洁的表达方式,也许我们可以在其他语言中看到。</p>
  812. <pre class="regex"><code>\d{2}/[A-Z][a-z][a-z]/\d{4}</code></pre>
  813. <p>上面的代码,是为了从一堆数据中找出“某日/某月/某年”。如果一开始不理解那是正则表达式,就会觉得那个很复杂。</p>
  814. <p>这门语言可能是为设计师而设计的,但是设计师大部分还是不懂编程的,不过相对来说这门语言还是比其他语言简单易懂一些。</p>
  815. <h2 id="样式与目标"><span class="header-section-number">4.4</span> 样式与目标</h2>
  816. <p>如下所示,就是我们的样式</p>
  817. <pre class="sourceCode css"><code class="sourceCode css"><span class="fl">.para</span><span class="kw">{</span>
  818. <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
  819. <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
  820. <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
  821. <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
  822. <span class="kw">}</span></code></pre>
  823. <p>我们的目标就是</p>
  824. <pre><code>如果没有一个好的结构</code></pre>
  825. <p>所以样式和目标在这里牵手了,问题是他们是如何在一起的呢?下面就是CSS与HTML沟通的重点所在了:</p>
  826. <h3 id="选择器"><span class="header-section-number">4.4.1</span> 选择器</h3>
  827. <p>我们用到的选择器叫做类选择器,也就是class,或者说应该称之为class选择器更合适。与类选择器最常一起出现的是ID选择器,不过这个适用于比较高级的场合,诸如用JS控制DOM的时候就需要用到ID选择器。而基本的选择器就是如下面的例子:</p>
  828. <pre><code>p.para{
  829. color:#f0f;
  830. }</code></pre>
  831. <p>将代码添加到style.css的最下面会发现“如果没有一个好的结构”变成了粉红色,当然我们还会有这样的写法</p>
  832. <pre><code>p&gt;.para{
  833. color:#f0f;
  834. }</code></pre>
  835. <p>为了产生上面的特殊的样式,虽然不好看,但是我们终于理解什么叫层叠样式了,下面的代码的重要度比上面高,也因此有更高的优先规则。</p>
  836. <p>而通常我们可以通过一个</p>
  837. <pre><code>p{
  838. text-align:left;
  839. }</code></pre>
  840. <p>这样的元素选择器来给予所有的p元素一个左对齐。</p>
  841. <p>还有复杂一点的复合型选择器,下面的是HTML文件</p>
  842. <pre><code>&lt;!DOCTYPE html&gt;
  843. &lt;html&gt;
  844. &lt;head&gt;
  845. &lt;title&gt;CSS example&lt;/title&gt;
  846. &lt;link href=&quot;./style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
  847. &lt;/head&gt;
  848. &lt;body&gt;
  849. &lt;p class=&quot;para&quot;&gt;如果没有一个好的结构&lt;/p&gt;
  850. &lt;div id=&quot;content&quot;&gt;
  851. &lt;p class=&quot;para2&quot;&gt;那么以后可能就是这样子。。。。&lt;/p&gt;
  852. &lt;/div&gt;
  853. &lt;/body&gt;
  854. &lt;/html&gt;</code></pre>
  855. <p>还有CSS文件</p>
  856. <pre><code>.para{
  857. font-size: 22px;
  858. color:#f00;
  859. text-align: center;
  860. padding-left: 20px;
  861. }
  862. .para2{
  863. font-size:44px;
  864. color:#3ed;
  865. text-indent: 2em;
  866. padding-left: 2em;
  867. }
  868. p.para{
  869. color:#f0f;
  870. }
  871. div#content p {
  872. font-size:22px;
  873. }</code></pre>
  874. <h2 id="更有趣的css"><span class="header-section-number">4.5</span> 更有趣的CSS</h2>
  875. <p>一个包含了para2以及para_bg的例子</p>
  876. <pre><code> &lt;div id=&quot;content&quot;&gt;
  877. &lt;p class=&quot;para2 para_bg&quot;&gt;那么以后可能就是这样子。。。。&lt;/p&gt;
  878. &lt;/div&gt;
  879. </code></pre>
  880. <p>我们只是添加了一个黑色的背景</p>
  881. <pre><code>.para_bg{
  882. background-color:#000;
  883. }</code></pre>
  884. <p>重新改变后的网页变得比原来有趣了很多,所谓的继承与合并就是上面的例子。</p>
  885. <p>我们还可以用CSS3做出更多有趣的效果,而这些并不在我们的讨论范围里面,因为我们讨论的是be a geek。</p>
  886. <p>或许我们写的代码都是那么的简单,从HTML到Javascript,还有现在的CSS,只是总有一些核心的东西,而不是去考虑那些基础语法,基础的东西我们可以在实践的过程中一一发现。但是我们可能发现不了,或者在平时的使用中考虑不到一些有趣的用法或者说特殊的用法,这时候可以通过观察一些精致设计的代码中学习到。复杂的东西可以变得很简单,简单的东西也可以变得很复杂。</p>
  887. <h1 id="无处不在的三剑客"><span class="header-section-number">5</span> 无处不在的三剑客</h1>
  888. <p>这时我们终于了解了我们的三剑客,他们也就这么可以结合到一起了,HTML+Javascript+CSS是这一切的基础。而我们用到的其他语言如PHP、Python、Ruby等等到最后都会变成上面的结果,当然还有Coffeescript之类的语言都是以此为基础,这才是我们需要的知识。</p>
  889. <h2 id="hellogeek"><span class="header-section-number">5.1</span> Hello,Geek</h2>
  890. <p>有了一些些基础之后,我们终于能试着去写一些程序了。也是时候去创建一个像样的东西,或许你在一些界面设计方面的书籍看过类似的东西,可能我写得也没有那些内容好,只是这些都是一些过程。过去我们都是一点点慢慢过来的,只是现在我们也是如此,技术上的一些东西,事实上大家都是知道的。就好比我们都觉得我们可以开个超市,但是如果让我们去开超市的话,我们并不一定能赚钱。</p>
  891. <p>学习编程的目的可能不在于我们能找到一份工作,那只是在编程之外的东西,虽然确实也是很确定的。但是除此之处,有些东西也是很重要的。</p>
  892. <p>过去总是不理解为什么会一些人会不厌其烦地去回答别人的问题,有时候可能会想是一种能力越大责任越大的感觉,但是有时候在写一些博客或者回答别人的问题的时候我们又重新思考了这些问题,又重新学习了这些技能。所以这里可能说的不是关于编程的东西而是一些编程以外的东西,关于学习或者学习以外的东西。</p>
  893. <h2 id="从源码学习"><span class="header-section-number">5.2</span> 从源码学习</h2>
  894. <p>过去总觉得学了一种语言的语法便算是学会了一种语言,直到有一天接触运用该语言的项目的时候,虽然也会写上几行代码,但是却不像这种语言的风格。于是这也是这一篇的意义所在了:</p>
  895. <h2 id="浏览器渲染过程"><span class="header-section-number">5.3</span> 浏览器渲染过程</h2>
  896. <p>基本的渲染引擎的过程如下图所示:</p>
  897. <div class="figure">
  898. <img src="./images/flow.png" alt="flow" />
  899. <p>flow</p>
  900. </div>
  901. <ul>
  902. <li>解析HTML去构建DOM树</li>
  903. <li>渲染树形结构</li>
  904. <li>生成渲染的树形图布局</li>
  905. <li>绘制树形图</li>
  906. </ul>
  907. <p>对于Webkit浏览器来说,他的过程如下所示:</p>
  908. <div class="figure">
  909. <img src="./images/webkitflow.png" alt="webkitflow" />
  910. <p>webkitflow</p>
  911. </div>
  912. <h3 id="html"><span class="header-section-number">5.3.1</span> HTML</h3>
  913. <p>写好HTML的一个要点在于读别人写的代码,这只是一方面,我们所说的HTML方面的内容可能不够多,原因有很多,很多东西都需要在实战中去解决。读万卷书和行万里路,分不清哪个有重要的意义,但是如果可以同时做好两个的话,成长会更快的。</p>
  914. <p>写好HTML应该会有下面的要点</p>
  915. <ul>
  916. <li>了解标准及遵守绝大多数标准</li>
  917. <li>注重可读性,从ID及CLASS的命名</li>
  918. <li>关注SEO与代码的联系</li>
  919. </ul>
  920. <p>或许在这方面我也算不上很了解,不过按笔者的经验来说,大致就是如此。</p>
  921. <p>多数情况下我们的HTML是类似于下面这样子的</p>
  922. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;col-md-3 right&quot;</span><span class="kw">&gt;</span>
  923. {% nevercache %}
  924. {% include &quot;includes/user_panel.html&quot; %}
  925. {% endnevercache %}
  926. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;panel panel-default&quot;</span><span class="kw">&gt;</span>
  927. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;panel-body&quot;</span><span class="kw">&gt;</span>
  928. {% block right_panel %}
  929. {% ifinstalled mezzanine.twitter %}
  930. {% include &quot;twitter/tweets.html&quot; %}
  931. {% endifinstalled %}
  932. {% endblock %}
  933. <span class="kw">&lt;/div&gt;</span>
  934. <span class="kw">&lt;/div&gt;</span>
  935. <span class="kw">&lt;/div&gt;</span></code></pre>
  936. <p>换句话说HTML只是基础,而不是日常用到的。我们的HTML是由template生成的,我们可以借助于mustache.js又或者是angluarjs之类的js库来生成最后的HTML,所以这里只是一个开始。</p>
  937. <p>还需要了解的一部分就是HTML的另外一个重要的部分,DOM树形结构</p>
  938. <h2 id="dom树形结构图"><span class="header-section-number">5.4</span> DOM树形结构图</h2>
  939. <blockquote>
  940. <p>DOM是文档对象化模型(Document Object Model)的简称。DOM Tree是指通过DOM将HTML页面进行解析,并生成的HTML tree树状结构和对应访问方法。</p>
  941. </blockquote>
  942. <div class="figure">
  943. <img src="./images/dom_tree.jpg" alt="DOM Tree" />
  944. <p>DOM Tree</p>
  945. </div>
  946. <h3 id="javascript"><span class="header-section-number">5.4.1</span> javascript</h3>
  947. <p>这里以未压缩的jQuery源码和zepto.js作一个小小的比较,zepto.js是兼容jQuery的,因此我们举几个有意思的函数作一简单的比较,关于源码可以在官网上下载到。</p>
  948. <p>在zepto.js下面判断一个值是否是函数的方面如下,</p>
  949. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">isFunction</span>(value) { <span class="kw">return</span> <span class="fu">type</span>(value) == <span class="st">&quot;function&quot;</span> }</code></pre>
  950. <p>而在jQuery下面则是这样的</p>
  951. <pre class="sourceCode javascript"><code class="sourceCode javascript">isFunction: <span class="kw">function</span>( obj ) {
  952. <span class="kw">return</span> <span class="ot">jQuery</span>.<span class="fu">type</span>(obj) === <span class="st">&quot;function&quot;</span>;
  953. }</code></pre>
  954. <p>而他们的用法是一样的,都是</p>
  955. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">$</span>.<span class="fu">isFunction</span>();</code></pre>
  956. <p>jQuery的作法是将诸如isFunction,isArray这些函数打包到jQuery.extend中,而zepto.js的也是这样的,只不过多了一行</p>
  957. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">$</span>.<span class="fu">isFunction</span> = isFunction</code></pre>
  958. <p>遗憾的是我也没去了解过为什么,之前我也没有看过这些库的代码,所以这个问题就要交给读者去解决了。jQuery里面提供了函数式编程接口,不过jQuery更多的是构建于CSS选择器之上,对于DOM的操作比javascript自身提供的功能强大得多。如果我们的目的在于更好的编程,那么可能需要诸如Underscore.js之类的库。或许说打包自己常用的函数功能为一个库,诸如jQuery</p>
  959. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">isFunction</span>(value) { <span class="kw">return</span> <span class="fu">type</span>(value) == <span class="st">&quot;function&quot;</span> }
  960. <span class="kw">function</span> <span class="fu">isWindow</span>(obj) { <span class="kw">return</span> obj != <span class="kw">null</span> &amp;&amp; obj == <span class="ot">obj</span>.<span class="fu">window</span> }
  961. <span class="kw">function</span> <span class="fu">isDocument</span>(obj) { <span class="kw">return</span> obj != <span class="kw">null</span> &amp;&amp; <span class="ot">obj</span>.<span class="fu">nodeType</span> == <span class="ot">obj</span>.<span class="fu">DOCUMENT_NODE</span> }
  962. <span class="kw">function</span> <span class="fu">isObject</span>(obj) { <span class="kw">return</span> <span class="fu">type</span>(obj) == <span class="st">&quot;object&quot;</span> }</code></pre>
  963. <p>我们需要去了解一些故事背后的原因,越来越害怕GUI的原因之一,在于不知道背后发生了什么,即使是开源的,我们也无法了解真正的背后发生什么了。对于不是这个工具、软件的用户来说,开源更多的意义可能在于我们可以添加新的功能,当然还有免费。如果没有所谓的危机感,以及认为自己一直在学习工具的话,可以试着去打包自己的函数,打包自己的库。</p>
  964. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> calc={
  965. <span class="dt">add</span>: <span class="kw">function</span>(a,b){
  966. <span class="kw">return</span> a+b;
  967. },
  968. <span class="dt">sub</span>: <span class="kw">function</span>(a,b){
  969. <span class="kw">return</span> a-b;
  970. },
  971. <span class="dt">dif</span>: <span class="kw">function</span>(a,b){
  972. <span class="kw">if</span>(a&gt;b){
  973. <span class="kw">return</span> a;
  974. }<span class="kw">else</span>{
  975. <span class="kw">return</span> b;
  976. }
  977. }
  978. }</code></pre>
  979. <p>然后用诸如jslint测试一下代码。</p>
  980. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">./jsl</span> -conf jsl.default.conf
  981. <span class="kw">JavaScript</span> Lint 0.3.0 (JavaScript-C 1.5 2004-09-24)
  982. <span class="kw">Developed</span> by Matthias Miller (http://www.JavaScriptLint.com)
  983. <span class="kw">app.js</span>
  984. <span class="kw">/Users/fdhuang/beageek/chapter4/src</span>/<span class="kw">app.js</span>(15)<span class="kw">:</span> lint warning: missing semicolon
  985. }
  986. <span class="kw">........</span>^
  987. <span class="kw">0</span> error(s), <span class="kw">1</span> warning(s)</code></pre>
  988. <p>于是我们需要在第15行添加一个分号。</p>
  989. <p>最好的方法还是阅读别人的代码,而所谓的别人指的是一些相对较大的网站的,有比较完善的开发流程,代码质量也不会太差。而所谓的复杂的代码都是一步步构建上去的,罗马不是一天建成的。</p>
  990. <p>有意思的是多数情况下,我们可能会用原型去开发我们的应用,而这也是我们需要去了解和掌握的地方,</p>
  991. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">Calc</span>(){
  992. }
  993. <span class="ot">Calc</span>.<span class="ot">prototype</span>.<span class="fu">add</span>=<span class="kw">function</span>(a,b){
  994. <span class="kw">return</span> a+b;
  995. };
  996. <span class="ot">Calc</span>.<span class="ot">prototype</span>.<span class="fu">sub</span>=<span class="kw">function</span>(a,b){
  997. <span class="kw">return</span> a-b;
  998. };</code></pre>
  999. <p>我们似乎在这里展示了更多的Javascript的用法,但是这不是一好的关于Javascript的介绍,有一天我们还要用诸如qunit之类的工具去为我们的function写测试,这时就是一个更好的开始。</p>
  1000. <p>如果我们乐意的话,我们也可以构建一个类似于jQuery的框架,以用来学习。</p>
  1001. <p>作为一门编程语言来说,我们学得很普通,在某种意义上来说算不上是一种入门。但是如果我们可以在其他的好书在看到的内容,就没有必要在这里进行复述,目的在于一种学习习惯的养成。</p>
  1002. <h3 id="css-1"><span class="header-section-number">5.4.2</span> CSS</h3>
  1003. <p>CSS有时候很有趣,但是有时候有很多我们没有意识到的用法,这里以Bootstrap为例,这是一个不错的CSS库。最令人兴奋的是没有闭源的CSS,没有闭源的JS,这也就是前端好学习的地方所在了,不过这是一个开源的CSS库,虽然是这样叫的,但是称之为CSS库显然不合适。</p>
  1004. <pre class="sourceCode css"><code class="sourceCode css">a,
  1005. a<span class="dv">:visited</span> <span class="kw">{</span>
  1006. <span class="kw">text-decoration:</span> <span class="dt">underline</span><span class="kw">;</span>
  1007. <span class="kw">}</span>
  1008. a<span class="ch">[href]</span><span class="dv">:after</span> <span class="kw">{</span>
  1009. <span class="kw">content:</span> <span class="st">&quot; (&quot;</span> <span class="dt">attr(</span>href<span class="dt">)</span> <span class="st">&quot;)&quot;</span><span class="kw">;</span>
  1010. <span class="kw">}</span>
  1011. abbr<span class="ch">[title]</span><span class="dv">:after</span> <span class="kw">{</span>
  1012. <span class="kw">content:</span> <span class="st">&quot; (&quot;</span> <span class="dt">attr(</span>title<span class="dt">)</span> <span class="st">&quot;)&quot;</span><span class="kw">;</span>
  1013. <span class="kw">}</span>
  1014. a<span class="ch">[href^=</span><span class="st">&quot;javascript:&quot;</span><span class="ch">]</span><span class="dv">:after</span>,
  1015. a<span class="ch">[href^=</span><span class="st">&quot;#&quot;</span><span class="ch">]</span><span class="dv">:after</span> <span class="kw">{</span>
  1016. <span class="kw">content:</span> <span class="st">&quot;&quot;</span><span class="kw">;</span>
  1017. <span class="kw">}</span></code></pre>
  1018. <p>这里有一些有趣的,值得一讲的CSS用法。</p>
  1019. <ul>
  1020. <li>伪类选择器,如a:visited这样需要其他条件来对元素应用样式,用于已访问的链接。</li>
  1021. <li>属性选择器,如a[href]这样当a元素存在href这样的属性的时候来寻找应用元素。</li>
  1022. </ul>
  1023. <p>其他的还需要去好好了解的就是<strong>CSS的盒模型</strong>,作为CSS的基石之一。</p>
  1024. <h2 id="css盒模型图"><span class="header-section-number">5.5</span> CSS盒模型图</h2>
  1025. <p>(ps:以下内容来自于Mozilla Developer NetWorks)</p>
  1026. <p>CSS下这些矩形盒子由标准盒模型描述。这个模型描述元素内容占用空间。盒子有四个边界:外边距边界margin edge, 边框边界border edge, 内边距边界padding edge 与 内容边界content edge。</p>
  1027. <div class="figure">
  1028. <img src="./images/box-model.gif" alt="CSS Box Model" />
  1029. <p>CSS Box Model</p>
  1030. </div>
  1031. <p>内容区域content area 是真正包含元素内容的区域。位于内容边界的内部,它的大小为内容宽度 或 content-box宽及内容高度或content-box高。</p>
  1032. <p>如果 box-sizing 为默认值, width, min-width, max-width, height, min-height 与 max-height 控制内容大小。</p>
  1033. <p>内边距区域padding area 用内容及可能的边框之间的空白区域扩展内容区域。它位于内边距边界内部,通常有背景——颜色或图片(不透明图片盖住背景颜色). 它的大小为 padding-box 宽与 padding-box 高。</p>
  1034. <p>内边距与内容边界之间的空间可以由 padding-top, padding-right, padding-bottom, padding-left 和简写属性 padding 控制。</p>
  1035. <p>边框区域border area 是包含边框的区域,扩展了内边距区域。它位于边框边界内部,大小为 border-box 宽和 border-box 高。由 border-width 及简写属性 border控制。</p>
  1036. <p>外边距区域margin area用空白区域扩展边框区域,以分开相邻的元素。它的大小为 margin-box 的高宽。</p>
  1037. <p>外边距区域大小由 margin-top, margin-right, margin-bottom, margin-left 及简写属性 margin 控制。</p>
  1038. <p>在 外边距合并 的情况下,由于盒之间共享外边距,外边距不容易弄清楚。</p>
  1039. <p>最后注意,对于行内非替换元素,其占用空间(行高)由 line-height 决定,即使有内边距与边框。</p>
  1040. <p>诸如</p>
  1041. <pre class="sourceCode css"><code class="sourceCode css">* <span class="kw">{</span>
  1042. <span class="kw">margin:</span> <span class="dt">0px</span><span class="kw">;</span>
  1043. <span class="kw">padding:</span> <span class="dt">0px</span><span class="kw">;</span>
  1044. <span class="kw">font-family:</span> Helvetica<span class="kw">;</span>
  1045. <span class="kw">}</span></code></pre>
  1046. <p>这样的通用器用来进行全局选择的工具和我们用于抵消某个body对于子选择器的影响一样值得注意得多。</p>
  1047. <h2 id="笔记"><span class="header-section-number">5.6</span> 笔记</h2>
  1048. <p>写博客似乎是一个不错的好习惯,作为一个不是很优秀的写手。对于来说,有时候发现原来能教会别人对于自己的能力来说算是一种肯定。有些时候教会别人才算是自己学会的表现,总会在项目上的时候需要自己去复述工作的一个过程,我们需要整理好我们的思路才能带给别人更多的收获。我们的笔记上总会留下自己的学习的一些过程,有些时候我们想要的只是一点点的鼓励,有时是诸如评论一类,有时可能是诸如访问量。更多的可能是我们可以重新整理自己的知识,好好复习一下,以便于好好记住,写出来是一个好的过程。</p>
  1049. <p>无处不在的三剑客就这样到了这里,写得似乎很多也很少,但是还是没有做出来一个东西,于是我们朝着这样一个方向前进。</p>
  1050. <h1 id="gnulinux-强大且free"><span class="header-section-number">6</span> GNU/Linux 强大且Free</h1>
  1051. <div class="figure">
  1052. <img src="./images/gnu_linux.png" alt="GNU/Linux" />
  1053. <p>GNU/Linux</p>
  1054. </div>
  1055. <h2 id="什么是linux"><span class="header-section-number">6.1</span> 什么是Linux</h2>
  1056. <p>Linux是一种自由和开放源码的类UNIX操作系统内核。目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机、平板电脑、路由器和影音游戏控制台,到桌上型电脑,大型电脑和超级电脑。 Linux是一个领先的操作系统内核,世界上运算最快的10台超级电脑运行的都是基于Linux内核的操作系统。</p>
  1057. <p>Linux操作系统也是自由软件和开放源代码发展中最著名的例子。只要遵循GNU通用公共许可证,任何人和机构都可以自由地使用Linux的所有底层源代码,也可以自由地修改和再发布。<strong>严格来讲,Linux这个词本身只表示Linux内核,但在实际上人们已经习惯了用Linux来形容整个基于Linux内核,并且使用GNU工程各种工具和数据库的操作系统(也被称为GNU/Linux)。</strong>通常情况下,Linux被打包成供桌上型电脑和服务器使用的Linux发行版本。一些流行的主流Linux发行版本,包括Debian(及其衍生版本Ubuntu),Fedora和openSUSE等。Linux得名于电脑业余爱好者Linus Torvalds。</p>
  1058. <p>而不是如百度百科所讲的<del>Linux操作系统是UNIX操作系统的一种克隆系统。它诞生于1991年的 Linux桌面[1]10 月5日(这是第一次正式向外公布的时间)。以后借助于Internet网络,并通过全世界各地计算机爱好者的共同努力,已成为今天世界上使用最多的一种UNIX类操作系统,并且使用人数还在迅猛增长。</del></p>
  1059. <p>Linux只是个内核,而不是操作系统,所以在这我们再理解一下操作系统是由什么组成的。</p>
  1060. <h2 id="操作系统"><span class="header-section-number">6.2</span> 操作系统</h2>
  1061. <p>操作系统(英语:Operating System,简称OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石。操作系统需要处理如管理与配置内存、决定系统资源供需的优先次序、控制输入与输出设备、操作网络与管理文件系统等基本事务。操作系统也提供一个让用户与系统交互的操作界面。 操作系统的型态非常多样,不同机器安装的操作系统可从简单到复杂,可从手机的嵌入式系统到超级计算机的大型操作系统。许多操作系统制造者对它涵盖范畴的定义也不尽一致,例如有些操作系统集成了图形用户界面(GUI),而有些仅使用命令行界面(CLI),而将GUI视为一种非必要的应用程序。</p>
  1062. <p>操作系统位于底层硬件与用户之间,是两者沟通的桥梁。用户可以通过操作系统的用户界面,输入命令。操作系统则对命令进行解释,驱动硬件设备,实现用户要求。以现代标准而言,一个标准PC的操作系统应该提供以下的功能:</p>
  1063. <ul>
  1064. <li>进程管理(Processing management)</li>
  1065. <li>内存管理(Memory management)</li>
  1066. <li>文件系统(File system)</li>
  1067. <li>网络通信(Networking)</li>
  1068. <li>安全机制(Security)</li>
  1069. <li>用户界面(User interface)</li>
  1070. <li>驱动程序(Device drivers)</li>
  1071. </ul>
  1072. <p>而让我们来看一下两者之间的不同之处,这是一张linux的架构图我们可以发现内核只是位于底层。</p>
  1073. <h3 id="linux架构图"><span class="header-section-number">6.2.1</span> Linux架构图</h3>
  1074. <div class="figure">
  1075. <img src="./images/linux_kernel.jpg" alt="Linux Kernel" />
  1076. <p>Linux Kernel</p>
  1077. </div>
  1078. <h4 id="用户模式"><span class="header-section-number">6.2.1.1</span> 用户模式</h4>
  1079. <p>应用程序(sh、<a href="http://zh.wikipedia.org/wiki/Vi" title="Vi">vi</a>、<a href="http://zh.wikipedia.org/wiki/OpenOffice.org" title="OpenOffice.org">OpenOffice.org</a>等)</p>
  1080. <p>复杂<a href="http://zh.wikipedia.org/wiki/%E5%BA%93" title="库">库</a>(<a href="http://zh.wikipedia.org/wiki/KDE" title="KDE">KDE</a>、glib等) 简单库(opendbm、sin等)</p>
  1081. <p>C库(open、fopen、socket、exec、calloc等)</p>
  1082. <h4 id="内核模式"><span class="header-section-number">6.2.1.2</span> 内核模式</h4>
  1083. <ul>
  1084. <li>系统中断、调用、错误等软硬件消息</li>
  1085. <li>内核(驱动程序、进程、网络、内存管理等)</li>
  1086. <li>硬件(处理器、内存、各种设备)</li>
  1087. </ul>
  1088. <p>我们可以发现,由linux内核+shell可以构成一个操作系统,而linux本身只是个内核,也就是图中的内核模式,负责控制系统的这些部分。也就是我们可以发现,Linux内核构成了一个操作系统除用户界面以外的部分,而shell就是这最后的用户界面。</p>
  1089. <p>而linux内核以外的部分就是由GNU计划构成的。</p>
  1090. <h3 id="shell"><span class="header-section-number">6.2.2</span> Shell</h3>
  1091. <div class="figure">
  1092. <img src="./images/shell.png" alt="Shell" />
  1093. <p>Shell</p>
  1094. </div>
  1095. <p>Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。</p>
  1096. <p>实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果</p>
  1097. <p>bash 是一个为GNU计划编写的Unix shell。它的名字是一系列缩写:Bourne-Again SHell — 这是关于Bourne shell(sh)的一个双关语(Bourne again / born again)。Bourne shell是一个早期的重要shell,由史蒂夫·伯恩在1978年前后编写,并同Version 7 Unix一起发布。bash则在1987年由布莱恩·福克斯创造。在1990年,Chet Ramey成为了主要的维护者。</p>
  1098. <p>shell将会是我们在GNU/linux中经常用到的经常有到的工具之一,用来操作计算机用的。在迁移到linux之前我们可以试用cygwin来进行模拟:</p>
  1099. <p>Cygwin是许多自由软件的集合,最初由Cygnus Solutions开发,用于各种版本的Microsoft Windows上,运行<code>UNIX类</code>系统。</p>
  1100. <h3 id="gcc"><span class="header-section-number">6.2.3</span> GCC</h3>
  1101. <p>GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器。它是一套以GPL及LGPL许可证所发行的自由软件,也是GNU计划的关键部分,亦是自由的类Unix及苹果电脑Mac OS X 操作系统的标准编译器。GCC(特别是其中的C语言编译器)也常被认为是跨平台编译器的事实标准。</p>
  1102. <p>GCC原名为GNU C语言编译器(GNU C Compiler),因为它原本只能处理C语言。GCC很快地扩展,变得可处理C++。之后也变得可处理Fortran、Pascal、Objective-C、Java、Ada,以及Go与其他语言。</p>
  1103. <pre class="sourceCode c"><code class="sourceCode c"><span class="ot">#include &lt;stdio.h&gt;</span>
  1104. main()
  1105. {
  1106. printf(<span class="st">&quot;Hello world</span><span class="ch">\n</span><span class="st">&quot;</span>);
  1107. }</code></pre>
  1108. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">~/temp</span>/<span class="kw">free&gt;</span> gcc hello.c -o hello
  1109. <span class="kw">hello.c</span>:2:1: warning: type specifier missing, defaults to <span class="st">&#39;int&#39;</span> [-Wimplicit-int]
  1110. <span class="fu">main()</span>
  1111. ^<span class="kw">~~~</span>
  1112. <span class="kw">1</span> warning generated.
  1113. <span class="kw">~/temp</span>/<span class="kw">free&gt;</span> ./hello
  1114. <span class="kw">Hello</span> world</code></pre>
  1115. <p>同shell一样,对于GNU/linux系统而言,GCC的作用也是无可取代的。当然如果只是一般用途的话,GCC对于一般用户可能没用,但是在些GNU/Linux系统上,我们可能就需要自己编译源码成二进制文件,而没有软件包,因而其重要性是不言而喻的。自然的如果我们自己动手编译GNU/Linux操作系统的话,我们会理解其的重要意义。有兴趣的同学可以试一下:Linux From Scratch (LFS)。</p>
  1116. <h3 id="启动引导程序"><span class="header-section-number">6.2.4</span> 启动引导程序</h3>
  1117. <p>最后,当我们构成以上的那些之后,我们就需要一个引导程序,以便使系统启动,引导进内核。</p>
  1118. <p>启动程序(bootloader)于电脑或其他计算机应用上,是指引导操作系统启动的程序。启动程序启动方式与程序视应用机型种类。例如在普通PC上,引导程序通常分为两部分:第一阶段引导程序位于主引导记录,用于引导位于某个分区上的第二阶段引导程序,如NTLDR、GNU GRUB等。</p>
  1119. <p>BIOS 开机完成后,bootloader就接手初始化硬件设备、创建存储器空间的映射,以便为操作系统内核准备好</p>
  1120. <p>正确的软硬件环境。</p>
  1121. <p>简单的bootloader的虚拟汇编码,如其后的八个指令:</p>
  1122. <ul>
  1123. <li>0: 将P暂存器的值设为8</li>
  1124. <li>1: 检查纸带({paper tape)读取器,是否已经可以进行读取</li>
  1125. <li>2: 如果还不能进行读取, 跳至1</li>
  1126. <li>3: 从纸带读取器,读取一byte至累加器</li>
  1127. <li>4: 如为带子结尾,跳至8</li>
  1128. <li>5: 将暂存器的值,存储至P暂存器中的数值所指定的地址</li>
  1129. <li>6: 增加P暂存器的值</li>
  1130. <li>7: 跳至1</li>
  1131. </ul>
  1132. <p>但是随着计算机操作系统越来越复杂,位于MBR的空间已经放不下引导操作系统的代码,于是就有了第二阶段的引导程序,而MBR中代码的功能也从直接引导操作系统变成了引导第二阶段的引导程序。</p>
  1133. <p>通常在一个GNU/Linux系统中选用GNUGRUB做为引导程序,例如Ubuntu就是用GRUB2。</p>
  1134. <p>GNU GRUB(简称“GRUB”)是一个来自GNU项目的启动引导程序。GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统。GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数。</p>
  1135. <p>GNU GRUB的前身为Grand Unified Bootloader。它主要用于类Unix系统;同大多Linux发行版一样,GNU系统也采用GNU GRUB作为它的启动器。Solaris从10 1/06版开始在x86系统上也采用GNU GRUB作为启动器。</p>
  1136. <p>以上也就构成了一个简单的操作系统。</p>
  1137. <h2 id="从编译开始"><span class="header-section-number">6.3</span> 从编译开始</h2>
  1138. <p>我们以一次编译开始我们的Linux学习之旅。</p>
  1139. <h3 id="开始之前"><span class="header-section-number">6.3.1</span> 开始之前</h3>
  1140. <ul>
  1141. <li>如果你没有用过GNU/Linux,我想你需要在虚拟机上安装一个。</li>
  1142. <li>一个主流的GNU/Linux发行版,如Ubuntu,CentOS,Debian,Mint,OpenSUSE,Fedora等等。</li>
  1143. <li>学会如何打开shell(ps:bash,zsh,sh等等)。</li>
  1144. </ul>
  1145. <p>或者你也可以在Windows上安装Cygwin。</p>
  1146. <h3 id="编译nginx"><span class="header-section-number">6.3.2</span> 编译Nginx</h3>
  1147. <p>1.下载这个软件的源码包</p>
  1148. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">wget</span> http://nginx.org/download/nginx-1.7.4.tar.gz</code></pre>
  1149. <p>wget是一个用于下载的软件,当然你也可以用软件,只是用wget似乎会比图形界面快哦。</p>
  1150. <p>2.解压软件包</p>
  1151. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">tar</span> -vf nginx-1.7.4.tar.gz</code></pre>
  1152. <p>-vf的意思是Extract,也就是解压,而tar则是这个解压软件的名字。看上去似乎比WinRAR来得复制得多,但是你可以计时一下,从下载到解压完,和你用鼠标比哪个比较快。</p>
  1153. <p>3.到nginx目录下</p>
  1154. <p>这里需要分两部进行</p>
  1155. <p>1).列出所有文件</p>
  1156. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">ls</span> -al
  1157. <span class="kw">drwxr-xr-x</span> 15 fdhuang staff 510B Sep 2 13:44 nginx-1.7.4
  1158. <span class="kw">-rw-r--r--</span> 1 fdhuang staff 798K Aug 5 21:55 nginx-1.7.4.tar.gz</code></pre>
  1159. <p>2).到nginx-1.7.4目录</p>
  1160. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">cd</span> nginx-1.7.4 </code></pre>
  1161. <p>4.配置nginx</p>
  1162. <p>一次简单的配置如下</p>
  1163. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./configure</span></code></pre>
  1164. <p>当你熟练以后,你可能和我一样用下面的配置(<code>注意</code>:用下面的代码会出错。)</p>
  1165. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./configure</span> --user=www --group=www --add-module=../ngx_pagespeed-1.8.3.4-beta --add-module=../ngx_cache_purge --prefix=/usr/local/nginx --with-pcre --with-http_spdy_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-ipv6</code></pre>
  1166. <p>过程中可能会提示你其中出了多少错误,而这时你便可以很愉快地去用搜索引擎搜索他们。</p>
  1167. <p>5.make</p>
  1168. <p>这里就会用到GCC等等。</p>
  1169. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">make</span></code></pre>
  1170. <p>6.运行</p>
  1171. <p>如果运行顺利的话,应该可以直接</p>
  1172. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./objs/nginx</span> </code></pre>
  1173. <h3 id="其他-1"><span class="header-section-number">6.3.3</span> 其他</h3>
  1174. <p>1.如果没有wget,make,gcc等命令的时候可以用类似于下面的方法安装,</p>
  1175. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> apt-get install gcc,make,wget</code></pre>
  1176. <p>2.正常情况下一个开源项目都会有一个README,会告诉你应该如何去做。</p>
  1177. <h2 id="包管理"><span class="header-section-number">6.4</span> 包管理</h2>
  1178. <p>GNU/Linux最方便的东西莫过于包管理了。</p>
  1179. <p>引自OpenSUSE官网的说明及图片<span id="fnref1"><a href="#fn1"><sup>1</sup></a></span></p>
  1180. <div class="figure">
  1181. <img src="http://designiot.phodal.com/images/pm.png" alt="package management" />
  1182. <p>package management</p>
  1183. </div>
  1184. <ol>
  1185. <li><p>Linux 发行版无非就是一堆软件包 (package) 形式的应用程序加上整体地管理这些应用程序的工具。通常这些 Linux 发行版,包括 openSUSE,都是由成千上万不同的软件包构成的。</p></li>
  1186. <li><p>软件包: 软件包不止是一个文件,内含构成软件的所有文件,包括程序本身、共享库、开发包以及使用说明等。</p></li>
  1187. <li><p>元数据 (metadata) 包含于软件包之中,包含软件正常运行所需要的一些信息。软件包安装之后,其元数据就存储于本地的软件包数据库之中,以用于软件包检索。</p></li>
  1188. <li><p>依赖关系 (dependencies) 是软件包管理的一个重要方面。实际上每个软件包都会涉及到其他的软件包,软件包里程序的运行需要有一个可执行的环境(要求有其他的程序、库等),软件包依赖关系正是用来描述这种关系的。</p></li>
  1189. </ol>
  1190. <p>Linux 下的软件包通常是以下三种格式:</p>
  1191. <ul>
  1192. <li>tgz - tar gzip 文件。这类文件是基本的压缩软件包,可以容纳软件包维护者认为有用的所有的东西。此格式除本身的压缩格式外,并没有有关软件包内容的标准。</li>
  1193. <li>deb - 此格式的软件包常用于 Debian 系统,是标准的 Debian 软件包格式。</li>
  1194. <li>rpm - 此格式由 Red Hat Linux 所创建,并经由 LSB 标准化,现已为众多 Linux 发行版所采用,是一个优秀的软件包格式。openSUSE 即是用此格式。更多信息可以参阅此处。</li>
  1195. </ul>
  1196. <blockquote>
  1197. <p>所以这就需要能自动解决依赖关系的软件包管理器。软件包管理系统就是一个工具集,为系统提供一个统一的安装、升级、删除软件的方式。</p>
  1198. </blockquote>
  1199. <h2 id="ubuntu-lnmp"><span class="header-section-number">6.5</span> Ubuntu LNMP</h2>
  1200. <p>在余下的章节中,我们需要去部署,需要去使用Ubuntu。如果在Windows下可以使用LAMP,但是在这里我们只说Ubuntu。开始之前你需要安装好Ubuntu,无论是在虚拟机,还是在真机安装,或者是Docker。</p>
  1201. <h3 id="update软件包列表"><span class="header-section-number">6.5.1</span> Update软件包列表</h3>
  1202. <p><code>apt-get</code>是debian,ubuntu发行版的包管理工具。<code>apt-get update</code>可以确保我们的软件包列表是最新的,下面是一个简单的更新过程。</p>
  1203. <p>打开Terminal或者Konsole等等之类的终端控制台。</p>
  1204. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get update
  1205. <span class="kw">Ign</span> http://archive.ubuntu.com trusty InRelease
  1206. <span class="kw">Ign</span> http://archive.ubuntu.com trusty-updates InRelease
  1207. <span class="kw">Ign</span> http://archive.ubuntu.com trusty-security InRelease
  1208. <span class="kw">Ign</span> http://archive.ubuntu.com trusty-proposed InRelease
  1209. <span class="kw">Get</span>:1 http://archive.ubuntu.com trusty Release.gpg [933 B]
  1210. <span class="kw">Get</span>:2 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
  1211. <span class="kw">Get</span>:3 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
  1212. <span class="kw">Get</span>:4 http://archive.ubuntu.com trusty-proposed Release.gpg [933 B]
  1213. <span class="kw">Get</span>:5 http://archive.ubuntu.com trusty Release [58.5 kB]
  1214. <span class="kw">Get</span>:6 http://archive.ubuntu.com trusty-updates Release [62.0 kB]
  1215. <span class="kw">Get</span>:7 http://archive.ubuntu.com trusty-security Release [62.0 kB]
  1216. <span class="kw">Get</span>:8 http://archive.ubuntu.com trusty-proposed Release [209 kB]
  1217. <span class="kw">Get</span>:9 http://archive.ubuntu.com trusty/main Sources [1335 kB]
  1218. <span class="kw">Get</span>:10 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
  1219. <span class="kw">Get</span>:11 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
  1220. <span class="kw">Get</span>:12 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
  1221. <span class="kw">Get</span>:13 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
  1222. <span class="kw">Get</span>:14 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
  1223. <span class="kw">64%</span> [14 Packages 664 kB/7589 kB 9%] 58.3 kB/s 1min 58s</code></pre>
  1224. <p>更新完应该会显示:</p>
  1225. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Fetched</span> 20.5 MB in 5min 22s (63.6 kB/s)
  1226. <span class="kw">Reading</span> package lists... Done</code></pre>
  1227. <h3 id="安装mysql"><span class="header-section-number">6.5.2</span> 安装MySQL</h3>
  1228. <p>安装命令</p>
  1229. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> apt-get install mysql-server php5-mysql</code></pre>
  1230. <p>过程:</p>
  1231. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get install mysql-server php5-mysql
  1232. <span class="kw">Reading</span> package lists... 0%
  1233. <span class="kw">Reading</span> package lists... Done
  1234. <span class="kw">Building</span> dependency tree
  1235. <span class="kw">Reading</span> state information... Done
  1236. <span class="kw">The</span> following extra packages will be installed:
  1237. <span class="kw">libaio1</span> libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  1238. <span class="kw">libterm-readkey-perl</span> libwrap0 lsof mysql-client-5.5 mysql-client-core-5.5
  1239. <span class="kw">mysql-common</span> mysql-server-5.5 mysql-server-core-5.5 php5-common php5-json
  1240. <span class="kw">psmisc</span> tcpd
  1241. <span class="kw">Suggested</span> packages:
  1242. <span class="kw">libclone-perl</span> libmldbm-perl libnet-daemon-perl libplrpc-perl
  1243. <span class="kw">libsql-statement-perl</span> libipc-sharedcache-perl tinyca mailx php5-user-cache
  1244. <span class="kw">The</span> following NEW packages will be installed:
  1245. <span class="kw">libaio1</span> libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  1246. <span class="kw">libterm-readkey-perl</span> libwrap0 lsof mysql-client-5.5 mysql-client-core-5.5
  1247. <span class="kw">mysql-common</span> mysql-server mysql-server-5.5 mysql-server-core-5.5 php5-common
  1248. <span class="kw">php5-json</span> php5-mysql psmisc tcpd
  1249. <span class="kw">0</span> upgraded, 19 newly installed, 0 to remove and 12 not upgraded.
  1250. <span class="kw">Need</span> to get 9982 kB of archives.
  1251. <span class="kw">After</span> this operation, 99.1 MB of additional disk space will be used.
  1252. <span class="kw">Get</span>:1 http://archive.ubuntu.com/ubuntu/ trusty/main libaio1 amd64 0.3.109-4 [6364 B]
  1253. <span class="kw">Get</span>:2 http://archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-common all 5.5.40-0ubuntu0.14.04.1 [14.1 kB]
  1254. <span class="kw">Get</span>:3 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libmysqlclient18 amd64 5.5.40-0ubuntu0.14.04.1 [598 kB]
  1255. <span class="kw">Get</span>:4 http://archive.ubuntu.com/ubuntu/ trusty/main libwrap0 amd64 7.6.q-25 [46.2 kB]
  1256. <span class="kw">Get</span>:5 http://archive.ubuntu.com/ubuntu/ trusty/main libdbi-perl amd64 1.630-1 [879 kB]
  1257. <span class="kw">Get</span>:6 http://archive.ubuntu.com/ubuntu/ trusty/main libdbd-mysql-perl amd64 4.025-1 [99.3 kB]
  1258. <span class="kw">Get</span>:7 http://archive.ubuntu.com/ubuntu/ trusty/main libterm-readkey-perl amd64 2.31-1 [27.4 kB]
  1259. <span class="kw">Get</span>:8 http://archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-client-core-5.5 amd64 5.5.40-0ubuntu0.14.04.1 [703 kB]
  1260. <span class="kw">Get</span>:9 http://archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-client-5.5 amd64 5.5.40-0ubuntu0.14.04.1 [1466 kB]
  1261. <span class="kw">Get</span>:10 http://archive.ubuntu.com/ubuntu/ trusty-updates/main mysql-server-core-5.5 amd64 5.5.40-0ubuntu0.14.04.1 [3215 kB]
  1262. <span class="kw">47%</span> [10 mysql-server-core-5.5 850 kB/3215 kB 26%] 79.9 kB/s 1min 6s</code></pre>
  1263. <p>在安装的过程中会要求你输入数据库密码。(默认为空)</p>
  1264. <h3 id="安装nginx"><span class="header-section-number">6.5.3</span> 安装Nginx</h3>
  1265. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">echo</span> <span class="st">&quot;deb http://ppa.launchpad.net/nginx/stable/ubuntu </span><span class="ot">$(</span><span class="kw">lsb_release</span> -sc<span class="ot">)</span><span class="st"> main&quot;</span> <span class="kw">|</span> <span class="kw">sudo</span> tee /etc/apt/sources.list.d/nginx-stable.list
  1266. <span class="kw">sudo</span> apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
  1267. <span class="kw">sudo</span> apt-get update
  1268. <span class="kw">sudo</span> apt-get install nginx</code></pre>
  1269. <p>启动Nginx Server</p>
  1270. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> service nginx start</code></pre>
  1271. <h3 id="安装php"><span class="header-section-number">6.5.4</span> 安装PHP</h3>
  1272. <p>sudo apt-get install php5-fpm</p>
  1273. <p>安装过程</p>
  1274. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get install php5-fpm
  1275. <span class="kw">Reading</span> package lists... Done
  1276. <span class="kw">Building</span> dependency tree
  1277. <span class="kw">Reading</span> state information... Done
  1278. <span class="kw">The</span> following extra packages will be installed:
  1279. <span class="kw">libsystemd-daemon0</span>
  1280. <span class="kw">Suggested</span> packages:
  1281. <span class="kw">php-pear</span>
  1282. <span class="kw">The</span> following NEW packages will be installed:
  1283. <span class="kw">libsystemd-daemon0</span> php5-fpm
  1284. <span class="kw">0</span> upgraded, 2 newly installed, 0 to remove and 12 not upgraded.
  1285. <span class="kw">Need</span> to get 2201 kB of archives.
  1286. <span class="kw">After</span> this operation, 9326 kB of additional disk space will be used.
  1287. <span class="kw">Do</span> you want to continue? [Y/n] y
  1288. <span class="kw">Get</span>:1 http://archive.ubuntu.com/ubuntu/ trusty-proposed/main libsystemd-daemon0 amd64 204-5ubuntu20.8 [9608 B]
  1289. <span class="kw">Get</span>:2 http://archive.ubuntu.com/ubuntu/ trusty-updates/universe php5-fpm amd64 5.5.9+dfsg-1ubuntu4.5 [2191 kB]
  1290. <span class="kw">Fetched</span> 2201 kB in 1min 5s (33.6 kB/s)
  1291. <span class="kw">Selecting</span> previously unselected package libsystemd-daemon0:amd64.
  1292. <span class="kw">(Reading</span> database ... 13105 files and directories currently installed.<span class="kw">)</span>
  1293. <span class="kw">Preparing</span> to unpack .../libsystemd-daemon0_204-5ubuntu20.8_amd64.deb ...
  1294. <span class="kw">Unpacking</span> libsystemd-daemon0:amd64 (204-5ubuntu20.8) <span class="kw">...</span>
  1295. <span class="kw">Selecting</span> previously unselected package php5-fpm.
  1296. <span class="kw">Preparing</span> to unpack .../php5-fpm_5.5.9+dfsg-1ubuntu4.5_amd64.deb ...
  1297. <span class="kw">Unpacking</span> php5-fpm (5.5.9+dfsg-1ubuntu4.5) <span class="kw">...</span>
  1298. <span class="kw">Processing</span> triggers for ureadahead (0.100.0-16) <span class="kw">...</span>
  1299. <span class="kw">Setting</span> up libsystemd-daemon0:amd64 (204-5ubuntu20.8) <span class="kw">...</span>
  1300. <span class="kw">Setting</span> up php5-fpm (5.5.9+dfsg-1ubuntu4.5) <span class="kw">...</span>
  1301. <span class="kw">Creating</span> config file /etc/php5/fpm/php.ini with new version
  1302. <span class="kw">php5_invoke</span>: Enable module pdo for fpm SAPI
  1303. <span class="kw">php5_invoke</span>: Enable module pdo_mysql for fpm SAPI
  1304. <span class="kw">php5_invoke</span>: Enable module opcache for fpm SAPI
  1305. <span class="kw">php5_invoke</span>: Enable module json for fpm SAPI
  1306. <span class="kw">php5_invoke</span>: Enable module mysql for fpm SAPI
  1307. <span class="kw">php5_invoke</span>: Enable module mysqli for fpm SAPI
  1308. <span class="kw">invoke-rc.d</span>: policy-rc.d denied execution of start.
  1309. <span class="kw">Processing</span> triggers for libc-bin (2.19-0ubuntu6.3) <span class="kw">...</span>
  1310. <span class="kw">Processing</span> triggers for ureadahead (0.100.0-16) <span class="kw">...</span></code></pre>
  1311. <h1 id="arduino-极客的玩具"><span class="header-section-number">7</span> Arduino 极客的玩具</h1>
  1312. <h2 id="极客的玩具"><span class="header-section-number">7.1</span> 极客的玩具</h2>
  1313. <p>Arduino,是一个开放源代码的单芯片微电脑,它使用了Atmel AVR单片机,采用了基于开放源代码的软硬件平台,构建于开放源代码 simple I/O 接口板,并且具有使用类似Java,C 语言的Processing/Wiring开发环境。</p>
  1314. <p>Arduino开发板封装了常用的库到开发环境中,可以让用户在开发产品时,将主要注意力放置于所需要实现的功能上,而不是开发的过程中。在为Arduino写串口程序时,我们只需要用Serial.begin(9600)以9600的速率初始化串口,而在往串口发送数据时,可以用Serial.write(‘1’)的方式向串口发送字串’1’。</p>
  1315. <p>Arduino的出现很大程度上降低了电子制作的难度,初学者甚至不懂编程也可以上手Arduino,这也是它的魅力所在。</p>
  1316. <h2 id="硬件熟悉"><span class="header-section-number">7.2</span> 硬件熟悉</h2>
  1317. <p>为了满足各种需求,Arduino团队设计了很多款开发板,如UNO、Pro mini、Mega2560、Due、Leonardo、Yún、Pro、Fio、Nano等十几种 开发板和扩展板。最适合初学者的一款是Arduino UNO 。下图是Arduino UNO 的外观图:</p>
  1318. <div class="figure">
  1319. <img src="http://designiot.phodal.com/images/uno.png" alt="UNO" />
  1320. <p>UNO</p>
  1321. </div>
  1322. <p>注:后面的程序也是基于Arduino UNO开发板来讲解。</p>
  1323. <h2 id="开发环境"><span class="header-section-number">7.3</span> 开发环境</h2>
  1324. <div class="figure">
  1325. <img src="http://designiot.phodal.com/images/arduino.png" alt="Arduino" />
  1326. <p>Arduino</p>
  1327. </div>
  1328. <p>开发环境如上图,十分简洁,编写代码需要知道两个基本的函数:</p>
  1329. <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">void</span> setup(){
  1330. }
  1331. <span class="dt">void</span> loop(){
  1332. }</code></pre>
  1333. <p><code>setup()</code>函数用于初始化(如GPIO初始化,串口初始化,定时器初始化等)特点是只执行一次;<code>loop()</code>函数是一个死循环,可以看做C语言的<code>while(1)</code>函数。</p>
  1334. <h2 id="点亮一个led"><span class="header-section-number">7.4</span> 点亮一个LED</h2>
  1335. <p>对初学者来说,点亮led已成为入门必修课,使用Arduino控制led十分简单,并且很容易理解。 使用到的函数:</p>
  1336. <ul>
  1337. <li>pinMode(pin,mode)</li>
  1338. <li>digitalWrite(pin,value)</li>
  1339. </ul>
  1340. <p>上一段代码分析:</p>
  1341. <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">int</span> led=<span class="dv">13</span>;
  1342. <span class="dt">void</span> setup()
  1343. {
  1344. pinMode(led,OUTPUT);
  1345. }
  1346. <span class="dt">void</span> loop()
  1347. {
  1348. digitalWrite(led,HIGH);
  1349. delay(<span class="dv">1000</span>);
  1350. digitalWrite(led,LOW);
  1351. delay(<span class="dv">1000</span>);
  1352. }</code></pre>
  1353. <p>该程序实现Arduino单片机13号引脚以1S时间电平翻转,如果外接一个led,就可以看到led以1S的间隔闪烁;函数<code>pinMode()</code>有两个参数pin、value,pin参数用来指定引脚号,本程序中设置为13号引脚,mode用于设置引脚模式,有三个值:</p>
  1354. <ul>
  1355. <li><code>INPUT</code></li>
  1356. <li><code>OUTPUT</code></li>
  1357. <li><code>INPUT_PULLUP</code></li>
  1358. </ul>
  1359. <p>表示让某一个IO引脚作输入,反之,</p>
  1360. <ul>
  1361. <li><code>OUTPUT</code>则使一个IO引脚做输出</li>
  1362. <li><code>INPUT_PULLUP</code>则配置一个IO引脚具有上拉输入功能(上拉电阻的目的是为了保证在无信号输入时输入端的电平为高电平),从英文意思也能很直观的看出来。</li>
  1363. </ul>
  1364. <p>理解了<code>pinMode()</code>函数,<code>digitalWrite()</code>就很容易理解啦,value的取值有两个<code>HIGH</code>、<code>LOW</code>,<code>HIGH</code>表示让某一个引脚输出高电平,反之,<code>LOW</code>则使某一个引脚输出低电平。 程序中还是用到<code>delay(ms)</code>函数,它表示延时多少毫秒,例如延时500 ms ,直接调用<code>delay(500);</code>就可以了。</p>
  1365. <p>如果你仔细查看我的描述,你会发现我没有讲13号引脚怎么来的,是这样的:Arduino团队为了简化对引脚描述,对每个引脚都进行了编号,以UNO开发板为例,可以发现开发板排座的附近有对应的白颜色的数字,那便是所有的引脚编号,A0~A5是6路ADC输入引脚,0-13表示13路基本IO,数字前面的<code>~</code>表示该引脚具有PWM功能。如果要使用某一引脚,只需要知道引脚编号就可编写相应代码进行操作。</p>
  1366. <p>例如<code>digitalWrite(2,LOW)</code>表示向2号引脚输出低电平。其他操作类似,是不是so easy <sup>-</sup> !</p>
  1367. <h2 id="串口通信"><span class="header-section-number">7.5</span> 串口通信</h2>
  1368. <p>使用到的基本函数:</p>
  1369. <ul>
  1370. <li>Serial.begin()</li>
  1371. <li>Serial.write()</li>
  1372. <li>Serial.read()</li>
  1373. <li>Serial.available()</li>
  1374. </ul>
  1375. <p>在此项目中需要使用串口,Arduino串口初始化使用<code>Serial.begin(9600);</code>,其传输波特率为9600,其他波特率也行,函数位于<code>setup()</code>中,之后可以使用<code>Serial.read()</code>、<code>Serial.write()</code>读入一个字符,输出一个字符,使用<code>Serial.print()</code>输出字符串.代码如下:</p>
  1376. <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">char</span> ch=<span class="st">&#39;1&#39;</span>;
  1377. <span class="dt">void</span> setup()
  1378. {
  1379. Serial.begin(<span class="dv">9600</span>);
  1380. }
  1381. <span class="dt">void</span> loop()
  1382. {
  1383. Serial.write(ch);
  1384. <span class="kw">while</span>(<span class="dv">1</span>)
  1385. {
  1386. <span class="kw">if</span>(Serial.available())
  1387. {
  1388. ch = Serial.read();
  1389. Serial.print(ch);
  1390. }
  1391. }
  1392. }</code></pre>
  1393. <p>以上程序实现字符的输出(Serial.write(),Serial.print())和读入(Serial.read())。如果需要了解更多,可以参考:<a href="www.arduino.cc">Arduino官网</a></p>
  1394. <h3 id="关于arduino-setup"><span class="header-section-number">7.5.1</span> 关于Arduino Setup()</h3>
  1395. <p>如果你对Arduino的Setup很疑惑的话,可以看看这里。下面Arduino源码目录中的main函数:</p>
  1396. <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="ot">#include &lt;Arduino.h&gt;</span>
  1397. <span class="dt">int</span> main(<span class="dt">void</span>)
  1398. {
  1399. init();
  1400. setup();
  1401. <span class="kw">for</span> (;;) {
  1402. loop();
  1403. <span class="kw">if</span> (serialEventRun) serialEventRun();
  1404. }
  1405. <span class="kw">return</span> <span class="dv">0</span>;
  1406. }</code></pre>
  1407. <div class="figure">
  1408. <img src="./images/hwcnt.png" alt="hwcnt" />
  1409. <p>hwcnt</p>
  1410. </div>
  1411. <h1 id="python-代码如散文"><span class="header-section-number">8</span> Python 代码如散文</h1>
  1412. <div class="figure">
  1413. <img src="./images/python.png" alt="python" />
  1414. <p>python</p>
  1415. </div>
  1416. <p>作为一门计算机语言来说,Python会有下面的特点。</p>
  1417. <ul>
  1418. <li>语言学习起来容易</li>
  1419. <li>解决生活中的实际问题</li>
  1420. <li>支持多学科</li>
  1421. </ul>
  1422. <p>我们可以和其他不是脚本语言的语言进行一个简单的对比,如C,你需要去编译去运行,有时候还需要解决跨平台问题,本来你是在你的Windows上运行得好好的,但是有一天你换了一个Mac电脑的时候,问题变得很棘手,你甚至不知道怎么去解决问题。我没有用过MFC,听说很方便,但是在其他平台下就没有一个好的解决方案。这里可能跑得有点远,但是不同的用户可能在不同的平台上,这也就是脚本语言的优势所在了。</p>
  1423. <h2 id="代码与散文"><span class="header-section-number">8.1</span> 代码与散文</h2>
  1424. <p>你可能听过,也可能了解过,不过在这里我们可能不会去讲述那些基础的语法的东西,我们想说的是代码格式的重要性,在html中你可以这样去写你的代码</p>
  1425. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;&lt;head&gt;&lt;title&gt;</span>This is a Title
  1426. <span class="kw">&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;div</span><span class="ot"> class=</span><span class="st">&quot;content&quot;</span><span class="kw">&gt;</span>
  1427. <span class="kw">&lt;p&gt;</span>flakjfaklfjalfa<span class="kw">&lt;/p&gt;&lt;/div&gt;</span>
  1428. <span class="kw">&lt;/body&gt;&lt;/html&gt;</span></code></pre>
  1429. <p>又或者是js的minify,它可能会使你的代码看起来像是这样的:</p>
  1430. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">NolTracker</span>(b,a){<span class="kw">this</span>.<span class="fu">pvar</span>=b;<span class="kw">this</span>.<span class="fu">mergeFeatures</span>(a)}</code></pre>
  1431. <p>可能的是如果是python的话,你可能会遇到下面的问题。。</p>
  1432. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">File</span> <span class="st">&quot;steps.py&quot;</span>, line 10
  1433. <span class="kw">try</span>:
  1434. ^
  1435. <span class="kw">IndentationError</span>: expected an indented block</code></pre>
  1436. <p>如果你对JSLint、Lint这类的工具有点印象的话,你也可以认为python集成了这类工具。整洁的代码至少应该看上去要有整洁的衣服,就好像是我们看到的一个人一样,而后我们才会有一个好的印象。更主要的一点是代码是写给人看的,而衣服更多的时候对于像我这样的人来说,他首先应该是要保暖的,其次对于一个懒的人来说。。。</p>
  1437. <blockquote>
  1438. 程序应该是具有可读性的短文,它将在计算机上执行,从而解决某些问题
  1439. </blockquote>
  1440. <p>我们需要去读懂别人的代码,别人也需要去读懂我们的代码。计算机可以无条件地执行你那未经编排过的程序,但是人就不是如此了。</p>
  1441. <pre class="sourceCode javascript"><code class="sourceCode javascript"> <span class="kw">var</span> calc={<span class="dt">add</span>: <span class="kw">function</span>(a,b){<span class="kw">return</span> a+b;},<span class="dt">sub</span>: <span class="kw">function</span>(a,b){<span class="kw">return</span> a-b;},<span class="dt">dif</span>: <span class="kw">function</span>(a,b){<span class="kw">if</span>(a&gt;b){<span class="kw">return</span> a;}<span class="kw">else</span>{<span class="kw">return</span> b;}}}</code></pre>
  1442. <p>上面的代码相对于下面的代码可读性没有那么多,但是计算机可以无条件地执行上面的代码。上面的代码对于网络传输来说是好的,但是对于人来说并不是如此,我们需要一些工具来辅助我们去读懂上面的代码。如果代码上写得没有一点可读性,诸如函数命名没有多少实际意义,如果我们把前面的函数就成这样:</p>
  1443. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> c={
  1444. <span class="dt">a</span>: <span class="kw">function</span>(a,b){
  1445. <span class="kw">return</span> a+b;
  1446. },
  1447. <span class="dt">s</span>: <span class="kw">function</span>(a,b){
  1448. <span class="kw">return</span> a-b;
  1449. },
  1450. <span class="dt">d</span>: <span class="kw">function</span>(a,b){
  1451. <span class="kw">if</span>(a&gt;b){
  1452. <span class="kw">return</span> a;
  1453. }<span class="kw">else</span>{
  1454. <span class="kw">return</span> b;
  1455. }
  1456. }
  1457. }</code></pre>
  1458. <p>那么只有在我们理解了这个函数是干什么之后才能理解函数是干什么,而不是光看函数名就可以了。</p>
  1459. <p>在Javascript解决一个函数的办法有很多,在其他一些语言如Ruby或者Perl中也是如此,解决问题的办法有很多,对于写代码的人来说是一个享受的过程,但是对于维护的人来说并非如此。而这个和Python的思想不是很一致的是,Python设计的理念是</p>
  1460. <blockquote>
  1461. 对于特定的问题,只要有一种最好的方法来解决就够了
  1462. </blockquote>
  1463. <p>可读性的代码在今天显得比以前重要的多,以前写程序的时候我们需要去考虑使用汇编或者其他工具来提高程序的效率。</p>
  1464. <pre><code> .global _start
  1465. .text
  1466. _start:
  1467. # write(1, message, 13)
  1468. mov $1, %rax # system call 1 is write
  1469. mov $1, %rdi # file handle 1 is stdout
  1470. mov $message, %rsi # address of string to output
  1471. mov $13, %rdx # number of bytes
  1472. syscall # invoke operating system to do the write
  1473. # exit(0)
  1474. mov $60, %rax # system call 60 is exit
  1475. xor %rdi, %rdi # we want return code 0
  1476. syscall # invoke operating system to exit
  1477. message:
  1478. .ascii &quot;Hello, world\n&quot;</code></pre>
  1479. <p>所以上面的代码的可读性在今天新生一代的程序员来说可能没有那么容易理解。芯片运行的速度越来越快,在程序上我们也需要一个越来越快的解决方案,而所谓的越来越快的解决方案指的不是运行速度上,而是开发速度上。如果你没有办法在同样时间内开发出更好的程序,那么你就可能输给你的竞争对手。</p>
  1480. <h3 id="开始之前-1"><span class="header-section-number">8.1.1</span> 开始之前</h3>
  1481. <p>我们终于又从一种语言跳到了另外一种语言,我们可能习惯了一种模式,而不敢于去尝试新的东西,这些或许是我们的一些习惯又或者是因为害怕等等。</p>
  1482. <p>作为另外一个跨平台能力很强的语言,这里说的是与Javascript、HTML比较,或许你会觉得C算是最好的,但是我们这里讨论更多的是脚本语言,也就是直接可以运行的。在现在主流的大多数移动平台上,python也有良好的支持,如Android,IOS,只是这些算是类Unix系统内核,python还支持之前Nokia的Symbian。</p>
  1483. <p>开始之前我们需要确认我们的平台上已经有了python环境,也就是可以运行下面的Hello,World,你可以在网上很多地方看到,最简单的地方还是到官网,又或者是所用移动平台的store下载。</p>
  1484. <h3 id="python的helloworld"><span class="header-section-number">8.1.2</span> Python的Hello,World</h3>
  1485. <p>Python的Hello,World有两种形式,作为一种脚本语言来说,Javascript也是一种脚本语言,只是两者之间有太多的不同之处,每个人都会有不同的选择对于一种语言用来作为其的习惯。于是这就是我们的</p>
  1486. <pre><code>print &quot;Hello,World&quot;</code></pre>
  1487. <p>当我们把我们的脚本在shell环境下运行时</p>
  1488. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">&gt;&gt;&gt;</span> <span class="kw">print</span> <span class="st">&quot;Hello,world&quot;</span>
  1489. <span class="kw">File</span> <span class="st">&quot;&lt;stdin&gt;&quot;</span>, line 1
  1490. <span class="kw">print</span> <span class="st">&quot;Hello,world&quot;</span>
  1491. ^
  1492. <span class="kw">IndentationError</span>: unexpected indent
  1493. <span class="kw">&gt;&gt;&gt;</span> <span class="kw">print</span> <span class="st">&quot;Hello,world&quot;</span>
  1494. <span class="kw">Hello</span>,world
  1495. <span class="kw">&gt;&gt;&gt;</span></code></pre>
  1496. <p>如果你没有预料到缩进带来的问题的时候,这就是一个问题了。</p>
  1497. <p>和我们在Javascript或者是CSS里面一样,我们也可以用一个文件的方式来写入我们的代码,文件后缀名是py,所以创建一个helloworld.py,输入上面的代码,然后执行</p>
  1498. <pre><code>python helloworld.py</code></pre>
  1499. <p>一个理想的结果,或许你试过C语言的helloworld,如果了解过GCC的话应该是可以这样的:</p>
  1500. <pre><code>./a.out</code></pre>
  1501. <p>也就是执行编译完后的程序,需要注意的是helloworld.py没有编译,不过也会输出</p>
  1502. <pre><code>Hello,world</code></pre>
  1503. <h3 id="我们想要的helloworld"><span class="header-section-number">8.1.3</span> 我们想要的Hello,World</h3>
  1504. <p>我们想展示的是如何结合前面学习的内容做一个更有意思的Hello,World。</p>
  1505. <pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> cherrypy
  1506. <span class="kw">class</span> HelloWorld(<span class="dt">object</span>):
  1507. <span class="kw">def</span> index(<span class="ot">self</span>):
  1508. <span class="kw">return</span> <span class="st">&quot;Hello World!&quot;</span>
  1509. index.exposed = <span class="ot">True</span>
  1510. cherrypy.quickstart(HelloWorld())</code></pre>
  1511. <h2 id="算法"><span class="header-section-number">8.2</span> 算法</h2>
  1512. <p>我们需要去了解算法(algorithm),引经据典的话就是这样子:</p>
  1513. <blockquote>
  1514. a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer
  1515. </blockquote>
  1516. <p>也就是计算或其他解决问题的操作需要遵循的一个过程或者一套规则,书上还提到的说法是——解决问题的诀窍,让我想起了hack一词。我们总会去想某些东西是否有一个更快的计算方法,有时候在处理某些问题上也显示了一个好的算法的重要性。</p>
  1517. <h2 id="实用主义哲学"><span class="header-section-number">8.3</span> 实用主义哲学</h2>
  1518. <p>(来自于:HyryStudio)</p>
  1519. <p>大多数工程师、科学家使用科学计算软件的目的都是为了快速解决其工作中遇到的问题,而不是开发出一套完整的软件。这就是为什么MATLAB这样的商用科学计算软件十分流行的原因。而Python在这一点上实际上和MATLAB十分相似,我们也可以使用Python众多的扩展库快速写出一次性的数据处理、运算的脚本。然而由于Python语言的一些高级特性,以及众多的科学计算之外的扩展库,我们可以将积累下来的一次性脚本进行改造,为它们提供命令行、GUI、数据库、网络等众多接口,最终成为一套完整的工具包或者实用的计算软件。而且由于是开源的自由软件,我们可以在任何系统中安装Python环境,运行我们 的程序。</p>
  1520. <p>Python一直保持着很强的实用主义,它通常不会去试着重新开发一整套函数库,而是将现有的开源函数库包装成其扩展库。而Python则通过这些扩展库将众多的开源函数库连接在一起,是名符其实的胶水语言。例如由华盛顿大学的教授主导开发的 Sage ,就是一套以代替MATLAB、Mathematica、Maple等商用科学计算软件为目的的开源系统。它通过Python结合了众多的开源科学计算软件,并通过网页浏览器提供了一个与其交互的记事本文档界面。 Python的科学计算扩展库非常多,不同专业的技术人员都可以找到适合自己的扩展库。下面是我经常会用到的一个非常不完全的列表:</p>
  1521. <ul>
  1522. <li>NumPy + SciPy + matplotlib + IPython : 这几个应该是每位开发者都应具备的扩展库。NumPy提供了多维数组以及众多的处理函数,SciPy提供了各种数值运算功能,matplotlib能绘制 出精美的二维图表,IPython则提供了一个超强的命令行,最新版的IPython还添加于Sage类似的浏览器的记事本界面(notebook)。</li>
  1523. <li>SciKits : 其中包括许多独立的扩展库,作为SciPy的补充。其中 scikit-learn 是一套机器学习库,包含了比较完善的文档以及众多的实例程序。</li>
  1524. <li>Pandas : 以Python世界中 R 的替代品为目标的数据分析库。根据其官方网站的测试,Pandas在许多方面的性能都比R要高。</li>
  1525. <li>ETS : 这是一套Enthought公司开发的函数库,其中的 Mayavi 能很方便地对数据进行三维可视化。</li>
  1526. <li>OpenCV : 这是一套计算机视觉库,目前的最新版本已经提供了十分完备的Python接口,能够调用OpenCV中众多的图像处理、模式识别函数直接对NumPy数组进行处理。</li>
  1527. </ul>
  1528. <h2 id="包管理-1"><span class="header-section-number">8.4</span> 包管理</h2>
  1529. <p>关于Python的包管理</p>
  1530. <ul>
  1531. <li>Eggs 格式是 setuptools 引入的一种文件格式,它使用 .egg 扩展名,用于 Python 模块的安装。</li>
  1532. <li>pip 是目前 python 包管理的事实标准,2008年发布。它被用作 easy_install 的替代品,但是它仍有大量的功能建立在 setuptools 组件之上。</li>
  1533. </ul>
  1534. <h3 id="python-requests"><span class="header-section-number">8.4.1</span> python requests</h3>
  1535. <pre><code>Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。
  1536. Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。</code></pre>
  1537. <p>以安装requests为例:</p>
  1538. <p>命令:</p>
  1539. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> pip install requests</code></pre>
  1540. <p>结果:</p>
  1541. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Downloading/unpacking</span> requests
  1542. <span class="kw">Downloading</span> requests-2.4.3-py2.py3-none-any.whl (459kB)<span class="kw">:</span> 459kB downloaded
  1543. <span class="kw">Installing</span> collected packages: requests
  1544. <span class="kw">Successfully</span> installed requests
  1545. <span class="kw">Cleaning</span> up...</code></pre>
  1546. <p>用这个库我们可以做些什么?看看官网的示例:</p>
  1547. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">&gt;&gt;&gt;</span> <span class="kw">import</span> requests
  1548. <span class="kw">&gt;&gt;&gt;</span> <span class="kw">r</span> = requests.get(<span class="st">&#39;https://github.com/timeline.json&#39;</span>)
  1549. <span class="kw">&gt;&gt;&gt;</span> <span class="kw">r.json</span>()</code></pre>
  1550. <p>到现在你会发现我们没有说到任何的Python语法,这不是一本关于Python语法的书,如我们在开头所说的。下面是我们将会在后面用到的代码</p>
  1551. <pre class="sourceCode python"><code class="sourceCode python"><span class="co">#!/usr/bin/env python</span>
  1552. <span class="ch">import</span> requests
  1553. url = <span class="st">&quot;http://b.phodal.com/athome/1&quot;</span>
  1554. r = requests.get(url)
  1555. <span class="dt">print</span> r.text</code></pre>
  1556. <h1 id="raspberry-pi"><span class="header-section-number">9</span> Raspberry Pi</h1>
  1557. <div class="figure">
  1558. <img src="./images/rpi.jpg" alt="Raspberry Pi" />
  1559. <p>Raspberry Pi</p>
  1560. </div>
  1561. <h2 id="geek的盛宴"><span class="header-section-number">9.1</span> Geek的盛宴</h2>
  1562. <p>Raspberry Pi是一款针对电脑业余爱好者、教师、小学生以及小型企业等用户的迷你电脑,预装Linux系统,体积仅信用卡大小,搭载ARM架构处理器,运算性能和智能手机相仿。在接口方面,Raspberry Pi提供了可供键鼠使用的USB接口,此外还有千兆以太网接口、SD卡扩展接口以及1个HDMI高清视频输出接口,可与显示器或者TV相连。</p>
  1563. <p>Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。</p>
  1564. <p>Raspberry Pi相比于一般的ARM开发板来说,由于其本身搭载着Linux操作系统,可以用诸如Python、Ruby或Bash来执行脚本,而不是通过编译程序来运行,具有更高的开发效率。</p>
  1565. <h2 id="raspberry-pi-初始化"><span class="header-section-number">9.2</span> Raspberry Pi 初始化</h2>
  1566. <p>今天的Raspbian默认已经安装<code>openssh-server</code>,并默认开启了OpenSSH-Server。</p>
  1567. <p>接着我们就可以看到系统启动了,要我们输入用户名和密码</p>
  1568. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Raspbian</span> GNU/Linux 7 raspberrypi ttyAMA0
  1569. <span class="kw">raspberrypi</span> login: pi
  1570. <span class="kw">Password</span>:
  1571. <span class="kw">Last</span> login: Sat Apr 26 05:58:07 UTC 2014 on ttyAMA0
  1572. <span class="kw">Linux</span> raspberrypi 3.10.25+ <span class="co">#622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l</span>
  1573. <span class="kw">The</span> programs included with the Debian GNU/Linux system are free software<span class="kw">;</span>
  1574. <span class="kw">the</span> exact distribution terms for each program are described in the
  1575. <span class="kw">individual</span> files in /usr/share/doc/*/copyright.
  1576. <span class="kw">Debian</span> GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
  1577. <span class="kw">permitted</span> by applicable law.
  1578. <span class="kw">ls</span>
  1579. <span class="kw">NOTICE</span>: the software on this Raspberry Pi has not been fully configured. Please run <span class="st">&#39;sudo raspi-config&#39;</span></code></pre>
  1580. <p>然后</p>
  1581. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> raspi-config</code></pre>
  1582. <p>选择第一个,下面就可以继续了</p>
  1583. <p><code>Expand Filesystem Ensures that all of the SD card s</code></p>
  1584. <p>接着重启后,便可以扩展SD卡成功。</p>
  1585. <p>注: Raspbian与一般的Debian系统使用起来区别不是太大(ps:命令上),由于CPU是不同的架构,在编译上可能有所区别。通常PC上的软件需要重新编译才能在RPi上运行,所以如果可以用apt-get安装的话,就不要自己编译了。</p>
  1586. <h2 id="raspberry-pi-gpio"><span class="header-section-number">9.3</span> Raspberry Pi GPIO</h2>
  1587. <blockquote>
  1588. <p>General Purpose Input Output (通用输入/输出)简称为GPIO,或总线扩展器,利用工业标准I2C、SMBus或SPI接口简化了I/O口的扩展。当微控制器或芯片组没有足够的I/O端口,或当系统需要采用远端串行通信或控制时,GPIO产品能够提供额外的控制和监视功能。</p>
  1589. </blockquote>
  1590. <div class="figure">
  1591. <img src="./images/gpio.png" alt="GPIO" />
  1592. <p>GPIO</p>
  1593. </div>
  1594. <h1 id="server-一切皆为服务"><span class="header-section-number">10</span> Server 一切皆为服务</h1>
  1595. <h2 id="服务器"><span class="header-section-number">10.1</span> 服务器</h2>
  1596. <p>服务器(Server)指:</p>
  1597. <ul>
  1598. <li>一个管理资源并为用户提供服务的计算机软件,通常分为文件服务器(能使用户在其它计算机访问文件),数据库服务器和应用程序服务器。</li>
  1599. <li>运行以上软件的计算机,或称为网络主机(Host)。</li>
  1600. <li>一般来说,服务器通过网络对外提供服务。可以通过Intranet对内网提供服务,也可以通过Internet对外提供服务。</li>
  1601. </ul>
  1602. <h2 id="web服务器"><span class="header-section-number">10.2</span> Web服务器</h2>
  1603. <p>WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。 WWW 是 Internet的多媒体信息查询工具,是 Internet 上近年才发展起来的服务,也是发展最快和目前用的最广泛的服务。正是因为有了WWW工具,才使得近年来 Internet 迅速发展,且用户数量飞速增长。</p>
  1604. <h2 id="lnmp"><span class="header-section-number">10.3</span> LNMP</h2>
  1605. <div class="figure">
  1606. <img src="./images/lnmp.gif" alt="LNMP" />
  1607. <p>LNMP</p>
  1608. </div>
  1609. <p>Linux+Nginx+MySQL+PHP</p>
  1610. <blockquote>
  1611. <p>Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。</p>
  1612. </blockquote>
  1613. <blockquote>
  1614. <p>MySQL 是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。</p>
  1615. </blockquote>
  1616. <blockquote>
  1617. <p>PHP于1994年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf为了要维护个人网页而制作的一个简单的用Perl语言编写的程序。这些工具程序用来显示 Rasmus Lerdorf 的个人履历,以及统计网页流量。后来又用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。</p>
  1618. </blockquote>
  1619. <h1 id="web服务"><span class="header-section-number">11</span> Web服务</h1>
  1620. <p>Web服务是一种服务导向架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作。</p>
  1621. <p>根据W3C的定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作。网络服务通常是许多应用程序接口(API)所组成的,它们透过网络,例如国际互联网(Internet)的远程服务器端,执行客户所提交服务的请求。</p>
  1622. <p>尽管W3C的定义涵盖诸多相异且无法介分的系统,不过通常我们指有关于主从式架构(Client-server)之间根据SOAP协议进行传递XML格式消息。无论定义还是实现,WEB服务过程中会由服务器提供一个机器可读的描述(通常基于WSDL)以辨识服务器所提供的WEB服务。另外,虽然WSDL不是SOAP服务端点的必要条件,但目前基于Java的主流WEB服务开发框架往往需要WSDL实现客户端的源代码生成。一些工业标准化组织,比如WS-I,就在WEB服务定义中强制包含SOAP和WSDL。</p>
  1623. <p>WEB服务实际上是一组工具,并有多种不同的方法调用之。三种最普遍的手段是:</p>
  1624. <ul>
  1625. <li>远程过程调用(RPC)</li>
  1626. <li>面向服务架构(SOA)</li>
  1627. <li>表述性状态转移(REST)。</li>
  1628. </ul>
  1629. <h2 id="soap-vs-restful"><span class="header-section-number">11.1</span> SOAP VS RESTful</h2>
  1630. <p>简单对象访问协议是交换数据的一种协议规范,使用在计算机网络Web服务中,交换带结构信息。SOAP为了简化网页服务器从XML数据库中提取数据时,节省去格式化页面时间,以及不同应用程序之间按照HTTP通信协议,遵从XML格式执行资料互换,使其抽象于语言实现、平台和硬件。</p>
  1631. <h1 id="http-熟悉陌生"><span class="header-section-number">12</span> HTTP 熟悉&amp;陌生</h1>
  1632. <h2 id="你所没有深入的http"><span class="header-section-number">12.1</span> 你所没有深入的HTTP</h2>
  1633. <p>Internet有两个核心协议: IP和TCP,这样讲述起来似乎会很漫长。</p>
  1634. <p>基本概念</p>
  1635. <blockquote>
  1636. <p>超文本传输协议 (HTTP-Hypertext transfer protocol) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议。</p>
  1637. </blockquote>
  1638. <ul>
  1639. <li>HTTP是用于客户端与服务端之间的通信。</li>
  1640. <li>传输层的TCP是基于网络层的IP协议的,而应用层的HTTP协议又是基于传输层的TCP协议的。</li>
  1641. </ul>
  1642. <p><code>注意</code>: HTTP协议只规定了客户端与服务端的通信规则,而没有规定其通讯协议,只是现在的大部分实现都是将TCP作为通讯协议。</p>
  1643. <h3 id="打开网页时发生了什么"><span class="header-section-number">12.1.1</span> 打开网页时发生了什么</h3>
  1644. <p>简单地来说,当我们在浏览器上输入URL的敲下回车的时候。</p>
  1645. <ul>
  1646. <li>浏览器需要查找域名<span id="fnref2"><a href="#fn2"><sup>2</sup></a></span>的IP,从不同的缓存直至DNS服务器。</li>
  1647. <li>浏览器会给web服务器发送一个HTTP请求</li>
  1648. <li>服务器“处理”请求</li>
  1649. <li>服务器发回一个HTML响应</li>
  1650. <li>浏览器渲染HTML到页面。</li>
  1651. </ul>
  1652. <p>在<a href="http://stackoverflow.com/questions/2092527/what-happens-when-you-type-in-a-url-in-browser">StackOverflow</a>上有一个这样的回答会比较详细。</p>
  1653. <ul>
  1654. <li>browser checks cache; if requested object is in cache and is fresh, skip to #9</li>
  1655. <li>browser asks OS for server’s IP address</li>
  1656. <li>OS makes a DNS lookup and replies the IP address to the browser</li>
  1657. <li>browser opens a TCP connection to server (this step is much more complex with HTTPS)</li>
  1658. <li>browser sends the HTTP request through TCP connection</li>
  1659. <li>browser receives HTTP response and may close the TCP connection, or reuse it for another request</li>
  1660. <li>browser checks if the response is a redirect (3xx result status codes), authorization request (401), error (4xx and 5xx), etc.; these are handled differently from normal responses (2xx)</li>
  1661. <li>if cacheable, response is stored in cache</li>
  1662. <li>browser decodes response (e.g. if it’s gzipped)</li>
  1663. <li>browser determines what to do with response (e.g. is it a HTML page, is it an image, is it a sound clip?)</li>
  1664. <li>browser renders response, or offers a download dialog for unrecognized types</li>
  1665. </ul>
  1666. <p>忽略一些细节便剩下了</p>
  1667. <ol>
  1668. <li>从浏览器输入URL</li>
  1669. <li>浏览器找到服务器,服务器返回HTML文档</li>
  1670. <li>从对应的服务器下载资源</li>
  1671. </ol>
  1672. <p>说说第一步,开始时我们输入的是URI(统一资源标识符,Uniform Resource Identifier),它还有另外一个名字叫统一资源定位器(URL<span id="fnref3"><a href="#fn3"><sup>3</sup></a></span>,Uniform Resource Locator)。</p>
  1673. <h3 id="url组成"><span class="header-section-number">12.1.2</span> URL组成</h3>
  1674. <p>网址算是URL的一个俗称,让我们来看看一个URL的组成,以HTTP版IOT中的URL为例。</p>
  1675. <p><code>http://b.phodal.com/athome/1</code></p>
  1676. <p>开始之前,我们需要标出URL的80端口以及json文件的全称,那么上面的网址就会变成</p>
  1677. <p><code>http://b.phodal.com:80/athome/1.json</code></p>
  1678. <p>那么对于这个URL的就有下面几部分组成</p>
  1679. <ul>
  1680. <li><code>http://</code> http说的是这个URL用的是HTTP协议,至于<code>//</code>是一个分隔符,用法和C语言中的<code>;</code>一样。这样的协议还可以是coap,https,ftp等等。</li>
  1681. <li><code>b</code> 是子域名,一个域名在<strong>允许</strong>的情况下可以有不限数量的子域名。</li>
  1682. <li><code>phodal.com</code> 代表了一个URL是phodal.com下面的域名</li>
  1683. <li><code>80</code> 80是指80端口,默认的都是80,对于一个不是80端口的URL应该是这样的<code>http://iot-coap.phodal.com:8896/</code></li>
  1684. <li><code>athome</code> 指的是虚拟目录部分,或者文件路径</li>
  1685. <li><code>1.json</code>看上去就是一个文件名,然而也代表着这是一个资源。</li>
  1686. </ul>
  1687. <p>对就一个稍微复杂点的例子就是</p>
  1688. <p><code>http://designiot.phodal.com/#你所没有深入的http</code></p>
  1689. <p>这里的<code>#</code>后面是锚部分,如果你打开这个URL就会发现会直接跳转到相应的锚部分,对就于下面这样的一个例子来说</p>
  1690. <p><code>http://www.phodal.com/search/?q=iot&amp;type=blog</code></p>
  1691. <p><code>?</code>后面的<code>q=iot&amp;type=blog</code>的部分是参数部分,通常用于查询或者、搜索。</p>
  1692. <h2 id="一次http-get请求"><span class="header-section-number">12.2</span> 一次HTTP GET请求</h2>
  1693. <p>当我们打开最小物联网系统的一个页面时,如<a href="http://b.phodal.com/athome/1.json" class="uri">http://b.phodal.com/athome/1.json</a></p>
  1694. <p>我们在浏览器上看到的结果是</p>
  1695. <pre class="sourceCode javascript"><code class="sourceCode javascript">[
  1696. {
  1697. <span class="st">&quot;id&quot;</span>: <span class="dv">1</span>,
  1698. <span class="st">&quot;temperature&quot;</span>: <span class="dv">19</span>,
  1699. <span class="st">&quot;sensors1&quot;</span>: <span class="dv">31</span>,
  1700. <span class="st">&quot;sensors2&quot;</span>: <span class="fl">7.5</span>,
  1701. <span class="st">&quot;led1&quot;</span>: <span class="dv">0</span>
  1702. }
  1703. ]</code></pre>
  1704. <p>只是我们看到的是结果,忽略了这其中的过程,于是我们用curl<span id="fnref4"><a href="#fn4"><sup>4</sup></a></span>命令来看看详细的情况。</p>
  1705. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">curl</span> -I -s http://b.phodal.com/athome/1.json</code></pre>
  1706. <p>出于某种原因考虑,删去了其中一些元素,剩下下面这些。</p>
  1707. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.1</span> 200 OK
  1708. <span class="kw">Content-Type</span>: application/json
  1709. <span class="kw">Date</span>: Fri, 05 Sep 2014 15:05:49 GMT
  1710. [<span class="dt">{&quot;id&quot;:1,&quot;temperature&quot;:19,&quot;sensors1&quot;:31,&quot;sensors2&quot;:7.5,&quot;led1&quot;:0}</span>]</code></pre>
  1711. <p>我们用curl命令向服务器发起了GET请求,服务器返回了上面的结果。</p>
  1712. <h3 id="http响应"><span class="header-section-number">12.2.1</span> HTTP响应</h3>
  1713. <p>一个HTTP响应由三部分组成</p>
  1714. <ul>
  1715. <li>状态行(状态码)</li>
  1716. <li>消息报头(响应报头)</li>
  1717. <li>响应正文(消息体)</li>
  1718. </ul>
  1719. <h4 id="http响应-状态码"><span class="header-section-number">12.2.1.1</span> HTTP响应 状态码</h4>
  1720. <p>在上面的结果中,状态行是</p>
  1721. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.1</span> 200 OK</code></pre>
  1722. <p>返回的状态码是200,OK是状态码的原因短语。</p>
  1723. <p>如果是一个跳转的页面,它就可能是下面的结果:</p>
  1724. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.0</span> 301 MOVED PERMANENTLY
  1725. <span class="kw">Date</span>: Mon, 08 Sep 2014 12:04:01 GMT
  1726. <span class="kw">Content-Type</span>: text/html<span class="kw">;</span> <span class="ot">charset=</span>utf-8 </code></pre>
  1727. <p>HTTP Status有五种状态,而这五种状态又有所细分,提一下这五种状态,详细可参见<a href="http://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81">http://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81</a></p>
  1728. <ul>
  1729. <li>1xx消息</li>
  1730. <li>2xx成功</li>
  1731. <li>3xx重定向</li>
  1732. <li>4xx客户端错误</li>
  1733. <li>5xx服务器错误</li>
  1734. </ul>
  1735. <p>如</p>
  1736. <ul>
  1737. <li>200 ok - 成功返回状态,对应,GET,PUT,PATCH,DELETE.</li>
  1738. <li>201 created - 成功创建。</li>
  1739. <li>304 not modified - HTTP缓存有效。</li>
  1740. <li>400 bad request - 请求格式错误。</li>
  1741. <li>401 unauthorized - 未授权。</li>
  1742. <li>403 forbidden - 鉴权成功,但是该用户没有权限。</li>
  1743. <li>404 not found - 请求的资源不存在</li>
  1744. <li>405 method not allowed - 该http方法不被允许。</li>
  1745. <li>410 gone - 这个url对应的资源现在不可用。</li>
  1746. <li>415 unsupported media type - 请求类型错误。</li>
  1747. <li>422 unprocessable entity - 校验错误时用。</li>
  1748. <li>429 too many request - 请求过多。</li>
  1749. </ul>
  1750. <h4 id="http响应-响应报头"><span class="header-section-number">12.2.1.2</span> HTTP响应 响应报头</h4>
  1751. <p>在这次响应中,返回了两个报头,即</p>
  1752. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Content-Type</span>: application/json
  1753. <span class="kw">Date</span>: Fri, 05 Sep 2014 15:05:49 GMT</code></pre>
  1754. <p>Content-Type和Date,在这里的Context-Type是application/json,而通常情况下我们打开一个网站时,他的Content-Type应该是text/html。</p>
  1755. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Content-Type</span>: text/html<span class="kw">;</span></code></pre>
  1756. <p>Content-Type是最重要的报头。</p>
  1757. <h4 id="http响应-响应正文"><span class="header-section-number">12.2.1.3</span> HTTP响应 响应正文</h4>
  1758. <p>正文才是我们真正想要的内容,上面的都是写给浏览器看的,一般的人不会去关注这些。</p>
  1759. <pre class="sourceCode javascript"><code class="sourceCode javascript">HTTP/<span class="fl">1.1</span> <span class="dv">200</span> OK
  1760. Server: <span class="ot">phodal</span>.<span class="fu">com</span>/<span class="fl">0.17.5</span>
  1761. Content-Type: application/json
  1762. [{<span class="st">&quot;id&quot;</span>:<span class="dv">1</span>,<span class="st">&quot;temperature&quot;</span>:<span class="dv">19</span>,<span class="st">&quot;sensors1&quot;</span>:<span class="dv">31</span>,<span class="st">&quot;sensors2&quot;</span>:<span class="fl">7.5</span>,<span class="st">&quot;led1&quot;</span>:<span class="dv">0</span>}]</code></pre>
  1763. <p>通常这是以某种格式写的,在这里是以JSON写的,而对于一个网站的时候则是HTML,如:</p>
  1764. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  1765. <span class="kw">&lt;head&gt;</span>
  1766. <span class="kw">&lt;meta</span><span class="ot"> charset=</span><span class="st">&quot;UTF-8&quot;</span><span class="kw">&gt;</span>
  1767. <span class="kw">&lt;title&gt;</span>Document<span class="kw">&lt;/title&gt;</span>
  1768. <span class="kw">&lt;/head&gt;</span>
  1769. <span class="kw">&lt;body&gt;</span>
  1770. <span class="kw">&lt;/body&gt;</span>
  1771. <span class="kw">&lt;/html&gt;</span> </code></pre>
  1772. <p>那么这次GET请求返回的就是:</p>
  1773. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.0</span> 200 OK
  1774. <span class="kw">Date</span>: Mon, 08 Sep 2014 12:04:01 GMT
  1775. <span class="kw">Content-Type</span>: text/html<span class="kw">;</span> <span class="ot">charset=</span>utf-8
  1776. <span class="kw">&lt;html&gt;</span>
  1777. <span class="kw">&lt;head&gt;</span>
  1778. <span class="kw">&lt;meta</span> charset=<span class="st">&quot;UTF-8&quot;</span><span class="kw">&gt;</span>
  1779. <span class="kw">&lt;title&gt;</span>Document<span class="kw">&lt;</span>/title<span class="kw">&gt;</span>
  1780. <span class="kw">&lt;</span>/<span class="kw">head&gt;</span>
  1781. <span class="kw">&lt;body&gt;</span>
  1782. [<span class="dt">{&quot;id&quot;:1,&quot;temperature&quot;:19,&quot;sensors1&quot;:31,&quot;sensors2&quot;:7.5,&quot;led1&quot;:0}</span>]
  1783. <span class="kw">&lt;</span>/<span class="kw">body&gt;</span>
  1784. <span class="kw">&lt;</span>/<span class="kw">html&gt;</span> </code></pre>
  1785. <p>虽然与第一次请求的结果在游览器上看似乎是一样的(ps:可能有微小的差异),然而其本质是不同的。</p>
  1786. <p>推荐及参考书目:</p>
  1787. <ul>
  1788. <li>《Web性能权威指南》</li>
  1789. <li>《图解HTTP》</li>
  1790. <li>《RESTful Web Services Cookbook》</li>
  1791. <li>《RESTful Web APIs》</li>
  1792. </ul>
  1793. <h1 id="设计restful-api"><span class="header-section-number">13</span> 设计RESTful API</h1>
  1794. <blockquote>
  1795. <p>REST从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表征。获得这些表征致使这些应用程序转变了其状态。随着不断获取资源的表征,客户端应用不断地在转变着其状态,所谓表征状态转移。</p>
  1796. </blockquote>
  1797. <p>因为我们需要的是一个Machine到Machine沟通的平台,需要设计一个API。而设计一个API来说,RESTful是很不错的一种选择,也是主流的选择。而设计一个RESTful服务,的首要步骤便是设计资源模型。</p>
  1798. <h3 id="资源"><span class="header-section-number">13.0.1</span> 资源</h3>
  1799. <p>互联网上的一切信息都可以看作是一种资源。</p>
  1800. <table>
  1801. <thead>
  1802. <tr class="header">
  1803. <th align="left">HTTP Method</th>
  1804. <th align="left">Operation Performed</th>
  1805. </tr>
  1806. </thead>
  1807. <tbody>
  1808. <tr class="odd">
  1809. <td align="left">GET</td>
  1810. <td align="left">Get a resource (Read a resource)</td>
  1811. </tr>
  1812. <tr class="even">
  1813. <td align="left">POST</td>
  1814. <td align="left">Create a resource</td>
  1815. </tr>
  1816. <tr class="odd">
  1817. <td align="left">PUT</td>
  1818. <td align="left">Update a resource</td>
  1819. </tr>
  1820. <tr class="even">
  1821. <td align="left">DELETE</td>
  1822. <td align="left">Delete Resource</td>
  1823. </tr>
  1824. </tbody>
  1825. </table>
  1826. <h1 id="设计restful-api-1"><span class="header-section-number">14</span> 设计RESTful API</h1>
  1827. <p>设计RESTful API是一个有意思的话题。下面是一些常用的RESTful设计原则:</p>
  1828. <h2 id="rest关键目标"><span class="header-section-number">14.1</span> REST关键目标</h2>
  1829. <ul>
  1830. <li>组件间交互的可伸缩性</li>
  1831. <li>接口的通用性</li>
  1832. <li>组件的独立部署</li>
  1833. <li>通过中间组件来减少延迟、实施安全策略和封装已有系统</li>
  1834. </ul>
  1835. <h2 id="判断是否是-restful的约束条件"><span class="header-section-number">14.2</span> 判断是否是 RESTful的约束条件</h2>
  1836. <ul>
  1837. <li>客户端-服务器分离</li>
  1838. <li>无状态</li>
  1839. <li>可缓存</li>
  1840. <li>多层系统</li>
  1841. <li>统一接口</li>
  1842. <li>随需代码(可选)</li>
  1843. </ul>
  1844. <h2 id="json"><span class="header-section-number">14.3</span> JSON</h2>
  1845. <div class="figure">
  1846. <img src="./images/xml-vs-json.png" alt="xml-vs-json" />
  1847. <p>xml-vs-json</p>
  1848. </div>
  1849. <h1 id="环境准备"><span class="header-section-number">15</span> 环境准备</h1>
  1850. <h2 id="laravel"><span class="header-section-number">15.1</span> Laravel</h2>
  1851. <blockquote>
  1852. <p>Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。</p>
  1853. </blockquote>
  1854. <ul>
  1855. <li>RESTful 路由: 通过简单的闭包就能响应HTTP请求。帮你快速开始构建非凡的应用。</li>
  1856. <li>强大的数据操纵能力: Laravel自带了强大的Eloquent ORM 和迁移工具。能够完美的与MySQL、Postgres、SQL Server 和 SQLite协同工作。</li>
  1857. <li>优雅的模版引擎: PHP代码或轻量级的Blade模版引擎都可无缝融合。Blade模版可以继承,并且拥有极快的解析速度。相信你会喜欢它的。</li>
  1858. <li>为明天做准备: 构建大型的企业级应用或者只是提供简单的JSON API;书写强大的控制器或轻巧的RESTful路由,Laravel适应所有级别的开发工作。</li>
  1859. <li>可靠的基石: Laravel 的基石是数个Symfony组件,这些经过千锤百炼、可靠的组件为你的应用提供坚实的基础。</li>
  1860. <li>基于Composer管理器: Composer 是一套帮你管理第三方扩展包的工具。能够让你迅速在 Packagist 中找到需要的扩展包。</li>
  1861. <li>强大的社区支持: 无论你是一个PHP新手还是经验丰富的架构师,都能在社区中找到需要的知识。你可以在IRC中讨论Idea,或者在论坛中发布问题。</li>
  1862. <li>测试、重构: Laravel 从开始就将测试作为重点功能。我们提供了灵活的IoC容器,集成了PHPUnit 测试工具。不用担心,这些都很容易上手。</li>
  1863. </ul>
  1864. <h3 id="为什么是-laravel"><span class="header-section-number">15.1.1</span> 为什么是 Laravel</h3>
  1865. <ul>
  1866. <li>因为个人喜爱,你也可以用 Ruby On Rails来搭建这样一个功能,或者是Java。</li>
  1867. <li>PHP在我的服务器上运行得挺不错的,而且我又不需要重新去写配置那些配置。</li>
  1868. <li>Laravel 可以简单的开发我们所需要的功能,换句话说他是 PHP 世界的 Ruby On Rails。</li>
  1869. </ul>
  1870. <p>这里不会再重述之前的问题,这里只是将需要的步骤一个个写下来,然后丢到这里好好说一下。至于RESTful是什么,前面已经介绍了,就不再重复了。那么下面,我们就用Laravel来搭建一个平台给物联网用的。</p>
  1871. <h2 id="安装-laravel"><span class="header-section-number">15.2</span> 安装 Laravel</h2>
  1872. <h3 id="gnulinux安装composer"><span class="header-section-number">15.2.1</span> GNU/Linux安装Composer</h3>
  1873. <p>GNU/Linux Ubuntu/OpenSUSE下可以执行</p>
  1874. <pre><code>$ curl -sS https://getcomposer.org/installer | php</code></pre>
  1875. <h4 id="windows安装composer"><span class="header-section-number">15.2.1.1</span> Windows安装Composer</h4>
  1876. <p>请直接下载</p>
  1877. <p><a href="https://getcomposer.org/Composer-Setup.exe">Composer-Setup</a></p>
  1878. <h4 id="mac-os"><span class="header-section-number">15.2.1.2</span> Mac OS</h4>
  1879. <p>1.安装Composer</p>
  1880. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install homebrew/php/composer</code></pre>
  1881. <p>2.安装Laravel</p>
  1882. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> global require <span class="st">&quot;laravel/installer=~1.1&quot;</span></code></pre>
  1883. <p>3.创建Laravel工程</p>
  1884. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> create-project laravel/laravel your-project-name --prefer-dist </code></pre>
  1885. <h4 id="mac-os-1"><span class="header-section-number">15.2.1.3</span> Mac OS</h4>
  1886. <p>1.下载laravel.phar</p>
  1887. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">wget</span> http://laravel.com/laravel.phar</code></pre>
  1888. <p>2.重命名</p>
  1889. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">mv</span> laravel.phar laravel</code></pre>
  1890. <p>3.移动到bin中</p>
  1891. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> mv laravel /usr/local/bin</code></pre>
  1892. <p>4.创建项目</p>
  1893. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">laravel</span> new blog</code></pre>
  1894. <h2 id="mysql"><span class="header-section-number">15.3</span> MySQL</h2>
  1895. <h3 id="安装mysql-1"><span class="header-section-number">15.3.1</span> 安装MySQL</h3>
  1896. <p><code>出于某些原因,我建议用MariaDB替换MySQL,如果你&quot;真正&quot;需要mysql,将mariadb替换为mysql</code></p>
  1897. <p>ps: 在下文中我会继续用MySQL,而不是MariaDB,MairaDB是MySQL的一个分支,真正的开源分支。</p>
  1898. <p>Ubuntu/Debian/Mint</p>
  1899. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> apt-get install mariadb-server</code></pre>
  1900. <p>Fedora/Centos</p>
  1901. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> yum install mariadb-server </code></pre>
  1902. <p>openSUSE</p>
  1903. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> zypper install mariadb-server</code></pre>
  1904. <p>Mac OS</p>
  1905. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">brew</span> install mariadb</code></pre>
  1906. <h3 id="配置mysql"><span class="header-section-number">15.3.2</span> 配置MySQL</h3>
  1907. <p>修改database.php</p>
  1908. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">app/config/database.php</span></code></pre>
  1909. <p>要修改的就是这个</p>
  1910. <pre class="sourceCode php"><code class="sourceCode php"><span class="st">&#39;mysql&#39;</span> =&gt; <span class="fu">array</span><span class="ot">(</span>
  1911. <span class="st">&#39;driver&#39;</span> =&gt; <span class="st">&#39;mysql&#39;</span><span class="ot">,</span>
  1912. <span class="st">&#39;host&#39;</span> =&gt; <span class="st">&#39;localhost&#39;</span><span class="ot">,</span>
  1913. <span class="st">&#39;database&#39;</span> =&gt; <span class="st">&#39;iot&#39;</span><span class="ot">,</span>
  1914. <span class="st">&#39;username&#39;</span> =&gt; <span class="st">&#39;root&#39;</span><span class="ot">,</span>
  1915. <span class="st">&#39;password&#39;</span> =&gt; <span class="st">&#39;940217&#39;</span><span class="ot">,</span>
  1916. <span class="st">&#39;charset&#39;</span> =&gt; <span class="st">&#39;utf8&#39;</span><span class="ot">,</span>
  1917. <span class="st">&#39;collation&#39;</span> =&gt; <span class="st">&#39;utf8_unicode_ci&#39;</span><span class="ot">,</span>
  1918. <span class="st">&#39;prefix&#39;</span> =&gt; <span class="st">&#39;&#39;</span><span class="ot">,</span>
  1919. <span class="ot">),</span></code></pre>
  1920. <p>如果你已经有phpmyadmin,似乎对你来说已经很简单了,如果没有的话,就直接用</p>
  1921. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre>
  1922. <p>来创建一个新的</p>
  1923. <pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">CREATE</span> <span class="kw">DATABASE</span> <span class="kw">IF</span> <span class="kw">NOT</span> <span class="kw">EXISTS</span> iot <span class="kw">default</span> charset utf8 COLLATE utf8_general_ci;</code></pre>
  1924. <p>数据库的目的在于存储数据等等的闲话这里就不多说了,创建一个RESTful的目的在于产生下面的JSON格式数据,以便于我们在Android、Java、Python、jQuery等语言框架或者平台上可以调用,最主要的是可以直接用Ajax来产生更炫目的效果。</p>
  1925. <pre class="sourceCode javascript"><code class="sourceCode javascript">{
  1926. <span class="st">&quot;id&quot;</span>: <span class="dv">1</span>,
  1927. <span class="st">&quot;temperature&quot;</span>: <span class="dv">14</span>,
  1928. <span class="st">&quot;sensors1&quot;</span>: <span class="dv">12</span>,
  1929. <span class="st">&quot;sensors2&quot;</span>: <span class="dv">12</span>,
  1930. <span class="st">&quot;led1&quot;</span>: <span class="dv">0</span>
  1931. }</code></pre>
  1932. <h1 id="创建rest服务"><span class="header-section-number">16</span> 创建REST服务</h1>
  1933. <h2 id="数据库迁移"><span class="header-section-number">16.1</span> 数据库迁移</h2>
  1934. <p>这个名字是源自于Ruby On Rails在那时候的印象,不直接使用MySQL的目的在于让我们可以专注于过程。</p>
  1935. <h3 id="创建表"><span class="header-section-number">16.1.1</span> 创建表</h3>
  1936. <p>表的概念,类似于在Excel中的表,如果你真实不懂数据库。 让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些次要的因素吧。</p>
  1937. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate:make create_athomes_table</code></pre>
  1938. <p>打开 app/database/migrations/<strong><em>create_athomes_table.php这里的</em></strong>是由日期和某些东西组成的,修改生成的代码为下面。</p>
  1939. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">use</span> Illuminate\Database\Schema\Blueprint<span class="ot">;</span>
  1940. <span class="kw">use</span> Illuminate\Database\Migrations\Migration<span class="ot">;</span>
  1941. <span class="kw">class</span> CreateAthomesTable <span class="kw">extends</span> Migration {
  1942. <span class="kw">public</span> <span class="kw">function</span> up<span class="ot">()</span>
  1943. {
  1944. Schema::create<span class="ot">(</span><span class="st">&#39;athomes&#39;</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span>Blueprint <span class="kw">$table</span><span class="ot">)</span>
  1945. {
  1946. <span class="kw">$table</span>---&gt;increments<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  1947. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  1948. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  1949. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  1950. <span class="kw">$table</span>-&gt;boolean<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  1951. <span class="kw">$table</span>-&gt;timestamps<span class="ot">();</span>
  1952. }<span class="ot">);</span>
  1953. }
  1954. <span class="kw">public</span> <span class="kw">function</span> down<span class="ot">()</span>
  1955. {
  1956. Schema::drop<span class="ot">(</span><span class="st">&#39;athomes&#39;</span><span class="ot">);</span>
  1957. }
  1958. }</code></pre>
  1959. <p>意思大致就是id是自加的,也就是我们在localhost/athome/{id},当我们创建一个新的数据的时候,会自动加上去,最后一个timestamps批的是时间,会包含创建时间和修改时间。 剩下的temperature,sensors1,sensors2是小数,以及只有真和假的led1。</p>
  1960. <h3 id="数据库迁移-1"><span class="header-section-number">16.1.2</span> 数据库迁移</h3>
  1961. <p>我们只是写了我们需要的数据的格式而并没有丢到数据库里,</p>
  1962. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate</code></pre>
  1963. <p>这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。</p>
  1964. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre>
  1965. <pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">use</span> iot;
  1966. <span class="kw">select</span> * <span class="kw">from</span> athomes;</code></pre>
  1967. <p>就可以看到我们写的东西,那么接下来就是创建RESTful服务了</p>
  1968. <h2 id="创建restful"><span class="header-section-number">16.2</span> 创建RESTful</h2>
  1969. <p>用下面的代码实现我们称之为Athomes控制器的创建</p>
  1970. <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan controller:make AthomesController </code></pre>
  1971. <p>就会在app/controllers下面生成下面的代码</p>
  1972. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
  1973. <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span> {}
  1974. <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span> {}
  1975. <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span> {}
  1976. <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  1977. <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  1978. <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  1979. <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  1980. }</code></pre>
  1981. <h2 id="laravel-resources"><span class="header-section-number">16.3</span> Laravel Resources</h2>
  1982. <p>上面的代码过于沉重,请让我用 Ctrl+C 来带来点知识吧。</p>
  1983. <table>
  1984. <thead>
  1985. <tr class="header">
  1986. <th align="left">Verb</th>
  1987. <th align="left">Path</th>
  1988. <th align="left">Action</th>
  1989. <th align="left">Route Name</th>
  1990. </tr>
  1991. </thead>
  1992. <tbody>
  1993. <tr class="odd">
  1994. <td align="left">GET</td>
  1995. <td align="left">/resource</td>
  1996. <td align="left">index</td>
  1997. <td align="left">resource.index</td>
  1998. </tr>
  1999. <tr class="even">
  2000. <td align="left">GET</td>
  2001. <td align="left">/resource/create</td>
  2002. <td align="left">create</td>
  2003. <td align="left">resource.create</td>
  2004. </tr>
  2005. <tr class="odd">
  2006. <td align="left">POST</td>
  2007. <td align="left">/resource</td>
  2008. <td align="left">store</td>
  2009. <td align="left">resource.store</td>
  2010. </tr>
  2011. <tr class="even">
  2012. <td align="left">GET</td>
  2013. <td align="left">/resource/{resource}</td>
  2014. <td align="left">show</td>
  2015. <td align="left">resource.show</td>
  2016. </tr>
  2017. <tr class="odd">
  2018. <td align="left">GET</td>
  2019. <td align="left">/resource/{resource}/edit</td>
  2020. <td align="left">edit</td>
  2021. <td align="left">resource.edit</td>
  2022. </tr>
  2023. <tr class="even">
  2024. <td align="left">PUT/PATCH</td>
  2025. <td align="left">/resource/{resource}</td>
  2026. <td align="left">update</td>
  2027. <td align="left">resource.update</td>
  2028. </tr>
  2029. <tr class="odd">
  2030. <td align="left">DELETE</td>
  2031. <td align="left">/resource/{resource}</td>
  2032. <td align="left">destroy</td>
  2033. <td align="left">resource.destroy</td>
  2034. </tr>
  2035. </tbody>
  2036. </table>
  2037. <p>所以我们只需要专注于创建 create, edit, show, destory 等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在 app/model 加个 class</p>
  2038. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> Athomes <span class="kw">extends</span> Eloquent {
  2039. <span class="kw">protected</span> <span class="kw">$table</span> = <span class="st">&#39;athomes&#39;</span><span class="ot">;</span>
  2040. }</code></pre>
  2041. <p>如果你想要的只是控制器Athomes的代码的话。。</p>
  2042. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
  2043. <span class="kw">public</span> <span class="kw">$restful</span>=<span class="kw">true</span><span class="ot">;</span>
  2044. <span class="kw">protected</span> <span class="kw">$athome</span><span class="ot">;</span>
  2045. <span class="kw">public</span> <span class="kw">function</span> <span class="fu">__construct</span><span class="ot">(</span>Athomes <span class="kw">$athome</span><span class="ot">)</span>
  2046. {
  2047. <span class="kw">$this</span>---&gt;athome = <span class="kw">$athome</span> <span class="ot">;</span>
  2048. }
  2049. <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span>
  2050. {
  2051. <span class="kw">$maxid</span>=Athomes::all<span class="ot">();</span>
  2052. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
  2053. }
  2054. <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  2055. {
  2056. <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  2057. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">)</span>-&gt;with<span class="ot">(</span><span class="st">&#39;maxid&#39;</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
  2058. }
  2059. <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
  2060. {
  2061. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  2062. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required&#39;</span><span class="ot">,</span>
  2063. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2064. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2065. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  2066. <span class="ot">);</span>
  2067. <span class="kw">$validator</span> = Validator::make<span class="ot">(</span>Input::all<span class="ot">(),</span> <span class="kw">$rules</span><span class="ot">);</span>
  2068. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  2069. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/create&#39;</span><span class="ot">)</span>
  2070. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">)</span>
  2071. -&gt;withInput<span class="ot">(</span>Input::except<span class="ot">(</span><span class="st">&#39;password&#39;</span><span class="ot">));</span>
  2072. } <span class="kw">else</span> {
  2073. <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
  2074. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  2075. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  2076. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  2077. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  2078. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  2079. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully created athome!&#39;</span><span class="ot">);</span>
  2080. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2081. }
  2082. }
  2083. <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2084. {
  2085. <span class="kw">$myid</span>=Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2086. <span class="kw">$maxid</span>=Athomes::where<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">,</span><span class="st">&#39;=&#39;</span><span class="ot">,</span><span class="kw">$id</span><span class="ot">)</span>
  2087. -&gt;select<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">,</span><span class="st">&#39;temperature&#39;</span><span class="ot">,</span><span class="st">&#39;sensors1&#39;</span><span class="ot">,</span><span class="st">&#39;sensors2&#39;</span><span class="ot">,</span><span class="st">&#39;led1&#39;</span><span class="ot">)</span>
  2088. -&gt;get<span class="ot">();</span>
  2089. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
  2090. }
  2091. <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2092. {
  2093. <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2094. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.edit&#39;</span><span class="ot">)</span>
  2095. -&gt;with<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">,</span> <span class="kw">$athome</span><span class="ot">);</span>
  2096. }
  2097. <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2098. {
  2099. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  2100. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required|&#39;</span><span class="ot">,</span>
  2101. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2102. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2103. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  2104. <span class="ot">);</span>
  2105. <span class="kw">$validator</span> = Validator::make<span class="ot">(</span>Input::all<span class="ot">(),</span> <span class="kw">$rules</span><span class="ot">);</span>
  2106. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  2107. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/&#39;</span> . <span class="kw">$id</span> . <span class="st">&#39;/edit&#39;</span><span class="ot">)</span>
  2108. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
  2109. } <span class="kw">else</span> {
  2110. <span class="kw">$nerd</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2111. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  2112. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  2113. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  2114. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  2115. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  2116. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully created athome!&#39;</span><span class="ot">);</span>
  2117. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2118. }
  2119. }
  2120. <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2121. {
  2122. <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2123. <span class="kw">$athome</span>-&gt;delete<span class="ot">();</span>
  2124. <span class="kw">if</span><span class="ot">(</span><span class="fu">is_null</span><span class="ot">(</span><span class="kw">$athome</span><span class="ot">))</span>
  2125. {
  2126. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="st">&#39;Todo not found&#39;</span><span class="ot">,</span> <span class="dv">404</span><span class="ot">);</span>
  2127. }
  2128. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully deleted the nerd!&#39;</span><span class="ot">);</span>
  2129. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2130. }
  2131. }</code></pre>
  2132. <p>希望你能读懂,没有的话,继续。</p>
  2133. <p>下面这部分来自于之前的博客,这里就不多加论述了。 这个也就是我们要的模板,</p>
  2134. <h3 id="修改create"><span class="header-section-number">16.3.1</span> 修改Create()</h3>
  2135. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  2136. {
  2137. <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  2138. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">)</span>-&gt;with<span class="ot">(</span><span class="st">&#39;maxid&#39;</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
  2139. }</code></pre>
  2140. <p>这里需要在app/views/创建一个athome里面创建一个create.blade.php,至于maxid,暂时还不需要,后面会用到show。如果只需要模板,可以简化为</p>
  2141. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  2142. {
  2143. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">);</span>
  2144. }</code></pre>
  2145. <p>这里只是对其中代码的进行一下说明。</p>
  2146. <h3 id="创建表单"><span class="header-section-number">16.3.2</span> 创建表单</h3>
  2147. <h4 id="创建表单之前"><span class="header-section-number">16.3.2.1</span> 创建表单之前</h4>
  2148. <p>由于使用到了bootstrap以及bootstrap-select,记得添加css。</p>
  2149. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span>
  2150. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap-select.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span></code></pre>
  2151. <p>以及javascript</p>
  2152. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/jquery.min.js&#39;)?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  2153. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  2154. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap-select.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  2155. <span class="kw">&lt;script&gt;</span>
  2156. <span class="fu">$</span>(<span class="st">&#39;.selectpicker&#39;</span>).<span class="fu">selectpicker</span>();
  2157. &lt;<span class="ot">/script&gt;</span></code></pre>
  2158. <h4 id="创建表单-1"><span class="header-section-number">16.3.2.2</span> 创建表单</h4>
  2159. <p>这里用到的是之前提到的那个作者写下的,稍微修改了一下。</p>
  2160. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;row-fluid&quot;</span><span class="kw">&gt;</span>
  2161. {{ HTML::ul($errors-&gt;all()) }}
  2162. {{ Form::open(array(&#39;url&#39; =&gt; &#39;athome&#39;)) }}
  2163. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2164. {{ Form::label(&#39;led1&#39;, &#39;开关1&#39;) }}
  2165. {{ Form::select(&#39;led1&#39;,array(&#39;关&#39;,&#39;开&#39;),$selected=NULL,array(&#39;class&#39;=&gt;&#39;selectpicker&#39;)) }}
  2166. <span class="kw">&lt;/div&gt;</span>
  2167. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2168. {{ Form::label(&#39;sensors1&#39;, &#39;sensors1&#39;) }}
  2169. {{ Form::text(&#39;sensors1&#39;, Input::old(&#39;sensors1&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2170. <span class="kw">&lt;/div&gt;</span>
  2171. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2172. {{ Form::label(&#39;sensors2&#39;, &#39;sensors2&#39;) }}
  2173. {{ Form::text(&#39;sensors2&#39;, Input::old(&#39;sensors2&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2174. <span class="kw">&lt;/div&gt;</span>
  2175. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2176. {{ Form::label(&#39;temperature&#39;, &#39;temperature&#39;) }}
  2177. {{ Form::text(&#39;temperature&#39;, Input::old(&#39;temperature&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2178. <span class="kw">&lt;/div&gt;</span>
  2179. {{ Form::submit(&#39;Create!&#39;, array(&#39;class&#39; =&gt; &#39;btn btn-primary&#39;)) }}
  2180. {{ Form::close() }}
  2181. <span class="kw">&lt;/div&gt;</span></code></pre>
  2182. <p>开关一开始打算用 checkbox,加上 bootstrap-switch 实现 ON OFF 弱弱地觉得还是没掌握好的节奏,所以最后用 select 来实现。</p>
  2183. <p>还需要修改一下之前的 create(),添加一行</p>
  2184. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span></code></pre>
  2185. <p>也就是添加完后,重定向到首页查看,最后例子给出的 create 如下</p>
  2186. <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
  2187. {
  2188. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  2189. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required&#39;</span><span class="ot">,</span>
  2190. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2191. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2192. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  2193. <span class="ot">);</span>
  2194. <span class="kw">$validator</span> = Validator::make<span class="ot">(</span>Input::all<span class="ot">(),</span> <span class="kw">$rules</span><span class="ot">);</span>
  2195. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  2196. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/create&#39;</span><span class="ot">)</span>
  2197. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
  2198. } <span class="kw">else</span> {
  2199. <span class="co">// store</span>
  2200. <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
  2201. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  2202. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  2203. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  2204. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  2205. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  2206. Session::flash<span class="ot">(</span><span class="st">&#39;message&#39;</span><span class="ot">,</span> <span class="st">&#39;Successfully created athome!&#39;</span><span class="ot">);</span>
  2207. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2208. }
  2209. }</code></pre>
  2210. <h3 id="编辑模板"><span class="header-section-number">16.3.3</span> 编辑模板</h3>
  2211. <p>完整的 blade 模板文件</p>
  2212. <pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html lang=&quot;zh-cn&quot;<span class="dt">&gt;</span>
  2213. <span class="kw">&lt;html&gt;</span>
  2214. <span class="kw">&lt;head&gt;</span>
  2215. <span class="kw">&lt;meta</span><span class="ot"> http-equiv=</span><span class="st">&quot;Content-type&quot;</span><span class="ot"> content=</span><span class="st">&quot;text/html; charset=utf-8&quot;</span><span class="kw">&gt;</span>
  2216. <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;keywords&quot;</span><span class="ot"> content=</span><span class="st">&quot;&quot;</span><span class="kw">&gt;</span>
  2217. <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;viewport&quot;</span><span class="ot"> content=</span><span class="st">&quot;width=device-width&quot;</span><span class="kw">&gt;</span>
  2218. <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;description&quot;</span><span class="ot"> content=</span><span class="st">&quot;&quot;</span><span class="kw">&gt;</span>
  2219. <span class="kw">&lt;title&gt;</span>@yield(&#39;title&#39;)<span class="kw">&lt;/title&gt;</span>
  2220. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span>
  2221. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/bootstrap-select.min.css&#39;) ?&gt;&quot;</span> <span class="kw">/&gt;</span>
  2222. <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> href=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;css/justified-nav.css&#39;) ?&gt;&quot;</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="ot"> media=</span><span class="st">&quot;screen&quot;</span> <span class="kw">/&gt;</span>
  2223. <span class="kw">&lt;/head&gt;</span>
  2224. <span class="kw">&lt;body&gt;</span>
  2225. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;container&quot;</span><span class="kw">&gt;</span>
  2226. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;container&quot;</span><span class="kw">&gt;</span>
  2227. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;row-fluid&quot;</span><span class="kw">&gt;</span>
  2228. <span class="kw">&lt;h1&gt;</span>Edit {{ $athome-&gt;id }}<span class="kw">&lt;/h1&gt;</span>
  2229. <span class="co">&lt;!-- if there are creation errors, they will show here --&gt;</span>
  2230. {{ HTML::ul($errors-&gt;all()) }}
  2231. {{ Form::model($athome, array(&#39;route&#39; =&gt; array(&#39;athome.update&#39;, $athome-&gt;id), &#39;method&#39; =&gt; &#39;PUT&#39;)) }}
  2232. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2233. {{ Form::label(&#39;led1&#39;, &#39;开关1&#39;) }}
  2234. {{ Form::select(&#39;led1&#39;,array(&#39;关&#39;,&#39;开&#39;),$selected=NULL,array(&#39;class&#39;=&gt;&#39;selectpicker&#39;)) }}
  2235. <span class="kw">&lt;/div&gt;</span>
  2236. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2237. {{ Form::label(&#39;sensors1&#39;, &#39;传感器1&#39;) }}
  2238. {{ Form::text(&#39;sensors1&#39;, Input::old(&#39;sensors1&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2239. <span class="kw">&lt;/div&gt;</span>
  2240. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2241. {{ Form::label(&#39;sensors2&#39;, &#39;传感器2&#39;) }}
  2242. {{ Form::text(&#39;sensors2&#39;, Input::old(&#39;sensors2&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2243. <span class="kw">&lt;/div&gt;</span>
  2244. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2245. {{ Form::label(&#39;temperature&#39;, &#39;温度传感器&#39;) }}
  2246. {{ Form::text(&#39;temperature&#39;, Input::old(&#39;temperature&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2247. <span class="kw">&lt;/div&gt;</span>
  2248. {{ Form::submit(&#39;Edit the Nerd!&#39;, array(&#39;class&#39; =&gt; &#39;btn btn-primary&#39;)) }}
  2249. {{ Form::close() }}
  2250. <span class="kw">&lt;/div&gt;</span>
  2251. <span class="kw">&lt;/div&gt;</span>
  2252. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;footer&quot;</span><span class="kw">&gt;</span>
  2253. <span class="kw">&lt;p&gt;</span>© Company 2013<span class="kw">&lt;/p&gt;</span>
  2254. <span class="kw">&lt;/div&gt;</span>
  2255. <span class="kw">&lt;/div&gt;</span>
  2256. <span class="kw">&lt;/div&gt;</span>
  2257. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/jquery.min.js&#39;)?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  2258. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  2259. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="ot"> src=</span><span class="st">&quot;</span><span class="er">&lt;</span><span class="st">?= url(&#39;js/bootstrap-select.min.js&#39;) ?&gt;&quot;</span><span class="kw">&gt;&lt;/script&gt;</span>
  2260. <span class="kw">&lt;script&gt;</span>
  2261. <span class="fu">$</span>(<span class="st">&#39;.selectpicker&#39;</span>).<span class="fu">selectpicker</span>();
  2262. &lt;<span class="ot">/script&gt;</span>
  2263. <span class="ot">&lt;script type=&quot;text/javascript</span><span class="st">&quot; src=&quot;</span>&lt;?= <span class="fu">url</span>(<span class="st">&#39;js/log.js&#39;</span>) ?&gt;<span class="st">&quot;&gt;&lt;/script&gt;</span>
  2264. &lt;<span class="ot">/body&gt;</span>
  2265. <span class="ot">&lt;/html</span>&gt;</code></pre>
  2266. <p>效果图:</p>
  2267. <div class="figure">
  2268. <img src="./images/edit.png" alt="Blade Edit" />
  2269. <p>Blade Edit</p>
  2270. </div>
  2271. <p>最后效果见:<a href="http://b.phodal.com/" class="uri">http://b.phodal.com/</a></p>
  2272. <h1 id="前端显示"><span class="header-section-number">17</span> 前端显示</h1>
  2273. <h2 id="库与车轮子"><span class="header-section-number">17.1</span> 库与车轮子</h2>
  2274. <p>在多数的情况下我们都没有理由也没有必要去重新发明我们的车轮,在这时使用库会是一个比较好的做法。</p>
  2275. <h2 id="库"><span class="header-section-number">17.2</span> 库</h2>
  2276. <h3 id="jquery"><span class="header-section-number">17.2.1</span> jQuery</h3>
  2277. <blockquote>
  2278. <p>Jquery是继prototype之后又一个优秀的Javascript库。它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及后续版本将不再支持IE6/7/8浏览器。jQuery使用户能更方便地处理HTML(标准通用标记语言下的一个应用)、events、实现动画效果,并且方便地为网站提供AJAX交互。jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。jQuery能够使用户的html页面保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需要定义id即可。</p>
  2279. </blockquote>
  2280. <p>在我们的代码里用到了jQuery,然而这是一种简单而且快速有速地方法。</p>
  2281. <h3 id="jquery-mobile"><span class="header-section-number">17.2.2</span> jQuery Mobile</h3>
  2282. <blockquote>
  2283. <p>jQuery Mobile是jQuery 在手机上和平板设备上的版本。jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架。支持全球主流的移动平台。jQuery Mobile开发团队说:能开发这个项目,我们非常兴奋。移动Web太需要一个跨浏览器的框架,让开发人员开发出真正的移动Web网站。</p>
  2284. </blockquote>
  2285. <p>整个展示页面由三部分组成,即<code>header</code>,<code>content</code>,<code>footer</code>。而我们主要需要关心的是content,也就是真下的内容。</p>
  2286. <p>我们只需要结合<code>HighChart</code>来设计我们的content就可以了,下面是一个简单地<code>HighChart</code>示例:</p>
  2287. <pre><code>&lt;div id=&quot;Tchart&quot; style=&quot;min-width:800px;width:100%;height:300px&quot;&gt;&lt;/div&gt;
  2288. &lt;/div&gt;</code></pre>
  2289. <p>剩下的事就由<code>HighChart</code>来做。</p>
  2290. <p>最后代码如下所示</p>
  2291. <pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;body&gt;</span>
  2292. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;page&quot;</span><span class="kw">&gt;</span>
  2293. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;header&quot;</span><span class="ot"> class=</span><span class="st">&quot;ui-bar ui-bar-b&quot;</span><span class="ot"> class=</span><span class="st">&quot;jqm-header&quot;</span><span class="kw">&gt;</span>
  2294. <span class="kw">&lt;h1&gt;</span>基础控制<span class="kw">&lt;/h1&gt;</span>
  2295. <span class="kw">&lt;/div&gt;</span>
  2296. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;collapsible&quot;</span><span class="ot"> data-collapsed=</span><span class="st">&quot;false&quot;</span><span class="kw">&gt;</span>
  2297. <span class="kw">&lt;h3&gt;</span>查看温度情况<span class="kw">&lt;/h3&gt;</span>
  2298. <span class="kw">&lt;div&gt;</span>
  2299. <span class="kw">&lt;div</span><span class="ot"> id=</span><span class="st">&quot;Tchart&quot;</span><span class="ot"> style=</span><span class="st">&quot;min-width:800px;width:100%;height:300px&quot;</span><span class="kw">&gt;&lt;/div&gt;</span>
  2300. <span class="kw">&lt;/div&gt;</span>
  2301. <span class="kw">&lt;/div&gt;</span>
  2302. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;footer&quot;</span><span class="ot"> class=</span><span class="st">&quot;ui-bar ui-bar-b&quot;</span><span class="ot"> data-position=</span><span class="st">&quot;fixed&quot;</span><span class="kw">&gt;</span>
  2303. <span class="kw">&lt;h4&gt;&lt;a</span><span class="ot"> href=</span><span class="st">&quot;http://www.phodal.com&quot;</span><span class="kw">&gt;</span>Power by Phodal<span class="kw">&lt;/a&gt;&lt;/h4&gt;</span>
  2304. <span class="kw">&lt;/div&gt;</span>
  2305. <span class="kw">&lt;/body&gt;</span></code></pre>
  2306. <p>可以看到上面的代码与一般的HTML不同的地方是<code>data-role</code>,<code>data-collapsed</code>,<code>data-position</code>,而这些是jQuery Mobile所拥有的data属性。</p>
  2307. <p><strong>data-role参数表</strong>: rold | 详细 ————–|—- page | 页面容器,其内部的mobile元素将会继承这个容器上所设置的属性 header | 页面标题容器,这个容器内部可以包含文字、返回按钮、功能按钮等元素 footer | 页面页脚容器,这个容器内部也可以包含文字、返回按钮、功能按钮等元素 content | 页面内容容器,这是一个很宽容的容器,内部可以包含标准的html元素和jQueryMobile元素 controlgroup | 将几个元素设置成一组,一般是几个相同的元素类型 fieldcontain | 区域包裹容器,用增加边距和分割线的方式将容器内的元素和容器外的元素明显分隔 navbar |功能导航容器,通俗的讲就是工具条 listview |列表展示容器,类似手机中联系人列表的展示方式 list-divider | 列表展示容器的表头,用来展示一组列表的标题,内部不可包含链接 button | 按钮,将链接和普通按钮的样式设置成为jQueryMobile的风格 none | 阻止框架对元素进行渲染,使元素以html原生的状态显示,主要用于form元素。</p>
  2308. <p>同上,我们也可以在网上找到其他相对就的属性。</p>
  2309. <h2 id="网站前台显示"><span class="header-section-number">17.3</span> 网站前台显示</h2>
  2310. <h3 id="highcharts"><span class="header-section-number">17.3.1</span> Highcharts</h3>
  2311. <p>Highcharts有以下的特点</p>
  2312. <ul>
  2313. <li>兼容性:兼容当今所有的浏览器,包括 iPhone、IE 和火狐等等;</li>
  2314. <li>对个人用户完全免费;</li>
  2315. <li>纯JS,无BS;</li>
  2316. <li>支持大部分的图表类型:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散布图;</li>
  2317. <li>跨语言:不管是 PHP、Asp.net 还是 Java 都可以使用,它只需要三个文件:一个是Highcharts 的核心文件 highcharts.js,还有 a canvas emulator for IE 和 Jquery类库或者 MooTools 类库;</li>
  2318. <li>提示功能:鼠标移动到图表的某一点上有提示信息;</li>
  2319. <li>放大功能:选中图表部分放大,近距离观察图表;</li>
  2320. <li>易用性:无需要特殊的开发技能,只需要设置一下选项就可以制作适合自己的图表;</li>
  2321. <li>时间轴:可以精确到毫秒;</li>
  2322. </ul>
  2323. <p>在这里只需将需要处理的数据存储到数组中,便可以将其渲染成为图形,下面的温度走势图便是基于Highcharts的结果:</p>
  2324. <div class="figure">
  2325. <img src="./images/temperture.png" alt="Temperture" />
  2326. <p>Temperture</p>
  2327. </div>
  2328. <p>先看看最后代码如下所示:</p>
  2329. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength = [];
  2330. <span class="kw">function</span> <span class="fu">drawTemp</span>() {
  2331. <span class="kw">var</span> zero = [];
  2332. <span class="ot">$</span>.<span class="fu">getJSON</span>(<span class="st">&#39;/athome/&#39;</span>, <span class="kw">function</span>(json) {
  2333. <span class="kw">var</span> items = [];
  2334. <span class="ot">dataLength</span>.<span class="fu">push</span>(<span class="ot">json</span>.<span class="fu">length</span>);
  2335. <span class="ot">$</span>.<span class="fu">each</span>(json, <span class="kw">function</span>(key, val) {
  2336. <span class="ot">zero</span>.<span class="fu">push</span>(<span class="ot">val</span>.<span class="fu">temperature</span>);
  2337. });
  2338. chart = <span class="kw">new</span> <span class="ot">Highcharts</span>.<span class="fu">Chart</span>({
  2339. <span class="dt">color</span>: {
  2340. <span class="dt">linearGradient</span>: {
  2341. <span class="dt">x1</span>: <span class="dv">0</span>,
  2342. <span class="dt">x2</span>: <span class="dv">0</span>,
  2343. <span class="dt">y1</span>: <span class="dv">0</span>,
  2344. <span class="dt">y1</span>: <span class="dv">1</span>
  2345. },
  2346. <span class="dt">stops</span>: [
  2347. [<span class="dv">0</span>, <span class="st">&#39;#003399&#39;</span>],
  2348. [<span class="dv">1</span>, <span class="st">&#39;#3366AA&#39;</span>]
  2349. ]
  2350. },
  2351. <span class="dt">chart</span>: {
  2352. <span class="dt">renderTo</span>: <span class="st">&#39;Tchart&#39;</span>,
  2353. <span class="dt">type</span>: <span class="st">&#39;spline&#39;</span>
  2354. },
  2355. <span class="dt">title</span>: {
  2356. <span class="dt">text</span>: <span class="st">&#39;本月温度情况&#39;</span>
  2357. },
  2358. <span class="dt">subtitle</span>: {
  2359. <span class="dt">text</span>: <span class="st">&#39;&#39;</span>
  2360. },
  2361. <span class="dt">xAxis</span>: {
  2362. <span class="dt">categories</span>: [],
  2363. <span class="dt">title</span>: {
  2364. <span class="dt">text</span>: <span class="st">&#39;&#39;</span>
  2365. }
  2366. },
  2367. <span class="dt">yAxis</span>: {
  2368. <span class="dt">title</span>: {
  2369. <span class="dt">text</span>: <span class="st">&#39;温度 (°C)&#39;</span>
  2370. }
  2371. },
  2372. <span class="dt">tooltip</span>: {
  2373. <span class="dt">backgroundColor</span>: <span class="st">&#39;#FCFFC5&#39;</span>,
  2374. <span class="dt">borderColor</span>: <span class="st">&#39;black&#39;</span>,
  2375. <span class="dt">borderRadius</span>: <span class="dv">10</span>,
  2376. <span class="dt">borderWidth</span>: <span class="dv">1</span>,
  2377. <span class="dt">enabled</span>: <span class="kw">true</span>,
  2378. <span class="dt">formatter</span>: <span class="kw">function</span>() {
  2379. <span class="kw">return</span> <span class="st">&#39;&lt;b&gt;&#39;</span> + <span class="kw">this</span>.<span class="ot">series</span>.<span class="fu">name</span> + <span class="st">&#39;&lt;/b&gt;&lt;br/&gt;&#39;</span> + <span class="kw">this</span>.<span class="fu">x</span> + <span class="st">&#39;: &#39;</span> + <span class="kw">this</span>.<span class="fu">y</span> + <span class="st">&#39;°C&#39;</span>;
  2380. }
  2381. },
  2382. <span class="dt">legend</span>: {
  2383. <span class="dt">layout</span>: <span class="st">&#39;vertical&#39;</span>,
  2384. <span class="dt">align</span>: <span class="st">&#39;right&#39;</span>,
  2385. <span class="dt">verticalAlign</span>: <span class="st">&#39;top&#39;</span>,
  2386. <span class="dt">x</span>: -<span class="dv">10</span>,
  2387. <span class="dt">y</span>: <span class="dv">100</span>,
  2388. <span class="dt">borderWidth</span>: <span class="dv">0</span>
  2389. },
  2390. <span class="dt">plotOptions</span>: {
  2391. <span class="dt">line</span>: {
  2392. <span class="dt">dataLabels</span>: {
  2393. <span class="dt">enabled</span>: <span class="kw">true</span>
  2394. },
  2395. <span class="dt">enableMouseTracking</span>: <span class="kw">false</span>
  2396. }
  2397. },
  2398. <span class="dt">series</span>: [{
  2399. <span class="dt">name</span>: <span class="st">&#39;本月&#39;</span>,
  2400. <span class="dt">data</span>: zero
  2401. }, {
  2402. <span class="dt">name</span>: <span class="st">&#39;对比&#39;</span>,
  2403. <span class="dt">data</span>: [<span class="fl">26.0</span>]
  2404. }]
  2405. });
  2406. });
  2407. };
  2408. <span class="kw">function</span> <span class="fu">showTemper</span>() {
  2409. <span class="kw">var</span> length = dataLength[<span class="dv">0</span>];
  2410. <span class="ot">$</span>.<span class="fu">ajax</span>({
  2411. <span class="dt">url</span>: <span class="st">&#39;/athome/&#39;</span> + length,
  2412. <span class="dt">type</span>: <span class="st">&#39;GET&#39;</span>,
  2413. <span class="dt">dataType</span>: <span class="st">&#39;json&#39;</span>,
  2414. <span class="dt">async</span>: <span class="kw">true</span>,
  2415. <span class="dt">timeout</span>: <span class="dv">1000</span>,
  2416. <span class="dt">error</span>: <span class="kw">function</span>() {},
  2417. <span class="dt">success</span>: <span class="kw">function</span>(sdata) {
  2418. <span class="fu">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="fu">empty</span>();
  2419. <span class="fu">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="fu">append</span>(<span class="ot">sdata</span>.<span class="fu">temperature</span>);
  2420. }
  2421. });
  2422. };
  2423. <span class="fu">$</span>(document).<span class="fu">ready</span>(<span class="kw">function</span>() {
  2424. <span class="fu">setInterval</span>(<span class="st">&quot;drawTemp();&quot;</span>, <span class="dv">5000</span>);
  2425. <span class="fu">setInterval</span>(<span class="st">&quot;showTemper();&quot;</span>, <span class="dv">800</span>);
  2426. <span class="fu">drawTemp</span>();
  2427. <span class="fu">showTemper</span>();
  2428. });</code></pre>
  2429. <p>我们先把<code>HighChart</code>部分的代码删去,就变成下面的内容:</p>
  2430. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength = [];
  2431. <span class="kw">function</span> <span class="fu">drawTemp</span>() {
  2432. <span class="kw">var</span> zero = [];
  2433. <span class="ot">$</span>.<span class="fu">getJSON</span>(<span class="st">&#39;/athome/&#39;</span>, <span class="kw">function</span>(json) {
  2434. <span class="kw">var</span> items = [];
  2435. <span class="ot">dataLength</span>.<span class="fu">push</span>(<span class="ot">json</span>.<span class="fu">length</span>);
  2436. <span class="ot">$</span>.<span class="fu">each</span>(json, <span class="kw">function</span>(key, val) {
  2437. <span class="ot">zero</span>.<span class="fu">push</span>(<span class="ot">val</span>.<span class="fu">temperature</span>);
  2438. });
  2439. });
  2440. };
  2441. <span class="kw">function</span> <span class="fu">showTemper</span>() {
  2442. <span class="kw">var</span> length = dataLength[<span class="dv">0</span>];
  2443. <span class="ot">$</span>.<span class="fu">ajax</span>({
  2444. <span class="dt">url</span>: <span class="st">&#39;/athome/&#39;</span> + length,
  2445. <span class="dt">type</span>: <span class="st">&#39;GET&#39;</span>,
  2446. <span class="dt">dataType</span>: <span class="st">&#39;json&#39;</span>,
  2447. <span class="dt">async</span>: <span class="kw">true</span>,
  2448. <span class="dt">timeout</span>: <span class="dv">1000</span>,
  2449. <span class="dt">error</span>: <span class="kw">function</span>() {},
  2450. <span class="dt">success</span>: <span class="kw">function</span>(sdata) {
  2451. <span class="fu">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="fu">empty</span>();
  2452. <span class="fu">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="fu">append</span>(<span class="ot">sdata</span>.<span class="fu">temperature</span>);
  2453. }
  2454. });
  2455. };
  2456. <span class="fu">$</span>(document).<span class="fu">ready</span>(<span class="kw">function</span>() {
  2457. <span class="fu">setInterval</span>(<span class="st">&quot;drawTemp();&quot;</span>, <span class="dv">5000</span>);
  2458. <span class="fu">setInterval</span>(<span class="st">&quot;showTemper();&quot;</span>, <span class="dv">800</span>);
  2459. <span class="fu">drawTemp</span>();
  2460. <span class="fu">showTemper</span>();
  2461. });</code></pre>
  2462. <p>代码看上去是由两部分组成的<code>drawTemperature</code>和<code>showTemperature</code></p>
  2463. <h3 id="实时数据"><span class="header-section-number">17.3.2</span> 实时数据</h3>
  2464. <p>剥离后的Ajax部分代码如下所示,主要用的是jQuery框架的getJSON来实现的</p>
  2465. <pre><code>var dataLength = [];
  2466. function drawTemp() {
  2467. var zero = [];
  2468. $.getJSON(&#39;/athome/&#39;, function(json) {
  2469. var items = [];
  2470. dataLength.push(json.length);
  2471. $.each(json, function(key, val) {
  2472. zero.push(val.temperature);
  2473. });
  2474. };</code></pre>
  2475. <p>实际上,我们做的只是从/athome/下面获取数据,再将数据堆到数组里面,再把这部分放到图形中。等等,什么是Ajax?</p>
  2476. <p>AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。 AJAX 不是新的编程语言,而是一种使用现有标准的新方法。 AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。 JSON我们前面也已经了解过了,看看getJSON吧。</p>
  2477. <p>jQuery. getJSON</p>
  2478. <p>方法定义:jQuery.getJSON( url, data, callback )</p>
  2479. <p>通过get请求得到json数据</p>
  2480. <p>·url用于提供json数据的地址页 ·data(Optional)用于传送到服务器的键值对 ·callback(Optional)回调函数,json数据请求成功后的处理函数 我想你似乎应该懂得了一点,就是在不刷新网页的同时,用javascript获取数据放到图表上,就这么简单。</p>
  2481. <h1 id="restful的coap协议"><span class="header-section-number">18</span> RESTful的CoAP协议</h1>
  2482. <h2 id="coap-嵌入式系统的rest"><span class="header-section-number">18.1</span> CoAP: 嵌入式系统的REST</h2>
  2483. <p>引自维基百科上的介绍,用的是谷歌翻译。。。</p>
  2484. <blockquote>
  2485. <p>受约束的应用协议(COAP)是一种软件协议旨在以非常简单的电子设备,使他们能够在互联网上进行交互式通信中使用。它特别针对小型低功率传感器,开关,阀门和需要被控制或监督远程,通过标准的Internet网络类似的组件。 COAP是一个应用层协议,该协议是用于在资源受限的网络连接设备,例如无线传感器网络节点使用。 COAP被设计为容易地转换为HTTP与Web简化集成,同时也能满足特殊的要求,例如多播支持,非常低的开销,和简单性。多播,低开销,以及简单性是因特网极其重要物联网(IOT)和机器对机器(M2M)设备,这往往是积重难返,有太多的内存和电源,比传统的互联网设备有。因此,效率是非常重要的。 COAP可以在支持UDP或UDP的模拟大多数设备上运行。</p>
  2486. </blockquote>
  2487. <p>简单地来说,CoAP是简化了HTTP协议的RESTful API,因而也只提供了REST的四个方法,即PUT,GET,POST和DELETE。对于微小的资源受限,在资源受限的通信的IP的网络,HTTP不是一种可行的选择。它占用了太多的资源和太多的带宽。而对于物联网这种嵌入式设备来说,关于资源与带宽,是我们需要优先考虑的内容。</p>
  2488. <ul>
  2489. <li>CoAP采用了二进制报头,而不是文本报头(text header)</li>
  2490. <li>CoAP降低了头的可用选项的数量。</li>
  2491. <li>CoAP减少了一些HTTP的方法</li>
  2492. <li>CoAP可以支持检测装置</li>
  2493. </ul>
  2494. <h2 id="coap-命令行工具"><span class="header-section-number">18.2</span> CoAP 命令行工具</h2>
  2495. <p>为了测试测试我们的代码是否是正确工作,我们需要一个CoAP的命令行工具。目前有两个不错的工具可以使用。</p>
  2496. <ul>
  2497. <li>CoAP-cli,一个基于NodeJS的CoAP命令行工具,其核心是基于Node-CoAP库。</li>
  2498. <li>libcooap,一个用C写的CoAP命令行工具。</li>
  2499. <li>FireFox Copper, 一个Firefox的插件。</li>
  2500. </ul>
  2501. <h3 id="node-coap-cli"><span class="header-section-number">18.2.1</span> Node CoAP CLI</h3>
  2502. <p>安装命令如下</p>
  2503. <pre class="sourceCode bash"><code class="sourceCode bash"> <span class="kw">npm</span> install coap-cli -g </code></pre>
  2504. <h4 id="coap命令行"><span class="header-section-number">18.2.1.1</span> CoAP命令行</h4>
  2505. <p>在coap-cli中,一共有四个方法。分别表示REST的四种不同的方式:</p>
  2506. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Commands</span>:
  2507. <span class="kw">get</span> performs a GET request
  2508. <span class="kw">put</span> performs a PUT request
  2509. <span class="kw">post</span> performs a POST request
  2510. <span class="kw">delete</span> performs a DELETE request</code></pre>
  2511. <p>在这里,我们用<a href="coap://vs0.inf.ethz.ch/" class="uri">coap://vs0.inf.ethz.ch/</a>来作一个简单的测试</p>
  2512. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> get coap://vs0.inf.ethz.ch/
  2513. <span class="kw">(2.05)</span> <span class="kw">************************************************************</span>
  2514. <span class="kw">I-D</span></code></pre>
  2515. <p>测试一下现在的最小的物联网系统CoAP版</p>
  2516. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> get coap://iot-coap.phodal.com/id/1
  2517. <span class="kw">(2.05)</span> [{<span class="st">&quot;id&quot;</span>:<span class="kw">1</span>,<span class="st">&quot;value&quot;</span>:<span class="st">&quot;is id 1&quot;</span>,<span class="st">&quot;sensors1&quot;</span>:19,<span class="st">&quot;sensors2&quot;</span>:20}]</code></pre>
  2518. <h3 id="libcoap"><span class="header-section-number">18.2.2</span> libcoap</h3>
  2519. <h4 id="mac-os-libcoap安装"><span class="header-section-number">18.2.2.1</span> mac os libcoap安装</h4>
  2520. <p>Mac OS下可以直接用</p>
  2521. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install libcoap</code></pre>
  2522. <h4 id="ubuntu-libcoap安装"><span class="header-section-number">18.2.2.2</span> Ubuntu libcoap安装</h4>
  2523. <p>Ubuntu GNU/Linux下</p>
  2524. <h4 id="windows-libcoap安装"><span class="header-section-number">18.2.2.3</span> Windows libcoap安装</h4>
  2525. <p>Windows 下</p>
  2526. <p>安装完libcoap,我们可以直接用自带的两个命令</p>
  2527. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-client</span>
  2528. <span class="kw">coap-server</span></code></pre>
  2529. <p>1.用coap-server启一个CoAP服务</p>
  2530. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-server</span></code></pre>
  2531. <p>2.客户端获取数据</p>
  2532. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-client</span> -m get coap://localhost</code></pre>
  2533. <p>返回结果</p>
  2534. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">v</span>:1 t:0 tkl:0 c:1 id:37109
  2535. <span class="kw">This</span> is a test server made with libcoap (see http://libcoap.sf.net)
  2536. <span class="kw">Copyright</span> (C) <span class="kw">2010--2013</span> Olaf Bergmann <span class="kw">&lt;</span>bergmann@tzi.org<span class="kw">&gt;</span></code></pre>
  2537. <h3 id="firefox-copper"><span class="header-section-number">18.2.3</span> Firefox Copper</h3>
  2538. <p>为了能访问<a href="coap://localhost/" class="uri">coap://localhost/</a>,于是我们便需要安装一个Firefox并安装一个名为Copper的插件。</p>
  2539. <ol>
  2540. <li><p>下载地址: <a href="https://addons.mozilla.org/en-US/firefox/addon/copper-270430/" class="uri">https://addons.mozilla.org/en-US/firefox/addon/copper-270430/</a></p></li>
  2541. <li><p>作为测试我们同样可以访问 <a href="coap://vs0.inf.ethz.ch:5683/" class="uri">coap://vs0.inf.ethz.ch:5683/</a></p></li>
  2542. </ol>
  2543. <h2 id="coap-helloworld"><span class="header-section-number">18.3</span> CoAP Hello,World</h2>
  2544. <p>接着我们便开始试试做一个简单的CoAP协议的应用:</p>
  2545. <p>这里用到的是一个Nodejs的扩展Node-CoAP</p>
  2546. <blockquote>
  2547. <p>node-coap is a client and server library for CoAP modelled after the http module.</p>
  2548. </blockquote>
  2549. <p>Node-CoAP是一个客户端和服务端的库用于CoAP的模块建模。创建一个package.json文件,添加这个库</p>
  2550. <pre class="sourceCode javascript"><code class="sourceCode javascript">{
  2551. <span class="st">&quot;dependencies&quot;</span>:{
  2552. <span class="st">&quot;coap&quot;</span>: <span class="st">&quot;0.7.2&quot;</span>
  2553. }
  2554. }</code></pre>
  2555. <p>接着执行</p>
  2556. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install</code></pre>
  2557. <p>就可以安装好这个库。如果遇到权限问题,请用</p>
  2558. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> npm install</code></pre>
  2559. <p>接着,创建这样一个app.js</p>
  2560. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">&#39;coap&#39;</span>)
  2561. , server = <span class="ot">coap</span>.<span class="fu">createServer</span>()
  2562. <span class="ot">server</span>.<span class="fu">on</span>(<span class="st">&#39;request&#39;</span>, <span class="kw">function</span>(req, res) {
  2563. <span class="ot">res</span>.<span class="fu">end</span>(<span class="st">&#39;Hello &#39;</span> + <span class="ot">req</span>.<span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] + <span class="st">&#39;</span><span class="ch">\n</span><span class="st">&#39;</span>)
  2564. })
  2565. <span class="ot">server</span>.<span class="fu">listen</span>(<span class="kw">function</span>() {
  2566. <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">&#39;server started&#39;</span>)
  2567. }) </code></pre>
  2568. <p>执行</p>
  2569. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> app.js</code></pre>
  2570. <p>便可以在浏览器上访问了,因为现在什么也没有,所以什么也不会返回。</p>
  2571. <p>接着下来再创建一个client端的js,并运行之</p>
  2572. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">&#39;coap&#39;</span>)
  2573. , req = <span class="ot">coap</span>.<span class="fu">request</span>(<span class="st">&#39;coap://localhost/World&#39;</span>)
  2574. <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">&#39;response&#39;</span>, <span class="kw">function</span>(res) {
  2575. <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="ot">process</span>.<span class="fu">stdout</span>)
  2576. })
  2577. <span class="ot">req</span>.<span class="fu">end</span>()</code></pre>
  2578. <p>就可以在console上输出</p>
  2579. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Hello</span> World </code></pre>
  2580. <p>也就达到了我们的目的,用CoAP协议创建一个服务,接着我们应该用它创建更多的东西,如产生JSON数据,以及RESTful。和HTTP版的最小物联网系统一样,CoAP版的最小物联网系统也是要返回JSON的。</p>
  2581. <h2 id="coap-数据库查询"><span class="header-section-number">18.4</span> CoAP 数据库查询</h2>
  2582. <h3 id="node-module"><span class="header-section-number">18.4.1</span> Node Module</h3>
  2583. <p>这说里NodeJS Module的意义是因为我们需要在别的地方引用到db_helper这个库,也就是下一小节要的讲的内容。</p>
  2584. <p>这样我们就可以在server.js类似于这样去引用这个js库。</p>
  2585. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> DBHelper = <span class="fu">require</span>(<span class="st">&#39;./db_helper.js&#39;</span>);
  2586. <span class="ot">DBHelper</span>.<span class="fu">initDB</span>();</code></pre>
  2587. <p>而这样调用的前提是我们需要去声明这样的module,为了方便地导出函数功能调用。</p>
  2588. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">DBHelper</span>(){
  2589. }
  2590. <span class="ot">DBHelper</span>.<span class="fu">initDB</span> = <span class="kw">function</span>(){};
  2591. <span class="ot">module</span>.<span class="fu">exports</span> = DBHelper;</code></pre>
  2592. <h3 id="node-sqlite3"><span class="header-section-number">18.4.2</span> Node-Sqlite3</h3>
  2593. <p>这次我们用的是SQLite3(你可以用MySQL,出于安全考虑用SQLite3,SQLite3产生的是一个文件)。一个简单的initDB函数</p>
  2594. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(config[<span class="st">&quot;db_name&quot;</span>]);
  2595. <span class="kw">var</span> create_table = <span class="st">&#39;create table if not exists basic (&#39;</span> + config[<span class="st">&quot;db_table&quot;</span>] + <span class="st">&#39;);&#39;</span>;
  2596. <span class="ot">db</span>.<span class="fu">serialize</span>(<span class="kw">function</span>() {
  2597. <span class="ot">db</span>.<span class="fu">run</span>(create_table);
  2598. <span class="ot">_</span>.<span class="fu">each</span>(config[<span class="st">&quot;init_table&quot;</span>], <span class="kw">function</span>(insert_data) {
  2599. <span class="ot">db</span>.<span class="fu">run</span>(insert_data);
  2600. });
  2601. });
  2602. <span class="ot">db</span>.<span class="fu">close</span>();</code></pre>
  2603. <p>首先从配置中读取db_name,接着创建table,然后调用underscore的each方法,创建几个数据。配置如下所示</p>
  2604. <pre class="sourceCode javascript"><code class="sourceCode javascript">config = {
  2605. <span class="st">&quot;db_name&quot;</span>: <span class="st">&quot;iot.db&quot;</span>,
  2606. <span class="st">&quot;db_table&quot;</span>: <span class="st">&quot;id integer primary key, value text, sensors1 float, sensors2 float&quot;</span>,
  2607. <span class="st">&quot;init_table&quot;</span>:[
  2608. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, &#39;is id 1&#39;, 19, 20);&quot;</span>,
  2609. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, &#39;is id 2&#39;, 20, 21);&quot;</span>
  2610. ],
  2611. <span class="st">&quot;query_table&quot;</span>:<span class="st">&quot;select * from basic;&quot;</span>
  2612. };</code></pre>
  2613. <p>而之前所提到的url查询所做的事情便是</p>
  2614. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">DBHelper</span>.<span class="fu">urlQueryData</span> = <span class="kw">function</span> (url, callback) {
  2615. <span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(<span class="st">&quot;iot.db&quot;</span>);
  2616. <span class="kw">var</span> result = [];
  2617. <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] + <span class="st">&quot;=&quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>]);
  2618. <span class="ot">db</span>.<span class="fu">all</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] + <span class="st">&quot;=&quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>], <span class="kw">function</span>(err, rows) {
  2619. <span class="ot">db</span>.<span class="fu">close</span>();
  2620. <span class="fu">callback</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>(rows));
  2621. });
  2622. };</code></pre>
  2623. <p>将URL传进来,便解析这个参数,接着再放到数据库中查询,再回调回结果。这样我们就可以构成之前所说的查询功能,而我们所谓的post功能似乎也可以用同样的方法加进去。</p>
  2624. <h3 id="查询数据"><span class="header-section-number">18.4.3</span> 查询数据</h3>
  2625. <p>简单地记录一下在IoT-CoAP中一次获取数据地过程。</p>
  2626. <p>先看看在示例中的Get.js的代码,这关乎在后面server端的代码。</p>
  2627. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">&#39;coap&#39;</span>)
  2628. ,requestURI = <span class="st">&#39;coap://localhost/&#39;</span>
  2629. ,url = <span class="fu">require</span>(<span class="st">&#39;url&#39;</span>).<span class="fu">parse</span>(requestURI + <span class="st">&#39;id/1/&#39;</span>)
  2630. ,req = <span class="ot">coap</span>.<span class="fu">request</span>(url)
  2631. ,bl = <span class="fu">require</span>(<span class="st">&#39;bl&#39;</span>);
  2632. <span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">&quot;Accept&quot;</span>, <span class="st">&quot;application/json&quot;</span>);
  2633. <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">&#39;response&#39;</span>, <span class="kw">function</span>(res) {
  2634. <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="fu">bl</span>(<span class="kw">function</span>(err, data) {
  2635. <span class="kw">var</span> json = <span class="ot">JSON</span>.<span class="fu">parse</span>(data);
  2636. <span class="ot">console</span>.<span class="fu">log</span>(json);
  2637. }));
  2638. });
  2639. <span class="ot">req</span>.<span class="fu">end</span>();</code></pre>
  2640. <p>const定义数据的方法,和我们在其他语言中有点像。只是这的const主要是为了程序的健壮型,减少程序出错,当然这不是javascript的用法。</p>
  2641. <p>我们构建了一个请求的URL</p>
  2642. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span>://localhost/id/1/</code></pre>
  2643. <p>我们对我们的请求添加了一个Header,内容是Accept,值是’application/json’也就是JSON格式。接着,便是等待请求回来,再处理返回的内容。</p>
  2644. <p><strong>判断请求的方法</strong></p>
  2645. <p>在这里先把一些无关的代码删除掉,并保证其能工作,so,下面就是简要的逻辑代码。</p>
  2646. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> coap = <span class="fu">require</span>(<span class="st">&#39;coap&#39;</span>);
  2647. <span class="kw">var</span> server = <span class="ot">coap</span>.<span class="fu">createServer</span>({});
  2648. <span class="kw">var</span> request_handler = <span class="fu">require</span>(<span class="st">&#39;./request_handler.js&#39;</span>);
  2649. <span class="ot">server</span>.<span class="fu">on</span>(<span class="st">&#39;request&#39;</span>, <span class="kw">function</span>(req, res) {
  2650. <span class="kw">switch</span>(<span class="ot">req</span>.<span class="fu">method</span>){
  2651. <span class="kw">case</span> <span class="st">&quot;GET&quot;</span>: <span class="ot">request_handler</span>.<span class="fu">getHandler</span>(req, res);
  2652. <span class="kw">break</span>;
  2653. }
  2654. });
  2655. <span class="ot">server</span>.<span class="fu">listen</span>(<span class="kw">function</span>() {
  2656. <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">&#39;server started&#39;</span>);
  2657. });</code></pre>
  2658. <p>创建一个CoAP服务,判断req.method,也就是请求的方法,如果是GET的话,就调用request_handler.getHandler(req, res)。而在getHandler里,判断了下请求的Accept</p>
  2659. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">request_helper</span>.<span class="fu">getHandler</span> = <span class="kw">function</span>(req, res) {
  2660. <span class="kw">switch</span> (<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">&#39;Accept&#39;</span>]) {
  2661. <span class="kw">case</span> <span class="st">&quot;application/json&quot;</span>:
  2662. <span class="ot">qh</span>.<span class="fu">returnJSON</span>(req, res);
  2663. <span class="kw">break</span>;
  2664. <span class="kw">case</span> <span class="st">&quot;application/xml&quot;</span>:
  2665. <span class="ot">qh</span>.<span class="fu">returnXML</span>(req, res);
  2666. <span class="kw">break</span>;
  2667. }
  2668. };</code></pre>
  2669. <p>如果是json刚调用returnJSON,</p>
  2670. <p><strong>Database与回调</strong></p>
  2671. <p>而这里为了处理回调函数刚分为了两部分</p>
  2672. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">query_helper</span>.<span class="fu">returnJSON</span> = <span class="kw">function</span>(req, res) {
  2673. <span class="ot">DBHelper</span>.<span class="fu">urlQueryData</span>(<span class="ot">req</span>.<span class="fu">url</span>, <span class="kw">function</span> (result) {
  2674. <span class="ot">QueryData</span>.<span class="fu">returnJSON</span>(result, res);
  2675. });
  2676. };</code></pre>
  2677. <p>而这里只是调用了</p>
  2678. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">DBHelper</span>.<span class="fu">urlQueryData</span> = <span class="kw">function</span> (url, callback) {
  2679. <span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(config[<span class="st">&quot;db_name&quot;</span>]);
  2680. <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] + <span class="st">&quot;=&quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>]);
  2681. <span class="ot">db</span>.<span class="fu">all</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] + <span class="st">&quot;=&quot;</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>], <span class="kw">function</span>(err, rows) {
  2682. <span class="ot">db</span>.<span class="fu">close</span>();
  2683. <span class="fu">callback</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>(rows));
  2684. });
  2685. };</code></pre>
  2686. <p>这里调用了node sqlite3去查询对应id的数据,用回调处理了数据无法到外部的问题,而上面的returnJSON则只是返回最后的结果,code以及其他的内容。</p>
  2687. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">QueryData</span>.<span class="fu">returnJSON</span> = <span class="kw">function</span>(result, res) {
  2688. <span class="kw">if</span> (<span class="ot">result</span>.<span class="fu">length</span> == <span class="dv">2</span>) {
  2689. <span class="ot">res</span>.<span class="fu">code</span> = <span class="st">&#39;4.04&#39;</span>;
  2690. <span class="ot">res</span>.<span class="fu">end</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>({
  2691. <span class="dt">error</span>: <span class="st">&quot;Not Found&quot;</span>
  2692. }));
  2693. } <span class="kw">else</span> {
  2694. <span class="ot">res</span>.<span class="fu">code</span> = <span class="st">&#39;2.05&#39;</span>;
  2695. <span class="ot">res</span>.<span class="fu">end</span>(result);
  2696. }
  2697. };</code></pre>
  2698. <p>当resulst的结果为空时,返回一个404,因为没有数据。这样我们就构成了整个的链,再一步步返回结果。</p>
  2699. <p>在<a href="https://github.com/phodal/iot-coap">IoT-CoAP</a>中我们使用到了一个Block2的东西,于是便整理相关的一些资料,作一个简单的介绍,以及在代码中的使用。</p>
  2700. <h2 id="coap-block"><span class="header-section-number">18.5</span> CoAP Block</h2>
  2701. <p>CoAP是一个RESTful传输协议用于受限设备的节点和网络。基本的CoAP消息是一个不错的选择对于小型载荷如</p>
  2702. <ul>
  2703. <li>温度传感器</li>
  2704. <li>灯光开关</li>
  2705. <li>楼宇自动化设备</li>
  2706. </ul>
  2707. <p>然而,有时我们的应用需要传输更大的有效载荷,如——更新固件。与HTTP,TCP做繁重工作将大型有效载荷分成多个数据包,并确保他们所有到达并以正确的顺序被处理。</p>
  2708. <p>CoAP是同UDP与DLTS一样是基于数据报传输的,这限制了资源表示(resource representation)的最大大小,使得传输不需要太多的分割。虽然UDP支持通过IP分片传输更大的有效载荷,且仅限于64KiB,更重要的是,并没有真正很好地约束应用和网络。</p>
  2709. <p>而不是依赖于IP分片,这种规范基本COAP了对“块”选项,用于传输信息从多个资源区块的请求 - 响应对。在许多重要的情况下,阻止使服务器能够真正无状态:服务器可以处理每块分开传输,而无需建立连接以前的数据块传输的其他服务器端内存。</p>
  2710. <p>综上所述,块(Block)选项提供了传送一个最小的在分块的方式更大的陈述。</p>
  2711. <h3 id="coap-post"><span class="header-section-number">18.5.1</span> CoAP POST</h3>
  2712. <p>看看在IoT CoAP中的post示例。</p>
  2713. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">&#39;coap&#39;</span>)
  2714. ,request = <span class="ot">coap</span>.<span class="fu">request</span>
  2715. ,bl = <span class="fu">require</span>(<span class="st">&#39;bl&#39;</span>)
  2716. ,req = <span class="fu">request</span>({<span class="dt">hostname</span>: <span class="st">&#39;localhost&#39;</span>,<span class="dt">port</span>:<span class="dv">5683</span>,<span class="dt">pathname</span>: <span class="st">&#39;&#39;</span>,<span class="dt">method</span>: <span class="st">&#39;POST&#39;</span>});
  2717. <span class="ot">req</span>.<span class="fu">setOption</span>(<span class="st">&#39;Block2&#39;</span>, [<span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">&#39;1&#39;</span>),<span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">&quot;&#39;must&#39;&quot;</span>), <span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">&#39;23&#39;</span>), <span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">&#39;12&#39;</span>)]);
  2718. <span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">&quot;Accept&quot;</span>, <span class="st">&quot;application/json&quot;</span>);
  2719. <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">&#39;response&#39;</span>, <span class="kw">function</span>(res) {
  2720. <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="fu">bl</span>(<span class="kw">function</span>(err, data) {
  2721. <span class="ot">console</span>.<span class="fu">log</span>(data);
  2722. <span class="ot">process</span>.<span class="fu">exit</span>(<span class="dv">0</span>);
  2723. }));
  2724. });
  2725. <span class="ot">req</span>.<span class="fu">end</span>();</code></pre>
  2726. <p>Block2中一共有四个数据,相应的数据结果应该是</p>
  2727. <pre class="sourceCode javascript"><code class="sourceCode javascript">{ <span class="dt">name</span>: <span class="st">&#39;Block2&#39;</span>, <span class="dt">value</span>: &lt;Buffer <span class="dv">31</span>&gt; }
  2728. { <span class="dt">name</span>: <span class="st">&#39;Block2&#39;</span>, <span class="dt">value</span>: &lt;Buffer <span class="dv">27</span> 6d <span class="dv">75</span> <span class="dv">73</span> <span class="dv">74</span> <span class="dv">27</span>&gt; }
  2729. { <span class="dt">name</span>: <span class="st">&#39;Block2&#39;</span>, <span class="dt">value</span>: &lt;Buffer <span class="dv">32</span> <span class="dv">33</span>&gt; }
  2730. { <span class="dt">name</span>: <span class="st">&#39;Block2&#39;</span>, <span class="dt">value</span>: &lt;Buffer <span class="dv">31</span> <span class="dv">32</span>&gt; }</code></pre>
  2731. <p>这是没有解析的Block2,简单地可以用</p>
  2732. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">_</span>.<span class="fu">values</span>(e).<span class="fu">toString</span>()</code></pre>
  2733. <p>将结果转换为</p>
  2734. <p>Block2,1 Block2,‘must’ Block2,23 Block2,12</p>
  2735. <p>接着按“,”分开,</p>
  2736. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">_</span>.<span class="fu">values</span>(e).<span class="fu">toString</span>().<span class="fu">split</span>(<span class="st">&#39;,&#39;</span>)[<span class="dv">1</span>]</code></pre>
  2737. <p>就有</p>
  2738. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">[</span> <span class="st">&#39;1&#39;</span>, <span class="st">&#39;\&#39;</span>must<span class="dt">\&#39;</span><span class="st">&#39;, &#39;</span>23<span class="st">&#39;, &#39;</span>12<span class="st">&#39; ]</span></code></pre>
  2739. <p>便可以很愉快地将其post到数据库中了,</p>
  2740. <p>在做IoT-CoAP的过程中只支持JSON,查阅CoAP的草稿时发现支持了诸多的Content Types。</p>
  2741. <h3 id="coap-content-types"><span class="header-section-number">18.5.2</span> CoAP Content Types</h3>
  2742. <p>以下文字来自谷歌翻译:</p>
  2743. <blockquote>
  2744. <p>互联网媒体类型是通过HTTP字符串标识,如“application/xml”。该字符串是由一个顶层的类型“applicaion”和子类型的“XML”。为了尽量减少使用这些类型的媒体类型来表示的开销消息有效载荷,COAP定义一个标识符编码方案互联网媒体类型的子集。预计这桌将可扩展标识符的值的IANA维护。内容类型选项被格式化为一个8位无符号整数。初始映射到一个合适的互联网媒体类型标识符表所示。复合型高层次类型(multipart和不支持消息)。标识符值是从201-255保留的特定于供应商的,应用程序特定的或实验使用和不由IANA。</p>
  2745. </blockquote>
  2746. <p>下面是HTTP的标识符及类型</p>
  2747. <table>
  2748. <thead>
  2749. <tr class="header">
  2750. <th align="left">Internet media type</th>
  2751. <th align="left">Identifier</th>
  2752. </tr>
  2753. </thead>
  2754. <tbody>
  2755. <tr class="odd">
  2756. <td align="left">text/plain (UTF-8)</td>
  2757. <td align="left">0</td>
  2758. </tr>
  2759. <tr class="even">
  2760. <td align="left">text/xml (UTF-8)</td>
  2761. <td align="left">1</td>
  2762. </tr>
  2763. <tr class="odd">
  2764. <td align="left">text/csv (UTF-8)</td>
  2765. <td align="left">2</td>
  2766. </tr>
  2767. <tr class="even">
  2768. <td align="left">text/html (UTF-8)</td>
  2769. <td align="left">3</td>
  2770. </tr>
  2771. <tr class="odd">
  2772. <td align="left">image/gif</td>
  2773. <td align="left">21</td>
  2774. </tr>
  2775. <tr class="even">
  2776. <td align="left">image/jpeg</td>
  2777. <td align="left">22</td>
  2778. </tr>
  2779. <tr class="odd">
  2780. <td align="left">image/png</td>
  2781. <td align="left">23</td>
  2782. </tr>
  2783. <tr class="even">
  2784. <td align="left">image/tiff</td>
  2785. <td align="left">24</td>
  2786. </tr>
  2787. <tr class="odd">
  2788. <td align="left">audio/raw</td>
  2789. <td align="left">25</td>
  2790. </tr>
  2791. <tr class="even">
  2792. <td align="left">video/raw</td>
  2793. <td align="left">26</td>
  2794. </tr>
  2795. <tr class="odd">
  2796. <td align="left">application/link-format [I-D.ietf-core-link-format]</td>
  2797. <td align="left">40</td>
  2798. </tr>
  2799. <tr class="even">
  2800. <td align="left">application/xml</td>
  2801. <td align="left">41</td>
  2802. </tr>
  2803. <tr class="odd">
  2804. <td align="left">application/octet-stream</td>
  2805. <td align="left">42</td>
  2806. </tr>
  2807. <tr class="even">
  2808. <td align="left">application/rdf+xml</td>
  2809. <td align="left">43</td>
  2810. </tr>
  2811. <tr class="odd">
  2812. <td align="left">application/soap+xml</td>
  2813. <td align="left">44</td>
  2814. </tr>
  2815. <tr class="even">
  2816. <td align="left">application/atom+xml</td>
  2817. <td align="left">45</td>
  2818. </tr>
  2819. <tr class="odd">
  2820. <td align="left">application/xmpp+xml</td>
  2821. <td align="left">46</td>
  2822. </tr>
  2823. <tr class="even">
  2824. <td align="left">application/exi</td>
  2825. <td align="left">47</td>
  2826. </tr>
  2827. <tr class="odd">
  2828. <td align="left">application/x-bxml</td>
  2829. <td align="left">48</td>
  2830. </tr>
  2831. <tr class="even">
  2832. <td align="left">application/fastinfoset</td>
  2833. <td align="left">49</td>
  2834. </tr>
  2835. <tr class="odd">
  2836. <td align="left">application/soap+fastinfoset</td>
  2837. <td align="left">50</td>
  2838. </tr>
  2839. <tr class="even">
  2840. <td align="left">application/json</td>
  2841. <td align="left">51</td>
  2842. </tr>
  2843. </tbody>
  2844. </table>
  2845. <p>而在CoAP中只有简单地几个</p>
  2846. <table>
  2847. <colgroup>
  2848. <col width="25%" />
  2849. <col width="25%" />
  2850. <col width="25%" />
  2851. <col width="25%" />
  2852. </colgroup>
  2853. <thead>
  2854. <tr class="header">
  2855. <th align="left">Media type</th>
  2856. <th align="left">Encoding</th>
  2857. <th align="left">Id.</th>
  2858. <th align="left">Reference</th>
  2859. </tr>
  2860. </thead>
  2861. <tbody>
  2862. <tr class="odd">
  2863. <td align="left">text/plain;</td>
  2864. <td align="left">-</td>
  2865. <td align="left">0</td>
  2866. <td align="left">[RFC2046][RFC3676][RFC5147]</td>
  2867. </tr>
  2868. <tr class="even">
  2869. <td align="left">charset=utf-8</td>
  2870. <td align="left"></td>
  2871. <td align="left"></td>
  2872. <td align="left"></td>
  2873. </tr>
  2874. <tr class="odd">
  2875. <td align="left">application/</td>
  2876. <td align="left">-</td>
  2877. <td align="left">40</td>
  2878. <td align="left">[RFC6690]</td>
  2879. </tr>
  2880. <tr class="even">
  2881. <td align="left">link-format</td>
  2882. <td align="left"></td>
  2883. <td align="left"></td>
  2884. <td align="left"></td>
  2885. </tr>
  2886. <tr class="odd">
  2887. <td align="left">application/xml</td>
  2888. <td align="left">-</td>
  2889. <td align="left">41</td>
  2890. <td align="left">[RFC3023]</td>
  2891. </tr>
  2892. <tr class="even">
  2893. <td align="left">application/</td>
  2894. <td align="left">-</td>
  2895. <td align="left">42</td>
  2896. <td align="left">[RFC2045][RFC2046]</td>
  2897. </tr>
  2898. <tr class="odd">
  2899. <td align="left">octet-stream</td>
  2900. <td align="left"></td>
  2901. <td align="left"></td>
  2902. <td align="left"></td>
  2903. </tr>
  2904. <tr class="even">
  2905. <td align="left">application/exi</td>
  2906. <td align="left">-</td>
  2907. <td align="left">47</td>
  2908. <td align="left">[EXIMIME]</td>
  2909. </tr>
  2910. <tr class="odd">
  2911. <td align="left">application/json</td>
  2912. <td align="left">-</td>
  2913. <td align="left">50</td>
  2914. <td align="left">[RFC4627]</td>
  2915. </tr>
  2916. </tbody>
  2917. </table>
  2918. <p>简单地说就是:</p>
  2919. <p><code>诸如application/json的Content Types在CoAP中应该是50</code>。如上表所示的结果是其对应的结果,这样的话可以减少传递的信息量。</p>
  2920. <h2 id="coap-json"><span class="header-section-number">18.6</span> CoAP JSON</h2>
  2921. <p>于是在一开始的时候首先支持的便是“application/json”这样的类型。</p>
  2922. <p>首先判断请求的header</p>
  2923. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">request_helper</span>.<span class="fu">getHandler</span> = <span class="kw">function</span>(req, res) {
  2924. <span class="kw">switch</span> (<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">&#39;Accept&#39;</span>]) {
  2925. <span class="kw">case</span> <span class="st">&quot;application/json&quot;</span>:
  2926. <span class="ot">qh</span>.<span class="fu">returnJSON</span>(req, res);
  2927. <span class="kw">break</span>;
  2928. <span class="kw">case</span> <span class="st">&quot;application/xml&quot;</span>:
  2929. <span class="ot">qh</span>.<span class="fu">returnXML</span>(req, res);
  2930. <span class="kw">break</span>;
  2931. }
  2932. };</code></pre>
  2933. <p>再转至相应的函数处理,而判断的依据则是Accept是不是“application/json”。</p>
  2934. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="fu">registerFormat</span>(<span class="st">&#39;text/plain&#39;</span>, <span class="dv">0</span>)
  2935. <span class="fu">registerFormat</span>(<span class="st">&#39;application/link-format&#39;</span>, <span class="dv">40</span>)
  2936. <span class="fu">registerFormat</span>(<span class="st">&#39;application/xml&#39;</span>, <span class="dv">41</span>)
  2937. <span class="fu">registerFormat</span>(<span class="st">&#39;application/octet-stream&#39;</span>, <span class="dv">42</span>)
  2938. <span class="fu">registerFormat</span>(<span class="st">&#39;application/exi&#39;</span>, <span class="dv">47</span>)
  2939. <span class="fu">registerFormat</span>(<span class="st">&#39;application/json&#39;</span>, <span class="dv">50</span>)</code></pre>
  2940. <p>对应地我们需要在一发出请求的时候设置好Accept,要不就没有办法返回我们需要的结果。</p>
  2941. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">&quot;Accept&quot;</span>, <span class="st">&quot;application/json&quot;</span>);</code></pre>
  2942. <p><strong>返回JSON</strong></p>
  2943. <p>在给IoT CoAP添加了JSON支持之后,变得非常有意思,至少我们可以获得我们想要的结果。在上一篇中我们介绍了一些常用的工具——<a href="http://www.phodal.com/blog/coap-command-line-tools-set/">CoAP 命令行工具集</a>。</p>
  2944. <p><strong>CoAP客户端代码</strong></p>
  2945. <p>开始之前我们需要有一个客户端代码,以便我们的服务端可以返回正确的数据并解析</p>
  2946. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> coap = <span class="fu">require</span>(<span class="st">&#39;coap&#39;</span>);
  2947. <span class="kw">var</span> requestURI = <span class="st">&#39;coap://localhost/&#39;</span>;
  2948. <span class="kw">var</span> url = <span class="fu">require</span>(<span class="st">&#39;url&#39;</span>).<span class="fu">parse</span>(requestURI + <span class="st">&#39;id/1/&#39;</span>);
  2949. <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">&quot;Request URL: &quot;</span> + <span class="ot">url</span>.<span class="fu">href</span>);
  2950. <span class="kw">var</span> req = <span class="ot">coap</span>.<span class="fu">request</span>(url);
  2951. <span class="kw">var</span> bl = <span class="fu">require</span>(<span class="st">&#39;bl&#39;</span>);
  2952. <span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">&quot;Accept&quot;</span>, <span class="st">&quot;application/json&quot;</span>);
  2953. <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">&#39;response&#39;</span>, <span class="kw">function</span>(res) {
  2954. <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="fu">bl</span>(<span class="kw">function</span>(err, data) {
  2955. <span class="kw">var</span> json = <span class="ot">JSON</span>.<span class="fu">parse</span>(data);
  2956. <span class="ot">console</span>.<span class="fu">log</span>(json);
  2957. }));
  2958. });
  2959. <span class="ot">req</span>.<span class="fu">end</span>();</code></pre>
  2960. <p>代码有点长内容也有点多,但是核心是这句话:</p>
  2961. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">&quot;Accept&quot;</span>, <span class="st">&quot;application/json&quot;</span>);</code></pre>
  2962. <p>这样的话,我们只需要在我们的服务端一判断,</p>
  2963. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">if</span>(<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">&#39;Accept&#39;</span>] == <span class="st">&#39;application/json&#39;</span>) {
  2964. <span class="co">//do something</span>
  2965. };</code></pre>
  2966. <p>这样就可以返回数据了</p>
  2967. <p><strong>CoAP Server端代码</strong></p>
  2968. <p>Server端的代码比较简单,判断一下</p>
  2969. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">if</span> (<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">&#39;Accept&#39;</span>] == <span class="st">&#39;application/json&#39;</span>) {
  2970. <span class="fu">parse_url</span>(<span class="ot">req</span>.<span class="fu">url</span>, <span class="kw">function</span>(result){
  2971. <span class="ot">res</span>.<span class="fu">end</span>(result);
  2972. });
  2973. <span class="ot">res</span>.<span class="fu">code</span> = <span class="st">&#39;2.05&#39;</span>;
  2974. }</code></pre>
  2975. <p>请求的是否是JSON格式,再返回一个205,也就是Content,只是这时设计是请求一个URL返回对应的数据。如</p>
  2976. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span>://localhost/id/1/</code></pre>
  2977. <p>这时应该请求的是ID为1的数据,即</p>
  2978. <pre class="sourceCode javascript"><code class="sourceCode javascript">[ { <span class="dt">id</span>: <span class="dv">1</span>, <span class="dt">value</span>: <span class="st">&#39;is id 1&#39;</span>, <span class="dt">sensors1</span>: <span class="dv">19</span>, <span class="dt">sensors2</span>: <span class="dv">20</span> }]</code></pre>
  2979. <p>而parse_url只是从数据库从读取相应的数据。</p>
  2980. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">parse_url</span>(url ,callback) {
  2981. <span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(config[<span class="st">&quot;db_name&quot;</span>]);
  2982. <span class="kw">var</span> result = [];
  2983. <span class="ot">db</span>.<span class="fu">all</span>(<span class="st">&quot;SELECT * FROM basic;&quot;</span>, <span class="kw">function</span>(err, rows) {
  2984. <span class="fu">callback</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>(rows));
  2985. })
  2986. }</code></pre>
  2987. <p>并且全部都显示出来,设计得真是有点不行,不过现在已经差不多了。</p>
  2988. <h2 id="使用iot-coap构建物联网"><span class="header-section-number">18.7</span> 使用IoT-CoAP构建物联网</h2>
  2989. <p>(<code>注意</code>:windows系统npm install失败时,需要自己建立一个C:and Settings[USERNAME]Data文件)</p>
  2990. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install iot-coap</code></pre>
  2991. <p>1.新建<strong>index.js</strong></p>
  2992. <p><code>注意</code>: 如果已经存在一个index.js文件,请将下面内容添加到文件末尾(create index.js, and add)</p>
  2993. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> iotcoap = <span class="fu">require</span>(<span class="st">&#39;iot-coap&#39;</span>);
  2994. <span class="ot">iotcoap</span>.<span class="fu">run</span>();
  2995. <span class="ot">iotcoap</span>.<span class="ot">rest</span>.<span class="fu">run</span>();</code></pre>
  2996. <p><code>注意</code>:在db配置可以选择mongodb和sqlite3,替换所需要的数据库即可。(you can choice db on iot.js with ‘sqlite’ or ‘mongodb’)</p>
  2997. <p>2.创建<strong>iot.js</strong></p>
  2998. <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">exports</span>.<span class="fu">config</span> = {
  2999. <span class="st">&quot;db_name&quot;</span>: <span class="st">&quot;iot.db&quot;</span>,
  3000. <span class="st">&quot;mongodb_name&quot;</span>: <span class="st">&quot;iot&quot;</span>,
  3001. <span class="st">&quot;mongodb_documents&quot;</span>: <span class="st">&quot;iot&quot;</span>,
  3002. <span class="st">&quot;db&quot;</span>: <span class="st">&quot;mongodb&quot;</span>,
  3003. <span class="st">&quot;table_name&quot;</span>: <span class="st">&quot;basic&quot;</span>,
  3004. <span class="st">&quot;keys&quot;</span>:[
  3005. <span class="st">&quot;id&quot;</span>,
  3006. <span class="st">&quot;value&quot;</span>,
  3007. <span class="st">&quot;sensors1&quot;</span>,
  3008. <span class="st">&quot;sensors2&quot;</span>
  3009. ],
  3010. <span class="st">&quot;db_table&quot;</span>: <span class="st">&quot;id integer primary key, value text, sensors1 float, sensors2 float&quot;</span>,
  3011. <span class="st">&quot;mongodb_init&quot;</span>:[
  3012. {
  3013. <span class="dt">id</span>: <span class="dv">1</span>,
  3014. <span class="dt">value</span>: <span class="st">&quot;is id 1&quot;</span>,
  3015. <span class="dt">sensors1</span>: <span class="dv">19</span>,
  3016. <span class="dt">sensors2</span>: <span class="dv">20</span>
  3017. },
  3018. {
  3019. <span class="dt">id</span>: <span class="dv">2</span>,
  3020. <span class="dt">value</span>: <span class="st">&quot;is id 2&quot;</span>,
  3021. <span class="dt">sensors1</span>: <span class="dv">20</span>,
  3022. <span class="dt">sensors2</span>: <span class="dv">21</span>
  3023. }
  3024. ],
  3025. <span class="st">&quot;init_table&quot;</span>:[
  3026. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, &#39;is id 1&#39;, 19, 20);&quot;</span>,
  3027. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, &#39;is id 2&#39;, 20, 21);&quot;</span>
  3028. ],
  3029. <span class="st">&quot;query_table&quot;</span>:<span class="st">&quot;select * from basic;&quot;</span>,
  3030. <span class="st">&quot;rest_url&quot;</span>: <span class="st">&quot;/id/:id&quot;</span>,
  3031. <span class="st">&quot;rest_post_url&quot;</span>: <span class="st">&quot;/&quot;</span>,
  3032. <span class="st">&quot;rest_port&quot;</span>: <span class="dv">8848</span>
  3033. };</code></pre>
  3034. <p>3.运行(run)</p>
  3035. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> index.js</code></pre>
  3036. <p>show:</p>
  3037. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> listening at coap://0.0.0.0:5683
  3038. <span class="kw">restify</span> listening at http://0.0.0.0:8848</code></pre>
  3039. <h1 id="简单物联网"><span class="header-section-number">19</span> 简单物联网</h1>
  3040. <p>到这时,我们算搭建了一个简单的REST服务了。接着我们可以简单的做一个最小的物联网系统,将我们的单片机、MCU等等连上网。</p>
  3041. <div class="figure">
  3042. <img src="./images/arch.jpeg" alt="硬件结构图" />
  3043. <p>硬件结构图</p>
  3044. </div>
  3045. <p>考虑到如果我们只是单一连接各个节点,那么系统的结构图,同下所示</p>
  3046. <div class="figure">
  3047. <img src="./images/fullconnected.png" alt="全连接" />
  3048. <p>全连接</p>
  3049. </div>
  3050. <p>下面的星形结构图类似于我们在接下来所要构建的系统</p>
  3051. <div class="figure">
  3052. <img src="./images/star.png" alt="星形结构图" />
  3053. <p>星形结构图</p>
  3054. </div>
  3055. <p>一个用于控制真实电器的硬件实物图</p>
  3056. <div class="figure">
  3057. <img src="./images/hardware.jpg" alt="简单实物图" />
  3058. <p>简单实物图</p>
  3059. </div>
  3060. <h2 id="硬件通信"><span class="header-section-number">19.1</span> 硬件通信</h2>
  3061. <h3 id="串口通信-1"><span class="header-section-number">19.1.1</span> 串口通信</h3>
  3062. <p>Arduino与Raspberry Pi通过串口通信的方式实现通信,相互传输所需要的数据,Raspberry Pi将资源传于互联网上对应的接口,接口可以在互联网上被访问。Laravel框架构架于服务器之上,将Raspbery Pi获取过来的数据存储于MySQL数据,再以REST服务的方式共享数据,互联网上的其他设备便可以通过网络来访问这些设备。Ajax用于将后台的数据以不需要刷新的方式传递到网站前台,通过HighCharts框架显示给终端用户。</p>
  3063. <h4 id="python"><span class="header-section-number">19.1.1.1</span> Python</h4>
  3064. <p>1.在Windows中的串口通常是<code>COM1</code>,<code>COM0</code>等等</p>
  3065. <pre class="sourceCode python"><code class="sourceCode python">ser=serial.Serial(<span class="st">&quot;COM0&quot;</span>,<span class="dv">9600</span>)</code></pre>
  3066. <p>2.Mac OS系统中位于/dev目录下,名字类似于<code>tty.usbmodem1451</code>。</p>
  3067. <pre class="sourceCode python"><code class="sourceCode python"> serial.Serial(<span class="st">&quot;/dev/tty.usbmodem1451&quot;</span>,<span class="dv">9600</span>)</code></pre>
  3068. <p>3.在Linux内核的系统中虚拟串口用的节点是ttyACM,位于/dev目录下。</p>
  3069. <pre class="sourceCode python"><code class="sourceCode python"> serial.Serial(<span class="st">&quot;/dev/ttyACM0&quot;</span>,<span class="dv">9600</span>)</code></pre>
  3070. <blockquote>
  3071. <p>串行接口是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件。一般完成这种功能 的电路,我们称为串行接口电路。</p>
  3072. </blockquote>
  3073. <p>便是打开这个设备,以9600的速率传输数据。</p>
  3074. <p>程序框架如下所示:</p>
  3075. <div class="figure">
  3076. <img src="./images/raspberrypi_flow.png" alt="Raspberry Pi" />
  3077. <p>Raspberry Pi</p>
  3078. </div>
  3079. <p>代码如下:</p>
  3080. <pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> json
  3081. <span class="ch">import</span> urllib2
  3082. <span class="ch">import</span> serial
  3083. <span class="ch">import</span> time
  3084. url=<span class="st">&quot;http://www.xianuniversity.com/athome/1&quot;</span>
  3085. <span class="kw">while</span> <span class="dv">1</span>:
  3086. <span class="kw">try</span>:
  3087. date=urllib2.urlopen(url)
  3088. result=json.load(date)
  3089. status=result[<span class="dv">0</span>][<span class="st">&quot;led1&quot;</span>]
  3090. ser=serial.Serial(<span class="st">&quot;/dev/ttyACM0&quot;</span>,<span class="dv">9600</span>)
  3091. <span class="kw">if</span> status==<span class="dv">1</span> :
  3092. ser.write(<span class="st">&quot;1&quot;</span>)
  3093. <span class="kw">elif</span> status==<span class="dv">0</span>:
  3094. ser.write(<span class="st">&quot;0&quot;</span>)
  3095. time.sleep(<span class="dv">1</span>)
  3096. <span class="kw">except</span> urllib2.URLError:
  3097. <span class="dt">print</span> <span class="st">&quot;Bad URL or timeout&quot;</span></code></pre>
  3098. <div class="figure">
  3099. <img src="./images/getjson.png" alt="python返回json数据" />
  3100. <p>python返回json数据</p>
  3101. </div>
  3102. <p>系统还需要对上面的数据进行处理,只拿其中的结果</p>
  3103. <div class="figure">
  3104. <img src="./images/origin.png" alt="python处理完后的结果" />
  3105. <p>python处理完后的结果</p>
  3106. </div>
  3107. <p>当改变led的状态后,便可以得到下面的结果</p>
  3108. <div class="figure">
  3109. <img src="./images/change.png" alt="改变状态后的结果" />
  3110. <p>改变状态后的结果</p>
  3111. </div>
  3112. <h4 id="ruby"><span class="header-section-number">19.1.1.2</span> Ruby</h4>
  3113. <p>如果你用的是Ruby的话,可以尝试使用<code>serialport</code></p>
  3114. <p>安装</p>
  3115. <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> gem install serialport</code></pre>
  3116. <p>代码大致如下</p>
  3117. <pre class="sourceCode ruby"><code class="sourceCode ruby">require <span class="st">&#39;serialport&#39;</span>
  3118. sp = <span class="dt">SerialPort</span>.new <span class="st">&quot;/dev/ACM0&quot;</span>, <span class="dv">9600</span>
  3119. sp.write <span class="st">&quot;1&quot;</span></code></pre>
  3120. <p>注意: 根据相关的系统修改相关的代码。</p>
  3121. <h2 id="硬件"><span class="header-section-number">19.2</span> 硬件</h2>
  3122. <h3 id="arduino"><span class="header-section-number">19.2.1</span> Arduino</h3>
  3123. <p>这样我们在我们的Arduino上所要做的便是,读取串口的结果并控制IO口。</p>
  3124. <pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> ledPort=<span class="dv">13</span>;
  3125. <span class="dt">void</span> setup() {
  3126. Serial.begin(<span class="dv">9600</span>);
  3127. pinMode(ledPort,OUTPUT);
  3128. }
  3129. <span class="dt">int</span> serialData;
  3130. <span class="dt">void</span> loop() {
  3131. String inString = <span class="st">&quot;&quot;</span>;
  3132. <span class="kw">while</span> (Serial.available()&gt; <span class="dv">0</span>)
  3133. {
  3134. <span class="dt">int</span> inChar = Serial.read();
  3135. <span class="kw">if</span> (isDigit(inChar)) {
  3136. inString += (<span class="dt">char</span>)inChar;
  3137. }
  3138. serialData=inString.toInt();
  3139. Serial.print(serialData);
  3140. }
  3141. <span class="kw">if</span>(serialData==<span class="dv">1</span>){
  3142. digitalWrite(ledPort,HIGH);
  3143. }<span class="kw">else</span>{
  3144. digitalWrite(ledPort,LOW);
  3145. }
  3146. }</code></pre>
  3147. <p>如果结果是1的话,就让13口为高电平,也就是让灯亮起来。</p>
  3148. <h3 id="继电器"><span class="header-section-number">19.2.2</span> 继电器</h3>
  3149. <blockquote>
  3150. <p>继电器(英文名称:relay)是一种电控制器件,是当输入量(激励量)的变化达到规定要求时,在电气输出电路中使被控量发生预定的阶跃变化的一种电器。它具有控制系统(又称输入回路)和被控制系统(又称输出回路)之间的互动关系。通常应用于自动化的控制电路中,它实际上是用小电流去控制大电流运作的一种“自动开关”。故在电路中起着自动调节、安全保护、转换电路等作用。</p>
  3151. </blockquote>
  3152. <p>在这里我们可以默认为我们想要为单片机的5V电压控制220V的电器。</p>
  3153. <p>最后我们便可以通过些来控制灯的开和关。</p>
  3154. <h1 id="android简单示例"><span class="header-section-number">20</span> Android简单示例</h1>
  3155. <p>由于在某些嵌入式系统中使用的是Android系统,这里给出一个简单的Android App的示例,具体代码可以从clone自<a href="https://github.com/phodal/iot-android" class="uri">https://github.com/phodal/iot-android</a></p>
  3156. <p>代码说明,经过测试的版本有</p>
  3157. <ul>
  3158. <li>Android 2.3</li>
  3159. <li>Android 4.0.4</li>
  3160. </ul>
  3161. <p>机型有</p>
  3162. <ul>
  3163. <li>HTC G1 (android 2.3)</li>
  3164. <li>Motor xt300 (android 2.3)</li>
  3165. <li>Sony ST25I (android 4.0.4)</li>
  3166. <li>MI2</li>
  3167. </ul>
  3168. <p>应该可以在大部分的手机上工作。</p>
  3169. <h2 id="调用web-services-get"><span class="header-section-number">20.1</span> 调用Web Services GET</h2>
  3170. <p>这里我们参考一篇文章来调用Web Services——<a href="http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/">Calling Web Services in Android using HttpClient</a></p>
  3171. <h3 id="创建restclient"><span class="header-section-number">20.1.1</span> 创建RESTClient</h3>
  3172. <p>在这里我们首先会定义四个REST方法GET、POST、PUT、DELETE</p>
  3173. <pre class="sourceCode java"><code class="sourceCode java"><span class="kw">public</span> <span class="dt">void</span> <span class="fu">Execute</span>(RequestMethod method) <span class="kw">throws</span> Exception {
  3174. <span class="kw">switch</span> (method) {
  3175. <span class="kw">case</span> GET: {
  3176. <span class="co">// add parameters</span>
  3177. String combinedParams = <span class="st">&quot;&quot;</span>;
  3178. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  3179. combinedParams += <span class="st">&quot;?&quot;</span>;
  3180. <span class="kw">for</span> (NameValuePair p : params) {
  3181. String paramString = p.<span class="fu">getName</span>() + <span class="st">&quot;=&quot;</span>
  3182. + URLEncoder.<span class="fu">encode</span>(p.<span class="fu">getValue</span>(), HTTP.<span class="fu">UTF_8</span>);
  3183. <span class="kw">if</span> (combinedParams.<span class="fu">length</span>() &gt; <span class="dv">1</span>) {
  3184. combinedParams += <span class="st">&quot;&amp;&quot;</span> + paramString;
  3185. } <span class="kw">else</span> {
  3186. combinedParams += paramString;
  3187. }
  3188. }
  3189. }
  3190. HttpGet request = <span class="kw">new</span> <span class="fu">HttpGet</span>(url + combinedParams);
  3191. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3192. <span class="co">// add headers</span>
  3193. <span class="kw">for</span> (NameValuePair h : headers) {
  3194. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3195. }
  3196. <span class="fu">executeRequest</span>(request, url);
  3197. <span class="kw">break</span>;
  3198. }
  3199. <span class="kw">case</span> POST: {
  3200. HttpPost request = <span class="kw">new</span> <span class="fu">HttpPost</span>(url);
  3201. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3202. <span class="co">// add headers</span>
  3203. <span class="kw">for</span> (NameValuePair h : headers) {
  3204. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3205. }
  3206. <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">&quot;&quot;</span>)) {
  3207. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
  3208. }
  3209. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  3210. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
  3211. }
  3212. <span class="fu">executeRequest</span>(request, url);
  3213. <span class="kw">break</span>;
  3214. }
  3215. <span class="kw">case</span> PUT: {
  3216. HttpPut request = <span class="kw">new</span> <span class="fu">HttpPut</span>(url);
  3217. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3218. <span class="co">// add headers</span>
  3219. <span class="kw">for</span> (NameValuePair h : headers) {
  3220. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3221. }
  3222. <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">&quot;&quot;</span>)) {
  3223. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
  3224. }
  3225. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  3226. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
  3227. }
  3228. <span class="fu">executeRequest</span>(request, url);
  3229. <span class="kw">break</span>;
  3230. }
  3231. <span class="kw">case</span> DELETE: {
  3232. HttpDelete request = <span class="kw">new</span> <span class="fu">HttpDelete</span>(url);
  3233. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3234. <span class="co">// add headers</span>
  3235. <span class="kw">for</span> (NameValuePair h : headers) {
  3236. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3237. }
  3238. <span class="fu">executeRequest</span>(request, url);
  3239. <span class="kw">break</span>;
  3240. }
  3241. }
  3242. }</code></pre>
  3243. <p>这四个方法最后都执行executeRequest来获取响应结果。</p>
  3244. <pre class="sourceCode java"><code class="sourceCode java"><span class="kw">protected</span> <span class="dt">void</span> <span class="fu">executeRequest</span>(HttpUriRequest request, String url) {
  3245. HttpParams httpParameters = <span class="kw">new</span> <span class="fu">BasicHttpParams</span>();
  3246. HttpConnectionParams.<span class="fu">setConnectionTimeout</span>(httpParameters,
  3247. timeoutConnection);
  3248. HttpConnectionParams.<span class="fu">setSoTimeout</span>(httpParameters, timeoutSocket);
  3249. HttpProtocolParams.<span class="fu">setUseExpectContinue</span>(httpParameters, <span class="kw">false</span>);
  3250. request.<span class="fu">setParams</span>(httpParameters);
  3251. <span class="fu">setOauth</span>(request);
  3252. DefaultHttpClient client = <span class="kw">new</span> <span class="fu">DefaultHttpClient</span>();
  3253. HttpResponse httpResponse;
  3254. <span class="kw">try</span> {
  3255. httpResponse = client.<span class="fu">execute</span>(request);
  3256. responseCode = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getStatusCode</span>();
  3257. message = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getReasonPhrase</span>();
  3258. HttpEntity entity = httpResponse.<span class="fu">getEntity</span>();
  3259. <span class="kw">if</span> (entity != <span class="kw">null</span>) {
  3260. InputStream instream = httpResponse.<span class="fu">getEntity</span>().<span class="fu">getContent</span>();
  3261. Header contentEncoding = httpResponse
  3262. .<span class="fu">getFirstHeader</span>(<span class="st">&quot;Content-Encoding&quot;</span>);
  3263. <span class="kw">if</span> (contentEncoding != <span class="kw">null</span>
  3264. &amp;&amp; contentEncoding.<span class="fu">getValue</span>().<span class="fu">equalsIgnoreCase</span>(<span class="st">&quot;gzip&quot;</span>)) {
  3265. instream = <span class="kw">new</span> GZIPInputStream(instream);
  3266. }
  3267. <span class="co">// instream = entity.getContent();</span>
  3268. response = <span class="fu">convertStreamToString</span>(instream);
  3269. <span class="co">// Closing the input stream will trigger connection release</span>
  3270. instream.<span class="fu">close</span>();
  3271. }
  3272. } <span class="kw">catch</span> (ClientProtocolException e) {
  3273. client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
  3274. e.<span class="fu">printStackTrace</span>();
  3275. } <span class="kw">catch</span> (IOException e) {
  3276. client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
  3277. e.<span class="fu">printStackTrace</span>();
  3278. }
  3279. }</code></pre>
  3280. <p>接着,我们便可以执行getResponse()函数来获取结果。</p>
  3281. <h2 id="使用rest-client获取结果"><span class="header-section-number">20.2</span> 使用REST Client获取结果</h2>
  3282. <p>使用RESTClient时,便可以用下面的示例</p>
  3283. <pre class="sourceCode java"><code class="sourceCode java">RestClient client = <span class="kw">new</span> <span class="fu">RestClient</span>(tUrl);
  3284. <span class="kw">try</span> {
  3285. client.<span class="fu">Execute</span>(RequestMethod.<span class="fu">GET</span>);
  3286. <span class="kw">if</span> (client.<span class="fu">getResponseCode</span>() != <span class="dv">200</span>) {
  3287. <span class="co">//do something</span>
  3288. }
  3289. <span class="co">//JSONArray jArray = new JSONArray(client.getResponse());</span>
  3290. } <span class="kw">catch</span> (Exception e) {
  3291. <span class="co">//do something</span>
  3292. }</code></pre>
  3293. <p>而这时,我们只需要对相应的数据进行处理就可以了,如</p>
  3294. <pre class="sourceCode java"><code class="sourceCode java">JSONArray jArray = <span class="kw">new</span> <span class="fu">JSONArray</span>(client.<span class="fu">getResponse</span>());
  3295. JSONObject jObj=jArray.<span class="fu">getJSONObject</span>(<span class="dv">0</span>);
  3296. vshow.<span class="fu">setText</span>(jObj.<span class="fu">toString</span>());
  3297. <span class="fu">outputJSON</span>(jObj);</code></pre>
  3298. <p>将他转换为String,接着在Android端上显示最后的结果。</p>
  3299. <h1 id="尾声"><span class="header-section-number">21</span> 尾声</h1>
  3300. <h2 id="路"><span class="header-section-number">21.1</span> 路</h2>
  3301. <h2 id="其他-2"><span class="header-section-number">21.2</span> 其他</h2>
  3302. <p>意见及建议: <a href="https://github.com/phodal/designiot/issues" class="uri">https://github.com/phodal/designiot/issues</a></p>
  3303. <p>邮箱: <a href="h@phodal.com" class="uri">h@phodal.com</a></p>
  3304. <div class="footnotes">
  3305. <hr />
  3306. <ol>
  3307. <li><div id="fn1">
  3308. </div>
  3309. <p>https://zh.opensuse.org/index.php?title=%E8%BD%AF%E4%BB%B6%E5%8C%85%E7%AE%A1%E7%90%86&amp;variant=zh<a href="#fnref1">↩</a></p></li>
  3310. <li><div id="fn2">
  3311. </div>
  3312. <p>形如http://www.phodal.com<a href="#fnref2">↩</a></p></li>
  3313. <li><div id="fn3">
  3314. </div>
  3315. <p>URL 是 URI 的子集<a href="#fnref3">↩</a></p></li>
  3316. <li><div id="fn4">
  3317. </div>
  3318. <p>curl是利用URL语法在命令行方式下工作的开源文件传输工具。<a href="#fnref4">↩</a></p></li>
  3319. </ol>
  3320. </div>