1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406 |
- <div id="TOC">
- <ul>
- <li><a href="#前言"><span class="toc-section-number">1</span> 前言</a>
- <ul>
- <li><a href="#目标读者"><span class="toc-section-number">1.1</span> 目标读者</a></li>
- <li><a href="#不适合人群"><span class="toc-section-number">1.2</span> 不适合人群</a></li>
- <li><a href="#介绍"><span class="toc-section-number">1.3</span> 介绍</a>
- <ul>
- <li><a href="#为什么没有c"><span class="toc-section-number">1.3.1</span> 为什么没有C ?</a></li>
- <li><a href="#为什么不是java"><span class="toc-section-number">1.3.2</span> 为什么不是JAVA ?</a></li>
- </ul></li>
- <li><a href="#如何阅读"><span class="toc-section-number">1.4</span> 如何阅读</a></li>
- </ul></li>
- <li><a href="#无处不在的html"><span class="toc-section-number">2</span> 无处不在的HTML</a>
- <ul>
- <li><a href="#html的helloworld"><span class="toc-section-number">2.1</span> html的hello,world</a>
- <ul>
- <li><a href="#调试helloworld"><span class="toc-section-number">2.1.1</span> 调试hello,world</a></li>
- <li><a href="#说说helloworld"><span class="toc-section-number">2.1.2</span> 说说hello,world</a></li>
- <li><a href="#想用中文"><span class="toc-section-number">2.1.3</span> 想用中文?</a></li>
- </ul></li>
- <li><a href="#其他html标记"><span class="toc-section-number">2.2</span> 其他html标记</a>
- <ul>
- <li><a href="#美妙之处"><span class="toc-section-number">2.2.1</span> 美妙之处</a></li>
- <li><a href="#更多"><span class="toc-section-number">2.2.2</span> 更多</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#无处不在的javascript"><span class="toc-section-number">3</span> 无处不在的Javascript</a>
- <ul>
- <li><a href="#javascript的helloworld"><span class="toc-section-number">3.1</span> Javascript的Hello,world</a></li>
- <li><a href="#更js一点"><span class="toc-section-number">3.2</span> 更js一点</a>
- <ul>
- <li><a href="#从数学出发"><span class="toc-section-number">3.2.1</span> 从数学出发</a></li>
- </ul></li>
- <li><a href="#设计和编程"><span class="toc-section-number">3.3</span> 设计和编程</a>
- <ul>
- <li><a href="#函数"><span class="toc-section-number">3.3.1</span> 函数</a></li>
- <li><a href="#重新设计"><span class="toc-section-number">3.3.2</span> 重新设计</a></li>
- <li><a href="#object和函数"><span class="toc-section-number">3.3.3</span> object和函数</a></li>
- <li><a href="#面向对象"><span class="toc-section-number">3.3.4</span> 面向对象</a></li>
- </ul></li>
- <li><a href="#其他"><span class="toc-section-number">3.4</span> 其他</a></li>
- <li><a href="#美妙之处-1"><span class="toc-section-number">3.5</span> 美妙之处</a></li>
- </ul></li>
- <li><a href="#无处不在的css"><span class="toc-section-number">4</span> 无处不在的CSS</a>
- <ul>
- <li><a href="#css"><span class="toc-section-number">4.1</span> CSS</a></li>
- <li><a href="#关于css"><span class="toc-section-number">4.2</span> 关于CSS</a></li>
- <li><a href="#代码结构"><span class="toc-section-number">4.3</span> 代码结构</a></li>
- <li><a href="#样式与目标"><span class="toc-section-number">4.4</span> 样式与目标</a>
- <ul>
- <li><a href="#选择器"><span class="toc-section-number">4.4.1</span> 选择器</a></li>
- </ul></li>
- <li><a href="#更有趣的css"><span class="toc-section-number">4.5</span> 更有趣的CSS</a></li>
- </ul></li>
- <li><a href="#无处不在的三剑客"><span class="toc-section-number">5</span> 无处不在的三剑客</a>
- <ul>
- <li><a href="#hellogeek"><span class="toc-section-number">5.1</span> Hello,Geek</a></li>
- <li><a href="#从源码学习"><span class="toc-section-number">5.2</span> 从源码学习</a></li>
- <li><a href="#浏览器渲染过程"><span class="toc-section-number">5.3</span> 浏览器渲染过程</a>
- <ul>
- <li><a href="#html"><span class="toc-section-number">5.3.1</span> HTML</a></li>
- </ul></li>
- <li><a href="#dom树形结构图"><span class="toc-section-number">5.4</span> DOM树形结构图</a>
- <ul>
- <li><a href="#javascript"><span class="toc-section-number">5.4.1</span> javascript</a></li>
- <li><a href="#css-1"><span class="toc-section-number">5.4.2</span> CSS</a></li>
- </ul></li>
- <li><a href="#css盒模型图"><span class="toc-section-number">5.5</span> CSS盒模型图</a></li>
- <li><a href="#笔记"><span class="toc-section-number">5.6</span> 笔记</a></li>
- </ul></li>
- <li><a href="#gnulinux-强大且free"><span class="toc-section-number">6</span> GNU/Linux 强大且Free</a>
- <ul>
- <li><a href="#什么是linux"><span class="toc-section-number">6.1</span> 什么是Linux</a></li>
- <li><a href="#操作系统"><span class="toc-section-number">6.2</span> 操作系统</a>
- <ul>
- <li><a href="#linux架构图"><span class="toc-section-number">6.2.1</span> Linux架构图</a></li>
- <li><a href="#shell"><span class="toc-section-number">6.2.2</span> Shell</a></li>
- <li><a href="#gcc"><span class="toc-section-number">6.2.3</span> GCC</a></li>
- <li><a href="#启动引导程序"><span class="toc-section-number">6.2.4</span> 启动引导程序</a></li>
- </ul></li>
- <li><a href="#从编译开始"><span class="toc-section-number">6.3</span> 从编译开始</a>
- <ul>
- <li><a href="#开始之前"><span class="toc-section-number">6.3.1</span> 开始之前</a></li>
- <li><a href="#编译nginx"><span class="toc-section-number">6.3.2</span> 编译Nginx</a></li>
- <li><a href="#其他-1"><span class="toc-section-number">6.3.3</span> 其他</a></li>
- </ul></li>
- <li><a href="#包管理"><span class="toc-section-number">6.4</span> 包管理</a></li>
- <li><a href="#ubuntu-lnmp"><span class="toc-section-number">6.5</span> Ubuntu LNMP</a>
- <ul>
- <li><a href="#update软件包列表"><span class="toc-section-number">6.5.1</span> Update软件包列表</a></li>
- <li><a href="#安装mysql"><span class="toc-section-number">6.5.2</span> 安装MySQL</a></li>
- <li><a href="#安装nginx"><span class="toc-section-number">6.5.3</span> 安装Nginx</a></li>
- <li><a href="#安装php"><span class="toc-section-number">6.5.4</span> 安装PHP</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#arduino-极客的玩具"><span class="toc-section-number">7</span> Arduino 极客的玩具</a>
- <ul>
- <li><a href="#极客的玩具"><span class="toc-section-number">7.1</span> 极客的玩具</a></li>
- <li><a href="#硬件熟悉"><span class="toc-section-number">7.2</span> 硬件熟悉</a></li>
- <li><a href="#开发环境"><span class="toc-section-number">7.3</span> 开发环境</a></li>
- <li><a href="#点亮一个led"><span class="toc-section-number">7.4</span> 点亮一个LED</a></li>
- <li><a href="#串口通信"><span class="toc-section-number">7.5</span> 串口通信</a>
- <ul>
- <li><a href="#关于arduino-setup"><span class="toc-section-number">7.5.1</span> 关于Arduino Setup()</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#python-代码如散文"><span class="toc-section-number">8</span> Python 代码如散文</a>
- <ul>
- <li><a href="#代码与散文"><span class="toc-section-number">8.1</span> 代码与散文</a>
- <ul>
- <li><a href="#开始之前-1"><span class="toc-section-number">8.1.1</span> 开始之前</a></li>
- <li><a href="#python的helloworld"><span class="toc-section-number">8.1.2</span> Python的Hello,World</a></li>
- <li><a href="#我们想要的helloworld"><span class="toc-section-number">8.1.3</span> 我们想要的Hello,World</a></li>
- </ul></li>
- <li><a href="#算法"><span class="toc-section-number">8.2</span> 算法</a></li>
- <li><a href="#实用主义哲学"><span class="toc-section-number">8.3</span> 实用主义哲学</a></li>
- <li><a href="#包管理-1"><span class="toc-section-number">8.4</span> 包管理</a>
- <ul>
- <li><a href="#python-requests"><span class="toc-section-number">8.4.1</span> python requests</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#raspberry-pi"><span class="toc-section-number">9</span> Raspberry Pi</a>
- <ul>
- <li><a href="#geek的盛宴"><span class="toc-section-number">9.1</span> Geek的盛宴</a></li>
- <li><a href="#raspberry-pi-初始化"><span class="toc-section-number">9.2</span> Raspberry Pi 初始化</a></li>
- <li><a href="#raspberry-pi-gpio"><span class="toc-section-number">9.3</span> Raspberry Pi GPIO</a></li>
- </ul></li>
- <li><a href="#server-一切皆为服务"><span class="toc-section-number">10</span> Server 一切皆为服务</a>
- <ul>
- <li><a href="#服务器"><span class="toc-section-number">10.1</span> 服务器</a></li>
- <li><a href="#web服务器"><span class="toc-section-number">10.2</span> Web服务器</a></li>
- <li><a href="#lnmp"><span class="toc-section-number">10.3</span> LNMP</a></li>
- </ul></li>
- <li><a href="#web服务"><span class="toc-section-number">11</span> Web服务</a>
- <ul>
- <li><a href="#soap-vs-restful"><span class="toc-section-number">11.1</span> SOAP VS RESTful</a></li>
- </ul></li>
- <li><a href="#http-熟悉陌生"><span class="toc-section-number">12</span> HTTP 熟悉&陌生</a>
- <ul>
- <li><a href="#你所没有深入的http"><span class="toc-section-number">12.1</span> 你所没有深入的HTTP</a>
- <ul>
- <li><a href="#打开网页时发生了什么"><span class="toc-section-number">12.1.1</span> 打开网页时发生了什么</a></li>
- <li><a href="#url组成"><span class="toc-section-number">12.1.2</span> URL组成</a></li>
- </ul></li>
- <li><a href="#一次http-get请求"><span class="toc-section-number">12.2</span> 一次HTTP GET请求</a>
- <ul>
- <li><a href="#http响应"><span class="toc-section-number">12.2.1</span> HTTP响应</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#设计restful-api"><span class="toc-section-number">13</span> 设计RESTful API</a>
- <ul>
- <li><a href="#资源"><span class="toc-section-number">13.0.1</span> 资源</a></li>
- </ul></li>
- <li><a href="#设计restful-api-1"><span class="toc-section-number">14</span> 设计RESTful API</a>
- <ul>
- <li><a href="#rest关键目标"><span class="toc-section-number">14.1</span> REST关键目标</a></li>
- <li><a href="#判断是否是-restful的约束条件"><span class="toc-section-number">14.2</span> 判断是否是 RESTful的约束条件</a></li>
- <li><a href="#json"><span class="toc-section-number">14.3</span> JSON</a></li>
- </ul></li>
- <li><a href="#环境准备"><span class="toc-section-number">15</span> 环境准备</a>
- <ul>
- <li><a href="#laravel"><span class="toc-section-number">15.1</span> Laravel</a>
- <ul>
- <li><a href="#为什么是-laravel"><span class="toc-section-number">15.1.1</span> 为什么是 Laravel</a></li>
- </ul></li>
- <li><a href="#安装-laravel"><span class="toc-section-number">15.2</span> 安装 Laravel</a>
- <ul>
- <li><a href="#gnulinux安装composer"><span class="toc-section-number">15.2.1</span> GNU/Linux安装Composer</a></li>
- </ul></li>
- <li><a href="#mysql"><span class="toc-section-number">15.3</span> MySQL</a>
- <ul>
- <li><a href="#安装mysql-1"><span class="toc-section-number">15.3.1</span> 安装MySQL</a></li>
- <li><a href="#配置mysql"><span class="toc-section-number">15.3.2</span> 配置MySQL</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#创建rest服务"><span class="toc-section-number">16</span> 创建REST服务</a>
- <ul>
- <li><a href="#数据库迁移"><span class="toc-section-number">16.1</span> 数据库迁移</a>
- <ul>
- <li><a href="#创建表"><span class="toc-section-number">16.1.1</span> 创建表</a></li>
- <li><a href="#数据库迁移-1"><span class="toc-section-number">16.1.2</span> 数据库迁移</a></li>
- </ul></li>
- <li><a href="#创建restful"><span class="toc-section-number">16.2</span> 创建RESTful</a></li>
- <li><a href="#laravel-resources"><span class="toc-section-number">16.3</span> Laravel Resources</a>
- <ul>
- <li><a href="#修改create"><span class="toc-section-number">16.3.1</span> 修改Create()</a></li>
- <li><a href="#创建表单"><span class="toc-section-number">16.3.2</span> 创建表单</a></li>
- <li><a href="#编辑模板"><span class="toc-section-number">16.3.3</span> 编辑模板</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#前端显示"><span class="toc-section-number">17</span> 前端显示</a>
- <ul>
- <li><a href="#库与车轮子"><span class="toc-section-number">17.1</span> 库与车轮子</a></li>
- <li><a href="#库"><span class="toc-section-number">17.2</span> 库</a>
- <ul>
- <li><a href="#jquery"><span class="toc-section-number">17.2.1</span> jQuery</a></li>
- <li><a href="#jquery-mobile"><span class="toc-section-number">17.2.2</span> jQuery Mobile</a></li>
- </ul></li>
- <li><a href="#网站前台显示"><span class="toc-section-number">17.3</span> 网站前台显示</a>
- <ul>
- <li><a href="#highcharts"><span class="toc-section-number">17.3.1</span> Highcharts</a></li>
- <li><a href="#实时数据"><span class="toc-section-number">17.3.2</span> 实时数据</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#restful的coap协议"><span class="toc-section-number">18</span> RESTful的CoAP协议</a>
- <ul>
- <li><a href="#coap-嵌入式系统的rest"><span class="toc-section-number">18.1</span> CoAP: 嵌入式系统的REST</a></li>
- <li><a href="#coap-命令行工具"><span class="toc-section-number">18.2</span> CoAP 命令行工具</a>
- <ul>
- <li><a href="#node-coap-cli"><span class="toc-section-number">18.2.1</span> Node CoAP CLI</a></li>
- <li><a href="#libcoap"><span class="toc-section-number">18.2.2</span> libcoap</a></li>
- <li><a href="#firefox-copper"><span class="toc-section-number">18.2.3</span> Firefox Copper</a></li>
- </ul></li>
- <li><a href="#coap-helloworld"><span class="toc-section-number">18.3</span> CoAP Hello,World</a></li>
- <li><a href="#coap-数据库查询"><span class="toc-section-number">18.4</span> CoAP 数据库查询</a>
- <ul>
- <li><a href="#node-module"><span class="toc-section-number">18.4.1</span> Node Module</a></li>
- <li><a href="#node-sqlite3"><span class="toc-section-number">18.4.2</span> Node-Sqlite3</a></li>
- <li><a href="#查询数据"><span class="toc-section-number">18.4.3</span> 查询数据</a></li>
- </ul></li>
- <li><a href="#coap-block"><span class="toc-section-number">18.5</span> CoAP Block</a>
- <ul>
- <li><a href="#coap-post"><span class="toc-section-number">18.5.1</span> CoAP POST</a></li>
- <li><a href="#coap-content-types"><span class="toc-section-number">18.5.2</span> CoAP Content Types</a></li>
- </ul></li>
- <li><a href="#coap-json"><span class="toc-section-number">18.6</span> CoAP JSON</a></li>
- <li><a href="#使用iot-coap构建物联网"><span class="toc-section-number">18.7</span> 使用IoT-CoAP构建物联网</a></li>
- </ul></li>
- <li><a href="#简单物联网"><span class="toc-section-number">19</span> 简单物联网</a>
- <ul>
- <li><a href="#硬件通信"><span class="toc-section-number">19.1</span> 硬件通信</a>
- <ul>
- <li><a href="#串口通信-1"><span class="toc-section-number">19.1.1</span> 串口通信</a></li>
- </ul></li>
- <li><a href="#硬件"><span class="toc-section-number">19.2</span> 硬件</a>
- <ul>
- <li><a href="#arduino"><span class="toc-section-number">19.2.1</span> Arduino</a></li>
- <li><a href="#继电器"><span class="toc-section-number">19.2.2</span> 继电器</a></li>
- </ul></li>
- </ul></li>
- <li><a href="#android简单示例"><span class="toc-section-number">20</span> Android简单示例</a>
- <ul>
- <li><a href="#调用web-services-get"><span class="toc-section-number">20.1</span> 调用Web Services GET</a>
- <ul>
- <li><a href="#创建restclient"><span class="toc-section-number">20.1.1</span> 创建RESTClient</a></li>
- </ul></li>
- <li><a href="#使用rest-client获取结果"><span class="toc-section-number">20.2</span> 使用REST Client获取结果</a></li>
- </ul></li>
- <li><a href="#尾声"><span class="toc-section-number">21</span> 尾声</a>
- <ul>
- <li><a href="#路"><span class="toc-section-number">21.1</span> 路</a></li>
- <li><a href="#其他-2"><span class="toc-section-number">21.2</span> 其他</a></li>
- </ul></li>
- </ul>
- </div>
- <p>本作品采用<a href="http://creativecommons.org/licenses/by-nc/4.0/">知识共享署名-非商业性使用 4.0 国际许可协议</a>进行许可。<img src="./images/88x31.png" alt="cc" /></p>
- <p>© 2014 <a href="http://www.phodal.com">Phodal Huang</a>.</p>
- <h1 id="前言"><span class="header-section-number">1</span> 前言</h1>
- <p>设计物联网系统是件有意思的事情,它需要考虑到软件、硬件、通讯等多个不同方面。通过探索不同的语言,不同的框架,从而形成不同的解决方案。</p>
- <p>在这里,我们将对设计物联网系统有一个简单的介绍,并探讨如何设计一个最小的物联网系统。</p>
- <h2 id="目标读者"><span class="header-section-number">1.1</span> 目标读者</h2>
- <p>目标读者: 初入物联网领域,希望对物联网系统有一个大概的认识和把握,并学会掌握一个基础的物联网系统的设计。</p>
- <ul>
- <li>硬件开发人员,对物联网有兴趣。</li>
- <li>没有web开发经验</li>
- <li>几乎为0的linux使用经验</li>
- <li>想快速用于生产环境<br /></li>
- <li>对硬件了解有限的开发人员。</li>
- <li>没接触过51、ARM、Arduino<br /></li>
- <li>想了解以下内容:</li>
- <li>RESTful与IOT</li>
- <li>CoAP协议</li>
- <li>MQTT</li>
- </ul>
- <p>本文档对一些概念(如)只做了一些基本介绍,以方便读者理解。如果您想进一步了解这些知识,会列出一些推荐书目,以供参考。</p>
- <h2 id="不适合人群"><span class="header-section-number">1.2</span> 不适合人群</h2>
- <ul>
- <li>如果你是在这方面已经有了丰富经验的开发者。</li>
- <li>不是为了学习而学习这方面的知识。</li>
- </ul>
- <h2 id="介绍"><span class="header-section-number">1.3</span> 介绍</h2>
- <p>关于内容的选择,这是一个有意思的话题,因为我们很难判断不同的开发者用的是怎样的语言,用的是怎样的框架。</p>
- <p>于是我们便自作主张地选择了那些适合于理论学习的语言、框架、硬件,去除掉其他一些我们不需要考虑的因素,如语法,复杂度等等。当然,这些语言、框架、硬件也是最流行的。</p>
- <ul>
- <li>Arduino: 如果你从头开始学过硬件的话,那么你会爱上它的。</li>
- <li>Raspberry PI: 如果你从头编译过GNU/Linux的话,我想你会爱上她的。</li>
- <li>Python: 简单地来说,你可以方便地使用一些扩展,同时代码就表达了你的想法。</li>
- <li>PHP : 这是一门容易部署的语言,我想你只需要在你的Ubuntu机器上,执行一下脚本就能完成安装了。而且,如果你是一个硬件开发者的话,你会更容易地找到其他开发者。</li>
- <li>Javascript : 考虑到javascript这门语言已经无处不在了,而且会更加流行。所以,在这里CoAP、MQTT等版本是基于Nodejs的。</li>
- <li>HTML、CSS : 这是必须的,同样,他们仍然无处不在。</li>
- <li>GNU/Linux: 作为部署到服务器的一部分——你需要掌握他。当然如果你要用WAMP也是可以的。</li>
- <li>CoAP: 用NodeJS构建IOT CoAP物联网</li>
- </ul>
- <h3 id="为什么没有c"><span class="header-section-number">1.3.1</span> 为什么没有C ?</h3>
- <p>如果你还想用C学理论的话,烦请出门左拐。我想,C并不适合于学习相关硬件的理论。</p>
- <h3 id="为什么不是java"><span class="header-section-number">1.3.2</span> 为什么不是JAVA ?</h3>
- <p>大有以下两个原因</p>
- <ul>
- <li>学习JAVA的人很多,但是它不适合我们集中精力构建与学习,因为无关的代码太多了。</li>
- <li>之前以及现在,我还是不喜欢JAVA (我更喜欢脚本语言,可以提高工作效率)。</li>
- </ul>
- <h2 id="如何阅读"><span class="header-section-number">1.4</span> 如何阅读</h2>
- <p>这只是一个小小的建议,仅针对于在选择阅读上没有经验的读者。</p>
- <table>
- <thead>
- <tr class="header">
- <th align="left">当前状态</th>
- <th align="left">建议</th>
- </tr>
- </thead>
- <tbody>
- <tr class="odd">
- <td align="left">软件初学者</td>
- <td align="left">从头阅读</td>
- </tr>
- <tr class="even">
- <td align="left">硬件开发者</td>
- <td align="left">从头阅读</td>
- </tr>
- <tr class="odd">
- <td align="left">没有web经验的开发者</td>
- <td align="left">从第二部分开始</td>
- </tr>
- </tbody>
- </table>
- <p>我们会在前面十章里简单介绍一些必要的基础知识,这些知识将会在后面我们构建物联网系统时用到。</p>
- <p>某一天,正走在回学校的路上的我突然想到:“未来将会是一个科技的时代——虽然现在也是——只是在未来,科技将会无处不在。如果我们依旧对周围这些无处不在的代码一无所知的话,或许我们会成为黑客帝国之中被控制的普通人。”于是开始想着,有一天人们会像学习一门语言一样开始学习编程,直到又有一天我看到了学习编程如同学习一门语言的说法。这又恰好在我做完最小物联网系统之后,算是一个有趣的时间点,我开始想着像之前做最小物联网系统的那些步骤一样,写一个简单的入门。也可以补充好之前在这个最小物联网系统缺失的那些东西,给那些正在开始试图去解决编程问题的人。</p>
- <p>让我们先从身边的语言下手,也就是现在无处不在的html+javascript+css。</p>
- <h1 id="无处不在的html"><span class="header-section-number">2</span> 无处不在的HTML</h1>
- <p>之所以从html开始,是因为我们不需要配置一个复杂的开发环境,也许你还不知道开发环境是什么东西,不过这也没关系,毕竟这些知识需要慢慢的接触才能有所了解,尤其是对于普通的业余爱好者来说,当然,对于专业选手言自然不是问题。HTML是Web的核心语言,也算是比较基础的语言。</p>
- <h2 id="html的helloworld"><span class="header-section-number">2.1</span> html的hello,world</h2>
- <p>Hello,world是一个传统,所以在这里也遵循这个有趣的传统,我们所要做的事情其实很简单,虽然也有一点点hack的感觉。——让我们先来新建一个文并命名为“helloworld.html”。</p>
- <p>(PS:大部分人应该都是在windows环境下工作的,所以你需要新建一个文本,然后重命名,或者你需要一个编辑器,在这里我们推荐用<strong>sublime text</strong>。破解不破解,注册不注册都不会对你的使用有太多的影响。)</p>
- <ol>
- <li><p>新建文件</p></li>
- <li><p>输入</p>
- <pre><code>hello,world</code></pre></li>
- <li><p>保存为->“helloworld.html”,</p></li>
- <li><p>双击打开这个文件。 正常情况下都应该是用你的默认浏览器打开。只要是一个正常工作的现代浏览器,都应该可以看到上面显示的是“Hello,world”。</p></li>
- </ol>
- <p>这才是最短的hello,world程序,但是呢?在ruby中会是这样子的</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">2.0.0-p353</span> :001 <span class="kw">></span> p <span class="st">"hello,world"</span>
- <span class="st">"hello,world"</span>
- =<span class="kw">></span> <span class="st">"hello,world"</span>
- <span class="kw">2.0.0-p353</span> :002 <span class="kw">></span></code></pre>
- <p>等等,如果你了解过html的话,会觉得这一点都不符合语法规则,但是他工作了,没有什么比安装完Nginx后看到It works!更让人激动了。</p>
- <p>遗憾的是,它可能无法在所有的浏览器上工作,所以我们需要去调试其中的bug。</p>
- <h3 id="调试helloworld"><span class="header-section-number">2.1.1</span> 调试hello,world</h3>
- <p>我们会发现我们的代码在浏览器中变成了下面的代码,如果你和我一样用的是chrome,那么你可以右键浏览器中的空白区域,点击审查元素,就会看到下面的代码。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><html></span>
- <span class="kw"><head></head></span>
- <span class="kw"><body></span>hello,world<span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>这个才是真正能在大部分浏览器上工作的代码,所以复制它到编辑器里吧。</p>
- <h3 id="说说helloworld"><span class="header-section-number">2.1.2</span> 说说hello,world</h3>
- <p>我很不喜欢其中的<*>超文本标记语言</p>
- <p>所以我们可以发现其中的关键词是标记——markup,也就是说html是一个markup,head是一个markup,body也是一个markup。</p>
- <p>然而,我们真正工作的代码是在body里面,至于为什么是在这里面,这个问题就太复杂了。打个比方来说:</p>
- <ol>
- <li><p>我们所使用的汉语是人类用智慧创造的,我们所正在学的这门语言同样也是人类创造的。</p></li>
- <li><p>我们在自己的语言里遵循着<strong>桌子是桌子,凳子是凳子</strong>的原则,很少有人会问为什么。</p></li>
- </ol>
- <h3 id="想用中文"><span class="header-section-number">2.1.3</span> 想用中文?</h3>
- <p>所以我们也可以把计算机语言与现实世界里用于交流沟通的语言划上一个等号。而我们所要学习的语言,并不是我们最熟悉的汉语语言,所以我们便觉得这些很复杂,但是如果我们试着用汉语替换掉上面的代码的话</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="er"><</span>语言>
- <span class="er"><</span>头><span class="er"><</span>结束头>
- <span class="er"><</span>身体>你好,世界<span class="er"><</span>结束身体>
- <span class="er"><</span>结束语言></code></pre>
- <p>这看上去很奇怪,只是因为是直译过去的原因,也许你会觉得这样会好理解一点,但是输入上可就一点儿也不方便,因为这键盘本身就不适合我们去输入汉字,同时也意味着可能你输入的会有问题。</p>
- <p>让我们把上面的代码代替掉原来的代码然后保存,打开浏览器会看到下面的结果</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="er"><</span>语言> <span class="er"><</span>头><span class="er"><</span>结束头> <span class="er"><</span>身体>你好,世界<span class="er"><</span>结束身体> <span class="er"><</span>结束语言></code></pre>
- <p>更不幸的结果可能是</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="er"><</span>璇█> <span class="er"><</span>澶�><span class="er"><</span>缁撴潫澶�> <span class="er"><</span>韬綋>浣犲ソ锛屼笘鐣�<span class="er"><</span>缁撴潫韬綋> <span class="er"><</span>缁撴潫璇█></code></pre>
- <p>这是一个编码问题,对中文支持不友好。</p>
- <p>我们把上面的代码改为和标记语言一样的结构</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="er"><</span>语言>
- <span class="er"><</span>头><span class="er"><</span>/头>
- <span class="er"><</span>身体>你好,世界<span class="er"><</span>/身体>
- <span class="er"><</span>结束语言></code></pre>
- <p>于是我们看到的结果便是</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="er"><</span>语言> <span class="er"><</span>头> <span class="er"><</span>身体>你好,世界</code></pre>
- <p>被chrome浏览器解析成什么样了?</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><html><head></head><body></span><span class="er"><</span>语言>
- <span class="er"><</span>头><span class="co"><!--头--></span>
- <span class="er"><</span>身体>你好,世界<span class="co"><!--身体--></span>
- <span class="co"><!--语言--></span>
- <span class="kw"></body></html></span> </code></pre>
- <p>以</p>
- <p>结尾的是注释,写给人看的代码,不是给机器看的,所以机器不会去理解这些代码。</p>
- <p>但是当我们把代码改成</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><whatwewanttosay></span>你好世界<span class="kw"></whatwewanttosay></span></code></pre>
- <p>浏览器上面显示的内容就变成了</p>
- <pre class="sourceCode html"><code class="sourceCode html">你好世界</code></pre>
- <p>或许你会觉得很神奇,但是这一点儿也不神奇,虽然我们的中文语法也遵循着标记语言的标准,但是我们的浏览器不支持中文标记。</p>
- <p>结论:</p>
- <ol>
- <li>浏览器对中文支持不友好。</li>
- <li>浏览器对英文支持友好。</li>
- </ol>
- <p>刚开始的时候不要对中文编程有太多的想法,这是很不现实的:</p>
- <ol>
- <li>现有的系统都是基于英语语言环境构建的,对中文支持不是很友好。</li>
- <li>中文输入的速度在某种程度上来说没有英语快。</li>
- </ol>
- <p>我们离开话题已经很远了,但是这里说的都是针对于那些不满于英语的人来说的,只有当我们可以从头构建一个中文系统的时候才是可行的,而这些就要将cpu、软件、硬件都包含在内,甚至我们还需要考虑重新设计cpu的结构,在某种程度上来说会有些不现实。或许,需要一代又一代人的努力。忘记那些吧,师夷长之技以治夷。</p>
- <h2 id="其他html标记"><span class="header-section-number">2.2</span> 其他html标记</h2>
- <p>添加一个标题,</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><title></span>标题<span class="kw"></title></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>hello,world<span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>我们便可以在浏览器的最上方看到“标题”二字,就像我们常用的淘宝网,也包含了上面的东西,只是还包括了更多的东西,所以你也可以看懂那些我们可以看到的淘宝的标题。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><title></span>标题<span class="kw"></title></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- hello,world
- <span class="kw"><h1></span>大标题<span class="kw"></h1></span>
- <span class="kw"><h2></span>次标题<span class="kw"></h2></span>
- <span class="kw"><h3></span>...<span class="kw"></h3></span>
- <span class="kw"><ul></span>
- <span class="kw"><li></span>列表1<span class="kw"></li></span>
- <span class="kw"><li></span>列表2<span class="kw"></li></span>
- <span class="kw"></ul></span>
- <span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>更多的东西可以在一些书籍上看到,这边所要说的只是一次简单的语言入门,其他的东西都和这些类似。</p>
- <h3 id="美妙之处"><span class="header-section-number">2.2.1</span> 美妙之处</h3>
- <p>我们简单地上手了一门不算是语言的语言,浏览器简化了这其中的大部分过程,虽然没有C和其他语言来得有专业感,但是我们试着去开始写代码了。我们可能在未来的某一篇中可能会看到类似的语言,诸如python,我们所要做的就是</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">python</span> file.py
- =<span class="kw">>hello</span>,world</code></pre>
- <p>然后在终端上返回结果。只是因为在我看来学会html是有意义的,简单的上手,然后再慢慢地深入,如果一开始我们就去理解指针,开始去理解类。我们甚至还知道程序是怎么编译运行的时候,在这个过程中又发生了什么。虽然现在我们也没能理解这其中发生了什么,但是至少展示了</p>
- <ol>
- <li>中文编程语言在当前意义不大,不现实,效率不高兼容性差</li>
- <li>语言的语法是固定的。(ps:虽然我们也可以进行扩充,我们将会在后来支持上述的中文标记。)</li>
- <li>已经开始写代码,而不是还在配置开发环境。</li>
- <li>随身的工具才是最好的,最常用的code也才是实在的。</li>
- </ol>
- <h3 id="更多"><span class="header-section-number">2.2.2</span> 更多</h3>
- <p>我们还没有试着去解决“某商店里的糖一颗5块钱,小明买了3颗糖,小明一共花了多少钱”的问题。也就是说我们学会的是一个还不能解决实际问题的语言,于是我们还需要学点东西,比如javascript,css。我们可以将Javascript理解为解决问题的语言,html则是前端显示,css是配置文件,这样的话,我们会在那之后学会成为一个近乎专业的程序员。我们刚刚学习了一下怎么在前端显示那些代码的行为,于是我们还需要Javascript。</p>
- <h1 id="无处不在的javascript"><span class="header-section-number">3</span> 无处不在的Javascript</h1>
- <p>Javascript现在已经无处不在了,也许你正打开的某个网站,他便可能是node.js+json+javascript+mustache.js完成的,虽然你还没理解上面那些是什么,也正是因为你不理解才需要去学习更多的东西。但是你只要知道Javascript已经无处不在了,它可能就在你手机上的某个app里,就在你浏览的网页里,就运行在你IDE中的某个进程里。</p>
- <h2 id="javascript的helloworld"><span class="header-section-number">3.1</span> Javascript的Hello,world</h2>
- <p>这里我们还需要有一个helloworld.html,Javascript是专为网页交互而设计的脚本语言,所以我们一点点来开始这部分的旅途,先写一个符合标准的helloworld.html</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></head></span>
- <span class="kw"><body></body></span>
- <span class="kw"></html></span></code></pre>
- <p>然后开始融入我们的javascript,向HTML中插入Javascript的方法,就需要用到html中的<script>标签,我们先用页面嵌入的方法来写helloworld。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><script></span>
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">'hello,world'</span>);
- <<span class="ot">/script></span>
- <span class="ot"> </head</span>>
- <body><<span class="ot">/body></span>
- <span class="ot"></html</span>></code></pre>
- <p>按照标准的写法,我们还需要声明这个脚本的类型</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="kw">></span>
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">'hello,world'</span>);
- <<span class="ot">/script></span>
- <span class="ot"> </head</span>>
- <body><<span class="ot">/body></span>
- <span class="ot"></html</span>></code></pre>
- <p>没有显示hello,world?试试下面的代码</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="kw">></span>
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">'hello,world'</span>);
- <<span class="ot">/script></span>
- <span class="ot"> </head</span>>
- <body>
- <noscript>
- disable Javascript
- <<span class="ot">/noscript></span>
- <span class="ot"> </body</span>>
- <<span class="ot">/html></span></code></pre>
- <h2 id="更js一点"><span class="header-section-number">3.2</span> 更js一点</h2>
- <p>我们需要让我们的代码看上去更像是js,同时是以js结尾。就像C语言的源码是以C结尾的,我们也同样需要让我们的代码看上去更正式一点。于是我们需要在helloworld.html的同一文件夹下创建一个app.js文件,在里面写着</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="st">'hello,world'</span>);</code></pre>
- <p>同时我们的helloworld.html还需要告诉我们的浏览器js代码在哪里</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"app.js"</span><span class="kw">></script></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><noscript></span>
- disable Javascript
- <span class="kw"></noscript></span>
- <span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <h3 id="从数学出发"><span class="header-section-number">3.2.1</span> 从数学出发</h3>
- <p>让我们回到第一章讲述的小明的问题,<strong>从实际问题下手编程,更容易学会编程</strong>。小学时代的数学题最喜欢这样子了——某商店里的糖一个5块钱,小明买了3个糖,小明一共花了多少钱。在编程方面,也许我们还算是小学生。最直接的方法就是直接计算3x5=?</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="dv">3</span>*<span class="dv">5</span>);</code></pre>
- <p>document.write实际也我们可以理解为输出,也就是往页面里写入3*5的结果,在有双引号的情况下会输出字符串。我们便会在浏览器上看到15,这便是一个好的开始,也是一个糟糕的开始。</p>
- <h2 id="设计和编程"><span class="header-section-number">3.3</span> 设计和编程</h2>
- <p>对于实际问题,如果我们只是止于所要得到的结果,很多年之后,我们就成为了code monkey。对这个问题进行再一次设计,所谓的设计有些时候会把简单的问题复杂化,有些时候会使以后的扩展更加简单。这一天因为这家商店的糖价格太高了,于是店长将价格降为了4块钱。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="dv">3</span>*<span class="dv">4</span>);</code></pre>
- <p>于是我们又得到了我们的结果,但是下次我们看到这些代码的时候没有分清楚哪个是糖的数量,哪个是价格,于是我们重新设计了程序</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">tang=<span class="dv">4</span>;
- num=<span class="dv">3</span>;
- <span class="ot">document</span>.<span class="fu">write</span>(tang*num);</code></pre>
- <p>这才能叫得上是程序设计,或许你注意到了“;”这个符号的存在,我想说的是这是另外一个标准,我们不得不去遵守,也不得不去fuck。</p>
- <h3 id="函数"><span class="header-section-number">3.3.1</span> 函数</h3>
- <p>记得刚开始学三角函数的时候,我们会写</p>
- <pre><code>sin 30=0.5</code></pre>
- <p>而我们的函数也是类似于此,换句话说,因为很多搞计算机的先驱都学好了数学,都把数学世界的规律带到了计算机世界,所以我们的函数也是类似于此,让我们做一个简单的开始。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">hello</span>(){
- <span class="kw">return</span> <span class="ot">document</span>.<span class="fu">write</span>(<span class="st">"hello,world"</span>);
- }
- <span class="fu">hello</span>();</code></pre>
- <p>当我第一次看到函数的时候,有些小激动终于出现了。我们写了一个叫hello的函数,它返回了往页面中写入hello,world的方法,然后我们调用了hello这个函数,于是页面上有了hello,world。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">sin</span>(degree){
- <span class="kw">return</span> <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">Math</span>.<span class="fu">sin</span>(degree));
- }
- <span class="fu">sin</span>(<span class="dv">30</span>);</code></pre>
- <p>在这里degree就称之为变量。 于是输出了-0.9880316240928602,而不是0.5,因为这里用的是弧度制,而不是角度制。</p>
- <pre><code>sin(30)</code></pre>
- <p>的输出结果有点类似于sin 30。写括号的目的在于,括号是为了方便解析,这个在不同的语言中可能是不一样的,比如在ruby中我们可以直接用类似于数学中的表达:</p>
- <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> > <span class="dt">Math</span>.sin <span class="dv">30</span>
- => -<span class="fl">0.9880316240928618</span>
- <span class="fl">2.0</span>.<span class="dv">0</span>-p353 :<span class="dv">005</span> ></code></pre>
- <p>我们可以在函数中传入多个变量,于是我们再回到小明的问题,就会这样去编写代码。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">calc</span>(tang,num){
- result=tang*num;
- <span class="ot">document</span>.<span class="fu">write</span>(result);
- }
- <span class="fu">calc</span>(<span class="dv">3</span>,<span class="dv">4</span>);</code></pre>
- <p>但是从某种程度上来说,我们的calc做了计算的事又做了输出的事,总的来说设计上有些不好。</p>
- <h3 id="重新设计"><span class="header-section-number">3.3.2</span> 重新设计</h3>
- <p>我们将输出的工作移到函数的外面,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">calc</span>(tang,num){
- <span class="kw">return</span> tang*num;
- }
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="fu">calc</span>(<span class="dv">3</span>,<span class="dv">4</span>));</code></pre>
- <p>接着我们用一种更有意思的方法来写这个问题的解决方案</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">calc</span>(tang,num){
- <span class="kw">return</span> tang*num;
- }
- <span class="kw">function</span> <span class="fu">printResult</span>(tang,num){
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="fu">calc</span>(tang,num));
- }
- <span class="fu">printResult</span>(<span class="dv">3</span>, <span class="dv">4</span>)</code></pre>
- <p>看上去更专业了一点点,如果我们只需要计算的时候我们只需要调用calc,如果我们需要输出的时候我们就调用printResult的方法。</p>
- <h3 id="object和函数"><span class="header-section-number">3.3.3</span> object和函数</h3>
- <p>我们还没有说清楚之前我们遇到过的document.write以及Math.sin的语法为什么看上去很奇怪,所以让我们看看他们到底是什么,修改app.js为以下内容</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> document);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> Math);</code></pre>
- <p>typeof document会返回document的数据类型,就会发现输出的结果是</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">object object</code></pre>
- <p>所以我们需要去弄清楚什么是object。对象的定义是</p>
- <blockquote>
- 无序属性的集合,其属性可以包含基本值、对象或者函数。
- </blockquote>
- <p>创建一个object,然后观察这便是我们接下来要做的</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">store={};
- <span class="ot">store</span>.<span class="fu">tang</span>=<span class="dv">4</span>;
- <span class="ot">store</span>.<span class="fu">num</span>=<span class="dv">3</span>;
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">store</span>.<span class="fu">tang</span>*<span class="ot">store</span>.<span class="fu">num</span>);</code></pre>
- <p>我们就有了和document.write一样的用法,这也是对象的美妙之处,只是这里的对象只是包含着基本值,因为</p>
- <pre><code>typeof story.tang="number"</code></pre>
- <p>一个包含对象的对象应该是这样子的。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">store={};
- <span class="ot">store</span>.<span class="fu">tang</span>=<span class="dv">4</span>;
- <span class="ot">store</span>.<span class="fu">num</span>=<span class="dv">3</span>;
- <span class="ot">document</span>.<span class="fu">writeln</span>(<span class="ot">store</span>.<span class="fu">tang</span>*<span class="ot">store</span>.<span class="fu">num</span>);
- <span class="kw">var</span> wall=<span class="kw">new</span> <span class="fu">Object</span>();
- <span class="ot">wall</span>.<span class="fu">store</span>=store;
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> <span class="ot">wall</span>.<span class="fu">store</span>);</code></pre>
- <p>而我们用到的document.write和上面用到的document.writeln都是属于这个无序属性集合中的函数。</p>
- <p>下面代码说的就是这个无序属性集中中的函数。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> IO=<span class="kw">new</span> <span class="fu">Object</span>();
- <span class="kw">function</span> <span class="fu">print</span>(result){
- <span class="ot">document</span>.<span class="fu">write</span>(result);
- };
- <span class="ot">IO</span>.<span class="fu">print</span>=print;
- <span class="ot">IO</span>.<span class="fu">print</span>(<span class="st">"a obejct with function"</span>);
- <span class="ot">IO</span>.<span class="fu">print</span>(<span class="kw">typeof</span> <span class="ot">IO</span>.<span class="fu">print</span>);</code></pre>
- <p>我们定义了一个叫IO的对象,声明对象可以用</p>
- <pre><code>var store={};</code></pre>
- <p>又或者是</p>
- <pre><code>var store=new Object{};</code></pre>
- <p>两者是等价的,但是用后者的可读性会更好一点,我们定义了一个叫print的函数,他的作用也就是document.write,IO中的print函数是等价于print()函数,这也就是对象和函数之间的一些区别,对象可以包含函数,对象是无序属性的集合,其属性可以包含基本值、对象或者函数。</p>
- <p>复杂一点的对象应该是下面这样的一种情况。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person={<span class="dt">name</span>:<span class="st">"phodal"</span>,<span class="dt">weight</span>:<span class="dv">50</span>,<span class="dt">height</span>:<span class="dv">166</span>};
- <span class="kw">function</span> <span class="fu">dream</span>(){
- future;
- };
- <span class="ot">Person</span>.<span class="fu">future</span>=dream;
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> Person);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">Person</span>.<span class="fu">future</span>);</code></pre>
- <p>而这些会在我们未来的实际编程过程中用得更多。</p>
- <h3 id="面向对象"><span class="header-section-number">3.3.4</span> 面向对象</h3>
- <p>开始之前先让我们简化上面的代码,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">Person</span>.<span class="fu">future</span>=<span class="kw">function</span> <span class="fu">dream</span>(){
- future;
- }</code></pre>
- <p>看上去比上面的简单多了,不过我们还可以简化为下面的代码。。。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person=<span class="kw">function</span>(){
- <span class="kw">this</span>.<span class="fu">name</span>=<span class="st">"phodal"</span>;
- <span class="kw">this</span>.<span class="fu">weight</span>=<span class="dv">50</span>;
- <span class="kw">this</span>.<span class="fu">height</span>=<span class="dv">166</span>;
- <span class="kw">this</span>.<span class="fu">future</span>=<span class="kw">function</span> <span class="fu">dream</span>(){
- <span class="kw">return</span> <span class="st">"future"</span>;
- };
- };
- <span class="kw">var</span> person=<span class="kw">new</span> <span class="fu">Person</span>();
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">person</span>.<span class="fu">name</span>+<span class="st">"<br>"</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> person+<span class="st">"<br>"</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="kw">typeof</span> <span class="ot">person</span>.<span class="fu">future</span>+<span class="st">"<br>"</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">person</span>.<span class="fu">future</span>()+<span class="st">"<br>"</span>);</code></pre>
- <p>只是在这个时候Person是一个函数,但是我们声明的person却变成了一个对象<strong>一个Javascript函数也是一个对象,并且,所有的对象从技术上讲也只不过是函数。</strong>这里的“<br>”是HTML中的元素,称之为DOM,在这里起的是换行的作用,我们会在稍后介绍它,这里我们先关心下this。this关键字表示函数的所有者或作用域,也就是这里的Person。</p>
- <p>上面的方法显得有点不可取,换句话说和一开始的</p>
- <pre><code>document.write(3*4);</code></pre>
- <p>一样,不具有灵活性,因此在我们完成功能之后,我们需要对其进行优化,这就是程序设计的真谛——解决完实际问题后,我们需要开始真正的设计,而不是解决问题时的编程。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Person=<span class="kw">function</span>(name,weight,height){
- <span class="kw">this</span>.<span class="fu">name</span>=name;
- <span class="kw">this</span>.<span class="fu">weight</span>=weight;
- <span class="kw">this</span>.<span class="fu">height</span>=height;
- <span class="kw">this</span>.<span class="fu">future</span>=<span class="kw">function</span>(){
- <span class="kw">return</span> <span class="st">"future"</span>;
- };
- };
- <span class="kw">var</span> phodal=<span class="kw">new</span> <span class="fu">Person</span>(<span class="st">"phodal"</span>,<span class="dv">50</span>,<span class="dv">166</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">name</span>+<span class="st">"<br>"</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">weight</span>+<span class="st">"<br>"</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">height</span>+<span class="st">"<br>"</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">future</span>()+<span class="st">"<br>"</span>);</code></pre>
- <p>于是,产生了这样一个可重用的Javascript对象,this关键字确立了属性的所有者。</p>
- <h2 id="其他"><span class="header-section-number">3.4</span> 其他</h2>
- <p>Javascript还有一个很强大的特性,也就是原型继承,不过这里我们先不考虑这些部分,用尽量少的代码及关键字来实际我们所要表达的核心功能,这才是这里的核心,其他的东西我们可以从其他书本上学到。</p>
- <p>所谓的继承,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> Chinese=<span class="kw">function</span>(){
- <span class="kw">this</span>.<span class="fu">country</span>=<span class="st">"China"</span>;
- }
- <span class="kw">var</span> Person=<span class="kw">function</span>(name,weight,height){
- <span class="kw">this</span>.<span class="fu">name</span>=name;
- <span class="kw">this</span>.<span class="fu">weight</span>=weight;
- <span class="kw">this</span>.<span class="fu">height</span>=height;
- <span class="kw">this</span>.<span class="fu">futrue</span>=<span class="kw">function</span>(){
- <span class="kw">return</span> <span class="st">"future"</span>;
- }
- }
- <span class="ot">Chinese</span>.<span class="fu">prototype</span>=<span class="kw">new</span> <span class="fu">Person</span>();
- <span class="kw">var</span> phodal=<span class="kw">new</span> <span class="fu">Chinese</span>(<span class="st">"phodal"</span>,<span class="dv">50</span>,<span class="dv">166</span>);
- <span class="ot">document</span>.<span class="fu">write</span>(<span class="ot">phodal</span>.<span class="fu">country</span>);</code></pre>
- <p>完整的Javascript应该由下列三个部分组成:</p>
- <ul>
- <li>核心(ECMAScript)——核心语言功能</li>
- <li>文档对象模型(DOM)——访问和操作网页内容的方法和接口</li>
- <li>浏览器对象模型(BOM)——与浏览器交互的方法和接口</li>
- </ul>
- <p>我们在上面讲的都是ECMAScript,也就是语法相关的,但是JS真正强大的,或者说我们最需要的可能就是对DOM的操作,这也就是为什么jQuery等库可以流行的原因之一,而核心语言功能才是真正在哪里都适用的,至于BOM,真正用到的机会很少,因为没有完善的统一的标准。</p>
- <p>一个简单的DOM示例,</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><noscript></span>
- disable Javascript
- <span class="kw"></noscript></span>
- <span class="kw"><p</span><span class="ot"> id=</span><span class="st">"para"</span><span class="ot"> style=</span><span class="st">"color:red"</span><span class="kw">></span>Red<span class="kw"></p></span>
- <span class="kw"></body></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"app.js"</span><span class="kw">></script></span>
- <span class="kw"></html></span></code></pre>
- <p>我们需要修改一下helloworld.html添加</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><p</span><span class="ot"> id=</span><span class="st">"para"</span><span class="ot"> style=</span><span class="st">"color:red"</span><span class="kw">></span>Red<span class="kw"></p></span></code></pre>
- <p>同时还需要将script标签移到body下面,如果没有意外的话我们会看到页面上用红色的字体显示Red,修改app.js。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para=<span class="ot">document</span>.<span class="fu">getElementById</span>(<span class="st">"para"</span>);
- <span class="ot">para</span>.<span class="ot">style</span>.<span class="fu">color</span>=<span class="st">"blue"</span>;</code></pre>
- <p>接着,字体就变成了蓝色,有了DOM我们就可以对页面进行操作,可以说我们看到的绝大部分的页面效果都是通过DOM操作实现的。</p>
- <h2 id="美妙之处-1"><span class="header-section-number">3.5</span> 美妙之处</h2>
- <p>这里说到的Javascript仅仅只是其中的一小小部分,忽略掉的东西很多,只关心的是如何去设计一个实用的app,作为一门编程语言,他还有其他强大的内制函数,要学好需要一本有价值的参考书。这里提到的只是其中的不到20%的东西,其他的80%或者更多会在你解决问题的时候出现。</p>
- <ul>
- <li>我们可以创建一个对象或者函数,它可以包含基本值、对象或者函数。</li>
- <li>我们可以用Javascript修改页面的属性,虽然只是简单的示例。</li>
- <li>我们还可以去解决实际的编程问题。</li>
- </ul>
- <h1 id="无处不在的css"><span class="header-section-number">4</span> 无处不在的CSS</h1>
- <p>或许你觉得CSS一点儿也不重要,而事实上,如果说HTML是建筑的框架,CSS就是房子的装修。那么Javascript呢,我听到的最有趣的说法是小三——还是先让我们回到代码上来吧。</p>
- <h2 id="css"><span class="header-section-number">4.1</span> CSS</h2>
- <p>下面就是我们之前说到的代码,css将Red三个字母变成了红色。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><p</span><span class="ot"> id=</span><span class="st">"para"</span><span class="ot"> style=</span><span class="st">"color:red"</span><span class="kw">></span>Red<span class="kw"></p></span>
- <span class="kw"></body></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"app.js"</span><span class="kw">></script></span>
- <span class="kw"></html></span></code></pre>
- <p>只是,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para=<span class="ot">document</span>.<span class="fu">getElementById</span>(<span class="st">"para"</span>);
- <span class="ot">para</span>.<span class="ot">style</span>.<span class="fu">color</span>=<span class="st">"blue"</span>;</code></pre>
- <p>将字体变成了蓝色,CSS+HTML让页面有序的工作着,但是Javascript却打乱了这些秩序,有着唯恐世界不乱的精彩,也难怪被冠以小三之名了——或许终于可以理解,为什么以前人们对于Javascript没有好感了——不过这里要讲的是正室,也就是CSS,这时还没有Javascript。</p>
- <div class="figure">
- <img src="./images/redfonts.png" alt="Red Fonts" />
- <p>Red Fonts</p>
- </div>
- <h2 id="关于css"><span class="header-section-number">4.2</span> 关于CSS</h2>
- <p>这不是一篇专业讲述CSS的书籍,所以我不会去说CSS是怎么来的,有些东西我们既然可以很容易从其他地方知道,也就不需要花太多时间去重复。诸如重构等这些的目的之一也在于去除重复的代码,不过有些重复是不可少的,也是有必要的,而通常这些东西可能是由其他地方复制过来的。</p>
- <p>到目前为止我们没有依赖于任何特殊的硬件或者是软件,对于我们来说我们最基本的需求就是一台电脑,或者可以是你的平板电脑,当然也可以是你的智能手机,因为他们都有个浏览器,而这些都是能用的,对于我们的CSS来说也不会有例外的。</p>
- <p>CSS(Cascading Style Sheets),到今天我也没有记得他的全称,CSS还有一个中文名字是层叠式样式表,事实上翻译成什么可能并不是我们关心的内容,我们需要关心的是他能做些什么。作为三剑客之一,它的主要目的在于可以让我们方便灵活地去控制Web页面的外观表现。我们可以用它做出像淘宝一样复杂的界面,也可以像我们的书本一样简单,不过如果要和我们书本一样简单的话,可能不需要用到CSS。HTML一开始就是依照报纸的格式而设计的,我们还可以继续用上面说到的编辑器,又或者是其他的。如果你喜欢DreamWeaver那也不错,不过一开始使用IDE可无助于我们写出良好的代码。</p>
- <p>忘说了,CSS也是有版本的,和windows,Linux内核等等一样,但是更新可能没有那么频繁,HTML也是有版本的,JS也是有版本的,复杂的东西不是当前考虑的内容。</p>
- <h2 id="代码结构"><span class="header-section-number">4.3</span> 代码结构</h2>
- <p>对于我们的上面的Red示例来说,如果没有一个好的结构,那么以后可能就是这样子。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><p</span><span class="ot"> style=</span><span class="st">"font-size: 22px;color:#f00;text-align: center;padding-left: 20px;"</span><span class="kw">></span>如果没有一个好的结构<span class="kw"></p></span>
- <span class="kw"><p</span><span class="ot"> style=</span><span class="st">" font-size:44px;color:#3ed;text-indent: 2em;padding-left: 2em;"</span><span class="kw">></span>那么以后可能就是这样子。。。。<span class="kw"></p></span>
- <span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>虽然我们看到的还是一样的:</p>
- <div class="figure">
- <img src="./images/nostyle.png" alt="No Style" />
- <p>No Style</p>
- </div>
- <p>于是我们就按各种书上的建议重新写了上面的代码</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><title></span>CSS example<span class="kw"></title></span>
- <span class="kw"><style</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="kw">></span>
- <span class="fl">.para</span><span class="kw">{</span>
- <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
- <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
- <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
- <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
- <span class="kw">}</span>
- <span class="fl">.para2</span><span class="kw">{</span>
- <span class="kw">font-size:</span><span class="dt">44px</span><span class="kw">;</span>
- <span class="kw">color:</span><span class="dt">#3ed</span><span class="kw">;</span>
- <span class="kw">text-indent:</span> <span class="dt">2em</span><span class="kw">;</span>
- <span class="kw">padding-left:</span> <span class="dt">2em</span><span class="kw">;</span>
- <span class="kw">}</span>
- <span class="kw"></style></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><p</span><span class="ot"> class=</span><span class="st">"para"</span><span class="kw">></span>如果没有一个好的结构<span class="kw"></p></span>
- <span class="kw"><p</span><span class="ot"> class=</span><span class="st">"para2"</span><span class="kw">></span>那么以后可能就是这样子。。。。<span class="kw"></p></span>
- <span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>总算比上面好看也好理解多了,这只是临时的用法,当文件太大的时候,正式一点的写法应该如下所示:</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><title></span>CSS example<span class="kw"></title></span>
- <span class="kw"><style</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"style.css"</span><span class="kw">></style></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><p</span><span class="ot"> class=</span><span class="st">"para"</span><span class="kw">></span>如果没有一个好的结构<span class="kw"></p></span>
- <span class="kw"><p</span><span class="ot"> class=</span><span class="st">"para2"</span><span class="kw">></span>那么以后可能就是这样子。。。。<span class="kw"></p></span>
- <span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>我们需要</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><title></span>CSS example<span class="kw"></title></span>
- <span class="kw"><link</span><span class="ot"> href=</span><span class="st">"./style.css"</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span> <span class="kw">/></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><p</span><span class="ot"> class=</span><span class="st">"para"</span><span class="kw">></span>如果没有一个好的结构<span class="kw"></p></span>
- <span class="kw"><p</span><span class="ot"> class=</span><span class="st">"para2"</span><span class="kw">></span>那么以后可能就是这样子。。。。<span class="kw"></p></span>
- <span class="kw"></body></span>
- <span class="kw"></html></span></code></pre>
- <p>然后我们有一个像app.js一样的style.css放在同目录下,而他的内容便是</p>
- <pre class="sourceCode css"><code class="sourceCode css"><span class="fl">.para</span><span class="kw">{</span>
- <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
- <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
- <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
- <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
- <span class="kw">}</span>
- <span class="fl">.para2</span><span class="kw">{</span>
- <span class="kw">font-size:</span><span class="dt">44px</span><span class="kw">;</span>
- <span class="kw">color:</span><span class="dt">#3ed</span><span class="kw">;</span>
- <span class="kw">text-indent:</span> <span class="dt">2em</span><span class="kw">;</span>
- <span class="kw">padding-left:</span> <span class="dt">2em</span><span class="kw">;</span>
- <span class="kw">}</span></code></pre>
- <p>这代码和JS的代码有如此多的相似</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para={
- <span class="dt">font_size</span>:<span class="st">'22px'</span>,
- <span class="dt">color</span>:<span class="st">'#f00'</span>,
- <span class="dt">text_align</span>:<span class="st">'center'</span>,
- <span class="dt">padding_left</span>:<span class="st">'20px'</span>,
- }</code></pre>
- <p>而22px、20px以及#f00都是数值,因此:</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> para={
- <span class="dt">font_size</span>:22px,
- <span class="dt">color</span>:#f00,
- <span class="dt">text_align</span>:center,
- <span class="dt">padding_left</span>:20px,
- } </code></pre>
- <p>目测差距已经尽可能的小了,至于这些话题会在以后讨论到,如果要让我们的编译器更正确的工作,那么我们就需要非常多这样的符号,除非你乐意去理解:</p>
- <pre class="lisp"><code>(dotimes (i 4) (print i))</code></pre>
- <p>总的来说我们减少了符号的使用,但是用lisp便带入了更多的括号,不过这是一种简洁的表达方式,也许我们可以在其他语言中看到。</p>
- <pre class="regex"><code>\d{2}/[A-Z][a-z][a-z]/\d{4}</code></pre>
- <p>上面的代码,是为了从一堆数据中找出“某日/某月/某年”。如果一开始不理解那是正则表达式,就会觉得那个很复杂。</p>
- <p>这门语言可能是为设计师而设计的,但是设计师大部分还是不懂编程的,不过相对来说这门语言还是比其他语言简单易懂一些。</p>
- <h2 id="样式与目标"><span class="header-section-number">4.4</span> 样式与目标</h2>
- <p>如下所示,就是我们的样式</p>
- <pre class="sourceCode css"><code class="sourceCode css"><span class="fl">.para</span><span class="kw">{</span>
- <span class="kw">font-size:</span> <span class="dt">22px</span><span class="kw">;</span>
- <span class="kw">color:</span><span class="dt">#f00</span><span class="kw">;</span>
- <span class="kw">text-align:</span> <span class="dt">center</span><span class="kw">;</span>
- <span class="kw">padding-left:</span> <span class="dt">20px</span><span class="kw">;</span>
- <span class="kw">}</span></code></pre>
- <p>我们的目标就是</p>
- <pre><code>如果没有一个好的结构</code></pre>
- <p>所以样式和目标在这里牵手了,问题是他们是如何在一起的呢?下面就是CSS与HTML沟通的重点所在了:</p>
- <h3 id="选择器"><span class="header-section-number">4.4.1</span> 选择器</h3>
- <p>我们用到的选择器叫做类选择器,也就是class,或者说应该称之为class选择器更合适。与类选择器最常一起出现的是ID选择器,不过这个适用于比较高级的场合,诸如用JS控制DOM的时候就需要用到ID选择器。而基本的选择器就是如下面的例子:</p>
- <pre><code>p.para{
- color:#f0f;
- }</code></pre>
- <p>将代码添加到style.css的最下面会发现“如果没有一个好的结构”变成了粉红色,当然我们还会有这样的写法</p>
- <pre><code>p>.para{
- color:#f0f;
- }</code></pre>
- <p>为了产生上面的特殊的样式,虽然不好看,但是我们终于理解什么叫层叠样式了,下面的代码的重要度比上面高,也因此有更高的优先规则。</p>
- <p>而通常我们可以通过一个</p>
- <pre><code>p{
- text-align:left;
- }</code></pre>
- <p>这样的元素选择器来给予所有的p元素一个左对齐。</p>
- <p>还有复杂一点的复合型选择器,下面的是HTML文件</p>
- <pre><code><!DOCTYPE html>
- <html>
- <head>
- <title>CSS example</title>
- <link href="./style.css" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <p class="para">如果没有一个好的结构</p>
- <div id="content">
- <p class="para2">那么以后可能就是这样子。。。。</p>
- </div>
- </body>
- </html></code></pre>
- <p>还有CSS文件</p>
- <pre><code>.para{
- font-size: 22px;
- color:#f00;
- text-align: center;
- padding-left: 20px;
- }
- .para2{
- font-size:44px;
- color:#3ed;
- text-indent: 2em;
- padding-left: 2em;
- }
- p.para{
- color:#f0f;
- }
- div#content p {
- font-size:22px;
- }</code></pre>
- <h2 id="更有趣的css"><span class="header-section-number">4.5</span> 更有趣的CSS</h2>
- <p>一个包含了para2以及para_bg的例子</p>
- <pre><code> <div id="content">
- <p class="para2 para_bg">那么以后可能就是这样子。。。。</p>
- </div>
- </code></pre>
- <p>我们只是添加了一个黑色的背景</p>
- <pre><code>.para_bg{
- background-color:#000;
- }</code></pre>
- <p>重新改变后的网页变得比原来有趣了很多,所谓的继承与合并就是上面的例子。</p>
- <p>我们还可以用CSS3做出更多有趣的效果,而这些并不在我们的讨论范围里面,因为我们讨论的是be a geek。</p>
- <p>或许我们写的代码都是那么的简单,从HTML到Javascript,还有现在的CSS,只是总有一些核心的东西,而不是去考虑那些基础语法,基础的东西我们可以在实践的过程中一一发现。但是我们可能发现不了,或者在平时的使用中考虑不到一些有趣的用法或者说特殊的用法,这时候可以通过观察一些精致设计的代码中学习到。复杂的东西可以变得很简单,简单的东西也可以变得很复杂。</p>
- <h1 id="无处不在的三剑客"><span class="header-section-number">5</span> 无处不在的三剑客</h1>
- <p>这时我们终于了解了我们的三剑客,他们也就这么可以结合到一起了,HTML+Javascript+CSS是这一切的基础。而我们用到的其他语言如PHP、Python、Ruby等等到最后都会变成上面的结果,当然还有Coffeescript之类的语言都是以此为基础,这才是我们需要的知识。</p>
- <h2 id="hellogeek"><span class="header-section-number">5.1</span> Hello,Geek</h2>
- <p>有了一些些基础之后,我们终于能试着去写一些程序了。也是时候去创建一个像样的东西,或许你在一些界面设计方面的书籍看过类似的东西,可能我写得也没有那些内容好,只是这些都是一些过程。过去我们都是一点点慢慢过来的,只是现在我们也是如此,技术上的一些东西,事实上大家都是知道的。就好比我们都觉得我们可以开个超市,但是如果让我们去开超市的话,我们并不一定能赚钱。</p>
- <p>学习编程的目的可能不在于我们能找到一份工作,那只是在编程之外的东西,虽然确实也是很确定的。但是除此之处,有些东西也是很重要的。</p>
- <p>过去总是不理解为什么会一些人会不厌其烦地去回答别人的问题,有时候可能会想是一种能力越大责任越大的感觉,但是有时候在写一些博客或者回答别人的问题的时候我们又重新思考了这些问题,又重新学习了这些技能。所以这里可能说的不是关于编程的东西而是一些编程以外的东西,关于学习或者学习以外的东西。</p>
- <h2 id="从源码学习"><span class="header-section-number">5.2</span> 从源码学习</h2>
- <p>过去总觉得学了一种语言的语法便算是学会了一种语言,直到有一天接触运用该语言的项目的时候,虽然也会写上几行代码,但是却不像这种语言的风格。于是这也是这一篇的意义所在了:</p>
- <h2 id="浏览器渲染过程"><span class="header-section-number">5.3</span> 浏览器渲染过程</h2>
- <p>基本的渲染引擎的过程如下图所示:</p>
- <div class="figure">
- <img src="./images/flow.png" alt="flow" />
- <p>flow</p>
- </div>
- <ul>
- <li>解析HTML去构建DOM树</li>
- <li>渲染树形结构</li>
- <li>生成渲染的树形图布局</li>
- <li>绘制树形图</li>
- </ul>
- <p>对于Webkit浏览器来说,他的过程如下所示:</p>
- <div class="figure">
- <img src="./images/webkitflow.png" alt="webkitflow" />
- <p>webkitflow</p>
- </div>
- <h3 id="html"><span class="header-section-number">5.3.1</span> HTML</h3>
- <p>写好HTML的一个要点在于读别人写的代码,这只是一方面,我们所说的HTML方面的内容可能不够多,原因有很多,很多东西都需要在实战中去解决。读万卷书和行万里路,分不清哪个有重要的意义,但是如果可以同时做好两个的话,成长会更快的。</p>
- <p>写好HTML应该会有下面的要点</p>
- <ul>
- <li>了解标准及遵守绝大多数标准</li>
- <li>注重可读性,从ID及CLASS的命名</li>
- <li>关注SEO与代码的联系</li>
- </ul>
- <p>或许在这方面我也算不上很了解,不过按笔者的经验来说,大致就是如此。</p>
- <p>多数情况下我们的HTML是类似于下面这样子的</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><div</span><span class="ot"> class=</span><span class="st">"col-md-3 right"</span><span class="kw">></span>
- {% nevercache %}
- {% include "includes/user_panel.html" %}
- {% endnevercache %}
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"panel panel-default"</span><span class="kw">></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"panel-body"</span><span class="kw">></span>
- {% block right_panel %}
- {% ifinstalled mezzanine.twitter %}
- {% include "twitter/tweets.html" %}
- {% endifinstalled %}
- {% endblock %}
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"></div></span></code></pre>
- <p>换句话说HTML只是基础,而不是日常用到的。我们的HTML是由template生成的,我们可以借助于mustache.js又或者是angluarjs之类的js库来生成最后的HTML,所以这里只是一个开始。</p>
- <p>还需要了解的一部分就是HTML的另外一个重要的部分,DOM树形结构</p>
- <h2 id="dom树形结构图"><span class="header-section-number">5.4</span> DOM树形结构图</h2>
- <blockquote>
- <p>DOM是文档对象化模型(Document Object Model)的简称。DOM Tree是指通过DOM将HTML页面进行解析,并生成的HTML tree树状结构和对应访问方法。</p>
- </blockquote>
- <div class="figure">
- <img src="./images/dom_tree.jpg" alt="DOM Tree" />
- <p>DOM Tree</p>
- </div>
- <h3 id="javascript"><span class="header-section-number">5.4.1</span> javascript</h3>
- <p>这里以未压缩的jQuery源码和zepto.js作一个小小的比较,zepto.js是兼容jQuery的,因此我们举几个有意思的函数作一简单的比较,关于源码可以在官网上下载到。</p>
- <p>在zepto.js下面判断一个值是否是函数的方面如下,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">isFunction</span>(value) { <span class="kw">return</span> <span class="fu">type</span>(value) == <span class="st">"function"</span> }</code></pre>
- <p>而在jQuery下面则是这样的</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">isFunction: <span class="kw">function</span>( obj ) {
- <span class="kw">return</span> <span class="ot">jQuery</span>.<span class="fu">type</span>(obj) === <span class="st">"function"</span>;
- }</code></pre>
- <p>而他们的用法是一样的,都是</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">$</span>.<span class="fu">isFunction</span>();</code></pre>
- <p>jQuery的作法是将诸如isFunction,isArray这些函数打包到jQuery.extend中,而zepto.js的也是这样的,只不过多了一行</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">$</span>.<span class="fu">isFunction</span> = isFunction</code></pre>
- <p>遗憾的是我也没去了解过为什么,之前我也没有看过这些库的代码,所以这个问题就要交给读者去解决了。jQuery里面提供了函数式编程接口,不过jQuery更多的是构建于CSS选择器之上,对于DOM的操作比javascript自身提供的功能强大得多。如果我们的目的在于更好的编程,那么可能需要诸如Underscore.js之类的库。或许说打包自己常用的函数功能为一个库,诸如jQuery</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">isFunction</span>(value) { <span class="kw">return</span> <span class="fu">type</span>(value) == <span class="st">"function"</span> }
- <span class="kw">function</span> <span class="fu">isWindow</span>(obj) { <span class="kw">return</span> obj != <span class="kw">null</span> && obj == <span class="ot">obj</span>.<span class="fu">window</span> }
- <span class="kw">function</span> <span class="fu">isDocument</span>(obj) { <span class="kw">return</span> obj != <span class="kw">null</span> && <span class="ot">obj</span>.<span class="fu">nodeType</span> == <span class="ot">obj</span>.<span class="fu">DOCUMENT_NODE</span> }
- <span class="kw">function</span> <span class="fu">isObject</span>(obj) { <span class="kw">return</span> <span class="fu">type</span>(obj) == <span class="st">"object"</span> }</code></pre>
- <p>我们需要去了解一些故事背后的原因,越来越害怕GUI的原因之一,在于不知道背后发生了什么,即使是开源的,我们也无法了解真正的背后发生什么了。对于不是这个工具、软件的用户来说,开源更多的意义可能在于我们可以添加新的功能,当然还有免费。如果没有所谓的危机感,以及认为自己一直在学习工具的话,可以试着去打包自己的函数,打包自己的库。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> calc={
- <span class="dt">add</span>: <span class="kw">function</span>(a,b){
- <span class="kw">return</span> a+b;
- },
- <span class="dt">sub</span>: <span class="kw">function</span>(a,b){
- <span class="kw">return</span> a-b;
- },
- <span class="dt">dif</span>: <span class="kw">function</span>(a,b){
- <span class="kw">if</span>(a>b){
- <span class="kw">return</span> a;
- }<span class="kw">else</span>{
- <span class="kw">return</span> b;
- }
- }
- }</code></pre>
- <p>然后用诸如jslint测试一下代码。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">./jsl</span> -conf jsl.default.conf
- <span class="kw">JavaScript</span> Lint 0.3.0 (JavaScript-C 1.5 2004-09-24)
- <span class="kw">Developed</span> by Matthias Miller (http://www.JavaScriptLint.com)
- <span class="kw">app.js</span>
- <span class="kw">/Users/fdhuang/beageek/chapter4/src</span>/<span class="kw">app.js</span>(15)<span class="kw">:</span> lint warning: missing semicolon
- }
- <span class="kw">........</span>^
- <span class="kw">0</span> error(s), <span class="kw">1</span> warning(s)</code></pre>
- <p>于是我们需要在第15行添加一个分号。</p>
- <p>最好的方法还是阅读别人的代码,而所谓的别人指的是一些相对较大的网站的,有比较完善的开发流程,代码质量也不会太差。而所谓的复杂的代码都是一步步构建上去的,罗马不是一天建成的。</p>
- <p>有意思的是多数情况下,我们可能会用原型去开发我们的应用,而这也是我们需要去了解和掌握的地方,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">Calc</span>(){
- }
- <span class="ot">Calc</span>.<span class="ot">prototype</span>.<span class="fu">add</span>=<span class="kw">function</span>(a,b){
- <span class="kw">return</span> a+b;
- };
- <span class="ot">Calc</span>.<span class="ot">prototype</span>.<span class="fu">sub</span>=<span class="kw">function</span>(a,b){
- <span class="kw">return</span> a-b;
- };</code></pre>
- <p>我们似乎在这里展示了更多的Javascript的用法,但是这不是一好的关于Javascript的介绍,有一天我们还要用诸如qunit之类的工具去为我们的function写测试,这时就是一个更好的开始。</p>
- <p>如果我们乐意的话,我们也可以构建一个类似于jQuery的框架,以用来学习。</p>
- <p>作为一门编程语言来说,我们学得很普通,在某种意义上来说算不上是一种入门。但是如果我们可以在其他的好书在看到的内容,就没有必要在这里进行复述,目的在于一种学习习惯的养成。</p>
- <h3 id="css-1"><span class="header-section-number">5.4.2</span> CSS</h3>
- <p>CSS有时候很有趣,但是有时候有很多我们没有意识到的用法,这里以Bootstrap为例,这是一个不错的CSS库。最令人兴奋的是没有闭源的CSS,没有闭源的JS,这也就是前端好学习的地方所在了,不过这是一个开源的CSS库,虽然是这样叫的,但是称之为CSS库显然不合适。</p>
- <pre class="sourceCode css"><code class="sourceCode css">a,
- a<span class="dv">:visited</span> <span class="kw">{</span>
- <span class="kw">text-decoration:</span> <span class="dt">underline</span><span class="kw">;</span>
- <span class="kw">}</span>
- a<span class="ch">[href]</span><span class="dv">:after</span> <span class="kw">{</span>
- <span class="kw">content:</span> <span class="st">" ("</span> <span class="dt">attr(</span>href<span class="dt">)</span> <span class="st">")"</span><span class="kw">;</span>
- <span class="kw">}</span>
- abbr<span class="ch">[title]</span><span class="dv">:after</span> <span class="kw">{</span>
- <span class="kw">content:</span> <span class="st">" ("</span> <span class="dt">attr(</span>title<span class="dt">)</span> <span class="st">")"</span><span class="kw">;</span>
- <span class="kw">}</span>
- a<span class="ch">[href^=</span><span class="st">"javascript:"</span><span class="ch">]</span><span class="dv">:after</span>,
- a<span class="ch">[href^=</span><span class="st">"#"</span><span class="ch">]</span><span class="dv">:after</span> <span class="kw">{</span>
- <span class="kw">content:</span> <span class="st">""</span><span class="kw">;</span>
- <span class="kw">}</span></code></pre>
- <p>这里有一些有趣的,值得一讲的CSS用法。</p>
- <ul>
- <li>伪类选择器,如a:visited这样需要其他条件来对元素应用样式,用于已访问的链接。</li>
- <li>属性选择器,如a[href]这样当a元素存在href这样的属性的时候来寻找应用元素。</li>
- </ul>
- <p>其他的还需要去好好了解的就是<strong>CSS的盒模型</strong>,作为CSS的基石之一。</p>
- <h2 id="css盒模型图"><span class="header-section-number">5.5</span> CSS盒模型图</h2>
- <p>(ps:以下内容来自于Mozilla Developer NetWorks)</p>
- <p>CSS下这些矩形盒子由标准盒模型描述。这个模型描述元素内容占用空间。盒子有四个边界:外边距边界margin edge, 边框边界border edge, 内边距边界padding edge 与 内容边界content edge。</p>
- <div class="figure">
- <img src="./images/box-model.gif" alt="CSS Box Model" />
- <p>CSS Box Model</p>
- </div>
- <p>内容区域content area 是真正包含元素内容的区域。位于内容边界的内部,它的大小为内容宽度 或 content-box宽及内容高度或content-box高。</p>
- <p>如果 box-sizing 为默认值, width, min-width, max-width, height, min-height 与 max-height 控制内容大小。</p>
- <p>内边距区域padding area 用内容及可能的边框之间的空白区域扩展内容区域。它位于内边距边界内部,通常有背景——颜色或图片(不透明图片盖住背景颜色). 它的大小为 padding-box 宽与 padding-box 高。</p>
- <p>内边距与内容边界之间的空间可以由 padding-top, padding-right, padding-bottom, padding-left 和简写属性 padding 控制。</p>
- <p>边框区域border area 是包含边框的区域,扩展了内边距区域。它位于边框边界内部,大小为 border-box 宽和 border-box 高。由 border-width 及简写属性 border控制。</p>
- <p>外边距区域margin area用空白区域扩展边框区域,以分开相邻的元素。它的大小为 margin-box 的高宽。</p>
- <p>外边距区域大小由 margin-top, margin-right, margin-bottom, margin-left 及简写属性 margin 控制。</p>
- <p>在 外边距合并 的情况下,由于盒之间共享外边距,外边距不容易弄清楚。</p>
- <p>最后注意,对于行内非替换元素,其占用空间(行高)由 line-height 决定,即使有内边距与边框。</p>
- <p>诸如</p>
- <pre class="sourceCode css"><code class="sourceCode css">* <span class="kw">{</span>
- <span class="kw">margin:</span> <span class="dt">0px</span><span class="kw">;</span>
- <span class="kw">padding:</span> <span class="dt">0px</span><span class="kw">;</span>
- <span class="kw">font-family:</span> Helvetica<span class="kw">;</span>
- <span class="kw">}</span></code></pre>
- <p>这样的通用器用来进行全局选择的工具和我们用于抵消某个body对于子选择器的影响一样值得注意得多。</p>
- <h2 id="笔记"><span class="header-section-number">5.6</span> 笔记</h2>
- <p>写博客似乎是一个不错的好习惯,作为一个不是很优秀的写手。对于来说,有时候发现原来能教会别人对于自己的能力来说算是一种肯定。有些时候教会别人才算是自己学会的表现,总会在项目上的时候需要自己去复述工作的一个过程,我们需要整理好我们的思路才能带给别人更多的收获。我们的笔记上总会留下自己的学习的一些过程,有些时候我们想要的只是一点点的鼓励,有时是诸如评论一类,有时可能是诸如访问量。更多的可能是我们可以重新整理自己的知识,好好复习一下,以便于好好记住,写出来是一个好的过程。</p>
- <p>无处不在的三剑客就这样到了这里,写得似乎很多也很少,但是还是没有做出来一个东西,于是我们朝着这样一个方向前进。</p>
- <h1 id="gnulinux-强大且free"><span class="header-section-number">6</span> GNU/Linux 强大且Free</h1>
- <div class="figure">
- <img src="./images/gnu_linux.png" alt="GNU/Linux" />
- <p>GNU/Linux</p>
- </div>
- <h2 id="什么是linux"><span class="header-section-number">6.1</span> 什么是Linux</h2>
- <p>Linux是一种自由和开放源码的类UNIX操作系统内核。目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机、平板电脑、路由器和影音游戏控制台,到桌上型电脑,大型电脑和超级电脑。 Linux是一个领先的操作系统内核,世界上运算最快的10台超级电脑运行的都是基于Linux内核的操作系统。</p>
- <p>Linux操作系统也是自由软件和开放源代码发展中最著名的例子。只要遵循GNU通用公共许可证,任何人和机构都可以自由地使用Linux的所有底层源代码,也可以自由地修改和再发布。<strong>严格来讲,Linux这个词本身只表示Linux内核,但在实际上人们已经习惯了用Linux来形容整个基于Linux内核,并且使用GNU工程各种工具和数据库的操作系统(也被称为GNU/Linux)。</strong>通常情况下,Linux被打包成供桌上型电脑和服务器使用的Linux发行版本。一些流行的主流Linux发行版本,包括Debian(及其衍生版本Ubuntu),Fedora和openSUSE等。Linux得名于电脑业余爱好者Linus Torvalds。</p>
- <p>而不是如百度百科所讲的<del>Linux操作系统是UNIX操作系统的一种克隆系统。它诞生于1991年的 Linux桌面[1]10 月5日(这是第一次正式向外公布的时间)。以后借助于Internet网络,并通过全世界各地计算机爱好者的共同努力,已成为今天世界上使用最多的一种UNIX类操作系统,并且使用人数还在迅猛增长。</del></p>
- <p>Linux只是个内核,而不是操作系统,所以在这我们再理解一下操作系统是由什么组成的。</p>
- <h2 id="操作系统"><span class="header-section-number">6.2</span> 操作系统</h2>
- <p>操作系统(英语:Operating System,简称OS)是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石。操作系统需要处理如管理与配置内存、决定系统资源供需的优先次序、控制输入与输出设备、操作网络与管理文件系统等基本事务。操作系统也提供一个让用户与系统交互的操作界面。 操作系统的型态非常多样,不同机器安装的操作系统可从简单到复杂,可从手机的嵌入式系统到超级计算机的大型操作系统。许多操作系统制造者对它涵盖范畴的定义也不尽一致,例如有些操作系统集成了图形用户界面(GUI),而有些仅使用命令行界面(CLI),而将GUI视为一种非必要的应用程序。</p>
- <p>操作系统位于底层硬件与用户之间,是两者沟通的桥梁。用户可以通过操作系统的用户界面,输入命令。操作系统则对命令进行解释,驱动硬件设备,实现用户要求。以现代标准而言,一个标准PC的操作系统应该提供以下的功能:</p>
- <ul>
- <li>进程管理(Processing management)</li>
- <li>内存管理(Memory management)</li>
- <li>文件系统(File system)</li>
- <li>网络通信(Networking)</li>
- <li>安全机制(Security)</li>
- <li>用户界面(User interface)</li>
- <li>驱动程序(Device drivers)</li>
- </ul>
- <p>而让我们来看一下两者之间的不同之处,这是一张linux的架构图我们可以发现内核只是位于底层。</p>
- <h3 id="linux架构图"><span class="header-section-number">6.2.1</span> Linux架构图</h3>
- <div class="figure">
- <img src="./images/linux_kernel.jpg" alt="Linux Kernel" />
- <p>Linux Kernel</p>
- </div>
- <h4 id="用户模式"><span class="header-section-number">6.2.1.1</span> 用户模式</h4>
- <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>
- <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>
- <p>C库(open、fopen、socket、exec、calloc等)</p>
- <h4 id="内核模式"><span class="header-section-number">6.2.1.2</span> 内核模式</h4>
- <ul>
- <li>系统中断、调用、错误等软硬件消息</li>
- <li>内核(驱动程序、进程、网络、内存管理等)</li>
- <li>硬件(处理器、内存、各种设备)</li>
- </ul>
- <p>我们可以发现,由linux内核+shell可以构成一个操作系统,而linux本身只是个内核,也就是图中的内核模式,负责控制系统的这些部分。也就是我们可以发现,Linux内核构成了一个操作系统除用户界面以外的部分,而shell就是这最后的用户界面。</p>
- <p>而linux内核以外的部分就是由GNU计划构成的。</p>
- <h3 id="shell"><span class="header-section-number">6.2.2</span> Shell</h3>
- <div class="figure">
- <img src="./images/shell.png" alt="Shell" />
- <p>Shell</p>
- </div>
- <p>Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。</p>
- <p>实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果</p>
- <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>
- <p>shell将会是我们在GNU/linux中经常用到的经常有到的工具之一,用来操作计算机用的。在迁移到linux之前我们可以试用cygwin来进行模拟:</p>
- <p>Cygwin是许多自由软件的集合,最初由Cygnus Solutions开发,用于各种版本的Microsoft Windows上,运行<code>UNIX类</code>系统。</p>
- <h3 id="gcc"><span class="header-section-number">6.2.3</span> GCC</h3>
- <p>GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器。它是一套以GPL及LGPL许可证所发行的自由软件,也是GNU计划的关键部分,亦是自由的类Unix及苹果电脑Mac OS X 操作系统的标准编译器。GCC(特别是其中的C语言编译器)也常被认为是跨平台编译器的事实标准。</p>
- <p>GCC原名为GNU C语言编译器(GNU C Compiler),因为它原本只能处理C语言。GCC很快地扩展,变得可处理C++。之后也变得可处理Fortran、Pascal、Objective-C、Java、Ada,以及Go与其他语言。</p>
- <pre class="sourceCode c"><code class="sourceCode c"><span class="ot">#include <stdio.h></span>
- main()
- {
- printf(<span class="st">"Hello world</span><span class="ch">\n</span><span class="st">"</span>);
- }</code></pre>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">~/temp</span>/<span class="kw">free></span> gcc hello.c -o hello
- <span class="kw">hello.c</span>:2:1: warning: type specifier missing, defaults to <span class="st">'int'</span> [-Wimplicit-int]
- <span class="fu">main()</span>
- ^<span class="kw">~~~</span>
- <span class="kw">1</span> warning generated.
- <span class="kw">~/temp</span>/<span class="kw">free></span> ./hello
- <span class="kw">Hello</span> world</code></pre>
- <p>同shell一样,对于GNU/linux系统而言,GCC的作用也是无可取代的。当然如果只是一般用途的话,GCC对于一般用户可能没用,但是在些GNU/Linux系统上,我们可能就需要自己编译源码成二进制文件,而没有软件包,因而其重要性是不言而喻的。自然的如果我们自己动手编译GNU/Linux操作系统的话,我们会理解其的重要意义。有兴趣的同学可以试一下:Linux From Scratch (LFS)。</p>
- <h3 id="启动引导程序"><span class="header-section-number">6.2.4</span> 启动引导程序</h3>
- <p>最后,当我们构成以上的那些之后,我们就需要一个引导程序,以便使系统启动,引导进内核。</p>
- <p>启动程序(bootloader)于电脑或其他计算机应用上,是指引导操作系统启动的程序。启动程序启动方式与程序视应用机型种类。例如在普通PC上,引导程序通常分为两部分:第一阶段引导程序位于主引导记录,用于引导位于某个分区上的第二阶段引导程序,如NTLDR、GNU GRUB等。</p>
- <p>BIOS 开机完成后,bootloader就接手初始化硬件设备、创建存储器空间的映射,以便为操作系统内核准备好</p>
- <p>正确的软硬件环境。</p>
- <p>简单的bootloader的虚拟汇编码,如其后的八个指令:</p>
- <ul>
- <li>0: 将P暂存器的值设为8</li>
- <li>1: 检查纸带({paper tape)读取器,是否已经可以进行读取</li>
- <li>2: 如果还不能进行读取, 跳至1</li>
- <li>3: 从纸带读取器,读取一byte至累加器</li>
- <li>4: 如为带子结尾,跳至8</li>
- <li>5: 将暂存器的值,存储至P暂存器中的数值所指定的地址</li>
- <li>6: 增加P暂存器的值</li>
- <li>7: 跳至1</li>
- </ul>
- <p>但是随着计算机操作系统越来越复杂,位于MBR的空间已经放不下引导操作系统的代码,于是就有了第二阶段的引导程序,而MBR中代码的功能也从直接引导操作系统变成了引导第二阶段的引导程序。</p>
- <p>通常在一个GNU/Linux系统中选用GNUGRUB做为引导程序,例如Ubuntu就是用GRUB2。</p>
- <p>GNU GRUB(简称“GRUB”)是一个来自GNU项目的启动引导程序。GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统。GRUB可用于选择操作系统分区上的不同内核,也可用于向这些内核传递启动参数。</p>
- <p>GNU GRUB的前身为Grand Unified Bootloader。它主要用于类Unix系统;同大多Linux发行版一样,GNU系统也采用GNU GRUB作为它的启动器。Solaris从10 1/06版开始在x86系统上也采用GNU GRUB作为启动器。</p>
- <p>以上也就构成了一个简单的操作系统。</p>
- <h2 id="从编译开始"><span class="header-section-number">6.3</span> 从编译开始</h2>
- <p>我们以一次编译开始我们的Linux学习之旅。</p>
- <h3 id="开始之前"><span class="header-section-number">6.3.1</span> 开始之前</h3>
- <ul>
- <li>如果你没有用过GNU/Linux,我想你需要在虚拟机上安装一个。</li>
- <li>一个主流的GNU/Linux发行版,如Ubuntu,CentOS,Debian,Mint,OpenSUSE,Fedora等等。</li>
- <li>学会如何打开shell(ps:bash,zsh,sh等等)。</li>
- </ul>
- <p>或者你也可以在Windows上安装Cygwin。</p>
- <h3 id="编译nginx"><span class="header-section-number">6.3.2</span> 编译Nginx</h3>
- <p>1.下载这个软件的源码包</p>
- <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>
- <p>wget是一个用于下载的软件,当然你也可以用软件,只是用wget似乎会比图形界面快哦。</p>
- <p>2.解压软件包</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">tar</span> -vf nginx-1.7.4.tar.gz</code></pre>
- <p>-vf的意思是Extract,也就是解压,而tar则是这个解压软件的名字。看上去似乎比WinRAR来得复制得多,但是你可以计时一下,从下载到解压完,和你用鼠标比哪个比较快。</p>
- <p>3.到nginx目录下</p>
- <p>这里需要分两部进行</p>
- <p>1).列出所有文件</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">ls</span> -al
- <span class="kw">drwxr-xr-x</span> 15 fdhuang staff 510B Sep 2 13:44 nginx-1.7.4
- <span class="kw">-rw-r--r--</span> 1 fdhuang staff 798K Aug 5 21:55 nginx-1.7.4.tar.gz</code></pre>
- <p>2).到nginx-1.7.4目录</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">cd</span> nginx-1.7.4 </code></pre>
- <p>4.配置nginx</p>
- <p>一次简单的配置如下</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./configure</span></code></pre>
- <p>当你熟练以后,你可能和我一样用下面的配置(<code>注意</code>:用下面的代码会出错。)</p>
- <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>
- <p>过程中可能会提示你其中出了多少错误,而这时你便可以很愉快地去用搜索引擎搜索他们。</p>
- <p>5.make</p>
- <p>这里就会用到GCC等等。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">make</span></code></pre>
- <p>6.运行</p>
- <p>如果运行顺利的话,应该可以直接</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">./objs/nginx</span> </code></pre>
- <h3 id="其他-1"><span class="header-section-number">6.3.3</span> 其他</h3>
- <p>1.如果没有wget,make,gcc等命令的时候可以用类似于下面的方法安装,</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> apt-get install gcc,make,wget</code></pre>
- <p>2.正常情况下一个开源项目都会有一个README,会告诉你应该如何去做。</p>
- <h2 id="包管理"><span class="header-section-number">6.4</span> 包管理</h2>
- <p>GNU/Linux最方便的东西莫过于包管理了。</p>
- <p>引自OpenSUSE官网的说明及图片<span id="fnref1"><a href="#fn1"><sup>1</sup></a></span></p>
- <div class="figure">
- <img src="http://designiot.phodal.com/images/pm.png" alt="package management" />
- <p>package management</p>
- </div>
- <ol>
- <li><p>Linux 发行版无非就是一堆软件包 (package) 形式的应用程序加上整体地管理这些应用程序的工具。通常这些 Linux 发行版,包括 openSUSE,都是由成千上万不同的软件包构成的。</p></li>
- <li><p>软件包: 软件包不止是一个文件,内含构成软件的所有文件,包括程序本身、共享库、开发包以及使用说明等。</p></li>
- <li><p>元数据 (metadata) 包含于软件包之中,包含软件正常运行所需要的一些信息。软件包安装之后,其元数据就存储于本地的软件包数据库之中,以用于软件包检索。</p></li>
- <li><p>依赖关系 (dependencies) 是软件包管理的一个重要方面。实际上每个软件包都会涉及到其他的软件包,软件包里程序的运行需要有一个可执行的环境(要求有其他的程序、库等),软件包依赖关系正是用来描述这种关系的。</p></li>
- </ol>
- <p>Linux 下的软件包通常是以下三种格式:</p>
- <ul>
- <li>tgz - tar gzip 文件。这类文件是基本的压缩软件包,可以容纳软件包维护者认为有用的所有的东西。此格式除本身的压缩格式外,并没有有关软件包内容的标准。</li>
- <li>deb - 此格式的软件包常用于 Debian 系统,是标准的 Debian 软件包格式。</li>
- <li>rpm - 此格式由 Red Hat Linux 所创建,并经由 LSB 标准化,现已为众多 Linux 发行版所采用,是一个优秀的软件包格式。openSUSE 即是用此格式。更多信息可以参阅此处。</li>
- </ul>
- <blockquote>
- <p>所以这就需要能自动解决依赖关系的软件包管理器。软件包管理系统就是一个工具集,为系统提供一个统一的安装、升级、删除软件的方式。</p>
- </blockquote>
- <h2 id="ubuntu-lnmp"><span class="header-section-number">6.5</span> Ubuntu LNMP</h2>
- <p>在余下的章节中,我们需要去部署,需要去使用Ubuntu。如果在Windows下可以使用LAMP,但是在这里我们只说Ubuntu。开始之前你需要安装好Ubuntu,无论是在虚拟机,还是在真机安装,或者是Docker。</p>
- <h3 id="update软件包列表"><span class="header-section-number">6.5.1</span> Update软件包列表</h3>
- <p><code>apt-get</code>是debian,ubuntu发行版的包管理工具。<code>apt-get update</code>可以确保我们的软件包列表是最新的,下面是一个简单的更新过程。</p>
- <p>打开Terminal或者Konsole等等之类的终端控制台。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get update
- <span class="kw">Ign</span> http://archive.ubuntu.com trusty InRelease
- <span class="kw">Ign</span> http://archive.ubuntu.com trusty-updates InRelease
- <span class="kw">Ign</span> http://archive.ubuntu.com trusty-security InRelease
- <span class="kw">Ign</span> http://archive.ubuntu.com trusty-proposed InRelease
- <span class="kw">Get</span>:1 http://archive.ubuntu.com trusty Release.gpg [933 B]
- <span class="kw">Get</span>:2 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
- <span class="kw">Get</span>:3 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
- <span class="kw">Get</span>:4 http://archive.ubuntu.com trusty-proposed Release.gpg [933 B]
- <span class="kw">Get</span>:5 http://archive.ubuntu.com trusty Release [58.5 kB]
- <span class="kw">Get</span>:6 http://archive.ubuntu.com trusty-updates Release [62.0 kB]
- <span class="kw">Get</span>:7 http://archive.ubuntu.com trusty-security Release [62.0 kB]
- <span class="kw">Get</span>:8 http://archive.ubuntu.com trusty-proposed Release [209 kB]
- <span class="kw">Get</span>:9 http://archive.ubuntu.com trusty/main Sources [1335 kB]
- <span class="kw">Get</span>:10 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
- <span class="kw">Get</span>:11 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
- <span class="kw">Get</span>:12 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
- <span class="kw">Get</span>:13 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
- <span class="kw">Get</span>:14 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
- <span class="kw">64%</span> [14 Packages 664 kB/7589 kB 9%] 58.3 kB/s 1min 58s</code></pre>
- <p>更新完应该会显示:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Fetched</span> 20.5 MB in 5min 22s (63.6 kB/s)
- <span class="kw">Reading</span> package lists... Done</code></pre>
- <h3 id="安装mysql"><span class="header-section-number">6.5.2</span> 安装MySQL</h3>
- <p>安装命令</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> apt-get install mysql-server php5-mysql</code></pre>
- <p>过程:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get install mysql-server php5-mysql
- <span class="kw">Reading</span> package lists... 0%
- <span class="kw">Reading</span> package lists... Done
- <span class="kw">Building</span> dependency tree
- <span class="kw">Reading</span> state information... Done
- <span class="kw">The</span> following extra packages will be installed:
- <span class="kw">libaio1</span> libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
- <span class="kw">libterm-readkey-perl</span> libwrap0 lsof mysql-client-5.5 mysql-client-core-5.5
- <span class="kw">mysql-common</span> mysql-server-5.5 mysql-server-core-5.5 php5-common php5-json
- <span class="kw">psmisc</span> tcpd
- <span class="kw">Suggested</span> packages:
- <span class="kw">libclone-perl</span> libmldbm-perl libnet-daemon-perl libplrpc-perl
- <span class="kw">libsql-statement-perl</span> libipc-sharedcache-perl tinyca mailx php5-user-cache
- <span class="kw">The</span> following NEW packages will be installed:
- <span class="kw">libaio1</span> libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
- <span class="kw">libterm-readkey-perl</span> libwrap0 lsof mysql-client-5.5 mysql-client-core-5.5
- <span class="kw">mysql-common</span> mysql-server mysql-server-5.5 mysql-server-core-5.5 php5-common
- <span class="kw">php5-json</span> php5-mysql psmisc tcpd
- <span class="kw">0</span> upgraded, 19 newly installed, 0 to remove and 12 not upgraded.
- <span class="kw">Need</span> to get 9982 kB of archives.
- <span class="kw">After</span> this operation, 99.1 MB of additional disk space will be used.
- <span class="kw">Get</span>:1 http://archive.ubuntu.com/ubuntu/ trusty/main libaio1 amd64 0.3.109-4 [6364 B]
- <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]
- <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]
- <span class="kw">Get</span>:4 http://archive.ubuntu.com/ubuntu/ trusty/main libwrap0 amd64 7.6.q-25 [46.2 kB]
- <span class="kw">Get</span>:5 http://archive.ubuntu.com/ubuntu/ trusty/main libdbi-perl amd64 1.630-1 [879 kB]
- <span class="kw">Get</span>:6 http://archive.ubuntu.com/ubuntu/ trusty/main libdbd-mysql-perl amd64 4.025-1 [99.3 kB]
- <span class="kw">Get</span>:7 http://archive.ubuntu.com/ubuntu/ trusty/main libterm-readkey-perl amd64 2.31-1 [27.4 kB]
- <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]
- <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]
- <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]
- <span class="kw">47%</span> [10 mysql-server-core-5.5 850 kB/3215 kB 26%] 79.9 kB/s 1min 6s</code></pre>
- <p>在安装的过程中会要求你输入数据库密码。(默认为空)</p>
- <h3 id="安装nginx"><span class="header-section-number">6.5.3</span> 安装Nginx</h3>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">echo</span> <span class="st">"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"</span> <span class="kw">|</span> <span class="kw">sudo</span> tee /etc/apt/sources.list.d/nginx-stable.list
- <span class="kw">sudo</span> apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
- <span class="kw">sudo</span> apt-get update
- <span class="kw">sudo</span> apt-get install nginx</code></pre>
- <p>启动Nginx Server</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> service nginx start</code></pre>
- <h3 id="安装php"><span class="header-section-number">6.5.4</span> 安装PHP</h3>
- <p>sudo apt-get install php5-fpm</p>
- <p>安装过程</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">root@70cdc7a176a5</span>:/# sudo apt-get install php5-fpm
- <span class="kw">Reading</span> package lists... Done
- <span class="kw">Building</span> dependency tree
- <span class="kw">Reading</span> state information... Done
- <span class="kw">The</span> following extra packages will be installed:
- <span class="kw">libsystemd-daemon0</span>
- <span class="kw">Suggested</span> packages:
- <span class="kw">php-pear</span>
- <span class="kw">The</span> following NEW packages will be installed:
- <span class="kw">libsystemd-daemon0</span> php5-fpm
- <span class="kw">0</span> upgraded, 2 newly installed, 0 to remove and 12 not upgraded.
- <span class="kw">Need</span> to get 2201 kB of archives.
- <span class="kw">After</span> this operation, 9326 kB of additional disk space will be used.
- <span class="kw">Do</span> you want to continue? [Y/n] y
- <span class="kw">Get</span>:1 http://archive.ubuntu.com/ubuntu/ trusty-proposed/main libsystemd-daemon0 amd64 204-5ubuntu20.8 [9608 B]
- <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]
- <span class="kw">Fetched</span> 2201 kB in 1min 5s (33.6 kB/s)
- <span class="kw">Selecting</span> previously unselected package libsystemd-daemon0:amd64.
- <span class="kw">(Reading</span> database ... 13105 files and directories currently installed.<span class="kw">)</span>
- <span class="kw">Preparing</span> to unpack .../libsystemd-daemon0_204-5ubuntu20.8_amd64.deb ...
- <span class="kw">Unpacking</span> libsystemd-daemon0:amd64 (204-5ubuntu20.8) <span class="kw">...</span>
- <span class="kw">Selecting</span> previously unselected package php5-fpm.
- <span class="kw">Preparing</span> to unpack .../php5-fpm_5.5.9+dfsg-1ubuntu4.5_amd64.deb ...
- <span class="kw">Unpacking</span> php5-fpm (5.5.9+dfsg-1ubuntu4.5) <span class="kw">...</span>
- <span class="kw">Processing</span> triggers for ureadahead (0.100.0-16) <span class="kw">...</span>
- <span class="kw">Setting</span> up libsystemd-daemon0:amd64 (204-5ubuntu20.8) <span class="kw">...</span>
- <span class="kw">Setting</span> up php5-fpm (5.5.9+dfsg-1ubuntu4.5) <span class="kw">...</span>
- <span class="kw">Creating</span> config file /etc/php5/fpm/php.ini with new version
- <span class="kw">php5_invoke</span>: Enable module pdo for fpm SAPI
- <span class="kw">php5_invoke</span>: Enable module pdo_mysql for fpm SAPI
- <span class="kw">php5_invoke</span>: Enable module opcache for fpm SAPI
- <span class="kw">php5_invoke</span>: Enable module json for fpm SAPI
- <span class="kw">php5_invoke</span>: Enable module mysql for fpm SAPI
- <span class="kw">php5_invoke</span>: Enable module mysqli for fpm SAPI
- <span class="kw">invoke-rc.d</span>: policy-rc.d denied execution of start.
- <span class="kw">Processing</span> triggers for libc-bin (2.19-0ubuntu6.3) <span class="kw">...</span>
- <span class="kw">Processing</span> triggers for ureadahead (0.100.0-16) <span class="kw">...</span></code></pre>
- <h1 id="arduino-极客的玩具"><span class="header-section-number">7</span> Arduino 极客的玩具</h1>
- <h2 id="极客的玩具"><span class="header-section-number">7.1</span> 极客的玩具</h2>
- <p>Arduino,是一个开放源代码的单芯片微电脑,它使用了Atmel AVR单片机,采用了基于开放源代码的软硬件平台,构建于开放源代码 simple I/O 接口板,并且具有使用类似Java,C 语言的Processing/Wiring开发环境。</p>
- <p>Arduino开发板封装了常用的库到开发环境中,可以让用户在开发产品时,将主要注意力放置于所需要实现的功能上,而不是开发的过程中。在为Arduino写串口程序时,我们只需要用Serial.begin(9600)以9600的速率初始化串口,而在往串口发送数据时,可以用Serial.write(‘1’)的方式向串口发送字串’1’。</p>
- <p>Arduino的出现很大程度上降低了电子制作的难度,初学者甚至不懂编程也可以上手Arduino,这也是它的魅力所在。</p>
- <h2 id="硬件熟悉"><span class="header-section-number">7.2</span> 硬件熟悉</h2>
- <p>为了满足各种需求,Arduino团队设计了很多款开发板,如UNO、Pro mini、Mega2560、Due、Leonardo、Yún、Pro、Fio、Nano等十几种 开发板和扩展板。最适合初学者的一款是Arduino UNO 。下图是Arduino UNO 的外观图:</p>
- <div class="figure">
- <img src="http://designiot.phodal.com/images/uno.png" alt="UNO" />
- <p>UNO</p>
- </div>
- <p>注:后面的程序也是基于Arduino UNO开发板来讲解。</p>
- <h2 id="开发环境"><span class="header-section-number">7.3</span> 开发环境</h2>
- <div class="figure">
- <img src="http://designiot.phodal.com/images/arduino.png" alt="Arduino" />
- <p>Arduino</p>
- </div>
- <p>开发环境如上图,十分简洁,编写代码需要知道两个基本的函数:</p>
- <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">void</span> setup(){
- }
- <span class="dt">void</span> loop(){
- }</code></pre>
- <p><code>setup()</code>函数用于初始化(如GPIO初始化,串口初始化,定时器初始化等)特点是只执行一次;<code>loop()</code>函数是一个死循环,可以看做C语言的<code>while(1)</code>函数。</p>
- <h2 id="点亮一个led"><span class="header-section-number">7.4</span> 点亮一个LED</h2>
- <p>对初学者来说,点亮led已成为入门必修课,使用Arduino控制led十分简单,并且很容易理解。 使用到的函数:</p>
- <ul>
- <li>pinMode(pin,mode)</li>
- <li>digitalWrite(pin,value)</li>
- </ul>
- <p>上一段代码分析:</p>
- <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">int</span> led=<span class="dv">13</span>;
- <span class="dt">void</span> setup()
- {
- pinMode(led,OUTPUT);
- }
- <span class="dt">void</span> loop()
- {
- digitalWrite(led,HIGH);
- delay(<span class="dv">1000</span>);
- digitalWrite(led,LOW);
- delay(<span class="dv">1000</span>);
- }</code></pre>
- <p>该程序实现Arduino单片机13号引脚以1S时间电平翻转,如果外接一个led,就可以看到led以1S的间隔闪烁;函数<code>pinMode()</code>有两个参数pin、value,pin参数用来指定引脚号,本程序中设置为13号引脚,mode用于设置引脚模式,有三个值:</p>
- <ul>
- <li><code>INPUT</code></li>
- <li><code>OUTPUT</code></li>
- <li><code>INPUT_PULLUP</code></li>
- </ul>
- <p>表示让某一个IO引脚作输入,反之,</p>
- <ul>
- <li><code>OUTPUT</code>则使一个IO引脚做输出</li>
- <li><code>INPUT_PULLUP</code>则配置一个IO引脚具有上拉输入功能(上拉电阻的目的是为了保证在无信号输入时输入端的电平为高电平),从英文意思也能很直观的看出来。</li>
- </ul>
- <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>
- <p>如果你仔细查看我的描述,你会发现我没有讲13号引脚怎么来的,是这样的:Arduino团队为了简化对引脚描述,对每个引脚都进行了编号,以UNO开发板为例,可以发现开发板排座的附近有对应的白颜色的数字,那便是所有的引脚编号,A0~A5是6路ADC输入引脚,0-13表示13路基本IO,数字前面的<code>~</code>表示该引脚具有PWM功能。如果要使用某一引脚,只需要知道引脚编号就可编写相应代码进行操作。</p>
- <p>例如<code>digitalWrite(2,LOW)</code>表示向2号引脚输出低电平。其他操作类似,是不是so easy <sup>-</sup> !</p>
- <h2 id="串口通信"><span class="header-section-number">7.5</span> 串口通信</h2>
- <p>使用到的基本函数:</p>
- <ul>
- <li>Serial.begin()</li>
- <li>Serial.write()</li>
- <li>Serial.read()</li>
- <li>Serial.available()</li>
- </ul>
- <p>在此项目中需要使用串口,Arduino串口初始化使用<code>Serial.begin(9600);</code>,其传输波特率为9600,其他波特率也行,函数位于<code>setup()</code>中,之后可以使用<code>Serial.read()</code>、<code>Serial.write()</code>读入一个字符,输出一个字符,使用<code>Serial.print()</code>输出字符串.代码如下:</p>
- <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="dt">char</span> ch=<span class="st">'1'</span>;
- <span class="dt">void</span> setup()
- {
- Serial.begin(<span class="dv">9600</span>);
- }
- <span class="dt">void</span> loop()
- {
- Serial.write(ch);
- <span class="kw">while</span>(<span class="dv">1</span>)
- {
- <span class="kw">if</span>(Serial.available())
- {
- ch = Serial.read();
- Serial.print(ch);
- }
- }
- }</code></pre>
- <p>以上程序实现字符的输出(Serial.write(),Serial.print())和读入(Serial.read())。如果需要了解更多,可以参考:<a href="www.arduino.cc">Arduino官网</a></p>
- <h3 id="关于arduino-setup"><span class="header-section-number">7.5.1</span> 关于Arduino Setup()</h3>
- <p>如果你对Arduino的Setup很疑惑的话,可以看看这里。下面Arduino源码目录中的main函数:</p>
- <pre class="sourceCode cpp"><code class="sourceCode cpp"><span class="ot">#include <Arduino.h></span>
- <span class="dt">int</span> main(<span class="dt">void</span>)
- {
- init();
- setup();
- <span class="kw">for</span> (;;) {
- loop();
- <span class="kw">if</span> (serialEventRun) serialEventRun();
- }
- <span class="kw">return</span> <span class="dv">0</span>;
- }</code></pre>
- <div class="figure">
- <img src="./images/hwcnt.png" alt="hwcnt" />
- <p>hwcnt</p>
- </div>
- <h1 id="python-代码如散文"><span class="header-section-number">8</span> Python 代码如散文</h1>
- <div class="figure">
- <img src="./images/python.png" alt="python" />
- <p>python</p>
- </div>
- <p>作为一门计算机语言来说,Python会有下面的特点。</p>
- <ul>
- <li>语言学习起来容易</li>
- <li>解决生活中的实际问题</li>
- <li>支持多学科</li>
- </ul>
- <p>我们可以和其他不是脚本语言的语言进行一个简单的对比,如C,你需要去编译去运行,有时候还需要解决跨平台问题,本来你是在你的Windows上运行得好好的,但是有一天你换了一个Mac电脑的时候,问题变得很棘手,你甚至不知道怎么去解决问题。我没有用过MFC,听说很方便,但是在其他平台下就没有一个好的解决方案。这里可能跑得有点远,但是不同的用户可能在不同的平台上,这也就是脚本语言的优势所在了。</p>
- <h2 id="代码与散文"><span class="header-section-number">8.1</span> 代码与散文</h2>
- <p>你可能听过,也可能了解过,不过在这里我们可能不会去讲述那些基础的语法的东西,我们想说的是代码格式的重要性,在html中你可以这样去写你的代码</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><html><head><title></span>This is a Title
- <span class="kw"></title></head><body><div</span><span class="ot"> class=</span><span class="st">"content"</span><span class="kw">></span>
- <span class="kw"><p></span>flakjfaklfjalfa<span class="kw"></p></div></span>
- <span class="kw"></body></html></span></code></pre>
- <p>又或者是js的minify,它可能会使你的代码看起来像是这样的:</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">NolTracker</span>(b,a){<span class="kw">this</span>.<span class="fu">pvar</span>=b;<span class="kw">this</span>.<span class="fu">mergeFeatures</span>(a)}</code></pre>
- <p>可能的是如果是python的话,你可能会遇到下面的问题。。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">File</span> <span class="st">"steps.py"</span>, line 10
- <span class="kw">try</span>:
- ^
- <span class="kw">IndentationError</span>: expected an indented block</code></pre>
- <p>如果你对JSLint、Lint这类的工具有点印象的话,你也可以认为python集成了这类工具。整洁的代码至少应该看上去要有整洁的衣服,就好像是我们看到的一个人一样,而后我们才会有一个好的印象。更主要的一点是代码是写给人看的,而衣服更多的时候对于像我这样的人来说,他首先应该是要保暖的,其次对于一个懒的人来说。。。</p>
- <blockquote>
- 程序应该是具有可读性的短文,它将在计算机上执行,从而解决某些问题
- </blockquote>
- <p>我们需要去读懂别人的代码,别人也需要去读懂我们的代码。计算机可以无条件地执行你那未经编排过的程序,但是人就不是如此了。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"> <span class="kw">var</span> calc={<span class="dt">add</span>: <span class="kw">function</span>(a,b){<span class="kw">return</span> a+b;},<span class="dt">sub</span>: <span class="kw">function</span>(a,b){<span class="kw">return</span> a-b;},<span class="dt">dif</span>: <span class="kw">function</span>(a,b){<span class="kw">if</span>(a>b){<span class="kw">return</span> a;}<span class="kw">else</span>{<span class="kw">return</span> b;}}}</code></pre>
- <p>上面的代码相对于下面的代码可读性没有那么多,但是计算机可以无条件地执行上面的代码。上面的代码对于网络传输来说是好的,但是对于人来说并不是如此,我们需要一些工具来辅助我们去读懂上面的代码。如果代码上写得没有一点可读性,诸如函数命名没有多少实际意义,如果我们把前面的函数就成这样:</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> c={
- <span class="dt">a</span>: <span class="kw">function</span>(a,b){
- <span class="kw">return</span> a+b;
- },
- <span class="dt">s</span>: <span class="kw">function</span>(a,b){
- <span class="kw">return</span> a-b;
- },
- <span class="dt">d</span>: <span class="kw">function</span>(a,b){
- <span class="kw">if</span>(a>b){
- <span class="kw">return</span> a;
- }<span class="kw">else</span>{
- <span class="kw">return</span> b;
- }
- }
- }</code></pre>
- <p>那么只有在我们理解了这个函数是干什么之后才能理解函数是干什么,而不是光看函数名就可以了。</p>
- <p>在Javascript解决一个函数的办法有很多,在其他一些语言如Ruby或者Perl中也是如此,解决问题的办法有很多,对于写代码的人来说是一个享受的过程,但是对于维护的人来说并非如此。而这个和Python的思想不是很一致的是,Python设计的理念是</p>
- <blockquote>
- 对于特定的问题,只要有一种最好的方法来解决就够了
- </blockquote>
- <p>可读性的代码在今天显得比以前重要的多,以前写程序的时候我们需要去考虑使用汇编或者其他工具来提高程序的效率。</p>
- <pre><code> .global _start
- .text
- _start:
- # write(1, message, 13)
- mov $1, %rax # system call 1 is write
- mov $1, %rdi # file handle 1 is stdout
- mov $message, %rsi # address of string to output
- mov $13, %rdx # number of bytes
- syscall # invoke operating system to do the write
- # exit(0)
- mov $60, %rax # system call 60 is exit
- xor %rdi, %rdi # we want return code 0
- syscall # invoke operating system to exit
- message:
- .ascii "Hello, world\n"</code></pre>
- <p>所以上面的代码的可读性在今天新生一代的程序员来说可能没有那么容易理解。芯片运行的速度越来越快,在程序上我们也需要一个越来越快的解决方案,而所谓的越来越快的解决方案指的不是运行速度上,而是开发速度上。如果你没有办法在同样时间内开发出更好的程序,那么你就可能输给你的竞争对手。</p>
- <h3 id="开始之前-1"><span class="header-section-number">8.1.1</span> 开始之前</h3>
- <p>我们终于又从一种语言跳到了另外一种语言,我们可能习惯了一种模式,而不敢于去尝试新的东西,这些或许是我们的一些习惯又或者是因为害怕等等。</p>
- <p>作为另外一个跨平台能力很强的语言,这里说的是与Javascript、HTML比较,或许你会觉得C算是最好的,但是我们这里讨论更多的是脚本语言,也就是直接可以运行的。在现在主流的大多数移动平台上,python也有良好的支持,如Android,IOS,只是这些算是类Unix系统内核,python还支持之前Nokia的Symbian。</p>
- <p>开始之前我们需要确认我们的平台上已经有了python环境,也就是可以运行下面的Hello,World,你可以在网上很多地方看到,最简单的地方还是到官网,又或者是所用移动平台的store下载。</p>
- <h3 id="python的helloworld"><span class="header-section-number">8.1.2</span> Python的Hello,World</h3>
- <p>Python的Hello,World有两种形式,作为一种脚本语言来说,Javascript也是一种脚本语言,只是两者之间有太多的不同之处,每个人都会有不同的选择对于一种语言用来作为其的习惯。于是这就是我们的</p>
- <pre><code>print "Hello,World"</code></pre>
- <p>当我们把我们的脚本在shell环境下运行时</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">>>></span> <span class="kw">print</span> <span class="st">"Hello,world"</span>
- <span class="kw">File</span> <span class="st">"<stdin>"</span>, line 1
- <span class="kw">print</span> <span class="st">"Hello,world"</span>
- ^
- <span class="kw">IndentationError</span>: unexpected indent
- <span class="kw">>>></span> <span class="kw">print</span> <span class="st">"Hello,world"</span>
- <span class="kw">Hello</span>,world
- <span class="kw">>>></span></code></pre>
- <p>如果你没有预料到缩进带来的问题的时候,这就是一个问题了。</p>
- <p>和我们在Javascript或者是CSS里面一样,我们也可以用一个文件的方式来写入我们的代码,文件后缀名是py,所以创建一个helloworld.py,输入上面的代码,然后执行</p>
- <pre><code>python helloworld.py</code></pre>
- <p>一个理想的结果,或许你试过C语言的helloworld,如果了解过GCC的话应该是可以这样的:</p>
- <pre><code>./a.out</code></pre>
- <p>也就是执行编译完后的程序,需要注意的是helloworld.py没有编译,不过也会输出</p>
- <pre><code>Hello,world</code></pre>
- <h3 id="我们想要的helloworld"><span class="header-section-number">8.1.3</span> 我们想要的Hello,World</h3>
- <p>我们想展示的是如何结合前面学习的内容做一个更有意思的Hello,World。</p>
- <pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> cherrypy
- <span class="kw">class</span> HelloWorld(<span class="dt">object</span>):
- <span class="kw">def</span> index(<span class="ot">self</span>):
- <span class="kw">return</span> <span class="st">"Hello World!"</span>
- index.exposed = <span class="ot">True</span>
- cherrypy.quickstart(HelloWorld())</code></pre>
- <h2 id="算法"><span class="header-section-number">8.2</span> 算法</h2>
- <p>我们需要去了解算法(algorithm),引经据典的话就是这样子:</p>
- <blockquote>
- a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer
- </blockquote>
- <p>也就是计算或其他解决问题的操作需要遵循的一个过程或者一套规则,书上还提到的说法是——解决问题的诀窍,让我想起了hack一词。我们总会去想某些东西是否有一个更快的计算方法,有时候在处理某些问题上也显示了一个好的算法的重要性。</p>
- <h2 id="实用主义哲学"><span class="header-section-number">8.3</span> 实用主义哲学</h2>
- <p>(来自于:HyryStudio)</p>
- <p>大多数工程师、科学家使用科学计算软件的目的都是为了快速解决其工作中遇到的问题,而不是开发出一套完整的软件。这就是为什么MATLAB这样的商用科学计算软件十分流行的原因。而Python在这一点上实际上和MATLAB十分相似,我们也可以使用Python众多的扩展库快速写出一次性的数据处理、运算的脚本。然而由于Python语言的一些高级特性,以及众多的科学计算之外的扩展库,我们可以将积累下来的一次性脚本进行改造,为它们提供命令行、GUI、数据库、网络等众多接口,最终成为一套完整的工具包或者实用的计算软件。而且由于是开源的自由软件,我们可以在任何系统中安装Python环境,运行我们 的程序。</p>
- <p>Python一直保持着很强的实用主义,它通常不会去试着重新开发一整套函数库,而是将现有的开源函数库包装成其扩展库。而Python则通过这些扩展库将众多的开源函数库连接在一起,是名符其实的胶水语言。例如由华盛顿大学的教授主导开发的 Sage ,就是一套以代替MATLAB、Mathematica、Maple等商用科学计算软件为目的的开源系统。它通过Python结合了众多的开源科学计算软件,并通过网页浏览器提供了一个与其交互的记事本文档界面。 Python的科学计算扩展库非常多,不同专业的技术人员都可以找到适合自己的扩展库。下面是我经常会用到的一个非常不完全的列表:</p>
- <ul>
- <li>NumPy + SciPy + matplotlib + IPython : 这几个应该是每位开发者都应具备的扩展库。NumPy提供了多维数组以及众多的处理函数,SciPy提供了各种数值运算功能,matplotlib能绘制 出精美的二维图表,IPython则提供了一个超强的命令行,最新版的IPython还添加于Sage类似的浏览器的记事本界面(notebook)。</li>
- <li>SciKits : 其中包括许多独立的扩展库,作为SciPy的补充。其中 scikit-learn 是一套机器学习库,包含了比较完善的文档以及众多的实例程序。</li>
- <li>Pandas : 以Python世界中 R 的替代品为目标的数据分析库。根据其官方网站的测试,Pandas在许多方面的性能都比R要高。</li>
- <li>ETS : 这是一套Enthought公司开发的函数库,其中的 Mayavi 能很方便地对数据进行三维可视化。</li>
- <li>OpenCV : 这是一套计算机视觉库,目前的最新版本已经提供了十分完备的Python接口,能够调用OpenCV中众多的图像处理、模式识别函数直接对NumPy数组进行处理。</li>
- </ul>
- <h2 id="包管理-1"><span class="header-section-number">8.4</span> 包管理</h2>
- <p>关于Python的包管理</p>
- <ul>
- <li>Eggs 格式是 setuptools 引入的一种文件格式,它使用 .egg 扩展名,用于 Python 模块的安装。</li>
- <li>pip 是目前 python 包管理的事实标准,2008年发布。它被用作 easy_install 的替代品,但是它仍有大量的功能建立在 setuptools 组件之上。</li>
- </ul>
- <h3 id="python-requests"><span class="header-section-number">8.4.1</span> python requests</h3>
- <pre><code>Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。
- Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。</code></pre>
- <p>以安装requests为例:</p>
- <p>命令:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> pip install requests</code></pre>
- <p>结果:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Downloading/unpacking</span> requests
- <span class="kw">Downloading</span> requests-2.4.3-py2.py3-none-any.whl (459kB)<span class="kw">:</span> 459kB downloaded
- <span class="kw">Installing</span> collected packages: requests
- <span class="kw">Successfully</span> installed requests
- <span class="kw">Cleaning</span> up...</code></pre>
- <p>用这个库我们可以做些什么?看看官网的示例:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">>>></span> <span class="kw">import</span> requests
- <span class="kw">>>></span> <span class="kw">r</span> = requests.get(<span class="st">'https://github.com/timeline.json'</span>)
- <span class="kw">>>></span> <span class="kw">r.json</span>()</code></pre>
- <p>到现在你会发现我们没有说到任何的Python语法,这不是一本关于Python语法的书,如我们在开头所说的。下面是我们将会在后面用到的代码</p>
- <pre class="sourceCode python"><code class="sourceCode python"><span class="co">#!/usr/bin/env python</span>
- <span class="ch">import</span> requests
- url = <span class="st">"http://b.phodal.com/athome/1"</span>
- r = requests.get(url)
- <span class="dt">print</span> r.text</code></pre>
- <h1 id="raspberry-pi"><span class="header-section-number">9</span> Raspberry Pi</h1>
- <div class="figure">
- <img src="./images/rpi.jpg" alt="Raspberry Pi" />
- <p>Raspberry Pi</p>
- </div>
- <h2 id="geek的盛宴"><span class="header-section-number">9.1</span> Geek的盛宴</h2>
- <p>Raspberry Pi是一款针对电脑业余爱好者、教师、小学生以及小型企业等用户的迷你电脑,预装Linux系统,体积仅信用卡大小,搭载ARM架构处理器,运算性能和智能手机相仿。在接口方面,Raspberry Pi提供了可供键鼠使用的USB接口,此外还有千兆以太网接口、SD卡扩展接口以及1个HDMI高清视频输出接口,可与显示器或者TV相连。</p>
- <p>Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。它支持32位和64位硬件。Linux继承了Unix以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。</p>
- <p>Raspberry Pi相比于一般的ARM开发板来说,由于其本身搭载着Linux操作系统,可以用诸如Python、Ruby或Bash来执行脚本,而不是通过编译程序来运行,具有更高的开发效率。</p>
- <h2 id="raspberry-pi-初始化"><span class="header-section-number">9.2</span> Raspberry Pi 初始化</h2>
- <p>今天的Raspbian默认已经安装<code>openssh-server</code>,并默认开启了OpenSSH-Server。</p>
- <p>接着我们就可以看到系统启动了,要我们输入用户名和密码</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Raspbian</span> GNU/Linux 7 raspberrypi ttyAMA0
- <span class="kw">raspberrypi</span> login: pi
- <span class="kw">Password</span>:
- <span class="kw">Last</span> login: Sat Apr 26 05:58:07 UTC 2014 on ttyAMA0
- <span class="kw">Linux</span> raspberrypi 3.10.25+ <span class="co">#622 PREEMPT Fri Jan 3 18:41:00 GMT 2014 armv6l</span>
- <span class="kw">The</span> programs included with the Debian GNU/Linux system are free software<span class="kw">;</span>
- <span class="kw">the</span> exact distribution terms for each program are described in the
- <span class="kw">individual</span> files in /usr/share/doc/*/copyright.
- <span class="kw">Debian</span> GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
- <span class="kw">permitted</span> by applicable law.
- <span class="kw">ls</span>
- <span class="kw">NOTICE</span>: the software on this Raspberry Pi has not been fully configured. Please run <span class="st">'sudo raspi-config'</span></code></pre>
- <p>然后</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> raspi-config</code></pre>
- <p>选择第一个,下面就可以继续了</p>
- <p><code>Expand Filesystem Ensures that all of the SD card s</code></p>
- <p>接着重启后,便可以扩展SD卡成功。</p>
- <p>注: Raspbian与一般的Debian系统使用起来区别不是太大(ps:命令上),由于CPU是不同的架构,在编译上可能有所区别。通常PC上的软件需要重新编译才能在RPi上运行,所以如果可以用apt-get安装的话,就不要自己编译了。</p>
- <h2 id="raspberry-pi-gpio"><span class="header-section-number">9.3</span> Raspberry Pi GPIO</h2>
- <blockquote>
- <p>General Purpose Input Output (通用输入/输出)简称为GPIO,或总线扩展器,利用工业标准I2C、SMBus或SPI接口简化了I/O口的扩展。当微控制器或芯片组没有足够的I/O端口,或当系统需要采用远端串行通信或控制时,GPIO产品能够提供额外的控制和监视功能。</p>
- </blockquote>
- <div class="figure">
- <img src="./images/gpio.png" alt="GPIO" />
- <p>GPIO</p>
- </div>
- <h1 id="server-一切皆为服务"><span class="header-section-number">10</span> Server 一切皆为服务</h1>
- <h2 id="服务器"><span class="header-section-number">10.1</span> 服务器</h2>
- <p>服务器(Server)指:</p>
- <ul>
- <li>一个管理资源并为用户提供服务的计算机软件,通常分为文件服务器(能使用户在其它计算机访问文件),数据库服务器和应用程序服务器。</li>
- <li>运行以上软件的计算机,或称为网络主机(Host)。</li>
- <li>一般来说,服务器通过网络对外提供服务。可以通过Intranet对内网提供服务,也可以通过Internet对外提供服务。</li>
- </ul>
- <h2 id="web服务器"><span class="header-section-number">10.2</span> Web服务器</h2>
- <p>WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务。 WWW 是 Internet的多媒体信息查询工具,是 Internet 上近年才发展起来的服务,也是发展最快和目前用的最广泛的服务。正是因为有了WWW工具,才使得近年来 Internet 迅速发展,且用户数量飞速增长。</p>
- <h2 id="lnmp"><span class="header-section-number">10.3</span> LNMP</h2>
- <div class="figure">
- <img src="./images/lnmp.gif" alt="LNMP" />
- <p>LNMP</p>
- </div>
- <p>Linux+Nginx+MySQL+PHP</p>
- <blockquote>
- <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>
- </blockquote>
- <blockquote>
- <p>MySQL 是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。</p>
- </blockquote>
- <blockquote>
- <p>PHP于1994年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf为了要维护个人网页而制作的一个简单的用Perl语言编写的程序。这些工具程序用来显示 Rasmus Lerdorf 的个人履历,以及统计网页流量。后来又用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。</p>
- </blockquote>
- <h1 id="web服务"><span class="header-section-number">11</span> Web服务</h1>
- <p>Web服务是一种服务导向架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作。</p>
- <p>根据W3C的定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作。网络服务通常是许多应用程序接口(API)所组成的,它们透过网络,例如国际互联网(Internet)的远程服务器端,执行客户所提交服务的请求。</p>
- <p>尽管W3C的定义涵盖诸多相异且无法介分的系统,不过通常我们指有关于主从式架构(Client-server)之间根据SOAP协议进行传递XML格式消息。无论定义还是实现,WEB服务过程中会由服务器提供一个机器可读的描述(通常基于WSDL)以辨识服务器所提供的WEB服务。另外,虽然WSDL不是SOAP服务端点的必要条件,但目前基于Java的主流WEB服务开发框架往往需要WSDL实现客户端的源代码生成。一些工业标准化组织,比如WS-I,就在WEB服务定义中强制包含SOAP和WSDL。</p>
- <p>WEB服务实际上是一组工具,并有多种不同的方法调用之。三种最普遍的手段是:</p>
- <ul>
- <li>远程过程调用(RPC)</li>
- <li>面向服务架构(SOA)</li>
- <li>表述性状态转移(REST)。</li>
- </ul>
- <h2 id="soap-vs-restful"><span class="header-section-number">11.1</span> SOAP VS RESTful</h2>
- <p>简单对象访问协议是交换数据的一种协议规范,使用在计算机网络Web服务中,交换带结构信息。SOAP为了简化网页服务器从XML数据库中提取数据时,节省去格式化页面时间,以及不同应用程序之间按照HTTP通信协议,遵从XML格式执行资料互换,使其抽象于语言实现、平台和硬件。</p>
- <h1 id="http-熟悉陌生"><span class="header-section-number">12</span> HTTP 熟悉&陌生</h1>
- <h2 id="你所没有深入的http"><span class="header-section-number">12.1</span> 你所没有深入的HTTP</h2>
- <p>Internet有两个核心协议: IP和TCP,这样讲述起来似乎会很漫长。</p>
- <p>基本概念</p>
- <blockquote>
- <p>超文本传输协议 (HTTP-Hypertext transfer protocol) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议。</p>
- </blockquote>
- <ul>
- <li>HTTP是用于客户端与服务端之间的通信。</li>
- <li>传输层的TCP是基于网络层的IP协议的,而应用层的HTTP协议又是基于传输层的TCP协议的。</li>
- </ul>
- <p><code>注意</code>: HTTP协议只规定了客户端与服务端的通信规则,而没有规定其通讯协议,只是现在的大部分实现都是将TCP作为通讯协议。</p>
- <h3 id="打开网页时发生了什么"><span class="header-section-number">12.1.1</span> 打开网页时发生了什么</h3>
- <p>简单地来说,当我们在浏览器上输入URL的敲下回车的时候。</p>
- <ul>
- <li>浏览器需要查找域名<span id="fnref2"><a href="#fn2"><sup>2</sup></a></span>的IP,从不同的缓存直至DNS服务器。</li>
- <li>浏览器会给web服务器发送一个HTTP请求</li>
- <li>服务器“处理”请求</li>
- <li>服务器发回一个HTML响应</li>
- <li>浏览器渲染HTML到页面。</li>
- </ul>
- <p>在<a href="http://stackoverflow.com/questions/2092527/what-happens-when-you-type-in-a-url-in-browser">StackOverflow</a>上有一个这样的回答会比较详细。</p>
- <ul>
- <li>browser checks cache; if requested object is in cache and is fresh, skip to #9</li>
- <li>browser asks OS for server’s IP address</li>
- <li>OS makes a DNS lookup and replies the IP address to the browser</li>
- <li>browser opens a TCP connection to server (this step is much more complex with HTTPS)</li>
- <li>browser sends the HTTP request through TCP connection</li>
- <li>browser receives HTTP response and may close the TCP connection, or reuse it for another request</li>
- <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>
- <li>if cacheable, response is stored in cache</li>
- <li>browser decodes response (e.g. if it’s gzipped)</li>
- <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>
- <li>browser renders response, or offers a download dialog for unrecognized types</li>
- </ul>
- <p>忽略一些细节便剩下了</p>
- <ol>
- <li>从浏览器输入URL</li>
- <li>浏览器找到服务器,服务器返回HTML文档</li>
- <li>从对应的服务器下载资源</li>
- </ol>
- <p>说说第一步,开始时我们输入的是URI(统一资源标识符,Uniform Resource Identifier),它还有另外一个名字叫统一资源定位器(URL<span id="fnref3"><a href="#fn3"><sup>3</sup></a></span>,Uniform Resource Locator)。</p>
- <h3 id="url组成"><span class="header-section-number">12.1.2</span> URL组成</h3>
- <p>网址算是URL的一个俗称,让我们来看看一个URL的组成,以HTTP版IOT中的URL为例。</p>
- <p><code>http://b.phodal.com/athome/1</code></p>
- <p>开始之前,我们需要标出URL的80端口以及json文件的全称,那么上面的网址就会变成</p>
- <p><code>http://b.phodal.com:80/athome/1.json</code></p>
- <p>那么对于这个URL的就有下面几部分组成</p>
- <ul>
- <li><code>http://</code> http说的是这个URL用的是HTTP协议,至于<code>//</code>是一个分隔符,用法和C语言中的<code>;</code>一样。这样的协议还可以是coap,https,ftp等等。</li>
- <li><code>b</code> 是子域名,一个域名在<strong>允许</strong>的情况下可以有不限数量的子域名。</li>
- <li><code>phodal.com</code> 代表了一个URL是phodal.com下面的域名</li>
- <li><code>80</code> 80是指80端口,默认的都是80,对于一个不是80端口的URL应该是这样的<code>http://iot-coap.phodal.com:8896/</code></li>
- <li><code>athome</code> 指的是虚拟目录部分,或者文件路径</li>
- <li><code>1.json</code>看上去就是一个文件名,然而也代表着这是一个资源。</li>
- </ul>
- <p>对就一个稍微复杂点的例子就是</p>
- <p><code>http://designiot.phodal.com/#你所没有深入的http</code></p>
- <p>这里的<code>#</code>后面是锚部分,如果你打开这个URL就会发现会直接跳转到相应的锚部分,对就于下面这样的一个例子来说</p>
- <p><code>http://www.phodal.com/search/?q=iot&type=blog</code></p>
- <p><code>?</code>后面的<code>q=iot&type=blog</code>的部分是参数部分,通常用于查询或者、搜索。</p>
- <h2 id="一次http-get请求"><span class="header-section-number">12.2</span> 一次HTTP GET请求</h2>
- <p>当我们打开最小物联网系统的一个页面时,如<a href="http://b.phodal.com/athome/1.json" class="uri">http://b.phodal.com/athome/1.json</a></p>
- <p>我们在浏览器上看到的结果是</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">[
- {
- <span class="st">"id"</span>: <span class="dv">1</span>,
- <span class="st">"temperature"</span>: <span class="dv">19</span>,
- <span class="st">"sensors1"</span>: <span class="dv">31</span>,
- <span class="st">"sensors2"</span>: <span class="fl">7.5</span>,
- <span class="st">"led1"</span>: <span class="dv">0</span>
- }
- ]</code></pre>
- <p>只是我们看到的是结果,忽略了这其中的过程,于是我们用curl<span id="fnref4"><a href="#fn4"><sup>4</sup></a></span>命令来看看详细的情况。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">curl</span> -I -s http://b.phodal.com/athome/1.json</code></pre>
- <p>出于某种原因考虑,删去了其中一些元素,剩下下面这些。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.1</span> 200 OK
- <span class="kw">Content-Type</span>: application/json
- <span class="kw">Date</span>: Fri, 05 Sep 2014 15:05:49 GMT
- [<span class="dt">{"id":1,"temperature":19,"sensors1":31,"sensors2":7.5,"led1":0}</span>]</code></pre>
- <p>我们用curl命令向服务器发起了GET请求,服务器返回了上面的结果。</p>
- <h3 id="http响应"><span class="header-section-number">12.2.1</span> HTTP响应</h3>
- <p>一个HTTP响应由三部分组成</p>
- <ul>
- <li>状态行(状态码)</li>
- <li>消息报头(响应报头)</li>
- <li>响应正文(消息体)</li>
- </ul>
- <h4 id="http响应-状态码"><span class="header-section-number">12.2.1.1</span> HTTP响应 状态码</h4>
- <p>在上面的结果中,状态行是</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.1</span> 200 OK</code></pre>
- <p>返回的状态码是200,OK是状态码的原因短语。</p>
- <p>如果是一个跳转的页面,它就可能是下面的结果:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.0</span> 301 MOVED PERMANENTLY
- <span class="kw">Date</span>: Mon, 08 Sep 2014 12:04:01 GMT
- <span class="kw">Content-Type</span>: text/html<span class="kw">;</span> <span class="ot">charset=</span>utf-8 </code></pre>
- <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>
- <ul>
- <li>1xx消息</li>
- <li>2xx成功</li>
- <li>3xx重定向</li>
- <li>4xx客户端错误</li>
- <li>5xx服务器错误</li>
- </ul>
- <p>如</p>
- <ul>
- <li>200 ok - 成功返回状态,对应,GET,PUT,PATCH,DELETE.</li>
- <li>201 created - 成功创建。</li>
- <li>304 not modified - HTTP缓存有效。</li>
- <li>400 bad request - 请求格式错误。</li>
- <li>401 unauthorized - 未授权。</li>
- <li>403 forbidden - 鉴权成功,但是该用户没有权限。</li>
- <li>404 not found - 请求的资源不存在</li>
- <li>405 method not allowed - 该http方法不被允许。</li>
- <li>410 gone - 这个url对应的资源现在不可用。</li>
- <li>415 unsupported media type - 请求类型错误。</li>
- <li>422 unprocessable entity - 校验错误时用。</li>
- <li>429 too many request - 请求过多。</li>
- </ul>
- <h4 id="http响应-响应报头"><span class="header-section-number">12.2.1.2</span> HTTP响应 响应报头</h4>
- <p>在这次响应中,返回了两个报头,即</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Content-Type</span>: application/json
- <span class="kw">Date</span>: Fri, 05 Sep 2014 15:05:49 GMT</code></pre>
- <p>Content-Type和Date,在这里的Context-Type是application/json,而通常情况下我们打开一个网站时,他的Content-Type应该是text/html。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Content-Type</span>: text/html<span class="kw">;</span></code></pre>
- <p>Content-Type是最重要的报头。</p>
- <h4 id="http响应-响应正文"><span class="header-section-number">12.2.1.3</span> HTTP响应 响应正文</h4>
- <p>正文才是我们真正想要的内容,上面的都是写给浏览器看的,一般的人不会去关注这些。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">HTTP/<span class="fl">1.1</span> <span class="dv">200</span> OK
- Server: <span class="ot">phodal</span>.<span class="fu">com</span>/<span class="fl">0.17.5</span>
- Content-Type: application/json
- [{<span class="st">"id"</span>:<span class="dv">1</span>,<span class="st">"temperature"</span>:<span class="dv">19</span>,<span class="st">"sensors1"</span>:<span class="dv">31</span>,<span class="st">"sensors2"</span>:<span class="fl">7.5</span>,<span class="st">"led1"</span>:<span class="dv">0</span>}]</code></pre>
- <p>通常这是以某种格式写的,在这里是以JSON写的,而对于一个网站的时候则是HTML,如:</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><meta</span><span class="ot"> charset=</span><span class="st">"UTF-8"</span><span class="kw">></span>
- <span class="kw"><title></span>Document<span class="kw"></title></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
-
- <span class="kw"></body></span>
- <span class="kw"></html></span> </code></pre>
- <p>那么这次GET请求返回的就是:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">HTTP/1.0</span> 200 OK
- <span class="kw">Date</span>: Mon, 08 Sep 2014 12:04:01 GMT
- <span class="kw">Content-Type</span>: text/html<span class="kw">;</span> <span class="ot">charset=</span>utf-8
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><meta</span> charset=<span class="st">"UTF-8"</span><span class="kw">></span>
- <span class="kw"><title></span>Document<span class="kw"><</span>/title<span class="kw">></span>
- <span class="kw"><</span>/<span class="kw">head></span>
- <span class="kw"><body></span>
- [<span class="dt">{"id":1,"temperature":19,"sensors1":31,"sensors2":7.5,"led1":0}</span>]
- <span class="kw"><</span>/<span class="kw">body></span>
- <span class="kw"><</span>/<span class="kw">html></span> </code></pre>
- <p>虽然与第一次请求的结果在游览器上看似乎是一样的(ps:可能有微小的差异),然而其本质是不同的。</p>
- <p>推荐及参考书目:</p>
- <ul>
- <li>《Web性能权威指南》</li>
- <li>《图解HTTP》</li>
- <li>《RESTful Web Services Cookbook》</li>
- <li>《RESTful Web APIs》</li>
- </ul>
- <h1 id="设计restful-api"><span class="header-section-number">13</span> 设计RESTful API</h1>
- <blockquote>
- <p>REST从资源的角度来观察整个网络,分布在各处的资源由URI确定,而客户端的应用通过URI来获取资源的表征。获得这些表征致使这些应用程序转变了其状态。随着不断获取资源的表征,客户端应用不断地在转变着其状态,所谓表征状态转移。</p>
- </blockquote>
- <p>因为我们需要的是一个Machine到Machine沟通的平台,需要设计一个API。而设计一个API来说,RESTful是很不错的一种选择,也是主流的选择。而设计一个RESTful服务,的首要步骤便是设计资源模型。</p>
- <h3 id="资源"><span class="header-section-number">13.0.1</span> 资源</h3>
- <p>互联网上的一切信息都可以看作是一种资源。</p>
- <table>
- <thead>
- <tr class="header">
- <th align="left">HTTP Method</th>
- <th align="left">Operation Performed</th>
- </tr>
- </thead>
- <tbody>
- <tr class="odd">
- <td align="left">GET</td>
- <td align="left">Get a resource (Read a resource)</td>
- </tr>
- <tr class="even">
- <td align="left">POST</td>
- <td align="left">Create a resource</td>
- </tr>
- <tr class="odd">
- <td align="left">PUT</td>
- <td align="left">Update a resource</td>
- </tr>
- <tr class="even">
- <td align="left">DELETE</td>
- <td align="left">Delete Resource</td>
- </tr>
- </tbody>
- </table>
- <h1 id="设计restful-api-1"><span class="header-section-number">14</span> 设计RESTful API</h1>
- <p>设计RESTful API是一个有意思的话题。下面是一些常用的RESTful设计原则:</p>
- <h2 id="rest关键目标"><span class="header-section-number">14.1</span> REST关键目标</h2>
- <ul>
- <li>组件间交互的可伸缩性</li>
- <li>接口的通用性</li>
- <li>组件的独立部署</li>
- <li>通过中间组件来减少延迟、实施安全策略和封装已有系统</li>
- </ul>
- <h2 id="判断是否是-restful的约束条件"><span class="header-section-number">14.2</span> 判断是否是 RESTful的约束条件</h2>
- <ul>
- <li>客户端-服务器分离</li>
- <li>无状态</li>
- <li>可缓存</li>
- <li>多层系统</li>
- <li>统一接口</li>
- <li>随需代码(可选)</li>
- </ul>
- <h2 id="json"><span class="header-section-number">14.3</span> JSON</h2>
- <div class="figure">
- <img src="./images/xml-vs-json.png" alt="xml-vs-json" />
- <p>xml-vs-json</p>
- </div>
- <h1 id="环境准备"><span class="header-section-number">15</span> 环境准备</h1>
- <h2 id="laravel"><span class="header-section-number">15.1</span> Laravel</h2>
- <blockquote>
- <p>Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。</p>
- </blockquote>
- <ul>
- <li>RESTful 路由: 通过简单的闭包就能响应HTTP请求。帮你快速开始构建非凡的应用。</li>
- <li>强大的数据操纵能力: Laravel自带了强大的Eloquent ORM 和迁移工具。能够完美的与MySQL、Postgres、SQL Server 和 SQLite协同工作。</li>
- <li>优雅的模版引擎: PHP代码或轻量级的Blade模版引擎都可无缝融合。Blade模版可以继承,并且拥有极快的解析速度。相信你会喜欢它的。</li>
- <li>为明天做准备: 构建大型的企业级应用或者只是提供简单的JSON API;书写强大的控制器或轻巧的RESTful路由,Laravel适应所有级别的开发工作。</li>
- <li>可靠的基石: Laravel 的基石是数个Symfony组件,这些经过千锤百炼、可靠的组件为你的应用提供坚实的基础。</li>
- <li>基于Composer管理器: Composer 是一套帮你管理第三方扩展包的工具。能够让你迅速在 Packagist 中找到需要的扩展包。</li>
- <li>强大的社区支持: 无论你是一个PHP新手还是经验丰富的架构师,都能在社区中找到需要的知识。你可以在IRC中讨论Idea,或者在论坛中发布问题。</li>
- <li>测试、重构: Laravel 从开始就将测试作为重点功能。我们提供了灵活的IoC容器,集成了PHPUnit 测试工具。不用担心,这些都很容易上手。</li>
- </ul>
- <h3 id="为什么是-laravel"><span class="header-section-number">15.1.1</span> 为什么是 Laravel</h3>
- <ul>
- <li>因为个人喜爱,你也可以用 Ruby On Rails来搭建这样一个功能,或者是Java。</li>
- <li>PHP在我的服务器上运行得挺不错的,而且我又不需要重新去写配置那些配置。</li>
- <li>Laravel 可以简单的开发我们所需要的功能,换句话说他是 PHP 世界的 Ruby On Rails。</li>
- </ul>
- <p>这里不会再重述之前的问题,这里只是将需要的步骤一个个写下来,然后丢到这里好好说一下。至于RESTful是什么,前面已经介绍了,就不再重复了。那么下面,我们就用Laravel来搭建一个平台给物联网用的。</p>
- <h2 id="安装-laravel"><span class="header-section-number">15.2</span> 安装 Laravel</h2>
- <h3 id="gnulinux安装composer"><span class="header-section-number">15.2.1</span> GNU/Linux安装Composer</h3>
- <p>GNU/Linux Ubuntu/OpenSUSE下可以执行</p>
- <pre><code>$ curl -sS https://getcomposer.org/installer | php</code></pre>
- <h4 id="windows安装composer"><span class="header-section-number">15.2.1.1</span> Windows安装Composer</h4>
- <p>请直接下载</p>
- <p><a href="https://getcomposer.org/Composer-Setup.exe">Composer-Setup</a></p>
- <h4 id="mac-os"><span class="header-section-number">15.2.1.2</span> Mac OS</h4>
- <p>1.安装Composer</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install homebrew/php/composer</code></pre>
- <p>2.安装Laravel</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> global require <span class="st">"laravel/installer=~1.1"</span></code></pre>
- <p>3.创建Laravel工程</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">composer</span> create-project laravel/laravel your-project-name --prefer-dist </code></pre>
- <h4 id="mac-os-1"><span class="header-section-number">15.2.1.3</span> Mac OS</h4>
- <p>1.下载laravel.phar</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">wget</span> http://laravel.com/laravel.phar</code></pre>
- <p>2.重命名</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">mv</span> laravel.phar laravel</code></pre>
- <p>3.移动到bin中</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> mv laravel /usr/local/bin</code></pre>
- <p>4.创建项目</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">laravel</span> new blog</code></pre>
- <h2 id="mysql"><span class="header-section-number">15.3</span> MySQL</h2>
- <h3 id="安装mysql-1"><span class="header-section-number">15.3.1</span> 安装MySQL</h3>
- <p><code>出于某些原因,我建议用MariaDB替换MySQL,如果你"真正"需要mysql,将mariadb替换为mysql</code></p>
- <p>ps: 在下文中我会继续用MySQL,而不是MariaDB,MairaDB是MySQL的一个分支,真正的开源分支。</p>
- <p>Ubuntu/Debian/Mint</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> apt-get install mariadb-server</code></pre>
- <p>Fedora/Centos</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> yum install mariadb-server </code></pre>
- <p>openSUSE</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">sudo</span> zypper install mariadb-server</code></pre>
- <p>Mac OS</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">brew</span> install mariadb</code></pre>
- <h3 id="配置mysql"><span class="header-section-number">15.3.2</span> 配置MySQL</h3>
- <p>修改database.php</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">app/config/database.php</span></code></pre>
- <p>要修改的就是这个</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="st">'mysql'</span> => <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'driver'</span> => <span class="st">'mysql'</span><span class="ot">,</span>
- <span class="st">'host'</span> => <span class="st">'localhost'</span><span class="ot">,</span>
- <span class="st">'database'</span> => <span class="st">'iot'</span><span class="ot">,</span>
- <span class="st">'username'</span> => <span class="st">'root'</span><span class="ot">,</span>
- <span class="st">'password'</span> => <span class="st">'940217'</span><span class="ot">,</span>
- <span class="st">'charset'</span> => <span class="st">'utf8'</span><span class="ot">,</span>
- <span class="st">'collation'</span> => <span class="st">'utf8_unicode_ci'</span><span class="ot">,</span>
- <span class="st">'prefix'</span> => <span class="st">''</span><span class="ot">,</span>
- <span class="ot">),</span></code></pre>
- <p>如果你已经有phpmyadmin,似乎对你来说已经很简单了,如果没有的话,就直接用</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre>
- <p>来创建一个新的</p>
- <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>
- <p>数据库的目的在于存储数据等等的闲话这里就不多说了,创建一个RESTful的目的在于产生下面的JSON格式数据,以便于我们在Android、Java、Python、jQuery等语言框架或者平台上可以调用,最主要的是可以直接用Ajax来产生更炫目的效果。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">{
- <span class="st">"id"</span>: <span class="dv">1</span>,
- <span class="st">"temperature"</span>: <span class="dv">14</span>,
- <span class="st">"sensors1"</span>: <span class="dv">12</span>,
- <span class="st">"sensors2"</span>: <span class="dv">12</span>,
- <span class="st">"led1"</span>: <span class="dv">0</span>
- }</code></pre>
- <h1 id="创建rest服务"><span class="header-section-number">16</span> 创建REST服务</h1>
- <h2 id="数据库迁移"><span class="header-section-number">16.1</span> 数据库迁移</h2>
- <p>这个名字是源自于Ruby On Rails在那时候的印象,不直接使用MySQL的目的在于让我们可以专注于过程。</p>
- <h3 id="创建表"><span class="header-section-number">16.1.1</span> 创建表</h3>
- <p>表的概念,类似于在Excel中的表,如果你真实不懂数据库。 让我们创建一个athomes的表,为什么是athomes,因为以前在写android程序的时候就叫的是athome,忽略掉这些次要的因素吧。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate:make create_athomes_table</code></pre>
- <p>打开 app/database/migrations/<strong><em>create_athomes_table.php这里的</em></strong>是由日期和某些东西组成的,修改生成的代码为下面。</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">use</span> Illuminate\Database\Schema\Blueprint<span class="ot">;</span>
- <span class="kw">use</span> Illuminate\Database\Migrations\Migration<span class="ot">;</span>
- <span class="kw">class</span> CreateAthomesTable <span class="kw">extends</span> Migration {
- <span class="kw">public</span> <span class="kw">function</span> up<span class="ot">()</span>
- {
- Schema::create<span class="ot">(</span><span class="st">'athomes'</span><span class="ot">,</span> <span class="kw">function</span><span class="ot">(</span>Blueprint <span class="kw">$table</span><span class="ot">)</span>
- {
- <span class="kw">$table</span>--->increments<span class="ot">(</span><span class="st">'id'</span><span class="ot">);</span>
- <span class="kw">$table</span>->float<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$table</span>->float<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$table</span>->float<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$table</span>->boolean<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$table</span>->timestamps<span class="ot">();</span>
- }<span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> down<span class="ot">()</span>
- {
- Schema::drop<span class="ot">(</span><span class="st">'athomes'</span><span class="ot">);</span>
- }
- }</code></pre>
- <p>意思大致就是id是自加的,也就是我们在localhost/athome/{id},当我们创建一个新的数据的时候,会自动加上去,最后一个timestamps批的是时间,会包含创建时间和修改时间。 剩下的temperature,sensors1,sensors2是小数,以及只有真和假的led1。</p>
- <h3 id="数据库迁移-1"><span class="header-section-number">16.1.2</span> 数据库迁移</h3>
- <p>我们只是写了我们需要的数据的格式而并没有丢到数据库里,</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan migrate</code></pre>
- <p>这个就是我们执行迁移的命令,如果你用phpmyadmin可以直接打开查看,没有的话,可以。</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">mysql</span> -uroot -p</code></pre>
- <pre class="sourceCode sql"><code class="sourceCode sql"><span class="kw">use</span> iot;
- <span class="kw">select</span> * <span class="kw">from</span> athomes;</code></pre>
- <p>就可以看到我们写的东西,那么接下来就是创建RESTful服务了</p>
- <h2 id="创建restful"><span class="header-section-number">16.2</span> 创建RESTful</h2>
- <p>用下面的代码实现我们称之为Athomes控制器的创建</p>
- <pre class="sourceCode bash"><code class="sourceCode bash">$ <span class="kw">php</span> artisan controller:make AthomesController </code></pre>
- <p>就会在app/controllers下面生成下面的代码</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
- <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span> {}
- <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span> {}
- <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span> {}
- <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span> {}
- }</code></pre>
- <h2 id="laravel-resources"><span class="header-section-number">16.3</span> Laravel Resources</h2>
- <p>上面的代码过于沉重,请让我用 Ctrl+C 来带来点知识吧。</p>
- <table>
- <thead>
- <tr class="header">
- <th align="left">Verb</th>
- <th align="left">Path</th>
- <th align="left">Action</th>
- <th align="left">Route Name</th>
- </tr>
- </thead>
- <tbody>
- <tr class="odd">
- <td align="left">GET</td>
- <td align="left">/resource</td>
- <td align="left">index</td>
- <td align="left">resource.index</td>
- </tr>
- <tr class="even">
- <td align="left">GET</td>
- <td align="left">/resource/create</td>
- <td align="left">create</td>
- <td align="left">resource.create</td>
- </tr>
- <tr class="odd">
- <td align="left">POST</td>
- <td align="left">/resource</td>
- <td align="left">store</td>
- <td align="left">resource.store</td>
- </tr>
- <tr class="even">
- <td align="left">GET</td>
- <td align="left">/resource/{resource}</td>
- <td align="left">show</td>
- <td align="left">resource.show</td>
- </tr>
- <tr class="odd">
- <td align="left">GET</td>
- <td align="left">/resource/{resource}/edit</td>
- <td align="left">edit</td>
- <td align="left">resource.edit</td>
- </tr>
- <tr class="even">
- <td align="left">PUT/PATCH</td>
- <td align="left">/resource/{resource}</td>
- <td align="left">update</td>
- <td align="left">resource.update</td>
- </tr>
- <tr class="odd">
- <td align="left">DELETE</td>
- <td align="left">/resource/{resource}</td>
- <td align="left">destroy</td>
- <td align="left">resource.destroy</td>
- </tr>
- </tbody>
- </table>
- <p>所以我们只需要专注于创建 create, edit, show, destory 等等。好吧,你可能没有耐心了,但是在修改这个之前我们需要先在 app/model 加个 class</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> Athomes <span class="kw">extends</span> Eloquent {
- <span class="kw">protected</span> <span class="kw">$table</span> = <span class="st">'athomes'</span><span class="ot">;</span>
- }</code></pre>
- <p>如果你想要的只是控制器Athomes的代码的话。。</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">class</span> AthomesController <span class="kw">extends</span> \BaseController {
- <span class="kw">public</span> <span class="kw">$restful</span>=<span class="kw">true</span><span class="ot">;</span>
- <span class="kw">protected</span> <span class="kw">$athome</span><span class="ot">;</span>
- <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>
- {
- <span class="kw">$this</span>--->athome = <span class="kw">$athome</span> <span class="ot">;</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> index<span class="ot">()</span>
- {
- <span class="kw">$maxid</span>=Athomes::all<span class="ot">();</span>
- <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
- {
- <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">'id'</span><span class="ot">);</span>
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.create'</span><span class="ot">)</span>->with<span class="ot">(</span><span class="st">'maxid'</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
- {
- <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'led1'</span>=><span class="st">'required'</span><span class="ot">,</span>
- <span class="st">'sensors1'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'sensors2'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'temperature'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span>
- <span class="ot">);</span>
- <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>
- <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>->fails<span class="ot">())</span> {
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome/create'</span><span class="ot">)</span>
- ->withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">)</span>
- ->withInput<span class="ot">(</span>Input::except<span class="ot">(</span><span class="st">'password'</span><span class="ot">));</span>
- } <span class="kw">else</span> {
- <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
- <span class="kw">$nerd</span>->sensors1 = Input::get<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors2 = Input::get<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->temperature = Input::get<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->led1 = Input::get<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->save<span class="ot">();</span>
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully created athome!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }
- <span class="kw">public</span> <span class="kw">function</span> show<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$myid</span>=Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">$maxid</span>=Athomes::where<span class="ot">(</span><span class="st">'id'</span><span class="ot">,</span><span class="st">'='</span><span class="ot">,</span><span class="kw">$id</span><span class="ot">)</span>
- ->select<span class="ot">(</span><span class="st">'id'</span><span class="ot">,</span><span class="st">'temperature'</span><span class="ot">,</span><span class="st">'sensors1'</span><span class="ot">,</span><span class="st">'sensors2'</span><span class="ot">,</span><span class="st">'led1'</span><span class="ot">)</span>
- ->get<span class="ot">();</span>
- <span class="kw">return</span> Response::json<span class="ot">(</span><span class="kw">$maxid</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> edit<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.edit'</span><span class="ot">)</span>
- ->with<span class="ot">(</span><span class="st">'athome'</span><span class="ot">,</span> <span class="kw">$athome</span><span class="ot">);</span>
- }
- <span class="kw">public</span> <span class="kw">function</span> update<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'led1'</span>=><span class="st">'required|'</span><span class="ot">,</span>
- <span class="st">'sensors1'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'sensors2'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'temperature'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span>
- <span class="ot">);</span>
- <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>
- <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>->fails<span class="ot">())</span> {
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome/'</span> . <span class="kw">$id</span> . <span class="st">'/edit'</span><span class="ot">)</span>
- ->withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
- } <span class="kw">else</span> {
- <span class="kw">$nerd</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors1 = Input::get<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors2 = Input::get<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->temperature = Input::get<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->led1 = Input::get<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->save<span class="ot">();</span>
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully created athome!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }
- <span class="kw">public</span> <span class="kw">function</span> destroy<span class="ot">(</span><span class="kw">$id</span><span class="ot">)</span>
- {
- <span class="kw">$athome</span> = Athomes::find<span class="ot">(</span><span class="kw">$id</span><span class="ot">);</span>
- <span class="kw">$athome</span>->delete<span class="ot">();</span>
- <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>
- {
- <span class="kw">return</span> Response::json<span class="ot">(</span><span class="st">'Todo not found'</span><span class="ot">,</span> <span class="dv">404</span><span class="ot">);</span>
- }
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully deleted the nerd!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }</code></pre>
- <p>希望你能读懂,没有的话,继续。</p>
- <p>下面这部分来自于之前的博客,这里就不多加论述了。 这个也就是我们要的模板,</p>
- <h3 id="修改create"><span class="header-section-number">16.3.1</span> 修改Create()</h3>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
- {
- <span class="kw">$maxid</span>=Athomes::<span class="fu">max</span><span class="ot">(</span><span class="st">'id'</span><span class="ot">);</span>
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.create'</span><span class="ot">)</span>->with<span class="ot">(</span><span class="st">'maxid'</span><span class="ot">,</span><span class="kw">$maxid</span><span class="ot">);</span>
- }</code></pre>
- <p>这里需要在app/views/创建一个athome里面创建一个create.blade.php,至于maxid,暂时还不需要,后面会用到show。如果只需要模板,可以简化为</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> create<span class="ot">()</span>
- {
- <span class="kw">return</span> View::make<span class="ot">(</span><span class="st">'athome.create'</span><span class="ot">);</span>
- }</code></pre>
- <p>这里只是对其中代码的进行一下说明。</p>
- <h3 id="创建表单"><span class="header-section-number">16.3.2</span> 创建表单</h3>
- <h4 id="创建表单之前"><span class="header-section-number">16.3.2.1</span> 创建表单之前</h4>
- <p>由于使用到了bootstrap以及bootstrap-select,记得添加css。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap.min.css') ?>"</span> <span class="kw">/></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap-select.min.css') ?>"</span> <span class="kw">/></span></code></pre>
- <p>以及javascript</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/jquery.min.js')?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap-select.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script></span>
- <span class="fu">$</span>(<span class="st">'.selectpicker'</span>).<span class="fu">selectpicker</span>();
- <<span class="ot">/script></span></code></pre>
- <h4 id="创建表单-1"><span class="header-section-number">16.3.2.2</span> 创建表单</h4>
- <p>这里用到的是之前提到的那个作者写下的,稍微修改了一下。</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><div</span><span class="ot"> class=</span><span class="st">"row-fluid"</span><span class="kw">></span>
- {{ HTML::ul($errors->all()) }}
- {{ Form::open(array('url' => 'athome')) }}
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('led1', '开关1') }}
- {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors1', 'sensors1') }}
- {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors2', 'sensors2') }}
- {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('temperature', 'temperature') }}
- {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- {{ Form::submit('Create!', array('class' => 'btn btn-primary')) }}
- {{ Form::close() }}
- <span class="kw"></div></span></code></pre>
- <p>开关一开始打算用 checkbox,加上 bootstrap-switch 实现 ON OFF 弱弱地觉得还是没掌握好的节奏,所以最后用 select 来实现。</p>
- <p>还需要修改一下之前的 create(),添加一行</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span></code></pre>
- <p>也就是添加完后,重定向到首页查看,最后例子给出的 create 如下</p>
- <pre class="sourceCode php"><code class="sourceCode php"><span class="kw">public</span> <span class="kw">function</span> store<span class="ot">()</span>
- {
- <span class="kw">$rules</span> = <span class="fu">array</span><span class="ot">(</span>
- <span class="st">'led1'</span>=><span class="st">'required'</span><span class="ot">,</span>
- <span class="st">'sensors1'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'sensors2'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span><span class="ot">,</span>
- <span class="st">'temperature'</span> => <span class="st">'required|numeric|Min:-50|Max:80'</span>
- <span class="ot">);</span>
- <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>
- <span class="kw">if</span> <span class="ot">(</span><span class="kw">$validator</span>->fails<span class="ot">())</span> {
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome/create'</span><span class="ot">)</span>
- ->withErrors<span class="ot">(</span><span class="kw">$validator</span><span class="ot">);</span>
- } <span class="kw">else</span> {
- <span class="co">// store</span>
- <span class="kw">$nerd</span> = <span class="kw">new</span> Athomes<span class="ot">;</span>
- <span class="kw">$nerd</span>->sensors1 = Input::get<span class="ot">(</span><span class="st">'sensors1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->sensors2 = Input::get<span class="ot">(</span><span class="st">'sensors2'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->temperature = Input::get<span class="ot">(</span><span class="st">'temperature'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->led1 = Input::get<span class="ot">(</span><span class="st">'led1'</span><span class="ot">);</span>
- <span class="kw">$nerd</span>->save<span class="ot">();</span>
- Session::flash<span class="ot">(</span><span class="st">'message'</span><span class="ot">,</span> <span class="st">'Successfully created athome!'</span><span class="ot">);</span>
- <span class="kw">return</span> Redirect::to<span class="ot">(</span><span class="st">'athome'</span><span class="ot">);</span>
- }
- }</code></pre>
- <h3 id="编辑模板"><span class="header-section-number">16.3.3</span> 编辑模板</h3>
- <p>完整的 blade 模板文件</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="dt"><!DOCTYPE </span>html lang="zh-cn"<span class="dt">></span>
- <span class="kw"><html></span>
- <span class="kw"><head></span>
- <span class="kw"><meta</span><span class="ot"> http-equiv=</span><span class="st">"Content-type"</span><span class="ot"> content=</span><span class="st">"text/html; charset=utf-8"</span><span class="kw">></span>
- <span class="kw"><meta</span><span class="ot"> name=</span><span class="st">"keywords"</span><span class="ot"> content=</span><span class="st">""</span><span class="kw">></span>
- <span class="kw"><meta</span><span class="ot"> name=</span><span class="st">"viewport"</span><span class="ot"> content=</span><span class="st">"width=device-width"</span><span class="kw">></span>
- <span class="kw"><meta</span><span class="ot"> name=</span><span class="st">"description"</span><span class="ot"> content=</span><span class="st">""</span><span class="kw">></span>
- <span class="kw"><title></span>@yield('title')<span class="kw"></title></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap.min.css') ?>"</span> <span class="kw">/></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/bootstrap-select.min.css') ?>"</span> <span class="kw">/></span>
- <span class="kw"><link</span><span class="ot"> rel=</span><span class="st">"stylesheet"</span><span class="ot"> href=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('css/justified-nav.css') ?>"</span><span class="ot"> type=</span><span class="st">"text/css"</span><span class="ot"> media=</span><span class="st">"screen"</span> <span class="kw">/></span>
- <span class="kw"></head></span>
- <span class="kw"><body></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"container"</span><span class="kw">></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"container"</span><span class="kw">></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"row-fluid"</span><span class="kw">></span>
- <span class="kw"><h1></span>Edit {{ $athome->id }}<span class="kw"></h1></span>
- <span class="co"><!-- if there are creation errors, they will show here --></span>
- {{ HTML::ul($errors->all()) }}
- {{ Form::model($athome, array('route' => array('athome.update', $athome->id), 'method' => 'PUT')) }}
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('led1', '开关1') }}
- {{ Form::select('led1',array('关','开'),$selected=NULL,array('class'=>'selectpicker')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors1', '传感器1') }}
- {{ Form::text('sensors1', Input::old('sensors1'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('sensors2', '传感器2') }}
- {{ Form::text('sensors2', Input::old('sensors2'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"form-group"</span><span class="kw">></span>
- {{ Form::label('temperature', '温度传感器') }}
- {{ Form::text('temperature', Input::old('temperature'), array('class' => 'form-control')) }}
- <span class="kw"></div></span>
- {{ Form::submit('Edit the Nerd!', array('class' => 'btn btn-primary')) }}
- {{ Form::close() }}
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> class=</span><span class="st">"footer"</span><span class="kw">></span>
- <span class="kw"><p></span>© Company 2013<span class="kw"></p></span>
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/jquery.min.js')?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script</span><span class="ot"> type=</span><span class="st">"text/javascript"</span><span class="ot"> src=</span><span class="st">"</span><span class="er"><</span><span class="st">?= url('js/bootstrap-select.min.js') ?>"</span><span class="kw">></script></span>
- <span class="kw"><script></span>
- <span class="fu">$</span>(<span class="st">'.selectpicker'</span>).<span class="fu">selectpicker</span>();
- <<span class="ot">/script></span>
- <span class="ot"><script type="text/javascript</span><span class="st">" src="</span><?= <span class="fu">url</span>(<span class="st">'js/log.js'</span>) ?><span class="st">"></script></span>
- <<span class="ot">/body></span>
- <span class="ot"></html</span>></code></pre>
- <p>效果图:</p>
- <div class="figure">
- <img src="./images/edit.png" alt="Blade Edit" />
- <p>Blade Edit</p>
- </div>
- <p>最后效果见:<a href="http://b.phodal.com/" class="uri">http://b.phodal.com/</a></p>
- <h1 id="前端显示"><span class="header-section-number">17</span> 前端显示</h1>
- <h2 id="库与车轮子"><span class="header-section-number">17.1</span> 库与车轮子</h2>
- <p>在多数的情况下我们都没有理由也没有必要去重新发明我们的车轮,在这时使用库会是一个比较好的做法。</p>
- <h2 id="库"><span class="header-section-number">17.2</span> 库</h2>
- <h3 id="jquery"><span class="header-section-number">17.2.1</span> jQuery</h3>
- <blockquote>
- <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>
- </blockquote>
- <p>在我们的代码里用到了jQuery,然而这是一种简单而且快速有速地方法。</p>
- <h3 id="jquery-mobile"><span class="header-section-number">17.2.2</span> jQuery Mobile</h3>
- <blockquote>
- <p>jQuery Mobile是jQuery 在手机上和平板设备上的版本。jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架。支持全球主流的移动平台。jQuery Mobile开发团队说:能开发这个项目,我们非常兴奋。移动Web太需要一个跨浏览器的框架,让开发人员开发出真正的移动Web网站。</p>
- </blockquote>
- <p>整个展示页面由三部分组成,即<code>header</code>,<code>content</code>,<code>footer</code>。而我们主要需要关心的是content,也就是真下的内容。</p>
- <p>我们只需要结合<code>HighChart</code>来设计我们的content就可以了,下面是一个简单地<code>HighChart</code>示例:</p>
- <pre><code><div id="Tchart" style="min-width:800px;width:100%;height:300px"></div>
- </div></code></pre>
- <p>剩下的事就由<code>HighChart</code>来做。</p>
- <p>最后代码如下所示</p>
- <pre class="sourceCode html"><code class="sourceCode html"><span class="kw"><body></span>
- <span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"page"</span><span class="kw">></span>
- <span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"header"</span><span class="ot"> class=</span><span class="st">"ui-bar ui-bar-b"</span><span class="ot"> class=</span><span class="st">"jqm-header"</span><span class="kw">></span>
- <span class="kw"><h1></span>基础控制<span class="kw"></h1></span>
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"collapsible"</span><span class="ot"> data-collapsed=</span><span class="st">"false"</span><span class="kw">></span>
- <span class="kw"><h3></span>查看温度情况<span class="kw"></h3></span>
- <span class="kw"><div></span>
- <span class="kw"><div</span><span class="ot"> id=</span><span class="st">"Tchart"</span><span class="ot"> style=</span><span class="st">"min-width:800px;width:100%;height:300px"</span><span class="kw">></div></span>
- <span class="kw"></div></span>
- <span class="kw"></div></span>
- <span class="kw"><div</span><span class="ot"> data-role=</span><span class="st">"footer"</span><span class="ot"> class=</span><span class="st">"ui-bar ui-bar-b"</span><span class="ot"> data-position=</span><span class="st">"fixed"</span><span class="kw">></span>
- <span class="kw"><h4><a</span><span class="ot"> href=</span><span class="st">"http://www.phodal.com"</span><span class="kw">></span>Power by Phodal<span class="kw"></a></h4></span>
- <span class="kw"></div></span>
- <span class="kw"></body></span></code></pre>
- <p>可以看到上面的代码与一般的HTML不同的地方是<code>data-role</code>,<code>data-collapsed</code>,<code>data-position</code>,而这些是jQuery Mobile所拥有的data属性。</p>
- <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>
- <p>同上,我们也可以在网上找到其他相对就的属性。</p>
- <h2 id="网站前台显示"><span class="header-section-number">17.3</span> 网站前台显示</h2>
- <h3 id="highcharts"><span class="header-section-number">17.3.1</span> Highcharts</h3>
- <p>Highcharts有以下的特点</p>
- <ul>
- <li>兼容性:兼容当今所有的浏览器,包括 iPhone、IE 和火狐等等;</li>
- <li>对个人用户完全免费;</li>
- <li>纯JS,无BS;</li>
- <li>支持大部分的图表类型:直线图,曲线图、区域图、区域曲线图、柱状图、饼装图、散布图;</li>
- <li>跨语言:不管是 PHP、Asp.net 还是 Java 都可以使用,它只需要三个文件:一个是Highcharts 的核心文件 highcharts.js,还有 a canvas emulator for IE 和 Jquery类库或者 MooTools 类库;</li>
- <li>提示功能:鼠标移动到图表的某一点上有提示信息;</li>
- <li>放大功能:选中图表部分放大,近距离观察图表;</li>
- <li>易用性:无需要特殊的开发技能,只需要设置一下选项就可以制作适合自己的图表;</li>
- <li>时间轴:可以精确到毫秒;</li>
- </ul>
- <p>在这里只需将需要处理的数据存储到数组中,便可以将其渲染成为图形,下面的温度走势图便是基于Highcharts的结果:</p>
- <div class="figure">
- <img src="./images/temperture.png" alt="Temperture" />
- <p>Temperture</p>
- </div>
- <p>先看看最后代码如下所示:</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength = [];
- <span class="kw">function</span> <span class="fu">drawTemp</span>() {
- <span class="kw">var</span> zero = [];
- <span class="ot">$</span>.<span class="fu">getJSON</span>(<span class="st">'/athome/'</span>, <span class="kw">function</span>(json) {
- <span class="kw">var</span> items = [];
- <span class="ot">dataLength</span>.<span class="fu">push</span>(<span class="ot">json</span>.<span class="fu">length</span>);
- <span class="ot">$</span>.<span class="fu">each</span>(json, <span class="kw">function</span>(key, val) {
- <span class="ot">zero</span>.<span class="fu">push</span>(<span class="ot">val</span>.<span class="fu">temperature</span>);
- });
- chart = <span class="kw">new</span> <span class="ot">Highcharts</span>.<span class="fu">Chart</span>({
- <span class="dt">color</span>: {
- <span class="dt">linearGradient</span>: {
- <span class="dt">x1</span>: <span class="dv">0</span>,
- <span class="dt">x2</span>: <span class="dv">0</span>,
- <span class="dt">y1</span>: <span class="dv">0</span>,
- <span class="dt">y1</span>: <span class="dv">1</span>
- },
- <span class="dt">stops</span>: [
- [<span class="dv">0</span>, <span class="st">'#003399'</span>],
- [<span class="dv">1</span>, <span class="st">'#3366AA'</span>]
- ]
- },
- <span class="dt">chart</span>: {
- <span class="dt">renderTo</span>: <span class="st">'Tchart'</span>,
- <span class="dt">type</span>: <span class="st">'spline'</span>
- },
- <span class="dt">title</span>: {
- <span class="dt">text</span>: <span class="st">'本月温度情况'</span>
- },
- <span class="dt">subtitle</span>: {
- <span class="dt">text</span>: <span class="st">''</span>
- },
- <span class="dt">xAxis</span>: {
- <span class="dt">categories</span>: [],
- <span class="dt">title</span>: {
- <span class="dt">text</span>: <span class="st">''</span>
- }
- },
- <span class="dt">yAxis</span>: {
- <span class="dt">title</span>: {
- <span class="dt">text</span>: <span class="st">'温度 (°C)'</span>
- }
- },
- <span class="dt">tooltip</span>: {
- <span class="dt">backgroundColor</span>: <span class="st">'#FCFFC5'</span>,
- <span class="dt">borderColor</span>: <span class="st">'black'</span>,
- <span class="dt">borderRadius</span>: <span class="dv">10</span>,
- <span class="dt">borderWidth</span>: <span class="dv">1</span>,
- <span class="dt">enabled</span>: <span class="kw">true</span>,
- <span class="dt">formatter</span>: <span class="kw">function</span>() {
- <span class="kw">return</span> <span class="st">'<b>'</span> + <span class="kw">this</span>.<span class="ot">series</span>.<span class="fu">name</span> + <span class="st">'</b><br/>'</span> + <span class="kw">this</span>.<span class="fu">x</span> + <span class="st">': '</span> + <span class="kw">this</span>.<span class="fu">y</span> + <span class="st">'°C'</span>;
- }
- },
- <span class="dt">legend</span>: {
- <span class="dt">layout</span>: <span class="st">'vertical'</span>,
- <span class="dt">align</span>: <span class="st">'right'</span>,
- <span class="dt">verticalAlign</span>: <span class="st">'top'</span>,
- <span class="dt">x</span>: -<span class="dv">10</span>,
- <span class="dt">y</span>: <span class="dv">100</span>,
- <span class="dt">borderWidth</span>: <span class="dv">0</span>
- },
- <span class="dt">plotOptions</span>: {
- <span class="dt">line</span>: {
- <span class="dt">dataLabels</span>: {
- <span class="dt">enabled</span>: <span class="kw">true</span>
- },
- <span class="dt">enableMouseTracking</span>: <span class="kw">false</span>
- }
- },
- <span class="dt">series</span>: [{
- <span class="dt">name</span>: <span class="st">'本月'</span>,
- <span class="dt">data</span>: zero
- }, {
- <span class="dt">name</span>: <span class="st">'对比'</span>,
- <span class="dt">data</span>: [<span class="fl">26.0</span>]
- }]
- });
- });
- };
- <span class="kw">function</span> <span class="fu">showTemper</span>() {
- <span class="kw">var</span> length = dataLength[<span class="dv">0</span>];
- <span class="ot">$</span>.<span class="fu">ajax</span>({
- <span class="dt">url</span>: <span class="st">'/athome/'</span> + length,
- <span class="dt">type</span>: <span class="st">'GET'</span>,
- <span class="dt">dataType</span>: <span class="st">'json'</span>,
- <span class="dt">async</span>: <span class="kw">true</span>,
- <span class="dt">timeout</span>: <span class="dv">1000</span>,
- <span class="dt">error</span>: <span class="kw">function</span>() {},
- <span class="dt">success</span>: <span class="kw">function</span>(sdata) {
- <span class="fu">$</span>(<span class="st">'.temperStatus'</span>).<span class="fu">empty</span>();
- <span class="fu">$</span>(<span class="st">'.temperStatus'</span>).<span class="fu">append</span>(<span class="ot">sdata</span>.<span class="fu">temperature</span>);
- }
- });
- };
- <span class="fu">$</span>(document).<span class="fu">ready</span>(<span class="kw">function</span>() {
- <span class="fu">setInterval</span>(<span class="st">"drawTemp();"</span>, <span class="dv">5000</span>);
- <span class="fu">setInterval</span>(<span class="st">"showTemper();"</span>, <span class="dv">800</span>);
- <span class="fu">drawTemp</span>();
- <span class="fu">showTemper</span>();
- });</code></pre>
- <p>我们先把<code>HighChart</code>部分的代码删去,就变成下面的内容:</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> dataLength = [];
- <span class="kw">function</span> <span class="fu">drawTemp</span>() {
- <span class="kw">var</span> zero = [];
- <span class="ot">$</span>.<span class="fu">getJSON</span>(<span class="st">'/athome/'</span>, <span class="kw">function</span>(json) {
- <span class="kw">var</span> items = [];
- <span class="ot">dataLength</span>.<span class="fu">push</span>(<span class="ot">json</span>.<span class="fu">length</span>);
- <span class="ot">$</span>.<span class="fu">each</span>(json, <span class="kw">function</span>(key, val) {
- <span class="ot">zero</span>.<span class="fu">push</span>(<span class="ot">val</span>.<span class="fu">temperature</span>);
- });
- });
- };
- <span class="kw">function</span> <span class="fu">showTemper</span>() {
- <span class="kw">var</span> length = dataLength[<span class="dv">0</span>];
- <span class="ot">$</span>.<span class="fu">ajax</span>({
- <span class="dt">url</span>: <span class="st">'/athome/'</span> + length,
- <span class="dt">type</span>: <span class="st">'GET'</span>,
- <span class="dt">dataType</span>: <span class="st">'json'</span>,
- <span class="dt">async</span>: <span class="kw">true</span>,
- <span class="dt">timeout</span>: <span class="dv">1000</span>,
- <span class="dt">error</span>: <span class="kw">function</span>() {},
- <span class="dt">success</span>: <span class="kw">function</span>(sdata) {
- <span class="fu">$</span>(<span class="st">'.temperStatus'</span>).<span class="fu">empty</span>();
- <span class="fu">$</span>(<span class="st">'.temperStatus'</span>).<span class="fu">append</span>(<span class="ot">sdata</span>.<span class="fu">temperature</span>);
- }
- });
- };
- <span class="fu">$</span>(document).<span class="fu">ready</span>(<span class="kw">function</span>() {
- <span class="fu">setInterval</span>(<span class="st">"drawTemp();"</span>, <span class="dv">5000</span>);
- <span class="fu">setInterval</span>(<span class="st">"showTemper();"</span>, <span class="dv">800</span>);
- <span class="fu">drawTemp</span>();
- <span class="fu">showTemper</span>();
- });</code></pre>
- <p>代码看上去是由两部分组成的<code>drawTemperature</code>和<code>showTemperature</code></p>
- <h3 id="实时数据"><span class="header-section-number">17.3.2</span> 实时数据</h3>
- <p>剥离后的Ajax部分代码如下所示,主要用的是jQuery框架的getJSON来实现的</p>
- <pre><code>var dataLength = [];
- function drawTemp() {
- var zero = [];
- $.getJSON('/athome/', function(json) {
- var items = [];
- dataLength.push(json.length);
- $.each(json, function(key, val) {
- zero.push(val.temperature);
- });
- };</code></pre>
- <p>实际上,我们做的只是从/athome/下面获取数据,再将数据堆到数组里面,再把这部分放到图形中。等等,什么是Ajax?</p>
- <p>AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。 AJAX 不是新的编程语言,而是一种使用现有标准的新方法。 AJAX 是与服务器交换数据并更新部分网页的艺术,在不重新加载整个页面的情况下。 JSON我们前面也已经了解过了,看看getJSON吧。</p>
- <p>jQuery. getJSON</p>
- <p>方法定义:jQuery.getJSON( url, data, callback )</p>
- <p>通过get请求得到json数据</p>
- <p>·url用于提供json数据的地址页 ·data(Optional)用于传送到服务器的键值对 ·callback(Optional)回调函数,json数据请求成功后的处理函数 我想你似乎应该懂得了一点,就是在不刷新网页的同时,用javascript获取数据放到图表上,就这么简单。</p>
- <h1 id="restful的coap协议"><span class="header-section-number">18</span> RESTful的CoAP协议</h1>
- <h2 id="coap-嵌入式系统的rest"><span class="header-section-number">18.1</span> CoAP: 嵌入式系统的REST</h2>
- <p>引自维基百科上的介绍,用的是谷歌翻译。。。</p>
- <blockquote>
- <p>受约束的应用协议(COAP)是一种软件协议旨在以非常简单的电子设备,使他们能够在互联网上进行交互式通信中使用。它特别针对小型低功率传感器,开关,阀门和需要被控制或监督远程,通过标准的Internet网络类似的组件。 COAP是一个应用层协议,该协议是用于在资源受限的网络连接设备,例如无线传感器网络节点使用。 COAP被设计为容易地转换为HTTP与Web简化集成,同时也能满足特殊的要求,例如多播支持,非常低的开销,和简单性。多播,低开销,以及简单性是因特网极其重要物联网(IOT)和机器对机器(M2M)设备,这往往是积重难返,有太多的内存和电源,比传统的互联网设备有。因此,效率是非常重要的。 COAP可以在支持UDP或UDP的模拟大多数设备上运行。</p>
- </blockquote>
- <p>简单地来说,CoAP是简化了HTTP协议的RESTful API,因而也只提供了REST的四个方法,即PUT,GET,POST和DELETE。对于微小的资源受限,在资源受限的通信的IP的网络,HTTP不是一种可行的选择。它占用了太多的资源和太多的带宽。而对于物联网这种嵌入式设备来说,关于资源与带宽,是我们需要优先考虑的内容。</p>
- <ul>
- <li>CoAP采用了二进制报头,而不是文本报头(text header)</li>
- <li>CoAP降低了头的可用选项的数量。</li>
- <li>CoAP减少了一些HTTP的方法</li>
- <li>CoAP可以支持检测装置</li>
- </ul>
- <h2 id="coap-命令行工具"><span class="header-section-number">18.2</span> CoAP 命令行工具</h2>
- <p>为了测试测试我们的代码是否是正确工作,我们需要一个CoAP的命令行工具。目前有两个不错的工具可以使用。</p>
- <ul>
- <li>CoAP-cli,一个基于NodeJS的CoAP命令行工具,其核心是基于Node-CoAP库。</li>
- <li>libcooap,一个用C写的CoAP命令行工具。</li>
- <li>FireFox Copper, 一个Firefox的插件。</li>
- </ul>
- <h3 id="node-coap-cli"><span class="header-section-number">18.2.1</span> Node CoAP CLI</h3>
- <p>安装命令如下</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"> <span class="kw">npm</span> install coap-cli -g </code></pre>
- <h4 id="coap命令行"><span class="header-section-number">18.2.1.1</span> CoAP命令行</h4>
- <p>在coap-cli中,一共有四个方法。分别表示REST的四种不同的方式:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Commands</span>:
- <span class="kw">get</span> performs a GET request
- <span class="kw">put</span> performs a PUT request
- <span class="kw">post</span> performs a POST request
- <span class="kw">delete</span> performs a DELETE request</code></pre>
- <p>在这里,我们用<a href="coap://vs0.inf.ethz.ch/" class="uri">coap://vs0.inf.ethz.ch/</a>来作一个简单的测试</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> get coap://vs0.inf.ethz.ch/
- <span class="kw">(2.05)</span> <span class="kw">************************************************************</span>
- <span class="kw">I-D</span></code></pre>
- <p>测试一下现在的最小的物联网系统CoAP版</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> get coap://iot-coap.phodal.com/id/1
- <span class="kw">(2.05)</span> [{<span class="st">"id"</span>:<span class="kw">1</span>,<span class="st">"value"</span>:<span class="st">"is id 1"</span>,<span class="st">"sensors1"</span>:19,<span class="st">"sensors2"</span>:20}]</code></pre>
- <h3 id="libcoap"><span class="header-section-number">18.2.2</span> libcoap</h3>
- <h4 id="mac-os-libcoap安装"><span class="header-section-number">18.2.2.1</span> mac os libcoap安装</h4>
- <p>Mac OS下可以直接用</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">brew</span> install libcoap</code></pre>
- <h4 id="ubuntu-libcoap安装"><span class="header-section-number">18.2.2.2</span> Ubuntu libcoap安装</h4>
- <p>Ubuntu GNU/Linux下</p>
- <h4 id="windows-libcoap安装"><span class="header-section-number">18.2.2.3</span> Windows libcoap安装</h4>
- <p>Windows 下</p>
- <p>安装完libcoap,我们可以直接用自带的两个命令</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-client</span>
- <span class="kw">coap-server</span></code></pre>
- <p>1.用coap-server启一个CoAP服务</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-server</span></code></pre>
- <p>2.客户端获取数据</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap-client</span> -m get coap://localhost</code></pre>
- <p>返回结果</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">v</span>:1 t:0 tkl:0 c:1 id:37109
- <span class="kw">This</span> is a test server made with libcoap (see http://libcoap.sf.net)
- <span class="kw">Copyright</span> (C) <span class="kw">2010--2013</span> Olaf Bergmann <span class="kw"><</span>bergmann@tzi.org<span class="kw">></span></code></pre>
- <h3 id="firefox-copper"><span class="header-section-number">18.2.3</span> Firefox Copper</h3>
- <p>为了能访问<a href="coap://localhost/" class="uri">coap://localhost/</a>,于是我们便需要安装一个Firefox并安装一个名为Copper的插件。</p>
- <ol>
- <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>
- <li><p>作为测试我们同样可以访问 <a href="coap://vs0.inf.ethz.ch:5683/" class="uri">coap://vs0.inf.ethz.ch:5683/</a></p></li>
- </ol>
- <h2 id="coap-helloworld"><span class="header-section-number">18.3</span> CoAP Hello,World</h2>
- <p>接着我们便开始试试做一个简单的CoAP协议的应用:</p>
- <p>这里用到的是一个Nodejs的扩展Node-CoAP</p>
- <blockquote>
- <p>node-coap is a client and server library for CoAP modelled after the http module.</p>
- </blockquote>
- <p>Node-CoAP是一个客户端和服务端的库用于CoAP的模块建模。创建一个package.json文件,添加这个库</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">{
- <span class="st">"dependencies"</span>:{
- <span class="st">"coap"</span>: <span class="st">"0.7.2"</span>
- }
- }</code></pre>
- <p>接着执行</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install</code></pre>
- <p>就可以安装好这个库。如果遇到权限问题,请用</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> npm install</code></pre>
- <p>接着,创建这样一个app.js</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">'coap'</span>)
- , server = <span class="ot">coap</span>.<span class="fu">createServer</span>()
- <span class="ot">server</span>.<span class="fu">on</span>(<span class="st">'request'</span>, <span class="kw">function</span>(req, res) {
- <span class="ot">res</span>.<span class="fu">end</span>(<span class="st">'Hello '</span> + <span class="ot">req</span>.<span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">1</span>] + <span class="st">'</span><span class="ch">\n</span><span class="st">'</span>)
- })
- <span class="ot">server</span>.<span class="fu">listen</span>(<span class="kw">function</span>() {
- <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">'server started'</span>)
- }) </code></pre>
- <p>执行</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> app.js</code></pre>
- <p>便可以在浏览器上访问了,因为现在什么也没有,所以什么也不会返回。</p>
- <p>接着下来再创建一个client端的js,并运行之</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">'coap'</span>)
- , req = <span class="ot">coap</span>.<span class="fu">request</span>(<span class="st">'coap://localhost/World'</span>)
- <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">'response'</span>, <span class="kw">function</span>(res) {
- <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="ot">process</span>.<span class="fu">stdout</span>)
- })
- <span class="ot">req</span>.<span class="fu">end</span>()</code></pre>
- <p>就可以在console上输出</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">Hello</span> World </code></pre>
- <p>也就达到了我们的目的,用CoAP协议创建一个服务,接着我们应该用它创建更多的东西,如产生JSON数据,以及RESTful。和HTTP版的最小物联网系统一样,CoAP版的最小物联网系统也是要返回JSON的。</p>
- <h2 id="coap-数据库查询"><span class="header-section-number">18.4</span> CoAP 数据库查询</h2>
- <h3 id="node-module"><span class="header-section-number">18.4.1</span> Node Module</h3>
- <p>这说里NodeJS Module的意义是因为我们需要在别的地方引用到db_helper这个库,也就是下一小节要的讲的内容。</p>
- <p>这样我们就可以在server.js类似于这样去引用这个js库。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> DBHelper = <span class="fu">require</span>(<span class="st">'./db_helper.js'</span>);
- <span class="ot">DBHelper</span>.<span class="fu">initDB</span>();</code></pre>
- <p>而这样调用的前提是我们需要去声明这样的module,为了方便地导出函数功能调用。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">DBHelper</span>(){
- }
- <span class="ot">DBHelper</span>.<span class="fu">initDB</span> = <span class="kw">function</span>(){};
- <span class="ot">module</span>.<span class="fu">exports</span> = DBHelper;</code></pre>
- <h3 id="node-sqlite3"><span class="header-section-number">18.4.2</span> Node-Sqlite3</h3>
- <p>这次我们用的是SQLite3(你可以用MySQL,出于安全考虑用SQLite3,SQLite3产生的是一个文件)。一个简单的initDB函数</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(config[<span class="st">"db_name"</span>]);
- <span class="kw">var</span> create_table = <span class="st">'create table if not exists basic ('</span> + config[<span class="st">"db_table"</span>] + <span class="st">');'</span>;
- <span class="ot">db</span>.<span class="fu">serialize</span>(<span class="kw">function</span>() {
- <span class="ot">db</span>.<span class="fu">run</span>(create_table);
- <span class="ot">_</span>.<span class="fu">each</span>(config[<span class="st">"init_table"</span>], <span class="kw">function</span>(insert_data) {
- <span class="ot">db</span>.<span class="fu">run</span>(insert_data);
- });
- });
- <span class="ot">db</span>.<span class="fu">close</span>();</code></pre>
- <p>首先从配置中读取db_name,接着创建table,然后调用underscore的each方法,创建几个数据。配置如下所示</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">config = {
- <span class="st">"db_name"</span>: <span class="st">"iot.db"</span>,
- <span class="st">"db_table"</span>: <span class="st">"id integer primary key, value text, sensors1 float, sensors2 float"</span>,
- <span class="st">"init_table"</span>:[
- <span class="st">"insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, 'is id 1', 19, 20);"</span>,
- <span class="st">"insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, 'is id 2', 20, 21);"</span>
- ],
- <span class="st">"query_table"</span>:<span class="st">"select * from basic;"</span>
- };</code></pre>
- <p>而之前所提到的url查询所做的事情便是</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">DBHelper</span>.<span class="fu">urlQueryData</span> = <span class="kw">function</span> (url, callback) {
- <span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(<span class="st">"iot.db"</span>);
- <span class="kw">var</span> result = [];
- <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">"SELECT * FROM basic where "</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">1</span>] + <span class="st">"="</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">2</span>]);
- <span class="ot">db</span>.<span class="fu">all</span>(<span class="st">"SELECT * FROM basic where "</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">1</span>] + <span class="st">"="</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">2</span>], <span class="kw">function</span>(err, rows) {
- <span class="ot">db</span>.<span class="fu">close</span>();
- <span class="fu">callback</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>(rows));
- });
- };</code></pre>
- <p>将URL传进来,便解析这个参数,接着再放到数据库中查询,再回调回结果。这样我们就可以构成之前所说的查询功能,而我们所谓的post功能似乎也可以用同样的方法加进去。</p>
- <h3 id="查询数据"><span class="header-section-number">18.4.3</span> 查询数据</h3>
- <p>简单地记录一下在IoT-CoAP中一次获取数据地过程。</p>
- <p>先看看在示例中的Get.js的代码,这关乎在后面server端的代码。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">'coap'</span>)
- ,requestURI = <span class="st">'coap://localhost/'</span>
- ,url = <span class="fu">require</span>(<span class="st">'url'</span>).<span class="fu">parse</span>(requestURI + <span class="st">'id/1/'</span>)
- ,req = <span class="ot">coap</span>.<span class="fu">request</span>(url)
- ,bl = <span class="fu">require</span>(<span class="st">'bl'</span>);
- <span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">"Accept"</span>, <span class="st">"application/json"</span>);
- <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">'response'</span>, <span class="kw">function</span>(res) {
- <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="fu">bl</span>(<span class="kw">function</span>(err, data) {
- <span class="kw">var</span> json = <span class="ot">JSON</span>.<span class="fu">parse</span>(data);
- <span class="ot">console</span>.<span class="fu">log</span>(json);
- }));
- });
- <span class="ot">req</span>.<span class="fu">end</span>();</code></pre>
- <p>const定义数据的方法,和我们在其他语言中有点像。只是这的const主要是为了程序的健壮型,减少程序出错,当然这不是javascript的用法。</p>
- <p>我们构建了一个请求的URL</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span>://localhost/id/1/</code></pre>
- <p>我们对我们的请求添加了一个Header,内容是Accept,值是’application/json’也就是JSON格式。接着,便是等待请求回来,再处理返回的内容。</p>
- <p><strong>判断请求的方法</strong></p>
- <p>在这里先把一些无关的代码删除掉,并保证其能工作,so,下面就是简要的逻辑代码。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> coap = <span class="fu">require</span>(<span class="st">'coap'</span>);
- <span class="kw">var</span> server = <span class="ot">coap</span>.<span class="fu">createServer</span>({});
- <span class="kw">var</span> request_handler = <span class="fu">require</span>(<span class="st">'./request_handler.js'</span>);
- <span class="ot">server</span>.<span class="fu">on</span>(<span class="st">'request'</span>, <span class="kw">function</span>(req, res) {
- <span class="kw">switch</span>(<span class="ot">req</span>.<span class="fu">method</span>){
- <span class="kw">case</span> <span class="st">"GET"</span>: <span class="ot">request_handler</span>.<span class="fu">getHandler</span>(req, res);
- <span class="kw">break</span>;
- }
- });
- <span class="ot">server</span>.<span class="fu">listen</span>(<span class="kw">function</span>() {
- <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">'server started'</span>);
- });</code></pre>
- <p>创建一个CoAP服务,判断req.method,也就是请求的方法,如果是GET的话,就调用request_handler.getHandler(req, res)。而在getHandler里,判断了下请求的Accept</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">request_helper</span>.<span class="fu">getHandler</span> = <span class="kw">function</span>(req, res) {
- <span class="kw">switch</span> (<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">'Accept'</span>]) {
- <span class="kw">case</span> <span class="st">"application/json"</span>:
- <span class="ot">qh</span>.<span class="fu">returnJSON</span>(req, res);
- <span class="kw">break</span>;
- <span class="kw">case</span> <span class="st">"application/xml"</span>:
- <span class="ot">qh</span>.<span class="fu">returnXML</span>(req, res);
- <span class="kw">break</span>;
- }
- };</code></pre>
- <p>如果是json刚调用returnJSON,</p>
- <p><strong>Database与回调</strong></p>
- <p>而这里为了处理回调函数刚分为了两部分</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">query_helper</span>.<span class="fu">returnJSON</span> = <span class="kw">function</span>(req, res) {
- <span class="ot">DBHelper</span>.<span class="fu">urlQueryData</span>(<span class="ot">req</span>.<span class="fu">url</span>, <span class="kw">function</span> (result) {
- <span class="ot">QueryData</span>.<span class="fu">returnJSON</span>(result, res);
- });
- };</code></pre>
- <p>而这里只是调用了</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">DBHelper</span>.<span class="fu">urlQueryData</span> = <span class="kw">function</span> (url, callback) {
- <span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(config[<span class="st">"db_name"</span>]);
- <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">"SELECT * FROM basic where "</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">1</span>] + <span class="st">"="</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">2</span>]);
- <span class="ot">db</span>.<span class="fu">all</span>(<span class="st">"SELECT * FROM basic where "</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">1</span>] + <span class="st">"="</span> + <span class="ot">url</span>.<span class="fu">split</span>(<span class="st">'/'</span>)[<span class="dv">2</span>], <span class="kw">function</span>(err, rows) {
- <span class="ot">db</span>.<span class="fu">close</span>();
- <span class="fu">callback</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>(rows));
- });
- };</code></pre>
- <p>这里调用了node sqlite3去查询对应id的数据,用回调处理了数据无法到外部的问题,而上面的returnJSON则只是返回最后的结果,code以及其他的内容。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">QueryData</span>.<span class="fu">returnJSON</span> = <span class="kw">function</span>(result, res) {
- <span class="kw">if</span> (<span class="ot">result</span>.<span class="fu">length</span> == <span class="dv">2</span>) {
- <span class="ot">res</span>.<span class="fu">code</span> = <span class="st">'4.04'</span>;
- <span class="ot">res</span>.<span class="fu">end</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>({
- <span class="dt">error</span>: <span class="st">"Not Found"</span>
- }));
- } <span class="kw">else</span> {
- <span class="ot">res</span>.<span class="fu">code</span> = <span class="st">'2.05'</span>;
- <span class="ot">res</span>.<span class="fu">end</span>(result);
- }
- };</code></pre>
- <p>当resulst的结果为空时,返回一个404,因为没有数据。这样我们就构成了整个的链,再一步步返回结果。</p>
- <p>在<a href="https://github.com/phodal/iot-coap">IoT-CoAP</a>中我们使用到了一个Block2的东西,于是便整理相关的一些资料,作一个简单的介绍,以及在代码中的使用。</p>
- <h2 id="coap-block"><span class="header-section-number">18.5</span> CoAP Block</h2>
- <p>CoAP是一个RESTful传输协议用于受限设备的节点和网络。基本的CoAP消息是一个不错的选择对于小型载荷如</p>
- <ul>
- <li>温度传感器</li>
- <li>灯光开关</li>
- <li>楼宇自动化设备</li>
- </ul>
- <p>然而,有时我们的应用需要传输更大的有效载荷,如——更新固件。与HTTP,TCP做繁重工作将大型有效载荷分成多个数据包,并确保他们所有到达并以正确的顺序被处理。</p>
- <p>CoAP是同UDP与DLTS一样是基于数据报传输的,这限制了资源表示(resource representation)的最大大小,使得传输不需要太多的分割。虽然UDP支持通过IP分片传输更大的有效载荷,且仅限于64KiB,更重要的是,并没有真正很好地约束应用和网络。</p>
- <p>而不是依赖于IP分片,这种规范基本COAP了对“块”选项,用于传输信息从多个资源区块的请求 - 响应对。在许多重要的情况下,阻止使服务器能够真正无状态:服务器可以处理每块分开传输,而无需建立连接以前的数据块传输的其他服务器端内存。</p>
- <p>综上所述,块(Block)选项提供了传送一个最小的在分块的方式更大的陈述。</p>
- <h3 id="coap-post"><span class="header-section-number">18.5.1</span> CoAP POST</h3>
- <p>看看在IoT CoAP中的post示例。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">const</span> coap = <span class="fu">require</span>(<span class="st">'coap'</span>)
- ,request = <span class="ot">coap</span>.<span class="fu">request</span>
- ,bl = <span class="fu">require</span>(<span class="st">'bl'</span>)
- ,req = <span class="fu">request</span>({<span class="dt">hostname</span>: <span class="st">'localhost'</span>,<span class="dt">port</span>:<span class="dv">5683</span>,<span class="dt">pathname</span>: <span class="st">''</span>,<span class="dt">method</span>: <span class="st">'POST'</span>});
- <span class="ot">req</span>.<span class="fu">setOption</span>(<span class="st">'Block2'</span>, [<span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">'1'</span>),<span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">"'must'"</span>), <span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">'23'</span>), <span class="kw">new</span> <span class="fu">Buffer</span>(<span class="st">'12'</span>)]);
- <span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">"Accept"</span>, <span class="st">"application/json"</span>);
- <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">'response'</span>, <span class="kw">function</span>(res) {
- <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="fu">bl</span>(<span class="kw">function</span>(err, data) {
- <span class="ot">console</span>.<span class="fu">log</span>(data);
- <span class="ot">process</span>.<span class="fu">exit</span>(<span class="dv">0</span>);
- }));
- });
- <span class="ot">req</span>.<span class="fu">end</span>();</code></pre>
- <p>Block2中一共有四个数据,相应的数据结果应该是</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">{ <span class="dt">name</span>: <span class="st">'Block2'</span>, <span class="dt">value</span>: <Buffer <span class="dv">31</span>> }
- { <span class="dt">name</span>: <span class="st">'Block2'</span>, <span class="dt">value</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="dt">name</span>: <span class="st">'Block2'</span>, <span class="dt">value</span>: <Buffer <span class="dv">32</span> <span class="dv">33</span>> }
- { <span class="dt">name</span>: <span class="st">'Block2'</span>, <span class="dt">value</span>: <Buffer <span class="dv">31</span> <span class="dv">32</span>> }</code></pre>
- <p>这是没有解析的Block2,简单地可以用</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">_</span>.<span class="fu">values</span>(e).<span class="fu">toString</span>()</code></pre>
- <p>将结果转换为</p>
- <p>Block2,1 Block2,‘must’ Block2,23 Block2,12</p>
- <p>接着按“,”分开,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">_</span>.<span class="fu">values</span>(e).<span class="fu">toString</span>().<span class="fu">split</span>(<span class="st">','</span>)[<span class="dv">1</span>]</code></pre>
- <p>就有</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">[</span> <span class="st">'1'</span>, <span class="st">'\'</span>must<span class="dt">\'</span><span class="st">', '</span>23<span class="st">', '</span>12<span class="st">' ]</span></code></pre>
- <p>便可以很愉快地将其post到数据库中了,</p>
- <p>在做IoT-CoAP的过程中只支持JSON,查阅CoAP的草稿时发现支持了诸多的Content Types。</p>
- <h3 id="coap-content-types"><span class="header-section-number">18.5.2</span> CoAP Content Types</h3>
- <p>以下文字来自谷歌翻译:</p>
- <blockquote>
- <p>互联网媒体类型是通过HTTP字符串标识,如“application/xml”。该字符串是由一个顶层的类型“applicaion”和子类型的“XML”。为了尽量减少使用这些类型的媒体类型来表示的开销消息有效载荷,COAP定义一个标识符编码方案互联网媒体类型的子集。预计这桌将可扩展标识符的值的IANA维护。内容类型选项被格式化为一个8位无符号整数。初始映射到一个合适的互联网媒体类型标识符表所示。复合型高层次类型(multipart和不支持消息)。标识符值是从201-255保留的特定于供应商的,应用程序特定的或实验使用和不由IANA。</p>
- </blockquote>
- <p>下面是HTTP的标识符及类型</p>
- <table>
- <thead>
- <tr class="header">
- <th align="left">Internet media type</th>
- <th align="left">Identifier</th>
- </tr>
- </thead>
- <tbody>
- <tr class="odd">
- <td align="left">text/plain (UTF-8)</td>
- <td align="left">0</td>
- </tr>
- <tr class="even">
- <td align="left">text/xml (UTF-8)</td>
- <td align="left">1</td>
- </tr>
- <tr class="odd">
- <td align="left">text/csv (UTF-8)</td>
- <td align="left">2</td>
- </tr>
- <tr class="even">
- <td align="left">text/html (UTF-8)</td>
- <td align="left">3</td>
- </tr>
- <tr class="odd">
- <td align="left">image/gif</td>
- <td align="left">21</td>
- </tr>
- <tr class="even">
- <td align="left">image/jpeg</td>
- <td align="left">22</td>
- </tr>
- <tr class="odd">
- <td align="left">image/png</td>
- <td align="left">23</td>
- </tr>
- <tr class="even">
- <td align="left">image/tiff</td>
- <td align="left">24</td>
- </tr>
- <tr class="odd">
- <td align="left">audio/raw</td>
- <td align="left">25</td>
- </tr>
- <tr class="even">
- <td align="left">video/raw</td>
- <td align="left">26</td>
- </tr>
- <tr class="odd">
- <td align="left">application/link-format [I-D.ietf-core-link-format]</td>
- <td align="left">40</td>
- </tr>
- <tr class="even">
- <td align="left">application/xml</td>
- <td align="left">41</td>
- </tr>
- <tr class="odd">
- <td align="left">application/octet-stream</td>
- <td align="left">42</td>
- </tr>
- <tr class="even">
- <td align="left">application/rdf+xml</td>
- <td align="left">43</td>
- </tr>
- <tr class="odd">
- <td align="left">application/soap+xml</td>
- <td align="left">44</td>
- </tr>
- <tr class="even">
- <td align="left">application/atom+xml</td>
- <td align="left">45</td>
- </tr>
- <tr class="odd">
- <td align="left">application/xmpp+xml</td>
- <td align="left">46</td>
- </tr>
- <tr class="even">
- <td align="left">application/exi</td>
- <td align="left">47</td>
- </tr>
- <tr class="odd">
- <td align="left">application/x-bxml</td>
- <td align="left">48</td>
- </tr>
- <tr class="even">
- <td align="left">application/fastinfoset</td>
- <td align="left">49</td>
- </tr>
- <tr class="odd">
- <td align="left">application/soap+fastinfoset</td>
- <td align="left">50</td>
- </tr>
- <tr class="even">
- <td align="left">application/json</td>
- <td align="left">51</td>
- </tr>
- </tbody>
- </table>
- <p>而在CoAP中只有简单地几个</p>
- <table>
- <colgroup>
- <col width="25%" />
- <col width="25%" />
- <col width="25%" />
- <col width="25%" />
- </colgroup>
- <thead>
- <tr class="header">
- <th align="left">Media type</th>
- <th align="left">Encoding</th>
- <th align="left">Id.</th>
- <th align="left">Reference</th>
- </tr>
- </thead>
- <tbody>
- <tr class="odd">
- <td align="left">text/plain;</td>
- <td align="left">-</td>
- <td align="left">0</td>
- <td align="left">[RFC2046][RFC3676][RFC5147]</td>
- </tr>
- <tr class="even">
- <td align="left">charset=utf-8</td>
- <td align="left"></td>
- <td align="left"></td>
- <td align="left"></td>
- </tr>
- <tr class="odd">
- <td align="left">application/</td>
- <td align="left">-</td>
- <td align="left">40</td>
- <td align="left">[RFC6690]</td>
- </tr>
- <tr class="even">
- <td align="left">link-format</td>
- <td align="left"></td>
- <td align="left"></td>
- <td align="left"></td>
- </tr>
- <tr class="odd">
- <td align="left">application/xml</td>
- <td align="left">-</td>
- <td align="left">41</td>
- <td align="left">[RFC3023]</td>
- </tr>
- <tr class="even">
- <td align="left">application/</td>
- <td align="left">-</td>
- <td align="left">42</td>
- <td align="left">[RFC2045][RFC2046]</td>
- </tr>
- <tr class="odd">
- <td align="left">octet-stream</td>
- <td align="left"></td>
- <td align="left"></td>
- <td align="left"></td>
- </tr>
- <tr class="even">
- <td align="left">application/exi</td>
- <td align="left">-</td>
- <td align="left">47</td>
- <td align="left">[EXIMIME]</td>
- </tr>
- <tr class="odd">
- <td align="left">application/json</td>
- <td align="left">-</td>
- <td align="left">50</td>
- <td align="left">[RFC4627]</td>
- </tr>
- </tbody>
- </table>
- <p>简单地说就是:</p>
- <p><code>诸如application/json的Content Types在CoAP中应该是50</code>。如上表所示的结果是其对应的结果,这样的话可以减少传递的信息量。</p>
- <h2 id="coap-json"><span class="header-section-number">18.6</span> CoAP JSON</h2>
- <p>于是在一开始的时候首先支持的便是“application/json”这样的类型。</p>
- <p>首先判断请求的header</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">request_helper</span>.<span class="fu">getHandler</span> = <span class="kw">function</span>(req, res) {
- <span class="kw">switch</span> (<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">'Accept'</span>]) {
- <span class="kw">case</span> <span class="st">"application/json"</span>:
- <span class="ot">qh</span>.<span class="fu">returnJSON</span>(req, res);
- <span class="kw">break</span>;
- <span class="kw">case</span> <span class="st">"application/xml"</span>:
- <span class="ot">qh</span>.<span class="fu">returnXML</span>(req, res);
- <span class="kw">break</span>;
- }
- };</code></pre>
- <p>再转至相应的函数处理,而判断的依据则是Accept是不是“application/json”。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="fu">registerFormat</span>(<span class="st">'text/plain'</span>, <span class="dv">0</span>)
- <span class="fu">registerFormat</span>(<span class="st">'application/link-format'</span>, <span class="dv">40</span>)
- <span class="fu">registerFormat</span>(<span class="st">'application/xml'</span>, <span class="dv">41</span>)
- <span class="fu">registerFormat</span>(<span class="st">'application/octet-stream'</span>, <span class="dv">42</span>)
- <span class="fu">registerFormat</span>(<span class="st">'application/exi'</span>, <span class="dv">47</span>)
- <span class="fu">registerFormat</span>(<span class="st">'application/json'</span>, <span class="dv">50</span>)</code></pre>
- <p>对应地我们需要在一发出请求的时候设置好Accept,要不就没有办法返回我们需要的结果。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">"Accept"</span>, <span class="st">"application/json"</span>);</code></pre>
- <p><strong>返回JSON</strong></p>
- <p>在给IoT CoAP添加了JSON支持之后,变得非常有意思,至少我们可以获得我们想要的结果。在上一篇中我们介绍了一些常用的工具——<a href="http://www.phodal.com/blog/coap-command-line-tools-set/">CoAP 命令行工具集</a>。</p>
- <p><strong>CoAP客户端代码</strong></p>
- <p>开始之前我们需要有一个客户端代码,以便我们的服务端可以返回正确的数据并解析</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> coap = <span class="fu">require</span>(<span class="st">'coap'</span>);
- <span class="kw">var</span> requestURI = <span class="st">'coap://localhost/'</span>;
- <span class="kw">var</span> url = <span class="fu">require</span>(<span class="st">'url'</span>).<span class="fu">parse</span>(requestURI + <span class="st">'id/1/'</span>);
- <span class="ot">console</span>.<span class="fu">log</span>(<span class="st">"Request URL: "</span> + <span class="ot">url</span>.<span class="fu">href</span>);
- <span class="kw">var</span> req = <span class="ot">coap</span>.<span class="fu">request</span>(url);
- <span class="kw">var</span> bl = <span class="fu">require</span>(<span class="st">'bl'</span>);
- <span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">"Accept"</span>, <span class="st">"application/json"</span>);
- <span class="ot">req</span>.<span class="fu">on</span>(<span class="st">'response'</span>, <span class="kw">function</span>(res) {
- <span class="ot">res</span>.<span class="fu">pipe</span>(<span class="fu">bl</span>(<span class="kw">function</span>(err, data) {
- <span class="kw">var</span> json = <span class="ot">JSON</span>.<span class="fu">parse</span>(data);
- <span class="ot">console</span>.<span class="fu">log</span>(json);
- }));
- });
- <span class="ot">req</span>.<span class="fu">end</span>();</code></pre>
- <p>代码有点长内容也有点多,但是核心是这句话:</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">req</span>.<span class="fu">setHeader</span>(<span class="st">"Accept"</span>, <span class="st">"application/json"</span>);</code></pre>
- <p>这样的话,我们只需要在我们的服务端一判断,</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">if</span>(<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">'Accept'</span>] == <span class="st">'application/json'</span>) {
- <span class="co">//do something</span>
- };</code></pre>
- <p>这样就可以返回数据了</p>
- <p><strong>CoAP Server端代码</strong></p>
- <p>Server端的代码比较简单,判断一下</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">if</span> (<span class="ot">req</span>.<span class="fu">headers</span>[<span class="st">'Accept'</span>] == <span class="st">'application/json'</span>) {
- <span class="fu">parse_url</span>(<span class="ot">req</span>.<span class="fu">url</span>, <span class="kw">function</span>(result){
- <span class="ot">res</span>.<span class="fu">end</span>(result);
- });
- <span class="ot">res</span>.<span class="fu">code</span> = <span class="st">'2.05'</span>;
- }</code></pre>
- <p>请求的是否是JSON格式,再返回一个205,也就是Content,只是这时设计是请求一个URL返回对应的数据。如</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span>://localhost/id/1/</code></pre>
- <p>这时应该请求的是ID为1的数据,即</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript">[ { <span class="dt">id</span>: <span class="dv">1</span>, <span class="dt">value</span>: <span class="st">'is id 1'</span>, <span class="dt">sensors1</span>: <span class="dv">19</span>, <span class="dt">sensors2</span>: <span class="dv">20</span> }]</code></pre>
- <p>而parse_url只是从数据库从读取相应的数据。</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">function</span> <span class="fu">parse_url</span>(url ,callback) {
- <span class="kw">var</span> db = <span class="kw">new</span> <span class="ot">sqlite3</span>.<span class="fu">Database</span>(config[<span class="st">"db_name"</span>]);
- <span class="kw">var</span> result = [];
- <span class="ot">db</span>.<span class="fu">all</span>(<span class="st">"SELECT * FROM basic;"</span>, <span class="kw">function</span>(err, rows) {
- <span class="fu">callback</span>(<span class="ot">JSON</span>.<span class="fu">stringify</span>(rows));
- })
- }</code></pre>
- <p>并且全部都显示出来,设计得真是有点不行,不过现在已经差不多了。</p>
- <h2 id="使用iot-coap构建物联网"><span class="header-section-number">18.7</span> 使用IoT-CoAP构建物联网</h2>
- <p>(<code>注意</code>:windows系统npm install失败时,需要自己建立一个C:and Settings[USERNAME]Data文件)</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">npm</span> install iot-coap</code></pre>
- <p>1.新建<strong>index.js</strong></p>
- <p><code>注意</code>: 如果已经存在一个index.js文件,请将下面内容添加到文件末尾(create index.js, and add)</p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="kw">var</span> iotcoap = <span class="fu">require</span>(<span class="st">'iot-coap'</span>);
- <span class="ot">iotcoap</span>.<span class="fu">run</span>();
- <span class="ot">iotcoap</span>.<span class="ot">rest</span>.<span class="fu">run</span>();</code></pre>
- <p><code>注意</code>:在db配置可以选择mongodb和sqlite3,替换所需要的数据库即可。(you can choice db on iot.js with ‘sqlite’ or ‘mongodb’)</p>
- <p>2.创建<strong>iot.js</strong></p>
- <pre class="sourceCode javascript"><code class="sourceCode javascript"><span class="ot">exports</span>.<span class="fu">config</span> = {
- <span class="st">"db_name"</span>: <span class="st">"iot.db"</span>,
- <span class="st">"mongodb_name"</span>: <span class="st">"iot"</span>,
- <span class="st">"mongodb_documents"</span>: <span class="st">"iot"</span>,
- <span class="st">"db"</span>: <span class="st">"mongodb"</span>,
- <span class="st">"table_name"</span>: <span class="st">"basic"</span>,
- <span class="st">"keys"</span>:[
- <span class="st">"id"</span>,
- <span class="st">"value"</span>,
- <span class="st">"sensors1"</span>,
- <span class="st">"sensors2"</span>
- ],
- <span class="st">"db_table"</span>: <span class="st">"id integer primary key, value text, sensors1 float, sensors2 float"</span>,
- <span class="st">"mongodb_init"</span>:[
- {
- <span class="dt">id</span>: <span class="dv">1</span>,
- <span class="dt">value</span>: <span class="st">"is id 1"</span>,
- <span class="dt">sensors1</span>: <span class="dv">19</span>,
- <span class="dt">sensors2</span>: <span class="dv">20</span>
- },
- {
- <span class="dt">id</span>: <span class="dv">2</span>,
- <span class="dt">value</span>: <span class="st">"is id 2"</span>,
- <span class="dt">sensors1</span>: <span class="dv">20</span>,
- <span class="dt">sensors2</span>: <span class="dv">21</span>
- }
- ],
- <span class="st">"init_table"</span>:[
- <span class="st">"insert or replace into basic (id,value,sensors1,sensors2) VALUES (1, 'is id 1', 19, 20);"</span>,
- <span class="st">"insert or replace into basic (id,value,sensors1,sensors2) VALUES (2, 'is id 2', 20, 21);"</span>
- ],
- <span class="st">"query_table"</span>:<span class="st">"select * from basic;"</span>,
- <span class="st">"rest_url"</span>: <span class="st">"/id/:id"</span>,
- <span class="st">"rest_post_url"</span>: <span class="st">"/"</span>,
- <span class="st">"rest_port"</span>: <span class="dv">8848</span>
- };</code></pre>
- <p>3.运行(run)</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">node</span> index.js</code></pre>
- <p>show:</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">coap</span> listening at coap://0.0.0.0:5683
- <span class="kw">restify</span> listening at http://0.0.0.0:8848</code></pre>
- <h1 id="简单物联网"><span class="header-section-number">19</span> 简单物联网</h1>
- <p>到这时,我们算搭建了一个简单的REST服务了。接着我们可以简单的做一个最小的物联网系统,将我们的单片机、MCU等等连上网。</p>
- <div class="figure">
- <img src="./images/arch.jpeg" alt="硬件结构图" />
- <p>硬件结构图</p>
- </div>
- <p>考虑到如果我们只是单一连接各个节点,那么系统的结构图,同下所示</p>
- <div class="figure">
- <img src="./images/fullconnected.png" alt="全连接" />
- <p>全连接</p>
- </div>
- <p>下面的星形结构图类似于我们在接下来所要构建的系统</p>
- <div class="figure">
- <img src="./images/star.png" alt="星形结构图" />
- <p>星形结构图</p>
- </div>
- <p>一个用于控制真实电器的硬件实物图</p>
- <div class="figure">
- <img src="./images/hardware.jpg" alt="简单实物图" />
- <p>简单实物图</p>
- </div>
- <h2 id="硬件通信"><span class="header-section-number">19.1</span> 硬件通信</h2>
- <h3 id="串口通信-1"><span class="header-section-number">19.1.1</span> 串口通信</h3>
- <p>Arduino与Raspberry Pi通过串口通信的方式实现通信,相互传输所需要的数据,Raspberry Pi将资源传于互联网上对应的接口,接口可以在互联网上被访问。Laravel框架构架于服务器之上,将Raspbery Pi获取过来的数据存储于MySQL数据,再以REST服务的方式共享数据,互联网上的其他设备便可以通过网络来访问这些设备。Ajax用于将后台的数据以不需要刷新的方式传递到网站前台,通过HighCharts框架显示给终端用户。</p>
- <h4 id="python"><span class="header-section-number">19.1.1.1</span> Python</h4>
- <p>1.在Windows中的串口通常是<code>COM1</code>,<code>COM0</code>等等</p>
- <pre class="sourceCode python"><code class="sourceCode python">ser=serial.Serial(<span class="st">"COM0"</span>,<span class="dv">9600</span>)</code></pre>
- <p>2.Mac OS系统中位于/dev目录下,名字类似于<code>tty.usbmodem1451</code>。</p>
- <pre class="sourceCode python"><code class="sourceCode python"> serial.Serial(<span class="st">"/dev/tty.usbmodem1451"</span>,<span class="dv">9600</span>)</code></pre>
- <p>3.在Linux内核的系统中虚拟串口用的节点是ttyACM,位于/dev目录下。</p>
- <pre class="sourceCode python"><code class="sourceCode python"> serial.Serial(<span class="st">"/dev/ttyACM0"</span>,<span class="dv">9600</span>)</code></pre>
- <blockquote>
- <p>串行接口是一种可以将接受来自CPU的并行数据字符转换为连续的串行数据流发送出去,同时可将接受的串行数据流转换为并行的数据字符供给CPU的器件。一般完成这种功能 的电路,我们称为串行接口电路。</p>
- </blockquote>
- <p>便是打开这个设备,以9600的速率传输数据。</p>
- <p>程序框架如下所示:</p>
- <div class="figure">
- <img src="./images/raspberrypi_flow.png" alt="Raspberry Pi" />
- <p>Raspberry Pi</p>
- </div>
- <p>代码如下:</p>
- <pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> json
- <span class="ch">import</span> urllib2
- <span class="ch">import</span> serial
- <span class="ch">import</span> time
- url=<span class="st">"http://www.xianuniversity.com/athome/1"</span>
- <span class="kw">while</span> <span class="dv">1</span>:
- <span class="kw">try</span>:
- date=urllib2.urlopen(url)
- result=json.load(date)
- status=result[<span class="dv">0</span>][<span class="st">"led1"</span>]
- ser=serial.Serial(<span class="st">"/dev/ttyACM0"</span>,<span class="dv">9600</span>)
- <span class="kw">if</span> status==<span class="dv">1</span> :
- ser.write(<span class="st">"1"</span>)
- <span class="kw">elif</span> status==<span class="dv">0</span>:
- ser.write(<span class="st">"0"</span>)
- time.sleep(<span class="dv">1</span>)
- <span class="kw">except</span> urllib2.URLError:
- <span class="dt">print</span> <span class="st">"Bad URL or timeout"</span></code></pre>
- <div class="figure">
- <img src="./images/getjson.png" alt="python返回json数据" />
- <p>python返回json数据</p>
- </div>
- <p>系统还需要对上面的数据进行处理,只拿其中的结果</p>
- <div class="figure">
- <img src="./images/origin.png" alt="python处理完后的结果" />
- <p>python处理完后的结果</p>
- </div>
- <p>当改变led的状态后,便可以得到下面的结果</p>
- <div class="figure">
- <img src="./images/change.png" alt="改变状态后的结果" />
- <p>改变状态后的结果</p>
- </div>
- <h4 id="ruby"><span class="header-section-number">19.1.1.2</span> Ruby</h4>
- <p>如果你用的是Ruby的话,可以尝试使用<code>serialport</code></p>
- <p>安装</p>
- <pre class="sourceCode bash"><code class="sourceCode bash"><span class="kw">sudo</span> gem install serialport</code></pre>
- <p>代码大致如下</p>
- <pre class="sourceCode ruby"><code class="sourceCode ruby">require <span class="st">'serialport'</span>
- sp = <span class="dt">SerialPort</span>.new <span class="st">"/dev/ACM0"</span>, <span class="dv">9600</span>
- sp.write <span class="st">"1"</span></code></pre>
- <p>注意: 根据相关的系统修改相关的代码。</p>
- <h2 id="硬件"><span class="header-section-number">19.2</span> 硬件</h2>
- <h3 id="arduino"><span class="header-section-number">19.2.1</span> Arduino</h3>
- <p>这样我们在我们的Arduino上所要做的便是,读取串口的结果并控制IO口。</p>
- <pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> ledPort=<span class="dv">13</span>;
- <span class="dt">void</span> setup() {
- Serial.begin(<span class="dv">9600</span>);
- pinMode(ledPort,OUTPUT);
- }
- <span class="dt">int</span> serialData;
- <span class="dt">void</span> loop() {
- String inString = <span class="st">""</span>;
- <span class="kw">while</span> (Serial.available()> <span class="dv">0</span>)
- {
- <span class="dt">int</span> inChar = Serial.read();
- <span class="kw">if</span> (isDigit(inChar)) {
- inString += (<span class="dt">char</span>)inChar;
- }
- serialData=inString.toInt();
- Serial.print(serialData);
- }
- <span class="kw">if</span>(serialData==<span class="dv">1</span>){
- digitalWrite(ledPort,HIGH);
- }<span class="kw">else</span>{
- digitalWrite(ledPort,LOW);
- }
- }</code></pre>
- <p>如果结果是1的话,就让13口为高电平,也就是让灯亮起来。</p>
- <h3 id="继电器"><span class="header-section-number">19.2.2</span> 继电器</h3>
- <blockquote>
- <p>继电器(英文名称:relay)是一种电控制器件,是当输入量(激励量)的变化达到规定要求时,在电气输出电路中使被控量发生预定的阶跃变化的一种电器。它具有控制系统(又称输入回路)和被控制系统(又称输出回路)之间的互动关系。通常应用于自动化的控制电路中,它实际上是用小电流去控制大电流运作的一种“自动开关”。故在电路中起着自动调节、安全保护、转换电路等作用。</p>
- </blockquote>
- <p>在这里我们可以默认为我们想要为单片机的5V电压控制220V的电器。</p>
- <p>最后我们便可以通过些来控制灯的开和关。</p>
- <h1 id="android简单示例"><span class="header-section-number">20</span> Android简单示例</h1>
- <p>由于在某些嵌入式系统中使用的是Android系统,这里给出一个简单的Android App的示例,具体代码可以从clone自<a href="https://github.com/phodal/iot-android" class="uri">https://github.com/phodal/iot-android</a></p>
- <p>代码说明,经过测试的版本有</p>
- <ul>
- <li>Android 2.3</li>
- <li>Android 4.0.4</li>
- </ul>
- <p>机型有</p>
- <ul>
- <li>HTC G1 (android 2.3)</li>
- <li>Motor xt300 (android 2.3)</li>
- <li>Sony ST25I (android 4.0.4)</li>
- <li>MI2</li>
- </ul>
- <p>应该可以在大部分的手机上工作。</p>
- <h2 id="调用web-services-get"><span class="header-section-number">20.1</span> 调用Web Services GET</h2>
- <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>
- <h3 id="创建restclient"><span class="header-section-number">20.1.1</span> 创建RESTClient</h3>
- <p>在这里我们首先会定义四个REST方法GET、POST、PUT、DELETE</p>
- <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 {
- <span class="kw">switch</span> (method) {
- <span class="kw">case</span> GET: {
- <span class="co">// add parameters</span>
- String combinedParams = <span class="st">""</span>;
- <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
- combinedParams += <span class="st">"?"</span>;
- <span class="kw">for</span> (NameValuePair p : params) {
- String paramString = p.<span class="fu">getName</span>() + <span class="st">"="</span>
- + URLEncoder.<span class="fu">encode</span>(p.<span class="fu">getValue</span>(), HTTP.<span class="fu">UTF_8</span>);
- <span class="kw">if</span> (combinedParams.<span class="fu">length</span>() > <span class="dv">1</span>) {
- combinedParams += <span class="st">"&"</span> + paramString;
- } <span class="kw">else</span> {
- combinedParams += paramString;
- }
- }
- }
- HttpGet request = <span class="kw">new</span> <span class="fu">HttpGet</span>(url + combinedParams);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- <span class="kw">case</span> POST: {
- HttpPost request = <span class="kw">new</span> <span class="fu">HttpPost</span>(url);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">""</span>)) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- <span class="kw">case</span> PUT: {
- HttpPut request = <span class="kw">new</span> <span class="fu">HttpPut</span>(url);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="kw">if</span> (!data.<span class="fu">equals</span>(<span class="st">""</span>)) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">StringEntity</span>(data, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="kw">if</span> (!params.<span class="fu">isEmpty</span>()) {
- request.<span class="fu">setEntity</span>(<span class="kw">new</span> <span class="fu">UrlEncodedFormEntity</span>(params, HTTP.<span class="fu">UTF_8</span>));
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- <span class="kw">case</span> DELETE: {
- HttpDelete request = <span class="kw">new</span> <span class="fu">HttpDelete</span>(url);
- request.<span class="fu">addHeader</span>(<span class="st">"Accept-Encoding"</span>, <span class="st">"gzip"</span>);
- <span class="co">// add headers</span>
- <span class="kw">for</span> (NameValuePair h : headers) {
- request.<span class="fu">addHeader</span>(h.<span class="fu">getName</span>(), h.<span class="fu">getValue</span>());
- }
- <span class="fu">executeRequest</span>(request, url);
- <span class="kw">break</span>;
- }
- }
- }</code></pre>
- <p>这四个方法最后都执行executeRequest来获取响应结果。</p>
- <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) {
- HttpParams httpParameters = <span class="kw">new</span> <span class="fu">BasicHttpParams</span>();
- HttpConnectionParams.<span class="fu">setConnectionTimeout</span>(httpParameters,
- timeoutConnection);
- HttpConnectionParams.<span class="fu">setSoTimeout</span>(httpParameters, timeoutSocket);
-
- HttpProtocolParams.<span class="fu">setUseExpectContinue</span>(httpParameters, <span class="kw">false</span>);
- request.<span class="fu">setParams</span>(httpParameters);
-
- <span class="fu">setOauth</span>(request);
-
- DefaultHttpClient client = <span class="kw">new</span> <span class="fu">DefaultHttpClient</span>();
- HttpResponse httpResponse;
- <span class="kw">try</span> {
- httpResponse = client.<span class="fu">execute</span>(request);
- responseCode = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getStatusCode</span>();
- message = httpResponse.<span class="fu">getStatusLine</span>().<span class="fu">getReasonPhrase</span>();
- HttpEntity entity = httpResponse.<span class="fu">getEntity</span>();
- <span class="kw">if</span> (entity != <span class="kw">null</span>) {
- InputStream instream = httpResponse.<span class="fu">getEntity</span>().<span class="fu">getContent</span>();
- Header contentEncoding = httpResponse
- .<span class="fu">getFirstHeader</span>(<span class="st">"Content-Encoding"</span>);
- <span class="kw">if</span> (contentEncoding != <span class="kw">null</span>
- && contentEncoding.<span class="fu">getValue</span>().<span class="fu">equalsIgnoreCase</span>(<span class="st">"gzip"</span>)) {
- instream = <span class="kw">new</span> GZIPInputStream(instream);
- }
- <span class="co">// instream = entity.getContent();</span>
- response = <span class="fu">convertStreamToString</span>(instream);
- <span class="co">// Closing the input stream will trigger connection release</span>
- instream.<span class="fu">close</span>();
- }
- } <span class="kw">catch</span> (ClientProtocolException e) {
- client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
- e.<span class="fu">printStackTrace</span>();
- } <span class="kw">catch</span> (IOException e) {
- client.<span class="fu">getConnectionManager</span>().<span class="fu">shutdown</span>();
- e.<span class="fu">printStackTrace</span>();
- }
- }</code></pre>
- <p>接着,我们便可以执行getResponse()函数来获取结果。</p>
- <h2 id="使用rest-client获取结果"><span class="header-section-number">20.2</span> 使用REST Client获取结果</h2>
- <p>使用RESTClient时,便可以用下面的示例</p>
- <pre class="sourceCode java"><code class="sourceCode java">RestClient client = <span class="kw">new</span> <span class="fu">RestClient</span>(tUrl);
- <span class="kw">try</span> {
- client.<span class="fu">Execute</span>(RequestMethod.<span class="fu">GET</span>);
- <span class="kw">if</span> (client.<span class="fu">getResponseCode</span>() != <span class="dv">200</span>) {
- <span class="co">//do something</span>
- }
- <span class="co">//JSONArray jArray = new JSONArray(client.getResponse());</span>
- } <span class="kw">catch</span> (Exception e) {
- <span class="co">//do something</span>
- }</code></pre>
- <p>而这时,我们只需要对相应的数据进行处理就可以了,如</p>
- <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>());
- JSONObject jObj=jArray.<span class="fu">getJSONObject</span>(<span class="dv">0</span>);
- vshow.<span class="fu">setText</span>(jObj.<span class="fu">toString</span>());
- <span class="fu">outputJSON</span>(jObj);</code></pre>
- <p>将他转换为String,接着在Android端上显示最后的结果。</p>
- <h1 id="尾声"><span class="header-section-number">21</span> 尾声</h1>
- <h2 id="路"><span class="header-section-number">21.1</span> 路</h2>
- <h2 id="其他-2"><span class="header-section-number">21.2</span> 其他</h2>
- <p>意见及建议: <a href="https://github.com/phodal/designiot/issues" class="uri">https://github.com/phodal/designiot/issues</a></p>
- <p>邮箱: <a href="h@phodal.com" class="uri">h@phodal.com</a></p>
- <div class="footnotes">
- <hr />
- <ol>
- <li><div id="fn1">
- </div>
- <p>https://zh.opensuse.org/index.php?title=%E8%BD%AF%E4%BB%B6%E5%8C%85%E7%AE%A1%E7%90%86&variant=zh<a href="#fnref1">↩</a></p></li>
- <li><div id="fn2">
- </div>
- <p>形如http://www.phodal.com<a href="#fnref2">↩</a></p></li>
- <li><div id="fn3">
- </div>
- <p>URL 是 URI 的子集<a href="#fnref3">↩</a></p></li>
- <li><div id="fn4">
- </div>
- <p>curl是利用URL语法在命令行方式下工作的开源文件传输工具。<a href="#fnref4">↩</a></p></li>
- </ol>
- </div>
|