index.html 313 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <meta http-equiv="Content-Style-Type" content="text/css" />
  6. <meta name="generator" content="pandoc" />
  7. <title></title>
  8. <style type="text/css">code{white-space: pre;}</style>
  9. <style type="text/css">
  10. div.sourceCode { overflow-x: auto; }
  11. table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  12. margin: 0; padding: 0; vertical-align: baseline; border: none; }
  13. table.sourceCode { width: 100%; line-height: 100%; }
  14. td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
  15. td.sourceCode { padding-left: 5px; }
  16. code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
  17. code > span.dt { color: #902000; } /* DataType */
  18. code > span.dv { color: #40a070; } /* DecVal */
  19. code > span.bn { color: #40a070; } /* BaseN */
  20. code > span.fl { color: #40a070; } /* Float */
  21. code > span.ch { color: #4070a0; } /* Char */
  22. code > span.st { color: #4070a0; } /* String */
  23. code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
  24. code > span.ot { color: #007020; } /* Other */
  25. code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
  26. code > span.fu { color: #06287e; } /* Function */
  27. code > span.er { color: #ff0000; font-weight: bold; } /* Error */
  28. code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  29. code > span.cn { color: #880000; } /* Constant */
  30. code > span.sc { color: #4070a0; } /* SpecialChar */
  31. code > span.vs { color: #4070a0; } /* VerbatimString */
  32. code > span.ss { color: #bb6688; } /* SpecialString */
  33. code > span.im { } /* Import */
  34. code > span.va { color: #19177c; } /* Variable */
  35. code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  36. code > span.op { color: #666666; } /* Operator */
  37. code > span.bu { } /* BuiltIn */
  38. code > span.ex { } /* Extension */
  39. code > span.pp { color: #bc7a00; } /* Preprocessor */
  40. code > span.at { color: #7d9029; } /* Attribute */
  41. code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
  42. code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  43. code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  44. code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  45. </style>
  46. <link rel="stylesheet" href="css/vendor.css" type="text/css" />
  47. </head>
  48. <body>
  49. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:wb="http://open.weibo.com/wb" >
  50. <head>
  51. <script src="http://tjs.sjs.sinajs.cn/open/api/js/wb.js" type="text/javascript" charset="utf-8"></script>
  52. <title>一步步搭建物联网系统(教你设计物联网系统)</title>
  53. <meta name="keywords" content="设计物联网系统,物联网系统设计">
  54. <meta name="description" content="一份关于如何设计物联网系统的文档">
  55. <link rel="stylesheet" href="css/vendor.css" />
  56. </head>
  57. <body class="container inner wrap">
  58. <wb:follow-button uid="1533917471" type="red_4" width="100%" height="64" ></wb:follow-button>
  59. <table>
  60. <tr>
  61. <td>
  62. <iframe src="http://ghbtns.com/github-btn.html?user=phodal&repo=designiot&type=watch&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe>
  63. </td>
  64. <td>
  65. <iframe src="http://ghbtns.com/github-btn.html?user=phodal&repo=designiot&type=fork&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe>
  66. </td>
  67. <td>
  68. <iframe src="http://ghbtns.com/github-btn.html?user=phodal&type=follow&count=true&size=large" height="30" width="240" frameborder="0" scrolling="0" style="width:240px; height: 30px;" allowTransparency="true"></iframe>
  69. </td>
  70. </tr>
  71. </table>
  72. <h2>纸质版《自己动手设计物联网》现已出版</h2>
  73. <p><a href="./designiot.jpg" target="_blank"><img src="./designiot.jpg" alt="Designiot" style="max-width:100%;"></a></p>
  74. <p>立即购买:<a href="https://www.amazon.cn/dp/B01IBZWTWW/ref=wl_it_dp_o_pC_nS_ttl?_encoding=UTF8&amp;colid=BDXF90QZX6WX&amp;coliid=I19EB97K0GNLW8">亚马逊</a>、<a href="http://search.jd.com/Search?keyword=%E8%87%AA%E5%B7%B1%E5%8A%A8%E6%89%8B%E8%AE%BE%E8%AE%A1%E7%89%A9%E8%81%94%E7%BD%91&amp;enc=utf-8&amp;wq=%E8%87%AA%E5%B7%B1%E5%8A%A8%E6%89%8B%E8%AE%BE%E8%AE%A1%E7%89%A9%E8%81%94%E7%BD%91&amp;pvid=k24y6hri.l4xi28">京东</a></p><a href="http://product.dangdang.com/24000878.html">当当</a>
  75. <h1>一步步搭建物联网系统(教你设计物联网系统)</h1>
  76. 源自毕业论文:<a href="http://ebook.designiot.cn/iot.html" title="基于REST服务的最小物联网系统设计">基于REST服务的最小物联网系统设计</a>
  77. <script>
  78. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  79. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  80. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  81. })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  82. ga('create', 'UA-54524083-1', 'auto');
  83. ga('send', 'pageview');
  84. </script>
  85. </body>
  86. </html>
  87. <div id="TOC">
  88. <ul>
  89. <li><a href="#前言"><span class="toc-section-number">1</span> 前言</a><ul>
  90. <li><a href="#目标读者"><span class="toc-section-number">1.1</span> 目标读者</a></li>
  91. <li><a href="#不适合人群"><span class="toc-section-number">1.2</span> 不适合人群</a></li>
  92. <li><a href="#介绍"><span class="toc-section-number">1.3</span> 介绍</a><ul>
  93. <li><a href="#为什么没有c"><span class="toc-section-number">1.3.1</span> 为什么没有C ?</a></li>
  94. <li><a href="#为什么不是java"><span class="toc-section-number">1.3.2</span> 为什么不是JAVA ?</a></li>
  95. </ul></li>
  96. <li><a href="#如何阅读"><span class="toc-section-number">1.4</span> 如何阅读</a></li>
  97. </ul></li>
  98. <li><a href="#无处不在的html"><span class="toc-section-number">2</span> 无处不在的HTML</a><ul>
  99. <li><a href="#html的helloworld"><span class="toc-section-number">2.1</span> html的hello,world</a><ul>
  100. <li><a href="#调试helloworld"><span class="toc-section-number">2.1.1</span> 调试hello,world</a></li>
  101. <li><a href="#说说helloworld"><span class="toc-section-number">2.1.2</span> 说说hello,world</a></li>
  102. <li><a href="#想用中文"><span class="toc-section-number">2.1.3</span> 想用中文?</a></li>
  103. </ul></li>
  104. <li><a href="#其他html标记"><span class="toc-section-number">2.2</span> 其他html标记</a><ul>
  105. <li><a href="#美妙之处"><span class="toc-section-number">2.2.1</span> 美妙之处</a></li>
  106. <li><a href="#更多"><span class="toc-section-number">2.2.2</span> 更多</a></li>
  107. </ul></li>
  108. </ul></li>
  109. <li><a href="#无处不在的javascript"><span class="toc-section-number">3</span> 无处不在的Javascript</a><ul>
  110. <li><a href="#javascript的helloworld"><span class="toc-section-number">3.1</span> Javascript的Hello,world</a></li>
  111. <li><a href="#更js一点"><span class="toc-section-number">3.2</span> 更js一点</a><ul>
  112. <li><a href="#从数学出发"><span class="toc-section-number">3.2.1</span> 从数学出发</a></li>
  113. </ul></li>
  114. <li><a href="#设计和编程"><span class="toc-section-number">3.3</span> 设计和编程</a><ul>
  115. <li><a href="#函数"><span class="toc-section-number">3.3.1</span> 函数</a></li>
  116. <li><a href="#重新设计"><span class="toc-section-number">3.3.2</span> 重新设计</a></li>
  117. <li><a href="#object和函数"><span class="toc-section-number">3.3.3</span> object和函数</a></li>
  118. <li><a href="#面向对象"><span class="toc-section-number">3.3.4</span> 面向对象</a></li>
  119. </ul></li>
  120. <li><a href="#其他"><span class="toc-section-number">3.4</span> 其他</a></li>
  121. <li><a href="#美妙之处-1"><span class="toc-section-number">3.5</span> 美妙之处</a></li>
  122. </ul></li>
  123. <li><a href="#无处不在的css"><span class="toc-section-number">4</span> 无处不在的CSS</a><ul>
  124. <li><a href="#css"><span class="toc-section-number">4.1</span> CSS</a></li>
  125. <li><a href="#关于css"><span class="toc-section-number">4.2</span> 关于CSS</a></li>
  126. <li><a href="#代码结构"><span class="toc-section-number">4.3</span> 代码结构</a></li>
  127. <li><a href="#样式与目标"><span class="toc-section-number">4.4</span> 样式与目标</a><ul>
  128. <li><a href="#选择器"><span class="toc-section-number">4.4.1</span> 选择器</a></li>
  129. </ul></li>
  130. <li><a href="#更有趣的css"><span class="toc-section-number">4.5</span> 更有趣的CSS</a></li>
  131. </ul></li>
  132. <li><a href="#无处不在的三剑客"><span class="toc-section-number">5</span> 无处不在的三剑客</a><ul>
  133. <li><a href="#hellogeek"><span class="toc-section-number">5.1</span> Hello,Geek</a></li>
  134. <li><a href="#从源码学习"><span class="toc-section-number">5.2</span> 从源码学习</a></li>
  135. <li><a href="#浏览器渲染过程"><span class="toc-section-number">5.3</span> 浏览器渲染过程</a><ul>
  136. <li><a href="#html"><span class="toc-section-number">5.3.1</span> HTML</a></li>
  137. </ul></li>
  138. <li><a href="#dom树形结构图"><span class="toc-section-number">5.4</span> DOM树形结构图</a><ul>
  139. <li><a href="#javascript"><span class="toc-section-number">5.4.1</span> javascript</a></li>
  140. <li><a href="#css-1"><span class="toc-section-number">5.4.2</span> CSS</a></li>
  141. </ul></li>
  142. <li><a href="#css盒模型图"><span class="toc-section-number">5.5</span> CSS盒模型图</a></li>
  143. <li><a href="#笔记"><span class="toc-section-number">5.6</span> 笔记</a></li>
  144. </ul></li>
  145. <li><a href="#gnulinux-强大且free"><span class="toc-section-number">6</span> GNU/Linux 强大且Free</a><ul>
  146. <li><a href="#什么是linux"><span class="toc-section-number">6.1</span> 什么是Linux</a></li>
  147. <li><a href="#操作系统"><span class="toc-section-number">6.2</span> 操作系统</a><ul>
  148. <li><a href="#linux架构图"><span class="toc-section-number">6.2.1</span> Linux架构图</a></li>
  149. <li><a href="#shell"><span class="toc-section-number">6.2.2</span> Shell</a></li>
  150. <li><a href="#gcc"><span class="toc-section-number">6.2.3</span> GCC</a></li>
  151. <li><a href="#启动引导程序"><span class="toc-section-number">6.2.4</span> 启动引导程序</a></li>
  152. </ul></li>
  153. <li><a href="#从编译开始"><span class="toc-section-number">6.3</span> 从编译开始</a><ul>
  154. <li><a href="#开始之前"><span class="toc-section-number">6.3.1</span> 开始之前</a></li>
  155. <li><a href="#编译nginx"><span class="toc-section-number">6.3.2</span> 编译Nginx</a></li>
  156. <li><a href="#其他-1"><span class="toc-section-number">6.3.3</span> 其他</a></li>
  157. </ul></li>
  158. <li><a href="#包管理"><span class="toc-section-number">6.4</span> 包管理</a></li>
  159. <li><a href="#ubuntu-lnmp"><span class="toc-section-number">6.5</span> Ubuntu LNMP</a><ul>
  160. <li><a href="#update软件包列表"><span class="toc-section-number">6.5.1</span> Update软件包列表</a></li>
  161. <li><a href="#安装mysql"><span class="toc-section-number">6.5.2</span> 安装MySQL</a></li>
  162. <li><a href="#安装nginx"><span class="toc-section-number">6.5.3</span> 安装Nginx</a></li>
  163. <li><a href="#安装php"><span class="toc-section-number">6.5.4</span> 安装PHP</a></li>
  164. </ul></li>
  165. </ul></li>
  166. <li><a href="#arduino-极客的玩具"><span class="toc-section-number">7</span> Arduino 极客的玩具</a><ul>
  167. <li><a href="#极客的玩具"><span class="toc-section-number">7.1</span> 极客的玩具</a></li>
  168. <li><a href="#硬件熟悉"><span class="toc-section-number">7.2</span> 硬件熟悉</a></li>
  169. <li><a href="#开发环境"><span class="toc-section-number">7.3</span> 开发环境</a></li>
  170. <li><a href="#点亮一个led"><span class="toc-section-number">7.4</span> 点亮一个LED</a></li>
  171. <li><a href="#串口通信"><span class="toc-section-number">7.5</span> 串口通信</a><ul>
  172. <li><a href="#关于arduino-setup"><span class="toc-section-number">7.5.1</span> 关于Arduino Setup()</a></li>
  173. </ul></li>
  174. </ul></li>
  175. <li><a href="#python-代码如散文"><span class="toc-section-number">8</span> Python 代码如散文</a><ul>
  176. <li><a href="#代码与散文"><span class="toc-section-number">8.1</span> 代码与散文</a><ul>
  177. <li><a href="#开始之前-1"><span class="toc-section-number">8.1.1</span> 开始之前</a></li>
  178. <li><a href="#python的helloworld"><span class="toc-section-number">8.1.2</span> Python的Hello,World</a></li>
  179. <li><a href="#我们想要的helloworld"><span class="toc-section-number">8.1.3</span> 我们想要的Hello,World</a></li>
  180. </ul></li>
  181. <li><a href="#算法"><span class="toc-section-number">8.2</span> 算法</a></li>
  182. <li><a href="#实用主义哲学"><span class="toc-section-number">8.3</span> 实用主义哲学</a></li>
  183. <li><a href="#包管理-1"><span class="toc-section-number">8.4</span> 包管理</a><ul>
  184. <li><a href="#python-requests"><span class="toc-section-number">8.4.1</span> python requests</a></li>
  185. </ul></li>
  186. </ul></li>
  187. <li><a href="#raspberry-pi"><span class="toc-section-number">9</span> Raspberry Pi</a><ul>
  188. <li><a href="#geek的盛宴"><span class="toc-section-number">9.1</span> Geek的盛宴</a></li>
  189. <li><a href="#raspberry-pi-初始化"><span class="toc-section-number">9.2</span> Raspberry Pi 初始化</a></li>
  190. <li><a href="#raspberry-pi-gpio"><span class="toc-section-number">9.3</span> Raspberry Pi GPIO</a></li>
  191. </ul></li>
  192. <li><a href="#server-一切皆为服务"><span class="toc-section-number">10</span> Server 一切皆为服务</a><ul>
  193. <li><a href="#服务器"><span class="toc-section-number">10.1</span> 服务器</a></li>
  194. <li><a href="#web服务器"><span class="toc-section-number">10.2</span> Web服务器</a></li>
  195. <li><a href="#lnmp"><span class="toc-section-number">10.3</span> LNMP</a></li>
  196. </ul></li>
  197. <li><a href="#web服务"><span class="toc-section-number">11</span> Web服务</a><ul>
  198. <li><a href="#soap-vs-restful"><span class="toc-section-number">11.1</span> SOAP VS RESTful</a></li>
  199. </ul></li>
  200. <li><a href="#http-熟悉陌生"><span class="toc-section-number">12</span> HTTP 熟悉&amp;陌生</a><ul>
  201. <li><a href="#你所没有深入的http"><span class="toc-section-number">12.1</span> 你所没有深入的HTTP</a><ul>
  202. <li><a href="#打开网页时发生了什么"><span class="toc-section-number">12.1.1</span> 打开网页时发生了什么</a></li>
  203. <li><a href="#url组成"><span class="toc-section-number">12.1.2</span> URL组成</a></li>
  204. </ul></li>
  205. <li><a href="#一次http-get请求"><span class="toc-section-number">12.2</span> 一次HTTP GET请求</a><ul>
  206. <li><a href="#http响应"><span class="toc-section-number">12.2.1</span> HTTP响应</a></li>
  207. </ul></li>
  208. </ul></li>
  209. <li><a href="#设计restful-api"><span class="toc-section-number">13</span> 设计RESTful API</a><ul>
  210. <li><a href="#资源"><span class="toc-section-number">13.0.1</span> 资源</a></li>
  211. <li><a href="#设计restful-api-1"><span class="toc-section-number">13.1</span> 设计RESTful API</a></li>
  212. <li><a href="#rest关键目标"><span class="toc-section-number">13.2</span> REST关键目标</a></li>
  213. <li><a href="#判断是否是-restful的约束条件"><span class="toc-section-number">13.3</span> 判断是否是 RESTful的约束条件</a></li>
  214. <li><a href="#json"><span class="toc-section-number">13.4</span> JSON</a></li>
  215. </ul></li>
  216. <li><a href="#环境准备"><span class="toc-section-number">14</span> 环境准备</a><ul>
  217. <li><a href="#laravel"><span class="toc-section-number">14.1</span> Laravel</a><ul>
  218. <li><a href="#为什么是-laravel"><span class="toc-section-number">14.1.1</span> 为什么是 Laravel</a></li>
  219. </ul></li>
  220. <li><a href="#安装-laravel"><span class="toc-section-number">14.2</span> 安装 Laravel</a><ul>
  221. <li><a href="#gnulinux安装composer"><span class="toc-section-number">14.2.1</span> GNU/Linux安装Composer</a></li>
  222. </ul></li>
  223. <li><a href="#mysql"><span class="toc-section-number">14.3</span> MySQL</a><ul>
  224. <li><a href="#安装mysql-1"><span class="toc-section-number">14.3.1</span> 安装MySQL</a></li>
  225. <li><a href="#配置mysql"><span class="toc-section-number">14.3.2</span> 配置MySQL</a></li>
  226. </ul></li>
  227. </ul></li>
  228. <li><a href="#创建rest服务"><span class="toc-section-number">15</span> 创建REST服务</a><ul>
  229. <li><a href="#数据库迁移"><span class="toc-section-number">15.1</span> 数据库迁移</a><ul>
  230. <li><a href="#创建表"><span class="toc-section-number">15.1.1</span> 创建表</a></li>
  231. <li><a href="#数据库迁移-1"><span class="toc-section-number">15.1.2</span> 数据库迁移</a></li>
  232. </ul></li>
  233. <li><a href="#创建restful"><span class="toc-section-number">15.2</span> 创建RESTful</a></li>
  234. <li><a href="#laravel-resources"><span class="toc-section-number">15.3</span> Laravel Resources</a><ul>
  235. <li><a href="#修改create"><span class="toc-section-number">15.3.1</span> 修改Create()</a></li>
  236. <li><a href="#创建表单"><span class="toc-section-number">15.3.2</span> 创建表单</a></li>
  237. <li><a href="#编辑模板"><span class="toc-section-number">15.3.3</span> 编辑模板</a></li>
  238. </ul></li>
  239. </ul></li>
  240. <li><a href="#前端显示"><span class="toc-section-number">16</span> 前端显示</a><ul>
  241. <li><a href="#库与车轮子"><span class="toc-section-number">16.1</span> 库与车轮子</a></li>
  242. <li><a href="#库"><span class="toc-section-number">16.2</span> 库</a><ul>
  243. <li><a href="#jquery"><span class="toc-section-number">16.2.1</span> jQuery</a></li>
  244. <li><a href="#jquery-mobile"><span class="toc-section-number">16.2.2</span> jQuery Mobile</a></li>
  245. </ul></li>
  246. <li><a href="#网站前台显示"><span class="toc-section-number">16.3</span> 网站前台显示</a><ul>
  247. <li><a href="#highcharts"><span class="toc-section-number">16.3.1</span> Highcharts</a></li>
  248. <li><a href="#实时数据"><span class="toc-section-number">16.3.2</span> 实时数据</a></li>
  249. </ul></li>
  250. </ul></li>
  251. <li><a href="#restful的coap协议"><span class="toc-section-number">17</span> RESTful的CoAP协议</a><ul>
  252. <li><a href="#coap-嵌入式系统的rest"><span class="toc-section-number">17.1</span> CoAP: 嵌入式系统的REST</a></li>
  253. <li><a href="#coap-命令行工具"><span class="toc-section-number">17.2</span> CoAP 命令行工具</a><ul>
  254. <li><a href="#node-coap-cli"><span class="toc-section-number">17.2.1</span> Node CoAP CLI</a></li>
  255. <li><a href="#libcoap"><span class="toc-section-number">17.2.2</span> libcoap</a></li>
  256. <li><a href="#firefox-copper"><span class="toc-section-number">17.2.3</span> Firefox Copper</a></li>
  257. </ul></li>
  258. <li><a href="#coap-helloworld"><span class="toc-section-number">17.3</span> CoAP Hello,World</a></li>
  259. <li><a href="#coap-数据库查询"><span class="toc-section-number">17.4</span> CoAP 数据库查询</a><ul>
  260. <li><a href="#node-module"><span class="toc-section-number">17.4.1</span> Node Module</a></li>
  261. <li><a href="#node-sqlite3"><span class="toc-section-number">17.4.2</span> Node-Sqlite3</a></li>
  262. <li><a href="#查询数据"><span class="toc-section-number">17.4.3</span> 查询数据</a></li>
  263. </ul></li>
  264. <li><a href="#coap-block"><span class="toc-section-number">17.5</span> CoAP Block</a><ul>
  265. <li><a href="#coap-post"><span class="toc-section-number">17.5.1</span> CoAP POST</a></li>
  266. <li><a href="#coap-content-types"><span class="toc-section-number">17.5.2</span> CoAP Content Types</a></li>
  267. </ul></li>
  268. <li><a href="#coap-json"><span class="toc-section-number">17.6</span> CoAP JSON</a></li>
  269. <li><a href="#使用iot-coap构建物联网"><span class="toc-section-number">17.7</span> 使用IoT-CoAP构建物联网</a></li>
  270. </ul></li>
  271. <li><a href="#简单物联网"><span class="toc-section-number">18</span> 简单物联网</a><ul>
  272. <li><a href="#硬件通信"><span class="toc-section-number">18.1</span> 硬件通信</a><ul>
  273. <li><a href="#串口通信-1"><span class="toc-section-number">18.1.1</span> 串口通信</a></li>
  274. </ul></li>
  275. <li><a href="#硬件"><span class="toc-section-number">18.2</span> 硬件</a><ul>
  276. <li><a href="#arduino"><span class="toc-section-number">18.2.1</span> Arduino</a></li>
  277. <li><a href="#继电器"><span class="toc-section-number">18.2.2</span> 继电器</a></li>
  278. </ul></li>
  279. </ul></li>
  280. <li><a href="#android简单示例"><span class="toc-section-number">19</span> Android简单示例</a><ul>
  281. <li><a href="#调用web-services-get"><span class="toc-section-number">19.1</span> 调用Web Services GET</a><ul>
  282. <li><a href="#创建restclient"><span class="toc-section-number">19.1.1</span> 创建RESTClient</a></li>
  283. </ul></li>
  284. <li><a href="#使用rest-client获取结果"><span class="toc-section-number">19.2</span> 使用REST Client获取结果</a></li>
  285. </ul></li>
  286. <li><a href="#尾声"><span class="toc-section-number">20</span> 尾声</a><ul>
  287. <li><a href="#路"><span class="toc-section-number">20.1</span> 路</a></li>
  288. <li><a href="#其他-2"><span class="toc-section-number">20.2</span> 其他</a></li>
  289. </ul></li>
  290. </ul>
  291. </div>
  292. <p>本作品采用<a href="http://creativecommons.org/licenses/by-nc/4.0/">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可。<img src="./images/88x31.png" alt="cc" /></p>
  293. <p>© 2014 <a href="http://www.phodal.com">Phodal Huang</a>.</p>
  294. <h1 id="前言"><span class="header-section-number">1</span> 前言</h1>
  295. <p>设计物联网系统是件有意思的事情,它需要考虑到软件、硬件、通讯等多个不同方面。通过探索不同的语言,不同的框架,从而形成不同的解决方案。</p>
  296. <p>在这里,我们将对设计物联网系统有一个简单的介绍,并探讨如何设计一个最小的物联网系统。</p>
  297. <h2 id="目标读者"><span class="header-section-number">1.1</span> 目标读者</h2>
  298. <p>目标读者: 初入物联网领域,希望对物联网系统有一个大概的认识和把握,并学会掌握一个基础的物联网系统的设计。</p>
  299. <ul>
  300. <li>硬件开发人员,对物联网有兴趣。</li>
  301. <li>没有web开发经验</li>
  302. <li>几乎为0的linux使用经验</li>
  303. <li>想快速用于生产环境<br />
  304. </li>
  305. <li>对硬件了解有限的开发人员。</li>
  306. <li>没接触过51、ARM、Arduino<br />
  307. </li>
  308. <li>想了解以下内容:</li>
  309. <li>RESTful与IOT</li>
  310. <li>CoAP协议</li>
  311. <li>MQTT</li>
  312. </ul>
  313. <p>本文档对一些概念(如)只做了一些基本介绍,以方便读者理解。如果您想进一步了解这些知识,会列出一些推荐书目,以供参考。</p>
  314. <h2 id="不适合人群"><span class="header-section-number">1.2</span> 不适合人群</h2>
  315. <ul>
  316. <li>如果你是在这方面已经有了丰富经验的开发者。</li>
  317. <li>不是为了学习而学习这方面的知识。</li>
  318. </ul>
  319. <h2 id="介绍"><span class="header-section-number">1.3</span> 介绍</h2>
  320. <p>关于内容的选择,这是一个有意思的话题,因为我们很难判断不同的开发者用的是怎样的语言,用的是怎样的框架。</p>
  321. <p>于是我们便自作主张地选择了那些适合于理论学习的语言、框架、硬件,去除掉其他一些我们不需要考虑的因素,如语法,复杂度等等。当然,这些语言、框架、硬件也是最流行的。</p>
  322. <ul>
  323. <li>Arduino: 如果你从头开始学过硬件的话,那么你会爱上它的。</li>
  324. <li>Raspberry PI: 如果你从头编译过GNU/Linux的话,我想你会爱上她的。</li>
  325. <li>Python: 简单地来说,你可以方便地使用一些扩展,同时代码就表达了你的想法。</li>
  326. <li>PHP : 这是一门容易部署的语言,我想你只需要在你的Ubuntu机器上,执行一下脚本就能完成安装了。而且,如果你是一个硬件开发者的话,你会更容易地找到其他开发者。</li>
  327. <li>Javascript : 考虑到javascript这门语言已经无处不在了,而且会更加流行。所以,在这里CoAP、MQTT等版本是基于Nodejs的。</li>
  328. <li>HTML、CSS : 这是必须的,同样,他们仍然无处不在。</li>
  329. <li>GNU/Linux: 作为部署到服务器的一部分——你需要掌握他。当然如果你要用WAMP也是可以的。</li>
  330. <li>CoAP: 用NodeJS构建IOT CoAP物联网</li>
  331. </ul>
  332. <h3 id="为什么没有c"><span class="header-section-number">1.3.1</span> 为什么没有C ?</h3>
  333. <p><code>C都不懂还跑过来干嘛</code>。</p>
  334. <h3 id="为什么不是java"><span class="header-section-number">1.3.2</span> 为什么不是JAVA ?</h3>
  335. <p>大有以下两个原因</p>
  336. <ul>
  337. <li>学习JAVA的人很多,但是它不适合我们集中精力构建与学习,因为无关的代码太多了。</li>
  338. <li>之前以及现在,我还是不喜欢JAVA (我更喜欢脚本语言,可以提高工作效率)。</li>
  339. </ul>
  340. <h2 id="如何阅读"><span class="header-section-number">1.4</span> 如何阅读</h2>
  341. <p>这只是一个小小的建议,仅针对于在选择阅读上没有经验的读者。</p>
  342. <table>
  343. <thead>
  344. <tr class="header">
  345. <th>当前状态</th>
  346. <th>建议</th>
  347. </tr>
  348. </thead>
  349. <tbody>
  350. <tr class="odd">
  351. <td>软件初学者</td>
  352. <td>从头阅读</td>
  353. </tr>
  354. <tr class="even">
  355. <td>硬件开发者</td>
  356. <td>从头阅读</td>
  357. </tr>
  358. <tr class="odd">
  359. <td>没有web经验的开发者</td>
  360. <td>从第二部分开始</td>
  361. </tr>
  362. </tbody>
  363. </table>
  364. <p>我们会在前面十章里简单介绍一些必要的基础知识,这些知识将会在后面我们构建物联网系统时用到。</p>
  365. <p>某一天,正走在回学校的路上的我突然想到:“未来将会是一个科技的时代——虽然现在也是——只是在未来,科技将会无处不在。如果我们依旧对周围这些无处不在的代码一无所知的话,或许我们会成为黑客帝国之中被控制的普通人。”于是开始想着,有一天人们会像学习一门语言一样开始学习编程,直到又有一天我看到了学习编程如同学习一门语言的说法。这又恰好在我做完最小物联网系统之后,算是一个有趣的时间点,我开始想着像之前做最小物联网系统的那些步骤一样,写一个简单的入门。也可以补充好之前在这个最小物联网系统缺失的那些东西,给那些正在开始试图去解决编程问题的人。</p>
  366. <p>让我们先从身边的语言下手,也就是现在无处不在的html+javascript+css。</p>
  367. <h1 id="无处不在的html"><span class="header-section-number">2</span> 无处不在的HTML</h1>
  368. <p>之所以从html开始,是因为我们不需要配置一个复杂的开发环境,也许你还不知道开发环境是什么东西,不过这也没关系,毕竟这些知识需要慢慢的接触才能有所了解,尤其是对于普通的业余爱好者来说,当然,对于专业选手言自然不是问题。HTML是Web的核心语言,也算是比较基础的语言。</p>
  369. <h2 id="html的helloworld"><span class="header-section-number">2.1</span> html的hello,world</h2>
  370. <p>Hello,world是一个传统,所以在这里也遵循这个有趣的传统,我们所要做的事情其实很简单,虽然也有一点点hack的感觉。——让我们先来新建一个文并命名为“helloworld.html”。</p>
  371. <p>(PS:大部分人应该都是在windows环境下工作的,所以你需要新建一个文本,然后重命名,或者你需要一个编辑器,在这里我们推荐用<strong>sublime text</strong>。破解不破解,注册不注册都不会对你的使用有太多的影响。)</p>
  372. <ol style="list-style-type: decimal">
  373. <li><p>新建文件</p></li>
  374. <li>输入
  375. <pre><code class="html">hello,world</code></pre></li>
  376. <li><p>保存为-&gt;“helloworld.html”,</p></li>
  377. <li><p>双击打开这个文件。 正常情况下都应该是用你的默认浏览器打开。只要是一个正常工作的现代浏览器,都应该可以看到上面显示的是“Hello,world”。</p></li>
  378. </ol>
  379. <p>这才是最短的hello,world程序,但是呢?在ruby中会是这样子的</p>
  380. <div class="sourceCode"><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>
  381. <span class="st">&quot;hello,world&quot;</span>
  382. =<span class="kw">&gt;</span> <span class="st">&quot;hello,world&quot;</span>
  383. <span class="kw">2.0.0-p353</span> :002 <span class="kw">&gt;</span></code></pre></div>
  384. <p>等等,如果你了解过html的话,会觉得这一点都不符合语法规则,但是他工作了,没有什么比安装完Nginx后看到It works!更让人激动了。</p>
  385. <p>遗憾的是,它可能无法在所有的浏览器上工作,所以我们需要去调试其中的bug。</p>
  386. <h3 id="调试helloworld"><span class="header-section-number">2.1.1</span> 调试hello,world</h3>
  387. <p>我们会发现我们的代码在浏览器中变成了下面的代码,如果你和我一样用的是chrome,那么你可以右键浏览器中的空白区域,点击审查元素,就会看到下面的代码。</p>
  388. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  389. <span class="kw">&lt;head&gt;&lt;/head&gt;</span>
  390. <span class="kw">&lt;body&gt;</span>hello,world<span class="kw">&lt;/body&gt;</span>
  391. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  392. <p>这个才是真正能在大部分浏览器上工作的代码,所以复制它到编辑器里吧。</p>
  393. <h3 id="说说helloworld"><span class="header-section-number">2.1.2</span> 说说hello,world</h3>
  394. 我很不喜欢其中的&lt;*&gt;&lt;/*&gt;,但是我也没有找到别的方法来代替它们,所以这是一个设计得当的语言。甚至大部分人都说这算不上是一门真正的语言,不过html的原义是
  395. <blockquote>
  396. 超文本标记语言
  397. </blockquote>
  398. <p>所以我们可以发现其中的关键词是标记——markup,也就是说html是一个markup,head是一个markup,body也是一个markup。</p>
  399. <p>然而,我们真正工作的代码是在body里面,至于为什么是在这里面,这个问题就太复杂了。打个比方来说:</p>
  400. <ol style="list-style-type: decimal">
  401. <li><p>我们所使用的汉语是人类用智慧创造的,我们所正在学的这门语言同样也是人类创造的。</p></li>
  402. <li><p>我们在自己的语言里遵循着<strong>桌子是桌子,凳子是凳子</strong>的原则,很少有人会问为什么。</p></li>
  403. </ol>
  404. <h3 id="想用中文"><span class="header-section-number">2.1.3</span> 想用中文?</h3>
  405. <p>所以我们也可以把计算机语言与现实世界里用于交流沟通的语言划上一个等号。而我们所要学习的语言,并不是我们最熟悉的汉语语言,所以我们便觉得这些很复杂,但是如果我们试着用汉语替换掉上面的代码的话</p>
  406. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>语言&gt;
  407. <span class="er">&lt;</span>头&gt;<span class="er">&lt;</span>结束头&gt;
  408. <span class="er">&lt;</span>身体&gt;你好,世界<span class="er">&lt;</span>结束身体&gt;
  409. <span class="er">&lt;</span>结束语言&gt;</code></pre></div>
  410. <p>这看上去很奇怪,只是因为是直译过去的原因,也许你会觉得这样会好理解一点,但是输入上可就一点儿也不方便,因为这键盘本身就不适合我们去输入汉字,同时也意味着可能你输入的会有问题。</p>
  411. <p>让我们把上面的代码代替掉原来的代码然后保存,打开浏览器会看到下面的结果</p>
  412. <div class="sourceCode"><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></div>
  413. <p>更不幸的结果可能是</p>
  414. <div class="sourceCode"><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></div>
  415. <p>这是一个编码问题,对中文支持不友好。</p>
  416. <p>我们把上面的代码改为和标记语言一样的结构</p>
  417. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="er">&lt;</span>语言&gt;
  418. <span class="er">&lt;</span>头&gt;<span class="er">&lt;</span>/头&gt;
  419. <span class="er">&lt;</span>身体&gt;你好,世界<span class="er">&lt;</span>/身体&gt;
  420. <span class="er">&lt;</span>结束语言&gt;</code></pre></div>
  421. <p>于是我们看到的结果便是</p>
  422. <div class="sourceCode"><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></div>
  423. <p>被chrome浏览器解析成什么样了?</p>
  424. <div class="sourceCode"><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;
  425. <span class="er">&lt;</span>头&gt;<span class="co">&lt;!--头--&gt;</span>
  426. <span class="er">&lt;</span>身体&gt;你好,世界<span class="co">&lt;!--身体--&gt;</span>
  427. <span class="co">&lt;!--语言--&gt;</span>
  428. <span class="kw">&lt;/body&gt;&lt;/html&gt;</span> </code></pre></div>
  429. <p>以 <!--开头,--></p>
  430. <p>结尾的是注释,写给人看的代码,不是给机器看的,所以机器不会去理解这些代码。</p>
  431. <p>但是当我们把代码改成</p>
  432. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;whatwewanttosay&gt;</span>你好世界<span class="kw">&lt;/whatwewanttosay&gt;</span></code></pre></div>
  433. <p>浏览器上面显示的内容就变成了</p>
  434. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html">你好世界</code></pre></div>
  435. <p>或许你会觉得很神奇,但是这一点儿也不神奇,虽然我们的中文语法也遵循着标记语言的标准,但是我们的浏览器不支持中文标记。</p>
  436. <p>结论:</p>
  437. <ol style="list-style-type: decimal">
  438. <li>浏览器对中文支持不友好。</li>
  439. <li>浏览器对英文支持友好。</li>
  440. </ol>
  441. <p>刚开始的时候不要对中文编程有太多的想法,这是很不现实的:</p>
  442. <ol style="list-style-type: decimal">
  443. <li>现有的系统都是基于英语语言环境构建的,对中文支持不是很友好。</li>
  444. <li>中文输入的速度在某种程度上来说没有英语快。</li>
  445. </ol>
  446. <p>我们离开话题已经很远了,但是这里说的都是针对于那些不满于英语的人来说的,只有当我们可以从头构建一个中文系统的时候才是可行的,而这些就要将cpu、软件、硬件都包含在内,甚至我们还需要考虑重新设计cpu的结构,在某种程度上来说会有些不现实。或许,需要一代又一代人的努力。忘记那些吧,师夷长之技以治夷。</p>
  447. <h2 id="其他html标记"><span class="header-section-number">2.2</span> 其他html标记</h2>
  448. <p>添加一个标题,</p>
  449. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  450. <span class="kw">&lt;head&gt;</span>
  451. <span class="kw">&lt;title&gt;</span>标题<span class="kw">&lt;/title&gt;</span>
  452. <span class="kw">&lt;/head&gt;</span>
  453. <span class="kw">&lt;body&gt;</span>hello,world<span class="kw">&lt;/body&gt;</span>
  454. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  455. <p>我们便可以在浏览器的最上方看到“标题”二字,就像我们常用的淘宝网,也包含了上面的东西,只是还包括了更多的东西,所以你也可以看懂那些我们可以看到的淘宝的标题。</p>
  456. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  457. <span class="kw">&lt;head&gt;</span>
  458. <span class="kw">&lt;title&gt;</span>标题<span class="kw">&lt;/title&gt;</span>
  459. <span class="kw">&lt;/head&gt;</span>
  460. <span class="kw">&lt;body&gt;</span>
  461. hello,world
  462. <span class="kw">&lt;h1&gt;</span>大标题<span class="kw">&lt;/h1&gt;</span>
  463. <span class="kw">&lt;h2&gt;</span>次标题<span class="kw">&lt;/h2&gt;</span>
  464. <span class="kw">&lt;h3&gt;</span>...<span class="kw">&lt;/h3&gt;</span>
  465. <span class="kw">&lt;ul&gt;</span>
  466. <span class="kw">&lt;li&gt;</span>列表1<span class="kw">&lt;/li&gt;</span>
  467. <span class="kw">&lt;li&gt;</span>列表2<span class="kw">&lt;/li&gt;</span>
  468. <span class="kw">&lt;/ul&gt;</span>
  469. <span class="kw">&lt;/body&gt;</span>
  470. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  471. <p>更多的东西可以在一些书籍上看到,这边所要说的只是一次简单的语言入门,其他的东西都和这些类似。</p>
  472. <h3 id="美妙之处"><span class="header-section-number">2.2.1</span> 美妙之处</h3>
  473. <p>我们简单地上手了一门不算是语言的语言,浏览器简化了这其中的大部分过程,虽然没有C和其他语言来得有专业感,但是我们试着去开始写代码了。我们可能在未来的某一篇中可能会看到类似的语言,诸如python,我们所要做的就是</p>
  474. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">python</span> file.py
  475. =<span class="kw">&gt;hello</span>,world</code></pre></div>
  476. <p>然后在终端上返回结果。只是因为在我看来学会html是有意义的,简单的上手,然后再慢慢地深入,如果一开始我们就去理解指针,开始去理解类。我们甚至还知道程序是怎么编译运行的时候,在这个过程中又发生了什么。虽然现在我们也没能理解这其中发生了什么,但是至少展示了</p>
  477. <ol style="list-style-type: decimal">
  478. <li>中文编程语言在当前意义不大,不现实,效率不高兼容性差</li>
  479. <li>语言的语法是固定的。(ps:虽然我们也可以进行扩充,我们将会在后来支持上述的中文标记。)</li>
  480. <li>已经开始写代码,而不是还在配置开发环境。</li>
  481. <li>随身的工具才是最好的,最常用的code也才是实在的。</li>
  482. </ol>
  483. <h3 id="更多"><span class="header-section-number">2.2.2</span> 更多</h3>
  484. <p>我们还没有试着去解决“某商店里的糖一颗5块钱,小明买了3颗糖,小明一共花了多少钱”的问题。也就是说我们学会的是一个还不能解决实际问题的语言,于是我们还需要学点东西,比如javascript,css。我们可以将Javascript理解为解决问题的语言,html则是前端显示,css是配置文件,这样的话,我们会在那之后学会成为一个近乎专业的程序员。我们刚刚学习了一下怎么在前端显示那些代码的行为,于是我们还需要Javascript。</p>
  485. <h1 id="无处不在的javascript"><span class="header-section-number">3</span> 无处不在的Javascript</h1>
  486. <p>Javascript现在已经无处不在了,也许你正打开的某个网站,他便可能是node.js+json+javascript+mustache.js完成的,虽然你还没理解上面那些是什么,也正是因为你不理解才需要去学习更多的东西。但是你只要知道Javascript已经无处不在了,它可能就在你手机上的某个app里,就在你浏览的网页里,就运行在你IDE中的某个进程里。</p>
  487. <h2 id="javascript的helloworld"><span class="header-section-number">3.1</span> Javascript的Hello,world</h2>
  488. <p>这里我们还需要有一个helloworld.html,Javascript是专为网页交互而设计的脚本语言,所以我们一点点来开始这部分的旅途,先写一个符合标准的helloworld.html</p>
  489. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  490. <span class="kw">&lt;html&gt;</span>
  491. <span class="kw">&lt;head&gt;&lt;/head&gt;</span>
  492. <span class="kw">&lt;body&gt;&lt;/body&gt;</span>
  493. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  494. <p>然后开始融入我们的javascript,向HTML中插入Javascript的方法,就需要用到html中的&lt;script&gt;标签,我们先用页面嵌入的方法来写helloworld。</p>
  495. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  496. <span class="kw">&lt;html&gt;</span>
  497. <span class="kw">&lt;head&gt;</span>
  498. <span class="kw">&lt;script&gt;</span>
  499. <span class="va">document</span>.<span class="at">write</span>(<span class="st">&#39;hello,world&#39;</span>)<span class="op">;</span>
  500. <span class="op">&lt;</span><span class="ss">/script&gt;</span>
  501. <span class="ss"> &lt;/head</span><span class="op">&gt;</span>
  502. <span class="op">&lt;</span>body<span class="op">&gt;&lt;</span><span class="ss">/body&gt;</span>
  503. <span class="ss">&lt;/html</span><span class="op">&gt;</span></code></pre></div>
  504. <p>按照标准的写法,我们还需要声明这个脚本的类型</p>
  505. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  506. <span class="kw">&lt;html&gt;</span>
  507. <span class="kw">&lt;head&gt;</span>
  508. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="kw">&gt;</span>
  509. <span class="va">document</span>.<span class="at">write</span>(<span class="st">&#39;hello,world&#39;</span>)<span class="op">;</span>
  510. <span class="op">&lt;</span><span class="ss">/script&gt;</span>
  511. <span class="ss"> &lt;/head</span><span class="op">&gt;</span>
  512. <span class="op">&lt;</span>body<span class="op">&gt;&lt;</span><span class="ss">/body&gt;</span>
  513. <span class="ss">&lt;/html</span><span class="op">&gt;</span></code></pre></div>
  514. <p>没有显示hello,world?试试下面的代码</p>
  515. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  516. <span class="kw">&lt;html&gt;</span>
  517. <span class="kw">&lt;head&gt;</span>
  518. <span class="kw">&lt;script</span><span class="ot"> type=</span><span class="st">&quot;text/javascript&quot;</span><span class="kw">&gt;</span>
  519. <span class="va">document</span>.<span class="at">write</span>(<span class="st">&#39;hello,world&#39;</span>)<span class="op">;</span>
  520. <span class="op">&lt;</span><span class="ss">/script&gt;</span>
  521. <span class="ss"> &lt;/head</span><span class="op">&gt;</span>
  522. <span class="op">&lt;</span>body<span class="op">&gt;</span>
  523. <span class="op">&lt;</span>noscript<span class="op">&gt;</span>
  524. disable Javascript
  525. <span class="op">&lt;</span><span class="ss">/noscript&gt;</span>
  526. <span class="ss"> &lt;/body</span><span class="op">&gt;</span>
  527. <span class="op">&lt;</span><span class="ss">/html&gt;</span></code></pre></div>
  528. <h2 id="更js一点"><span class="header-section-number">3.2</span> 更js一点</h2>
  529. <p>我们需要让我们的代码看上去更像是js,同时是以js结尾。就像C语言的源码是以C结尾的,我们也同样需要让我们的代码看上去更正式一点。于是我们需要在helloworld.html的同一文件夹下创建一个app.js文件,在里面写着</p>
  530. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">document</span>.<span class="at">write</span>(<span class="st">&#39;hello,world&#39;</span>)<span class="op">;</span></code></pre></div>
  531. <p>同时我们的helloworld.html还需要告诉我们的浏览器js代码在哪里</p>
  532. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  533. <span class="kw">&lt;html&gt;</span>
  534. <span class="kw">&lt;head&gt;</span>
  535. <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>
  536. <span class="kw">&lt;/head&gt;</span>
  537. <span class="kw">&lt;body&gt;</span>
  538. <span class="kw">&lt;noscript&gt;</span>
  539. disable Javascript
  540. <span class="kw">&lt;/noscript&gt;</span>
  541. <span class="kw">&lt;/body&gt;</span>
  542. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  543. <h3 id="从数学出发"><span class="header-section-number">3.2.1</span> 从数学出发</h3>
  544. <p>让我们回到第一章讲述的小明的问题,<strong>从实际问题下手编程,更容易学会编程</strong>。小学时代的数学题最喜欢这样子了——某商店里的糖一个5块钱,小明买了3个糖,小明一共花了多少钱。在编程方面,也许我们还算是小学生。最直接的方法就是直接计算3x5=?</p>
  545. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">document</span>.<span class="at">write</span>(<span class="dv">3</span><span class="op">*</span><span class="dv">5</span>)<span class="op">;</span></code></pre></div>
  546. <p>document.write实际也我们可以理解为输出,也就是往页面里写入3*5的结果,在有双引号的情况下会输出字符串。我们便会在浏览器上看到15,这便是一个好的开始,也是一个糟糕的开始。</p>
  547. <h2 id="设计和编程"><span class="header-section-number">3.3</span> 设计和编程</h2>
  548. <p>对于实际问题,如果我们只是止于所要得到的结果,很多年之后,我们就成为了code monkey。对这个问题进行再一次设计,所谓的设计有些时候会把简单的问题复杂化,有些时候会使以后的扩展更加简单。这一天因为这家商店的糖价格太高了,于是店长将价格降为了4块钱。</p>
  549. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">document</span>.<span class="at">write</span>(<span class="dv">3</span><span class="op">*</span><span class="dv">4</span>)<span class="op">;</span></code></pre></div>
  550. <p>于是我们又得到了我们的结果,但是下次我们看到这些代码的时候没有分清楚哪个是糖的数量,哪个是价格,于是我们重新设计了程序</p>
  551. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">tang<span class="op">=</span><span class="dv">4</span><span class="op">;</span>
  552. num<span class="op">=</span><span class="dv">3</span><span class="op">;</span>
  553. <span class="va">document</span>.<span class="at">write</span>(tang<span class="op">*</span>num)<span class="op">;</span></code></pre></div>
  554. <p>这才能叫得上是程序设计,或许你注意到了“;”这个符号的存在,我想说的是这是另外一个标准,我们不得不去遵守,也不得不去fuck。</p>
  555. <h3 id="函数"><span class="header-section-number">3.3.1</span> 函数</h3>
  556. <p>记得刚开始学三角函数的时候,我们会写</p>
  557. <pre><code>sin 30=0.5</code></pre>
  558. <p>而我们的函数也是类似于此,换句话说,因为很多搞计算机的先驱都学好了数学,都把数学世界的规律带到了计算机世界,所以我们的函数也是类似于此,让我们做一个简单的开始。</p>
  559. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">hello</span>()<span class="op">{</span>
  560. <span class="cf">return</span> <span class="va">document</span>.<span class="at">write</span>(<span class="st">&quot;hello,world&quot;</span>)<span class="op">;</span>
  561. <span class="op">}</span>
  562. <span class="at">hello</span>()<span class="op">;</span></code></pre></div>
  563. <p>当我第一次看到函数的时候,有些小激动终于出现了。我们写了一个叫hello的函数,它返回了往页面中写入hello,world的方法,然后我们调用了hello这个函数,于是页面上有了hello,world。</p>
  564. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">sin</span>(degree)<span class="op">{</span>
  565. <span class="cf">return</span> <span class="va">document</span>.<span class="at">write</span>(<span class="va">Math</span>.<span class="at">sin</span>(degree))<span class="op">;</span>
  566. <span class="op">}</span>
  567. <span class="at">sin</span>(<span class="dv">30</span>)<span class="op">;</span></code></pre></div>
  568. <p>在这里degree就称之为变量。 于是输出了-0.9880316240928602,而不是0.5,因为这里用的是弧度制,而不是角度制。</p>
  569. <pre><code>sin(30)</code></pre>
  570. <p>的输出结果有点类似于sin 30。写括号的目的在于,括号是为了方便解析,这个在不同的语言中可能是不一样的,比如在ruby中我们可以直接用类似于数学中的表达:</p>
  571. <div class="sourceCode"><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>
  572. =&gt; -<span class="fl">0.9880316240928618</span>
  573. <span class="fl">2.0</span>.<span class="dv">0</span>-p353 :<span class="dv">005</span> &gt;</code></pre></div>
  574. <p>我们可以在函数中传入多个变量,于是我们再回到小明的问题,就会这样去编写代码。</p>
  575. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">calc</span>(tang<span class="op">,</span>num)<span class="op">{</span>
  576. result<span class="op">=</span>tang<span class="op">*</span>num<span class="op">;</span>
  577. <span class="va">document</span>.<span class="at">write</span>(result)<span class="op">;</span>
  578. <span class="op">}</span>
  579. <span class="at">calc</span>(<span class="dv">3</span><span class="op">,</span><span class="dv">4</span>)<span class="op">;</span></code></pre></div>
  580. <p>但是从某种程度上来说,我们的calc做了计算的事又做了输出的事,总的来说设计上有些不好。</p>
  581. <h3 id="重新设计"><span class="header-section-number">3.3.2</span> 重新设计</h3>
  582. <p>我们将输出的工作移到函数的外面,</p>
  583. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">calc</span>(tang<span class="op">,</span>num)<span class="op">{</span>
  584. <span class="cf">return</span> tang<span class="op">*</span>num<span class="op">;</span>
  585. <span class="op">}</span>
  586. <span class="va">document</span>.<span class="at">write</span>(<span class="at">calc</span>(<span class="dv">3</span><span class="op">,</span><span class="dv">4</span>))<span class="op">;</span></code></pre></div>
  587. <p>接着我们用一种更有意思的方法来写这个问题的解决方案</p>
  588. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">calc</span>(tang<span class="op">,</span>num)<span class="op">{</span>
  589. <span class="cf">return</span> tang<span class="op">*</span>num<span class="op">;</span>
  590. <span class="op">}</span>
  591. <span class="kw">function</span> <span class="at">printResult</span>(tang<span class="op">,</span>num)<span class="op">{</span>
  592. <span class="va">document</span>.<span class="at">write</span>(<span class="at">calc</span>(tang<span class="op">,</span>num))<span class="op">;</span>
  593. <span class="op">}</span>
  594. <span class="at">printResult</span>(<span class="dv">3</span><span class="op">,</span> <span class="dv">4</span>)</code></pre></div>
  595. <p>看上去更专业了一点点,如果我们只需要计算的时候我们只需要调用calc,如果我们需要输出的时候我们就调用printResult的方法。</p>
  596. <h3 id="object和函数"><span class="header-section-number">3.3.3</span> object和函数</h3>
  597. <p>我们还没有说清楚之前我们遇到过的document.write以及Math.sin的语法为什么看上去很奇怪,所以让我们看看他们到底是什么,修改app.js为以下内容</p>
  598. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">document</span>.<span class="at">write</span>(<span class="kw">typeof</span> document)<span class="op">;</span>
  599. <span class="va">document</span>.<span class="at">write</span>(<span class="kw">typeof</span> Math)<span class="op">;</span></code></pre></div>
  600. <p>typeof document会返回document的数据类型,就会发现输出的结果是</p>
  601. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">object object</code></pre></div>
  602. <p>所以我们需要去弄清楚什么是object。对象的定义是</p>
  603. <blockquote>
  604. 无序属性的集合,其属性可以包含基本值、对象或者函数。
  605. </blockquote>
  606. <p>创建一个object,然后观察这便是我们接下来要做的</p>
  607. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">store<span class="op">={};</span>
  608. <span class="va">store</span>.<span class="at">tang</span><span class="op">=</span><span class="dv">4</span><span class="op">;</span>
  609. <span class="va">store</span>.<span class="at">num</span><span class="op">=</span><span class="dv">3</span><span class="op">;</span>
  610. <span class="va">document</span>.<span class="at">write</span>(<span class="va">store</span>.<span class="at">tang</span><span class="op">*</span><span class="va">store</span>.<span class="at">num</span>)<span class="op">;</span></code></pre></div>
  611. <p>我们就有了和document.write一样的用法,这也是对象的美妙之处,只是这里的对象只是包含着基本值,因为</p>
  612. <pre><code>typeof story.tang=&quot;number&quot;</code></pre>
  613. <p>一个包含对象的对象应该是这样子的。</p>
  614. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">store<span class="op">={};</span>
  615. <span class="va">store</span>.<span class="at">tang</span><span class="op">=</span><span class="dv">4</span><span class="op">;</span>
  616. <span class="va">store</span>.<span class="at">num</span><span class="op">=</span><span class="dv">3</span><span class="op">;</span>
  617. <span class="va">document</span>.<span class="at">writeln</span>(<span class="va">store</span>.<span class="at">tang</span><span class="op">*</span><span class="va">store</span>.<span class="at">num</span>)<span class="op">;</span>
  618. <span class="kw">var</span> wall<span class="op">=</span><span class="kw">new</span> <span class="at">Object</span>()<span class="op">;</span>
  619. <span class="va">wall</span>.<span class="at">store</span><span class="op">=</span>store<span class="op">;</span>
  620. <span class="va">document</span>.<span class="at">write</span>(<span class="kw">typeof</span> <span class="va">wall</span>.<span class="at">store</span>)<span class="op">;</span></code></pre></div>
  621. <p>而我们用到的document.write和上面用到的document.writeln都是属于这个无序属性集合中的函数。</p>
  622. <p>下面代码说的就是这个无序属性集中中的函数。</p>
  623. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> IO<span class="op">=</span><span class="kw">new</span> <span class="at">Object</span>()<span class="op">;</span>
  624. <span class="kw">function</span> <span class="at">print</span>(result)<span class="op">{</span>
  625. <span class="va">document</span>.<span class="at">write</span>(result)<span class="op">;</span>
  626. <span class="op">};</span>
  627. <span class="va">IO</span>.<span class="at">print</span><span class="op">=</span>print<span class="op">;</span>
  628. <span class="va">IO</span>.<span class="at">print</span>(<span class="st">&quot;a obejct with function&quot;</span>)<span class="op">;</span>
  629. <span class="va">IO</span>.<span class="at">print</span>(<span class="kw">typeof</span> <span class="va">IO</span>.<span class="at">print</span>)<span class="op">;</span></code></pre></div>
  630. <p>我们定义了一个叫IO的对象,声明对象可以用</p>
  631. <pre><code>var store={};</code></pre>
  632. <p>又或者是</p>
  633. <pre><code>var store=new Object{};</code></pre>
  634. <p>两者是等价的,但是用后者的可读性会更好一点,我们定义了一个叫print的函数,他的作用也就是document.write,IO中的print函数是等价于print()函数,这也就是对象和函数之间的一些区别,对象可以包含函数,对象是无序属性的集合,其属性可以包含基本值、对象或者函数。</p>
  635. <p>复杂一点的对象应该是下面这样的一种情况。</p>
  636. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person<span class="op">={</span><span class="dt">name</span><span class="op">:</span><span class="st">&quot;phodal&quot;</span><span class="op">,</span><span class="dt">weight</span><span class="op">:</span><span class="dv">50</span><span class="op">,</span><span class="dt">height</span><span class="op">:</span><span class="dv">166</span><span class="op">};</span>
  637. <span class="kw">function</span> <span class="at">dream</span>()<span class="op">{</span>
  638. future<span class="op">;</span>
  639. <span class="op">};</span>
  640. <span class="va">Person</span>.<span class="at">future</span><span class="op">=</span>dream<span class="op">;</span>
  641. <span class="va">document</span>.<span class="at">write</span>(<span class="kw">typeof</span> Person)<span class="op">;</span>
  642. <span class="va">document</span>.<span class="at">write</span>(<span class="va">Person</span>.<span class="at">future</span>)<span class="op">;</span></code></pre></div>
  643. <p>而这些会在我们未来的实际编程过程中用得更多。</p>
  644. <h3 id="面向对象"><span class="header-section-number">3.3.4</span> 面向对象</h3>
  645. <p>开始之前先让我们简化上面的代码,</p>
  646. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">Person</span>.<span class="at">future</span><span class="op">=</span><span class="kw">function</span> <span class="at">dream</span>()<span class="op">{</span>
  647. future<span class="op">;</span>
  648. <span class="op">}</span></code></pre></div>
  649. <p>看上去比上面的简单多了,不过我们还可以简化为下面的代码。。。</p>
  650. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person<span class="op">=</span><span class="kw">function</span>()<span class="op">{</span>
  651. <span class="kw">this</span>.<span class="at">name</span><span class="op">=</span><span class="st">&quot;phodal&quot;</span><span class="op">;</span>
  652. <span class="kw">this</span>.<span class="at">weight</span><span class="op">=</span><span class="dv">50</span><span class="op">;</span>
  653. <span class="kw">this</span>.<span class="at">height</span><span class="op">=</span><span class="dv">166</span><span class="op">;</span>
  654. <span class="kw">this</span>.<span class="at">future</span><span class="op">=</span><span class="kw">function</span> <span class="at">dream</span>()<span class="op">{</span>
  655. <span class="cf">return</span> <span class="st">&quot;future&quot;</span><span class="op">;</span>
  656. <span class="op">};</span>
  657. <span class="op">};</span>
  658. <span class="kw">var</span> person<span class="op">=</span><span class="kw">new</span> <span class="at">Person</span>()<span class="op">;</span>
  659. <span class="va">document</span>.<span class="at">write</span>(<span class="va">person</span>.<span class="at">name</span><span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span>
  660. <span class="va">document</span>.<span class="at">write</span>(<span class="kw">typeof</span> person<span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span>
  661. <span class="va">document</span>.<span class="at">write</span>(<span class="kw">typeof</span> <span class="va">person</span>.<span class="at">future</span><span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span>
  662. <span class="va">document</span>.<span class="at">write</span>(<span class="va">person</span>.<span class="at">future</span>()<span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span></code></pre></div>
  663. <p>只是在这个时候Person是一个函数,但是我们声明的person却变成了一个对象<strong>一个Javascript函数也是一个对象,并且,所有的对象从技术上讲也只不过是函数。</strong>这里的“&lt;br&gt;”是HTML中的元素,称之为DOM,在这里起的是换行的作用,我们会在稍后介绍它,这里我们先关心下this。this关键字表示函数的所有者或作用域,也就是这里的Person。</p>
  664. <p>上面的方法显得有点不可取,换句话说和一开始的</p>
  665. <pre><code>document.write(3*4);</code></pre>
  666. <p>一样,不具有灵活性,因此在我们完成功能之后,我们需要对其进行优化,这就是程序设计的真谛——解决完实际问题后,我们需要开始真正的设计,而不是解决问题时的编程。</p>
  667. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person<span class="op">=</span><span class="kw">function</span>(name<span class="op">,</span>weight<span class="op">,</span>height)<span class="op">{</span>
  668. <span class="kw">this</span>.<span class="at">name</span><span class="op">=</span>name<span class="op">;</span>
  669. <span class="kw">this</span>.<span class="at">weight</span><span class="op">=</span>weight<span class="op">;</span>
  670. <span class="kw">this</span>.<span class="at">height</span><span class="op">=</span>height<span class="op">;</span>
  671. <span class="kw">this</span>.<span class="at">future</span><span class="op">=</span><span class="kw">function</span>()<span class="op">{</span>
  672. <span class="cf">return</span> <span class="st">&quot;future&quot;</span><span class="op">;</span>
  673. <span class="op">};</span>
  674. <span class="op">};</span>
  675. <span class="kw">var</span> phodal<span class="op">=</span><span class="kw">new</span> <span class="at">Person</span>(<span class="st">&quot;phodal&quot;</span><span class="op">,</span><span class="dv">50</span><span class="op">,</span><span class="dv">166</span>)<span class="op">;</span>
  676. <span class="va">document</span>.<span class="at">write</span>(<span class="va">phodal</span>.<span class="at">name</span><span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span>
  677. <span class="va">document</span>.<span class="at">write</span>(<span class="va">phodal</span>.<span class="at">weight</span><span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span>
  678. <span class="va">document</span>.<span class="at">write</span>(<span class="va">phodal</span>.<span class="at">height</span><span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span>
  679. <span class="va">document</span>.<span class="at">write</span>(<span class="va">phodal</span>.<span class="at">future</span>()<span class="op">+</span><span class="st">&quot;&lt;br&gt;&quot;</span>)<span class="op">;</span></code></pre></div>
  680. <p>于是,产生了这样一个可重用的Javascript对象,this关键字确立了属性的所有者。</p>
  681. <h2 id="其他"><span class="header-section-number">3.4</span> 其他</h2>
  682. <p>Javascript还有一个很强大的特性,也就是原型继承,不过这里我们先不考虑这些部分,用尽量少的代码及关键字来实际我们所要表达的核心功能,这才是这里的核心,其他的东西我们可以从其他书本上学到。</p>
  683. <p>所谓的继承,</p>
  684. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Chinese<span class="op">=</span><span class="kw">function</span>()<span class="op">{</span>
  685. <span class="kw">this</span>.<span class="at">country</span><span class="op">=</span><span class="st">&quot;China&quot;</span><span class="op">;</span>
  686. <span class="op">}</span>
  687. <span class="kw">var</span> Person<span class="op">=</span><span class="kw">function</span>(name<span class="op">,</span>weight<span class="op">,</span>height)<span class="op">{</span>
  688. <span class="kw">this</span>.<span class="at">name</span><span class="op">=</span>name<span class="op">;</span>
  689. <span class="kw">this</span>.<span class="at">weight</span><span class="op">=</span>weight<span class="op">;</span>
  690. <span class="kw">this</span>.<span class="at">height</span><span class="op">=</span>height<span class="op">;</span>
  691. <span class="kw">this</span>.<span class="at">futrue</span><span class="op">=</span><span class="kw">function</span>()<span class="op">{</span>
  692. <span class="cf">return</span> <span class="st">&quot;future&quot;</span><span class="op">;</span>
  693. <span class="op">}</span>
  694. <span class="op">}</span>
  695. <span class="va">Chinese</span>.<span class="at">prototype</span><span class="op">=</span><span class="kw">new</span> <span class="at">Person</span>()<span class="op">;</span>
  696. <span class="kw">var</span> phodal<span class="op">=</span><span class="kw">new</span> <span class="at">Chinese</span>(<span class="st">&quot;phodal&quot;</span><span class="op">,</span><span class="dv">50</span><span class="op">,</span><span class="dv">166</span>)<span class="op">;</span>
  697. <span class="va">document</span>.<span class="at">write</span>(<span class="va">phodal</span>.<span class="at">country</span>)<span class="op">;</span></code></pre></div>
  698. <p>完整的Javascript应该由下列三个部分组成:</p>
  699. <ul>
  700. <li>核心(ECMAScript)——核心语言功能</li>
  701. <li>文档对象模型(DOM)——访问和操作网页内容的方法和接口</li>
  702. <li>浏览器对象模型(BOM)——与浏览器交互的方法和接口</li>
  703. </ul>
  704. <p>我们在上面讲的都是ECMAScript,也就是语法相关的,但是JS真正强大的,或者说我们最需要的可能就是对DOM的操作,这也就是为什么jQuery等库可以流行的原因之一,而核心语言功能才是真正在哪里都适用的,至于BOM,真正用到的机会很少,因为没有完善的统一的标准。</p>
  705. <p>一个简单的DOM示例,</p>
  706. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  707. <span class="kw">&lt;html&gt;</span>
  708. <span class="kw">&lt;head&gt;</span>
  709. <span class="kw">&lt;/head&gt;</span>
  710. <span class="kw">&lt;body&gt;</span>
  711. <span class="kw">&lt;noscript&gt;</span>
  712. disable Javascript
  713. <span class="kw">&lt;/noscript&gt;</span>
  714. <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>
  715. <span class="kw">&lt;/body&gt;</span>
  716. <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>
  717. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  718. <p>我们需要修改一下helloworld.html添加</p>
  719. <div class="sourceCode"><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></div>
  720. <p>同时还需要将script标签移到body下面,如果没有意外的话我们会看到页面上用红色的字体显示Red,修改app.js。</p>
  721. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para<span class="op">=</span><span class="va">document</span>.<span class="at">getElementById</span>(<span class="st">&quot;para&quot;</span>)<span class="op">;</span>
  722. <span class="va">para</span>.<span class="va">style</span>.<span class="at">color</span><span class="op">=</span><span class="st">&quot;blue&quot;</span><span class="op">;</span></code></pre></div>
  723. <p>接着,字体就变成了蓝色,有了DOM我们就可以对页面进行操作,可以说我们看到的绝大部分的页面效果都是通过DOM操作实现的。</p>
  724. <h2 id="美妙之处-1"><span class="header-section-number">3.5</span> 美妙之处</h2>
  725. <p>这里说到的Javascript仅仅只是其中的一小小部分,忽略掉的东西很多,只关心的是如何去设计一个实用的app,作为一门编程语言,他还有其他强大的内制函数,要学好需要一本有价值的参考书。这里提到的只是其中的不到20%的东西,其他的80%或者更多会在你解决问题的时候出现。</p>
  726. <ul>
  727. <li>我们可以创建一个对象或者函数,它可以包含基本值、对象或者函数。</li>
  728. <li>我们可以用Javascript修改页面的属性,虽然只是简单的示例。</li>
  729. <li>我们还可以去解决实际的编程问题。</li>
  730. </ul>
  731. <h1 id="无处不在的css"><span class="header-section-number">4</span> 无处不在的CSS</h1>
  732. <p>或许你觉得CSS一点儿也不重要,而事实上,如果说HTML是建筑的框架,CSS就是房子的装修。那么Javascript呢,我听到的最有趣的说法是小三——还是先让我们回到代码上来吧。</p>
  733. <h2 id="css"><span class="header-section-number">4.1</span> CSS</h2>
  734. <p>下面就是我们之前说到的代码,css将Red三个字母变成了红色。</p>
  735. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  736. <span class="kw">&lt;html&gt;</span>
  737. <span class="kw">&lt;head&gt;</span>
  738. <span class="kw">&lt;/head&gt;</span>
  739. <span class="kw">&lt;body&gt;</span>
  740. <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>
  741. <span class="kw">&lt;/body&gt;</span>
  742. <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>
  743. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  744. <p>只是,</p>
  745. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para<span class="op">=</span><span class="va">document</span>.<span class="at">getElementById</span>(<span class="st">&quot;para&quot;</span>)<span class="op">;</span>
  746. <span class="va">para</span>.<span class="va">style</span>.<span class="at">color</span><span class="op">=</span><span class="st">&quot;blue&quot;</span><span class="op">;</span></code></pre></div>
  747. <p>将字体变成了蓝色,CSS+HTML让页面有序的工作着,但是Javascript却打乱了这些秩序,有着唯恐世界不乱的精彩,也难怪被冠以小三之名了——或许终于可以理解,为什么以前人们对于Javascript没有好感了——不过这里要讲的是正室,也就是CSS,这时还没有Javascript。</p>
  748. <div class="figure">
  749. <img src="./images/redfonts.png" alt="Red Fonts" />
  750. <p class="caption">Red Fonts</p>
  751. </div>
  752. <h2 id="关于css"><span class="header-section-number">4.2</span> 关于CSS</h2>
  753. <p>这不是一篇专业讲述CSS的书籍,所以我不会去说CSS是怎么来的,有些东西我们既然可以很容易从其他地方知道,也就不需要花太多时间去重复。诸如重构等这些的目的之一也在于去除重复的代码,不过有些重复是不可少的,也是有必要的,而通常这些东西可能是由其他地方复制过来的。</p>
  754. <p>到目前为止我们没有依赖于任何特殊的硬件或者是软件,对于我们来说我们最基本的需求就是一台电脑,或者可以是你的平板电脑,当然也可以是你的智能手机,因为他们都有个浏览器,而这些都是能用的,对于我们的CSS来说也不会有例外的。</p>
  755. <p>CSS(Cascading Style Sheets),到今天我也没有记得他的全称,CSS还有一个中文名字是层叠式样式表,事实上翻译成什么可能并不是我们关心的内容,我们需要关心的是他能做些什么。作为三剑客之一,它的主要目的在于可以让我们方便灵活地去控制Web页面的外观表现。我们可以用它做出像淘宝一样复杂的界面,也可以像我们的书本一样简单,不过如果要和我们书本一样简单的话,可能不需要用到CSS。HTML一开始就是依照报纸的格式而设计的,我们还可以继续用上面说到的编辑器,又或者是其他的。如果你喜欢DreamWeaver那也不错,不过一开始使用IDE可无助于我们写出良好的代码。</p>
  756. <p>忘说了,CSS也是有版本的,和windows,Linux内核等等一样,但是更新可能没有那么频繁,HTML也是有版本的,JS也是有版本的,复杂的东西不是当前考虑的内容。</p>
  757. <h2 id="代码结构"><span class="header-section-number">4.3</span> 代码结构</h2>
  758. <p>对于我们的上面的Red示例来说,如果没有一个好的结构,那么以后可能就是这样子。</p>
  759. <div class="sourceCode"><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;/head&gt;</span>
  763. <span class="kw">&lt;body&gt;</span>
  764. <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>
  765. <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>
  766. <span class="kw">&lt;/body&gt;</span>
  767. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  768. <p>虽然我们看到的还是一样的:</p>
  769. <div class="figure">
  770. <img src="./images/nostyle.png" alt="No Style" />
  771. <p class="caption">No Style</p>
  772. </div>
  773. <p>于是我们就按各种书上的建议重新写了上面的代码</p>
  774. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  775. <span class="kw">&lt;html&gt;</span>
  776. <span class="kw">&lt;head&gt;</span>
  777. <span class="kw">&lt;title&gt;</span>CSS example<span class="kw">&lt;/title&gt;</span>
  778. <span class="kw">&lt;style</span><span class="ot"> type=</span><span class="st">&quot;text/css&quot;</span><span class="kw">&gt;</span>
  779. <span class="fl">.para</span><span class="kw">{</span>
  780. <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
  781. <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
  782. <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
  783. <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
  784. <span class="kw">}</span>
  785. <span class="fl">.para2</span><span class="kw">{</span>
  786. <span class="kw">font-size:</span><span class="dt">44px</span><span class="kw">;</span>
  787. <span class="kw">color:</span><span class="dt">#3ed</span><span class="kw">;</span>
  788. <span class="kw">text-indent:</span> <span class="dt">2em</span><span class="kw">;</span>
  789. <span class="kw">padding-left:</span> <span class="dt">2em</span><span class="kw">;</span>
  790. <span class="kw">}</span>
  791. <span class="kw">&lt;/style&gt;</span>
  792. <span class="kw">&lt;/head&gt;</span>
  793. <span class="kw">&lt;body&gt;</span>
  794. <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>
  795. <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>
  796. <span class="kw">&lt;/body&gt;</span>
  797. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  798. <p>总算比上面好看也好理解多了,这只是临时的用法,当文件太大的时候,正式一点的写法应该如下所示:</p>
  799. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  800. <span class="kw">&lt;html&gt;</span>
  801. <span class="kw">&lt;head&gt;</span>
  802. <span class="kw">&lt;title&gt;</span>CSS example<span class="kw">&lt;/title&gt;</span>
  803. <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>
  804. <span class="kw">&lt;/head&gt;</span>
  805. <span class="kw">&lt;body&gt;</span>
  806. <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>
  807. <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>
  808. <span class="kw">&lt;/body&gt;</span>
  809. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  810. <p>我们需要</p>
  811. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span>
  812. <span class="kw">&lt;html&gt;</span>
  813. <span class="kw">&lt;head&gt;</span>
  814. <span class="kw">&lt;title&gt;</span>CSS example<span class="kw">&lt;/title&gt;</span>
  815. <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>
  816. <span class="kw">&lt;/head&gt;</span>
  817. <span class="kw">&lt;body&gt;</span>
  818. <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>
  819. <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>
  820. <span class="kw">&lt;/body&gt;</span>
  821. <span class="kw">&lt;/html&gt;</span></code></pre></div>
  822. <p>然后我们有一个像app.js一样的style.css放在同目录下,而他的内容便是</p>
  823. <div class="sourceCode"><pre class="sourceCode css"><code class="sourceCode css"><span class="fl">.para</span><span class="kw">{</span>
  824. <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
  825. <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
  826. <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
  827. <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
  828. <span class="kw">}</span>
  829. <span class="fl">.para2</span><span class="kw">{</span>
  830. <span class="kw">font-size:</span><span class="dt">44px</span><span class="kw">;</span>
  831. <span class="kw">color:</span><span class="dt">#3ed</span><span class="kw">;</span>
  832. <span class="kw">text-indent:</span> <span class="dt">2em</span><span class="kw">;</span>
  833. <span class="kw">padding-left:</span> <span class="dt">2em</span><span class="kw">;</span>
  834. <span class="kw">}</span></code></pre></div>
  835. <p>这代码和JS的代码有如此多的相似</p>
  836. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para<span class="op">={</span>
  837. <span class="dt">font_size</span><span class="op">:</span><span class="st">&#39;22px&#39;</span><span class="op">,</span>
  838. <span class="dt">color</span><span class="op">:</span><span class="st">&#39;#f00&#39;</span><span class="op">,</span>
  839. <span class="dt">text_align</span><span class="op">:</span><span class="st">&#39;center&#39;</span><span class="op">,</span>
  840. <span class="dt">padding_left</span><span class="op">:</span><span class="st">&#39;20px&#39;</span><span class="op">,</span>
  841. <span class="op">}</span></code></pre></div>
  842. <p>而22px、20px以及#f00都是数值,因此:</p>
  843. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para<span class="op">={</span>
  844. <span class="dt">font_size</span><span class="op">:</span>22px<span class="op">,</span>
  845. <span class="dt">color</span><span class="op">:</span>#f00<span class="op">,</span>
  846. <span class="dt">text_align</span><span class="op">:</span>center<span class="op">,</span>
  847. <span class="dt">padding_left</span><span class="op">:</span>20px<span class="op">,</span>
  848. <span class="op">}</span> </code></pre></div>
  849. <p>目测差距已经尽可能的小了,至于这些话题会在以后讨论到,如果要让我们的编译器更正确的工作,那么我们就需要非常多这样的符号,除非你乐意去理解:</p>
  850. <div class="sourceCode"><pre class="sourceCode lisp"><code class="sourceCode commonlisp">(<span class="kw">dotimes</span> (i <span class="dv">4</span>) (<span class="kw">print</span> i))</code></pre></div>
  851. <p>总的来说我们减少了符号的使用,但是用lisp便带入了更多的括号,不过这是一种简洁的表达方式,也许我们可以在其他语言中看到。</p>
  852. <pre class="regex"><code>\d{2}/[A-Z][a-z][a-z]/\d{4}</code></pre>
  853. <p>上面的代码,是为了从一堆数据中找出“某日/某月/某年”。如果一开始不理解那是正则表达式,就会觉得那个很复杂。</p>
  854. <p>这门语言可能是为设计师而设计的,但是设计师大部分还是不懂编程的,不过相对来说这门语言还是比其他语言简单易懂一些。</p>
  855. <h2 id="样式与目标"><span class="header-section-number">4.4</span> 样式与目标</h2>
  856. <p>如下所示,就是我们的样式</p>
  857. <div class="sourceCode"><pre class="sourceCode css"><code class="sourceCode css"><span class="fl">.para</span><span class="kw">{</span>
  858. <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
  859. <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
  860. <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
  861. <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
  862. <span class="kw">}</span></code></pre></div>
  863. <p>我们的目标就是</p>
  864. <pre><code>如果没有一个好的结构</code></pre>
  865. <p>所以样式和目标在这里牵手了,问题是他们是如何在一起的呢?下面就是CSS与HTML沟通的重点所在了:</p>
  866. <h3 id="选择器"><span class="header-section-number">4.4.1</span> 选择器</h3>
  867. <p>我们用到的选择器叫做类选择器,也就是class,或者说应该称之为class选择器更合适。与类选择器最常一起出现的是ID选择器,不过这个适用于比较高级的场合,诸如用JS控制DOM的时候就需要用到ID选择器。而基本的选择器就是如下面的例子:</p>
  868. <pre><code>p.para{
  869. color:#f0f;
  870. }</code></pre>
  871. <p>将代码添加到style.css的最下面会发现“如果没有一个好的结构”变成了粉红色,当然我们还会有这样的写法</p>
  872. <pre><code>p&gt;.para{
  873. color:#f0f;
  874. }</code></pre>
  875. <p>为了产生上面的特殊的样式,虽然不好看,但是我们终于理解什么叫层叠样式了,下面的代码的重要度比上面高,也因此有更高的优先规则。</p>
  876. <p>而通常我们可以通过一个</p>
  877. <pre><code>p{
  878. text-align:left;
  879. }</code></pre>
  880. <p>这样的元素选择器来给予所有的p元素一个左对齐。</p>
  881. <p>还有复杂一点的复合型选择器,下面的是HTML文件</p>
  882. <pre><code>&lt;!DOCTYPE html&gt;
  883. &lt;html&gt;
  884. &lt;head&gt;
  885. &lt;title&gt;CSS example&lt;/title&gt;
  886. &lt;link href=&quot;./style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
  887. &lt;/head&gt;
  888. &lt;body&gt;
  889. &lt;p class=&quot;para&quot;&gt;如果没有一个好的结构&lt;/p&gt;
  890. &lt;div id=&quot;content&quot;&gt;
  891. &lt;p class=&quot;para2&quot;&gt;那么以后可能就是这样子。。。。&lt;/p&gt;
  892. &lt;/div&gt;
  893. &lt;/body&gt;
  894. &lt;/html&gt;</code></pre>
  895. <p>还有CSS文件</p>
  896. <pre><code>.para{
  897. font-size: 22px;
  898. color:#f00;
  899. text-align: center;
  900. padding-left: 20px;
  901. }
  902. .para2{
  903. font-size:44px;
  904. color:#3ed;
  905. text-indent: 2em;
  906. padding-left: 2em;
  907. }
  908. p.para{
  909. color:#f0f;
  910. }
  911. div#content p {
  912. font-size:22px;
  913. }</code></pre>
  914. <h2 id="更有趣的css"><span class="header-section-number">4.5</span> 更有趣的CSS</h2>
  915. <p>一个包含了para2以及para_bg的例子</p>
  916. <pre><code> &lt;div id=&quot;content&quot;&gt;
  917. &lt;p class=&quot;para2 para_bg&quot;&gt;那么以后可能就是这样子。。。。&lt;/p&gt;
  918. &lt;/div&gt;
  919. </code></pre>
  920. <p>我们只是添加了一个黑色的背景</p>
  921. <pre><code>.para_bg{
  922. background-color:#000;
  923. }</code></pre>
  924. <p>重新改变后的网页变得比原来有趣了很多,所谓的继承与合并就是上面的例子。</p>
  925. <p>我们还可以用CSS3做出更多有趣的效果,而这些并不在我们的讨论范围里面,因为我们讨论的是be a geek。</p>
  926. <p>或许我们写的代码都是那么的简单,从HTML到Javascript,还有现在的CSS,只是总有一些核心的东西,而不是去考虑那些基础语法,基础的东西我们可以在实践的过程中一一发现。但是我们可能发现不了,或者在平时的使用中考虑不到一些有趣的用法或者说特殊的用法,这时候可以通过观察一些精致设计的代码中学习到。复杂的东西可以变得很简单,简单的东西也可以变得很复杂。</p>
  927. <h1 id="无处不在的三剑客"><span class="header-section-number">5</span> 无处不在的三剑客</h1>
  928. <p>这时我们终于了解了我们的三剑客,他们也就这么可以结合到一起了,HTML+Javascript+CSS是这一切的基础。而我们用到的其他语言如PHP、Python、Ruby等等到最后都会变成上面的结果,当然还有Coffeescript之类的语言都是以此为基础,这才是我们需要的知识。</p>
  929. <h2 id="hellogeek"><span class="header-section-number">5.1</span> Hello,Geek</h2>
  930. <p>有了一些些基础之后,我们终于能试着去写一些程序了。也是时候去创建一个像样的东西,或许你在一些界面设计方面的书籍看过类似的东西,可能我写得也没有那些内容好,只是这些都是一些过程。过去我们都是一点点慢慢过来的,只是现在我们也是如此,技术上的一些东西,事实上大家都是知道的。就好比我们都觉得我们可以开个超市,但是如果让我们去开超市的话,我们并不一定能赚钱。</p>
  931. <p>学习编程的目的可能不在于我们能找到一份工作,那只是在编程之外的东西,虽然确实也是很确定的。但是除此之处,有些东西也是很重要的。</p>
  932. <p>过去总是不理解为什么会一些人会不厌其烦地去回答别人的问题,有时候可能会想是一种能力越大责任越大的感觉,但是有时候在写一些博客或者回答别人的问题的时候我们又重新思考了这些问题,又重新学习了这些技能。所以这里可能说的不是关于编程的东西而是一些编程以外的东西,关于学习或者学习以外的东西。</p>
  933. <h2 id="从源码学习"><span class="header-section-number">5.2</span> 从源码学习</h2>
  934. <p>过去总觉得学了一种语言的语法便算是学会了一种语言,直到有一天接触运用该语言的项目的时候,虽然也会写上几行代码,但是却不像这种语言的风格。于是这也是这一篇的意义所在了:</p>
  935. <h2 id="浏览器渲染过程"><span class="header-section-number">5.3</span> 浏览器渲染过程</h2>
  936. <p>基本的渲染引擎的过程如下图所示:</p>
  937. <div class="figure">
  938. <img src="./images/flow.png" alt="flow" />
  939. <p class="caption">flow</p>
  940. </div>
  941. <ul>
  942. <li>解析HTML去构建DOM树</li>
  943. <li>渲染树形结构</li>
  944. <li>生成渲染的树形图布局</li>
  945. <li>绘制树形图</li>
  946. </ul>
  947. <p>对于Webkit浏览器来说,他的过程如下所示:</p>
  948. <div class="figure">
  949. <img src="./images/webkitflow.png" alt="webkitflow" />
  950. <p class="caption">webkitflow</p>
  951. </div>
  952. <h3 id="html"><span class="header-section-number">5.3.1</span> HTML</h3>
  953. <p>写好HTML的一个要点在于读别人写的代码,这只是一方面,我们所说的HTML方面的内容可能不够多,原因有很多,很多东西都需要在实战中去解决。读万卷书和行万里路,分不清哪个有重要的意义,但是如果可以同时做好两个的话,成长会更快的。</p>
  954. <p>写好HTML应该会有下面的要点</p>
  955. <ul>
  956. <li>了解标准及遵守绝大多数标准</li>
  957. <li>注重可读性,从ID及CLASS的命名</li>
  958. <li>关注SEO与代码的联系</li>
  959. </ul>
  960. <p>或许在这方面我也算不上很了解,不过按笔者的经验来说,大致就是如此。</p>
  961. <p>多数情况下我们的HTML是类似于下面这样子的</p>
  962. <div class="sourceCode"><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>
  963. {% nevercache %}
  964. {% include &quot;includes/user_panel.html&quot; %}
  965. {% endnevercache %}
  966. <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>
  967. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;panel-body&quot;</span><span class="kw">&gt;</span>
  968. {% block right_panel %}
  969. {% ifinstalled mezzanine.twitter %}
  970. {% include &quot;twitter/tweets.html&quot; %}
  971. {% endifinstalled %}
  972. {% endblock %}
  973. <span class="kw">&lt;/div&gt;</span>
  974. <span class="kw">&lt;/div&gt;</span>
  975. <span class="kw">&lt;/div&gt;</span></code></pre></div>
  976. <p>换句话说HTML只是基础,而不是日常用到的。我们的HTML是由template生成的,我们可以借助于mustache.js又或者是angluarjs之类的js库来生成最后的HTML,所以这里只是一个开始。</p>
  977. <p>还需要了解的一部分就是HTML的另外一个重要的部分,DOM树形结构</p>
  978. <h2 id="dom树形结构图"><span class="header-section-number">5.4</span> DOM树形结构图</h2>
  979. <blockquote>
  980. <p>DOM是文档对象化模型(Document Object Model)的简称。DOM Tree是指通过DOM将HTML页面进行解析,并生成的HTML tree树状结构和对应访问方法。</p>
  981. </blockquote>
  982. <div class="figure">
  983. <img src="./images/dom_tree.jpg" alt="DOM Tree" />
  984. <p class="caption">DOM Tree</p>
  985. </div>
  986. <h3 id="javascript"><span class="header-section-number">5.4.1</span> javascript</h3>
  987. <p>这里以未压缩的jQuery源码和zepto.js作一个小小的比较,zepto.js是兼容jQuery的,因此我们举几个有意思的函数作一简单的比较,关于源码可以在官网上下载到。</p>
  988. <p>在zepto.js下面判断一个值是否是函数的方面如下,</p>
  989. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">isFunction</span>(value) <span class="op">{</span> <span class="cf">return</span> <span class="at">type</span>(value) <span class="op">==</span> <span class="st">&quot;function&quot;</span> <span class="op">}</span></code></pre></div>
  990. <p>而在jQuery下面则是这样的</p>
  991. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">isFunction<span class="op">:</span> <span class="kw">function</span>( obj ) <span class="op">{</span>
  992. <span class="cf">return</span> <span class="va">jQuery</span>.<span class="at">type</span>(obj) <span class="op">===</span> <span class="st">&quot;function&quot;</span><span class="op">;</span>
  993. <span class="op">}</span></code></pre></div>
  994. <p>而他们的用法是一样的,都是</p>
  995. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">$</span>.<span class="at">isFunction</span>()<span class="op">;</span></code></pre></div>
  996. <p>jQuery的作法是将诸如isFunction,isArray这些函数打包到jQuery.extend中,而zepto.js的也是这样的,只不过多了一行</p>
  997. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">$</span>.<span class="at">isFunction</span> <span class="op">=</span> isFunction</code></pre></div>
  998. <p>遗憾的是我也没去了解过为什么,之前我也没有看过这些库的代码,所以这个问题就要交给读者去解决了。jQuery里面提供了函数式编程接口,不过jQuery更多的是构建于CSS选择器之上,对于DOM的操作比javascript自身提供的功能强大得多。如果我们的目的在于更好的编程,那么可能需要诸如Underscore.js之类的库。或许说打包自己常用的函数功能为一个库,诸如jQuery</p>
  999. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">isFunction</span>(value) <span class="op">{</span> <span class="cf">return</span> <span class="at">type</span>(value) <span class="op">==</span> <span class="st">&quot;function&quot;</span> <span class="op">}</span>
  1000. <span class="kw">function</span> <span class="at">isWindow</span>(obj) <span class="op">{</span> <span class="cf">return</span> obj <span class="op">!=</span> <span class="kw">null</span> <span class="op">&amp;&amp;</span> obj <span class="op">==</span> <span class="va">obj</span>.<span class="at">window</span> <span class="op">}</span>
  1001. <span class="kw">function</span> <span class="at">isDocument</span>(obj) <span class="op">{</span> <span class="cf">return</span> obj <span class="op">!=</span> <span class="kw">null</span> <span class="op">&amp;&amp;</span> <span class="va">obj</span>.<span class="at">nodeType</span> <span class="op">==</span> <span class="va">obj</span>.<span class="at">DOCUMENT_NODE</span> <span class="op">}</span>
  1002. <span class="kw">function</span> <span class="at">isObject</span>(obj) <span class="op">{</span> <span class="cf">return</span> <span class="at">type</span>(obj) <span class="op">==</span> <span class="st">&quot;object&quot;</span> <span class="op">}</span></code></pre></div>
  1003. <p>我们需要去了解一些故事背后的原因,越来越害怕GUI的原因之一,在于不知道背后发生了什么,即使是开源的,我们也无法了解真正的背后发生什么了。对于不是这个工具、软件的用户来说,开源更多的意义可能在于我们可以添加新的功能,当然还有免费。如果没有所谓的危机感,以及认为自己一直在学习工具的话,可以试着去打包自己的函数,打包自己的库。</p>
  1004. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> calc<span class="op">={</span>
  1005. <span class="dt">add</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1006. <span class="cf">return</span> a<span class="op">+</span>b<span class="op">;</span>
  1007. <span class="op">},</span>
  1008. <span class="dt">sub</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1009. <span class="cf">return</span> a<span class="op">-</span>b<span class="op">;</span>
  1010. <span class="op">},</span>
  1011. <span class="dt">dif</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1012. <span class="cf">if</span>(a<span class="op">&gt;</span>b)<span class="op">{</span>
  1013. <span class="cf">return</span> a<span class="op">;</span>
  1014. <span class="op">}</span><span class="cf">else</span><span class="op">{</span>
  1015. <span class="cf">return</span> b<span class="op">;</span>
  1016. <span class="op">}</span>
  1017. <span class="op">}</span>
  1018. <span class="op">}</span></code></pre></div>
  1019. <p>然后用诸如jslint测试一下代码。</p>
  1020. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">./jsl</span> -conf jsl.default.conf
  1021. <span class="kw">JavaScript</span> Lint 0.3.0 (JavaScript-C 1.5 2004-09-24)
  1022. <span class="kw">Developed</span> by Matthias Miller (http://www.JavaScriptLint.com)
  1023. <span class="kw">app.js</span>
  1024. <span class="kw">/Users/fdhuang/beageek/chapter4/src</span>/<span class="kw">app.js</span>(15)<span class="kw">:</span> lint warning: missing semicolon
  1025. }
  1026. <span class="kw">........</span>^
  1027. <span class="kw">0</span> error(s), <span class="kw">1</span> warning(s)</code></pre></div>
  1028. <p>于是我们需要在第15行添加一个分号。</p>
  1029. <p>最好的方法还是阅读别人的代码,而所谓的别人指的是一些相对较大的网站的,有比较完善的开发流程,代码质量也不会太差。而所谓的复杂的代码都是一步步构建上去的,罗马不是一天建成的。</p>
  1030. <p>有意思的是多数情况下,我们可能会用原型去开发我们的应用,而这也是我们需要去了解和掌握的地方,</p>
  1031. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">Calc</span>()<span class="op">{</span>
  1032. <span class="op">}</span>
  1033. <span class="va">Calc</span>.<span class="va">prototype</span>.<span class="at">add</span><span class="op">=</span><span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1034. <span class="cf">return</span> a<span class="op">+</span>b<span class="op">;</span>
  1035. <span class="op">};</span>
  1036. <span class="va">Calc</span>.<span class="va">prototype</span>.<span class="at">sub</span><span class="op">=</span><span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1037. <span class="cf">return</span> a<span class="op">-</span>b<span class="op">;</span>
  1038. <span class="op">};</span></code></pre></div>
  1039. <p>我们似乎在这里展示了更多的Javascript的用法,但是这不是一好的关于Javascript的介绍,有一天我们还要用诸如qunit之类的工具去为我们的function写测试,这时就是一个更好的开始。</p>
  1040. <p>如果我们乐意的话,我们也可以构建一个类似于jQuery的框架,以用来学习。</p>
  1041. <p>作为一门编程语言来说,我们学得很普通,在某种意义上来说算不上是一种入门。但是如果我们可以在其他的好书在看到的内容,就没有必要在这里进行复述,目的在于一种学习习惯的养成。</p>
  1042. <h3 id="css-1"><span class="header-section-number">5.4.2</span> CSS</h3>
  1043. <p>CSS有时候很有趣,但是有时候有很多我们没有意识到的用法,这里以Bootstrap为例,这是一个不错的CSS库。最令人兴奋的是没有闭源的CSS,没有闭源的JS,这也就是前端好学习的地方所在了,不过这是一个开源的CSS库,虽然是这样叫的,但是称之为CSS库显然不合适。</p>
  1044. <div class="sourceCode"><pre class="sourceCode css"><code class="sourceCode css">a,
  1045. a<span class="dv">:visited</span> <span class="kw">{</span>
  1046. <span class="kw">text-decoration:</span> <span class="dt">underline</span><span class="kw">;</span>
  1047. <span class="kw">}</span>
  1048. a<span class="ch">[href]</span><span class="dv">:after</span> <span class="kw">{</span>
  1049. <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>
  1050. <span class="kw">}</span>
  1051. abbr<span class="ch">[title]</span><span class="dv">:after</span> <span class="kw">{</span>
  1052. <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>
  1053. <span class="kw">}</span>
  1054. a<span class="ch">[href^=</span><span class="st">&quot;javascript:&quot;</span><span class="ch">]</span><span class="dv">:after</span>,
  1055. 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>
  1056. <span class="kw">content:</span> <span class="st">&quot;&quot;</span><span class="kw">;</span>
  1057. <span class="kw">}</span></code></pre></div>
  1058. <p>这里有一些有趣的,值得一讲的CSS用法。</p>
  1059. <ul>
  1060. <li>伪类选择器,如a:visited这样需要其他条件来对元素应用样式,用于已访问的链接。</li>
  1061. <li>属性选择器,如a[href]这样当a元素存在href这样的属性的时候来寻找应用元素。</li>
  1062. </ul>
  1063. <p>其他的还需要去好好了解的就是<strong>CSS的盒模型</strong>,作为CSS的基石之一。</p>
  1064. <h2 id="css盒模型图"><span class="header-section-number">5.5</span> CSS盒模型图</h2>
  1065. <p>(ps:以下内容来自于Mozilla Developer NetWorks)</p>
  1066. <p>CSS下这些矩形盒子由标准盒模型描述。这个模型描述元素内容占用空间。盒子有四个边界:外边距边界margin edge, 边框边界border edge, 内边距边界padding edge 与 内容边界content edge。</p>
  1067. <div class="figure">
  1068. <img src="./images/box-model.gif" alt="CSS Box Model" />
  1069. <p class="caption">CSS Box Model</p>
  1070. </div>
  1071. <p>内容区域content area 是真正包含元素内容的区域。位于内容边界的内部,它的大小为内容宽度 或 content-box宽及内容高度或content-box高。</p>
  1072. <p>如果 box-sizing 为默认值, width, min-width, max-width, height, min-height 与 max-height 控制内容大小。</p>
  1073. <p>内边距区域padding area 用内容及可能的边框之间的空白区域扩展内容区域。它位于内边距边界内部,通常有背景——颜色或图片(不透明图片盖住背景颜色). 它的大小为 padding-box 宽与 padding-box 高。</p>
  1074. <p>内边距与内容边界之间的空间可以由 padding-top, padding-right, padding-bottom, padding-left 和简写属性 padding 控制。</p>
  1075. <p>边框区域border area 是包含边框的区域,扩展了内边距区域。它位于边框边界内部,大小为 border-box 宽和 border-box 高。由 border-width 及简写属性 border控制。</p>
  1076. <p>外边距区域margin area用空白区域扩展边框区域,以分开相邻的元素。它的大小为 margin-box 的高宽。</p>
  1077. <p>外边距区域大小由 margin-top, margin-right, margin-bottom, margin-left 及简写属性 margin 控制。</p>
  1078. <p>在 外边距合并 的情况下,由于盒之间共享外边距,外边距不容易弄清楚。</p>
  1079. <p>最后注意,对于行内非替换元素,其占用空间(行高)由 line-height 决定,即使有内边距与边框。</p>
  1080. <p>诸如</p>
  1081. <div class="sourceCode"><pre class="sourceCode css"><code class="sourceCode css">* <span class="kw">{</span>
  1082. <span class="kw">margin:</span> <span class="dt">0px</span><span class="kw">;</span>
  1083. <span class="kw">padding:</span> <span class="dt">0px</span><span class="kw">;</span>
  1084. <span class="kw">font-family:</span> Helvetica<span class="kw">;</span>
  1085. <span class="kw">}</span></code></pre></div>
  1086. <p>这样的通用器用来进行全局选择的工具和我们用于抵消某个body对于子选择器的影响一样值得注意得多。</p>
  1087. <h2 id="笔记"><span class="header-section-number">5.6</span> 笔记</h2>
  1088. <p>写博客似乎是一个不错的好习惯,作为一个不是很优秀的写手。对于来说,有时候发现原来能教会别人对于自己的能力来说算是一种肯定。有些时候教会别人才算是自己学会的表现,总会在项目上的时候需要自己去复述工作的一个过程,我们需要整理好我们的思路才能带给别人更多的收获。我们的笔记上总会留下自己的学习的一些过程,有些时候我们想要的只是一点点的鼓励,有时是诸如评论一类,有时可能是诸如访问量。更多的可能是我们可以重新整理自己的知识,好好复习一下,以便于好好记住,写出来是一个好的过程。</p>
  1089. <p>无处不在的三剑客就这样到了这里,写得似乎很多也很少,但是还是没有做出来一个东西,于是我们朝着这样一个方向前进。</p>
  1090. <h1 id="gnulinux-强大且free"><span class="header-section-number">6</span> GNU/Linux 强大且Free</h1>
  1091. <div class="figure">
  1092. <img src="./images/gnu_linux.png" alt="GNU/Linux" />
  1093. <p class="caption">GNU/Linux</p>
  1094. </div>
  1095. <h2 id="什么是linux"><span class="header-section-number">6.1</span> 什么是Linux</h2>
  1096. <p>Linux是一种自由和开放源码的类UNIX操作系统内核。目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机、平板电脑、路由器和影音游戏控制台,到桌上型电脑,大型电脑和超级电脑。 Linux是一个领先的操作系统内核,世界上运算最快的10台超级电脑运行的都是基于Linux内核的操作系统。</p>
  1097. <p>Linux操作系统也是自由软件和开放源代码发展中最著名的例子。只要遵循GNU通用公共许可证,任何人和机构都可以自由地使用Linux的所有底层源代码,也可以自由地修改和再发布。<strong>严格来讲,Linux这个词本身只表示Linux内核,但在实际上人们已经习惯了用Linux来形容整个基于Linux内核,并且使用GNU工程各种工具和数据库的操作系统(也被称为GNU/Linux)。</strong>通常情况下,Linux被打包成供桌上型电脑和服务器使用的Linux发行版本。一些流行的主流Linux发行版本,包括Debian(及其衍生版本Ubuntu),Fedora和openSUSE等。Linux得名于电脑业余爱好者Linus Torvalds。</p>
  1098. <p>而不是如百度百科所讲的<del>Linux操作系统是UNIX操作系统的一种克隆系统。它诞生于1991年的 Linux桌面[1]10 月5日(这是第一次正式向外公布的时间)。以后借助于Internet网络,并通过全世界各地计算机爱好者的共同努力,已成为今天世界上使用最多的一种UNIX类操作系统,并且使用人数还在迅猛增长。</del></p>
  1099. <p>Linux只是个内核,而不是操作系统,所以在这我们再理解一下操作系统是由什么组成的。</p>
  1100. <h2 id="操作系统"><span class="header-section-number">6.2</span> 操作系统</h2>
  1101. <p>操作系统(英语:Operating System,简称OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石。操作系统需要处理如管理与配置内存、决定系统资源供需的优先次序、控制输入与输出设备、操作网络与管理文件系统等基本事务。操作系统也提供一个让用户与系统交互的操作界面。 操作系统的型态非常多样,不同机器安装的操作系统可从简单到复杂,可从手机的嵌入式系统到超级计算机的大型操作系统。许多操作系统制造者对它涵盖范畴的定义也不尽一致,例如有些操作系统集成了图形用户界面(GUI),而有些仅使用命令行界面(CLI),而将GUI视为一种非必要的应用程序。</p>
  1102. <p>操作系统位于底层硬件与用户之间,是两者沟通的桥梁。用户可以通过操作系统的用户界面,输入命令。操作系统则对命令进行解释,驱动硬件设备,实现用户要求。以现代标准而言,一个标准PC的操作系统应该提供以下的功能:</p>
  1103. <ul>
  1104. <li>进程管理(Processing management)</li>
  1105. <li>内存管理(Memory management)</li>
  1106. <li>文件系统(File system)</li>
  1107. <li>网络通信(Networking)</li>
  1108. <li>安全机制(Security)</li>
  1109. <li>用户界面(User interface)</li>
  1110. <li>驱动程序(Device drivers)</li>
  1111. </ul>
  1112. <p>而让我们来看一下两者之间的不同之处,这是一张linux的架构图我们可以发现内核只是位于底层。</p>
  1113. <h3 id="linux架构图"><span class="header-section-number">6.2.1</span> Linux架构图</h3>
  1114. <div class="figure">
  1115. <img src="./images/linux_kernel.jpg" alt="Linux Kernel" />
  1116. <p class="caption">Linux Kernel</p>
  1117. </div>
  1118. <h4 id="用户模式"><span class="header-section-number">6.2.1.1</span> 用户模式</h4>
  1119. <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>
  1120. <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>
  1121. <p>C库(open、fopen、socket、exec、calloc等)</p>
  1122. <h4 id="内核模式"><span class="header-section-number">6.2.1.2</span> 内核模式</h4>
  1123. <ul>
  1124. <li>系统中断、调用、错误等软硬件消息</li>
  1125. <li>内核(驱动程序、进程、网络、内存管理等)</li>
  1126. <li>硬件(处理器、内存、各种设备)</li>
  1127. </ul>
  1128. <p>我们可以发现,由linux内核+shell可以构成一个操作系统,而linux本身只是个内核,也就是图中的内核模式,负责控制系统的这些部分。也就是我们可以发现,Linux内核构成了一个操作系统除用户界面以外的部分,而shell就是这最后的用户界面。</p>
  1129. <p>而linux内核以外的部分就是由GNU计划构成的。</p>
  1130. <h3 id="shell"><span class="header-section-number">6.2.2</span> Shell</h3>
  1131. <div class="figure">
  1132. <img src="./images/shell.png" alt="Shell" />
  1133. <p class="caption">Shell</p>
  1134. </div>
  1135. <p>Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。</p>
  1136. <p>实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果</p>
  1137. <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>
  1138. <p>shell将会是我们在GNU/linux中经常用到的经常有到的工具之一,用来操作计算机用的。在迁移到linux之前我们可以试用cygwin来进行模拟:</p>
  1139. <p>Cygwin是许多自由软件的集合,最初由Cygnus Solutions开发,用于各种版本的Microsoft Windows上,运行<code>UNIX类</code>系统。</p>
  1140. <h3 id="gcc"><span class="header-section-number">6.2.3</span> GCC</h3>
  1141. <p>GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器。它是一套以GPL及LGPL许可证所发行的自由软件,也是GNU计划的关键部分,亦是自由的类Unix及苹果电脑Mac OS X 操作系统的标准编译器。GCC(特别是其中的C语言编译器)也常被认为是跨平台编译器的事实标准。</p>
  1142. <p>GCC原名为GNU C语言编译器(GNU C Compiler),因为它原本只能处理C语言。GCC很快地扩展,变得可处理C++。之后也变得可处理Fortran、Pascal、Objective-C、Java、Ada,以及Go与其他语言。</p>
  1143. <div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="ot">#include &lt;stdio.h&gt;</span>
  1144. main()
  1145. {
  1146. printf(<span class="st">&quot;Hello world</span><span class="ch">\n</span><span class="st">&quot;</span>);
  1147. }</code></pre></div>
  1148. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">~/temp</span>/<span class="kw">free&gt;</span> gcc hello.c -o hello
  1149. <span class="kw">hello.c</span>:2:1: warning: type specifier missing, defaults to <span class="st">&#39;int&#39;</span> [-Wimplicit-int]
  1150. <span class="fu">main()</span>
  1151. ^<span class="kw">~~~</span>
  1152. <span class="kw">1</span> warning generated.
  1153. <span class="kw">~/temp</span>/<span class="kw">free&gt;</span> ./hello
  1154. <span class="kw">Hello</span> world</code></pre></div>
  1155. <p>同shell一样,对于GNU/linux系统而言,GCC的作用也是无可取代的。当然如果只是一般用途的话,GCC对于一般用户可能没用,但是在些GNU/Linux系统上,我们可能就需要自己编译源码成二进制文件,而没有软件包,因而其重要性是不言而喻的。自然的如果我们自己动手编译GNU/Linux操作系统的话,我们会理解其的重要意义。有兴趣的同学可以试一下:Linux From Scratch (LFS)。</p>
  1156. <h3 id="启动引导程序"><span class="header-section-number">6.2.4</span> 启动引导程序</h3>
  1157. <p>最后,当我们构成以上的那些之后,我们就需要一个引导程序,以便使系统启动,引导进内核。</p>
  1158. <p>启动程序(bootloader)于电脑或其他计算机应用上,是指引导操作系统启动的程序。启动程序启动方式与程序视应用机型种类。例如在普通PC上,引导程序通常分为两部分:第一阶段引导程序位于主引导记录,用于引导位于某个分区上的第二阶段引导程序,如NTLDR、GNU GRUB等。</p>
  1159. <p>BIOS 开机完成后,bootloader就接手初始化硬件设备、创建存储器空间的映射,以便为操作系统内核准备好</p>
  1160. <p>正确的软硬件环境。</p>
  1161. <p>简单的bootloader的虚拟汇编码,如其后的八个指令:</p>
  1162. <ul>
  1163. <li>0: 将P暂存器的值设为8</li>
  1164. <li>1: 检查纸带({paper tape)读取器,是否已经可以进行读取</li>
  1165. <li>2: 如果还不能进行读取, 跳至1</li>
  1166. <li>3: 从纸带读取器,读取一byte至累加器</li>
  1167. <li>4: 如为带子结尾,跳至8</li>
  1168. <li>5: 将暂存器的值,存储至P暂存器中的数值所指定的地址</li>
  1169. <li>6: 增加P暂存器的值</li>
  1170. <li>7: 跳至1</li>
  1171. </ul>
  1172. <p>但是随着计算机操作系统越来越复杂,位于MBR的空间已经放不下引导操作系统的代码,于是就有了第二阶段的引导程序,而MBR中代码的功能也从直接引导操作系统变成了引导第二阶段的引导程序。</p>
  1173. <p>通常在一个GNU/Linux系统中选用GNUGRUB做为引导程序,例如Ubuntu就是用GRUB2。</p>
  1174. <p>GNU GRUB(简称“GRUB”)是一个来自GNU项目的启动引导程序。GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统。GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数。</p>
  1175. <p>GNU GRUB的前身为Grand Unified Bootloader。它主要用于类Unix系统;同大多Linux发行版一样,GNU系统也采用GNU GRUB作为它的启动器。Solaris从10 1/06版开始在x86系统上也采用GNU GRUB作为启动器。</p>
  1176. <p>以上也就构成了一个简单的操作系统。</p>
  1177. <h2 id="从编译开始"><span class="header-section-number">6.3</span> 从编译开始</h2>
  1178. <p>我们以一次编译开始我们的Linux学习之旅。</p>
  1179. <h3 id="开始之前"><span class="header-section-number">6.3.1</span> 开始之前</h3>
  1180. <ul>
  1181. <li>如果你没有用过GNU/Linux,我想你需要在虚拟机上安装一个。</li>
  1182. <li>一个主流的GNU/Linux发行版,如Ubuntu,CentOS,Debian,Mint,OpenSUSE,Fedora等等。</li>
  1183. <li>学会如何打开shell(ps:bash,zsh,sh等等)。</li>
  1184. </ul>
  1185. <p>或者你也可以在Windows上安装Cygwin。</p>
  1186. <h3 id="编译nginx"><span class="header-section-number">6.3.2</span> 编译Nginx</h3>
  1187. <p>1.下载这个软件的源码包</p>
  1188. <div class="sourceCode"><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></div>
  1189. <p>wget是一个用于下载的软件,当然你也可以用软件,只是用wget似乎会比图形界面快哦。</p>
  1190. <p>2.解压软件包</p>
  1191. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">tar</span> -vf nginx-1.7.4.tar.gz</code></pre></div>
  1192. <p>-vf的意思是Extract,也就是解压,而tar则是这个解压软件的名字。看上去似乎比WinRAR来得复制得多,但是你可以计时一下,从下载到解压完,和你用鼠标比哪个比较快。</p>
  1193. <p>3.到nginx目录下</p>
  1194. <p>这里需要分两部进行</p>
  1195. <p>1).列出所有文件</p>
  1196. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">ls</span> -al
  1197. <span class="kw">drwxr-xr-x</span> 15 fdhuang staff 510B Sep 2 13:44 nginx-1.7.4
  1198. <span class="kw">-rw-r--r--</span> 1 fdhuang staff 798K Aug 5 21:55 nginx-1.7.4.tar.gz</code></pre></div>
  1199. <p>2).到nginx-1.7.4目录</p>
  1200. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">cd</span> nginx-1.7.4 </code></pre></div>
  1201. <p>4.配置nginx</p>
  1202. <p>一次简单的配置如下</p>
  1203. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./configure</span></code></pre></div>
  1204. <p>当你熟练以后,你可能和我一样用下面的配置(<code>注意</code>:用下面的代码会出错。)</p>
  1205. <div class="sourceCode"><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></div>
  1206. <p>过程中可能会提示你其中出了多少错误,而这时你便可以很愉快地去用搜索引擎搜索他们。</p>
  1207. <p>5.make</p>
  1208. <p>这里就会用到GCC等等。</p>
  1209. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">make</span></code></pre></div>
  1210. <p>6.运行</p>
  1211. <p>如果运行顺利的话,应该可以直接</p>
  1212. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./objs/nginx</span> </code></pre></div>
  1213. <h3 id="其他-1"><span class="header-section-number">6.3.3</span> 其他</h3>
  1214. <p>1.如果没有wget,make,gcc等命令的时候可以用类似于下面的方法安装,</p>
  1215. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> apt-get install gcc,make,wget</code></pre></div>
  1216. <p>2.正常情况下一个开源项目都会有一个README,会告诉你应该如何去做。</p>
  1217. <h2 id="包管理"><span class="header-section-number">6.4</span> 包管理</h2>
  1218. <p>GNU/Linux最方便的东西莫过于包管理了。</p>
  1219. <p>引自OpenSUSE官网的说明及图片<a href="#fn1" class="footnoteRef" id="fnref1"><sup>1</sup></a></p>
  1220. <div class="figure">
  1221. <img src="http://ebook.designiot.cn/images/pm.png" alt="package management" />
  1222. <p class="caption">package management</p>
  1223. </div>
  1224. <ol style="list-style-type: decimal">
  1225. <li><p>Linux 发行版无非就是一堆软件包 (package) 形式的应用程序加上整体地管理这些应用程序的工具。通常这些 Linux 发行版,包括 openSUSE,都是由成千上万不同的软件包构成的。</p></li>
  1226. <li><p>软件包: 软件包不止是一个文件,内含构成软件的所有文件,包括程序本身、共享库、开发包以及使用说明等。</p></li>
  1227. <li><p>元数据 (metadata) 包含于软件包之中,包含软件正常运行所需要的一些信息。软件包安装之后,其元数据就存储于本地的软件包数据库之中,以用于软件包检索。</p></li>
  1228. <li><p>依赖关系 (dependencies) 是软件包管理的一个重要方面。实际上每个软件包都会涉及到其他的软件包,软件包里程序的运行需要有一个可执行的环境(要求有其他的程序、库等),软件包依赖关系正是用来描述这种关系的。</p></li>
  1229. </ol>
  1230. <p>Linux 下的软件包通常是以下三种格式:</p>
  1231. <ul>
  1232. <li>tgz - tar gzip 文件。这类文件是基本的压缩软件包,可以容纳软件包维护者认为有用的所有的东西。此格式除本身的压缩格式外,并没有有关软件包内容的标准。</li>
  1233. <li>deb - 此格式的软件包常用于 Debian 系统,是标准的 Debian 软件包格式。</li>
  1234. <li>rpm - 此格式由 Red Hat Linux 所创建,并经由 LSB 标准化,现已为众多 Linux 发行版所采用,是一个优秀的软件包格式。openSUSE 即是用此格式。更多信息可以参阅此处。</li>
  1235. </ul>
  1236. <blockquote>
  1237. <p>所以这就需要能自动解决依赖关系的软件包管理器。软件包管理系统就是一个工具集,为系统提供一个统一的安装、升级、删除软件的方式。</p>
  1238. </blockquote>
  1239. <h2 id="ubuntu-lnmp"><span class="header-section-number">6.5</span> Ubuntu LNMP</h2>
  1240. <p>在余下的章节中,我们需要去部署,需要去使用Ubuntu。如果在Windows下可以使用LAMP,但是在这里我们只说Ubuntu。开始之前你需要安装好Ubuntu,无论是在虚拟机,还是在真机安装,或者是Docker。</p>
  1241. <h3 id="update软件包列表"><span class="header-section-number">6.5.1</span> Update软件包列表</h3>
  1242. <p><code>apt-get</code>是debian,ubuntu发行版的包管理工具。<code>apt-get update</code>可以确保我们的软件包列表是最新的,下面是一个简单的更新过程。</p>
  1243. <p>打开Terminal或者Konsole等等之类的终端控制台。</p>
  1244. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get update
  1245. <span class="kw">Ign</span> http://archive.ubuntu.com trusty InRelease
  1246. <span class="kw">Ign</span> http://archive.ubuntu.com trusty-updates InRelease
  1247. <span class="kw">Ign</span> http://archive.ubuntu.com trusty-security InRelease
  1248. <span class="kw">Ign</span> http://archive.ubuntu.com trusty-proposed InRelease
  1249. <span class="kw">Get</span>:1 http://archive.ubuntu.com trusty Release.gpg [933 B]
  1250. <span class="kw">Get</span>:2 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
  1251. <span class="kw">Get</span>:3 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
  1252. <span class="kw">Get</span>:4 http://archive.ubuntu.com trusty-proposed Release.gpg [933 B]
  1253. <span class="kw">Get</span>:5 http://archive.ubuntu.com trusty Release [58.5 kB]
  1254. <span class="kw">Get</span>:6 http://archive.ubuntu.com trusty-updates Release [62.0 kB]
  1255. <span class="kw">Get</span>:7 http://archive.ubuntu.com trusty-security Release [62.0 kB]
  1256. <span class="kw">Get</span>:8 http://archive.ubuntu.com trusty-proposed Release [209 kB]
  1257. <span class="kw">Get</span>:9 http://archive.ubuntu.com trusty/main Sources [1335 kB]
  1258. <span class="kw">Get</span>:10 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
  1259. <span class="kw">Get</span>:11 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
  1260. <span class="kw">Get</span>:12 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
  1261. <span class="kw">Get</span>:13 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
  1262. <span class="kw">Get</span>:14 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
  1263. <span class="kw">64%</span> [14 Packages 664 kB/7589 kB 9%] 58.3 kB/s 1min 58s</code></pre></div>
  1264. <p>更新完应该会显示:</p>
  1265. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Fetched</span> 20.5 MB in 5min 22s (63.6 kB/s)
  1266. <span class="kw">Reading</span> package lists... Done</code></pre></div>
  1267. <h3 id="安装mysql"><span class="header-section-number">6.5.2</span> 安装MySQL</h3>
  1268. <p>安装命令</p>
  1269. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> apt-get install mysql-server php5-mysql</code></pre></div>
  1270. <p>过程:</p>
  1271. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get install mysql-server php5-mysql
  1272. <span class="kw">Reading</span> package lists... 0%
  1273. <span class="kw">Reading</span> package lists... Done
  1274. <span class="kw">Building</span> dependency tree
  1275. <span class="kw">Reading</span> state information... Done
  1276. <span class="kw">The</span> following extra packages will be installed:
  1277. <span class="kw">libaio1</span> libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  1278. <span class="kw">libterm-readkey-perl</span> libwrap0 lsof mysql-client-5.5 mysql-client-core-5.5
  1279. <span class="kw">mysql-common</span> mysql-server-5.5 mysql-server-core-5.5 php5-common php5-json
  1280. <span class="kw">psmisc</span> tcpd
  1281. <span class="kw">Suggested</span> packages:
  1282. <span class="kw">libclone-perl</span> libmldbm-perl libnet-daemon-perl libplrpc-perl
  1283. <span class="kw">libsql-statement-perl</span> libipc-sharedcache-perl tinyca mailx php5-user-cache
  1284. <span class="kw">The</span> following NEW packages will be installed:
  1285. <span class="kw">libaio1</span> libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  1286. <span class="kw">libterm-readkey-perl</span> libwrap0 lsof mysql-client-5.5 mysql-client-core-5.5
  1287. <span class="kw">mysql-common</span> mysql-server mysql-server-5.5 mysql-server-core-5.5 php5-common
  1288. <span class="kw">php5-json</span> php5-mysql psmisc tcpd
  1289. <span class="kw">0</span> upgraded, 19 newly installed, 0 to remove and 12 not upgraded.
  1290. <span class="kw">Need</span> to get 9982 kB of archives.
  1291. <span class="kw">After</span> this operation, 99.1 MB of additional disk space will be used.
  1292. <span class="kw">Get</span>:1 http://archive.ubuntu.com/ubuntu/ trusty/main libaio1 amd64 0.3.109-4 [6364 B]
  1293. <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]
  1294. <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]
  1295. <span class="kw">Get</span>:4 http://archive.ubuntu.com/ubuntu/ trusty/main libwrap0 amd64 7.6.q-25 [46.2 kB]
  1296. <span class="kw">Get</span>:5 http://archive.ubuntu.com/ubuntu/ trusty/main libdbi-perl amd64 1.630-1 [879 kB]
  1297. <span class="kw">Get</span>:6 http://archive.ubuntu.com/ubuntu/ trusty/main libdbd-mysql-perl amd64 4.025-1 [99.3 kB]
  1298. <span class="kw">Get</span>:7 http://archive.ubuntu.com/ubuntu/ trusty/main libterm-readkey-perl amd64 2.31-1 [27.4 kB]
  1299. <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]
  1300. <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]
  1301. <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]
  1302. <span class="kw">47%</span> [10 mysql-server-core-5.5 850 kB/3215 kB 26%] 79.9 kB/s 1min 6s</code></pre></div>
  1303. <p>在安装的过程中会要求你输入数据库密码。(默认为空)</p>
  1304. <h3 id="安装nginx"><span class="header-section-number">6.5.3</span> 安装Nginx</h3>
  1305. <div class="sourceCode"><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
  1306. <span class="kw">sudo</span> apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
  1307. <span class="kw">sudo</span> apt-get update
  1308. <span class="kw">sudo</span> apt-get install nginx</code></pre></div>
  1309. <p>启动Nginx Server</p>
  1310. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> service nginx start</code></pre></div>
  1311. <h3 id="安装php"><span class="header-section-number">6.5.4</span> 安装PHP</h3>
  1312. <p>sudo apt-get install php5-fpm</p>
  1313. <p>安装过程</p>
  1314. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get install php5-fpm
  1315. <span class="kw">Reading</span> package lists... Done
  1316. <span class="kw">Building</span> dependency tree
  1317. <span class="kw">Reading</span> state information... Done
  1318. <span class="kw">The</span> following extra packages will be installed:
  1319. <span class="kw">libsystemd-daemon0</span>
  1320. <span class="kw">Suggested</span> packages:
  1321. <span class="kw">php-pear</span>
  1322. <span class="kw">The</span> following NEW packages will be installed:
  1323. <span class="kw">libsystemd-daemon0</span> php5-fpm
  1324. <span class="kw">0</span> upgraded, 2 newly installed, 0 to remove and 12 not upgraded.
  1325. <span class="kw">Need</span> to get 2201 kB of archives.
  1326. <span class="kw">After</span> this operation, 9326 kB of additional disk space will be used.
  1327. <span class="kw">Do</span> you want to continue? [Y/n] y
  1328. <span class="kw">Get</span>:1 http://archive.ubuntu.com/ubuntu/ trusty-proposed/main libsystemd-daemon0 amd64 204-5ubuntu20.8 [9608 B]
  1329. <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]
  1330. <span class="kw">Fetched</span> 2201 kB in 1min 5s (33.6 kB/s)
  1331. <span class="kw">Selecting</span> previously unselected package libsystemd-daemon0:amd64.
  1332. <span class="kw">(Reading</span> database ... 13105 files and directories currently installed.<span class="kw">)</span>
  1333. <span class="kw">Preparing</span> to unpack .../libsystemd-daemon0_204-5ubuntu20.8_amd64.deb ...
  1334. <span class="kw">Unpacking</span> libsystemd-daemon0:amd64 (204-5ubuntu20.8) <span class="kw">...</span>
  1335. <span class="kw">Selecting</span> previously unselected package php5-fpm.
  1336. <span class="kw">Preparing</span> to unpack .../php5-fpm_5.5.9+dfsg-1ubuntu4.5_amd64.deb ...
  1337. <span class="kw">Unpacking</span> php5-fpm (5.5.9+dfsg-1ubuntu4.5) <span class="kw">...</span>
  1338. <span class="kw">Processing</span> triggers for ureadahead (0.100.0-16) <span class="kw">...</span>
  1339. <span class="kw">Setting</span> up libsystemd-daemon0:amd64 (204-5ubuntu20.8) <span class="kw">...</span>
  1340. <span class="kw">Setting</span> up php5-fpm (5.5.9+dfsg-1ubuntu4.5) <span class="kw">...</span>
  1341. <span class="kw">Creating</span> config file /etc/php5/fpm/php.ini with new version
  1342. <span class="kw">php5_invoke</span>: Enable module pdo for fpm SAPI
  1343. <span class="kw">php5_invoke</span>: Enable module pdo_mysql for fpm SAPI
  1344. <span class="kw">php5_invoke</span>: Enable module opcache for fpm SAPI
  1345. <span class="kw">php5_invoke</span>: Enable module json for fpm SAPI
  1346. <span class="kw">php5_invoke</span>: Enable module mysql for fpm SAPI
  1347. <span class="kw">php5_invoke</span>: Enable module mysqli for fpm SAPI
  1348. <span class="kw">invoke-rc.d</span>: policy-rc.d denied execution of start.
  1349. <span class="kw">Processing</span> triggers for libc-bin (2.19-0ubuntu6.3) <span class="kw">...</span>
  1350. <span class="kw">Processing</span> triggers for ureadahead (0.100.0-16) <span class="kw">...</span></code></pre></div>
  1351. <h1 id="arduino-极客的玩具"><span class="header-section-number">7</span> Arduino 极客的玩具</h1>
  1352. <h2 id="极客的玩具"><span class="header-section-number">7.1</span> 极客的玩具</h2>
  1353. <p>Arduino,是一个开放源代码的单芯片微电脑,它使用了Atmel AVR单片机,采用了基于开放源代码的软硬件平台,构建于开放源代码 simple I/O 接口板,并且具有使用类似Java,C 语言的Processing/Wiring开发环境。</p>
  1354. <p>Arduino开发板封装了常用的库到开发环境中,可以让用户在开发产品时,将主要注意力放置于所需要实现的功能上,而不是开发的过程中。在为Arduino写串口程序时,我们只需要用Serial.begin(9600)以9600的速率初始化串口,而在往串口发送数据时,可以用Serial.write(‘1’)的方式向串口发送字串’1’。</p>
  1355. <p>Arduino的出现很大程度上降低了电子制作的难度,初学者甚至不懂编程也可以上手Arduino,这也是它的魅力所在。</p>
  1356. <h2 id="硬件熟悉"><span class="header-section-number">7.2</span> 硬件熟悉</h2>
  1357. <p>为了满足各种需求,Arduino团队设计了很多款开发板,如UNO、Pro mini、Mega2560、Due、Leonardo、Yún、Pro、Fio、Nano等十几种 开发板和扩展板。最适合初学者的一款是Arduino UNO 。下图是Arduino UNO 的外观图:</p>
  1358. <div class="figure">
  1359. <img src="http://ebook.designiot.cn/images/uno.png" alt="UNO" />
  1360. <p class="caption">UNO</p>
  1361. </div>
  1362. <p>注:后面的程序也是基于Arduino UNO开发板来讲解。</p>
  1363. <h2 id="开发环境"><span class="header-section-number">7.3</span> 开发环境</h2>
  1364. <div class="figure">
  1365. <img src="http://ebook.designiot.cn/images/arduino.png" alt="Arduino" />
  1366. <p class="caption">Arduino</p>
  1367. </div>
  1368. <p>开发环境如上图,十分简洁,编写代码需要知道两个基本的函数:</p>
  1369. <div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">void</span> setup(){
  1370. }
  1371. <span class="dt">void</span> loop(){
  1372. }</code></pre></div>
  1373. <p><code>setup()</code>函数用于初始化(如GPIO初始化,串口初始化,定时器初始化等)特点是只执行一次;<code>loop()</code>函数是一个死循环,可以看做C语言的<code>while(1)</code>函数。</p>
  1374. <h2 id="点亮一个led"><span class="header-section-number">7.4</span> 点亮一个LED</h2>
  1375. <p>对初学者来说,点亮led已成为入门必修课,使用Arduino控制led十分简单,并且很容易理解。 使用到的函数:</p>
  1376. <ul>
  1377. <li>pinMode(pin,mode)</li>
  1378. <li>digitalWrite(pin,value)</li>
  1379. </ul>
  1380. <p>上一段代码分析:</p>
  1381. <div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">int</span> led=<span class="dv">13</span>;
  1382. <span class="dt">void</span> setup()
  1383. {
  1384. pinMode(led,OUTPUT);
  1385. }
  1386. <span class="dt">void</span> loop()
  1387. {
  1388. digitalWrite(led,HIGH);
  1389. delay(<span class="dv">1000</span>);
  1390. digitalWrite(led,LOW);
  1391. delay(<span class="dv">1000</span>);
  1392. }</code></pre></div>
  1393. <p>该程序实现Arduino单片机13号引脚以1S时间电平翻转,如果外接一个led,就可以看到led以1S的间隔闪烁;函数<code>pinMode()</code>有两个参数pin、value,pin参数用来指定引脚号,本程序中设置为13号引脚,mode用于设置引脚模式,有三个值:</p>
  1394. <ul>
  1395. <li><code>INPUT</code></li>
  1396. <li><code>OUTPUT</code></li>
  1397. <li><code>INPUT_PULLUP</code></li>
  1398. </ul>
  1399. <p>表示让某一个IO引脚作输入,反之,</p>
  1400. <ul>
  1401. <li><code>OUTPUT</code>则使一个IO引脚做输出</li>
  1402. <li><code>INPUT_PULLUP</code>则配置一个IO引脚具有上拉输入功能(上拉电阻的目的是为了保证在无信号输入时输入端的电平为高电平),从英文意思也能很直观的看出来。</li>
  1403. </ul>
  1404. <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>
  1405. <p>如果你仔细查看我的描述,你会发现我没有讲13号引脚怎么来的,是这样的:Arduino团队为了简化对引脚描述,对每个引脚都进行了编号,以UNO开发板为例,可以发现开发板排座的附近有对应的白颜色的数字,那便是所有的引脚编号,A0~A5是6路ADC输入引脚,0-13表示13路基本IO,数字前面的<code>~</code>表示该引脚具有PWM功能。如果要使用某一引脚,只需要知道引脚编号就可编写相应代码进行操作。</p>
  1406. <p>例如<code>digitalWrite(2,LOW)</code>表示向2号引脚输出低电平。其他操作类似,是不是so easy <sup>-</sup> !</p>
  1407. <h2 id="串口通信"><span class="header-section-number">7.5</span> 串口通信</h2>
  1408. <p>使用到的基本函数:</p>
  1409. <ul>
  1410. <li>Serial.begin()</li>
  1411. <li>Serial.write()</li>
  1412. <li>Serial.read()</li>
  1413. <li>Serial.available()</li>
  1414. </ul>
  1415. <p>在此项目中需要使用串口,Arduino串口初始化使用<code>Serial.begin(9600);</code>,其传输波特率为9600,其他波特率也行,函数位于<code>setup()</code>中,之后可以使用<code>Serial.read()</code>、<code>Serial.write()</code>读入一个字符,输出一个字符,使用<code>Serial.print()</code>输出字符串.代码如下:</p>
  1416. <div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">char</span> ch=<span class="st">&#39;1&#39;</span>;
  1417. <span class="dt">void</span> setup()
  1418. {
  1419. Serial.begin(<span class="dv">9600</span>);
  1420. }
  1421. <span class="dt">void</span> loop()
  1422. {
  1423. Serial.write(ch);
  1424. <span class="kw">while</span>(<span class="dv">1</span>)
  1425. {
  1426. <span class="kw">if</span>(Serial.available())
  1427. {
  1428. ch = Serial.read();
  1429. Serial.print(ch);
  1430. }
  1431. }
  1432. }</code></pre></div>
  1433. <p>以上程序实现字符的输出(Serial.write(),Serial.print())和读入(Serial.read())。如果需要了解更多,可以参考:<a href="www.arduino.cc">Arduino官网</a></p>
  1434. <h3 id="关于arduino-setup"><span class="header-section-number">7.5.1</span> 关于Arduino Setup()</h3>
  1435. <p>如果你对Arduino的Setup很疑惑的话,可以看看这里。下面Arduino源码目录中的main函数:</p>
  1436. <div class="sourceCode"><pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="ot">#include &lt;Arduino.h&gt;</span>
  1437. <span class="dt">int</span> main(<span class="dt">void</span>)
  1438. {
  1439. init();
  1440. setup();
  1441. <span class="kw">for</span> (;;) {
  1442. loop();
  1443. <span class="kw">if</span> (serialEventRun) serialEventRun();
  1444. }
  1445. <span class="kw">return</span> <span class="dv">0</span>;
  1446. }</code></pre></div>
  1447. <div class="figure">
  1448. <img src="./images/hwcnt.png" alt="hwcnt" />
  1449. <p class="caption">hwcnt</p>
  1450. </div>
  1451. <h1 id="python-代码如散文"><span class="header-section-number">8</span> Python 代码如散文</h1>
  1452. <div class="figure">
  1453. <img src="./images/python.png" alt="python" />
  1454. <p class="caption">python</p>
  1455. </div>
  1456. <p>作为一门计算机语言来说,Python会有下面的特点。</p>
  1457. <ul>
  1458. <li>语言学习起来容易</li>
  1459. <li>解决生活中的实际问题</li>
  1460. <li>支持多学科</li>
  1461. </ul>
  1462. <p>我们可以和其他不是脚本语言的语言进行一个简单的对比,如C,你需要去编译去运行,有时候还需要解决跨平台问题,本来你是在你的Windows上运行得好好的,但是有一天你换了一个Mac电脑的时候,问题变得很棘手,你甚至不知道怎么去解决问题。我没有用过MFC,听说很方便,但是在其他平台下就没有一个好的解决方案。这里可能跑得有点远,但是不同的用户可能在不同的平台上,这也就是脚本语言的优势所在了。</p>
  1463. <h2 id="代码与散文"><span class="header-section-number">8.1</span> 代码与散文</h2>
  1464. <p>你可能听过,也可能了解过,不过在这里我们可能不会去讲述那些基础的语法的东西,我们想说的是代码格式的重要性,在html中你可以这样去写你的代码</p>
  1465. <div class="sourceCode"><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
  1466. <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>
  1467. <span class="kw">&lt;p&gt;</span>flakjfaklfjalfa<span class="kw">&lt;/p&gt;&lt;/div&gt;</span>
  1468. <span class="kw">&lt;/body&gt;&lt;/html&gt;</span></code></pre></div>
  1469. <p>又或者是js的minify,它可能会使你的代码看起来像是这样的:</p>
  1470. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">NolTracker</span>(b<span class="op">,</span>a)<span class="op">{</span><span class="kw">this</span>.<span class="at">pvar</span><span class="op">=</span>b<span class="op">;</span><span class="kw">this</span>.<span class="at">mergeFeatures</span>(a)<span class="op">}</span></code></pre></div>
  1471. <p>可能的是如果是python的话,你可能会遇到下面的问题。。</p>
  1472. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">File</span> <span class="st">&quot;steps.py&quot;</span>, line 10
  1473. <span class="kw">try</span>:
  1474. ^
  1475. <span class="kw">IndentationError</span>: expected an indented block</code></pre></div>
  1476. <p>如果你对JSLint、Lint这类的工具有点印象的话,你也可以认为python集成了这类工具。整洁的代码至少应该看上去要有整洁的衣服,就好像是我们看到的一个人一样,而后我们才会有一个好的印象。更主要的一点是代码是写给人看的,而衣服更多的时候对于像我这样的人来说,他首先应该是要保暖的,其次对于一个懒的人来说。。。</p>
  1477. <blockquote>
  1478. 程序应该是具有可读性的短文,它将在计算机上执行,从而解决某些问题
  1479. </blockquote>
  1480. <p>我们需要去读懂别人的代码,别人也需要去读懂我们的代码。计算机可以无条件地执行你那未经编排过的程序,但是人就不是如此了。</p>
  1481. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"> <span class="kw">var</span> calc<span class="op">={</span><span class="dt">add</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span><span class="cf">return</span> a<span class="op">+</span>b<span class="op">;},</span><span class="dt">sub</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span><span class="cf">return</span> a<span class="op">-</span>b<span class="op">;},</span><span class="dt">dif</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span><span class="cf">if</span>(a<span class="op">&gt;</span>b)<span class="op">{</span><span class="cf">return</span> a<span class="op">;}</span><span class="cf">else</span><span class="op">{</span><span class="cf">return</span> b<span class="op">;}}}</span></code></pre></div>
  1482. <p>上面的代码相对于下面的代码可读性没有那么多,但是计算机可以无条件地执行上面的代码。上面的代码对于网络传输来说是好的,但是对于人来说并不是如此,我们需要一些工具来辅助我们去读懂上面的代码。如果代码上写得没有一点可读性,诸如函数命名没有多少实际意义,如果我们把前面的函数就成这样:</p>
  1483. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> c<span class="op">={</span>
  1484. <span class="dt">a</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1485. <span class="cf">return</span> a<span class="op">+</span>b<span class="op">;</span>
  1486. <span class="op">},</span>
  1487. <span class="dt">s</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1488. <span class="cf">return</span> a<span class="op">-</span>b<span class="op">;</span>
  1489. <span class="op">},</span>
  1490. <span class="dt">d</span><span class="op">:</span> <span class="kw">function</span>(a<span class="op">,</span>b)<span class="op">{</span>
  1491. <span class="cf">if</span>(a<span class="op">&gt;</span>b)<span class="op">{</span>
  1492. <span class="cf">return</span> a<span class="op">;</span>
  1493. <span class="op">}</span><span class="cf">else</span><span class="op">{</span>
  1494. <span class="cf">return</span> b<span class="op">;</span>
  1495. <span class="op">}</span>
  1496. <span class="op">}</span>
  1497. <span class="op">}</span></code></pre></div>
  1498. <p>那么只有在我们理解了这个函数是干什么之后才能理解函数是干什么,而不是光看函数名就可以了。</p>
  1499. <p>在Javascript解决一个函数的办法有很多,在其他一些语言如Ruby或者Perl中也是如此,解决问题的办法有很多,对于写代码的人来说是一个享受的过程,但是对于维护的人来说并非如此。而这个和Python的思想不是很一致的是,Python设计的理念是</p>
  1500. <blockquote>
  1501. 对于特定的问题,只要有一种最好的方法来解决就够了
  1502. </blockquote>
  1503. <p>可读性的代码在今天显得比以前重要的多,以前写程序的时候我们需要去考虑使用汇编或者其他工具来提高程序的效率。</p>
  1504. <pre><code> .global _start
  1505. .text
  1506. _start:
  1507. # write(1, message, 13)
  1508. mov $1, %rax # system call 1 is write
  1509. mov $1, %rdi # file handle 1 is stdout
  1510. mov $message, %rsi # address of string to output
  1511. mov $13, %rdx # number of bytes
  1512. syscall # invoke operating system to do the write
  1513. # exit(0)
  1514. mov $60, %rax # system call 60 is exit
  1515. xor %rdi, %rdi # we want return code 0
  1516. syscall # invoke operating system to exit
  1517. message:
  1518. .ascii &quot;Hello, world\n&quot;</code></pre>
  1519. <p>所以上面的代码的可读性在今天新生一代的程序员来说可能没有那么容易理解。芯片运行的速度越来越快,在程序上我们也需要一个越来越快的解决方案,而所谓的越来越快的解决方案指的不是运行速度上,而是开发速度上。如果你没有办法在同样时间内开发出更好的程序,那么你就可能输给你的竞争对手。</p>
  1520. <h3 id="开始之前-1"><span class="header-section-number">8.1.1</span> 开始之前</h3>
  1521. <p>我们终于又从一种语言跳到了另外一种语言,我们可能习惯了一种模式,而不敢于去尝试新的东西,这些或许是我们的一些习惯又或者是因为害怕等等。</p>
  1522. <p>作为另外一个跨平台能力很强的语言,这里说的是与Javascript、HTML比较,或许你会觉得C算是最好的,但是我们这里讨论更多的是脚本语言,也就是直接可以运行的。在现在主流的大多数移动平台上,python也有良好的支持,如Android,IOS,只是这些算是类Unix系统内核,python还支持之前Nokia的Symbian。</p>
  1523. <p>开始之前我们需要确认我们的平台上已经有了python环境,也就是可以运行下面的Hello,World,你可以在网上很多地方看到,最简单的地方还是到官网,又或者是所用移动平台的store下载。</p>
  1524. <h3 id="python的helloworld"><span class="header-section-number">8.1.2</span> Python的Hello,World</h3>
  1525. <p>Python的Hello,World有两种形式,作为一种脚本语言来说,Javascript也是一种脚本语言,只是两者之间有太多的不同之处,每个人都会有不同的选择对于一种语言用来作为其的习惯。于是这就是我们的</p>
  1526. <pre><code>print &quot;Hello,World&quot;</code></pre>
  1527. <p>当我们把我们的脚本在shell环境下运行时</p>
  1528. <div class="sourceCode"><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>
  1529. <span class="kw">File</span> <span class="st">&quot;&lt;stdin&gt;&quot;</span>, line 1
  1530. <span class="kw">print</span> <span class="st">&quot;Hello,world&quot;</span>
  1531. ^
  1532. <span class="kw">IndentationError</span>: unexpected indent
  1533. <span class="kw">&gt;&gt;&gt;</span> <span class="kw">print</span> <span class="st">&quot;Hello,world&quot;</span>
  1534. <span class="kw">Hello</span>,world
  1535. <span class="kw">&gt;&gt;&gt;</span></code></pre></div>
  1536. <p>如果你没有预料到缩进带来的问题的时候,这就是一个问题了。</p>
  1537. <p>和我们在Javascript或者是CSS里面一样,我们也可以用一个文件的方式来写入我们的代码,文件后缀名是py,所以创建一个helloworld.py,输入上面的代码,然后执行</p>
  1538. <pre><code>python helloworld.py</code></pre>
  1539. <p>一个理想的结果,或许你试过C语言的helloworld,如果了解过GCC的话应该是可以这样的:</p>
  1540. <pre><code>./a.out</code></pre>
  1541. <p>也就是执行编译完后的程序,需要注意的是helloworld.py没有编译,不过也会输出</p>
  1542. <pre><code>Hello,world</code></pre>
  1543. <h3 id="我们想要的helloworld"><span class="header-section-number">8.1.3</span> 我们想要的Hello,World</h3>
  1544. <p>我们想展示的是如何结合前面学习的内容做一个更有意思的Hello,World。</p>
  1545. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> cherrypy
  1546. <span class="kw">class</span> HelloWorld(<span class="bu">object</span>):
  1547. <span class="kw">def</span> index(<span class="va">self</span>):
  1548. <span class="cf">return</span> <span class="st">&quot;Hello World!&quot;</span>
  1549. index.exposed <span class="op">=</span> <span class="va">True</span>
  1550. cherrypy.quickstart(HelloWorld())</code></pre></div>
  1551. <h2 id="算法"><span class="header-section-number">8.2</span> 算法</h2>
  1552. <p>我们需要去了解算法(algorithm),引经据典的话就是这样子:</p>
  1553. <blockquote>
  1554. a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer
  1555. </blockquote>
  1556. <p>也就是计算或其他解决问题的操作需要遵循的一个过程或者一套规则,书上还提到的说法是——解决问题的诀窍,让我想起了hack一词。我们总会去想某些东西是否有一个更快的计算方法,有时候在处理某些问题上也显示了一个好的算法的重要性。</p>
  1557. <h2 id="实用主义哲学"><span class="header-section-number">8.3</span> 实用主义哲学</h2>
  1558. <p>(来自于:HyryStudio)</p>
  1559. <p>大多数工程师、科学家使用科学计算软件的目的都是为了快速解决其工作中遇到的问题,而不是开发出一套完整的软件。这就是为什么MATLAB这样的商用科学计算软件十分流行的原因。而Python在这一点上实际上和MATLAB十分相似,我们也可以使用Python众多的扩展库快速写出一次性的数据处理、运算的脚本。然而由于Python语言的一些高级特性,以及众多的科学计算之外的扩展库,我们可以将积累下来的一次性脚本进行改造,为它们提供命令行、GUI、数据库、网络等众多接口,最终成为一套完整的工具包或者实用的计算软件。而且由于是开源的自由软件,我们可以在任何系统中安装Python环境,运行我们 的程序。</p>
  1560. <p>Python一直保持着很强的实用主义,它通常不会去试着重新开发一整套函数库,而是将现有的开源函数库包装成其扩展库。而Python则通过这些扩展库将众多的开源函数库连接在一起,是名符其实的胶水语言。例如由华盛顿大学的教授主导开发的 Sage ,就是一套以代替MATLAB、Mathematica、Maple等商用科学计算软件为目的的开源系统。它通过Python结合了众多的开源科学计算软件,并通过网页浏览器提供了一个与其交互的记事本文档界面。 Python的科学计算扩展库非常多,不同专业的技术人员都可以找到适合自己的扩展库。下面是我经常会用到的一个非常不完全的列表:</p>
  1561. <ul>
  1562. <li>NumPy + SciPy + matplotlib + IPython : 这几个应该是每位开发者都应具备的扩展库。NumPy提供了多维数组以及众多的处理函数,SciPy提供了各种数值运算功能,matplotlib能绘制 出精美的二维图表,IPython则提供了一个超强的命令行,最新版的IPython还添加于Sage类似的浏览器的记事本界面(notebook)。</li>
  1563. <li>SciKits : 其中包括许多独立的扩展库,作为SciPy的补充。其中 scikit-learn 是一套机器学习库,包含了比较完善的文档以及众多的实例程序。</li>
  1564. <li>Pandas : 以Python世界中 R 的替代品为目标的数据分析库。根据其官方网站的测试,Pandas在许多方面的性能都比R要高。</li>
  1565. <li>ETS : 这是一套Enthought公司开发的函数库,其中的 Mayavi 能很方便地对数据进行三维可视化。</li>
  1566. <li>OpenCV : 这是一套计算机视觉库,目前的最新版本已经提供了十分完备的Python接口,能够调用OpenCV中众多的图像处理、模式识别函数直接对NumPy数组进行处理。</li>
  1567. </ul>
  1568. <h2 id="包管理-1"><span class="header-section-number">8.4</span> 包管理</h2>
  1569. <p>关于Python的包管理</p>
  1570. <ul>
  1571. <li>Eggs 格式是 setuptools 引入的一种文件格式,它使用 .egg 扩展名,用于 Python 模块的安装。</li>
  1572. <li>pip 是目前 python 包管理的事实标准,2008年发布。它被用作 easy_install 的替代品,但是它仍有大量的功能建立在 setuptools 组件之上。</li>
  1573. </ul>
  1574. <h3 id="python-requests"><span class="header-section-number">8.4.1</span> python requests</h3>
  1575. <pre><code>Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。
  1576. Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。</code></pre>
  1577. <p>以安装requests为例:</p>
  1578. <p>命令:</p>
  1579. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> pip install requests</code></pre></div>
  1580. <p>结果:</p>
  1581. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Downloading/unpacking</span> requests
  1582. <span class="kw">Downloading</span> requests-2.4.3-py2.py3-none-any.whl (459kB)<span class="kw">:</span> 459kB downloaded
  1583. <span class="kw">Installing</span> collected packages: requests
  1584. <span class="kw">Successfully</span> installed requests
  1585. <span class="kw">Cleaning</span> up...</code></pre></div>
  1586. <p>用这个库我们可以做些什么?看看官网的示例:</p>
  1587. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">&gt;&gt;&gt;</span> <span class="kw">import</span> requests
  1588. <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>)
  1589. <span class="kw">&gt;&gt;&gt;</span> <span class="kw">r.json</span>()</code></pre></div>
  1590. <p>到现在你会发现我们没有说到任何的Python语法,这不是一本关于Python语法的书,如我们在开头所说的。下面是我们将会在后面用到的代码</p>
  1591. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#!/usr/bin/env python</span>
  1592. <span class="im">import</span> requests
  1593. url <span class="op">=</span> <span class="st">&quot;http://b.phodal.com/athome/1&quot;</span>
  1594. r <span class="op">=</span> requests.get(url)
  1595. <span class="bu">print</span> r.text</code></pre></div>
  1596. <h1 id="raspberry-pi"><span class="header-section-number">9</span> Raspberry Pi</h1>
  1597. <div class="figure">
  1598. <img src="./images/rpi.jpg" alt="Raspberry Pi" />
  1599. <p class="caption">Raspberry Pi</p>
  1600. </div>
  1601. <h2 id="geek的盛宴"><span class="header-section-number">9.1</span> Geek的盛宴</h2>
  1602. <p>Raspberry Pi是一款针对电脑业余爱好者、教师、小学生以及小型企业等用户的迷你电脑,预装Linux系统,体积仅信用卡大小,搭载ARM架构处理器,运算性能和智能手机相仿。在接口方面,Raspberry Pi提供了可供键鼠使用的USB接口,此外还有千兆以太网接口、SD卡扩展接口以及1个HDMI高清视频输出接口,可与显示器或者TV相连。</p>
  1603. <p>Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。</p>
  1604. <p>Raspberry Pi相比于一般的ARM开发板来说,由于其本身搭载着Linux操作系统,可以用诸如Python、Ruby或Bash来执行脚本,而不是通过编译程序来运行,具有更高的开发效率。</p>
  1605. <h2 id="raspberry-pi-初始化"><span class="header-section-number">9.2</span> Raspberry Pi 初始化</h2>
  1606. <p>今天的Raspbian默认已经安装<code>openssh-server</code>,并默认开启了OpenSSH-Server。</p>
  1607. <p>接着我们就可以看到系统启动了,要我们输入用户名和密码</p>
  1608. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Raspbian</span> GNU/Linux 7 raspberrypi ttyAMA0
  1609. <span class="kw">raspberrypi</span> login: pi
  1610. <span class="kw">Password</span>:
  1611. <span class="kw">Last</span> login: Sat Apr 26 05:58:07 UTC 2014 on ttyAMA0
  1612. <span class="kw">Linux</span> raspberrypi 3.10.25+ <span class="co">#622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l</span>
  1613. <span class="kw">The</span> programs included with the Debian GNU/Linux system are free software<span class="kw">;</span>
  1614. <span class="kw">the</span> exact distribution terms for each program are described in the
  1615. <span class="kw">individual</span> files in /usr/share/doc/*/copyright.
  1616. <span class="kw">Debian</span> GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
  1617. <span class="kw">permitted</span> by applicable law.
  1618. <span class="kw">ls</span>
  1619. <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></div>
  1620. <p>然后</p>
  1621. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> raspi-config</code></pre></div>
  1622. <p>选择第一个,下面就可以继续了</p>
  1623. <p><code>Expand Filesystem Ensures that all of the SD card s</code></p>
  1624. <p>接着重启后,便可以扩展SD卡成功。</p>
  1625. <p>注: Raspbian与一般的Debian系统使用起来区别不是太大(ps:命令上),由于CPU是不同的架构,在编译上可能有所区别。通常PC上的软件需要重新编译才能在RPi上运行,所以如果可以用apt-get安装的话,就不要自己编译了。</p>
  1626. <h2 id="raspberry-pi-gpio"><span class="header-section-number">9.3</span> Raspberry Pi GPIO</h2>
  1627. <blockquote>
  1628. <p>General Purpose Input Output (通用输入/输出)简称为GPIO,或总线扩展器,利用工业标准I2C、SMBus或SPI接口简化了I/O口的扩展。当微控制器或芯片组没有足够的I/O端口,或当系统需要采用远端串行通信或控制时,GPIO产品能够提供额外的控制和监视功能。</p>
  1629. </blockquote>
  1630. <div class="figure">
  1631. <img src="./images/gpio.png" alt="GPIO" />
  1632. <p class="caption">GPIO</p>
  1633. </div>
  1634. <h1 id="server-一切皆为服务"><span class="header-section-number">10</span> Server 一切皆为服务</h1>
  1635. <h2 id="服务器"><span class="header-section-number">10.1</span> 服务器</h2>
  1636. <p>服务器(Server)指:</p>
  1637. <ul>
  1638. <li>一个管理资源并为用户提供服务的计算机软件,通常分为文件服务器(能使用户在其它计算机访问文件),数据库服务器和应用程序服务器。</li>
  1639. <li>运行以上软件的计算机,或称为网络主机(Host)。</li>
  1640. <li>一般来说,服务器通过网络对外提供服务。可以通过Intranet对内网提供服务,也可以通过Internet对外提供服务。</li>
  1641. </ul>
  1642. <h2 id="web服务器"><span class="header-section-number">10.2</span> Web服务器</h2>
  1643. <p>WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。 WWW 是 Internet的多媒体信息查询工具,是 Internet 上近年才发展起来的服务,也是发展最快和目前用的最广泛的服务。正是因为有了WWW工具,才使得近年来 Internet 迅速发展,且用户数量飞速增长。</p>
  1644. <h2 id="lnmp"><span class="header-section-number">10.3</span> LNMP</h2>
  1645. <div class="figure">
  1646. <img src="./images/lnmp.gif" alt="LNMP" />
  1647. <p class="caption">LNMP</p>
  1648. </div>
  1649. <p>Linux+Nginx+MySQL+PHP</p>
  1650. <blockquote>
  1651. <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>
  1652. </blockquote>
  1653. <blockquote>
  1654. <p>MySQL 是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。</p>
  1655. </blockquote>
  1656. <blockquote>
  1657. <p>PHP于1994年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf为了要维护个人网页而制作的一个简单的用Perl语言编写的程序。这些工具程序用来显示 Rasmus Lerdorf 的个人履历,以及统计网页流量。后来又用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。</p>
  1658. </blockquote>
  1659. <h1 id="web服务"><span class="header-section-number">11</span> Web服务</h1>
  1660. <p>Web服务是一种服务导向架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作。</p>
  1661. <p>根据W3C的定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作。网络服务通常是许多应用程序接口(API)所组成的,它们透过网络,例如国际互联网(Internet)的远程服务器端,执行客户所提交服务的请求。</p>
  1662. <p>尽管W3C的定义涵盖诸多相异且无法介分的系统,不过通常我们指有关于主从式架构(Client-server)之间根据SOAP协议进行传递XML格式消息。无论定义还是实现,WEB服务过程中会由服务器提供一个机器可读的描述(通常基于WSDL)以辨识服务器所提供的WEB服务。另外,虽然WSDL不是SOAP服务端点的必要条件,但目前基于Java的主流WEB服务开发框架往往需要WSDL实现客户端的源代码生成。一些工业标准化组织,比如WS-I,就在WEB服务定义中强制包含SOAP和WSDL。</p>
  1663. <p>WEB服务实际上是一组工具,并有多种不同的方法调用之。三种最普遍的手段是:</p>
  1664. <ul>
  1665. <li>远程过程调用(RPC)</li>
  1666. <li>面向服务架构(SOA)</li>
  1667. <li>表述性状态转移(REST)。</li>
  1668. </ul>
  1669. <h2 id="soap-vs-restful"><span class="header-section-number">11.1</span> SOAP VS RESTful</h2>
  1670. <p>简单对象访问协议是交换数据的一种协议规范,使用在计算机网络Web服务中,交换带结构信息。SOAP为了简化网页服务器从XML数据库中提取数据时,节省去格式化页面时间,以及不同应用程序之间按照HTTP通信协议,遵从XML格式执行资料互换,使其抽象于语言实现、平台和硬件。</p>
  1671. <h1 id="http-熟悉陌生"><span class="header-section-number">12</span> HTTP 熟悉&amp;陌生</h1>
  1672. <h2 id="你所没有深入的http"><span class="header-section-number">12.1</span> 你所没有深入的HTTP</h2>
  1673. <p>Internet有两个核心协议: IP和TCP,这样讲述起来似乎会很漫长。</p>
  1674. <p>基本概念</p>
  1675. <blockquote>
  1676. <p>超文本传输协议 (HTTP-Hypertext transfer protocol) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议。</p>
  1677. </blockquote>
  1678. <ul>
  1679. <li>HTTP是用于客户端与服务端之间的通信。</li>
  1680. <li>传输层的TCP是基于网络层的IP协议的,而应用层的HTTP协议又是基于传输层的TCP协议的。</li>
  1681. </ul>
  1682. <p><code>注意</code>: HTTP协议只规定了客户端与服务端的通信规则,而没有规定其通讯协议,只是现在的大部分实现都是将TCP作为通讯协议。</p>
  1683. <h3 id="打开网页时发生了什么"><span class="header-section-number">12.1.1</span> 打开网页时发生了什么</h3>
  1684. <p>简单地来说,当我们在浏览器上输入URL的敲下回车的时候。</p>
  1685. <ul>
  1686. <li>浏览器需要查找域名<a href="#fn2" class="footnoteRef" id="fnref2"><sup>2</sup></a>的IP,从不同的缓存直至DNS服务器。</li>
  1687. <li>浏览器会给web服务器发送一个HTTP请求</li>
  1688. <li>服务器“处理”请求</li>
  1689. <li>服务器发回一个HTML响应</li>
  1690. <li>浏览器渲染HTML到页面。</li>
  1691. </ul>
  1692. <p>在<a href="http://stackoverflow.com/questions/2092527/what-happens-when-you-type-in-a-url-in-browser">StackOverflow</a>上有一个这样的回答会比较详细。</p>
  1693. <ul>
  1694. <li>browser checks cache; if requested object is in cache and is fresh, skip to #9</li>
  1695. <li>browser asks OS for server’s IP address</li>
  1696. <li>OS makes a DNS lookup and replies the IP address to the browser</li>
  1697. <li>browser opens a TCP connection to server (this step is much more complex with HTTPS)</li>
  1698. <li>browser sends the HTTP request through TCP connection</li>
  1699. <li>browser receives HTTP response and may close the TCP connection, or reuse it for another request</li>
  1700. <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>
  1701. <li>if cacheable, response is stored in cache</li>
  1702. <li>browser decodes response (e.g. if it’s gzipped)</li>
  1703. <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>
  1704. <li>browser renders response, or offers a download dialog for unrecognized types</li>
  1705. </ul>
  1706. <p>忽略一些细节便剩下了</p>
  1707. <ol style="list-style-type: decimal">
  1708. <li>从浏览器输入URL</li>
  1709. <li>浏览器找到服务器,服务器返回HTML文档</li>
  1710. <li>从对应的服务器下载资源</li>
  1711. </ol>
  1712. <p>说说第一步,开始时我们输入的是URI(统一资源标识符,Uniform Resource Identifier),它还有另外一个名字叫统一资源定位器(URL<a href="#fn3" class="footnoteRef" id="fnref3"><sup>3</sup></a>,Uniform Resource Locator)。</p>
  1713. <h3 id="url组成"><span class="header-section-number">12.1.2</span> URL组成</h3>
  1714. <p>网址算是URL的一个俗称,让我们来看看一个URL的组成,以HTTP版IOT中的URL为例。</p>
  1715. <p><code>http://b.phodal.com/athome/1</code></p>
  1716. <p>开始之前,我们需要标出URL的80端口以及json文件的全称,那么上面的网址就会变成</p>
  1717. <p><code>http://b.phodal.com:80/athome/1.json</code></p>
  1718. <p>那么对于这个URL的就有下面几部分组成</p>
  1719. <ul>
  1720. <li><code>http://</code> http说的是这个URL用的是HTTP协议,至于<code>//</code>是一个分隔符,用法和C语言中的<code>;</code>一样。这样的协议还可以是coap,https,ftp等等。</li>
  1721. <li><code>b</code> 是子域名,一个域名在<strong>允许</strong>的情况下可以有不限数量的子域名。</li>
  1722. <li><code>phodal.com</code> 代表了一个URL是phodal.com下面的域名</li>
  1723. <li><code>80</code> 80是指80端口,默认的都是80,对于一个不是80端口的URL应该是这样的<code>http://iot-coap.phodal.com:8896/</code></li>
  1724. <li><code>athome</code> 指的是虚拟目录部分,或者文件路径</li>
  1725. <li><code>1.json</code>看上去就是一个文件名,然而也代表着这是一个资源。</li>
  1726. </ul>
  1727. <p>对就一个稍微复杂点的例子就是</p>
  1728. <p><code>http://ebook.designiot.cn/#你所没有深入的http</code></p>
  1729. <p>这里的<code>#</code>后面是锚部分,如果你打开这个URL就会发现会直接跳转到相应的锚部分,对就于下面这样的一个例子来说</p>
  1730. <p><code>http://www.phodal.com/search/?q=iot&amp;type=blog</code></p>
  1731. <p><code>?</code>后面的<code>q=iot&amp;type=blog</code>的部分是参数部分,通常用于查询或者、搜索。</p>
  1732. <h2 id="一次http-get请求"><span class="header-section-number">12.2</span> 一次HTTP GET请求</h2>
  1733. <p>当我们打开最小物联网系统的一个页面时,如<a href="http://b.phodal.com/athome/1.json" class="uri">http://b.phodal.com/athome/1.json</a></p>
  1734. <p>我们在浏览器上看到的结果是</p>
  1735. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">[
  1736. <span class="op">{</span>
  1737. <span class="st">&quot;id&quot;</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  1738. <span class="st">&quot;temperature&quot;</span><span class="op">:</span> <span class="dv">19</span><span class="op">,</span>
  1739. <span class="st">&quot;sensors1&quot;</span><span class="op">:</span> <span class="dv">31</span><span class="op">,</span>
  1740. <span class="st">&quot;sensors2&quot;</span><span class="op">:</span> <span class="fl">7.5</span><span class="op">,</span>
  1741. <span class="st">&quot;led1&quot;</span><span class="op">:</span> <span class="dv">0</span>
  1742. <span class="op">}</span>
  1743. ]</code></pre></div>
  1744. <p>只是我们看到的是结果,忽略了这其中的过程,于是我们用curl<a href="#fn4" class="footnoteRef" id="fnref4"><sup>4</sup></a>命令来看看详细的情况。</p>
  1745. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">curl</span> -I -s http://b.phodal.com/athome/1.json</code></pre></div>
  1746. <p>出于某种原因考虑,删去了其中一些元素,剩下下面这些。</p>
  1747. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.1</span> 200 OK
  1748. <span class="kw">Content-Type</span>: application/json
  1749. <span class="kw">Date</span>: Fri, 05 Sep 2014 15:05:49 GMT
  1750. [<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></div>
  1751. <p>我们用curl命令向服务器发起了GET请求,服务器返回了上面的结果。</p>
  1752. <h3 id="http响应"><span class="header-section-number">12.2.1</span> HTTP响应</h3>
  1753. <p>一个HTTP响应由三部分组成</p>
  1754. <ul>
  1755. <li>状态行(状态码)</li>
  1756. <li>消息报头(响应报头)</li>
  1757. <li>响应正文(消息体)</li>
  1758. </ul>
  1759. <h4 id="http响应-状态码"><span class="header-section-number">12.2.1.1</span> HTTP响应 状态码</h4>
  1760. <p>在上面的结果中,状态行是</p>
  1761. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.1</span> 200 OK</code></pre></div>
  1762. <p>返回的状态码是200,OK是状态码的原因短语。</p>
  1763. <p>如果是一个跳转的页面,它就可能是下面的结果:</p>
  1764. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.0</span> 301 MOVED PERMANENTLY
  1765. <span class="kw">Date</span>: Mon, 08 Sep 2014 12:04:01 GMT
  1766. <span class="kw">Content-Type</span>: text/html<span class="kw">;</span> <span class="ot">charset=</span>utf-8 </code></pre></div>
  1767. <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>
  1768. <ul>
  1769. <li>1xx消息</li>
  1770. <li>2xx成功</li>
  1771. <li>3xx重定向</li>
  1772. <li>4xx客户端错误</li>
  1773. <li>5xx服务器错误</li>
  1774. </ul>
  1775. <p>如</p>
  1776. <ul>
  1777. <li>200 ok - 成功返回状态,对应,GET,PUT,PATCH,DELETE.</li>
  1778. <li>201 created - 成功创建。</li>
  1779. <li>304 not modified - HTTP缓存有效。</li>
  1780. <li>400 bad request - 请求格式错误。</li>
  1781. <li>401 unauthorized - 未授权。</li>
  1782. <li>403 forbidden - 鉴权成功,但是该用户没有权限。</li>
  1783. <li>404 not found - 请求的资源不存在</li>
  1784. <li>405 method not allowed - 该http方法不被允许。</li>
  1785. <li>410 gone - 这个url对应的资源现在不可用。</li>
  1786. <li>415 unsupported media type - 请求类型错误。</li>
  1787. <li>422 unprocessable entity - 校验错误时用。</li>
  1788. <li>429 too many request - 请求过多。</li>
  1789. </ul>
  1790. <h4 id="http响应-响应报头"><span class="header-section-number">12.2.1.2</span> HTTP响应 响应报头</h4>
  1791. <p>在这次响应中,返回了两个报头,即</p>
  1792. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Content-Type</span>: application/json
  1793. <span class="kw">Date</span>: Fri, 05 Sep 2014 15:05:49 GMT</code></pre></div>
  1794. <p>Content-Type和Date,在这里的Context-Type是application/json,而通常情况下我们打开一个网站时,他的Content-Type应该是text/html。</p>
  1795. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Content-Type</span>: text/html<span class="kw">;</span></code></pre></div>
  1796. <p>Content-Type是最重要的报头。</p>
  1797. <h4 id="http响应-响应正文"><span class="header-section-number">12.2.1.3</span> HTTP响应 响应正文</h4>
  1798. <p>正文才是我们真正想要的内容,上面的都是写给浏览器看的,一般的人不会去关注这些。</p>
  1799. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">HTTP/<span class="fl">1.1</span> <span class="dv">200</span> OK
  1800. Server<span class="op">:</span> <span class="va">phodal</span>.<span class="at">com</span>/<span class="fl">0.17.5</span>
  1801. Content<span class="op">-</span>Type<span class="op">:</span> application/json
  1802. [<span class="op">{</span><span class="st">&quot;id&quot;</span><span class="op">:</span><span class="dv">1</span><span class="op">,</span><span class="st">&quot;temperature&quot;</span><span class="op">:</span><span class="dv">19</span><span class="op">,</span><span class="st">&quot;sensors1&quot;</span><span class="op">:</span><span class="dv">31</span><span class="op">,</span><span class="st">&quot;sensors2&quot;</span><span class="op">:</span><span class="fl">7.5</span><span class="op">,</span><span class="st">&quot;led1&quot;</span><span class="op">:</span><span class="dv">0</span><span class="op">}</span>]</code></pre></div>
  1803. <p>通常这是以某种格式写的,在这里是以JSON写的,而对于一个网站的时候则是HTML,如:</p>
  1804. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;html&gt;</span>
  1805. <span class="kw">&lt;head&gt;</span>
  1806. <span class="kw">&lt;meta</span><span class="ot"> charset=</span><span class="st">&quot;UTF-8&quot;</span><span class="kw">&gt;</span>
  1807. <span class="kw">&lt;title&gt;</span>Document<span class="kw">&lt;/title&gt;</span>
  1808. <span class="kw">&lt;/head&gt;</span>
  1809. <span class="kw">&lt;body&gt;</span>
  1810. <span class="kw">&lt;/body&gt;</span>
  1811. <span class="kw">&lt;/html&gt;</span> </code></pre></div>
  1812. <p>那么这次GET请求返回的就是:</p>
  1813. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.0</span> 200 OK
  1814. <span class="kw">Date</span>: Mon, 08 Sep 2014 12:04:01 GMT
  1815. <span class="kw">Content-Type</span>: text/html<span class="kw">;</span> <span class="ot">charset=</span>utf-8
  1816. <span class="kw">&lt;html&gt;</span>
  1817. <span class="kw">&lt;head&gt;</span>
  1818. <span class="kw">&lt;meta</span> charset=<span class="st">&quot;UTF-8&quot;</span><span class="kw">&gt;</span>
  1819. <span class="kw">&lt;title&gt;</span>Document<span class="kw">&lt;</span>/title<span class="kw">&gt;</span>
  1820. <span class="kw">&lt;</span>/<span class="kw">head&gt;</span>
  1821. <span class="kw">&lt;body&gt;</span>
  1822. [<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>]
  1823. <span class="kw">&lt;</span>/<span class="kw">body&gt;</span>
  1824. <span class="kw">&lt;</span>/<span class="kw">html&gt;</span> </code></pre></div>
  1825. <p>虽然与第一次请求的结果在游览器上看似乎是一样的(ps:可能有微小的差异),然而其本质是不同的。</p>
  1826. <p>推荐及参考书目:</p>
  1827. <ul>
  1828. <li>《Web性能权威指南》</li>
  1829. <li>《图解HTTP》</li>
  1830. <li>《RESTful Web Services Cookbook》</li>
  1831. <li>《RESTful Web APIs》</li>
  1832. </ul>
  1833. <h1 id="设计restful-api"><span class="header-section-number">13</span> 设计RESTful API</h1>
  1834. <blockquote>
  1835. <p>REST从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表征。获得这些表征致使这些应用程序转变了其状态。随着不断获取资源的表征,客户端应用不断地在转变着其状态,所谓表征状态转移。</p>
  1836. </blockquote>
  1837. <p>因为我们需要的是一个Machine到Machine沟通的平台,需要设计一个API。而设计一个API来说,RESTful是很不错的一种选择,也是主流的选择。而设计一个RESTful服务,的首要步骤便是设计资源模型。</p>
  1838. <h3 id="资源"><span class="header-section-number">13.0.1</span> 资源</h3>
  1839. <p>互联网上的一切信息都可以看作是一种资源。</p>
  1840. <table>
  1841. <thead>
  1842. <tr class="header">
  1843. <th>HTTP Method</th>
  1844. <th>Operation Performed</th>
  1845. </tr>
  1846. </thead>
  1847. <tbody>
  1848. <tr class="odd">
  1849. <td>GET</td>
  1850. <td>Get a resource (Read a resource)</td>
  1851. </tr>
  1852. <tr class="even">
  1853. <td>POST</td>
  1854. <td>Create a resource</td>
  1855. </tr>
  1856. <tr class="odd">
  1857. <td>PUT</td>
  1858. <td>Update a resource</td>
  1859. </tr>
  1860. <tr class="even">
  1861. <td>DELETE</td>
  1862. <td>Delete Resource</td>
  1863. </tr>
  1864. </tbody>
  1865. </table>
  1866. <h2 id="设计restful-api-1"><span class="header-section-number">13.1</span> 设计RESTful API</h2>
  1867. <p>设计RESTful API是一个有意思的话题。下面是一些常用的RESTful设计原则:</p>
  1868. <h2 id="rest关键目标"><span class="header-section-number">13.2</span> REST关键目标</h2>
  1869. <ul>
  1870. <li>组件间交互的可伸缩性</li>
  1871. <li>接口的通用性</li>
  1872. <li>组件的独立部署</li>
  1873. <li>通过中间组件来减少延迟、实施安全策略和封装已有系统</li>
  1874. </ul>
  1875. <h2 id="判断是否是-restful的约束条件"><span class="header-section-number">13.3</span> 判断是否是 RESTful的约束条件</h2>
  1876. <ul>
  1877. <li>客户端-服务器分离</li>
  1878. <li>无状态</li>
  1879. <li>可缓存</li>
  1880. <li>多层系统</li>
  1881. <li>统一接口</li>
  1882. <li>随需代码(可选)</li>
  1883. </ul>
  1884. <h2 id="json"><span class="header-section-number">13.4</span> JSON</h2>
  1885. <div class="figure">
  1886. <img src="./images/xml-vs-json.png" alt="xml-vs-json" />
  1887. <p class="caption">xml-vs-json</p>
  1888. </div>
  1889. <h1 id="环境准备"><span class="header-section-number">14</span> 环境准备</h1>
  1890. <h2 id="laravel"><span class="header-section-number">14.1</span> Laravel</h2>
  1891. <blockquote>
  1892. <p>Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。</p>
  1893. </blockquote>
  1894. <ul>
  1895. <li>RESTful 路由: 通过简单的闭包就能响应HTTP请求。帮你快速开始构建非凡的应用。</li>
  1896. <li>强大的数据操纵能力: Laravel自带了强大的Eloquent ORM 和迁移工具。能够完美的与MySQL、Postgres、SQL Server 和 SQLite协同工作。</li>
  1897. <li>优雅的模版引擎: PHP代码或轻量级的Blade模版引擎都可无缝融合。Blade模版可以继承,并且拥有极快的解析速度。相信你会喜欢它的。</li>
  1898. <li>为明天做准备: 构建大型的企业级应用或者只是提供简单的JSON API;书写强大的控制器或轻巧的RESTful路由,Laravel适应所有级别的开发工作。</li>
  1899. <li>可靠的基石: Laravel 的基石是数个Symfony组件,这些经过千锤百炼、可靠的组件为你的应用提供坚实的基础。</li>
  1900. <li>基于Composer管理器: Composer 是一套帮你管理第三方扩展包的工具。能够让你迅速在 Packagist 中找到需要的扩展包。</li>
  1901. <li>强大的社区支持: 无论你是一个PHP新手还是经验丰富的架构师,都能在社区中找到需要的知识。你可以在IRC中讨论Idea,或者在论坛中发布问题。</li>
  1902. <li>测试、重构: Laravel 从开始就将测试作为重点功能。我们提供了灵活的IoC容器,集成了PHPUnit 测试工具。不用担心,这些都很容易上手。</li>
  1903. </ul>
  1904. <h3 id="为什么是-laravel"><span class="header-section-number">14.1.1</span> 为什么是 Laravel</h3>
  1905. <ul>
  1906. <li>因为个人喜爱,你也可以用 Ruby On Rails来搭建这样一个功能,或者是Java。</li>
  1907. <li>PHP在我的服务器上运行得挺不错的,而且我又不需要重新去写配置那些配置。</li>
  1908. <li>Laravel 可以简单的开发我们所需要的功能,换句话说他是 PHP 世界的 Ruby On Rails。</li>
  1909. </ul>
  1910. <p>这里不会再重述之前的问题,这里只是将需要的步骤一个个写下来,然后丢到这里好好说一下。至于RESTful是什么,前面已经介绍了,就不再重复了。那么下面,我们就用Laravel来搭建一个平台给物联网用的。</p>
  1911. <h2 id="安装-laravel"><span class="header-section-number">14.2</span> 安装 Laravel</h2>
  1912. <h3 id="gnulinux安装composer"><span class="header-section-number">14.2.1</span> GNU/Linux安装Composer</h3>
  1913. <p>GNU/Linux Ubuntu/OpenSUSE下可以执行</p>
  1914. <pre><code>$ curl -sS https://getcomposer.org/installer | php</code></pre>
  1915. <h4 id="windows安装composer"><span class="header-section-number">14.2.1.1</span> Windows安装Composer</h4>
  1916. <p>请直接下载</p>
  1917. <p><a href="https://getcomposer.org/Composer-Setup.exe">Composer-Setup</a></p>
  1918. <h4 id="mac-os"><span class="header-section-number">14.2.1.2</span> Mac OS</h4>
  1919. <p>1.安装Composer</p>
  1920. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install homebrew/php/composer</code></pre></div>
  1921. <p>2.安装Laravel</p>
  1922. <div class="sourceCode"><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></div>
  1923. <p>3.创建Laravel工程</p>
  1924. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> create-project laravel/laravel your-project-name --prefer-dist </code></pre></div>
  1925. <h4 id="mac-os-1"><span class="header-section-number">14.2.1.3</span> Mac OS</h4>
  1926. <p>1.下载laravel.phar</p>
  1927. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">wget</span> http://laravel.com/laravel.phar</code></pre></div>
  1928. <p>2.重命名</p>
  1929. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">mv</span> laravel.phar laravel</code></pre></div>
  1930. <p>3.移动到bin中</p>
  1931. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> mv laravel /usr/local/bin</code></pre></div>
  1932. <p>4.创建项目</p>
  1933. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">laravel</span> new blog</code></pre></div>
  1934. <h2 id="mysql"><span class="header-section-number">14.3</span> MySQL</h2>
  1935. <h3 id="安装mysql-1"><span class="header-section-number">14.3.1</span> 安装MySQL</h3>
  1936. <p><code>出于某些原因,我建议用MariaDB替换MySQL,如果你&quot;真正&quot;需要mysql,将mariadb替换为mysql</code></p>
  1937. <p>ps: 在下文中我会继续用MySQL,而不是MariaDB,MairaDB是MySQL的一个分支,真正的开源分支。</p>
  1938. <p>Ubuntu/Debian/Mint</p>
  1939. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> apt-get install mariadb-server</code></pre></div>
  1940. <p>Fedora/Centos</p>
  1941. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> yum install mariadb-server </code></pre></div>
  1942. <p>openSUSE</p>
  1943. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> zypper install mariadb-server</code></pre></div>
  1944. <p>Mac OS</p>
  1945. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">brew</span> install mariadb</code></pre></div>
  1946. <h3 id="配置mysql"><span class="header-section-number">14.3.2</span> 配置MySQL</h3>
  1947. <p>修改database.php</p>
  1948. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">app/config/database.php</span></code></pre></div>
  1949. <p>要修改的就是这个</p>
  1950. <div class="sourceCode"><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>
  1951. <span class="st">&#39;driver&#39;</span> =&gt; <span class="st">&#39;mysql&#39;</span><span class="ot">,</span>
  1952. <span class="st">&#39;host&#39;</span> =&gt; <span class="st">&#39;localhost&#39;</span><span class="ot">,</span>
  1953. <span class="st">&#39;database&#39;</span> =&gt; <span class="st">&#39;iot&#39;</span><span class="ot">,</span>
  1954. <span class="st">&#39;username&#39;</span> =&gt; <span class="st">&#39;root&#39;</span><span class="ot">,</span>
  1955. <span class="st">&#39;password&#39;</span> =&gt; <span class="st">&#39;940217&#39;</span><span class="ot">,</span>
  1956. <span class="st">&#39;charset&#39;</span> =&gt; <span class="st">&#39;utf8&#39;</span><span class="ot">,</span>
  1957. <span class="st">&#39;collation&#39;</span> =&gt; <span class="st">&#39;utf8_unicode_ci&#39;</span><span class="ot">,</span>
  1958. <span class="st">&#39;prefix&#39;</span> =&gt; <span class="st">&#39;&#39;</span><span class="ot">,</span>
  1959. <span class="ot">),</span></code></pre></div>
  1960. <p>如果你已经有phpmyadmin,似乎对你来说已经很简单了,如果没有的话,就直接用</p>
  1961. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre></div>
  1962. <p>来创建一个新的</p>
  1963. <div class="sourceCode"><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></div>
  1964. <p>数据库的目的在于存储数据等等的闲话这里就不多说了,创建一个RESTful的目的在于产生下面的JSON格式数据,以便于我们在Android、Java、Python、jQuery等语言框架或者平台上可以调用,最主要的是可以直接用Ajax来产生更炫目的效果。</p>
  1965. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="op">{</span>
  1966. <span class="st">&quot;id&quot;</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  1967. <span class="st">&quot;temperature&quot;</span><span class="op">:</span> <span class="dv">14</span><span class="op">,</span>
  1968. <span class="st">&quot;sensors1&quot;</span><span class="op">:</span> <span class="dv">12</span><span class="op">,</span>
  1969. <span class="st">&quot;sensors2&quot;</span><span class="op">:</span> <span class="dv">12</span><span class="op">,</span>
  1970. <span class="st">&quot;led1&quot;</span><span class="op">:</span> <span class="dv">0</span>
  1971. <span class="op">}</span></code></pre></div>
  1972. <h1 id="创建rest服务"><span class="header-section-number">15</span> 创建REST服务</h1>
  1973. <h2 id="数据库迁移"><span class="header-section-number">15.1</span> 数据库迁移</h2>
  1974. <p>这个名字是源自于Ruby On Rails在那时候的印象,不直接使用MySQL的目的在于让我们可以专注于过程。</p>
  1975. <h3 id="创建表"><span class="header-section-number">15.1.1</span> 创建表</h3>
  1976. <p>表的概念,类似于在Excel中的表,如果你真实不懂数据库。 让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些次要的因素吧。</p>
  1977. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate:make create_athomes_table</code></pre></div>
  1978. <p>打开 app/database/migrations/<strong><em>create_athomes_table.php这里的</em></strong>是由日期和某些东西组成的,修改生成的代码为下面。</p>
  1979. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">use</span> Illuminate\Database\Schema\Blueprint<span class="ot">;</span>
  1980. <span class="kw">use</span> Illuminate\Database\Migrations\Migration<span class="ot">;</span>
  1981. <span class="kw">class</span> CreateAthomesTable <span class="kw">extends</span> Migration {
  1982. <span class="kw">public</span> <span class="kw">function</span> up<span class="ot">()</span>
  1983. {
  1984. 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>
  1985. {
  1986. <span class="kw">$table</span>---&gt;increments<span class="ot">(</span><span class="st">&#39;id&#39;</span><span class="ot">);</span>
  1987. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  1988. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  1989. <span class="kw">$table</span>-&gt;float<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  1990. <span class="kw">$table</span>-&gt;boolean<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  1991. <span class="kw">$table</span>-&gt;timestamps<span class="ot">();</span>
  1992. }<span class="ot">);</span>
  1993. }
  1994. <span class="kw">public</span> <span class="kw">function</span> down<span class="ot">()</span>
  1995. {
  1996. Schema::drop<span class="ot">(</span><span class="st">&#39;athomes&#39;</span><span class="ot">);</span>
  1997. }
  1998. }</code></pre></div>
  1999. <p>意思大致就是id是自加的,也就是我们在localhost/athome/{id},当我们创建一个新的数据的时候,会自动加上去,最后一个timestamps批的是时间,会包含创建时间和修改时间。 剩下的temperature,sensors1,sensors2是小数,以及只有真和假的led1。</p>
  2000. <h3 id="数据库迁移-1"><span class="header-section-number">15.1.2</span> 数据库迁移</h3>
  2001. <p>我们只是写了我们需要的数据的格式而并没有丢到数据库里,</p>
  2002. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate</code></pre></div>
  2003. <p>这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。</p>
  2004. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre></div>
  2005. <div class="sourceCode"><pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">use</span> iot;
  2006. <span class="kw">select</span> * <span class="kw">from</span> athomes;</code></pre></div>
  2007. <p>就可以看到我们写的东西,那么接下来就是创建RESTful服务了</p>
  2008. <h2 id="创建restful"><span class="header-section-number">15.2</span> 创建RESTful</h2>
  2009. <p>用下面的代码实现我们称之为Athomes控制器的创建</p>
  2010. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan controller:make AthomesController </code></pre></div>
  2011. <p>就会在app/controllers下面生成下面的代码</p>
  2012. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
  2013. <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span> {}
  2014. <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span> {}
  2015. <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span> {}
  2016. <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  2017. <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  2018. <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  2019. <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
  2020. }</code></pre></div>
  2021. <h2 id="laravel-resources"><span class="header-section-number">15.3</span> Laravel Resources</h2>
  2022. <p>上面的代码过于沉重,请让我用 Ctrl+C 来带来点知识吧。</p>
  2023. <table>
  2024. <thead>
  2025. <tr class="header">
  2026. <th>Verb</th>
  2027. <th>Path</th>
  2028. <th>Action</th>
  2029. <th>Route Name</th>
  2030. </tr>
  2031. </thead>
  2032. <tbody>
  2033. <tr class="odd">
  2034. <td>GET</td>
  2035. <td>/resource</td>
  2036. <td>index</td>
  2037. <td>resource.index</td>
  2038. </tr>
  2039. <tr class="even">
  2040. <td>GET</td>
  2041. <td>/resource/create</td>
  2042. <td>create</td>
  2043. <td>resource.create</td>
  2044. </tr>
  2045. <tr class="odd">
  2046. <td>POST</td>
  2047. <td>/resource</td>
  2048. <td>store</td>
  2049. <td>resource.store</td>
  2050. </tr>
  2051. <tr class="even">
  2052. <td>GET</td>
  2053. <td>/resource/{resource}</td>
  2054. <td>show</td>
  2055. <td>resource.show</td>
  2056. </tr>
  2057. <tr class="odd">
  2058. <td>GET</td>
  2059. <td>/resource/{resource}/edit</td>
  2060. <td>edit</td>
  2061. <td>resource.edit</td>
  2062. </tr>
  2063. <tr class="even">
  2064. <td>PUT/PATCH</td>
  2065. <td>/resource/{resource}</td>
  2066. <td>update</td>
  2067. <td>resource.update</td>
  2068. </tr>
  2069. <tr class="odd">
  2070. <td>DELETE</td>
  2071. <td>/resource/{resource}</td>
  2072. <td>destroy</td>
  2073. <td>resource.destroy</td>
  2074. </tr>
  2075. </tbody>
  2076. </table>
  2077. <p>所以我们只需要专注于创建 create, edit, show, destory 等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在 app/model 加个 class</p>
  2078. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> Athomes <span class="kw">extends</span> Eloquent {
  2079. <span class="kw">protected</span> <span class="kw">$table</span> = <span class="st">&#39;athomes&#39;</span><span class="ot">;</span>
  2080. }</code></pre></div>
  2081. <p>如果你想要的只是控制器Athomes的代码的话。。</p>
  2082. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
  2083. <span class="kw">public</span> <span class="kw">$restful</span>=<span class="kw">true</span><span class="ot">;</span>
  2084. <span class="kw">protected</span> <span class="kw">$athome</span><span class="ot">;</span>
  2085. <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>
  2086. {
  2087. <span class="kw">$this</span>---&gt;athome = <span class="kw">$athome</span> <span class="ot">;</span>
  2088. }
  2089. <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span>
  2090. {
  2091. <span class="kw">$maxid</span>=Athomes::all<span class="ot">();</span>
  2092. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
  2093. }
  2094. <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  2095. {
  2096. <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>
  2097. <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>
  2098. }
  2099. <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
  2100. {
  2101. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  2102. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required&#39;</span><span class="ot">,</span>
  2103. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2104. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2105. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  2106. <span class="ot">);</span>
  2107. <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>
  2108. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  2109. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/create&#39;</span><span class="ot">)</span>
  2110. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">)</span>
  2111. -&gt;withInput<span class="ot">(</span>Input::except<span class="ot">(</span><span class="st">&#39;password&#39;</span><span class="ot">));</span>
  2112. } <span class="kw">else</span> {
  2113. <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
  2114. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  2115. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  2116. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  2117. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  2118. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  2119. 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>
  2120. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2121. }
  2122. }
  2123. <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2124. {
  2125. <span class="kw">$myid</span>=Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2126. <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>
  2127. -&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>
  2128. -&gt;get<span class="ot">();</span>
  2129. <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
  2130. }
  2131. <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2132. {
  2133. <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2134. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.edit&#39;</span><span class="ot">)</span>
  2135. -&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>
  2136. }
  2137. <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2138. {
  2139. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  2140. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required|&#39;</span><span class="ot">,</span>
  2141. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2142. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2143. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  2144. <span class="ot">);</span>
  2145. <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>
  2146. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  2147. <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>
  2148. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
  2149. } <span class="kw">else</span> {
  2150. <span class="kw">$nerd</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2151. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  2152. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  2153. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  2154. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  2155. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  2156. 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>
  2157. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2158. }
  2159. }
  2160. <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
  2161. {
  2162. <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
  2163. <span class="kw">$athome</span>-&gt;delete<span class="ot">();</span>
  2164. <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>
  2165. {
  2166. <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>
  2167. }
  2168. 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>
  2169. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2170. }
  2171. }</code></pre></div>
  2172. <p>希望你能读懂,没有的话,继续。</p>
  2173. <p>下面这部分来自于之前的博客,这里就不多加论述了。 这个也就是我们要的模板,</p>
  2174. <h3 id="修改create"><span class="header-section-number">15.3.1</span> 修改Create()</h3>
  2175. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  2176. {
  2177. <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>
  2178. <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>
  2179. }</code></pre></div>
  2180. <p>这里需要在app/views/创建一个athome里面创建一个create.blade.php,至于maxid,暂时还不需要,后面会用到show。如果只需要模板,可以简化为</p>
  2181. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
  2182. {
  2183. <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">&#39;athome.create&#39;</span><span class="ot">);</span>
  2184. }</code></pre></div>
  2185. <p>这里只是对其中代码的进行一下说明。</p>
  2186. <h3 id="创建表单"><span class="header-section-number">15.3.2</span> 创建表单</h3>
  2187. <h4 id="创建表单之前"><span class="header-section-number">15.3.2.1</span> 创建表单之前</h4>
  2188. <p>由于使用到了bootstrap以及bootstrap-select,记得添加css。</p>
  2189. <div class="sourceCode"><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>
  2190. <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></div>
  2191. <p>以及javascript</p>
  2192. <div class="sourceCode"><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>
  2193. <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>
  2194. <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>
  2195. <span class="kw">&lt;script&gt;</span>
  2196. <span class="at">$</span>(<span class="st">&#39;.selectpicker&#39;</span>).<span class="at">selectpicker</span>()<span class="op">;</span>
  2197. <span class="op">&lt;</span><span class="ss">/script&gt;</span></code></pre></div>
  2198. <h4 id="创建表单-1"><span class="header-section-number">15.3.2.2</span> 创建表单</h4>
  2199. <p>这里用到的是之前提到的那个作者写下的,稍微修改了一下。</p>
  2200. <div class="sourceCode"><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>
  2201. {{ HTML::ul($errors-&gt;all()) }}
  2202. {{ Form::open(array(&#39;url&#39; =&gt; &#39;athome&#39;)) }}
  2203. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2204. {{ Form::label(&#39;led1&#39;, &#39;开关1&#39;) }}
  2205. {{ Form::select(&#39;led1&#39;,array(&#39;关&#39;,&#39;开&#39;),$selected=NULL,array(&#39;class&#39;=&gt;&#39;selectpicker&#39;)) }}
  2206. <span class="kw">&lt;/div&gt;</span>
  2207. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2208. {{ Form::label(&#39;sensors1&#39;, &#39;sensors1&#39;) }}
  2209. {{ Form::text(&#39;sensors1&#39;, Input::old(&#39;sensors1&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2210. <span class="kw">&lt;/div&gt;</span>
  2211. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2212. {{ Form::label(&#39;sensors2&#39;, &#39;sensors2&#39;) }}
  2213. {{ Form::text(&#39;sensors2&#39;, Input::old(&#39;sensors2&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2214. <span class="kw">&lt;/div&gt;</span>
  2215. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2216. {{ Form::label(&#39;temperature&#39;, &#39;temperature&#39;) }}
  2217. {{ Form::text(&#39;temperature&#39;, Input::old(&#39;temperature&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2218. <span class="kw">&lt;/div&gt;</span>
  2219. {{ Form::submit(&#39;Create!&#39;, array(&#39;class&#39; =&gt; &#39;btn btn-primary&#39;)) }}
  2220. {{ Form::close() }}
  2221. <span class="kw">&lt;/div&gt;</span></code></pre></div>
  2222. <p>开关一开始打算用 checkbox,加上 bootstrap-switch 实现 ON OFF 弱弱地觉得还是没掌握好的节奏,所以最后用 select 来实现。</p>
  2223. <p>还需要修改一下之前的 create(),添加一行</p>
  2224. <div class="sourceCode"><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></div>
  2225. <p>也就是添加完后,重定向到首页查看,最后例子给出的 create 如下</p>
  2226. <div class="sourceCode"><pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
  2227. {
  2228. <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
  2229. <span class="st">&#39;led1&#39;</span>=&gt;<span class="st">&#39;required&#39;</span><span class="ot">,</span>
  2230. <span class="st">&#39;sensors1&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2231. <span class="st">&#39;sensors2&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span><span class="ot">,</span>
  2232. <span class="st">&#39;temperature&#39;</span> =&gt; <span class="st">&#39;required|numeric|Min:-50|Max:80&#39;</span>
  2233. <span class="ot">);</span>
  2234. <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>
  2235. <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>-&gt;fails<span class="ot">())</span> {
  2236. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome/create&#39;</span><span class="ot">)</span>
  2237. -&gt;withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
  2238. } <span class="kw">else</span> {
  2239. <span class="co">// store</span>
  2240. <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
  2241. <span class="kw">$nerd</span>-&gt;sensors1 = Input::get<span class="ot">(</span><span class="st">&#39;sensors1&#39;</span><span class="ot">);</span>
  2242. <span class="kw">$nerd</span>-&gt;sensors2 = Input::get<span class="ot">(</span><span class="st">&#39;sensors2&#39;</span><span class="ot">);</span>
  2243. <span class="kw">$nerd</span>-&gt;temperature = Input::get<span class="ot">(</span><span class="st">&#39;temperature&#39;</span><span class="ot">);</span>
  2244. <span class="kw">$nerd</span>-&gt;led1 = Input::get<span class="ot">(</span><span class="st">&#39;led1&#39;</span><span class="ot">);</span>
  2245. <span class="kw">$nerd</span>-&gt;save<span class="ot">();</span>
  2246. 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>
  2247. <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">&#39;athome&#39;</span><span class="ot">);</span>
  2248. }
  2249. }</code></pre></div>
  2250. <h3 id="编辑模板"><span class="header-section-number">15.3.3</span> 编辑模板</h3>
  2251. <p>完整的 blade 模板文件</p>
  2252. <div class="sourceCode"><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>
  2253. <span class="kw">&lt;html&gt;</span>
  2254. <span class="kw">&lt;head&gt;</span>
  2255. <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>
  2256. <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>
  2257. <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>
  2258. <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>
  2259. <span class="kw">&lt;title&gt;</span>@yield(&#39;title&#39;)<span class="kw">&lt;/title&gt;</span>
  2260. <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>
  2261. <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>
  2262. <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>
  2263. <span class="kw">&lt;/head&gt;</span>
  2264. <span class="kw">&lt;body&gt;</span>
  2265. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;container&quot;</span><span class="kw">&gt;</span>
  2266. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;container&quot;</span><span class="kw">&gt;</span>
  2267. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;row-fluid&quot;</span><span class="kw">&gt;</span>
  2268. <span class="kw">&lt;h1&gt;</span>Edit {{ $athome-&gt;id }}<span class="kw">&lt;/h1&gt;</span>
  2269. <span class="co">&lt;!-- if there are creation errors, they will show here --&gt;</span>
  2270. {{ HTML::ul($errors-&gt;all()) }}
  2271. {{ Form::model($athome, array(&#39;route&#39; =&gt; array(&#39;athome.update&#39;, $athome-&gt;id), &#39;method&#39; =&gt; &#39;PUT&#39;)) }}
  2272. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2273. {{ Form::label(&#39;led1&#39;, &#39;开关1&#39;) }}
  2274. {{ Form::select(&#39;led1&#39;,array(&#39;关&#39;,&#39;开&#39;),$selected=NULL,array(&#39;class&#39;=&gt;&#39;selectpicker&#39;)) }}
  2275. <span class="kw">&lt;/div&gt;</span>
  2276. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2277. {{ Form::label(&#39;sensors1&#39;, &#39;传感器1&#39;) }}
  2278. {{ Form::text(&#39;sensors1&#39;, Input::old(&#39;sensors1&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2279. <span class="kw">&lt;/div&gt;</span>
  2280. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2281. {{ Form::label(&#39;sensors2&#39;, &#39;传感器2&#39;) }}
  2282. {{ Form::text(&#39;sensors2&#39;, Input::old(&#39;sensors2&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2283. <span class="kw">&lt;/div&gt;</span>
  2284. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;form-group&quot;</span><span class="kw">&gt;</span>
  2285. {{ Form::label(&#39;temperature&#39;, &#39;温度传感器&#39;) }}
  2286. {{ Form::text(&#39;temperature&#39;, Input::old(&#39;temperature&#39;), array(&#39;class&#39; =&gt; &#39;form-control&#39;)) }}
  2287. <span class="kw">&lt;/div&gt;</span>
  2288. {{ Form::submit(&#39;Edit the Nerd!&#39;, array(&#39;class&#39; =&gt; &#39;btn btn-primary&#39;)) }}
  2289. {{ Form::close() }}
  2290. <span class="kw">&lt;/div&gt;</span>
  2291. <span class="kw">&lt;/div&gt;</span>
  2292. <span class="kw">&lt;div</span><span class="ot"> class=</span><span class="st">&quot;footer&quot;</span><span class="kw">&gt;</span>
  2293. <span class="kw">&lt;p&gt;</span>© Company 2013<span class="kw">&lt;/p&gt;</span>
  2294. <span class="kw">&lt;/div&gt;</span>
  2295. <span class="kw">&lt;/div&gt;</span>
  2296. <span class="kw">&lt;/div&gt;</span>
  2297. <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>
  2298. <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>
  2299. <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>
  2300. <span class="kw">&lt;script&gt;</span>
  2301. <span class="at">$</span>(<span class="st">&#39;.selectpicker&#39;</span>).<span class="at">selectpicker</span>()<span class="op">;</span>
  2302. <span class="op">&lt;</span><span class="ss">/script&gt;</span>
  2303. <span class="ss">&lt;script type=&quot;text/javascript</span><span class="st">&quot; src=&quot;</span><span class="op">&lt;?=</span> <span class="at">url</span>(<span class="st">&#39;js/log.js&#39;</span>) <span class="op">?&gt;</span><span class="st">&quot;&gt;&lt;/script&gt;</span>
  2304. <span class="op">&lt;</span><span class="ss">/body&gt;</span>
  2305. <span class="ss">&lt;/html</span><span class="op">&gt;</span></code></pre></div>
  2306. <p>效果图:</p>
  2307. <div class="figure">
  2308. <img src="./images/edit.png" alt="Blade Edit" />
  2309. <p class="caption">Blade Edit</p>
  2310. </div>
  2311. <p>最后效果见:<a href="http://b.phodal.com/" class="uri">http://b.phodal.com/</a></p>
  2312. <h1 id="前端显示"><span class="header-section-number">16</span> 前端显示</h1>
  2313. <h2 id="库与车轮子"><span class="header-section-number">16.1</span> 库与车轮子</h2>
  2314. <p>在多数的情况下我们都没有理由也没有必要去重新发明我们的车轮,在这时使用库会是一个比较好的做法。</p>
  2315. <h2 id="库"><span class="header-section-number">16.2</span> 库</h2>
  2316. <h3 id="jquery"><span class="header-section-number">16.2.1</span> jQuery</h3>
  2317. <blockquote>
  2318. <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>
  2319. </blockquote>
  2320. <p>在我们的代码里用到了jQuery,然而这是一种简单而且快速有速地方法。</p>
  2321. <h3 id="jquery-mobile"><span class="header-section-number">16.2.2</span> jQuery Mobile</h3>
  2322. <blockquote>
  2323. <p>jQuery Mobile是jQuery 在手机上和平板设备上的版本。jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架。支持全球主流的移动平台。jQuery Mobile开发团队说:能开发这个项目,我们非常兴奋。移动Web太需要一个跨浏览器的框架,让开发人员开发出真正的移动Web网站。</p>
  2324. </blockquote>
  2325. <p>整个展示页面由三部分组成,即<code>header</code>,<code>content</code>,<code>footer</code>。而我们主要需要关心的是content,也就是真下的内容。</p>
  2326. <p>我们只需要结合<code>HighChart</code>来设计我们的content就可以了,下面是一个简单地<code>HighChart</code>示例:</p>
  2327. <pre><code>&lt;div id=&quot;Tchart&quot; style=&quot;min-width:800px;width:100%;height:300px&quot;&gt;&lt;/div&gt;
  2328. &lt;/div&gt;</code></pre>
  2329. <p>剩下的事就由<code>HighChart</code>来做。</p>
  2330. <p>最后代码如下所示</p>
  2331. <div class="sourceCode"><pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;body&gt;</span>
  2332. <span class="kw">&lt;div</span><span class="ot"> data-role=</span><span class="st">&quot;page&quot;</span><span class="kw">&gt;</span>
  2333. <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>
  2334. <span class="kw">&lt;h1&gt;</span>基础控制<span class="kw">&lt;/h1&gt;</span>
  2335. <span class="kw">&lt;/div&gt;</span>
  2336. <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>
  2337. <span class="kw">&lt;h3&gt;</span>查看温度情况<span class="kw">&lt;/h3&gt;</span>
  2338. <span class="kw">&lt;div&gt;</span>
  2339. <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>
  2340. <span class="kw">&lt;/div&gt;</span>
  2341. <span class="kw">&lt;/div&gt;</span>
  2342. <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>
  2343. <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>
  2344. <span class="kw">&lt;/div&gt;</span>
  2345. <span class="kw">&lt;/body&gt;</span></code></pre></div>
  2346. <p>可以看到上面的代码与一般的HTML不同的地方是<code>data-role</code>,<code>data-collapsed</code>,<code>data-position</code>,而这些是jQuery Mobile所拥有的data属性。</p>
  2347. <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>
  2348. <p>同上,我们也可以在网上找到其他相对就的属性。</p>
  2349. <h2 id="网站前台显示"><span class="header-section-number">16.3</span> 网站前台显示</h2>
  2350. <h3 id="highcharts"><span class="header-section-number">16.3.1</span> Highcharts</h3>
  2351. <p>Highcharts有以下的特点</p>
  2352. <ul>
  2353. <li>兼容性:兼容当今所有的浏览器,包括 iPhone、IE 和火狐等等;</li>
  2354. <li>对个人用户完全免费;</li>
  2355. <li>纯JS,无BS;</li>
  2356. <li>支持大部分的图表类型:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散布图;</li>
  2357. <li>跨语言:不管是 PHP、Asp.net 还是 Java 都可以使用,它只需要三个文件:一个是Highcharts 的核心文件 highcharts.js,还有 a canvas emulator for IE 和 Jquery类库或者 MooTools 类库;</li>
  2358. <li>提示功能:鼠标移动到图表的某一点上有提示信息;</li>
  2359. <li>放大功能:选中图表部分放大,近距离观察图表;</li>
  2360. <li>易用性:无需要特殊的开发技能,只需要设置一下选项就可以制作适合自己的图表;</li>
  2361. <li>时间轴:可以精确到毫秒;</li>
  2362. </ul>
  2363. <p>在这里只需将需要处理的数据存储到数组中,便可以将其渲染成为图形,下面的温度走势图便是基于Highcharts的结果:</p>
  2364. <div class="figure">
  2365. <img src="./images/temperture.png" alt="Temperture" />
  2366. <p class="caption">Temperture</p>
  2367. </div>
  2368. <p>先看看最后代码如下所示:</p>
  2369. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength <span class="op">=</span> []<span class="op">;</span>
  2370. <span class="kw">function</span> <span class="at">drawTemp</span>() <span class="op">{</span>
  2371. <span class="kw">var</span> zero <span class="op">=</span> []<span class="op">;</span>
  2372. <span class="va">$</span>.<span class="at">getJSON</span>(<span class="st">&#39;/athome/&#39;</span><span class="op">,</span> <span class="kw">function</span>(json) <span class="op">{</span>
  2373. <span class="kw">var</span> items <span class="op">=</span> []<span class="op">;</span>
  2374. <span class="va">dataLength</span>.<span class="at">push</span>(<span class="va">json</span>.<span class="at">length</span>)<span class="op">;</span>
  2375. <span class="va">$</span>.<span class="at">each</span>(json<span class="op">,</span> <span class="kw">function</span>(key<span class="op">,</span> val) <span class="op">{</span>
  2376. <span class="va">zero</span>.<span class="at">push</span>(<span class="va">val</span>.<span class="at">temperature</span>)<span class="op">;</span>
  2377. <span class="op">}</span>)<span class="op">;</span>
  2378. chart <span class="op">=</span> <span class="kw">new</span> <span class="va">Highcharts</span>.<span class="at">Chart</span>(<span class="op">{</span>
  2379. <span class="dt">color</span><span class="op">:</span> <span class="op">{</span>
  2380. <span class="dt">linearGradient</span><span class="op">:</span> <span class="op">{</span>
  2381. <span class="dt">x1</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
  2382. <span class="dt">x2</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
  2383. <span class="dt">y1</span><span class="op">:</span> <span class="dv">0</span><span class="op">,</span>
  2384. <span class="dt">y1</span><span class="op">:</span> <span class="dv">1</span>
  2385. <span class="op">},</span>
  2386. <span class="dt">stops</span><span class="op">:</span> [
  2387. [<span class="dv">0</span><span class="op">,</span> <span class="st">&#39;#003399&#39;</span>]<span class="op">,</span>
  2388. [<span class="dv">1</span><span class="op">,</span> <span class="st">&#39;#3366AA&#39;</span>]
  2389. ]
  2390. <span class="op">},</span>
  2391. <span class="dt">chart</span><span class="op">:</span> <span class="op">{</span>
  2392. <span class="dt">renderTo</span><span class="op">:</span> <span class="st">&#39;Tchart&#39;</span><span class="op">,</span>
  2393. <span class="dt">type</span><span class="op">:</span> <span class="st">&#39;spline&#39;</span>
  2394. <span class="op">},</span>
  2395. <span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
  2396. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;本月温度情况&#39;</span>
  2397. <span class="op">},</span>
  2398. <span class="dt">subtitle</span><span class="op">:</span> <span class="op">{</span>
  2399. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;&#39;</span>
  2400. <span class="op">},</span>
  2401. <span class="dt">xAxis</span><span class="op">:</span> <span class="op">{</span>
  2402. <span class="dt">categories</span><span class="op">:</span> []<span class="op">,</span>
  2403. <span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
  2404. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;&#39;</span>
  2405. <span class="op">}</span>
  2406. <span class="op">},</span>
  2407. <span class="dt">yAxis</span><span class="op">:</span> <span class="op">{</span>
  2408. <span class="dt">title</span><span class="op">:</span> <span class="op">{</span>
  2409. <span class="dt">text</span><span class="op">:</span> <span class="st">&#39;温度 (°C)&#39;</span>
  2410. <span class="op">}</span>
  2411. <span class="op">},</span>
  2412. <span class="dt">tooltip</span><span class="op">:</span> <span class="op">{</span>
  2413. <span class="dt">backgroundColor</span><span class="op">:</span> <span class="st">&#39;#FCFFC5&#39;</span><span class="op">,</span>
  2414. <span class="dt">borderColor</span><span class="op">:</span> <span class="st">&#39;black&#39;</span><span class="op">,</span>
  2415. <span class="dt">borderRadius</span><span class="op">:</span> <span class="dv">10</span><span class="op">,</span>
  2416. <span class="dt">borderWidth</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  2417. <span class="dt">enabled</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
  2418. <span class="dt">formatter</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{</span>
  2419. <span class="cf">return</span> <span class="st">&#39;&lt;b&gt;&#39;</span> <span class="op">+</span> <span class="kw">this</span>.<span class="va">series</span>.<span class="at">name</span> <span class="op">+</span> <span class="st">&#39;&lt;/b&gt;&lt;br/&gt;&#39;</span> <span class="op">+</span> <span class="kw">this</span>.<span class="at">x</span> <span class="op">+</span> <span class="st">&#39;: &#39;</span> <span class="op">+</span> <span class="kw">this</span>.<span class="at">y</span> <span class="op">+</span> <span class="st">&#39;°C&#39;</span><span class="op">;</span>
  2420. <span class="op">}</span>
  2421. <span class="op">},</span>
  2422. <span class="dt">legend</span><span class="op">:</span> <span class="op">{</span>
  2423. <span class="dt">layout</span><span class="op">:</span> <span class="st">&#39;vertical&#39;</span><span class="op">,</span>
  2424. <span class="dt">align</span><span class="op">:</span> <span class="st">&#39;right&#39;</span><span class="op">,</span>
  2425. <span class="dt">verticalAlign</span><span class="op">:</span> <span class="st">&#39;top&#39;</span><span class="op">,</span>
  2426. <span class="dt">x</span><span class="op">:</span> <span class="op">-</span><span class="dv">10</span><span class="op">,</span>
  2427. <span class="dt">y</span><span class="op">:</span> <span class="dv">100</span><span class="op">,</span>
  2428. <span class="dt">borderWidth</span><span class="op">:</span> <span class="dv">0</span>
  2429. <span class="op">},</span>
  2430. <span class="dt">plotOptions</span><span class="op">:</span> <span class="op">{</span>
  2431. <span class="dt">line</span><span class="op">:</span> <span class="op">{</span>
  2432. <span class="dt">dataLabels</span><span class="op">:</span> <span class="op">{</span>
  2433. <span class="dt">enabled</span><span class="op">:</span> <span class="kw">true</span>
  2434. <span class="op">},</span>
  2435. <span class="dt">enableMouseTracking</span><span class="op">:</span> <span class="kw">false</span>
  2436. <span class="op">}</span>
  2437. <span class="op">},</span>
  2438. <span class="dt">series</span><span class="op">:</span> [<span class="op">{</span>
  2439. <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;本月&#39;</span><span class="op">,</span>
  2440. <span class="dt">data</span><span class="op">:</span> zero
  2441. <span class="op">},</span> <span class="op">{</span>
  2442. <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;对比&#39;</span><span class="op">,</span>
  2443. <span class="dt">data</span><span class="op">:</span> [<span class="fl">26.0</span>]
  2444. <span class="op">}</span>]
  2445. <span class="op">}</span>)<span class="op">;</span>
  2446. <span class="op">}</span>)<span class="op">;</span>
  2447. <span class="op">};</span>
  2448. <span class="kw">function</span> <span class="at">showTemper</span>() <span class="op">{</span>
  2449. <span class="kw">var</span> length <span class="op">=</span> dataLength[<span class="dv">0</span>]<span class="op">;</span>
  2450. <span class="va">$</span>.<span class="at">ajax</span>(<span class="op">{</span>
  2451. <span class="dt">url</span><span class="op">:</span> <span class="st">&#39;/athome/&#39;</span> <span class="op">+</span> length<span class="op">,</span>
  2452. <span class="dt">type</span><span class="op">:</span> <span class="st">&#39;GET&#39;</span><span class="op">,</span>
  2453. <span class="dt">dataType</span><span class="op">:</span> <span class="st">&#39;json&#39;</span><span class="op">,</span>
  2454. <span class="dt">async</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
  2455. <span class="dt">timeout</span><span class="op">:</span> <span class="dv">1000</span><span class="op">,</span>
  2456. <span class="dt">error</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{},</span>
  2457. <span class="dt">success</span><span class="op">:</span> <span class="kw">function</span>(sdata) <span class="op">{</span>
  2458. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">empty</span>()<span class="op">;</span>
  2459. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">append</span>(<span class="va">sdata</span>.<span class="at">temperature</span>)<span class="op">;</span>
  2460. <span class="op">}</span>
  2461. <span class="op">}</span>)<span class="op">;</span>
  2462. <span class="op">};</span>
  2463. <span class="at">$</span>(document).<span class="at">ready</span>(<span class="kw">function</span>() <span class="op">{</span>
  2464. <span class="at">setInterval</span>(<span class="st">&quot;drawTemp();&quot;</span><span class="op">,</span> <span class="dv">5000</span>)<span class="op">;</span>
  2465. <span class="at">setInterval</span>(<span class="st">&quot;showTemper();&quot;</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span>
  2466. <span class="at">drawTemp</span>()<span class="op">;</span>
  2467. <span class="at">showTemper</span>()<span class="op">;</span>
  2468. <span class="op">}</span>)<span class="op">;</span></code></pre></div>
  2469. <p>我们先把<code>HighChart</code>部分的代码删去,就变成下面的内容:</p>
  2470. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength <span class="op">=</span> []<span class="op">;</span>
  2471. <span class="kw">function</span> <span class="at">drawTemp</span>() <span class="op">{</span>
  2472. <span class="kw">var</span> zero <span class="op">=</span> []<span class="op">;</span>
  2473. <span class="va">$</span>.<span class="at">getJSON</span>(<span class="st">&#39;/athome/&#39;</span><span class="op">,</span> <span class="kw">function</span>(json) <span class="op">{</span>
  2474. <span class="kw">var</span> items <span class="op">=</span> []<span class="op">;</span>
  2475. <span class="va">dataLength</span>.<span class="at">push</span>(<span class="va">json</span>.<span class="at">length</span>)<span class="op">;</span>
  2476. <span class="va">$</span>.<span class="at">each</span>(json<span class="op">,</span> <span class="kw">function</span>(key<span class="op">,</span> val) <span class="op">{</span>
  2477. <span class="va">zero</span>.<span class="at">push</span>(<span class="va">val</span>.<span class="at">temperature</span>)<span class="op">;</span>
  2478. <span class="op">}</span>)<span class="op">;</span>
  2479. <span class="op">}</span>)<span class="op">;</span>
  2480. <span class="op">};</span>
  2481. <span class="kw">function</span> <span class="at">showTemper</span>() <span class="op">{</span>
  2482. <span class="kw">var</span> length <span class="op">=</span> dataLength[<span class="dv">0</span>]<span class="op">;</span>
  2483. <span class="va">$</span>.<span class="at">ajax</span>(<span class="op">{</span>
  2484. <span class="dt">url</span><span class="op">:</span> <span class="st">&#39;/athome/&#39;</span> <span class="op">+</span> length<span class="op">,</span>
  2485. <span class="dt">type</span><span class="op">:</span> <span class="st">&#39;GET&#39;</span><span class="op">,</span>
  2486. <span class="dt">dataType</span><span class="op">:</span> <span class="st">&#39;json&#39;</span><span class="op">,</span>
  2487. <span class="dt">async</span><span class="op">:</span> <span class="kw">true</span><span class="op">,</span>
  2488. <span class="dt">timeout</span><span class="op">:</span> <span class="dv">1000</span><span class="op">,</span>
  2489. <span class="dt">error</span><span class="op">:</span> <span class="kw">function</span>() <span class="op">{},</span>
  2490. <span class="dt">success</span><span class="op">:</span> <span class="kw">function</span>(sdata) <span class="op">{</span>
  2491. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">empty</span>()<span class="op">;</span>
  2492. <span class="at">$</span>(<span class="st">&#39;.temperStatus&#39;</span>).<span class="at">append</span>(<span class="va">sdata</span>.<span class="at">temperature</span>)<span class="op">;</span>
  2493. <span class="op">}</span>
  2494. <span class="op">}</span>)<span class="op">;</span>
  2495. <span class="op">};</span>
  2496. <span class="at">$</span>(document).<span class="at">ready</span>(<span class="kw">function</span>() <span class="op">{</span>
  2497. <span class="at">setInterval</span>(<span class="st">&quot;drawTemp();&quot;</span><span class="op">,</span> <span class="dv">5000</span>)<span class="op">;</span>
  2498. <span class="at">setInterval</span>(<span class="st">&quot;showTemper();&quot;</span><span class="op">,</span> <span class="dv">800</span>)<span class="op">;</span>
  2499. <span class="at">drawTemp</span>()<span class="op">;</span>
  2500. <span class="at">showTemper</span>()<span class="op">;</span>
  2501. <span class="op">}</span>)<span class="op">;</span></code></pre></div>
  2502. <p>代码看上去是由两部分组成的<code>drawTemperature</code>和<code>showTemperature</code></p>
  2503. <h3 id="实时数据"><span class="header-section-number">16.3.2</span> 实时数据</h3>
  2504. <p>剥离后的Ajax部分代码如下所示,主要用的是jQuery框架的getJSON来实现的</p>
  2505. <pre><code>var dataLength = [];
  2506. function drawTemp() {
  2507. var zero = [];
  2508. $.getJSON(&#39;/athome/&#39;, function(json) {
  2509. var items = [];
  2510. dataLength.push(json.length);
  2511. $.each(json, function(key, val) {
  2512. zero.push(val.temperature);
  2513. });
  2514. };</code></pre>
  2515. <p>实际上,我们做的只是从/athome/下面获取数据,再将数据堆到数组里面,再把这部分放到图形中。等等,什么是Ajax?</p>
  2516. <p>AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。 AJAX 不是新的编程语言,而是一种使用现有标准的新方法。 AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。 JSON我们前面也已经了解过了,看看getJSON吧。</p>
  2517. <p>jQuery. getJSON</p>
  2518. <p>方法定义:jQuery.getJSON( url, data, callback )</p>
  2519. <p>通过get请求得到json数据</p>
  2520. <p>·url用于提供json数据的地址页 ·data(Optional)用于传送到服务器的键值对 ·callback(Optional)回调函数,json数据请求成功后的处理函数 我想你似乎应该懂得了一点,就是在不刷新网页的同时,用javascript获取数据放到图表上,就这么简单。</p>
  2521. <h1 id="restful的coap协议"><span class="header-section-number">17</span> RESTful的CoAP协议</h1>
  2522. <h2 id="coap-嵌入式系统的rest"><span class="header-section-number">17.1</span> CoAP: 嵌入式系统的REST</h2>
  2523. <p>引自维基百科上的介绍,用的是谷歌翻译。。。</p>
  2524. <blockquote>
  2525. <p>受约束的应用协议(COAP)是一种软件协议旨在以非常简单的电子设备,使他们能够在互联网上进行交互式通信中使用。它特别针对小型低功率传感器,开关,阀门和需要被控制或监督远程,通过标准的Internet网络类似的组件。 COAP是一个应用层协议,该协议是用于在资源受限的网络连接设备,例如无线传感器网络节点使用。 COAP被设计为容易地转换为HTTP与Web简化集成,同时也能满足特殊的要求,例如多播支持,非常低的开销,和简单性。多播,低开销,以及简单性是因特网极其重要物联网(IOT)和机器对机器(M2M)设备,这往往是积重难返,有太多的内存和电源,比传统的互联网设备有。因此,效率是非常重要的。 COAP可以在支持UDP或UDP的模拟大多数设备上运行。</p>
  2526. </blockquote>
  2527. <p>简单地来说,CoAP是简化了HTTP协议的RESTful API,因而也只提供了REST的四个方法,即PUT,GET,POST和DELETE。对于微小的资源受限,在资源受限的通信的IP的网络,HTTP不是一种可行的选择。它占用了太多的资源和太多的带宽。而对于物联网这种嵌入式设备来说,关于资源与带宽,是我们需要优先考虑的内容。</p>
  2528. <ul>
  2529. <li>CoAP采用了二进制报头,而不是文本报头(text header)</li>
  2530. <li>CoAP降低了头的可用选项的数量。</li>
  2531. <li>CoAP减少了一些HTTP的方法</li>
  2532. <li>CoAP可以支持检测装置</li>
  2533. </ul>
  2534. <h2 id="coap-命令行工具"><span class="header-section-number">17.2</span> CoAP 命令行工具</h2>
  2535. <p>为了测试测试我们的代码是否是正确工作,我们需要一个CoAP的命令行工具。目前有两个不错的工具可以使用。</p>
  2536. <ul>
  2537. <li>CoAP-cli,一个基于NodeJS的CoAP命令行工具,其核心是基于Node-CoAP库。</li>
  2538. <li>libcooap,一个用C写的CoAP命令行工具。</li>
  2539. <li>FireFox Copper, 一个Firefox的插件。</li>
  2540. </ul>
  2541. <h3 id="node-coap-cli"><span class="header-section-number">17.2.1</span> Node CoAP CLI</h3>
  2542. <p>安装命令如下</p>
  2543. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"> <span class="kw">npm</span> install coap-cli -g </code></pre></div>
  2544. <h4 id="coap命令行"><span class="header-section-number">17.2.1.1</span> CoAP命令行</h4>
  2545. <p>在coap-cli中,一共有四个方法。分别表示REST的四种不同的方式:</p>
  2546. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Commands</span>:
  2547. <span class="kw">get</span> performs a GET request
  2548. <span class="kw">put</span> performs a PUT request
  2549. <span class="kw">post</span> performs a POST request
  2550. <span class="kw">delete</span> performs a DELETE request</code></pre></div>
  2551. <p>在这里,我们用<a href="coap://vs0.inf.ethz.ch/" class="uri">coap://vs0.inf.ethz.ch/</a>来作一个简单的测试</p>
  2552. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> get coap://vs0.inf.ethz.ch/
  2553. <span class="kw">(2.05)</span> <span class="kw">************************************************************</span>
  2554. <span class="kw">I-D</span></code></pre></div>
  2555. <p>测试一下现在的最小的物联网系统CoAP版</p>
  2556. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> get coap://iot-coap.phodal.com/id/1
  2557. <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></div>
  2558. <h3 id="libcoap"><span class="header-section-number">17.2.2</span> libcoap</h3>
  2559. <h4 id="mac-os-libcoap安装"><span class="header-section-number">17.2.2.1</span> mac os libcoap安装</h4>
  2560. <p>Mac OS下可以直接用</p>
  2561. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install libcoap</code></pre></div>
  2562. <h4 id="ubuntu-libcoap安装"><span class="header-section-number">17.2.2.2</span> Ubuntu libcoap安装</h4>
  2563. <p>Ubuntu GNU/Linux下</p>
  2564. <h4 id="windows-libcoap安装"><span class="header-section-number">17.2.2.3</span> Windows libcoap安装</h4>
  2565. <p>Windows 下</p>
  2566. <p>安装完libcoap,我们可以直接用自带的两个命令</p>
  2567. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-client</span>
  2568. <span class="kw">coap-server</span></code></pre></div>
  2569. <p>1.用coap-server启一个CoAP服务</p>
  2570. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-server</span></code></pre></div>
  2571. <p>2.客户端获取数据</p>
  2572. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-client</span> -m get coap://localhost</code></pre></div>
  2573. <p>返回结果</p>
  2574. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">v</span>:1 t:0 tkl:0 c:1 id:37109
  2575. <span class="kw">This</span> is a test server made with libcoap (see http://libcoap.sf.net)
  2576. <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></div>
  2577. <h3 id="firefox-copper"><span class="header-section-number">17.2.3</span> Firefox Copper</h3>
  2578. <p>为了能访问<a href="coap://localhost/" class="uri">coap://localhost/</a>,于是我们便需要安装一个Firefox并安装一个名为Copper的插件。</p>
  2579. <ol style="list-style-type: decimal">
  2580. <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>
  2581. <li><p>作为测试我们同样可以访问 <a href="coap://vs0.inf.ethz.ch:5683/" class="uri">coap://vs0.inf.ethz.ch:5683/</a></p></li>
  2582. </ol>
  2583. <h2 id="coap-helloworld"><span class="header-section-number">17.3</span> CoAP Hello,World</h2>
  2584. <p>接着我们便开始试试做一个简单的CoAP协议的应用:</p>
  2585. <p>这里用到的是一个Nodejs的扩展Node-CoAP</p>
  2586. <blockquote>
  2587. <p>node-coap is a client and server library for CoAP modelled after the http module.</p>
  2588. </blockquote>
  2589. <p>Node-CoAP是一个客户端和服务端的库用于CoAP的模块建模。创建一个package.json文件,添加这个库</p>
  2590. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="op">{</span>
  2591. <span class="st">&quot;dependencies&quot;</span><span class="op">:{</span>
  2592. <span class="st">&quot;coap&quot;</span><span class="op">:</span> <span class="st">&quot;0.7.2&quot;</span>
  2593. <span class="op">}</span>
  2594. <span class="op">}</span></code></pre></div>
  2595. <p>接着执行</p>
  2596. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install</code></pre></div>
  2597. <p>就可以安装好这个库。如果遇到权限问题,请用</p>
  2598. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> npm install</code></pre></div>
  2599. <p>接着,创建这样一个app.js</p>
  2600. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;coap&#39;</span>)
  2601. <span class="op">,</span> server <span class="op">=</span> <span class="va">coap</span>.<span class="at">createServer</span>()
  2602. <span class="va">server</span>.<span class="at">on</span>(<span class="st">&#39;request&#39;</span><span class="op">,</span> <span class="kw">function</span>(req<span class="op">,</span> res) <span class="op">{</span>
  2603. <span class="va">res</span>.<span class="at">end</span>(<span class="st">&#39;Hello &#39;</span> <span class="op">+</span> <span class="va">req</span>.<span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] <span class="op">+</span> <span class="st">&#39;</span><span class="sc">\n</span><span class="st">&#39;</span>)
  2604. <span class="op">}</span>)
  2605. <span class="va">server</span>.<span class="at">listen</span>(<span class="kw">function</span>() <span class="op">{</span>
  2606. <span class="va">console</span>.<span class="at">log</span>(<span class="st">&#39;server started&#39;</span>)
  2607. <span class="op">}</span>) </code></pre></div>
  2608. <p>执行</p>
  2609. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> app.js</code></pre></div>
  2610. <p>便可以在浏览器上访问了,因为现在什么也没有,所以什么也不会返回。</p>
  2611. <p>接着下来再创建一个client端的js,并运行之</p>
  2612. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;coap&#39;</span>)
  2613. <span class="op">,</span> req <span class="op">=</span> <span class="va">coap</span>.<span class="at">request</span>(<span class="st">&#39;coap://localhost/World&#39;</span>)
  2614. <span class="va">req</span>.<span class="at">on</span>(<span class="st">&#39;response&#39;</span><span class="op">,</span> <span class="kw">function</span>(res) <span class="op">{</span>
  2615. <span class="va">res</span>.<span class="at">pipe</span>(<span class="va">process</span>.<span class="at">stdout</span>)
  2616. <span class="op">}</span>)
  2617. <span class="va">req</span>.<span class="at">end</span>()</code></pre></div>
  2618. <p>就可以在console上输出</p>
  2619. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Hello</span> World </code></pre></div>
  2620. <p>也就达到了我们的目的,用CoAP协议创建一个服务,接着我们应该用它创建更多的东西,如产生JSON数据,以及RESTful。和HTTP版的最小物联网系统一样,CoAP版的最小物联网系统也是要返回JSON的。</p>
  2621. <h2 id="coap-数据库查询"><span class="header-section-number">17.4</span> CoAP 数据库查询</h2>
  2622. <h3 id="node-module"><span class="header-section-number">17.4.1</span> Node Module</h3>
  2623. <p>这说里NodeJS Module的意义是因为我们需要在别的地方引用到db_helper这个库,也就是下一小节要的讲的内容。</p>
  2624. <p>这样我们就可以在server.js类似于这样去引用这个js库。</p>
  2625. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> DBHelper <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;./db_helper.js&#39;</span>)<span class="op">;</span>
  2626. <span class="va">DBHelper</span>.<span class="at">initDB</span>()<span class="op">;</span></code></pre></div>
  2627. <p>而这样调用的前提是我们需要去声明这样的module,为了方便地导出函数功能调用。</p>
  2628. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">DBHelper</span>()<span class="op">{</span>
  2629. <span class="op">}</span>
  2630. <span class="va">DBHelper</span>.<span class="at">initDB</span> <span class="op">=</span> <span class="kw">function</span>()<span class="op">{};</span>
  2631. <span class="va">module</span>.<span class="at">exports</span> <span class="op">=</span> DBHelper<span class="op">;</span></code></pre></div>
  2632. <h3 id="node-sqlite3"><span class="header-section-number">17.4.2</span> Node-Sqlite3</h3>
  2633. <p>这次我们用的是SQLite3(你可以用MySQL,出于安全考虑用SQLite3,SQLite3产生的是一个文件)。一个简单的initDB函数</p>
  2634. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> db <span class="op">=</span> <span class="kw">new</span> <span class="va">sqlite3</span>.<span class="at">Database</span>(config[<span class="st">&quot;db_name&quot;</span>])<span class="op">;</span>
  2635. <span class="kw">var</span> create_table <span class="op">=</span> <span class="st">&#39;create table if not exists basic (&#39;</span> <span class="op">+</span> config[<span class="st">&quot;db_table&quot;</span>] <span class="op">+</span> <span class="st">&#39;);&#39;</span><span class="op">;</span>
  2636. <span class="va">db</span>.<span class="at">serialize</span>(<span class="kw">function</span>() <span class="op">{</span>
  2637. <span class="va">db</span>.<span class="at">run</span>(create_table)<span class="op">;</span>
  2638. <span class="va">_</span>.<span class="at">each</span>(config[<span class="st">&quot;init_table&quot;</span>]<span class="op">,</span> <span class="kw">function</span>(insert_data) <span class="op">{</span>
  2639. <span class="va">db</span>.<span class="at">run</span>(insert_data)<span class="op">;</span>
  2640. <span class="op">}</span>)<span class="op">;</span>
  2641. <span class="op">}</span>)<span class="op">;</span>
  2642. <span class="va">db</span>.<span class="at">close</span>()<span class="op">;</span></code></pre></div>
  2643. <p>首先从配置中读取db_name,接着创建table,然后调用underscore的each方法,创建几个数据。配置如下所示</p>
  2644. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">config <span class="op">=</span> <span class="op">{</span>
  2645. <span class="st">&quot;db_name&quot;</span><span class="op">:</span> <span class="st">&quot;iot.db&quot;</span><span class="op">,</span>
  2646. <span class="st">&quot;db_table&quot;</span><span class="op">:</span> <span class="st">&quot;id integer primary key, value text, sensors1 float, sensors2 float&quot;</span><span class="op">,</span>
  2647. <span class="st">&quot;init_table&quot;</span><span class="op">:</span>[
  2648. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, &#39;is id 1&#39;, 19, 20);&quot;</span><span class="op">,</span>
  2649. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, &#39;is id 2&#39;, 20, 21);&quot;</span>
  2650. ]<span class="op">,</span>
  2651. <span class="st">&quot;query_table&quot;</span><span class="op">:</span><span class="st">&quot;select * from basic;&quot;</span>
  2652. <span class="op">};</span></code></pre></div>
  2653. <p>而之前所提到的url查询所做的事情便是</p>
  2654. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">DBHelper</span>.<span class="at">urlQueryData</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> callback) <span class="op">{</span>
  2655. <span class="kw">var</span> db <span class="op">=</span> <span class="kw">new</span> <span class="va">sqlite3</span>.<span class="at">Database</span>(<span class="st">&quot;iot.db&quot;</span>)<span class="op">;</span>
  2656. <span class="kw">var</span> result <span class="op">=</span> []<span class="op">;</span>
  2657. <span class="va">console</span>.<span class="at">log</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] <span class="op">+</span> <span class="st">&quot;=&quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>])<span class="op">;</span>
  2658. <span class="va">db</span>.<span class="at">all</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] <span class="op">+</span> <span class="st">&quot;=&quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>]<span class="op">,</span> <span class="kw">function</span>(err<span class="op">,</span> rows) <span class="op">{</span>
  2659. <span class="va">db</span>.<span class="at">close</span>()<span class="op">;</span>
  2660. <span class="at">callback</span>(<span class="va">JSON</span>.<span class="at">stringify</span>(rows))<span class="op">;</span>
  2661. <span class="op">}</span>)<span class="op">;</span>
  2662. <span class="op">};</span></code></pre></div>
  2663. <p>将URL传进来,便解析这个参数,接着再放到数据库中查询,再回调回结果。这样我们就可以构成之前所说的查询功能,而我们所谓的post功能似乎也可以用同样的方法加进去。</p>
  2664. <h3 id="查询数据"><span class="header-section-number">17.4.3</span> 查询数据</h3>
  2665. <p>简单地记录一下在IoT-CoAP中一次获取数据地过程。</p>
  2666. <p>先看看在示例中的Get.js的代码,这关乎在后面server端的代码。</p>
  2667. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;coap&#39;</span>)
  2668. <span class="op">,</span>requestURI <span class="op">=</span> <span class="st">&#39;coap://localhost/&#39;</span>
  2669. <span class="op">,</span>url <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;url&#39;</span>).<span class="at">parse</span>(requestURI <span class="op">+</span> <span class="st">&#39;id/1/&#39;</span>)
  2670. <span class="op">,</span>req <span class="op">=</span> <span class="va">coap</span>.<span class="at">request</span>(url)
  2671. <span class="op">,</span>bl <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;bl&#39;</span>)<span class="op">;</span>
  2672. <span class="va">req</span>.<span class="at">setHeader</span>(<span class="st">&quot;Accept&quot;</span><span class="op">,</span> <span class="st">&quot;application/json&quot;</span>)<span class="op">;</span>
  2673. <span class="va">req</span>.<span class="at">on</span>(<span class="st">&#39;response&#39;</span><span class="op">,</span> <span class="kw">function</span>(res) <span class="op">{</span>
  2674. <span class="va">res</span>.<span class="at">pipe</span>(<span class="at">bl</span>(<span class="kw">function</span>(err<span class="op">,</span> data) <span class="op">{</span>
  2675. <span class="kw">var</span> json <span class="op">=</span> <span class="va">JSON</span>.<span class="at">parse</span>(data)<span class="op">;</span>
  2676. <span class="va">console</span>.<span class="at">log</span>(json)<span class="op">;</span>
  2677. <span class="op">}</span>))<span class="op">;</span>
  2678. <span class="op">}</span>)<span class="op">;</span>
  2679. <span class="va">req</span>.<span class="at">end</span>()<span class="op">;</span></code></pre></div>
  2680. <p>const定义数据的方法,和我们在其他语言中有点像。只是这的const主要是为了程序的健壮型,减少程序出错,当然这不是javascript的用法。</p>
  2681. <p>我们构建了一个请求的URL</p>
  2682. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span>://localhost/id/1/</code></pre></div>
  2683. <p>我们对我们的请求添加了一个Header,内容是Accept,值是’application/json’也就是JSON格式。接着,便是等待请求回来,再处理返回的内容。</p>
  2684. <p><strong>判断请求的方法</strong></p>
  2685. <p>在这里先把一些无关的代码删除掉,并保证其能工作,so,下面就是简要的逻辑代码。</p>
  2686. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> coap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;coap&#39;</span>)<span class="op">;</span>
  2687. <span class="kw">var</span> server <span class="op">=</span> <span class="va">coap</span>.<span class="at">createServer</span>(<span class="op">{}</span>)<span class="op">;</span>
  2688. <span class="kw">var</span> request_handler <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;./request_handler.js&#39;</span>)<span class="op">;</span>
  2689. <span class="va">server</span>.<span class="at">on</span>(<span class="st">&#39;request&#39;</span><span class="op">,</span> <span class="kw">function</span>(req<span class="op">,</span> res) <span class="op">{</span>
  2690. <span class="cf">switch</span>(<span class="va">req</span>.<span class="at">method</span>)<span class="op">{</span>
  2691. <span class="cf">case</span> <span class="st">&quot;GET&quot;</span><span class="op">:</span> <span class="va">request_handler</span>.<span class="at">getHandler</span>(req<span class="op">,</span> res)<span class="op">;</span>
  2692. <span class="cf">break</span><span class="op">;</span>
  2693. <span class="op">}</span>
  2694. <span class="op">}</span>)<span class="op">;</span>
  2695. <span class="va">server</span>.<span class="at">listen</span>(<span class="kw">function</span>() <span class="op">{</span>
  2696. <span class="va">console</span>.<span class="at">log</span>(<span class="st">&#39;server started&#39;</span>)<span class="op">;</span>
  2697. <span class="op">}</span>)<span class="op">;</span></code></pre></div>
  2698. <p>创建一个CoAP服务,判断req.method,也就是请求的方法,如果是GET的话,就调用request_handler.getHandler(req, res)。而在getHandler里,判断了下请求的Accept</p>
  2699. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">request_helper</span>.<span class="at">getHandler</span> <span class="op">=</span> <span class="kw">function</span>(req<span class="op">,</span> res) <span class="op">{</span>
  2700. <span class="cf">switch</span> (<span class="va">req</span>.<span class="at">headers</span>[<span class="st">&#39;Accept&#39;</span>]) <span class="op">{</span>
  2701. <span class="cf">case</span> <span class="st">&quot;application/json&quot;</span><span class="op">:</span>
  2702. <span class="va">qh</span>.<span class="at">returnJSON</span>(req<span class="op">,</span> res)<span class="op">;</span>
  2703. <span class="cf">break</span><span class="op">;</span>
  2704. <span class="cf">case</span> <span class="st">&quot;application/xml&quot;</span><span class="op">:</span>
  2705. <span class="va">qh</span>.<span class="at">returnXML</span>(req<span class="op">,</span> res)<span class="op">;</span>
  2706. <span class="cf">break</span><span class="op">;</span>
  2707. <span class="op">}</span>
  2708. <span class="op">};</span></code></pre></div>
  2709. <p>如果是json刚调用returnJSON,</p>
  2710. <p><strong>Database与回调</strong></p>
  2711. <p>而这里为了处理回调函数刚分为了两部分</p>
  2712. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">query_helper</span>.<span class="at">returnJSON</span> <span class="op">=</span> <span class="kw">function</span>(req<span class="op">,</span> res) <span class="op">{</span>
  2713. <span class="va">DBHelper</span>.<span class="at">urlQueryData</span>(<span class="va">req</span>.<span class="at">url</span><span class="op">,</span> <span class="kw">function</span> (result) <span class="op">{</span>
  2714. <span class="va">QueryData</span>.<span class="at">returnJSON</span>(result<span class="op">,</span> res)<span class="op">;</span>
  2715. <span class="op">}</span>)<span class="op">;</span>
  2716. <span class="op">};</span></code></pre></div>
  2717. <p>而这里只是调用了</p>
  2718. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">DBHelper</span>.<span class="at">urlQueryData</span> <span class="op">=</span> <span class="kw">function</span> (url<span class="op">,</span> callback) <span class="op">{</span>
  2719. <span class="kw">var</span> db <span class="op">=</span> <span class="kw">new</span> <span class="va">sqlite3</span>.<span class="at">Database</span>(config[<span class="st">&quot;db_name&quot;</span>])<span class="op">;</span>
  2720. <span class="va">console</span>.<span class="at">log</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] <span class="op">+</span> <span class="st">&quot;=&quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>])<span class="op">;</span>
  2721. <span class="va">db</span>.<span class="at">all</span>(<span class="st">&quot;SELECT * FROM basic where &quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">1</span>] <span class="op">+</span> <span class="st">&quot;=&quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">split</span>(<span class="st">&#39;/&#39;</span>)[<span class="dv">2</span>]<span class="op">,</span> <span class="kw">function</span>(err<span class="op">,</span> rows) <span class="op">{</span>
  2722. <span class="va">db</span>.<span class="at">close</span>()<span class="op">;</span>
  2723. <span class="at">callback</span>(<span class="va">JSON</span>.<span class="at">stringify</span>(rows))<span class="op">;</span>
  2724. <span class="op">}</span>)<span class="op">;</span>
  2725. <span class="op">};</span></code></pre></div>
  2726. <p>这里调用了node sqlite3去查询对应id的数据,用回调处理了数据无法到外部的问题,而上面的returnJSON则只是返回最后的结果,code以及其他的内容。</p>
  2727. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">QueryData</span>.<span class="at">returnJSON</span> <span class="op">=</span> <span class="kw">function</span>(result<span class="op">,</span> res) <span class="op">{</span>
  2728. <span class="cf">if</span> (<span class="va">result</span>.<span class="at">length</span> <span class="op">==</span> <span class="dv">2</span>) <span class="op">{</span>
  2729. <span class="va">res</span>.<span class="at">code</span> <span class="op">=</span> <span class="st">&#39;4.04&#39;</span><span class="op">;</span>
  2730. <span class="va">res</span>.<span class="at">end</span>(<span class="va">JSON</span>.<span class="at">stringify</span>(<span class="op">{</span>
  2731. <span class="dt">error</span><span class="op">:</span> <span class="st">&quot;Not Found&quot;</span>
  2732. <span class="op">}</span>))<span class="op">;</span>
  2733. <span class="op">}</span> <span class="cf">else</span> <span class="op">{</span>
  2734. <span class="va">res</span>.<span class="at">code</span> <span class="op">=</span> <span class="st">&#39;2.05&#39;</span><span class="op">;</span>
  2735. <span class="va">res</span>.<span class="at">end</span>(result)<span class="op">;</span>
  2736. <span class="op">}</span>
  2737. <span class="op">};</span></code></pre></div>
  2738. <p>当resulst的结果为空时,返回一个404,因为没有数据。这样我们就构成了整个的链,再一步步返回结果。</p>
  2739. <p>在<a href="https://github.com/phodal/iot-coap">IoT-CoAP</a>中我们使用到了一个Block2的东西,于是便整理相关的一些资料,作一个简单的介绍,以及在代码中的使用。</p>
  2740. <h2 id="coap-block"><span class="header-section-number">17.5</span> CoAP Block</h2>
  2741. <p>CoAP是一个RESTful传输协议用于受限设备的节点和网络。基本的CoAP消息是一个不错的选择对于小型载荷如</p>
  2742. <ul>
  2743. <li>温度传感器</li>
  2744. <li>灯光开关</li>
  2745. <li>楼宇自动化设备</li>
  2746. </ul>
  2747. <p>然而,有时我们的应用需要传输更大的有效载荷,如——更新固件。与HTTP,TCP做繁重工作将大型有效载荷分成多个数据包,并确保他们所有到达并以正确的顺序被处理。</p>
  2748. <p>CoAP是同UDP与DLTS一样是基于数据报传输的,这限制了资源表示(resource representation)的最大大小,使得传输不需要太多的分割。虽然UDP支持通过IP分片传输更大的有效载荷,且仅限于64KiB,更重要的是,并没有真正很好地约束应用和网络。</p>
  2749. <p>而不是依赖于IP分片,这种规范基本COAP了对“块”选项,用于传输信息从多个资源区块的请求 - 响应对。在许多重要的情况下,阻止使服务器能够真正无状态:服务器可以处理每块分开传输,而无需建立连接以前的数据块传输的其他服务器端内存。</p>
  2750. <p>综上所述,块(Block)选项提供了传送一个最小的在分块的方式更大的陈述。</p>
  2751. <h3 id="coap-post"><span class="header-section-number">17.5.1</span> CoAP POST</h3>
  2752. <p>看看在IoT CoAP中的post示例。</p>
  2753. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;coap&#39;</span>)
  2754. <span class="op">,</span>request <span class="op">=</span> <span class="va">coap</span>.<span class="at">request</span>
  2755. <span class="op">,</span>bl <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;bl&#39;</span>)
  2756. <span class="op">,</span>req <span class="op">=</span> <span class="at">request</span>(<span class="op">{</span><span class="dt">hostname</span><span class="op">:</span> <span class="st">&#39;localhost&#39;</span><span class="op">,</span><span class="dt">port</span><span class="op">:</span><span class="dv">5683</span><span class="op">,</span><span class="dt">pathname</span><span class="op">:</span> <span class="st">&#39;&#39;</span><span class="op">,</span><span class="dt">method</span><span class="op">:</span> <span class="st">&#39;POST&#39;</span><span class="op">}</span>)<span class="op">;</span>
  2757. <span class="va">req</span>.<span class="at">setOption</span>(<span class="st">&#39;Block2&#39;</span><span class="op">,</span> [<span class="kw">new</span> <span class="at">Buffer</span>(<span class="st">&#39;1&#39;</span>)<span class="op">,</span><span class="kw">new</span> <span class="at">Buffer</span>(<span class="st">&quot;&#39;must&#39;&quot;</span>)<span class="op">,</span> <span class="kw">new</span> <span class="at">Buffer</span>(<span class="st">&#39;23&#39;</span>)<span class="op">,</span> <span class="kw">new</span> <span class="at">Buffer</span>(<span class="st">&#39;12&#39;</span>)])<span class="op">;</span>
  2758. <span class="va">req</span>.<span class="at">setHeader</span>(<span class="st">&quot;Accept&quot;</span><span class="op">,</span> <span class="st">&quot;application/json&quot;</span>)<span class="op">;</span>
  2759. <span class="va">req</span>.<span class="at">on</span>(<span class="st">&#39;response&#39;</span><span class="op">,</span> <span class="kw">function</span>(res) <span class="op">{</span>
  2760. <span class="va">res</span>.<span class="at">pipe</span>(<span class="at">bl</span>(<span class="kw">function</span>(err<span class="op">,</span> data) <span class="op">{</span>
  2761. <span class="va">console</span>.<span class="at">log</span>(data)<span class="op">;</span>
  2762. <span class="va">process</span>.<span class="at">exit</span>(<span class="dv">0</span>)<span class="op">;</span>
  2763. <span class="op">}</span>))<span class="op">;</span>
  2764. <span class="op">}</span>)<span class="op">;</span>
  2765. <span class="va">req</span>.<span class="at">end</span>()<span class="op">;</span></code></pre></div>
  2766. <p>Block2中一共有四个数据,相应的数据结果应该是</p>
  2767. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="op">{</span> <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;Block2&#39;</span><span class="op">,</span> <span class="dt">value</span><span class="op">:</span> <span class="op">&lt;</span>Buffer <span class="dv">31</span><span class="op">&gt;</span> <span class="op">}</span>
  2768. <span class="op">{</span> <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;Block2&#39;</span><span class="op">,</span> <span class="dt">value</span><span class="op">:</span> <span class="op">&lt;</span>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><span class="op">&gt;</span> <span class="op">}</span>
  2769. <span class="op">{</span> <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;Block2&#39;</span><span class="op">,</span> <span class="dt">value</span><span class="op">:</span> <span class="op">&lt;</span>Buffer <span class="dv">32</span> <span class="dv">33</span><span class="op">&gt;</span> <span class="op">}</span>
  2770. <span class="op">{</span> <span class="dt">name</span><span class="op">:</span> <span class="st">&#39;Block2&#39;</span><span class="op">,</span> <span class="dt">value</span><span class="op">:</span> <span class="op">&lt;</span>Buffer <span class="dv">31</span> <span class="dv">32</span><span class="op">&gt;</span> <span class="op">}</span></code></pre></div>
  2771. <p>这是没有解析的Block2,简单地可以用</p>
  2772. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">_</span>.<span class="at">values</span>(e).<span class="at">toString</span>()</code></pre></div>
  2773. <p>将结果转换为</p>
  2774. <p>Block2,1 Block2,‘must’ Block2,23 Block2,12</p>
  2775. <p>接着按“,”分开,</p>
  2776. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">_</span>.<span class="at">values</span>(e).<span class="at">toString</span>().<span class="at">split</span>(<span class="st">&#39;,&#39;</span>)[<span class="dv">1</span>]</code></pre></div>
  2777. <p>就有</p>
  2778. <div class="sourceCode"><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></div>
  2779. <p>便可以很愉快地将其post到数据库中了,</p>
  2780. <p>在做IoT-CoAP的过程中只支持JSON,查阅CoAP的草稿时发现支持了诸多的Content Types。</p>
  2781. <h3 id="coap-content-types"><span class="header-section-number">17.5.2</span> CoAP Content Types</h3>
  2782. <p>以下文字来自谷歌翻译:</p>
  2783. <blockquote>
  2784. <p>互联网媒体类型是通过HTTP字符串标识,如“application/xml”。该字符串是由一个顶层的类型“applicaion”和子类型的“XML”。为了尽量减少使用这些类型的媒体类型来表示的开销消息有效载荷,COAP定义一个标识符编码方案互联网媒体类型的子集。预计这桌将可扩展标识符的值的IANA维护。内容类型选项被格式化为一个8位无符号整数。初始映射到一个合适的互联网媒体类型标识符表所示。复合型高层次类型(multipart和不支持消息)。标识符值是从201-255保留的特定于供应商的,应用程序特定的或实验使用和不由IANA。</p>
  2785. </blockquote>
  2786. <p>下面是HTTP的标识符及类型</p>
  2787. <table>
  2788. <thead>
  2789. <tr class="header">
  2790. <th>Internet media type</th>
  2791. <th>Identifier</th>
  2792. </tr>
  2793. </thead>
  2794. <tbody>
  2795. <tr class="odd">
  2796. <td>text/plain (UTF-8)</td>
  2797. <td>0</td>
  2798. </tr>
  2799. <tr class="even">
  2800. <td>text/xml (UTF-8)</td>
  2801. <td>1</td>
  2802. </tr>
  2803. <tr class="odd">
  2804. <td>text/csv (UTF-8)</td>
  2805. <td>2</td>
  2806. </tr>
  2807. <tr class="even">
  2808. <td>text/html (UTF-8)</td>
  2809. <td>3</td>
  2810. </tr>
  2811. <tr class="odd">
  2812. <td>image/gif</td>
  2813. <td>21</td>
  2814. </tr>
  2815. <tr class="even">
  2816. <td>image/jpeg</td>
  2817. <td>22</td>
  2818. </tr>
  2819. <tr class="odd">
  2820. <td>image/png</td>
  2821. <td>23</td>
  2822. </tr>
  2823. <tr class="even">
  2824. <td>image/tiff</td>
  2825. <td>24</td>
  2826. </tr>
  2827. <tr class="odd">
  2828. <td>audio/raw</td>
  2829. <td>25</td>
  2830. </tr>
  2831. <tr class="even">
  2832. <td>video/raw</td>
  2833. <td>26</td>
  2834. </tr>
  2835. <tr class="odd">
  2836. <td>application/link-format [I-D.ietf-core-link-format]</td>
  2837. <td>40</td>
  2838. </tr>
  2839. <tr class="even">
  2840. <td>application/xml</td>
  2841. <td>41</td>
  2842. </tr>
  2843. <tr class="odd">
  2844. <td>application/octet-stream</td>
  2845. <td>42</td>
  2846. </tr>
  2847. <tr class="even">
  2848. <td>application/rdf+xml</td>
  2849. <td>43</td>
  2850. </tr>
  2851. <tr class="odd">
  2852. <td>application/soap+xml</td>
  2853. <td>44</td>
  2854. </tr>
  2855. <tr class="even">
  2856. <td>application/atom+xml</td>
  2857. <td>45</td>
  2858. </tr>
  2859. <tr class="odd">
  2860. <td>application/xmpp+xml</td>
  2861. <td>46</td>
  2862. </tr>
  2863. <tr class="even">
  2864. <td>application/exi</td>
  2865. <td>47</td>
  2866. </tr>
  2867. <tr class="odd">
  2868. <td>application/x-bxml</td>
  2869. <td>48</td>
  2870. </tr>
  2871. <tr class="even">
  2872. <td>application/fastinfoset</td>
  2873. <td>49</td>
  2874. </tr>
  2875. <tr class="odd">
  2876. <td>application/soap+fastinfoset</td>
  2877. <td>50</td>
  2878. </tr>
  2879. <tr class="even">
  2880. <td>application/json</td>
  2881. <td>51</td>
  2882. </tr>
  2883. </tbody>
  2884. </table>
  2885. <p>而在CoAP中只有简单地几个</p>
  2886. <table>
  2887. <thead>
  2888. <tr class="header">
  2889. <th>Media type</th>
  2890. <th>Encoding</th>
  2891. <th>Id.</th>
  2892. <th>Reference</th>
  2893. </tr>
  2894. </thead>
  2895. <tbody>
  2896. <tr class="odd">
  2897. <td>text/plain;</td>
  2898. <td>-</td>
  2899. <td>0</td>
  2900. <td>[RFC2046][RFC3676][RFC5147]</td>
  2901. </tr>
  2902. <tr class="even">
  2903. <td>charset=utf-8</td>
  2904. <td></td>
  2905. <td></td>
  2906. <td></td>
  2907. </tr>
  2908. <tr class="odd">
  2909. <td>application/</td>
  2910. <td>-</td>
  2911. <td>40</td>
  2912. <td>[RFC6690]</td>
  2913. </tr>
  2914. <tr class="even">
  2915. <td>link-format</td>
  2916. <td></td>
  2917. <td></td>
  2918. <td></td>
  2919. </tr>
  2920. <tr class="odd">
  2921. <td>application/xml</td>
  2922. <td>-</td>
  2923. <td>41</td>
  2924. <td>[RFC3023]</td>
  2925. </tr>
  2926. <tr class="even">
  2927. <td>application/</td>
  2928. <td>-</td>
  2929. <td>42</td>
  2930. <td>[RFC2045][RFC2046]</td>
  2931. </tr>
  2932. <tr class="odd">
  2933. <td>octet-stream</td>
  2934. <td></td>
  2935. <td></td>
  2936. <td></td>
  2937. </tr>
  2938. <tr class="even">
  2939. <td>application/exi</td>
  2940. <td>-</td>
  2941. <td>47</td>
  2942. <td>[EXIMIME]</td>
  2943. </tr>
  2944. <tr class="odd">
  2945. <td>application/json</td>
  2946. <td>-</td>
  2947. <td>50</td>
  2948. <td>[RFC4627]</td>
  2949. </tr>
  2950. </tbody>
  2951. </table>
  2952. <p>简单地说就是:</p>
  2953. <p><code>诸如application/json的Content Types在CoAP中应该是50</code>。如上表所示的结果是其对应的结果,这样的话可以减少传递的信息量。</p>
  2954. <h2 id="coap-json"><span class="header-section-number">17.6</span> CoAP JSON</h2>
  2955. <p>于是在一开始的时候首先支持的便是“application/json”这样的类型。</p>
  2956. <p>首先判断请求的header</p>
  2957. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">request_helper</span>.<span class="at">getHandler</span> <span class="op">=</span> <span class="kw">function</span>(req<span class="op">,</span> res) <span class="op">{</span>
  2958. <span class="cf">switch</span> (<span class="va">req</span>.<span class="at">headers</span>[<span class="st">&#39;Accept&#39;</span>]) <span class="op">{</span>
  2959. <span class="cf">case</span> <span class="st">&quot;application/json&quot;</span><span class="op">:</span>
  2960. <span class="va">qh</span>.<span class="at">returnJSON</span>(req<span class="op">,</span> res)<span class="op">;</span>
  2961. <span class="cf">break</span><span class="op">;</span>
  2962. <span class="cf">case</span> <span class="st">&quot;application/xml&quot;</span><span class="op">:</span>
  2963. <span class="va">qh</span>.<span class="at">returnXML</span>(req<span class="op">,</span> res)<span class="op">;</span>
  2964. <span class="cf">break</span><span class="op">;</span>
  2965. <span class="op">}</span>
  2966. <span class="op">};</span></code></pre></div>
  2967. <p>再转至相应的函数处理,而判断的依据则是Accept是不是“application/json”。</p>
  2968. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="at">registerFormat</span>(<span class="st">&#39;text/plain&#39;</span><span class="op">,</span> <span class="dv">0</span>)
  2969. <span class="at">registerFormat</span>(<span class="st">&#39;application/link-format&#39;</span><span class="op">,</span> <span class="dv">40</span>)
  2970. <span class="at">registerFormat</span>(<span class="st">&#39;application/xml&#39;</span><span class="op">,</span> <span class="dv">41</span>)
  2971. <span class="at">registerFormat</span>(<span class="st">&#39;application/octet-stream&#39;</span><span class="op">,</span> <span class="dv">42</span>)
  2972. <span class="at">registerFormat</span>(<span class="st">&#39;application/exi&#39;</span><span class="op">,</span> <span class="dv">47</span>)
  2973. <span class="at">registerFormat</span>(<span class="st">&#39;application/json&#39;</span><span class="op">,</span> <span class="dv">50</span>)</code></pre></div>
  2974. <p>对应地我们需要在一发出请求的时候设置好Accept,要不就没有办法返回我们需要的结果。</p>
  2975. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">req</span>.<span class="at">setHeader</span>(<span class="st">&quot;Accept&quot;</span><span class="op">,</span> <span class="st">&quot;application/json&quot;</span>)<span class="op">;</span></code></pre></div>
  2976. <p><strong>返回JSON</strong></p>
  2977. <p>在给IoT CoAP添加了JSON支持之后,变得非常有意思,至少我们可以获得我们想要的结果。在上一篇中我们介绍了一些常用的工具——<a href="http://www.phodal.com/blog/coap-command-line-tools-set/">CoAP 命令行工具集</a>。</p>
  2978. <p><strong>CoAP客户端代码</strong></p>
  2979. <p>开始之前我们需要有一个客户端代码,以便我们的服务端可以返回正确的数据并解析</p>
  2980. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> coap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;coap&#39;</span>)<span class="op">;</span>
  2981. <span class="kw">var</span> requestURI <span class="op">=</span> <span class="st">&#39;coap://localhost/&#39;</span><span class="op">;</span>
  2982. <span class="kw">var</span> url <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;url&#39;</span>).<span class="at">parse</span>(requestURI <span class="op">+</span> <span class="st">&#39;id/1/&#39;</span>)<span class="op">;</span>
  2983. <span class="va">console</span>.<span class="at">log</span>(<span class="st">&quot;Request URL: &quot;</span> <span class="op">+</span> <span class="va">url</span>.<span class="at">href</span>)<span class="op">;</span>
  2984. <span class="kw">var</span> req <span class="op">=</span> <span class="va">coap</span>.<span class="at">request</span>(url)<span class="op">;</span>
  2985. <span class="kw">var</span> bl <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;bl&#39;</span>)<span class="op">;</span>
  2986. <span class="va">req</span>.<span class="at">setHeader</span>(<span class="st">&quot;Accept&quot;</span><span class="op">,</span> <span class="st">&quot;application/json&quot;</span>)<span class="op">;</span>
  2987. <span class="va">req</span>.<span class="at">on</span>(<span class="st">&#39;response&#39;</span><span class="op">,</span> <span class="kw">function</span>(res) <span class="op">{</span>
  2988. <span class="va">res</span>.<span class="at">pipe</span>(<span class="at">bl</span>(<span class="kw">function</span>(err<span class="op">,</span> data) <span class="op">{</span>
  2989. <span class="kw">var</span> json <span class="op">=</span> <span class="va">JSON</span>.<span class="at">parse</span>(data)<span class="op">;</span>
  2990. <span class="va">console</span>.<span class="at">log</span>(json)<span class="op">;</span>
  2991. <span class="op">}</span>))<span class="op">;</span>
  2992. <span class="op">}</span>)<span class="op">;</span>
  2993. <span class="va">req</span>.<span class="at">end</span>()<span class="op">;</span></code></pre></div>
  2994. <p>代码有点长内容也有点多,但是核心是这句话:</p>
  2995. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">req</span>.<span class="at">setHeader</span>(<span class="st">&quot;Accept&quot;</span><span class="op">,</span> <span class="st">&quot;application/json&quot;</span>)<span class="op">;</span></code></pre></div>
  2996. <p>这样的话,我们只需要在我们的服务端一判断,</p>
  2997. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="cf">if</span>(<span class="va">req</span>.<span class="at">headers</span>[<span class="st">&#39;Accept&#39;</span>] <span class="op">==</span> <span class="st">&#39;application/json&#39;</span>) <span class="op">{</span>
  2998. <span class="co">//do something</span>
  2999. <span class="op">};</span></code></pre></div>
  3000. <p>这样就可以返回数据了</p>
  3001. <p><strong>CoAP Server端代码</strong></p>
  3002. <p>Server端的代码比较简单,判断一下</p>
  3003. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="cf">if</span> (<span class="va">req</span>.<span class="at">headers</span>[<span class="st">&#39;Accept&#39;</span>] <span class="op">==</span> <span class="st">&#39;application/json&#39;</span>) <span class="op">{</span>
  3004. <span class="at">parse_url</span>(<span class="va">req</span>.<span class="at">url</span><span class="op">,</span> <span class="kw">function</span>(result)<span class="op">{</span>
  3005. <span class="va">res</span>.<span class="at">end</span>(result)<span class="op">;</span>
  3006. <span class="op">}</span>)<span class="op">;</span>
  3007. <span class="va">res</span>.<span class="at">code</span> <span class="op">=</span> <span class="st">&#39;2.05&#39;</span><span class="op">;</span>
  3008. <span class="op">}</span></code></pre></div>
  3009. <p>请求的是否是JSON格式,再返回一个205,也就是Content,只是这时设计是请求一个URL返回对应的数据。如</p>
  3010. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span>://localhost/id/1/</code></pre></div>
  3011. <p>这时应该请求的是ID为1的数据,即</p>
  3012. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript">[ <span class="op">{</span> <span class="dt">id</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span> <span class="dt">value</span><span class="op">:</span> <span class="st">&#39;is id 1&#39;</span><span class="op">,</span> <span class="dt">sensors1</span><span class="op">:</span> <span class="dv">19</span><span class="op">,</span> <span class="dt">sensors2</span><span class="op">:</span> <span class="dv">20</span> <span class="op">}</span>]</code></pre></div>
  3013. <p>而parse_url只是从数据库从读取相应的数据。</p>
  3014. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="at">parse_url</span>(url <span class="op">,</span>callback) <span class="op">{</span>
  3015. <span class="kw">var</span> db <span class="op">=</span> <span class="kw">new</span> <span class="va">sqlite3</span>.<span class="at">Database</span>(config[<span class="st">&quot;db_name&quot;</span>])<span class="op">;</span>
  3016. <span class="kw">var</span> result <span class="op">=</span> []<span class="op">;</span>
  3017. <span class="va">db</span>.<span class="at">all</span>(<span class="st">&quot;SELECT * FROM basic;&quot;</span><span class="op">,</span> <span class="kw">function</span>(err<span class="op">,</span> rows) <span class="op">{</span>
  3018. <span class="at">callback</span>(<span class="va">JSON</span>.<span class="at">stringify</span>(rows))<span class="op">;</span>
  3019. <span class="op">}</span>)
  3020. <span class="op">}</span></code></pre></div>
  3021. <p>并且全部都显示出来,设计得真是有点不行,不过现在已经差不多了。</p>
  3022. <h2 id="使用iot-coap构建物联网"><span class="header-section-number">17.7</span> 使用IoT-CoAP构建物联网</h2>
  3023. <p>(<code>注意</code>:windows系统npm install失败时,需要自己建立一个C:and Settings[USERNAME]Data文件)</p>
  3024. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install iot-coap</code></pre></div>
  3025. <p>1.新建<strong>index.js</strong></p>
  3026. <p><code>注意</code>: 如果已经存在一个index.js文件,请将下面内容添加到文件末尾(create index.js, and add)</p>
  3027. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> iotcoap <span class="op">=</span> <span class="at">require</span>(<span class="st">&#39;iot-coap&#39;</span>)<span class="op">;</span>
  3028. <span class="va">iotcoap</span>.<span class="at">run</span>()<span class="op">;</span>
  3029. <span class="va">iotcoap</span>.<span class="va">rest</span>.<span class="at">run</span>()<span class="op">;</span></code></pre></div>
  3030. <p><code>注意</code>:在db配置可以选择mongodb和sqlite3,替换所需要的数据库即可。(you can choice db on iot.js with ‘sqlite’ or ‘mongodb’)</p>
  3031. <p>2.创建<strong>iot.js</strong></p>
  3032. <div class="sourceCode"><pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="va">exports</span>.<span class="at">config</span> <span class="op">=</span> <span class="op">{</span>
  3033. <span class="st">&quot;db_name&quot;</span><span class="op">:</span> <span class="st">&quot;iot.db&quot;</span><span class="op">,</span>
  3034. <span class="st">&quot;mongodb_name&quot;</span><span class="op">:</span> <span class="st">&quot;iot&quot;</span><span class="op">,</span>
  3035. <span class="st">&quot;mongodb_documents&quot;</span><span class="op">:</span> <span class="st">&quot;iot&quot;</span><span class="op">,</span>
  3036. <span class="st">&quot;db&quot;</span><span class="op">:</span> <span class="st">&quot;mongodb&quot;</span><span class="op">,</span>
  3037. <span class="st">&quot;table_name&quot;</span><span class="op">:</span> <span class="st">&quot;basic&quot;</span><span class="op">,</span>
  3038. <span class="st">&quot;keys&quot;</span><span class="op">:</span>[
  3039. <span class="st">&quot;id&quot;</span><span class="op">,</span>
  3040. <span class="st">&quot;value&quot;</span><span class="op">,</span>
  3041. <span class="st">&quot;sensors1&quot;</span><span class="op">,</span>
  3042. <span class="st">&quot;sensors2&quot;</span>
  3043. ]<span class="op">,</span>
  3044. <span class="st">&quot;db_table&quot;</span><span class="op">:</span> <span class="st">&quot;id integer primary key, value text, sensors1 float, sensors2 float&quot;</span><span class="op">,</span>
  3045. <span class="st">&quot;mongodb_init&quot;</span><span class="op">:</span>[
  3046. <span class="op">{</span>
  3047. <span class="dt">id</span><span class="op">:</span> <span class="dv">1</span><span class="op">,</span>
  3048. <span class="dt">value</span><span class="op">:</span> <span class="st">&quot;is id 1&quot;</span><span class="op">,</span>
  3049. <span class="dt">sensors1</span><span class="op">:</span> <span class="dv">19</span><span class="op">,</span>
  3050. <span class="dt">sensors2</span><span class="op">:</span> <span class="dv">20</span>
  3051. <span class="op">},</span>
  3052. <span class="op">{</span>
  3053. <span class="dt">id</span><span class="op">:</span> <span class="dv">2</span><span class="op">,</span>
  3054. <span class="dt">value</span><span class="op">:</span> <span class="st">&quot;is id 2&quot;</span><span class="op">,</span>
  3055. <span class="dt">sensors1</span><span class="op">:</span> <span class="dv">20</span><span class="op">,</span>
  3056. <span class="dt">sensors2</span><span class="op">:</span> <span class="dv">21</span>
  3057. <span class="op">}</span>
  3058. ]<span class="op">,</span>
  3059. <span class="st">&quot;init_table&quot;</span><span class="op">:</span>[
  3060. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, &#39;is id 1&#39;, 19, 20);&quot;</span><span class="op">,</span>
  3061. <span class="st">&quot;insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, &#39;is id 2&#39;, 20, 21);&quot;</span>
  3062. ]<span class="op">,</span>
  3063. <span class="st">&quot;query_table&quot;</span><span class="op">:</span><span class="st">&quot;select * from basic;&quot;</span><span class="op">,</span>
  3064. <span class="st">&quot;rest_url&quot;</span><span class="op">:</span> <span class="st">&quot;/id/:id&quot;</span><span class="op">,</span>
  3065. <span class="st">&quot;rest_post_url&quot;</span><span class="op">:</span> <span class="st">&quot;/&quot;</span><span class="op">,</span>
  3066. <span class="st">&quot;rest_port&quot;</span><span class="op">:</span> <span class="dv">8848</span>
  3067. <span class="op">};</span></code></pre></div>
  3068. <p>3.运行(run)</p>
  3069. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> index.js</code></pre></div>
  3070. <p>show:</p>
  3071. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> listening at coap://0.0.0.0:5683
  3072. <span class="kw">restify</span> listening at http://0.0.0.0:8848</code></pre></div>
  3073. <h1 id="简单物联网"><span class="header-section-number">18</span> 简单物联网</h1>
  3074. <p>到这时,我们算搭建了一个简单的REST服务了。接着我们可以简单的做一个最小的物联网系统,将我们的单片机、MCU等等连上网。</p>
  3075. <div class="figure">
  3076. <img src="./images/arch.jpeg" alt="硬件结构图" />
  3077. <p class="caption">硬件结构图</p>
  3078. </div>
  3079. <p>考虑到如果我们只是单一连接各个节点,那么系统的结构图,同下所示</p>
  3080. <div class="figure">
  3081. <img src="./images/fullconnected.png" alt="全连接" />
  3082. <p class="caption">全连接</p>
  3083. </div>
  3084. <p>下面的星形结构图类似于我们在接下来所要构建的系统</p>
  3085. <div class="figure">
  3086. <img src="./images/star.png" alt="星形结构图" />
  3087. <p class="caption">星形结构图</p>
  3088. </div>
  3089. <p>一个用于控制真实电器的硬件实物图</p>
  3090. <div class="figure">
  3091. <img src="./images/hardware.jpg" alt="简单实物图" />
  3092. <p class="caption">简单实物图</p>
  3093. </div>
  3094. <h2 id="硬件通信"><span class="header-section-number">18.1</span> 硬件通信</h2>
  3095. <h3 id="串口通信-1"><span class="header-section-number">18.1.1</span> 串口通信</h3>
  3096. <p>Arduino与Raspberry Pi通过串口通信的方式实现通信,相互传输所需要的数据,Raspberry Pi将资源传于互联网上对应的接口,接口可以在互联网上被访问。Laravel框架构架于服务器之上,将Raspbery Pi获取过来的数据存储于MySQL数据,再以REST服务的方式共享数据,互联网上的其他设备便可以通过网络来访问这些设备。Ajax用于将后台的数据以不需要刷新的方式传递到网站前台,通过HighCharts框架显示给终端用户。</p>
  3097. <h4 id="python"><span class="header-section-number">18.1.1.1</span> Python</h4>
  3098. <p>1.在Windows中的串口通常是<code>COM1</code>,<code>COM0</code>等等</p>
  3099. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">ser<span class="op">=</span>serial.Serial(<span class="st">&quot;COM0&quot;</span>,<span class="dv">9600</span>)</code></pre></div>
  3100. <p>2.Mac OS系统中位于/dev目录下,名字类似于<code>tty.usbmodem1451</code>。</p>
  3101. <div class="sourceCode"><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></div>
  3102. <p>3.在Linux内核的系统中虚拟串口用的节点是ttyACM,位于/dev目录下。</p>
  3103. <div class="sourceCode"><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></div>
  3104. <blockquote>
  3105. <p>串行接口是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件。一般完成这种功能 的电路,我们称为串行接口电路。</p>
  3106. </blockquote>
  3107. <p>便是打开这个设备,以9600的速率传输数据。</p>
  3108. <p>程序框架如下所示:</p>
  3109. <div class="figure">
  3110. <img src="./images/raspberrypi_flow.png" alt="Raspberry Pi" />
  3111. <p class="caption">Raspberry Pi</p>
  3112. </div>
  3113. <p>代码如下:</p>
  3114. <div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> json
  3115. <span class="im">import</span> urllib2
  3116. <span class="im">import</span> serial
  3117. <span class="im">import</span> time
  3118. url<span class="op">=</span><span class="st">&quot;http://www.xianuniversity.com/athome/1&quot;</span>
  3119. <span class="cf">while</span> <span class="dv">1</span>:
  3120. <span class="cf">try</span>:
  3121. date<span class="op">=</span>urllib2.urlopen(url)
  3122. result<span class="op">=</span>json.load(date)
  3123. status<span class="op">=</span>result[<span class="dv">0</span>][<span class="st">&quot;led1&quot;</span>]
  3124. ser<span class="op">=</span>serial.Serial(<span class="st">&quot;/dev/ttyACM0&quot;</span>,<span class="dv">9600</span>)
  3125. <span class="cf">if</span> status<span class="op">==</span><span class="dv">1</span> :
  3126. ser.write(<span class="st">&quot;1&quot;</span>)
  3127. <span class="cf">elif</span> status<span class="op">==</span><span class="dv">0</span>:
  3128. ser.write(<span class="st">&quot;0&quot;</span>)
  3129. time.sleep(<span class="dv">1</span>)
  3130. <span class="cf">except</span> urllib2.URLError:
  3131. <span class="bu">print</span> <span class="st">&quot;Bad URL or timeout&quot;</span></code></pre></div>
  3132. <div class="figure">
  3133. <img src="./images/getjson.png" alt="python返回json数据" />
  3134. <p class="caption">python返回json数据</p>
  3135. </div>
  3136. <p>系统还需要对上面的数据进行处理,只拿其中的结果</p>
  3137. <div class="figure">
  3138. <img src="./images/origin.png" alt="python处理完后的结果" />
  3139. <p class="caption">python处理完后的结果</p>
  3140. </div>
  3141. <p>当改变led的状态后,便可以得到下面的结果</p>
  3142. <div class="figure">
  3143. <img src="./images/change.png" alt="改变状态后的结果" />
  3144. <p class="caption">改变状态后的结果</p>
  3145. </div>
  3146. <h4 id="ruby"><span class="header-section-number">18.1.1.2</span> Ruby</h4>
  3147. <p>如果你用的是Ruby的话,可以尝试使用<code>serialport</code></p>
  3148. <p>安装</p>
  3149. <div class="sourceCode"><pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> gem install serialport</code></pre></div>
  3150. <p>代码大致如下</p>
  3151. <div class="sourceCode"><pre class="sourceCode ruby"><code class="sourceCode ruby">require <span class="st">&#39;serialport&#39;</span>
  3152. sp = <span class="dt">SerialPort</span>.new <span class="st">&quot;/dev/ACM0&quot;</span>, <span class="dv">9600</span>
  3153. sp.write <span class="st">&quot;1&quot;</span></code></pre></div>
  3154. <p>注意: 根据相关的系统修改相关的代码。</p>
  3155. <h2 id="硬件"><span class="header-section-number">18.2</span> 硬件</h2>
  3156. <h3 id="arduino"><span class="header-section-number">18.2.1</span> Arduino</h3>
  3157. <p>这样我们在我们的Arduino上所要做的便是,读取串口的结果并控制IO口。</p>
  3158. <div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> ledPort=<span class="dv">13</span>;
  3159. <span class="dt">void</span> setup() {
  3160. Serial.begin(<span class="dv">9600</span>);
  3161. pinMode(ledPort,OUTPUT);
  3162. }
  3163. <span class="dt">int</span> serialData;
  3164. <span class="dt">void</span> loop() {
  3165. String inString = <span class="st">&quot;&quot;</span>;
  3166. <span class="kw">while</span> (Serial.available()&gt; <span class="dv">0</span>)
  3167. {
  3168. <span class="dt">int</span> inChar = Serial.read();
  3169. <span class="kw">if</span> (isDigit(inChar)) {
  3170. inString += (<span class="dt">char</span>)inChar;
  3171. }
  3172. serialData=inString.toInt();
  3173. Serial.print(serialData);
  3174. }
  3175. <span class="kw">if</span>(serialData==<span class="dv">1</span>){
  3176. digitalWrite(ledPort,HIGH);
  3177. }<span class="kw">else</span>{
  3178. digitalWrite(ledPort,LOW);
  3179. }
  3180. }</code></pre></div>
  3181. <p>如果结果是1的话,就让13口为高电平,也就是让灯亮起来。</p>
  3182. <h3 id="继电器"><span class="header-section-number">18.2.2</span> 继电器</h3>
  3183. <blockquote>
  3184. <p>继电器(英文名称:relay)是一种电控制器件,是当输入量(激励量)的变化达到规定要求时,在电气输出电路中使被控量发生预定的阶跃变化的一种电器。它具有控制系统(又称输入回路)和被控制系统(又称输出回路)之间的互动关系。通常应用于自动化的控制电路中,它实际上是用小电流去控制大电流运作的一种“自动开关”。故在电路中起着自动调节、安全保护、转换电路等作用。</p>
  3185. </blockquote>
  3186. <p>在这里我们可以默认为我们想要为单片机的5V电压控制220V的电器。</p>
  3187. <p>最后我们便可以通过些来控制灯的开和关。</p>
  3188. <h1 id="android简单示例"><span class="header-section-number">19</span> Android简单示例</h1>
  3189. <p>由于在某些嵌入式系统中使用的是Android系统,这里给出一个简单的Android App的示例,具体代码可以从clone自<a href="https://github.com/phodal/iot-android" class="uri">https://github.com/phodal/iot-android</a></p>
  3190. <p>代码说明,经过测试的版本有</p>
  3191. <ul>
  3192. <li>Android 2.3</li>
  3193. <li>Android 4.0.4</li>
  3194. </ul>
  3195. <p>机型有</p>
  3196. <ul>
  3197. <li>HTC G1 (android 2.3)</li>
  3198. <li>Motor xt300 (android 2.3)</li>
  3199. <li>Sony ST25I (android 4.0.4)</li>
  3200. <li>MI2</li>
  3201. </ul>
  3202. <p>应该可以在大部分的手机上工作。</p>
  3203. <h2 id="调用web-services-get"><span class="header-section-number">19.1</span> 调用Web Services GET</h2>
  3204. <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>
  3205. <h3 id="创建restclient"><span class="header-section-number">19.1.1</span> 创建RESTClient</h3>
  3206. <p>在这里我们首先会定义四个REST方法GET、POST、PUT、DELETE</p>
  3207. <div class="sourceCode"><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 {
  3208. <span class="kw">switch</span> (method) {
  3209. <span class="kw">case</span> GET: {
  3210. <span class="co">// add parameters</span>
  3211. String combinedParams = <span class="st">&quot;&quot;</span>;
  3212. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  3213. combinedParams += <span class="st">&quot;?&quot;</span>;
  3214. <span class="kw">for</span> (NameValuePair p : params) {
  3215. String paramString = p.<span class="fu">getName</span>() + <span class="st">&quot;=&quot;</span>
  3216. + URLEncoder.<span class="fu">encode</span>(p.<span class="fu">getValue</span>(), HTTP.<span class="fu">UTF_8</span>);
  3217. <span class="kw">if</span> (combinedParams.<span class="fu">length</span>() &gt; <span class="dv">1</span>) {
  3218. combinedParams += <span class="st">&quot;&amp;&quot;</span> + paramString;
  3219. } <span class="kw">else</span> {
  3220. combinedParams += paramString;
  3221. }
  3222. }
  3223. }
  3224. HttpGet request = <span class="kw">new</span> <span class="fu">HttpGet</span>(url + combinedParams);
  3225. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3226. <span class="co">// add headers</span>
  3227. <span class="kw">for</span> (NameValuePair h : headers) {
  3228. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3229. }
  3230. <span class="fu">executeRequest</span>(request, url);
  3231. <span class="kw">break</span>;
  3232. }
  3233. <span class="kw">case</span> POST: {
  3234. HttpPost request = <span class="kw">new</span> <span class="fu">HttpPost</span>(url);
  3235. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3236. <span class="co">// add headers</span>
  3237. <span class="kw">for</span> (NameValuePair h : headers) {
  3238. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3239. }
  3240. <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">&quot;&quot;</span>)) {
  3241. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
  3242. }
  3243. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  3244. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
  3245. }
  3246. <span class="fu">executeRequest</span>(request, url);
  3247. <span class="kw">break</span>;
  3248. }
  3249. <span class="kw">case</span> PUT: {
  3250. HttpPut request = <span class="kw">new</span> <span class="fu">HttpPut</span>(url);
  3251. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3252. <span class="co">// add headers</span>
  3253. <span class="kw">for</span> (NameValuePair h : headers) {
  3254. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3255. }
  3256. <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">&quot;&quot;</span>)) {
  3257. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
  3258. }
  3259. <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
  3260. request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
  3261. }
  3262. <span class="fu">executeRequest</span>(request, url);
  3263. <span class="kw">break</span>;
  3264. }
  3265. <span class="kw">case</span> DELETE: {
  3266. HttpDelete request = <span class="kw">new</span> <span class="fu">HttpDelete</span>(url);
  3267. request.<span class="fu">addHeader</span>(<span class="st">&quot;Accept-Encoding&quot;</span>, <span class="st">&quot;gzip&quot;</span>);
  3268. <span class="co">// add headers</span>
  3269. <span class="kw">for</span> (NameValuePair h : headers) {
  3270. request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
  3271. }
  3272. <span class="fu">executeRequest</span>(request, url);
  3273. <span class="kw">break</span>;
  3274. }
  3275. }
  3276. }</code></pre></div>
  3277. <p>这四个方法最后都执行executeRequest来获取响应结果。</p>
  3278. <div class="sourceCode"><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) {
  3279. HttpParams httpParameters = <span class="kw">new</span> <span class="fu">BasicHttpParams</span>();
  3280. HttpConnectionParams.<span class="fu">setConnectionTimeout</span>(httpParameters,
  3281. timeoutConnection);
  3282. HttpConnectionParams.<span class="fu">setSoTimeout</span>(httpParameters, timeoutSocket);
  3283. HttpProtocolParams.<span class="fu">setUseExpectContinue</span>(httpParameters, <span class="kw">false</span>);
  3284. request.<span class="fu">setParams</span>(httpParameters);
  3285. <span class="fu">setOauth</span>(request);
  3286. DefaultHttpClient client = <span class="kw">new</span> <span class="fu">DefaultHttpClient</span>();
  3287. HttpResponse httpResponse;
  3288. <span class="kw">try</span> {
  3289. httpResponse = client.<span class="fu">execute</span>(request);
  3290. responseCode = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getStatusCode</span>();
  3291. message = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getReasonPhrase</span>();
  3292. HttpEntity entity = httpResponse.<span class="fu">getEntity</span>();
  3293. <span class="kw">if</span> (entity != <span class="kw">null</span>) {
  3294. InputStream instream = httpResponse.<span class="fu">getEntity</span>().<span class="fu">getContent</span>();
  3295. Header contentEncoding = httpResponse
  3296. .<span class="fu">getFirstHeader</span>(<span class="st">&quot;Content-Encoding&quot;</span>);
  3297. <span class="kw">if</span> (contentEncoding != <span class="kw">null</span>
  3298. &amp;&amp; contentEncoding.<span class="fu">getValue</span>().<span class="fu">equalsIgnoreCase</span>(<span class="st">&quot;gzip&quot;</span>)) {
  3299. instream = <span class="kw">new</span> GZIPInputStream(instream);
  3300. }
  3301. <span class="co">// instream = entity.getContent();</span>
  3302. response = <span class="fu">convertStreamToString</span>(instream);
  3303. <span class="co">// Closing the input stream will trigger connection release</span>
  3304. instream.<span class="fu">close</span>();
  3305. }
  3306. } <span class="kw">catch</span> (ClientProtocolException e) {
  3307. client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
  3308. e.<span class="fu">printStackTrace</span>();
  3309. } <span class="kw">catch</span> (IOException e) {
  3310. client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
  3311. e.<span class="fu">printStackTrace</span>();
  3312. }
  3313. }</code></pre></div>
  3314. <p>接着,我们便可以执行getResponse()函数来获取结果。</p>
  3315. <h2 id="使用rest-client获取结果"><span class="header-section-number">19.2</span> 使用REST Client获取结果</h2>
  3316. <p>使用RESTClient时,便可以用下面的示例</p>
  3317. <div class="sourceCode"><pre class="sourceCode java"><code class="sourceCode java">RestClient client = <span class="kw">new</span> <span class="fu">RestClient</span>(tUrl);
  3318. <span class="kw">try</span> {
  3319. client.<span class="fu">Execute</span>(RequestMethod.<span class="fu">GET</span>);
  3320. <span class="kw">if</span> (client.<span class="fu">getResponseCode</span>() != <span class="dv">200</span>) {
  3321. <span class="co">//do something</span>
  3322. }
  3323. <span class="co">//JSONArray jArray = new JSONArray(client.getResponse());</span>
  3324. } <span class="kw">catch</span> (Exception e) {
  3325. <span class="co">//do something</span>
  3326. }</code></pre></div>
  3327. <p>而这时,我们只需要对相应的数据进行处理就可以了,如</p>
  3328. <div class="sourceCode"><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>());
  3329. JSONObject jObj=jArray.<span class="fu">getJSONObject</span>(<span class="dv">0</span>);
  3330. vshow.<span class="fu">setText</span>(jObj.<span class="fu">toString</span>());
  3331. <span class="fu">outputJSON</span>(jObj);</code></pre></div>
  3332. <p>将他转换为String,接着在Android端上显示最后的结果。</p>
  3333. <h1 id="尾声"><span class="header-section-number">20</span> 尾声</h1>
  3334. <h2 id="路"><span class="header-section-number">20.1</span> 路</h2>
  3335. <h2 id="其他-2"><span class="header-section-number">20.2</span> 其他</h2>
  3336. <p>意见及建议: <a href="https://github.com/phodal/designiot/issues" class="uri">https://github.com/phodal/designiot/issues</a></p>
  3337. <p>邮箱: <a href="h@phodal.com" class="uri">h@phodal.com</a></p>
  3338. <div class="footnotes">
  3339. <hr />
  3340. <ol>
  3341. <li id="fn1"><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>
  3342. <li id="fn2"><p>形如http://www.phodal.com<a href="#fnref2">↩</a></p></li>
  3343. <li id="fn3"><p>URL 是 URI 的子集<a href="#fnref3">↩</a></p></li>
  3344. <li id="fn4"><p>curl是利用URL语法在命令行方式下工作的开源文件传输工具。<a href="#fnref4">↩</a></p></li>
  3345. </ol>
  3346. </div>
  3347. </body>
  3348. </html>