appweb_handle.c 239 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861
  1. #include "cJSON.h"
  2. #include "common.h"
  3. #include "json_handle.h"
  4. #include "sqlite_handle.h"
  5. #include "cJSON.h"
  6. #include "switch_ctrl.h"
  7. #include "sys.h"
  8. #include "thread.h"
  9. #include "cfg.h"
  10. #include "cascade.h"
  11. #include "upg.h"
  12. #include "lock.h"
  13. #include "file.h"
  14. #include "paras.h"
  15. #include "mongoose.h"
  16. #include "language_common.h"
  17. #include "websocket_handle.h"
  18. #include "breaker_detection.h"
  19. #define TOTAL_COUNT 60
  20. #define FN_MAX 30
  21. enum {
  22. FILE_IMPORT=0,
  23. FILE_EXPORT,
  24. };
  25. enum {
  26. FILE_SYS_CFG=0,
  27. FILE_SERV_CFG,
  28. FILE_ALARM_CFG,
  29. FILE_SENSOR_CFG,
  30. FILE_POWER_ALL,
  31. FILE_POWER_CHN,
  32. FILE_LOG_RUN,
  33. FILE_LOG_OPERA,
  34. FILE_LOG_ALARM,
  35. FILE_LOGO,
  36. FILE_HOST_FW,
  37. };
  38. typedef void (*http_fn_t)(void *arg);
  39. typedef struct mg_tls_opts mg_tls_t;
  40. typedef struct {
  41. struct mg_connection *c;
  42. int ev;
  43. void *ev_data;
  44. }mg_stream_t;
  45. typedef struct {
  46. http_fn_t f;
  47. char *uri;
  48. }mg_fn_t;
  49. typedef struct {
  50. int cnt;
  51. mg_fn_t fn[FN_MAX];
  52. char rootDir[MG_PATH_MAX];
  53. mg_tls_t tls;
  54. pthread_t tid;
  55. }mg_handle_t;
  56. typedef struct {
  57. _OverAllPwrAckInfo all;
  58. _OverAllPwrAckInfo chn;
  59. }status_data_t;
  60. typedef struct {
  61. mg_handle_t mg;
  62. pthread_t tid;
  63. status_data_t stt;
  64. }web_handle_t;
  65. static char* g_product_board_cpu[] = {
  66. "xxx",
  67. "T113"
  68. };
  69. static char* g_product_type_str[] = {
  70. "SmartPDU(DC)",
  71. "SmartPDU(AC)",
  72. "SmartPDU(Tree_AC_Tree)",
  73. "SmartPDU(Tree_AC_One)",
  74. };
  75. static char* g_product_pwr_type_str[] = {
  76. "xxx",
  77. "直流",
  78. "交流单相",
  79. "交流三相三出",
  80. "交流三相单出",
  81. };
  82. static web_handle_t webHandle;
  83. static int webSetCallback(void);
  84. ///////////////////////////////////////////////////
  85. static int upload_file(void *conn, char *path)
  86. {
  87. int r=-1,flag=0;
  88. char name[200];
  89. struct mg_http_part part;
  90. mg_stream_t *s=(mg_stream_t*)conn;
  91. size_t pos = 0, total_bytes = 0;
  92. struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
  93. memset(name, 0, sizeof(name));
  94. while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
  95. memcpy(name, part.filename.buf, part.filename.len);
  96. printf("filename: %s length: %lu bytes\n", name, (unsigned long) part.body.len);
  97. if(flag==0) {
  98. file_remove(path); flag = 1;
  99. }
  100. r = file_append(path, part.body.buf, part.body.len);
  101. if (r==0) {
  102. total_bytes += part.body.len;
  103. }
  104. }
  105. printf("___ upload file to %s %d\n", path, total_bytes);
  106. return r;
  107. }
  108. static int download_file(void *conn, char *fpath)
  109. {
  110. mg_stream_t *s=(mg_stream_t*)conn;
  111. struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
  112. struct mg_http_serve_opts opts={.root_dir=NULL};
  113. mg_http_serve_file2(s->c, hm, fpath, &opts);
  114. return 0;
  115. }
  116. static int http_reply(void *conn, int status, char *ack)
  117. {
  118. int r=0;
  119. mg_stream_t *s=(mg_stream_t*)conn;
  120. r = mg_http_reply2(s->c, status, "", ack);
  121. return r;
  122. }
  123. static void httpAddCallback(char *uri, http_fn_t f)
  124. {
  125. mg_handle_t *h=&webHandle.mg;
  126. if(h->cnt<FN_MAX-1) {
  127. h->fn[h->cnt].uri = uri;
  128. h->fn[h->cnt].f = f;
  129. h->cnt++;
  130. }
  131. }
  132. static time_t gettime(char *timestr)
  133. {
  134. int r;
  135. struct tm tm={0};
  136. //2024-10-29 05:19:09
  137. r = sscanf(timestr, "%d-%d-%d %d:%d:%d", &tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
  138. if(r<6) {
  139. return -1;
  140. }
  141. tm.tm_year -= 1900;
  142. tm.tm_mon -= 1;
  143. return mktime(&tm);
  144. }
  145. static int gettime2(char *buf, char *tok, char *timestr)
  146. {
  147. char *p=strstr(buf, tok);
  148. if(!p) {
  149. return -1;
  150. }
  151. p += strlen(tok);
  152. memcpy(timestr, p, 10); timestr[10] = ' ';
  153. memcpy(timestr+11, p+13, 8); timestr[19] = 0;
  154. return 0;
  155. }
  156. static int get_range(time_t *from, time_t *to, char *start, char *end, int max_days)
  157. {
  158. time_t cur=gettime(start);
  159. time_t pre=gettime(end);
  160. time_t sss=mktime0(),secs=3600*24*max_days;
  161. if(cur<=sss) {
  162. cur = sss;
  163. }
  164. if(pre<=sss) {
  165. pre = sss;
  166. }
  167. if(abs(cur-pre)>=secs) {
  168. if(cur>pre) {
  169. *from = cur-secs;
  170. *to = cur;
  171. }
  172. else {
  173. *from = pre-secs;
  174. *to = pre;
  175. }
  176. }
  177. else {
  178. if(cur>pre) {
  179. *from = pre;
  180. *to = cur;
  181. }
  182. else {
  183. *from = cur;
  184. *to = pre;
  185. }
  186. }
  187. return 0;
  188. }
  189. static int power_export(char *path, char *start, char *end)
  190. {
  191. int r;
  192. time_t from,to;
  193. char tmp1[100],tmp2[100];
  194. GlobalDeviceManager *dm=&__globalDeviceManage;
  195. get_range(&from, &to, start, end, 7);
  196. get_time_str2(tmp1,sizeof(tmp1),from);
  197. get_time_str2(tmp2,sizeof(tmp2),to);
  198. sprintf(path, "/tmp/power_%s_%s.csv", tmp1, tmp2);
  199. return dev_power_xport(from, to, path);
  200. }
  201. static int file_io(void *conn, int ftype, int io, char *buf)
  202. {
  203. int r,flag=0;
  204. char tmp[200],path[200];
  205. web_handle_t *wh=&webHandle;
  206. mg_stream_t *s=(mg_stream_t*)conn;
  207. struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
  208. switch(ftype) {
  209. case FILE_SYS_CFG:
  210. {
  211. sprintf(tmp, "/tmp/config.ini");
  212. sys_get_path(path, INI_FILE_NAME);
  213. if(io==FILE_IMPORT) {
  214. r = upload_file(conn, tmp);
  215. if(r==0) {
  216. r = paras_check(tmp);
  217. if(r==0) {
  218. paras_copy(path, tmp);
  219. file_remove(tmp);
  220. //clear some database table
  221. exit(0); //restart smartPDU
  222. }
  223. file_remove(tmp);
  224. }
  225. }
  226. else {
  227. file_copy(tmp, path);
  228. download_file(conn, tmp);
  229. file_remove(tmp);
  230. }
  231. }
  232. break;
  233. case FILE_SERV_CFG:
  234. {
  235. sprintf(path, "/tmp/service.ini");
  236. if(io==FILE_IMPORT) {
  237. r = upload_file(conn, path);
  238. if(r==0) {
  239. //thread_stop(THREAD_ID_POWER);
  240. r = paras_serv_xport(path, PARAS_IMPORT);
  241. file_remove(path);
  242. if(r==0) exit(0);
  243. //thread_start2(THREAD_ID_POWER);
  244. }
  245. }
  246. else {
  247. r = paras_serv_xport(path, PARAS_EXPORT);
  248. if(r==0) {
  249. r = download_file(conn, path);
  250. }
  251. file_remove(path);
  252. }
  253. }
  254. break;
  255. case FILE_ALARM_CFG:
  256. {
  257. sprintf(path, "/tmp/alarm.ini");
  258. if(io==FILE_IMPORT) {
  259. r = upload_file(conn, path);
  260. if(r==0) {
  261. r = paras_alarm_xport(path, PARAS_IMPORT);
  262. file_remove(path);
  263. if(r==0) exit(0);
  264. }
  265. }
  266. else {
  267. r = paras_alarm_xport(path, PARAS_EXPORT);
  268. if(r==0) {
  269. r = download_file(conn, path);
  270. file_remove(path);
  271. }
  272. }
  273. }
  274. break;
  275. case FILE_SENSOR_CFG:
  276. {
  277. sprintf(path, "/tmp/sensor.ini");
  278. if(io==FILE_IMPORT) {
  279. r = upload_file(conn, path);
  280. if(r==0) {
  281. r = paras_sensor_xport(path, PARAS_IMPORT);
  282. file_remove(path);
  283. if(r==0) exit(0);
  284. }
  285. }
  286. else {
  287. r = paras_sensor_xport(path, PARAS_EXPORT);
  288. if(r==0) {
  289. r = download_file(conn, path);
  290. file_remove(path);
  291. }
  292. }
  293. }
  294. break;
  295. case FILE_POWER_ALL:
  296. case FILE_POWER_CHN:
  297. {
  298. if(io==FILE_EXPORT) {
  299. char start[100],end[100];
  300. gettime2(buf, "startTime=", start);
  301. gettime2(buf, "endTime=", end);
  302. r = power_export(path, start, end);
  303. if(r==0) {
  304. download_file(conn, path);
  305. file_remove(path);
  306. }
  307. }
  308. }
  309. break;
  310. case FILE_LOG_RUN:
  311. {
  312. if(io==FILE_EXPORT) {
  313. //download_file(path);
  314. }
  315. }
  316. break;
  317. case FILE_LOG_OPERA:
  318. {
  319. if(io==FILE_EXPORT) {
  320. //download_file(path);
  321. }
  322. }
  323. break;
  324. case FILE_LOG_ALARM:
  325. {
  326. if(io==FILE_EXPORT) {
  327. //download_file(path);
  328. }
  329. }
  330. break;
  331. case FILE_LOGO:
  332. {
  333. if(io==FILE_IMPORT) {
  334. char tmp[100];
  335. sprintf(tmp, "%s/%s", IMAGE_FOLDER, LOGO_FILE);
  336. sys_get_path(path, tmp);
  337. upload_file(conn, path);
  338. }
  339. }
  340. break;
  341. case FILE_HOST_FW:
  342. {
  343. if(io==FILE_IMPORT) {
  344. sprintf(path, "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  345. r = upload_file(conn, path);
  346. if(r==0) {
  347. printf("_____ upg_start\n");
  348. upg_host_send(UPGRADE_EVT_UPLOAD_FINISH);
  349. }
  350. }
  351. }
  352. break;
  353. default:
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. /////////////////////////////////////////////////////////////////////////
  359. static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
  360. {
  361. int i,j,flag=0;
  362. mg_handle_t *h=&webHandle.mg;
  363. mg_stream_t s={c,ev,ev_data};
  364. if (ev == MG_EV_HTTP_MSG) {
  365. struct mg_http_message *hm = ev_data;
  366. for(i=0; i<h->cnt; i++) {
  367. if (h->fn[i].f && h->fn[i].uri && mg_match(hm->uri, mg_str(h->fn[i].uri), NULL)) {
  368. h->fn[i].f(&s); flag = 1; break;
  369. }
  370. }
  371. if (flag==0) {
  372. struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
  373. mg_http_serve_dir(c, ev_data, &opts);
  374. }
  375. }
  376. else if(ev == MG_EV_ACCEPT) {
  377. if (c->fn_data) {
  378. mg_tls_init(c, &h->tls);
  379. }
  380. }
  381. }
  382. static void *web_thread(void *arg)
  383. {
  384. struct mg_mgr mgr;
  385. struct mg_connection *c;
  386. mg_handle_t *mh=&webHandle.mg;
  387. thread_handle_t *h=(thread_handle_t*)arg;
  388. const char *http_addr="http://[::]:80";
  389. const char *https_addr="https://[::]:443";
  390. //mg_log_set(MG_LL_DEBUG);
  391. mg_mgr_init(&mgr);
  392. //sys_get_path(mh->rootDir, WEB_ROOT);
  393. strcpy(mh->rootDir, "/root/run/web");
  394. if (mg_http_listen(&mgr, http_addr, http_mg_cb, NULL) == NULL) {
  395. MG_ERROR(("cannot listen on %s\n", http_addr));
  396. return NULL;
  397. }
  398. if (mg_http_listen(&mgr, https_addr, http_mg_cb, (void*)1) == NULL) {
  399. MG_ERROR(("cannot listen on %s\n", https_addr));
  400. return NULL;
  401. }
  402. webSetCallback();
  403. printf("_____mgHandle.cnt: %d, rootdir: %s\n", mh->cnt, mh->rootDir);
  404. mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
  405. mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
  406. //mh->tls.name = mg_url_host(https_addr);
  407. while(h->quit==0) {
  408. mg_mgr_poll(&mgr, 200);
  409. }
  410. mg_mgr_free(&mgr);
  411. pthread_exit(NULL);
  412. }
  413. /// @brief 系统信息
  414. /// @param stream
  415. static void systemConfiguration(void* conn)
  416. {
  417. char *ack_str = NULL;
  418. char *g_token = NULL;
  419. mg_stream_t *s=(mg_stream_t *)conn;
  420. struct mg_http_message *hm=s->ev_data;
  421. char* rx_Head_buf=(char *)hm->head.buf;
  422. int tls=s->c->is_tls;
  423. g_token = json_to_Token_info(rx_Head_buf);
  424. // 检查token是否有效
  425. if (g_token != NULL && !CheckLoginStatus(g_token))
  426. {
  427. ack_str = log_AthFailedinfo_to_json();
  428. }
  429. else
  430. {
  431. ack_str = json_to_system_config_info(tls);
  432. }
  433. http_reply(conn, 200, ack_str);
  434. if (g_token != NULL)
  435. {
  436. free(g_token);
  437. }
  438. cJSON_free((void *)ack_str);
  439. }
  440. /// @brief 多语言切换
  441. /// @param stream
  442. static void changeLanguages(void* conn)
  443. {
  444. char* ack_str = NULL;
  445. mg_stream_t *s=(mg_stream_t*)conn;
  446. struct mg_http_message *hm=s->ev_data;
  447. char* rx_body_buf=(char*)hm->body.buf;
  448. json_to_switch_language(rx_body_buf);
  449. paras_save();
  450. ack_str = ctrl_sts_ack_to_json();
  451. http_reply(conn, 200, ack_str);
  452. cJSON_free((void*)ack_str);
  453. }
  454. /// @brief 登录信息
  455. /// @param stream
  456. static void loginFunc(void* conn)
  457. {
  458. char* ack_str = NULL ;
  459. char g_token[64] = {0};
  460. mg_stream_t *s=(mg_stream_t*)conn;
  461. struct mg_http_message *hm=s->ev_data;
  462. char *rx_body_buf=hm->body.buf;
  463. //用户信息创建-未析构
  464. _User_Log *_newUserLogin = (_User_Log *)malloc(sizeof(_User_Log));
  465. memset(_newUserLogin, 0, sizeof(_User_Log));
  466. //转换json参数
  467. if(json_to_User_login_info(rx_body_buf,_newUserLogin)!=0)
  468. {
  469. http_reply(conn, 400, NULL);
  470. return;
  471. }
  472. bool LoginFlag = false;
  473. // 查询用户信息
  474. UserInfo User_list;
  475. INIT_LIST_HEAD(&User_list.list);
  476. if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list))
  477. {
  478. UserInfo *_UserInfoTemp = NULL;
  479. list_for_each_entry(_UserInfoTemp, &User_list.list, list)
  480. {
  481. if (_UserInfoTemp == NULL)
  482. break;
  483. if (strcmp(_UserInfoTemp->userName,_newUserLogin->user_name)==0 && strcmp(_UserInfoTemp->userPW,_newUserLogin->user_password)==0)
  484. {
  485. LoginFlag=true;
  486. _GlobalUserInfoNew = *_UserInfoTemp;
  487. break;
  488. }
  489. }
  490. }
  491. //if (strcmp(_newUserLogin.user_name, "gowone") == 0 && strcmp(_newUserLogin.user_password,"gowone520") == 0)
  492. //{
  493. // LoginFlag = true;
  494. //}
  495. //获取随机token
  496. get_rand_str(g_token,32);
  497. //记录token
  498. strcpy(_newUserLogin->user_token,g_token);
  499. int ack_code=0;
  500. if (!LoginFlag)
  501. {
  502. ack_str = log_LoginFailedinfo_to_json();
  503. ack_code = 201;
  504. }
  505. else
  506. {
  507. strcpy(_GlobalUserToken,g_token);
  508. list_add_tail(&_newUserLogin->list, &_GlobalUserLoginInfo.list);
  509. ack_str = log_info_to_json(g_token);
  510. ack_code = 200;
  511. }
  512. http_reply(conn, ack_code, ack_str);
  513. cJSON_free((void*)ack_str);
  514. }
  515. /// @brief 注销信息
  516. /// @param stream
  517. static void loginOutFunc(void* conn)
  518. {
  519. char* ack_str = NULL ;
  520. char* g_token;
  521. mg_stream_t *s=(mg_stream_t*)conn;
  522. struct mg_http_message *hm=s->ev_data;
  523. char *rx_body_buf = (char*)hm->body.buf;
  524. //转换json参数
  525. if(json_to_User_loginOut_info(rx_body_buf,&g_token)!=0)
  526. {
  527. http_reply(conn, 400, NULL);
  528. return ;
  529. }
  530. ack_str = log_info_to_json(g_token);
  531. http_reply(conn, 200, ack_str);
  532. free(g_token);
  533. cJSON_free((void*)ack_str);
  534. }
  535. /// @brief 获取用户信息
  536. /// @param stream
  537. static void getUserInfo(void* conn)
  538. {
  539. char* ack_str = NULL ;
  540. ack_str = user_info_to_json();
  541. http_reply(conn, 200, ack_str);
  542. cJSON_free((void*)ack_str);
  543. }
  544. /// @brief 获取菜单数据
  545. /// @param stream
  546. static void getUserRouters(void* conn)
  547. {
  548. char* ack_str = NULL ;
  549. char* g_token=NULL;
  550. mg_stream_t *s=(mg_stream_t*)conn;
  551. struct mg_http_message *hm=s->ev_data;
  552. char *rx_Head_buf = (char*)hm->head.buf;
  553. // 查询用户信息
  554. UserInfo User_list;
  555. INIT_LIST_HEAD(&User_list.list);
  556. // 查询角色信息
  557. RoleInfo Role_list;
  558. INIT_LIST_HEAD(&Role_list.list);
  559. g_token = json_to_Token_info(rx_Head_buf);
  560. // 根据token获取用户及用户权限
  561. int nRoleID=3;
  562. if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) )
  563. {
  564. char *nName = getUserNamebyToken(g_token);
  565. if (nName != NULL)
  566. {
  567. nRoleID = GetRoleIDByUserName(nName,&User_list);
  568. }
  569. }
  570. SetMenuAuthority(nRoleID);
  571. ack_str = user_routers_info_to_json();
  572. http_reply(conn, 200, ack_str);
  573. if (g_token != NULL)
  574. {
  575. free(g_token);
  576. }
  577. cJSON_free((void*)ack_str);
  578. }
  579. /// @brief 设备总体信息
  580. /// @param stream
  581. static void overallStatusMonitoring(void* conn)
  582. {
  583. int ret = -1 ;
  584. cJSON *cjson=NULL;
  585. cmd_data_t cmd;
  586. _Over_All_Sts_Monitor _over_all_sts_monitor={0};
  587. _OverBackStsInfo _over_back_sts_info; //设备总体状态信息
  588. _OverAllPwrAckInfo _over_all_pwr_back_info; //设备电源信息
  589. _OverAllPwrAckInfo *pall=&webHandle.stt.all;
  590. char* ack_str=NULL;
  591. mg_stream_t *s=(mg_stream_t*)conn;
  592. struct mg_http_message *hm=s->ev_data;
  593. char *rx_body_buf = (char*)hm->body.buf;
  594. //转换json参数
  595. cjson = json_to_overall_status_monitor(rx_body_buf,&_over_all_sts_monitor);
  596. if(cjson==NULL)
  597. {
  598. if (strstr(hm->uri.buf,"type=export")) {
  599. file_io(conn, FILE_POWER_ALL, FILE_EXPORT, hm->uri.buf);
  600. }
  601. else {
  602. http_reply(conn, 400, NULL);
  603. }
  604. return;
  605. }
  606. //总电压电流信息
  607. if(strcmp(_over_all_sts_monitor.type,"listQuery")==0)
  608. {
  609. cur_dev_addr = _over_all_sts_monitor.product_id;
  610. if(cur_dev_addr>0) {
  611. int r=0;
  612. cmd.cmd = CASCADE_CMD_GET_INFO;
  613. cmd.obj = OBJ_OVERALL;
  614. r = cascade_request(&cmd);
  615. if(r) {
  616. goto quit;
  617. }
  618. cmd.cmd = CASCADE_CMD_QUERY_CH;
  619. cascade_request(&cmd);
  620. goto quit;
  621. }
  622. else {
  623. GlobalPowerInfo __globalPowerInfo;
  624. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  625. _over_all_pwr_back_info.product_name = __globalDeviceManage._globalDevInfo.product.name;
  626. _over_all_pwr_back_info.product_id = __globalDeviceManage._globalDevInfo.product.id;
  627. _over_all_pwr_back_info.product_number = __globalDeviceManage._globalDevInfo.product.number;
  628. //查询数据库总电源信息
  629. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree
  630. ||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One
  631. ||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One_B
  632. )
  633. {
  634. if (strcmp(_over_all_sts_monitor.product_phOrder, "L1") == 0) // a相
  635. {
  636. __globalDeviceManage.nTreeACLshow = 0;
  637. dev_search_latest_t_ac_power_statistic_info(0, 0, &_over_all_pwr_back_info,&__globalDeviceManage);
  638. ack_str = over_all_pwr_monitor_to_json(0,&_over_all_pwr_back_info);
  639. }
  640. else if (strcmp(_over_all_sts_monitor.product_phOrder, "L2") == 0) // b相
  641. {
  642. __globalDeviceManage.nTreeACLshow = 1;
  643. dev_search_latest_t_ac_power_statistic_info(0, 1, &_over_all_pwr_back_info,&__globalDeviceManage);
  644. ack_str = over_all_pwr_monitor_to_json(1,&_over_all_pwr_back_info);
  645. }
  646. else if (strcmp(_over_all_sts_monitor.product_phOrder, "L3") == 0) // c相
  647. {
  648. __globalDeviceManage.nTreeACLshow = 2;
  649. dev_search_latest_t_ac_power_statistic_info(0, 2, &_over_all_pwr_back_info,&__globalDeviceManage);
  650. ack_str = over_all_pwr_monitor_to_json(2,&_over_all_pwr_back_info);
  651. }
  652. }
  653. else {
  654. if (dev_search_latest_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, &__globalPowerInfo) != 0)
  655. {
  656. log_e("serach total pwr err.");
  657. }
  658. _over_all_pwr_back_info.voltage = __globalPowerInfo._power_info.voltage ;
  659. _over_all_pwr_back_info.current = __globalPowerInfo._power_info.current ;
  660. _over_all_pwr_back_info.power = __globalPowerInfo._power_info.power ;
  661. _over_all_pwr_back_info.freq = __globalPowerInfo._power_info.freq ;
  662. _over_all_pwr_back_info.consumption = __globalPowerInfo._power_info.consumption ;
  663. _over_all_pwr_back_info.factor = __globalPowerInfo._power_info.factor ;
  664. }
  665. ack_str = over_all_pwr_monitor_to_json(0,&_over_all_pwr_back_info);
  666. }
  667. }
  668. //打开所有开关
  669. else if(strcmp(_over_all_sts_monitor.type,"open")==0)
  670. {
  671. log_i("over all request:open");
  672. //总开
  673. int nSendAddr=0;
  674. char buff[256];
  675. if(cur_dev_addr>0) {
  676. cmd.cmd = CASCADE_CMD_OPEN;
  677. cmd.obj = OBJ_OVERALL;
  678. cmd.chId = 0xff;
  679. cascade_request(&cmd);
  680. ack_str = ctrl_sts_ack_to_json();
  681. goto quit;
  682. }
  683. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  684. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  685. {
  686. if(_globalPowerMangerTemp==NULL)
  687. break;
  688. if(_globalPowerMangerTemp->product_saddr==nSendAddr)continue;
  689. nSendAddr=_globalPowerMangerTemp->product_saddr;
  690. log_i("Open All address Start");
  691. int rec=g_switch_set_all_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,1);
  692. if(rec<0)log_i("Open All address:%d rec:%d:%s",nSendAddr,rec,modbus_strerror(errno));
  693. }
  694. sprintf(buff,"$开启$|$所有$|$通道$");
  695. dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
  696. ack_str = ctrl_sts_ack_to_json();
  697. }
  698. //关闭所有开关
  699. else if(strcmp(_over_all_sts_monitor.type,"close")==0)
  700. {
  701. log_i("over all request:close");
  702. if(cur_dev_addr>0) {
  703. cmd.cmd = CASCADE_CMD_CLOSE;
  704. cmd.obj = OBJ_OVERALL;
  705. cmd.chId = 0xff;
  706. cascade_request(&cmd);
  707. ack_str = ctrl_sts_ack_to_json();
  708. goto quit;
  709. }
  710. //总关
  711. int nSendAddr=0;
  712. char buff[256];
  713. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  714. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  715. {
  716. if(_globalPowerMangerTemp==NULL)
  717. break;
  718. if(_globalPowerMangerTemp->product_saddr==nSendAddr)continue;
  719. nSendAddr=_globalPowerMangerTemp->product_saddr;
  720. log_i("Close All address Start");
  721. int rec=g_switch_set_all_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,0);
  722. if(rec<0)log_i("Close All address:%d rec:%d:%s",nSendAddr,rec,modbus_strerror(errno));
  723. }
  724. sprintf(buff,"$关闭$|$所有$|$通道$");
  725. dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
  726. ack_str = ctrl_sts_ack_to_json();
  727. }
  728. //查询历史记录
  729. //ok
  730. else if(strcmp(_over_all_sts_monitor.type,"listHisQuery")==0)
  731. {
  732. #if 0
  733. log_i("over all request:listHisQuery");
  734. int ret = 0 ;
  735. int size = 0 ;
  736. int page_num = 0 ;
  737. int page_size = 0 ;
  738. page_num = atoi(_over_all_sts_monitor.page_num);
  739. page_size = atoi(_over_all_sts_monitor.page_size);
  740. GlobalPowerInfo _globalPowerInfo;
  741. INIT_LIST_HEAD(&_globalPowerInfo.list);
  742. ret = dev_search_power_info(__globalDeviceManage.db,
  743. __globalDeviceManage._globalDevInfo.product.id,
  744. 0,
  745. _over_all_sts_monitor.start_time,
  746. _over_all_sts_monitor.end_time,
  747. page_num,
  748. page_size,
  749. &size,
  750. &_globalPowerInfo);
  751. if(ret!=0)
  752. {
  753. log_e("dev all status history get error.\n");
  754. }
  755. //返回应答
  756. ack_str = overall_power_history_to_json(&_globalPowerInfo,&__globalDeviceManage);
  757. //释放资源
  758. GlobalPowerInfo* node;
  759. GlobalPowerInfo* next;
  760. list_for_each_entry_safe(node, next, &_globalPowerInfo.list, list)
  761. {
  762. list_del(&node->list);
  763. free(node);
  764. }
  765. #endif
  766. }
  767. //电压统计
  768. //1分钟1个点 6个小时 共360个点
  769. else if(strcmp(_over_all_sts_monitor.type,"listDyTotalQuery")==0)
  770. {
  771. //申请电压统计信息参数
  772. GlobalPowerInfo __globalPowerInfo;
  773. //清0
  774. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  775. if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
  776. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
  777. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
  778. {
  779. ack_str = over_all_data_t_ac_to_json(pall,&_over_all_sts_monitor, TOTAL_VOLTAGE_TYPE);
  780. }
  781. else if (_over_all_sts_monitor.lType && (strcmp(_over_all_sts_monitor.lType,"L1")==0 || strcmp(_over_all_sts_monitor.lType,"L2")==0 || strcmp(_over_all_sts_monitor.lType,"L3")==0))
  782. {
  783. int ph_id=-1;
  784. if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
  785. {
  786. ph_id=0;
  787. }
  788. else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
  789. {
  790. ph_id=1;
  791. }
  792. else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
  793. {
  794. ph_id=2;
  795. }
  796. // 查询数据库总电源信息 传给所申请的参数结构体
  797. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, ph_id, TOTAL_VOLTAGE_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  798. {
  799. log_e("serach total voltage err.");
  800. }
  801. //else
  802. {
  803. // 传值给总管理结构体
  804. for (int i = 0; i < TOTAL_COUNT; i++)
  805. {
  806. strcpy(pall->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
  807. pall->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
  808. }
  809. // 返回电压统计
  810. ack_str = over_all_total_info_to_json(pall, TOTAL_VOLTAGE_TYPE);
  811. }
  812. }
  813. else
  814. {
  815. // 查询数据库总电源信息 传给所申请的参数结构体
  816. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, TOTAL_VOLTAGE_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  817. {
  818. log_e("serach total voltage err.");
  819. }
  820. //else
  821. {
  822. // 传值给总管理结构体
  823. for (int i = 0; i < TOTAL_COUNT; i++)
  824. {
  825. strcpy(pall->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
  826. pall->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
  827. }
  828. // 返回电压统计
  829. ack_str = over_all_total_info_to_json(pall, TOTAL_VOLTAGE_TYPE);
  830. }
  831. }
  832. }
  833. //电流统计
  834. else if(strcmp(_over_all_sts_monitor.type,"listDlTotalQuery")==0)
  835. {
  836. #if DBUG_INFO
  837. log_i("over all request: listDlTotalQuery\n");
  838. #endif
  839. //申请电流统计信息参数
  840. GlobalPowerInfo __globalPowerInfo;
  841. //清0
  842. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  843. if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
  844. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
  845. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
  846. {
  847. ack_str = over_all_data_t_ac_to_json(pall, &_over_all_sts_monitor, TOTAL_CURRENT_TYPE);
  848. }
  849. else if (_over_all_sts_monitor.lType && (strcmp(_over_all_sts_monitor.lType,"L1")==0 || strcmp(_over_all_sts_monitor.lType,"L2")==0 || strcmp(_over_all_sts_monitor.lType,"L3")==0))
  850. {
  851. int ph_id=-1;
  852. if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
  853. {
  854. ph_id=0;
  855. }
  856. else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
  857. {
  858. ph_id=1;
  859. }
  860. else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
  861. {
  862. ph_id=2;
  863. }
  864. // 查询数据库总电源信息 传给所申请的参数结构体
  865. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, ph_id, TOTAL_CURRENT_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  866. {
  867. log_e("serach total current err.");
  868. }
  869. // 传值给总管理结构体
  870. for (int i = 0; i < TOTAL_COUNT; i++)
  871. {
  872. strcpy(pall->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
  873. pall->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
  874. }
  875. // 返回电流统计
  876. ack_str = over_all_total_info_to_json(pall, TOTAL_CURRENT_TYPE);
  877. }
  878. else
  879. {
  880. // 查询数据库总电源信息 传给所申请的参数结构体
  881. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, TOTAL_CURRENT_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  882. {
  883. log_e("serach total current err.");
  884. }
  885. // 传值给总管理结构体
  886. for (int i = 0; i < TOTAL_COUNT; i++)
  887. {
  888. strcpy(pall->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
  889. pall->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
  890. }
  891. // 返回电流统计
  892. ack_str = over_all_total_info_to_json(pall, TOTAL_CURRENT_TYPE);
  893. }
  894. }
  895. //功率统计
  896. else if(strcmp(_over_all_sts_monitor.type,"listGlTotalQuery")==0)
  897. {
  898. #if DBUG_INFO
  899. log_i("over all request: listGlTotalQuery\n");
  900. #endif
  901. //申请功率统计信息参数
  902. GlobalPowerInfo __globalPowerInfo;
  903. //清0
  904. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  905. if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
  906. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
  907. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
  908. {
  909. ack_str = over_all_data_t_ac_to_json(pall, &_over_all_sts_monitor, TOTAL_POWER_TYPE);
  910. }
  911. else if (_over_all_sts_monitor.lType && (strcmp(_over_all_sts_monitor.lType,"L1")==0 || strcmp(_over_all_sts_monitor.lType,"L2")==0 || strcmp(_over_all_sts_monitor.lType,"L3")==0))
  912. {
  913. int ph_id=-1;
  914. if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
  915. {
  916. ph_id=0;
  917. }
  918. else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
  919. {
  920. ph_id=1;
  921. }
  922. else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
  923. {
  924. ph_id=2;
  925. }
  926. // 查询数据库总电源信息 传给所申请的参数结构体
  927. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, ph_id, TOTAL_POWER_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  928. {
  929. log_e("serach total power err.");
  930. }
  931. // 传值给总管理结构体
  932. for (int i = 0; i < TOTAL_COUNT; i++)
  933. {
  934. strcpy(pall->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
  935. pall->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
  936. }
  937. // 返回功率统计
  938. ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_TYPE);
  939. }
  940. else
  941. {
  942. // 查询数据库总电源信息 传给所申请的参数结构体
  943. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, TOTAL_POWER_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  944. {
  945. log_e("serach total power err.");
  946. }
  947. // 传值给总管理结构体
  948. for (int i = 0; i < TOTAL_COUNT; i++)
  949. {
  950. strcpy(pall->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
  951. pall->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
  952. }
  953. // 返回功率统计
  954. ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_TYPE);
  955. }
  956. }
  957. //耗电量统计
  958. else if(strcmp(_over_all_sts_monitor.type,"listHdlTotalQuery")==0)
  959. {
  960. #if DBUG_INFO
  961. log_i("over all request: listHdlTotalQuery\n");
  962. #endif
  963. //申请耗电量统计信息参数
  964. GlobalPowerInfo __globalPowerInfo;
  965. //清0
  966. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  967. if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
  968. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
  969. ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
  970. {
  971. ack_str = over_all_data_t_ac_to_json(pall, &_over_all_sts_monitor, TOTAL_POWER_CONS_TYPE);
  972. }
  973. else if (_over_all_sts_monitor.product_pwr_type >= 0)
  974. {
  975. // 查询数据库总电源信息 传给所申请的参数结构体
  976. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, TOTAL_POWER_CONS_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  977. {
  978. log_e("serach total power consumption err.");
  979. }
  980. // 传值给总管理结构体
  981. for (int i = 0; i < TOTAL_COUNT; i++)
  982. {
  983. strcpy(pall->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
  984. pall->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
  985. }
  986. // 返回耗电量统计
  987. ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_CONS_TYPE);
  988. }
  989. else if (_over_all_sts_monitor.lType && (strcmp(_over_all_sts_monitor.lType,"L1")==0 || strcmp(_over_all_sts_monitor.lType,"L2")==0 || strcmp(_over_all_sts_monitor.lType,"L3")==0)
  990. &&(__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One_B) )
  991. {
  992. int ph_id=-1;
  993. if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
  994. {
  995. ph_id=0;
  996. }
  997. else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
  998. {
  999. ph_id=1;
  1000. }
  1001. else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
  1002. {
  1003. ph_id=2;
  1004. }
  1005. // 查询数据库总电源信息 传给所申请的参数结构体
  1006. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, ph_id, TOTAL_POWER_CONS_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  1007. {
  1008. log_e("serach total power consumption err.");
  1009. }
  1010. // 传值给总管理结构体
  1011. for (int i = 0; i < TOTAL_COUNT; i++)
  1012. {
  1013. strcpy(pall->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
  1014. pall->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
  1015. }
  1016. // 返回耗电量统计
  1017. ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_CONS_TYPE);
  1018. }
  1019. else
  1020. {
  1021. // 查询数据库总电源信息 传给所申请的参数结构体
  1022. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, TOTAL_POWER_CONS_TYPE, &__globalPowerInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  1023. {
  1024. log_e("serach total power consumption err.");
  1025. }
  1026. // 传值给总管理结构体
  1027. for (int i = 0; i < TOTAL_COUNT; i++)
  1028. {
  1029. strcpy(pall->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
  1030. pall->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
  1031. }
  1032. // 返回耗电量统计
  1033. ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_CONS_TYPE);
  1034. }
  1035. }
  1036. //查询系统总数情况
  1037. else if(strcmp(_over_all_sts_monitor.type,"listTotalQuery")==0)
  1038. {
  1039. #if DBUG_INFO
  1040. log_i("over all request: listTotalQuery\n");
  1041. #endif
  1042. //初始化系统总数结构体
  1043. SystemTotalInfo __system_total_info;
  1044. memset(&__system_total_info, 0, sizeof(SystemTotalInfo));
  1045. struct tm *t;
  1046. struct timeval tv;
  1047. char start_time[32] = {0};
  1048. char stop_time[32] = {0};
  1049. gettimeofday(&tv, NULL);
  1050. t = localtime(&tv.tv_sec);
  1051. sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon+1, t->tm_mday, 0,0,0);
  1052. //sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", 1970, 1, 1, 0,0,0);
  1053. log_d(start_time);
  1054. sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon+1, t->tm_mday, 23,59, 59);
  1055. //sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", 1970, 1, 1, 23,59, 59);
  1056. log_d(stop_time);
  1057. //从数据库获取参数信息
  1058. if(dev_get_system_total_info(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id,0,&__system_total_info,start_time,stop_time)!=0)
  1059. {
  1060. log_e("serach total System total err.");
  1061. }
  1062. int nSumChannel=0;
  1063. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  1064. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1065. {
  1066. nSumChannel+=1;
  1067. }
  1068. __system_total_info.channel_total = nSumChannel ;
  1069. //获取通道总数
  1070. // __GetSystemTotalInfo.channel_total = _globalDeviceManager->useSlaveCount;
  1071. //获取系统总数情况
  1072. _over_back_sts_info.system_total.channel_total = __system_total_info.channel_total;
  1073. _over_back_sts_info.system_total.log_total = __system_total_info.log_total;
  1074. _over_back_sts_info.system_total.sensor_total = __system_total_info.sensor_total;
  1075. _over_back_sts_info.system_total.warn_total = __system_total_info.warn_total;
  1076. //返回系统总数情况
  1077. ack_str = over_system_total_monitor_to_json(&_over_back_sts_info);
  1078. }
  1079. //功率趋势图接口
  1080. else if(strcmp(_over_all_sts_monitor.type,"listGlAllTotalQuery")==0)
  1081. {
  1082. #if DBUG_INFO
  1083. log_i("over all request: listGlAllTotalQuery\n");
  1084. #endif
  1085. //初始化功率趋势图参数
  1086. GlobalPowerInfo __PowerTrendInfo;
  1087. //清0
  1088. memset(&__PowerTrendInfo,0,sizeof(GlobalPowerInfo));
  1089. #if 1
  1090. if (_over_all_sts_monitor.lType && (strcmp(_over_all_sts_monitor.lType,"L1")==0 || strcmp(_over_all_sts_monitor.lType,"L2")==0 || strcmp(_over_all_sts_monitor.lType,"L3")==0)
  1091. &&(__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One_B) )
  1092. {
  1093. int ph_id=-1;
  1094. if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
  1095. {
  1096. ph_id=0;
  1097. }
  1098. else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
  1099. {
  1100. ph_id=1;
  1101. }
  1102. else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
  1103. {
  1104. ph_id=2;
  1105. }
  1106. // 查询数据库总电源信息 传给所申请的参数结构体
  1107. if (dev_search_power_AC_trend_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, ph_id, &__PowerTrendInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  1108. {
  1109. log_e("serach total power consumption err.");
  1110. }
  1111. }
  1112. else
  1113. {
  1114. // 查询数据库总电源信息 传给所申请的参数结构体
  1115. if (dev_search_power_trend_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, &__PowerTrendInfo, _over_all_sts_monitor.start_time, _over_all_sts_monitor.end_time) != 0)
  1116. {
  1117. log_e("serach total power consumption err.");
  1118. }
  1119. }
  1120. //传值给总管理结构体
  1121. for(int i=0;i< TOTAL_COUNT;i++)
  1122. {
  1123. strcpy(_over_all_pwr_back_info._Power_Trend_Info.xAxis[i],__PowerTrendInfo._power_trend_info.xAxis[i]);
  1124. _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_MERITORIOUS_TYPE][i] = __PowerTrendInfo._power_trend_info.ydata[POWER_MERITORIOUS_TYPE][i];
  1125. _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_REACTIVE_TYPE][i] = __PowerTrendInfo._power_trend_info.ydata[POWER_REACTIVE_TYPE][i];
  1126. _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_APPARENT_TYPE][i] = __PowerTrendInfo._power_trend_info.ydata[POWER_APPARENT_TYPE][i];
  1127. }
  1128. #else
  1129. for(int i=0;i< TOTAL_COUNT;i++)
  1130. {
  1131. strcpy(_over_all_pwr_back_info._Power_Trend_Info.xAxis[i],"00:00 ");
  1132. _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_MERITORIOUS_TYPE][i] = 10+i;
  1133. _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_REACTIVE_TYPE][i] = 20+i;
  1134. _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_APPARENT_TYPE][i] = 30+i;
  1135. }
  1136. #endif
  1137. //返回功率趋势
  1138. ack_str = over_power_trend_to_json(&_over_all_pwr_back_info);
  1139. }
  1140. quit:
  1141. cJSON_Delete(cjson);
  1142. http_reply(conn, 200, ack_str);
  1143. cJSON_free((void*)ack_str);
  1144. }
  1145. /// @brief 电源通道
  1146. /// @param stream
  1147. static void channelStatusMonitoring(void* conn)
  1148. {
  1149. int ret = -1 ;
  1150. cJSON *cjson;
  1151. cmd_data_t cmd;
  1152. _OverChnPwrRequest _over_chn_pwr_request;
  1153. _OverAllPwrAckInfo _over_all_pwr_back_info;
  1154. _OverAllPwrAckInfo *pch=&webHandle.stt.chn;
  1155. char* ack_str = NULL ;
  1156. mg_stream_t *s=(mg_stream_t*)conn;
  1157. struct mg_http_message *hm=s->ev_data;
  1158. char *rx_body_buf=(char*)hm->body.buf;
  1159. //转换json参数
  1160. memset(&_over_chn_pwr_request,0,sizeof(_OverChnPwrRequest));
  1161. cjson = json_to_chn_pwr_status_monitor(rx_body_buf,&_over_chn_pwr_request);
  1162. if(cjson==NULL)
  1163. {
  1164. if (strstr(hm->uri.buf,"type=export")) {
  1165. file_io(conn, FILE_POWER_CHN, FILE_EXPORT, hm->uri.buf);
  1166. }
  1167. return;
  1168. }
  1169. if (_over_chn_pwr_request.Zerotype!=NULL)
  1170. {
  1171. if (strcmp(_over_chn_pwr_request.Zerotype, "open") == 0) // 零线开
  1172. {
  1173. int ret = 0;
  1174. int product_chn_id = 0;
  1175. char buff[256];
  1176. log_i("channel NF status request:open");
  1177. product_chn_id = _over_chn_pwr_request.productChId;
  1178. if (cur_dev_addr > 0)
  1179. {
  1180. cmd.cmd = CASCADE_CMD_OPEN_NF;
  1181. cmd.obj = OBJ_CHANNEL;
  1182. cmd.chId = product_chn_id;
  1183. cascade_request(&cmd);
  1184. ack_str = ctrl_sts_ack_to_json();
  1185. goto quit;
  1186. }
  1187. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  1188. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1189. {
  1190. // log_d("channel:%d", _globalPowerMangerTemp->product_ch_id);
  1191. if (_globalPowerMangerTemp == NULL)
  1192. break;
  1193. if (product_chn_id == _globalPowerMangerTemp->product_ch_id)
  1194. {
  1195. log_d("switch ctrl:%d", _globalPowerMangerTemp->product_ch_id);
  1196. ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
  1197. _globalPowerMangerTemp->product_saddr,
  1198. _globalPowerMangerTemp->product_ch_addr,
  1199. 1);
  1200. if (ret < 0)
  1201. {
  1202. ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
  1203. _globalPowerMangerTemp->product_saddr,
  1204. _globalPowerMangerTemp->product_ch_addr,
  1205. 1);
  1206. }
  1207. sprintf(buff, "$开启$|$通道$|%d Naught wire!", _globalPowerMangerTemp->product_ch_id);
  1208. dev_insert_alarm_ctrl(__globalDeviceManage.db, _globalPowerMangerTemp->product_id, ALARM_TYPE_OPRATION, buff);
  1209. break;
  1210. }
  1211. }
  1212. ack_str = ctrl_sts_ack_to_json();
  1213. }
  1214. else if (strcmp(_over_chn_pwr_request.Zerotype, "close") == 0) // 零线关
  1215. {
  1216. int ret = 0;
  1217. int product_chn_id = 0;
  1218. char buff[256];
  1219. log_i("channel NF status request:open");
  1220. product_chn_id = _over_chn_pwr_request.productChId;
  1221. if (cur_dev_addr > 0)
  1222. {
  1223. cmd.cmd = CASCADE_CMD_CLOSE_NF;
  1224. cmd.obj = OBJ_CHANNEL;
  1225. cmd.chId = product_chn_id;
  1226. cascade_request(&cmd);
  1227. ack_str = ctrl_sts_ack_to_json();
  1228. goto quit;
  1229. }
  1230. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  1231. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1232. {
  1233. // log_d("channel:%d", _globalPowerMangerTemp->product_ch_id);
  1234. if (_globalPowerMangerTemp == NULL)
  1235. break;
  1236. if (product_chn_id == _globalPowerMangerTemp->product_ch_id)
  1237. {
  1238. log_d("switch ctrl:%d", _globalPowerMangerTemp->product_ch_id);
  1239. ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
  1240. _globalPowerMangerTemp->product_saddr,
  1241. _globalPowerMangerTemp->product_ch_addr,
  1242. 0);
  1243. if (ret < 0)
  1244. {
  1245. ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
  1246. _globalPowerMangerTemp->product_saddr,
  1247. _globalPowerMangerTemp->product_ch_addr,
  1248. 0);
  1249. }
  1250. sprintf(buff, "$关闭$|$通道$|%d Naught wire!", _globalPowerMangerTemp->product_ch_id);
  1251. dev_insert_alarm_ctrl(__globalDeviceManage.db, _globalPowerMangerTemp->product_id, ALARM_TYPE_OPRATION, buff);
  1252. break;
  1253. }
  1254. }
  1255. ack_str = ctrl_sts_ack_to_json();
  1256. }
  1257. }
  1258. if (_over_chn_pwr_request.type==NULL)
  1259. {
  1260. goto quit;
  1261. }
  1262. if(strcmp(_over_chn_pwr_request.type,"listQuery")==0||strcmp(_over_chn_pwr_request.type,"resetChannel")==0)
  1263. {
  1264. //log_i("channel status request:listQuery");
  1265. //log_d(rx_body_buf);
  1266. if (strcmp(_over_chn_pwr_request.type,"resetChannel")==0)
  1267. {
  1268. //__globalDeviceManage._ReloadChm_flag=true;
  1269. //ResetChmData();
  1270. //__globalDeviceManage._ReloadChm_flag=false;
  1271. }
  1272. GlobalPowerInfo _powerInfo;
  1273. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  1274. int product_chn_id = 0 ;
  1275. _OverChnPwrAckInfo _over_chn_pwr_back_info;
  1276. INIT_LIST_HEAD(&_over_chn_pwr_back_info.list);
  1277. product_chn_id = _over_chn_pwr_request.productChId;
  1278. cur_dev_addr = _over_chn_pwr_request.product_id;
  1279. if(cur_dev_addr>0) {
  1280. int r;
  1281. cmd.cmd = CASCADE_CMD_GET_INFO;
  1282. cmd.obj = OBJ_CHANNEL;
  1283. r = cascade_request(&cmd);
  1284. if(r) {
  1285. goto quit;
  1286. }
  1287. cmd.cmd = CASCADE_CMD_QUERY_CH;
  1288. r = cascade_request(&cmd);
  1289. if(r) {
  1290. goto quit;
  1291. }
  1292. cascade_get_ch(&_over_chn_pwr_back_info);
  1293. }
  1294. else {
  1295. if(product_chn_id==0)
  1296. {
  1297. lock_s_hold(LOCK_ID_POWER_UPDATE);
  1298. dev_search_latest_power_All_info(&__globalDeviceManage,&_over_chn_pwr_back_info, cur_dev_addr);
  1299. lock_s_release(LOCK_ID_POWER_UPDATE);
  1300. }
  1301. else //指定通道
  1302. {
  1303. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1304. {
  1305. if(product_chn_id==_globalPowerMangerTemp->product_ch_id)
  1306. {
  1307. break;
  1308. }
  1309. }
  1310. //更新数据
  1311. //log_d("serach ch%d pwr ok.",product_chn_id);
  1312. _OverChnPwrAckInfo* _overChnPwrBackInfoTemp = (_OverChnPwrAckInfo*)malloc(sizeof(_OverChnPwrAckInfo));
  1313. if(_overChnPwrBackInfoTemp==NULL)
  1314. {
  1315. log_e("_overChnPwrBackInfoTemp malloc err.");
  1316. }
  1317. //查询数据库总电源信息
  1318. if(dev_search_latest_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id,
  1319. product_chn_id, &_powerInfo)!=0)
  1320. {
  1321. log_e("serach total pwr err.");
  1322. }
  1323. {
  1324. //填数据
  1325. _overChnPwrBackInfoTemp->product_name = __globalDeviceManage._globalDevInfo.product.name;
  1326. _overChnPwrBackInfoTemp->product_number = __globalDeviceManage._globalDevInfo.product.number;
  1327. _overChnPwrBackInfoTemp->product_id = __globalDeviceManage._globalDevInfo.product.id;
  1328. _overChnPwrBackInfoTemp->productChName = _globalPowerMangerTemp->product_ch_name;
  1329. _overChnPwrBackInfoTemp->productChId = _globalPowerMangerTemp->product_ch_id;
  1330. _overChnPwrBackInfoTemp->status = _powerInfo._power_info.status;
  1331. _overChnPwrBackInfoTemp->NF_status = _powerInfo._power_info.NF_status;
  1332. _overChnPwrBackInfoTemp->voltage = _powerInfo._power_info.voltage;
  1333. _overChnPwrBackInfoTemp->current = _powerInfo._power_info.current;
  1334. _overChnPwrBackInfoTemp->power = _powerInfo._power_info.power ;
  1335. _overChnPwrBackInfoTemp->freq = _powerInfo._power_info.freq ;
  1336. _overChnPwrBackInfoTemp->consumption = _powerInfo._power_info.consumption ;
  1337. _overChnPwrBackInfoTemp->factor = _powerInfo._power_info.factor ;
  1338. list_add_tail(&_overChnPwrBackInfoTemp->list,&_over_chn_pwr_back_info.list);
  1339. }
  1340. }
  1341. ack_str = over_chn_pwr_monitor_to_json(&_over_chn_pwr_back_info);
  1342. _OverChnPwrAckInfo *node,*next;
  1343. list_for_each_entry_safe(node, next, &_over_chn_pwr_back_info.list, list)
  1344. {
  1345. list_del(&node->list);
  1346. free(node);
  1347. }
  1348. }
  1349. }
  1350. else if(strcmp(_over_chn_pwr_request.type,"open")==0)//通道开
  1351. {
  1352. int ret=0;
  1353. int product_chn_id = 0 ;
  1354. char buff[256];
  1355. log_i("channel status request:open");
  1356. /*
  1357. * todo:添加控制
  1358. */
  1359. product_chn_id = _over_chn_pwr_request.productChId;
  1360. if(cur_dev_addr>0) {
  1361. cmd.cmd = CASCADE_CMD_OPEN;
  1362. cmd.obj = OBJ_CHANNEL;
  1363. cmd.chId = product_chn_id;
  1364. cascade_request(&cmd);
  1365. ack_str = ctrl_sts_ack_to_json();
  1366. goto quit;
  1367. }
  1368. log_d("channel id:%d", product_chn_id);
  1369. // product_chn_id = strtol(_over_chn_pwr_request.productChId,NULL,10);
  1370. // log_d("productChId:%d",product_chn_id);
  1371. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  1372. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1373. {
  1374. //log_d("channel:%d", _globalPowerMangerTemp->product_ch_id);
  1375. if(_globalPowerMangerTemp==NULL)
  1376. break;
  1377. if(product_chn_id==_globalPowerMangerTemp->product_ch_id)
  1378. {
  1379. log_d("switch ctrl:%d",_globalPowerMangerTemp->product_ch_id);
  1380. ret=g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp,
  1381. _globalPowerMangerTemp->product_saddr,
  1382. _globalPowerMangerTemp->product_ch_addr,
  1383. 1,false);
  1384. if (ret < 0)
  1385. {
  1386. ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp,
  1387. _globalPowerMangerTemp->product_saddr,
  1388. _globalPowerMangerTemp->product_ch_addr,
  1389. 1,false);
  1390. }
  1391. char buff[256];
  1392. sprintf(buff,"$开启$|$通道$|%d",_globalPowerMangerTemp->product_ch_id);
  1393. dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
  1394. break;
  1395. }
  1396. }
  1397. ack_str = ctrl_sts_ack_to_json();
  1398. }
  1399. else if(strcmp(_over_chn_pwr_request.type,"close")==0)//通道关
  1400. {
  1401. log_i("channel status request:close");
  1402. /*
  1403. * todo:添加控制
  1404. */
  1405. char buff[256];
  1406. int product_chn_id = 0 ;
  1407. product_chn_id = _over_chn_pwr_request.productChId;
  1408. if(cur_dev_addr>0) {
  1409. cmd.cmd = CASCADE_CMD_CLOSE;
  1410. cmd.obj = OBJ_CHANNEL;
  1411. cmd.chId = product_chn_id;
  1412. cascade_request(&cmd);
  1413. ack_str = ctrl_sts_ack_to_json();
  1414. goto quit;
  1415. }
  1416. // product_chn_id = strtol(_over_chn_pwr_request.productChId,NULL,10);
  1417. // log_d("productChId:%d",product_chn_id);
  1418. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  1419. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1420. {
  1421. if(_globalPowerMangerTemp==NULL)
  1422. break;
  1423. if(product_chn_id==_globalPowerMangerTemp->product_ch_id)
  1424. {
  1425. //log_d("switch ctrl:%d",_globalPowerMangerTemp->product_ch_id);
  1426. ret=g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp,
  1427. _globalPowerMangerTemp->product_saddr,
  1428. _globalPowerMangerTemp->product_ch_addr ,//多版端口余8
  1429. 0,false);
  1430. if (ret < 0)
  1431. {
  1432. ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp,
  1433. _globalPowerMangerTemp->product_saddr,
  1434. _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
  1435. 0,false);
  1436. /* code */
  1437. }
  1438. sprintf(buff,"$关闭$|$通道$|%d",_globalPowerMangerTemp->product_ch_id);
  1439. dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
  1440. break;
  1441. }
  1442. }
  1443. ack_str = ctrl_sts_ack_to_json();
  1444. }
  1445. else if(strcmp(_over_chn_pwr_request.type,"save")==0)//负载
  1446. {
  1447. log_i("channel status request:save");
  1448. if(cur_dev_addr>0) {
  1449. cmd.cmd = CASCADE_CMD_SAVE;
  1450. cmd.obj = OBJ_CHANNEL;
  1451. cmd.chId = _over_chn_pwr_request.productChId;
  1452. cascade_request(&cmd);
  1453. ack_str = ctrl_sts_ack_to_json();
  1454. goto quit;
  1455. }
  1456. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  1457. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1458. {
  1459. if (_over_chn_pwr_request.productChId == _globalPowerMangerTemp->product_ch_id)
  1460. {
  1461. break;
  1462. }
  1463. }
  1464. if (_globalPowerMangerTemp != NULL)
  1465. {
  1466. if (strlen(_over_chn_pwr_request.productChName)<32)
  1467. {
  1468. strcpy(_globalPowerMangerTemp->product_ch_name, _over_chn_pwr_request.productChName);
  1469. if (dev_update_power_manage_genera_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, _globalPowerMangerTemp->product_ch_id, _globalPowerMangerTemp) != 0)
  1470. {
  1471. log_e("update pwr general data err.");
  1472. }
  1473. }else{
  1474. log_e("ChName too long.");
  1475. }
  1476. }
  1477. ack_str = ctrl_sts_ack_to_json();
  1478. }
  1479. else if(strcmp(_over_chn_pwr_request.type,"listHisQuery")==0) //历史记录查询
  1480. {
  1481. #if 0
  1482. int ret = 0 ;
  1483. int size = 0 ;
  1484. int h_pwrchn = 0 ;
  1485. int page_num = 0 ;
  1486. int page_size = 0 ;
  1487. GlobalPowerInfo _globalPowerInfo;
  1488. INIT_LIST_HEAD(&_globalPowerInfo.list);
  1489. h_pwrchn = strtol(_over_chn_pwr_request.productChId,NULL,10);
  1490. page_num = atoi(_over_chn_pwr_request.pageNum);
  1491. page_size = atoi(_over_chn_pwr_request.pageSize);
  1492. //查询数据
  1493. ret = dev_search_power_info(__globalDeviceManage.db,
  1494. __globalDeviceManage._globalDevInfo.product.id,
  1495. h_pwrchn,
  1496. _over_chn_pwr_request.start_time,
  1497. _over_chn_pwr_request.end_time,
  1498. page_num,
  1499. page_size,
  1500. &size,
  1501. &_globalPowerInfo);
  1502. if(ret!=0)
  1503. {
  1504. log_e("dev all status history get error.\n");
  1505. }
  1506. //应答
  1507. ack_str = over_channel_power_history_to_json(&_globalPowerInfo,
  1508. &__globalDeviceManage._globalDevInfo,
  1509. _over_chn_pwr_request.productChName,
  1510. h_pwrchn);
  1511. //释放资源
  1512. GlobalPowerInfo* node;
  1513. GlobalPowerInfo* next;
  1514. list_for_each_entry_safe(node, next, &_globalPowerInfo.list, list)
  1515. {
  1516. list_del(&node->list);
  1517. free(node);
  1518. }
  1519. #endif
  1520. }
  1521. else if(strcmp(_over_chn_pwr_request.type,"listDyTotalQuery")==0)
  1522. {
  1523. #if DBUG_INFO
  1524. log_i("over all request: listDyTotalQuery\n");
  1525. #endif
  1526. int product_chn_id = 0 ;
  1527. product_chn_id = _over_chn_pwr_request.productChId;
  1528. /*
  1529. log_d("productChId:%d",product_chn_id);
  1530. log_i("channel status request:listDyTotalQuery");
  1531. log_d("productChId:%d",_over_chn_pwr_request.productChId);
  1532. log_d("start_time:%s",_over_chn_pwr_request.start_time);
  1533. log_d("end_time:%s",_over_chn_pwr_request.end_time);
  1534. log_d("lType:%s",_over_chn_pwr_request.lType);
  1535. log_d("type:%s",_over_chn_pwr_request.type);
  1536. log_d("user_token:%s",_over_chn_pwr_request.user_token);
  1537. */
  1538. //ack_str = ctrl_sts_ack_to_json();
  1539. //申请电压统计信息参数
  1540. GlobalPowerInfo __globalPowerInfo;
  1541. //清0
  1542. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  1543. //查询数据库通道电源信息 传给所申请的参数结构体
  1544. if (_over_chn_pwr_request.lType && (strcmp(_over_chn_pwr_request.lType,"L1")==0 || strcmp(_over_chn_pwr_request.lType,"L2")==0 || strcmp(_over_chn_pwr_request.lType,"L3")==0))
  1545. {
  1546. int ph_id=-1;
  1547. if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
  1548. {
  1549. ph_id=0;
  1550. }
  1551. else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
  1552. {
  1553. ph_id=1;
  1554. }
  1555. else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
  1556. {
  1557. ph_id=2;
  1558. }
  1559. // 查询数据库总电源信息 传给所申请的参数结构体
  1560. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, ph_id, TOTAL_VOLTAGE_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1561. {
  1562. log_e("serach total power voltage err.");
  1563. }
  1564. // else
  1565. {
  1566. // 传值给总管理结构体
  1567. for (int i = 0; i < TOTAL_COUNT; i++)
  1568. {
  1569. strcpy(pch->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
  1570. pch->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
  1571. }
  1572. // 返回电压统计
  1573. ack_str = over_all_total_info_to_json(pch, TOTAL_VOLTAGE_TYPE);
  1574. }
  1575. }
  1576. else
  1577. {
  1578. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, TOTAL_VOLTAGE_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1579. {
  1580. log_e("serach total voltage err.");
  1581. }
  1582. // else
  1583. {
  1584. // 传值给总管理结构体
  1585. for (int i = 0; i < TOTAL_COUNT; i++)
  1586. {
  1587. strcpy(pch->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
  1588. pch->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
  1589. }
  1590. // 返回电压统计
  1591. ack_str = over_all_total_info_to_json(pch, TOTAL_VOLTAGE_TYPE);
  1592. }
  1593. }
  1594. }
  1595. //电流统计
  1596. else if(strcmp(_over_chn_pwr_request.type,"listDlTotalQuery")==0)
  1597. {
  1598. #if DBUG_INFO
  1599. log_i("over all request: listDlTotalQuery\n");
  1600. #endif
  1601. int product_chn_id = 0 ;
  1602. log_i("channel status request:listDlTotalQuery");
  1603. product_chn_id = _over_chn_pwr_request.productChId;
  1604. log_d("productChId:%d",product_chn_id);
  1605. //申请电流统计信息参数
  1606. GlobalPowerInfo __globalPowerInfo;
  1607. //清0
  1608. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  1609. //查询数据库通道电源信息 传给所申请的参数结构体
  1610. if (_over_chn_pwr_request.lType && (strcmp(_over_chn_pwr_request.lType,"L1")==0 || strcmp(_over_chn_pwr_request.lType,"L2")==0 || strcmp(_over_chn_pwr_request.lType,"L3")==0))
  1611. {
  1612. int ph_id=-1;
  1613. if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
  1614. {
  1615. ph_id=0;
  1616. }
  1617. else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
  1618. {
  1619. ph_id=1;
  1620. }
  1621. else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
  1622. {
  1623. ph_id=2;
  1624. }
  1625. // 查询数据库总电源信息 传给所申请的参数结构体
  1626. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, ph_id, TOTAL_CURRENT_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1627. {
  1628. log_e("serach total power current err.");
  1629. }
  1630. // else
  1631. {
  1632. // 传值给总管理结构体
  1633. for (int i = 0; i < TOTAL_COUNT; i++)
  1634. {
  1635. strcpy(pch->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
  1636. pch->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
  1637. }
  1638. // 返回电流统计
  1639. ack_str = over_all_total_info_to_json(pch, TOTAL_CURRENT_TYPE);
  1640. }
  1641. }
  1642. else
  1643. {
  1644. // 查询数据库总电源信息 传给所申请的参数结构体
  1645. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, TOTAL_CURRENT_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1646. {
  1647. log_e("serach total current err.");
  1648. }
  1649. //else
  1650. {
  1651. // 传值给总管理结构体
  1652. for (int i = 0; i < TOTAL_COUNT; i++)
  1653. {
  1654. strcpy(pch->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
  1655. pch->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
  1656. }
  1657. // 返回电流统计
  1658. ack_str = over_all_total_info_to_json(pch, TOTAL_CURRENT_TYPE);
  1659. }
  1660. }
  1661. }
  1662. //功率统计
  1663. else if(strcmp(_over_chn_pwr_request.type,"listGlTotalQuery")==0)
  1664. {
  1665. #if DBUG_INFO
  1666. log_i("over all request: listGlTotalQuery\n");
  1667. #endif
  1668. int product_chn_id = 0 ;
  1669. log_i("channel status request:listGlTotalQuery");
  1670. product_chn_id = _over_chn_pwr_request.productChId;
  1671. log_d("productChId:%d",product_chn_id);
  1672. //申请功率统计信息参数
  1673. GlobalPowerInfo __globalPowerInfo;
  1674. //清0
  1675. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  1676. //查询数据库通道电源信息 传给所申请的参数结构体
  1677. if (_over_chn_pwr_request.lType && (strcmp(_over_chn_pwr_request.lType,"L1")==0 || strcmp(_over_chn_pwr_request.lType,"L2")==0 || strcmp(_over_chn_pwr_request.lType,"L3")==0))
  1678. {
  1679. int ph_id=-1;
  1680. if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
  1681. {
  1682. ph_id=0;
  1683. }
  1684. else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
  1685. {
  1686. ph_id=1;
  1687. }
  1688. else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
  1689. {
  1690. ph_id=2;
  1691. }
  1692. // 查询数据库总电源信息 传给所申请的参数结构体
  1693. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, ph_id, TOTAL_POWER_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1694. {
  1695. log_e("serach total power err.");
  1696. }
  1697. //else
  1698. {
  1699. // 传值给总管理结构体
  1700. for (int i = 0; i < TOTAL_COUNT; i++)
  1701. {
  1702. strcpy(pch->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
  1703. pch->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
  1704. }
  1705. // 返回功率统计
  1706. ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_TYPE);
  1707. }
  1708. }
  1709. else
  1710. {
  1711. // 查询数据库总电源信息 传给所申请的参数结构体
  1712. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, TOTAL_POWER_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1713. {
  1714. log_e("serach total power err.");
  1715. }
  1716. //else
  1717. {
  1718. // 传值给总管理结构体
  1719. for (int i = 0; i < TOTAL_COUNT; i++)
  1720. {
  1721. strcpy(pch->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
  1722. pch->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
  1723. }
  1724. // 返回功率统计
  1725. ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_TYPE);
  1726. }
  1727. }
  1728. }
  1729. //耗电量统计
  1730. else if(strcmp(_over_chn_pwr_request.type,"listHdlTotalQuery")==0)
  1731. {
  1732. #if DBUG_INFO
  1733. log_i("over all request: listHdlTotalQuery\n");
  1734. #endif
  1735. int product_chn_id = 0 ;
  1736. log_i("channel status request:listHdlTotalQuery");
  1737. product_chn_id = _over_chn_pwr_request.productChId;
  1738. log_d("productChId:%d",product_chn_id);
  1739. //申请耗电量统计信息参数
  1740. GlobalPowerInfo __globalPowerInfo;
  1741. //清0
  1742. memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
  1743. //查询数据库通道电源信息 传给所申请的参数结构体
  1744. if (_over_chn_pwr_request.lType && (strcmp(_over_chn_pwr_request.lType,"L1")==0 || strcmp(_over_chn_pwr_request.lType,"L2")==0 || strcmp(_over_chn_pwr_request.lType,"L3")==0))
  1745. {
  1746. int ph_id=-1;
  1747. if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
  1748. {
  1749. ph_id=0;
  1750. }
  1751. else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
  1752. {
  1753. ph_id=1;
  1754. }
  1755. else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
  1756. {
  1757. ph_id=2;
  1758. }
  1759. // 查询数据库总电源信息 传给所申请的参数结构体
  1760. if (dev_search_time_t_ac_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, ph_id, TOTAL_POWER_CONS_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1761. {
  1762. log_e("serach total power err.");
  1763. }
  1764. // else
  1765. {
  1766. for (int i = 0; i < TOTAL_COUNT; i++)
  1767. {
  1768. strcpy(pch->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
  1769. pch->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
  1770. }
  1771. // 返回耗电量统计
  1772. ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_CONS_TYPE);
  1773. }
  1774. }
  1775. else
  1776. {
  1777. // 查询数据库总电源信息 传给所申请的参数结构体
  1778. if (dev_search_time_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, product_chn_id, TOTAL_POWER_CONS_TYPE, &__globalPowerInfo, _over_chn_pwr_request.start_time, _over_chn_pwr_request.end_time) != 0)
  1779. {
  1780. log_e("serach total power consumption err.");
  1781. }
  1782. // else
  1783. {
  1784. // 传值给总管理结构体
  1785. for (int i = 0; i < TOTAL_COUNT; i++)
  1786. {
  1787. strcpy(pch->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
  1788. pch->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
  1789. }
  1790. // 返回耗电量统计
  1791. ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_CONS_TYPE);
  1792. }
  1793. }
  1794. }
  1795. else if(strcmp(_over_chn_pwr_request.type,"threeTypeSave")==0)
  1796. {
  1797. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  1798. if(cur_dev_addr>0) {
  1799. cmd.cmd = CASCADE_CMD_SAVE3;
  1800. cmd.obj = OBJ_CHANNEL;
  1801. cmd.chId = _over_chn_pwr_request.productChId;
  1802. cascade_request(&cmd);
  1803. goto quit;
  1804. }
  1805. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1806. {
  1807. if (_over_chn_pwr_request.productChId == _globalPowerMangerTemp->product_ch_id)
  1808. {
  1809. break;
  1810. }
  1811. }
  1812. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One
  1813. ||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One_B)
  1814. {
  1815. if (_over_chn_pwr_request.outputType == 1)
  1816. {
  1817. http_reply(conn, 201, ack_str);
  1818. cJSON_free((void *)ack_str);
  1819. return;
  1820. }
  1821. }
  1822. if (_globalPowerMangerTemp != NULL)
  1823. {
  1824. GlobalTreeACManager *_globalTACManager = NULL;
  1825. list_for_each_entry(_globalTACManager, &_globalPowerMangerTemp->list_Tree_AC, list_Tree_AC)
  1826. {
  1827. if (_over_chn_pwr_request.outputType == 2)
  1828. {
  1829. if (strcmp(g_product_t_ac_type_str[_globalTACManager->product_ph_type], _over_chn_pwr_request.productPhOrder) == 0)
  1830. {
  1831. _globalTACManager->product_ph_outputStatus = 1;
  1832. }
  1833. else
  1834. {
  1835. _globalTACManager->product_ph_outputStatus = 2;
  1836. }
  1837. }
  1838. else
  1839. {
  1840. _globalTACManager->product_ph_outputStatus = 1;
  1841. }
  1842. _globalTACManager->product_ph_outputType=_over_chn_pwr_request.outputType;
  1843. if (dev_update_t_ac_power_manage_info(__globalDeviceManage.db, _globalTACManager) != 0)
  1844. {
  1845. log_e("update pwr general data err.");
  1846. }
  1847. }
  1848. }
  1849. }
  1850. else if (strcmp(_over_chn_pwr_request.type, "batteryReset") == 0) // 耗电量复位
  1851. {
  1852. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  1853. char *pCHlist;
  1854. char *pCHListSave;
  1855. pCHlist = strtok_r(_over_chn_pwr_request.strProductChIds, ",", &pCHListSave);
  1856. // 其他版本功能
  1857. while (pCHlist != NULL)
  1858. {
  1859. int g_pwr_ch_id = atoi(pCHlist);
  1860. _globalPowerMangerTemp=NULL;
  1861. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  1862. {
  1863. if (g_pwr_ch_id != _globalPowerMangerTemp->product_ch_id)
  1864. {
  1865. continue;
  1866. }
  1867. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_DC)
  1868. {
  1869. if(g_switch_set_dcpdu_chn_reset(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr-1)<0)
  1870. {
  1871. log_e("Error: g_switch_set_dcpdu_chn_reset");
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_AC)
  1877. {
  1878. if (g_switch_set_t_ac_chn_reset(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr - 1) < 0)
  1879. {
  1880. log_e("Error: g_switch_set_t_ac_chn_reset");
  1881. }
  1882. }
  1883. else
  1884. {
  1885. GlobalTreeACManager *_globalTACManager = NULL;
  1886. list_for_each_entry(_globalTACManager, &_globalPowerMangerTemp->list_Tree_AC, list_Tree_AC)
  1887. {
  1888. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree)
  1889. {
  1890. if (g_switch_set_t_ac_phchn_reset(&__globalDeviceManage._globalRelaySampManger, _globalTACManager->product_saddr, _globalTACManager->product_ch_addr - 1) < 0)
  1891. {
  1892. log_e("Error: g_switch_set_t_ac_phchn_reset");
  1893. }
  1894. }
  1895. else
  1896. {
  1897. if (g_switch_set_t_ac_chn_reset(&__globalDeviceManage._globalRelaySampManger, _globalTACManager->product_saddr, _globalTACManager->product_ch_addr - 1) < 0)
  1898. {
  1899. log_e("Error: g_switch_set_t_ac_chn_reset");
  1900. }
  1901. }
  1902. }
  1903. }
  1904. }
  1905. }
  1906. pCHlist = strtok_r(NULL, ",", &pCHListSave);
  1907. }
  1908. char log_buff[256];
  1909. sprintf(log_buff, "Reset power consumption!");
  1910. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
  1911. }
  1912. quit:
  1913. cJSON_Delete(cjson);
  1914. http_reply(conn, 200, ack_str);
  1915. cJSON_free((void*)ack_str);
  1916. }
  1917. /// @brief 传感器状态请求应答
  1918. /// @param stream
  1919. static void sensorStatusMonitoring(void* conn)
  1920. {
  1921. int ret = -1 ;
  1922. cJSON *cjson;
  1923. _OverSensorRequest _over_all_sensor_request;
  1924. _OverSensorBackInfo _over_all_sensor_back_info;
  1925. GlobalSensorManger _globalSensorMangerTemp ;
  1926. GlobalSensorInfo _globalSensorInfo;
  1927. char* ack_str = NULL ;
  1928. mg_stream_t *s=(mg_stream_t*)conn;
  1929. struct mg_http_message *hm=s->ev_data;
  1930. char *rx_body_buf=(char*)hm->body.buf;
  1931. //转换json参数
  1932. cjson = json_to_sensor_manage(rx_body_buf,&_over_all_sensor_request);
  1933. if(cjson==NULL)
  1934. {
  1935. http_reply(conn, 400, NULL);
  1936. return ;
  1937. }
  1938. //查询json参数
  1939. if(strcmp(_over_all_sensor_request.type,"listQuery")==0)
  1940. {
  1941. log_d("start listQuery handle.");
  1942. #if(1)
  1943. lock_s_hold(LOCK_ID_SENSOR);
  1944. INIT_LIST_HEAD(&_over_all_sensor_back_info.list);
  1945. //获取传感器当前的最新值
  1946. GlobalSensorManger* _globalSensorManagerTemp = NULL;
  1947. list_for_each_entry(_globalSensorManagerTemp, &__globalDeviceManage._globalSensorManger.list, list)
  1948. {
  1949. _OverSensorBackInfo* _overSensorBackInfoTemp = (_OverSensorBackInfo*)malloc(sizeof(_OverSensorBackInfo));
  1950. if(_overSensorBackInfoTemp==NULL)
  1951. log_e("_OverSensorBackInfo malloc error.");
  1952. memset(_overSensorBackInfoTemp,0,sizeof(_OverSensorBackInfo));
  1953. //基本信息
  1954. _overSensorBackInfoTemp->sensorId = _globalSensorManagerTemp->sensor_id;
  1955. //strcpy(_overSensorBackInfoTemp->sensorName,language_sensor_name[__globalDeviceManage._globalDevInfo.product.language][(int)_globalSensorManagerTemp->sensor_type]);
  1956. strcpy(_overSensorBackInfoTemp->sensorName,_globalSensorManagerTemp->sensor_name);
  1957. _overSensorBackInfoTemp->sensorType = _globalSensorManagerTemp->sensor_type;
  1958. memcpy(&_overSensorBackInfoTemp->comSlogan,&_globalSensorManagerTemp->sensor_port_name,sizeof(_overSensorBackInfoTemp->comSlogan));
  1959. //485独有
  1960. _overSensorBackInfoTemp->connectionStatus = _globalSensorManagerTemp->sensor_status;
  1961. _overSensorBackInfoTemp->nodeNumber = __globalDeviceManage._globalSensorManger.sensor_node_number;
  1962. if(_globalSensorManagerTemp->sensor_interface_type==1)
  1963. {
  1964. _overSensorBackInfoTemp->sensorAddr = _globalSensorManagerTemp->sensor_addr ;
  1965. _overSensorBackInfoTemp->sensorBaud = _globalSensorManagerTemp->sensor_baud;
  1966. strcpy(_overSensorBackInfoTemp->sensorvaluech,"RS485");
  1967. }
  1968. else
  1969. {
  1970. //_overSensorBackInfoTemp->connectionStatus = -1;
  1971. strcpy(_overSensorBackInfoTemp->sensorvaluech,_overSensorBackInfoTemp->comSlogan);
  1972. _overSensorBackInfoTemp->nodeNumber =_globalSensorManagerTemp->sensor_addr;
  1973. }
  1974. //拿数据
  1975. memset(&_globalSensorInfo, 0, sizeof(_globalSensorInfo));
  1976. switch(_globalSensorManagerTemp->sensor_type)
  1977. {
  1978. case 0://温湿度传感器 2个值
  1979. {
  1980. _overSensorBackInfoTemp->temperatureValue = _globalSensorManagerTemp->Cur_sensor_info.val1;
  1981. _overSensorBackInfoTemp->humidityValue =_globalSensorManagerTemp->Cur_sensor_info.val2;
  1982. }break;
  1983. case 1://烟雾传感器 1个值
  1984. {
  1985. _overSensorBackInfoTemp->alarmStatus = ! (bool) _globalSensorManagerTemp->Cur_sensor_info.val1; // 1/0 报警未报警
  1986. }break;
  1987. case 2://水浸传感器 1个值 c
  1988. {
  1989. _overSensorBackInfoTemp->waterImmersionState = !(bool)_globalSensorManagerTemp->Cur_sensor_info.val1; // 1/0 报警未报警
  1990. }break;
  1991. case 3://门禁传感器
  1992. {
  1993. _overSensorBackInfoTemp->accessControlStatus = !(bool)_globalSensorManagerTemp->Cur_sensor_info.val1; // 1/0 报警未报警
  1994. }break;
  1995. case 4: //气体传感器 1个值
  1996. {
  1997. _overSensorBackInfoTemp->gasConcentration = _globalSensorManagerTemp->Cur_sensor_info.val1; // 气体浓度
  1998. }break;
  1999. case 5://气压气流传感器 2个值
  2000. {
  2001. _overSensorBackInfoTemp->airPressureValue = _globalSensorManagerTemp->Cur_sensor_info.val1; // 气压
  2002. _overSensorBackInfoTemp->airflowValue = _globalSensorManagerTemp->Cur_sensor_info.val2; // 气流
  2003. };break;
  2004. case 6://空调遥控器 没有值
  2005. {
  2006. }break;
  2007. case 7://双头温度传感器 2个值
  2008. {
  2009. _overSensorBackInfoTemp->temperatureValue = _globalSensorManagerTemp->Cur_sensor_info.val1;
  2010. _overSensorBackInfoTemp->temperatureValue2 =_globalSensorManagerTemp->Cur_sensor_info.val2;
  2011. }break;
  2012. }
  2013. //入队
  2014. list_add_tail(&_overSensorBackInfoTemp->list,&_over_all_sensor_back_info.list);
  2015. }
  2016. lock_s_release(LOCK_ID_SENSOR);
  2017. //参数转换json
  2018. ack_str = over_all_sensor_info_to_json(&_over_all_sensor_back_info,0);
  2019. //释放资源
  2020. _OverSensorBackInfo* node;
  2021. _OverSensorBackInfo* next;
  2022. list_for_each_entry_safe(node, next, &_over_all_sensor_back_info.list, list)
  2023. {
  2024. list_del(&node->list);
  2025. free(node);
  2026. }
  2027. #endif
  2028. }
  2029. //删除传感器
  2030. else if(strcmp(_over_all_sensor_request.type,"delete")==0)
  2031. {
  2032. log_d("start delete handle.");
  2033. int sensor_id_temp = _over_all_sensor_request.sensorId;
  2034. if(dev_delete_sensor_manage(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id,sensor_id_temp)!=0)
  2035. {
  2036. log_e("sensor delete failed.");
  2037. }
  2038. else
  2039. {
  2040. // 维护列表也需要删除
  2041. lock_s_hold(LOCK_ID_SENSOR);
  2042. GlobalSensorManger *node;
  2043. GlobalSensorManger *next;
  2044. list_for_each_entry_safe(node, next, &__globalDeviceManage._globalSensorManger.list, list)
  2045. {
  2046. if (node->sensor_id == sensor_id_temp)
  2047. {
  2048. list_del(&node->list);
  2049. free(node);
  2050. break;
  2051. }
  2052. }
  2053. lock_s_release(LOCK_ID_SENSOR);
  2054. }
  2055. char temp[100];
  2056. //sprintf(temp,"$删除$|$传感器$|%d",sensor_id_temp);
  2057. sprintf(temp,"Delete sensor %d",sensor_id_temp);
  2058. dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  2059. ack_str = ctrl_sts_ack_to_json();
  2060. }
  2061. //更新传感器
  2062. else if(strcmp(_over_all_sensor_request.type,"update")==0)
  2063. {
  2064. log_d("start update handle.");
  2065. int sensor_id_temp = _over_all_sensor_request.sensorId;
  2066. GlobalSensorManger *node = NULL;
  2067. GlobalSensorManger *next = NULL;
  2068. list_for_each_entry_safe(node, next, &__globalDeviceManage._globalSensorManger.list, list)
  2069. {
  2070. if (node->sensor_id == sensor_id_temp)
  2071. {
  2072. break;
  2073. }
  2074. }
  2075. if (node)
  2076. {
  2077. #if (1)
  2078. GlobalSensorManger *_globalSensorMangerTemp = node;
  2079. // 填充数据
  2080. _globalSensorMangerTemp->sensor_type = strtol(_over_all_sensor_request.sensorType, NULL, 10);
  2081. if (_over_all_sensor_request.sensorName)
  2082. {
  2083. int nSize=strlen(_over_all_sensor_request.sensorName);
  2084. if (nSize<=64)
  2085. {
  2086. strcpy(_globalSensorMangerTemp->sensor_name, _over_all_sensor_request.sensorName);
  2087. }
  2088. else{
  2089. //响应
  2090. //stream->errorMsg="Edit Name Too Long";
  2091. http_reply(conn, 500, ack_str);
  2092. //释放资源
  2093. cJSON_free((void*)ack_str);
  2094. return ;
  2095. }
  2096. }
  2097. if(_over_all_sensor_request.cgqtd)
  2098. {
  2099. #ifdef IN_INDEX_MAP
  2100. char index_map[4]=IN_INDEX_MAP;
  2101. #else
  2102. char index_map[4]={0,1,3,2};
  2103. #endif
  2104. if (strcmp(_over_all_sensor_request.cgqtd, "RS485") == 0)
  2105. {
  2106. _globalSensorMangerTemp->sensor_interface_type = 1;
  2107. if (_over_all_sensor_request.sensorAddr)
  2108. {
  2109. _globalSensorMangerTemp->sensor_addr = strtol(_over_all_sensor_request.sensorAddr,NULL,10);
  2110. }
  2111. //前端未传com口信息,暂且不改
  2112. //if (_over_all_sensor_request.comSlogan)
  2113. // {
  2114. // strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.comSlogan);
  2115. // }
  2116. }
  2117. else if (strcmp(_over_all_sensor_request.cgqtd, "IN1") == 0)
  2118. {
  2119. _globalSensorMangerTemp->sensor_interface_type = 2;
  2120. _globalSensorMangerTemp->sensor_addr = index_map[0];
  2121. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2122. }
  2123. else if (strcmp(_over_all_sensor_request.cgqtd, "IN2") == 0)
  2124. {
  2125. _globalSensorMangerTemp->sensor_interface_type = 2;
  2126. _globalSensorMangerTemp->sensor_addr = index_map[1];
  2127. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2128. }
  2129. else if (strcmp(_over_all_sensor_request.cgqtd, "IN3") == 0)
  2130. {
  2131. _globalSensorMangerTemp->sensor_interface_type = 2;
  2132. _globalSensorMangerTemp->sensor_addr = index_map[2];
  2133. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2134. }
  2135. else if (strcmp(_over_all_sensor_request.cgqtd, "IN4") == 0)
  2136. {
  2137. _globalSensorMangerTemp->sensor_interface_type = 2;
  2138. _globalSensorMangerTemp->sensor_addr = index_map[3];
  2139. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2140. }
  2141. }
  2142. //_globalSensorMangerTemp->sensor_baud = strtol(_over_all_sensor_request.sensorBaud, NULL, 10);
  2143. //_globalSensorMangerTemp->sensor_addr = strtol(_over_all_sensor_request.sensorAddr, NULL, 10);
  2144. //_globalSensorMangerTemp->sensor_status = 0;
  2145. switch (_globalSensorMangerTemp->sensor_type)
  2146. {
  2147. case 0:
  2148. _globalSensorMangerTemp->sensor_val_count = 2;
  2149. break; // 温湿度传感器 2个值
  2150. case 1:
  2151. _globalSensorMangerTemp->sensor_val_count = 1;
  2152. break; // 烟雾传感器 1个值
  2153. case 2:
  2154. _globalSensorMangerTemp->sensor_val_count = 1;
  2155. break; // 水浸传感器 1个值
  2156. case 3:
  2157. _globalSensorMangerTemp->sensor_val_count = 1;
  2158. break; // 门禁传感器 没有值
  2159. case 4:
  2160. _globalSensorMangerTemp->sensor_val_count = 1;
  2161. break; // 气体传感器 1个值
  2162. case 5:
  2163. _globalSensorMangerTemp->sensor_val_count = 2;
  2164. break; // 气压气流传感器 2个值
  2165. case 6:
  2166. _globalSensorMangerTemp->sensor_val_count = 0;
  2167. break; // 空调遥控器 没有值
  2168. case 7:
  2169. _globalSensorMangerTemp->sensor_val_count = 2;
  2170. break; // 双头温度传感器 2个值
  2171. }
  2172. // 插入数据库
  2173. if (dev_update_sensor_genera_manage(__globalDeviceManage.db,
  2174. __globalDeviceManage._globalDevInfo.product.id,
  2175. _globalSensorMangerTemp->sensor_id,
  2176. _globalSensorMangerTemp) != 0)
  2177. {
  2178. log_e("dev update error.");
  2179. }
  2180. #endif
  2181. }
  2182. char temp[100];
  2183. // sprintf(temp,"$修改$|$传感器$|%d",sensor_id_temp);
  2184. sprintf(temp,"Update sensor %d",sensor_id_temp);
  2185. dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  2186. ack_str = ctrl_sts_ack_to_json();
  2187. }
  2188. //添加传感器
  2189. else if(strcmp(_over_all_sensor_request.type,"add")==0) //正常跑过
  2190. {
  2191. log_d("start add handle.");
  2192. int wrong_param=0;
  2193. #ifdef IN_INDEX_MAP
  2194. char index_map[4]=IN_INDEX_MAP;
  2195. #else
  2196. char index_map[4]={0,1,3,2};
  2197. #endif
  2198. GlobalSensorManger* _globalSensorMangerTemp = (GlobalSensorManger*)malloc(sizeof(GlobalSensorManger));
  2199. //清空数据
  2200. memset(_globalSensorMangerTemp, 0, sizeof(GlobalSensorManger));
  2201. //填充数据
  2202. _globalSensorMangerTemp->product_id = __globalDeviceManage._globalDevInfo.product.id ;
  2203. int nmaxID=0;
  2204. lock_s_hold(LOCK_ID_SENSOR);
  2205. GlobalSensorManger *node;
  2206. GlobalSensorManger *next;
  2207. list_for_each_entry_safe(node, next, &__globalDeviceManage._globalSensorManger.list, list)
  2208. {
  2209. if (_over_all_sensor_request.cgqtd)
  2210. {
  2211. if (strcmp(_over_all_sensor_request.cgqtd, "RS485") == 0 && node->sensor_interface_type == 1)
  2212. {
  2213. if (strtol(_over_all_sensor_request.sensorAddr, NULL, 10) == node->sensor_addr)
  2214. {
  2215. wrong_param = 1;
  2216. }
  2217. }
  2218. if (strcmp(_over_all_sensor_request.cgqtd, "IN1") == 0 && node->sensor_interface_type == 2)
  2219. {
  2220. if (index_map[0] == node->sensor_addr)
  2221. {
  2222. wrong_param = 1;
  2223. }
  2224. }
  2225. if (strcmp(_over_all_sensor_request.cgqtd, "IN2") == 0 && node->sensor_interface_type == 2)
  2226. {
  2227. if (index_map[1] == node->sensor_addr)
  2228. {
  2229. wrong_param = 1;
  2230. }
  2231. }
  2232. if (strcmp(_over_all_sensor_request.cgqtd, "IN3") == 0 && node->sensor_interface_type == 2)
  2233. {
  2234. if (index_map[2] == node->sensor_addr)
  2235. {
  2236. wrong_param = 1;
  2237. }
  2238. }
  2239. if (strcmp(_over_all_sensor_request.cgqtd, "IN4") == 0 && node->sensor_interface_type == 2)
  2240. {
  2241. if (index_map[3] == node->sensor_addr)
  2242. {
  2243. wrong_param = 1;
  2244. }
  2245. }
  2246. }
  2247. if (node->sensor_id > nmaxID)
  2248. {
  2249. nmaxID = node->sensor_id;
  2250. }
  2251. }
  2252. lock_s_release(LOCK_ID_SENSOR);
  2253. _globalSensorMangerTemp->sensor_id = nmaxID+1;
  2254. strcpy( _globalSensorMangerTemp->sensor_name,_over_all_sensor_request.sensorName);
  2255. if(_over_all_sensor_request.sensorType && strlen(_over_all_sensor_request.sensorType)) {
  2256. _globalSensorMangerTemp->sensor_type = strtol(_over_all_sensor_request.sensorType,NULL,10);
  2257. }
  2258. else {
  2259. wrong_param = 1;
  2260. }
  2261. if (_over_all_sensor_request.cgqtd)
  2262. {
  2263. if (strcmp(_over_all_sensor_request.cgqtd, "RS485") == 0)
  2264. {
  2265. _globalSensorMangerTemp->sensor_interface_type = 1;
  2266. if (_over_all_sensor_request.sensorAddr)
  2267. {
  2268. _globalSensorMangerTemp->sensor_addr = strtol(_over_all_sensor_request.sensorAddr, NULL, 10);
  2269. }
  2270. // if (_over_all_sensor_request.sensorBaud && strlen(_over_all_sensor_request.sensorBaud))
  2271. // {
  2272. _globalSensorMangerTemp->sensor_baud = 9600; // strtol(_over_all_sensor_request.sensorBaud,NULL,10);//默认9600
  2273. // }
  2274. // 前端未传com口信息,暂且不改
  2275. // if (_over_all_sensor_request.comSlogan)
  2276. // {
  2277. strcpy(_globalSensorMangerTemp->sensor_port_name, "COM");
  2278. // }
  2279. }
  2280. else if (strcmp(_over_all_sensor_request.cgqtd, "IN1") == 0)
  2281. {
  2282. _globalSensorMangerTemp->sensor_interface_type = 2;
  2283. _globalSensorMangerTemp->sensor_addr = index_map[0];
  2284. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2285. _globalSensorMangerTemp->sensor_baud = 0;
  2286. }
  2287. else if (strcmp(_over_all_sensor_request.cgqtd, "IN2") == 0)
  2288. {
  2289. _globalSensorMangerTemp->sensor_interface_type = 2;
  2290. _globalSensorMangerTemp->sensor_addr = index_map[1];
  2291. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2292. _globalSensorMangerTemp->sensor_baud = 0;
  2293. }
  2294. else if (strcmp(_over_all_sensor_request.cgqtd, "IN3") == 0)
  2295. {
  2296. _globalSensorMangerTemp->sensor_interface_type = 2;
  2297. _globalSensorMangerTemp->sensor_addr =index_map[2];
  2298. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2299. _globalSensorMangerTemp->sensor_baud = 0;
  2300. }
  2301. else if (strcmp(_over_all_sensor_request.cgqtd, "IN4") == 0)
  2302. {
  2303. _globalSensorMangerTemp->sensor_interface_type = 2;
  2304. _globalSensorMangerTemp->sensor_addr = index_map[3];
  2305. strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
  2306. _globalSensorMangerTemp->sensor_baud = 0;
  2307. }
  2308. }
  2309. else
  2310. {
  2311. wrong_param = 1;
  2312. }
  2313. if(wrong_param) {
  2314. cJSON_Delete(cjson);
  2315. ack_str = ctrl_breaker_err_ack_to_json("Sensor parameter error!");
  2316. http_reply(conn, 200, ack_str);
  2317. cJSON_free((void*)ack_str);
  2318. return ;
  2319. }
  2320. _globalSensorMangerTemp->sensor_status = 0;
  2321. switch( _globalSensorMangerTemp->sensor_type)
  2322. {
  2323. case 0:_globalSensorMangerTemp->sensor_val_count = 2 ;break; //温湿度传感器 2个值
  2324. case 1:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //烟雾传感器 1个值
  2325. case 2:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //水浸传感器 1个值
  2326. case 3:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //门禁传感器 没有值
  2327. case 4:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //气体传感器 1个值
  2328. case 5:_globalSensorMangerTemp->sensor_val_count = 2 ;break; //气压气流传感器 2个值
  2329. case 6:_globalSensorMangerTemp->sensor_val_count = 0 ;break; //空调遥控器 没有值
  2330. case 7:_globalSensorMangerTemp->sensor_val_count = 2 ;break; //双头温度传感器 2个值
  2331. default:break;
  2332. }
  2333. //插入数据库
  2334. if(dev_insert_sensor_genera_manage(__globalDeviceManage.db,_globalSensorMangerTemp)!=0)
  2335. {
  2336. log_e("dev insert error.");
  2337. }
  2338. //全局接口体中也需要添加相关参数
  2339. lock_s_hold(LOCK_ID_SENSOR);
  2340. list_add_tail(&_globalSensorMangerTemp->list,&__globalDeviceManage._globalSensorManger.list);
  2341. lock_s_release(LOCK_ID_SENSOR);
  2342. char temp[100];
  2343. // sprintf(temp,"$新增$|$传感器$|%d",_over_all_sensor_request.sensorId);
  2344. sprintf(temp,"Add sensor %d",_over_all_sensor_request.sensorId);
  2345. dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  2346. ack_str = ctrl_sts_ack_to_json();
  2347. }
  2348. //烟雾报警复位
  2349. else if(strcmp(_over_all_sensor_request.type,"reset")==0)
  2350. {
  2351. ack_str = ctrl_sts_ack_to_json();
  2352. }
  2353. //门禁释放
  2354. else if(strcmp(_over_all_sensor_request.type,"release")==0)
  2355. {
  2356. ack_str = ctrl_sts_ack_to_json();
  2357. }
  2358. //打开空调开关
  2359. else if(strcmp(_over_all_sensor_request.type,"open")==0)
  2360. {
  2361. ack_str = ctrl_sts_ack_to_json();
  2362. }
  2363. //关闭空调开关
  2364. else if(strcmp(_over_all_sensor_request.type,"close")==0)
  2365. {
  2366. ack_str = ctrl_sts_ack_to_json();
  2367. }
  2368. #if(1)
  2369. //历史信息
  2370. else if(strcmp(_over_all_sensor_request.type,"listHisQuery")==0)
  2371. {
  2372. int rsize = 0 ;
  2373. int g_sensor_id = 0 ;
  2374. int g_sensor_type = -1;
  2375. GlobalSensorManger* _globalSensorMangerSTemp;
  2376. int page_num = 0;
  2377. int page_size = 60;
  2378. char start_time[32] = {0};
  2379. char stop_time[32] = {0};
  2380. INIT_LIST_HEAD(&_over_all_sensor_back_info.list);
  2381. if (_over_all_sensor_request.sensorType)
  2382. {
  2383. g_sensor_type = atoi(_over_all_sensor_request.sensorType);
  2384. }
  2385. if ( _over_all_sensor_request.startTime == NULL || _over_all_sensor_request.endTime == NULL)
  2386. {
  2387. struct tm *t;
  2388. struct timeval tv;
  2389. gettimeofday(&tv, NULL);
  2390. t = localtime(&tv.tv_sec);
  2391. sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 23, 59, 59);
  2392. log_d(stop_time);
  2393. sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 0, 0, 0);
  2394. log_d(start_time);
  2395. }
  2396. else
  2397. {
  2398. strncpy(start_time, _over_all_sensor_request.startTime , sizeof(start_time) );
  2399. strncpy(stop_time, _over_all_sensor_request.endTime , sizeof(stop_time) );
  2400. }
  2401. // log_d("page_num:%d",page_num);
  2402. // log_d("page_size:%d",page_size);
  2403. lock_s_hold(LOCK_ID_SENSOR);
  2404. // 查找管理表找到关联信息
  2405. list_for_each_entry(_globalSensorMangerSTemp, &__globalDeviceManage._globalSensorManger.list, list)
  2406. {
  2407. // ID相等 记录信息
  2408. if (_globalSensorMangerSTemp->sensor_type != g_sensor_type)
  2409. {
  2410. continue;
  2411. }
  2412. // 查询数据
  2413. GlobalSensorInfo SensorInfoList;
  2414. INIT_LIST_HEAD(&SensorInfoList.list);
  2415. ret = dev_search_sensor_info(__globalDeviceManage.db,
  2416. __globalDeviceManage._globalDevInfo.product.id,
  2417. _globalSensorMangerSTemp->sensor_id,
  2418. start_time,
  2419. stop_time,
  2420. page_num,
  2421. page_size,
  2422. &rsize,
  2423. &SensorInfoList);
  2424. if (ret != 0)
  2425. {
  2426. log_e("get sensor history error.");
  2427. continue;
  2428. }
  2429. _OverSensorBackInfo *_overSensorBackInfoTemp = (_OverSensorBackInfo *)malloc(sizeof(_OverSensorBackInfo));
  2430. if (_overSensorBackInfoTemp == NULL)
  2431. log_e("_OverSensorBackInfo malloc error.");
  2432. memset(_overSensorBackInfoTemp, 0, sizeof(_OverSensorBackInfo));
  2433. // 基本信息
  2434. _overSensorBackInfoTemp->sensorId = _globalSensorMangerSTemp->sensor_id;
  2435. strcpy(_overSensorBackInfoTemp->sensorName,language_sensor_name[__globalDeviceManage._globalDevInfo.product.language][(int)_globalSensorMangerSTemp->sensor_type]);
  2436. _overSensorBackInfoTemp->sensorType = _globalSensorMangerSTemp->sensor_type;
  2437. strcpy(_overSensorBackInfoTemp->comSlogan, _globalSensorMangerSTemp->sensor_port_name);
  2438. // 485独有
  2439. if (_globalSensorMangerSTemp->sensor_interface_type == 1)
  2440. {
  2441. _overSensorBackInfoTemp->sensorAddr = _globalSensorMangerSTemp->sensor_addr;
  2442. _overSensorBackInfoTemp->sensorBaud = _globalSensorMangerSTemp->sensor_baud;
  2443. }
  2444. _overSensorBackInfoTemp->connectionStatus = _globalSensorMangerSTemp->sensor_status;
  2445. _overSensorBackInfoTemp->nodeNumber = _globalSensorMangerSTemp->sensor_node_number;
  2446. // 组返回数据
  2447. int _TempCount=0;
  2448. GlobalSensorInfo* _globalSensorInfoTemp;
  2449. list_for_each_entry(_globalSensorInfoTemp, &SensorInfoList.list, list)
  2450. {
  2451. if (_TempCount>=TOTAL_COUNT)
  2452. {
  2453. break;
  2454. }
  2455. // 判断sensor type 填充数据
  2456. switch (_globalSensorMangerSTemp->sensor_type)
  2457. {
  2458. case SENSOR_TYPE_TEMPERATURE: // 温湿度传感器 2个值
  2459. {
  2460. _overSensorBackInfoTemp->temp_humi_sensor.ydata[0][_TempCount] = _globalSensorInfoTemp->val1;
  2461. _overSensorBackInfoTemp->temp_humi_sensor.ydata[1][_TempCount] = _globalSensorInfoTemp->val2;
  2462. strcpy(_overSensorBackInfoTemp->temp_humi_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
  2463. }
  2464. break;
  2465. case SENSOR_TYPE_SMOKE: // 烟雾传感器 1个值
  2466. {
  2467. _overSensorBackInfoTemp->alarmStatus =! (bool)_globalSensorInfoTemp->val1; // 1/0 报警未报警
  2468. }
  2469. break;
  2470. case SENSOR_TYPE_WATER: // 水浸传感器
  2471. {
  2472. _overSensorBackInfoTemp->waterImmersionState =! (bool)_globalSensorInfoTemp->val1; // 1/0 报警未报警
  2473. }
  2474. break;
  2475. case SENSOR_TYPE_DOOR: // 门禁传感器 没有值
  2476. {
  2477. _overSensorBackInfoTemp->accessControlStatus =! (bool)_globalSensorInfoTemp->val1; // 1/0 报警未报警
  2478. }
  2479. break;
  2480. case SENSOR_TYPE_GAS: // 气体传感器 1个值
  2481. {
  2482. _overSensorBackInfoTemp->gas_sensor.ydata[_TempCount] = _globalSensorInfoTemp->val1; // 气体浓度
  2483. strcpy(_overSensorBackInfoTemp->gas_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
  2484. }
  2485. break;
  2486. case SENSOR_TYPE_AIR_PRESS:
  2487. {
  2488. _overSensorBackInfoTemp->air_press_sensor.ydata[0][_TempCount] = _globalSensorInfoTemp->val1;
  2489. _overSensorBackInfoTemp->air_press_sensor.ydata[1][_TempCount] = _globalSensorInfoTemp->val2;
  2490. strcpy(_overSensorBackInfoTemp->air_press_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
  2491. }
  2492. break;
  2493. case 6: // 空调遥控器 没有值
  2494. {
  2495. }
  2496. break;
  2497. case SENSOR_TYPE_DoubleT: // 温湿度传感器 2个值
  2498. {
  2499. _overSensorBackInfoTemp->temp_humi_sensor.ydata[0][_TempCount] = _globalSensorInfoTemp->val1;
  2500. _overSensorBackInfoTemp->temp_humi_sensor.ydata[1][_TempCount] = _globalSensorInfoTemp->val2;
  2501. strcpy(_overSensorBackInfoTemp->temp_humi_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
  2502. }
  2503. break;
  2504. }
  2505. _TempCount++;
  2506. }
  2507. // 入队
  2508. list_add_tail(&_overSensorBackInfoTemp->list, &_over_all_sensor_back_info.list);
  2509. // 释放资源
  2510. GlobalSensorInfo *node;
  2511. GlobalSensorInfo *next;
  2512. list_for_each_entry_safe(node, next, &SensorInfoList.list, list)
  2513. {
  2514. list_del(&node->list);
  2515. free(node);
  2516. }
  2517. }
  2518. lock_s_release(LOCK_ID_SENSOR);
  2519. if(strcmp(_over_all_sensor_request.sensorType,"0")==0)
  2520. {
  2521. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
  2522. }
  2523. else if(strcmp(_over_all_sensor_request.sensorType,"4")==0)
  2524. {
  2525. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
  2526. }
  2527. else if(strcmp(_over_all_sensor_request.sensorType,"5")==0)
  2528. {
  2529. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
  2530. }
  2531. else if(strcmp(_over_all_sensor_request.sensorType,"1")==0)
  2532. {
  2533. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
  2534. }
  2535. else if(strcmp(_over_all_sensor_request.sensorType,"2")==0)
  2536. {
  2537. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
  2538. }
  2539. else if(strcmp(_over_all_sensor_request.sensorType,"3")==0)
  2540. {
  2541. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
  2542. }
  2543. else if (strcmp(_over_all_sensor_request.sensorType, "6") == 0)
  2544. {
  2545. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
  2546. }
  2547. else if(strcmp(_over_all_sensor_request.sensorType,"7")==0)
  2548. {
  2549. ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
  2550. }
  2551. else ack_str = ctrl_sts_ack_to_json();
  2552. // //参数转换json
  2553. // ack_str = over_all_sensor_info_to_json(&_over_all_sensor_back_info);
  2554. // 释放资源
  2555. _OverSensorBackInfo *node;
  2556. _OverSensorBackInfo *next;
  2557. list_for_each_entry_safe(node, next, &_over_all_sensor_back_info.list, list)
  2558. {
  2559. list_del(&node->list);
  2560. free(node);
  2561. }
  2562. }
  2563. #endif
  2564. cJSON_Delete(cjson);
  2565. http_reply(conn, 200, ack_str);
  2566. cJSON_free((void*)ack_str);
  2567. }
  2568. /**
  2569. * @brief 设置通道报警阈值
  2570. *
  2571. * 根据给定的通道ID,设置报警请求的通道阈值。
  2572. *
  2573. * @param _alarm_request_info 报警请求信息指针
  2574. * @param nChID 通道ID
  2575. *
  2576. * @return 指向设置后的通道阈值字符串的指针
  2577. */
  2578. char* setThresholdToCh(_AlarmRequestInfo* _alarm_request_info,int nChID)
  2579. {
  2580. int ret = -1 ;
  2581. char *ack_str = NULL;
  2582. GlobalPowerManger *_globalPowerMangerSaveTemp;
  2583. // int g_pwr_ch_id = atoi(_alarm_request_info->_pwr_info.productChId);
  2584. int g_pwr_ch_id = nChID;
  2585. if (g_pwr_ch_id == 0)
  2586. {
  2587. _globalPowerMangerSaveTemp = &__globalDeviceManage._globalPowerManger;
  2588. }
  2589. else
  2590. {
  2591. list_for_each_entry(_globalPowerMangerSaveTemp, &__globalDeviceManage._globalPowerManger.list, list)
  2592. {
  2593. // ID相等 记录信息
  2594. if (_globalPowerMangerSaveTemp->product_ch_id == nChID)
  2595. {
  2596. break;
  2597. }
  2598. }
  2599. }
  2600. // 更新参数,使能全部放在控制板上判断,表头不做判断,统一发1
  2601. if (_alarm_request_info->_pwr_info.voltageUpperValue)
  2602. {
  2603. _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold = atof(_alarm_request_info->_pwr_info.voltageUpperValue);
  2604. }
  2605. _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 1;
  2606. /*if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold != 0)
  2607. {
  2608. _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 1;
  2609. }
  2610. else
  2611. {
  2612. _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 0;
  2613. }*/
  2614. if (_alarm_request_info->_pwr_info.voltageDownValue)
  2615. {
  2616. _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold = atof(_alarm_request_info->_pwr_info.voltageDownValue);
  2617. }
  2618. _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 1;
  2619. /*if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold != 0)
  2620. {
  2621. _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 1;
  2622. }
  2623. else
  2624. {
  2625. _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 0;
  2626. }*/
  2627. if (_alarm_request_info->_pwr_info.currentUpperValue)
  2628. {
  2629. _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold = atof(_alarm_request_info->_pwr_info.currentUpperValue);
  2630. }
  2631. _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 1;
  2632. /*if (_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold != 0)
  2633. {
  2634. _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 1;
  2635. }
  2636. else
  2637. {
  2638. _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 0;
  2639. }*/
  2640. if (_alarm_request_info->_pwr_info.powerUpperValue)
  2641. {
  2642. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold = atof(_alarm_request_info->_pwr_info.powerUpperValue);
  2643. }
  2644. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 1;
  2645. /*if (_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold != 0)
  2646. {
  2647. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 1;
  2648. }
  2649. else
  2650. {
  2651. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 0;
  2652. }*/
  2653. if (_alarm_request_info->_pwr_info.alarmContentUpperValue)
  2654. {
  2655. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold = atof(_alarm_request_info->_pwr_info.alarmContentUpperValue);
  2656. }
  2657. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1;
  2658. /* if (_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold != 0)
  2659. {
  2660. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1;
  2661. }
  2662. else
  2663. {
  2664. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 0;
  2665. }*/
  2666. //_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1 ;
  2667. char Alarm_Ctrl_type[16];
  2668. memset(Alarm_Ctrl_type, 0, sizeof(Alarm_Ctrl_type));
  2669. sprintf(Alarm_Ctrl_type, "%d", ALARM_CTRL_CLOSE_CH);
  2670. if (_alarm_request_info->_pwr_info.currentUpperAction && strcmp(_alarm_request_info->_pwr_info.currentUpperAction, Alarm_Ctrl_type) == 0)
  2671. {
  2672. _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2673. }
  2674. else
  2675. _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2676. if (_alarm_request_info->_pwr_info.voltageUpperAction && strcmp(_alarm_request_info->_pwr_info.voltageUpperAction, Alarm_Ctrl_type) == 0)
  2677. {
  2678. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2679. }
  2680. else
  2681. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2682. if (_alarm_request_info->_pwr_info.voltageDownAction && strcmp(_alarm_request_info->_pwr_info.voltageDownAction, Alarm_Ctrl_type) == 0)
  2683. {
  2684. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2685. }
  2686. else
  2687. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2688. if (_alarm_request_info->_pwr_info.powerUpperAction && strcmp(_alarm_request_info->_pwr_info.powerUpperAction, Alarm_Ctrl_type) == 0)
  2689. {
  2690. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2691. }
  2692. else
  2693. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2694. if (_alarm_request_info->_pwr_info.alarmContentUpperAction && strcmp(_alarm_request_info->_pwr_info.alarmContentUpperAction, Alarm_Ctrl_type) == 0)
  2695. {
  2696. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2697. }
  2698. else
  2699. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2700. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
  2701. {
  2702. GlobalTreeACManager *_pTreeACPowerMangerTemp = NULL;
  2703. list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
  2704. {
  2705. _pTreeACPowerMangerTemp->global_over_manager = _globalPowerMangerSaveTemp->global_over_manager;
  2706. ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
  2707. _globalPowerMangerSaveTemp->product_ch_type,
  2708. _pTreeACPowerMangerTemp->product_saddr,
  2709. _pTreeACPowerMangerTemp->product_ch_addr,
  2710. _globalPowerMangerSaveTemp);
  2711. }
  2712. }
  2713. else
  2714. {
  2715. ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
  2716. _globalPowerMangerSaveTemp->product_ch_type,
  2717. _globalPowerMangerSaveTemp->product_saddr,
  2718. _globalPowerMangerSaveTemp->product_ch_addr,
  2719. _globalPowerMangerSaveTemp);
  2720. }
  2721. if (ret < 0)
  2722. {
  2723. log_e("power threshold update error.ret:%d,%s", ret, modbus_strerror(errno));
  2724. /* code */
  2725. }
  2726. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
  2727. {
  2728. // 写入数据库
  2729. ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
  2730. __globalDeviceManage._globalDevInfo.product.id,
  2731. _globalPowerMangerSaveTemp->product_ch_id,
  2732. _globalPowerMangerSaveTemp);
  2733. if (ret != 0)
  2734. log_e("power threshold update error.");
  2735. GlobalTreeACManager *_pTreeACPowerMangerTemp = NULL;
  2736. list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
  2737. {
  2738. _pTreeACPowerMangerTemp->global_over_manager = _globalPowerMangerSaveTemp->global_over_manager;
  2739. ret = dev_update_t_ac_power_manage_over_info(__globalDeviceManage.db, _pTreeACPowerMangerTemp);
  2740. }
  2741. }
  2742. else
  2743. {
  2744. // 写入数据库
  2745. ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
  2746. __globalDeviceManage._globalDevInfo.product.id,
  2747. _globalPowerMangerSaveTemp->product_ch_id,
  2748. _globalPowerMangerSaveTemp);
  2749. if (ret != 0)
  2750. log_e("power threshold update error.");
  2751. }
  2752. // 返回数据
  2753. ack_str = ctrl_sts_ack_to_json();
  2754. return ack_str;
  2755. }
  2756. /// @brief 告警保护
  2757. /// @param stream
  2758. static void alarmProtect(void *conn)
  2759. {
  2760. int ret = -1 ;
  2761. cJSON *cjson;
  2762. _AlarmRequestInfo _alarm_request_info;
  2763. _AlarmAckInfo _alarm_ack_info; //填充数据
  2764. char* ack_str = NULL ;
  2765. int nAlarmNumber=0;
  2766. mg_stream_t *s=(mg_stream_t*)conn;
  2767. struct mg_http_message *hm=s->ev_data;
  2768. char *rx_body_buf=(char*)hm->body.buf;
  2769. memset(&_alarm_request_info, 0, sizeof(_alarm_request_info));
  2770. //转换json参数
  2771. cjson = json_to_alarm_manage(rx_body_buf,&_alarm_request_info);
  2772. if(cjson==NULL)
  2773. {
  2774. http_reply(conn, 400, NULL);
  2775. return ;
  2776. }
  2777. //查询json参数
  2778. if(strcmp(_alarm_request_info.type,"listAllQuery")==0) //告警信息查询
  2779. {
  2780. int i = 0 ;
  2781. //初始化表
  2782. INIT_LIST_HEAD(&_alarm_ack_info.list);
  2783. //查询数据库,最近的10个报警信息
  2784. char start_time[32] = {0};
  2785. char stop_time[32] = {0};
  2786. if (_alarm_request_info.startTime == NULL || _alarm_request_info.endTime == NULL)
  2787. {
  2788. struct tm *t;
  2789. struct timeval tv;
  2790. gettimeofday(&tv, NULL);
  2791. t = localtime(&tv.tv_sec);
  2792. sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 23, 59, 59);
  2793. log_d(stop_time);
  2794. sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 0, 0, 0);
  2795. log_d(start_time);
  2796. _alarm_request_info.startTime = start_time;
  2797. _alarm_request_info.endTime = stop_time;
  2798. }
  2799. ret = dev_search_alarm_info(__globalDeviceManage.db,
  2800. __globalDeviceManage._globalDevInfo.product.id,
  2801. -1,_alarm_request_info.startTime,_alarm_request_info.endTime,
  2802. 1,10,
  2803. &_alarm_ack_info,&nAlarmNumber);
  2804. if(ret!=0) {
  2805. log_e("get power alarm error.");
  2806. }
  2807. else {
  2808. ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
  2809. //释放资源
  2810. _AlarmAckInfo* node;
  2811. _AlarmAckInfo* next;
  2812. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  2813. {
  2814. list_del(&node->list);
  2815. free(node);
  2816. }
  2817. }
  2818. }
  2819. else if(strcmp(_alarm_request_info.type,"powerAlarmList")==0) //电源告警信息查询
  2820. {
  2821. //初始化表
  2822. INIT_LIST_HEAD(&_alarm_ack_info.list);
  2823. //查询数据库
  2824. ret = dev_search_alarm_info(__globalDeviceManage.db,
  2825. __globalDeviceManage._globalDevInfo.product.id,
  2826. ALARM_TYPE_POWER,_alarm_request_info.startTime,_alarm_request_info.endTime,
  2827. _alarm_request_info.pagenum,_alarm_request_info.pagesize,
  2828. &_alarm_ack_info,&nAlarmNumber);
  2829. if(ret!=0) {
  2830. log_e("get power alarm error.");
  2831. }
  2832. else {
  2833. ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
  2834. //释放资源
  2835. _AlarmAckInfo* node;
  2836. _AlarmAckInfo* next;
  2837. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  2838. {
  2839. list_del(&node->list);
  2840. free(node);
  2841. }
  2842. }
  2843. }
  2844. else if(strcmp(_alarm_request_info.type,"sensorAlarmList")==0) //传感器告警信息查询
  2845. {
  2846. //初始化表
  2847. INIT_LIST_HEAD(&_alarm_ack_info.list);
  2848. //查询数据库
  2849. ret = dev_search_alarm_info(__globalDeviceManage.db,
  2850. __globalDeviceManage._globalDevInfo.product.id,
  2851. ALARM_TYPE_SENSOR,_alarm_request_info.startTime,_alarm_request_info.endTime,
  2852. _alarm_request_info.pagenum,_alarm_request_info.pagesize,
  2853. &_alarm_ack_info,&nAlarmNumber);
  2854. if(ret!=0) {
  2855. log_e("get sensor alarm error.");
  2856. }
  2857. else {
  2858. ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
  2859. //释放资源
  2860. _AlarmAckInfo* node;
  2861. _AlarmAckInfo* next;
  2862. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  2863. {
  2864. list_del(&node->list);
  2865. free(node);
  2866. }
  2867. }
  2868. }
  2869. else if(strcmp(_alarm_request_info.type,"networAlarmkList")==0) //网络告警信息查询
  2870. {
  2871. //初始化表
  2872. INIT_LIST_HEAD(&_alarm_ack_info.list);
  2873. ret = dev_search_alarm_info(__globalDeviceManage.db,
  2874. __globalDeviceManage._globalDevInfo.product.id,
  2875. ALARM_TYPE_NETWORK,_alarm_request_info.startTime,_alarm_request_info.endTime,
  2876. _alarm_request_info.pagenum,_alarm_request_info.pagesize,
  2877. &_alarm_ack_info,&nAlarmNumber);
  2878. if(ret!=0) {
  2879. log_e("get net alarm error.");
  2880. }
  2881. else {
  2882. //转换参数
  2883. ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
  2884. //释放资源
  2885. _AlarmAckInfo* node;
  2886. _AlarmAckInfo* next;
  2887. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  2888. {
  2889. list_del(&node->list);
  2890. free(node);
  2891. }
  2892. }
  2893. }
  2894. else if(strcmp(_alarm_request_info.type,"powerThresholdList")==0)//电源阈值查询
  2895. {
  2896. //查询每个通道阈值
  2897. ack_str = alarm_power_threshold_to_json(&__globalDeviceManage._globalPowerManger);
  2898. }
  2899. else if(strcmp(_alarm_request_info.type,"save")==0)//电源阈值保存
  2900. {
  2901. if (!_alarm_request_info.user_token)
  2902. {
  2903. return;
  2904. }
  2905. //拿到参数保存
  2906. //GlobalPowerManger* _globalPowerMangerSaveTemp ;
  2907. // int g_pwr_ch_id = atoi(_alarm_request_info._pwr_info.productChId);
  2908. int g_pwr_ch_id = _alarm_request_info._pwr_info.productChId;
  2909. ack_str=setThresholdToCh(&_alarm_request_info,g_pwr_ch_id);
  2910. char log_buff[256];
  2911. if (0==g_pwr_ch_id)
  2912. {
  2913. sprintf(log_buff, "Update power threshold ALL_CH!");
  2914. }
  2915. else sprintf(log_buff, "Update power threshold CH%d!",g_pwr_ch_id);
  2916. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
  2917. /*
  2918. if (g_pwr_ch_id ==0)
  2919. {
  2920. _globalPowerMangerSaveTemp=&__globalDeviceManage._globalPowerManger;
  2921. }
  2922. else
  2923. {
  2924. list_for_each_entry(_globalPowerMangerSaveTemp, &__globalDeviceManage._globalPowerManger.list, list)
  2925. {
  2926. // ID相等 记录信息
  2927. if (_globalPowerMangerSaveTemp->product_ch_id == g_pwr_ch_id)
  2928. {
  2929. break;
  2930. }
  2931. }
  2932. }
  2933. //更新参数
  2934. if (_alarm_request_info._pwr_info.voltageUpperValue)
  2935. {
  2936. _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold =atof(_alarm_request_info._pwr_info.voltageUpperValue);
  2937. }
  2938. if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 1 ;
  2939. }else{_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 0;}
  2940. if (_alarm_request_info._pwr_info.voltageDownValue)
  2941. {
  2942. _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold =atof(_alarm_request_info._pwr_info.voltageDownValue);
  2943. }
  2944. if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 1 ;
  2945. }else{_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 0 ;}
  2946. if (_alarm_request_info._pwr_info.currentUpperValue)
  2947. {
  2948. _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold = atof(_alarm_request_info._pwr_info.currentUpperValue);
  2949. }
  2950. if (_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 1 ;
  2951. }else{_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 0 ;}
  2952. if (_alarm_request_info._pwr_info.powerUpperValue)
  2953. {
  2954. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold = atof(_alarm_request_info._pwr_info.powerUpperValue);
  2955. }
  2956. if (_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 1 ;
  2957. }else{_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 0 ;}
  2958. if (_alarm_request_info._pwr_info.alarmContentUpperValue)
  2959. {
  2960. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold = atof(_alarm_request_info._pwr_info.alarmContentUpperValue);
  2961. }
  2962. if (_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1 ;
  2963. }else{_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 0 ;}
  2964. //_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1 ;
  2965. char Alarm_Ctrl_type[16];
  2966. memset(Alarm_Ctrl_type, 0, sizeof(Alarm_Ctrl_type));
  2967. sprintf(Alarm_Ctrl_type, "%d", ALARM_CTRL_CLOSE_CH);
  2968. if (_alarm_request_info._pwr_info.currentUpperAction && strcmp(_alarm_request_info._pwr_info.currentUpperAction,Alarm_Ctrl_type) == 0)
  2969. {
  2970. _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2971. }
  2972. else
  2973. _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2974. if (_alarm_request_info._pwr_info.voltageUpperAction && strcmp(_alarm_request_info._pwr_info.voltageUpperAction, Alarm_Ctrl_type) == 0)
  2975. {
  2976. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2977. }
  2978. else
  2979. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2980. if (_alarm_request_info._pwr_info.voltageDownAction && strcmp(_alarm_request_info._pwr_info.voltageDownAction, Alarm_Ctrl_type) == 0)
  2981. {
  2982. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2983. }
  2984. else
  2985. _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2986. if (_alarm_request_info._pwr_info.powerUpperAction && strcmp(_alarm_request_info._pwr_info.powerUpperAction, Alarm_Ctrl_type) == 0)
  2987. {
  2988. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2989. }
  2990. else
  2991. _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2992. if (_alarm_request_info._pwr_info.alarmContentUpperAction && strcmp(_alarm_request_info._pwr_info.alarmContentUpperAction, Alarm_Ctrl_type) == 0)
  2993. {
  2994. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  2995. }
  2996. else
  2997. _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  2998. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
  2999. {
  3000. GlobalTreeACManager* _pTreeACPowerMangerTemp = NULL ;
  3001. list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
  3002. {
  3003. memcpy(_pTreeACPowerMangerTemp->global_over_manager,_globalPowerMangerSaveTemp->global_over_manager,sizeof(GlobalOverManager));
  3004. ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
  3005. _globalPowerMangerSaveTemp->product_ch_type,
  3006. _pTreeACPowerMangerTemp->product_saddr,
  3007. _pTreeACPowerMangerTemp->product_ch_addr,
  3008. _globalPowerMangerSaveTemp);
  3009. }
  3010. }
  3011. else
  3012. {
  3013. ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
  3014. _globalPowerMangerSaveTemp->product_ch_type,
  3015. _globalPowerMangerSaveTemp->product_saddr,
  3016. _globalPowerMangerSaveTemp->product_ch_addr,
  3017. _globalPowerMangerSaveTemp);
  3018. }
  3019. if (ret<0)
  3020. {
  3021. log_e("power threshold update error.ret:%d,%s",ret,modbus_strerror(errno));
  3022. }
  3023. if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
  3024. {
  3025. // 写入数据库
  3026. ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
  3027. __globalDeviceManage._globalDevInfo.product.id,
  3028. _globalPowerMangerSaveTemp->product_ch_id,
  3029. _globalPowerMangerSaveTemp);
  3030. if (ret != 0)
  3031. log_e("power threshold update error.");
  3032. GlobalTreeACManager *_pTreeACPowerMangerTemp = NULL;
  3033. list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
  3034. {
  3035. memcpy(_pTreeACPowerMangerTemp->global_over_manager, _globalPowerMangerSaveTemp->global_over_manager, sizeof(GlobalOverManager));
  3036. ret = dev_update_t_ac_power_manage_over_info(__globalDeviceManage.db,_pTreeACPowerMangerTemp);
  3037. }
  3038. }
  3039. else
  3040. {
  3041. // 写入数据库
  3042. ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
  3043. __globalDeviceManage._globalDevInfo.product.id,
  3044. _globalPowerMangerSaveTemp->product_ch_id,
  3045. _globalPowerMangerSaveTemp);
  3046. if (ret != 0)
  3047. log_e("power threshold update error.");
  3048. }
  3049. //返回数据
  3050. ack_str = ctrl_sts_ack_to_json();*/
  3051. }
  3052. else if (strcmp(_alarm_request_info.type, "batchSave") == 0) // 批量设置
  3053. {
  3054. if (!_alarm_request_info.user_token)
  3055. {
  3056. return;
  3057. }
  3058. char *pCHlist;
  3059. char *pCHListSave;
  3060. pCHlist = strtok_r(_alarm_request_info.strProductChIds, ",", &pCHListSave);
  3061. // 其他版本功能
  3062. while (pCHlist != NULL)
  3063. {
  3064. int g_pwr_ch_id = atoi(pCHlist);
  3065. ack_str = setThresholdToCh(&_alarm_request_info, g_pwr_ch_id);
  3066. pCHlist = strtok_r(NULL, ",", &pCHListSave);
  3067. //usleep(30000);//延时30ms,等待控制板串口写完
  3068. }
  3069. char log_buff[256];
  3070. sprintf(log_buff, "Batch setting power threshold!");
  3071. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
  3072. }
  3073. else if(strcmp(_alarm_request_info.type,"sensorThresholdList")==0)//传感器阈值查询
  3074. {
  3075. ack_str = alarm_sensor_threshold_to_json(&__globalDeviceManage._globalSensorManger);
  3076. }
  3077. else if(strcmp(_alarm_request_info.type,"update")==0)//传感器阈值保存
  3078. {
  3079. GlobalSensorManger* _globalSensorMangerSaveTemp;
  3080. int g_sensor_id = _alarm_request_info._sensor_info.sensorId;
  3081. list_for_each_entry(_globalSensorMangerSaveTemp, &__globalDeviceManage._globalSensorManger.list, list)
  3082. {
  3083. //ID相等 记录信息
  3084. if(_globalSensorMangerSaveTemp->sensor_id== g_sensor_id)
  3085. {
  3086. break;
  3087. }
  3088. }
  3089. char Alarm_Ctrl_type[16];
  3090. memset(Alarm_Ctrl_type, 0, sizeof(Alarm_Ctrl_type));
  3091. sprintf(Alarm_Ctrl_type, "%d", ALARM_CTRL_CLOSE_CH);
  3092. switch (_globalSensorMangerSaveTemp->sensor_type)
  3093. {
  3094. case SENSOR_TYPE_TEMPERATURE:
  3095. case SENSOR_TYPE_DoubleT:
  3096. {
  3097. char strName1[100]={0};
  3098. char strName2[100]={0};
  3099. sprintf(strName1, "%s%d", language_temperature[__globalDeviceManage._globalDevInfo.product.language], 1);
  3100. sprintf(strName2, "%s%d", language_temperature[__globalDeviceManage._globalDevInfo.product.language], 2);
  3101. if (strcmp(_alarm_request_info._sensor_info.thresholdType, language_temperature[__globalDeviceManage._globalDevInfo.product.language]) == 0||strcmp(_alarm_request_info._sensor_info.thresholdType, strName1) == 0)
  3102. {
  3103. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold = atof(_alarm_request_info._sensor_info.thresholdUpperValue);
  3104. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold = atof(_alarm_request_info._sensor_info.thresholdDownValue);
  3105. if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold>=-0.05 )
  3106. {
  3107. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 0;
  3108. }
  3109. else _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 1;
  3110. if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold>=-0.05 )
  3111. {
  3112. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled = 0;
  3113. }
  3114. else _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled =1;
  3115. if (_alarm_request_info._sensor_info.thresholdUpperAction && strcmp(_alarm_request_info._sensor_info.thresholdUpperAction,Alarm_Ctrl_type) == 0)
  3116. {
  3117. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  3118. if(_alarm_request_info._sensor_info.thresholdUpperChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdUpperChanel);
  3119. }
  3120. else
  3121. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  3122. if (_alarm_request_info._sensor_info.thresholdDownAction && strcmp(_alarm_request_info._sensor_info.thresholdDownAction, Alarm_Ctrl_type) == 0)
  3123. {
  3124. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  3125. if(_alarm_request_info._sensor_info.thresholdDownChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdDownChanel);
  3126. }
  3127. else
  3128. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  3129. }
  3130. else if (strcmp(_alarm_request_info._sensor_info.thresholdType, language_humidity[__globalDeviceManage._globalDevInfo.product.language]) == 0||strcmp(_alarm_request_info._sensor_info.thresholdType, strName2) == 0)
  3131. {
  3132. _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold = atof(_alarm_request_info._sensor_info.thresholdUpperValue);
  3133. _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold = atof(_alarm_request_info._sensor_info.thresholdDownValue);
  3134. if (_globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold>=-0.05 )
  3135. {
  3136. _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_enabled = 0;
  3137. }
  3138. else _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_enabled = 1;
  3139. if (_globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold>=-0.05 )
  3140. {
  3141. _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_enabled = 0;
  3142. }
  3143. else _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_enabled = 1;
  3144. if (_alarm_request_info._sensor_info.thresholdUpperAction && strcmp(_alarm_request_info._sensor_info.thresholdUpperAction, Alarm_Ctrl_type) == 0)
  3145. {
  3146. _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  3147. if(_alarm_request_info._sensor_info.thresholdUpperChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdUpperChanel);
  3148. }
  3149. else
  3150. _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  3151. if (_alarm_request_info._sensor_info.thresholdDownAction && strcmp(_alarm_request_info._sensor_info.thresholdDownAction, Alarm_Ctrl_type) == 0)
  3152. {
  3153. _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  3154. if(_alarm_request_info._sensor_info.thresholdDownChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdDownChanel);
  3155. }
  3156. else
  3157. _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  3158. }
  3159. }
  3160. break;
  3161. case SENSOR_TYPE_SMOKE:
  3162. case SENSOR_TYPE_WATER:
  3163. case SENSOR_TYPE_DOOR:
  3164. break;
  3165. case SENSOR_TYPE_GAS:
  3166. {
  3167. char strName1[100] = {0};
  3168. char strName2[100] = {0};
  3169. sprintf(strName1, "%s%d", language_atmosphere_gas[__globalDeviceManage._globalDevInfo.product.language], 1);
  3170. sprintf(strName2, "%s%d", language_atmosphere_gas[__globalDeviceManage._globalDevInfo.product.language], 2);
  3171. if (strcmp(_alarm_request_info._sensor_info.thresholdType, language_atmosphere_gas[__globalDeviceManage._globalDevInfo.product.language]) == 0 || strcmp(_alarm_request_info._sensor_info.thresholdType, strName1) == 0)
  3172. {
  3173. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold = atof(_alarm_request_info._sensor_info.thresholdUpperValue);
  3174. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold = atof(_alarm_request_info._sensor_info.thresholdDownValue);
  3175. if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold <= 0.05 && _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold >= -0.05)
  3176. {
  3177. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 0;
  3178. }
  3179. else
  3180. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 1;
  3181. if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold <= 0.05 && _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold >= -0.05)
  3182. {
  3183. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled = 0;
  3184. }
  3185. else
  3186. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled = 1;
  3187. if (_alarm_request_info._sensor_info.thresholdUpperAction && strcmp(_alarm_request_info._sensor_info.thresholdUpperAction, Alarm_Ctrl_type) == 0)
  3188. {
  3189. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  3190. if (_alarm_request_info._sensor_info.thresholdUpperChanel)
  3191. strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl_para, _alarm_request_info._sensor_info.thresholdUpperChanel);
  3192. }
  3193. else
  3194. _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  3195. if (_alarm_request_info._sensor_info.thresholdDownAction && strcmp(_alarm_request_info._sensor_info.thresholdDownAction, Alarm_Ctrl_type) == 0)
  3196. {
  3197. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
  3198. if (_alarm_request_info._sensor_info.thresholdDownChanel)
  3199. strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl_para, _alarm_request_info._sensor_info.thresholdDownChanel);
  3200. }
  3201. else
  3202. _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
  3203. }
  3204. }
  3205. break;
  3206. case SENSOR_TYPE_AIR_PRESS:
  3207. default:
  3208. break;
  3209. }
  3210. //更新参数
  3211. ret = dev_update_sensor_over_manage(__globalDeviceManage.db,
  3212. __globalDeviceManage._globalDevInfo.product.id,
  3213. g_sensor_id,
  3214. _globalSensorMangerSaveTemp);
  3215. if(ret!=0)
  3216. log_e("power threshold update error.");
  3217. char log_buff[256];
  3218. sprintf(log_buff, "Update sensor threshold ID%d!",g_sensor_id);
  3219. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
  3220. //拿到参数保存
  3221. ack_str = ctrl_sts_ack_to_json();
  3222. }
  3223. cJSON_Delete(cjson);
  3224. http_reply(conn, 200, ack_str);
  3225. cJSON_free((void*)ack_str);
  3226. }
  3227. /// @brief 查询日志
  3228. /// @param stream
  3229. static void logInformation(void *conn)
  3230. {
  3231. int ret = -1 ;
  3232. char* ack_str = NULL ;
  3233. int nAlarmNum = 0;
  3234. cJSON *cjson;
  3235. _LogInformationRequestInfo __logInformationRequestInfo;
  3236. _AlarmAckInfo _alarm_ack_info; //填充数据
  3237. mg_stream_t *s=(mg_stream_t*)conn;
  3238. struct mg_http_message *hm=s->ev_data;
  3239. char *rx_body_buf=(char*)hm->body.buf;
  3240. memset(&__logInformationRequestInfo,0,sizeof(_LogInformationRequestInfo));
  3241. cjson = json_to_loginformation_manage(rx_body_buf,&__logInformationRequestInfo);
  3242. if(cjson==NULL)
  3243. {
  3244. http_reply(conn, 400, NULL);
  3245. return ;
  3246. }
  3247. //查询json参数
  3248. if(strcmp(__logInformationRequestInfo.type,"listJgrzQuery")==0) //告警日志
  3249. {
  3250. //初始化表
  3251. INIT_LIST_HEAD(&_alarm_ack_info.list);
  3252. if(__logInformationRequestInfo.startTime==NULL || __logInformationRequestInfo.endtime==NULL)
  3253. {
  3254. struct tm* t ;
  3255. struct timeval tv;
  3256. char start_time[32] = {0};
  3257. char stop_time[32] = {0};
  3258. gettimeofday(&tv, NULL);
  3259. t = localtime(&tv.tv_sec);
  3260. sprintf(stop_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,23,59,59);
  3261. log_d(stop_time);
  3262. sprintf(start_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,0,0,0);
  3263. log_d(start_time);
  3264. dev_search_alarm_info(__globalDeviceManage.db,
  3265. __globalDeviceManage._globalDevInfo.product.id,
  3266. 0, //注意注意 此处需要查询的参数是0 1 2
  3267. start_time,//启动时间
  3268. stop_time, //结束时间
  3269. __logInformationRequestInfo.pagenum, //页
  3270. __logInformationRequestInfo.pagesize, //条
  3271. &_alarm_ack_info,&nAlarmNum);
  3272. ack_str = alarm_type_info_to_json(&_alarm_ack_info,nAlarmNum);
  3273. //释放资源
  3274. _AlarmAckInfo* node;
  3275. _AlarmAckInfo* next;
  3276. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  3277. {
  3278. list_del(&node->list);
  3279. free(node);
  3280. }
  3281. }
  3282. else
  3283. {
  3284. dev_search_alarm_info(__globalDeviceManage.db,
  3285. __globalDeviceManage._globalDevInfo.product.id,
  3286. 0, //注意注意 此处需要查询的参数是0 1 2
  3287. __logInformationRequestInfo.startTime,//开始时间
  3288. __logInformationRequestInfo.endtime, //结束时间
  3289. __logInformationRequestInfo.pagenum, //页
  3290. __logInformationRequestInfo.pagesize, //条
  3291. &_alarm_ack_info,&nAlarmNum);
  3292. ack_str = alarm_type_info_to_json(&_alarm_ack_info,nAlarmNum);
  3293. //释放资源
  3294. _AlarmAckInfo* node;
  3295. _AlarmAckInfo* next;
  3296. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  3297. {
  3298. list_del(&node->list);
  3299. free(node);
  3300. }
  3301. }
  3302. //转换参数
  3303. //ack_str = alarm_type_info_to_json(&_alarm_ack_info);
  3304. }
  3305. else if(strcmp(__logInformationRequestInfo.type,"listCzrzQuery")==0) //操作日志
  3306. {
  3307. //初始化表
  3308. INIT_LIST_HEAD(&_alarm_ack_info.list);
  3309. //默认查当天日志
  3310. if(__logInformationRequestInfo.startTime==NULL || __logInformationRequestInfo.endtime==NULL)
  3311. {
  3312. struct tm* t ;
  3313. struct timeval tv;
  3314. char start_time[32] = {0};
  3315. char stop_time[32] = {0};
  3316. gettimeofday(&tv, NULL);
  3317. t = localtime(&tv.tv_sec);
  3318. sprintf(stop_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,23,59,59);
  3319. log_d(stop_time);
  3320. sprintf(start_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,0,0,0);
  3321. log_d(start_time);
  3322. dev_search_alarm_info(__globalDeviceManage.db,
  3323. __globalDeviceManage._globalDevInfo.product.id,
  3324. ALARM_TYPE_OPRATION, // alarm_type, 操作日志
  3325. start_time,
  3326. stop_time,
  3327. __logInformationRequestInfo.pagenum,
  3328. __logInformationRequestInfo.pagesize,
  3329. &_alarm_ack_info, &nAlarmNum);
  3330. }
  3331. else
  3332. {
  3333. dev_search_alarm_info(__globalDeviceManage.db,
  3334. __globalDeviceManage._globalDevInfo.product.id,
  3335. ALARM_TYPE_OPRATION,// alarm_type, 操作日志
  3336. __logInformationRequestInfo.startTime,
  3337. __logInformationRequestInfo.endtime,
  3338. __logInformationRequestInfo.pagenum,
  3339. __logInformationRequestInfo.pagesize,
  3340. &_alarm_ack_info,&nAlarmNum);
  3341. }
  3342. //转换参数
  3343. ack_str = alarm_ctrl_type_info_to_json(&_alarm_ack_info,nAlarmNum);
  3344. //释放资源
  3345. _AlarmAckInfo* node;
  3346. _AlarmAckInfo* next;
  3347. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  3348. {
  3349. list_del(&node->list);
  3350. free(node);
  3351. }
  3352. }
  3353. else if(strcmp(__logInformationRequestInfo.type,"listYxrzQuery")==0) //运行日志
  3354. {
  3355. //初始化表
  3356. INIT_LIST_HEAD(&_alarm_ack_info.list);
  3357. //默认查当天日志
  3358. if(__logInformationRequestInfo.startTime==NULL || __logInformationRequestInfo.endtime==NULL)
  3359. {
  3360. struct tm *t;
  3361. struct timeval tv;
  3362. char start_time[32] = {0};
  3363. char stop_time[32] = {0};
  3364. gettimeofday(&tv, NULL);
  3365. t = localtime(&tv.tv_sec);
  3366. sprintf(stop_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,23,59,59);
  3367. log_d(stop_time);
  3368. sprintf(start_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,0,0,0);
  3369. log_d(start_time);
  3370. dev_search_alarm_info(__globalDeviceManage.db,
  3371. __globalDeviceManage._globalDevInfo.product.id,
  3372. ALARM_TYPE_RUNNING, // alarm_type, 运行日志
  3373. start_time,
  3374. stop_time,
  3375. __logInformationRequestInfo.pagenum,
  3376. __logInformationRequestInfo.pagesize,
  3377. &_alarm_ack_info, &nAlarmNum);
  3378. }
  3379. else
  3380. {
  3381. dev_search_alarm_info(__globalDeviceManage.db,
  3382. __globalDeviceManage._globalDevInfo.product.id,
  3383. ALARM_TYPE_RUNNING, // alarm_type, 运行日志
  3384. __logInformationRequestInfo.startTime,
  3385. __logInformationRequestInfo.endtime,
  3386. __logInformationRequestInfo.pagenum,
  3387. __logInformationRequestInfo.pagesize,
  3388. &_alarm_ack_info, &nAlarmNum);
  3389. }
  3390. // 转换参数
  3391. ack_str = alarm_type_info_to_json(&_alarm_ack_info, nAlarmNum);
  3392. //释放资源
  3393. _AlarmAckInfo* node;
  3394. _AlarmAckInfo* next;
  3395. list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
  3396. {
  3397. list_del(&node->list);
  3398. free(node);
  3399. }
  3400. }
  3401. else if(strcmp(__logInformationRequestInfo.type,"exportCzrz")==0) //导出操作日志
  3402. {
  3403. }
  3404. else if(strcmp(__logInformationRequestInfo.type,"exportYxrz")==0) //导出运行日志
  3405. {
  3406. }
  3407. else if(strcmp(__logInformationRequestInfo.type,"exportJgrz")==0) //导出告警日志
  3408. {
  3409. //
  3410. }
  3411. cJSON_Delete(cjson);
  3412. http_reply(conn, 200, ack_str);
  3413. cJSON_free((void*)ack_str);
  3414. }
  3415. /// @brief 通信网口设置
  3416. /// @param stream
  3417. static void communicationManagement(void *conn)
  3418. {
  3419. int r = -1 ;
  3420. char* ack_str = NULL ;
  3421. cJSON *cjson;
  3422. _CommunicationManageRequestInfo _communicationManageRequestInfo;
  3423. _CommunicationManageRequestInfo reqInfo;
  3424. GlobalDeviceInfo *devInfo=&__globalDeviceManage._globalDevInfo;
  3425. mg_stream_t *s=(mg_stream_t*)conn;
  3426. struct mg_http_message *hm=s->ev_data;
  3427. char *rx_body_buf=(char*)hm->body.buf;
  3428. //转换json参数
  3429. cjson = json_to_communication_manage(rx_body_buf,&reqInfo);
  3430. if(cjson==NULL)
  3431. {
  3432. http_reply(conn, 400, NULL);
  3433. return ;
  3434. }
  3435. //查询网络参数
  3436. if(strcmp(reqInfo.type,"listWkIPV4Query")==0 || strcmp(reqInfo.type,"listWkIPV6Query")==0)//缃戠粶鍙傛暟鏌ヨ
  3437. {
  3438. NetworkInfo_t info={0};
  3439. int r,ipv=(strstr(reqInfo.type, "listWkIPV4Query")?IP_V4:IP_V6);
  3440. r = sys_get_net(&info, ipv);
  3441. if(r==0) {
  3442. ack_str = communication_net_ack_to_json(&info);
  3443. }
  3444. }
  3445. else if(strcmp(reqInfo.type,"WkIPV4Save")==0 || strcmp(reqInfo.type,"WkIPV6Save")==0 ) //淇濆瓨鏁版嵁
  3446. {
  3447. NetworkInfo_t* info = &devInfo->network;
  3448. _NET_CommunicationRequestInfo *info2=&reqInfo._net_info;
  3449. int ipv=(strstr(reqInfo.type, "WkIPV4Save")?IP_V4:IP_V6);
  3450. int mode = atoi(info2->mode);
  3451. if(ipv==IP_V4 || (ipv==IP_V6 && mode==IP_STATIC)) {
  3452. info->mode = mode;
  3453. if(info2->ipaddr) strcpy(info->ip_address, info2->ipaddr);
  3454. if(info2->maskaddr) strcpy(info->mask, info2->maskaddr);
  3455. if(info2->gateway) strcpy(info->gateway, info2->gateway);
  3456. if(info2->dns1) strcpy(info->dns1, info2->dns1);
  3457. if(info2->dns2) strcpy(info->dns2, info2->dns2);
  3458. r = sys_set_net(info, ipv);
  3459. //print_net("set", info);
  3460. }
  3461. ack_str = ctrl_sts_ack_to_json();
  3462. }
  3463. //查询设置RS485参数
  3464. else if(strcmp(reqInfo.type,"listRsQuery")==0)//modbus鍙傛暟鏌ヨ
  3465. {
  3466. ModbusInfo_t* _modbusInfo_temp = &devInfo->cascade;
  3467. ack_str = communication_rs_ack_to_json(_modbusInfo_temp);
  3468. }
  3469. else if(strcmp(reqInfo.type,"RsSave")==0)//淇濆瓨modbus鏁版嵁
  3470. {
  3471. ModbusInfo_t* info = &devInfo->cascade;
  3472. bool isStrMaster=strcmp(reqInfo._rs_info.mode,language_alarm_RS485_master[__globalDeviceManage._globalDevInfo.product.language])==0;
  3473. bool isStrSlave=strcmp(reqInfo._rs_info.mode,language_alarm_RS485_slave[__globalDeviceManage._globalDevInfo.product.language])==0;
  3474. if (isStrMaster||isStrSlave)
  3475. {
  3476. if (isStrMaster)
  3477. {
  3478. info->mode = MODBUS_MASTER;
  3479. }
  3480. else
  3481. {
  3482. info->mode =MODBUS_SLAVE;
  3483. }
  3484. }
  3485. else
  3486. {
  3487. info->mode = atoi(reqInfo._rs_info.mode);
  3488. }
  3489. info->baudrate = atoi(reqInfo._rs_info.baud);
  3490. info->addr = atoi(reqInfo._rs_info.addr);
  3491. ///////////////////////////////////////////////////////////////////////////////
  3492. r = cascade_set_modbus(info);
  3493. if(r==0) {
  3494. paras_save();
  3495. }
  3496. char buff[256];
  3497. if (!info->mode)
  3498. {
  3499. sprintf(buff, "$%s$|$%s$|BD:%d", language_RS485_install[0],language_alarm_RS485_master[0],info->baudrate);
  3500. }
  3501. else
  3502. {
  3503. sprintf(buff, "$%s$|$%s$|addr:%d BD:%d",language_RS485_install[0],language_alarm_RS485_slave[0],info->addr,info->baudrate);
  3504. }
  3505. dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalPowerManger.product_id,ALARM_TYPE_OPRATION,buff);
  3506. ack_str = ctrl_sts_ack_to_json();
  3507. }
  3508. cJSON_Delete(cjson);
  3509. http_reply(conn, 200, ack_str);
  3510. cJSON_free((void*)ack_str);
  3511. }
  3512. /// @brief 服务管理
  3513. /// @param stream
  3514. static void serviceManagement(void *conn)
  3515. {
  3516. int ret = -1 ;
  3517. cJSON *cjson;
  3518. _ServiceManageRequestInfo _serviceManageRequestInfo;
  3519. char* ack_str = NULL ;
  3520. mg_stream_t *s=(mg_stream_t*)conn;
  3521. struct mg_http_message *hm=s->ev_data;
  3522. char *rx_body_buf = (char*)hm->body.buf;
  3523. GlobalDeviceManager *gdm=&__globalDeviceManage;
  3524. //转换json参数
  3525. memset(&_serviceManageRequestInfo, 0, sizeof(_serviceManageRequestInfo));
  3526. cjson = json_to_service_manage(rx_body_buf,&_serviceManageRequestInfo);
  3527. if(cjson==NULL)
  3528. {
  3529. http_reply(conn, 400, NULL);
  3530. return ;
  3531. }
  3532. if(strcmp(_serviceManageRequestInfo.type,"listSmtpQuery")==0)
  3533. {
  3534. ack_str = service_mail_ack_to_json(&gdm->mailInfo);
  3535. }
  3536. else if(strcmp(_serviceManageRequestInfo.type,"SmtpSave")==0)
  3537. {
  3538. mail_info_t mailInfo=gdm->mailInfo;
  3539. mail_send_t *send=&mailInfo.send;
  3540. mail_recv_t *recv=mailInfo.recv;
  3541. _MAIL_ServerRequestInfo *info=&_serviceManageRequestInfo._smtp_info;
  3542. if(info->mode) {
  3543. int save_flag=0;
  3544. send->mode = atoi(info->mode);
  3545. if(send->mode) {
  3546. if(info->account && info->password && info->server && info->port && info->auth) {
  3547. if(strcmp(send->account, info->account)) {
  3548. strcpy(send->account, info->account); save_flag = 1;
  3549. }
  3550. if(strcmp(send->password, info->password)) {
  3551. strcpy(send->password, info->password); save_flag = 1;
  3552. }
  3553. if(strcmp(send->server, info->server)) {
  3554. strcpy(send->server, info->server);;
  3555. }
  3556. if(strcmp(send->port, info->port)) {
  3557. strcpy(send->port, info->port);;
  3558. }
  3559. if(strcmp(send->auth, info->auth)) {
  3560. strcpy(send->auth, info->auth);;
  3561. }
  3562. }
  3563. }
  3564. else {
  3565. memset(send, 0, sizeof(mail_send_t));;
  3566. }
  3567. if(memcmp(&mailInfo.send, &gdm->mailInfo.send, sizeof(mail_send_t))) {
  3568. dev_mail_update_send(gdm->db, send);
  3569. gdm->mailInfo.send = mailInfo.send;
  3570. }
  3571. for(int i=0; i<MAIL_RECV_MAX; i++) {
  3572. recv[i].id = i;
  3573. if(info->recvAccount[i]) {
  3574. recv[i].use = 1;
  3575. strcpy(recv[i].account, info->recvAccount[i]);
  3576. }
  3577. else{
  3578. recv[i].use = 0;
  3579. strcpy(recv[i].account,"");
  3580. }
  3581. }
  3582. if(memcmp(mailInfo.recv, gdm->mailInfo.recv, sizeof(mail_recv_t)*MAIL_RECV_MAX)) {
  3583. dev_mail_update_recv(gdm->db, recv);
  3584. memcpy(gdm->mailInfo.recv, mailInfo.recv, sizeof(mail_recv_t)*MAIL_RECV_MAX);
  3585. }
  3586. }
  3587. }
  3588. #ifdef USE_MQTT
  3589. else if(strcmp(_serviceManageRequestInfo.type,"listMqttQuery")==0)
  3590. {
  3591. ack_str = service_mqtt_ack_to_json(&gdm->mqttInfo);
  3592. }
  3593. else if(strcmp(_serviceManageRequestInfo.type,"mqttSave")==0)
  3594. {
  3595. int save_flag=0;
  3596. _MQTT_ServerRequestInfo *req=&_serviceManageRequestInfo._mqtt_info;
  3597. mqtt_server_t *ser=&gdm->mqttInfo.ser[req->idx];
  3598. if(req->idx |= ser->id) { ser->id = req->idx; save_flag=1; }
  3599. if(req->name) { strcpy(ser->name, req->name); save_flag=1; }
  3600. if(req->server) { strcpy(ser->server, req->server); save_flag=1; }
  3601. if(req->port) { strcpy(ser->port, req->port); save_flag=1; }
  3602. if(req->user) { strcpy(ser->user, req->user); save_flag=1; }
  3603. if(req->password) { strcpy(ser->password, req->password); save_flag=1; }
  3604. if(req->cid) { strcpy(ser->cid, req->cid); save_flag=1; }
  3605. if(save_flag) {
  3606. dev_mqtt_update_ser(gdm->db, ser);
  3607. }
  3608. }
  3609. else if(strcmp(_serviceManageRequestInfo.type,"mqttOpen")==0 ||
  3610. strcmp(_serviceManageRequestInfo.type,"mqttClose")==0)
  3611. {
  3612. int save_flag=0;
  3613. _MQTT_ServerRequestInfo *req=&_serviceManageRequestInfo._mqtt_info;
  3614. mqtt_server_t *ser=&gdm->mqttInfo.ser[req->idx];
  3615. if(req->mode!=ser->mode) {
  3616. ser->mode = req->mode;
  3617. dev_mqtt_update_ser(gdm->db, ser);
  3618. }
  3619. }
  3620. #endif
  3621. else if(strcmp(_serviceManageRequestInfo.type,"listSnmpQuery")==0)
  3622. {
  3623. // 查询SNMP信息
  3624. if (0 == dev_get_snmp_info(__globalDeviceManage.db, &_globalSNMPManager))
  3625. {
  3626. ack_str = service_snmp_ack_to_json(&_globalSNMPManager);
  3627. }
  3628. }
  3629. else if(strcmp(_serviceManageRequestInfo.type,"SnmpSave")==0)
  3630. {
  3631. int len=0;
  3632. _globalSNMPManager.mode = atoi(_serviceManageRequestInfo._snmp_info.mode);
  3633. if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V1")==0)
  3634. {
  3635. _globalSNMPManager.version=0;
  3636. }
  3637. else if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V2c")==0)
  3638. {
  3639. _globalSNMPManager.version=1;
  3640. }
  3641. else if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V3")==0)
  3642. {
  3643. _globalSNMPManager.version=2;
  3644. }
  3645. else
  3646. {
  3647. _globalSNMPManager.version=-1;
  3648. }
  3649. strcpy(_globalSNMPManager.public, _serviceManageRequestInfo._snmp_info.public);
  3650. _globalSNMPManager.trapmode = atoi(_serviceManageRequestInfo._snmp_info.trapmode);
  3651. if (_serviceManageRequestInfo._snmp_info.nms_IP) strcpy(_globalSNMPManager.nmsIP, _serviceManageRequestInfo._snmp_info.nms_IP);
  3652. crypt_t *pc=&_globalSNMPManager.crypt;
  3653. _SNMP_ServerRequestInfo *info=&_serviceManageRequestInfo._snmp_info;
  3654. if (_globalSNMPManager.version == 2)
  3655. {
  3656. if (info->user)
  3657. {
  3658. len = strlen(info->user);
  3659. if (len < 8 || len > 32)
  3660. {
  3661. log_e("snmp user length %d is wrong! correct is [8~32]", len);
  3662. return;
  3663. }
  3664. strcpy(pc->user, info->user);
  3665. if (info->auth_alg)
  3666. {
  3667. strcpy(pc->auth.alg, info->auth_alg);
  3668. if (strlen(info->auth_alg) > 1 && info->auth_passwd)
  3669. {
  3670. len = strlen(info->auth_passwd);
  3671. if (len < 8 || len > 32)
  3672. {
  3673. log_e("snmp auth passwd length %d is wrong! correct is [8~32]", len);
  3674. return;
  3675. }
  3676. strcpy(pc->auth.passwd, info->auth_passwd);
  3677. }
  3678. }
  3679. if (info->priv_alg)
  3680. {
  3681. strcpy(pc->priv.alg, info->priv_alg);
  3682. if (strlen(info->priv_alg) > 1 && info->priv_passwd)
  3683. {
  3684. len = strlen(info->priv_passwd);
  3685. if (len < 8 || len > 32)
  3686. {
  3687. log_e("snmp priv passwd length %d is wrong! correct is [8~32]", len);
  3688. return;
  3689. }
  3690. strcpy(pc->priv.passwd, info->priv_passwd);
  3691. }
  3692. }
  3693. }
  3694. }
  3695. _globalSNMPManager.product_id = __globalDeviceManage._globalDevInfo.product.id;
  3696. if (dev_update_snmp_info(__globalDeviceManage.db,_globalSNMPManager.product_id,&_globalSNMPManager) != 0)
  3697. {
  3698. log_e("update snmp info err.");
  3699. }
  3700. else {
  3701. extern int snmp_config(SNMPManager *m, int quit);
  3702. snmp_config(&_globalSNMPManager, 1);
  3703. }
  3704. }
  3705. //查询snmp_trap信息
  3706. else if(strcmp(_serviceManageRequestInfo.type,"listStQuery")==0)
  3707. {
  3708. }
  3709. else if(strcmp(_serviceManageRequestInfo.type,"StSave")==0)
  3710. {
  3711. }
  3712. //查询ntp信息
  3713. else if(strcmp(_serviceManageRequestInfo.type,"listNtpQuery")==0)
  3714. {
  3715. // 查询NTP信息
  3716. if (0 == dev_get_ntp_info(__globalDeviceManage.db,&_globalNTPManager))
  3717. {
  3718. ack_str = service_ntp_ack_to_json(&_globalNTPManager);
  3719. }
  3720. }
  3721. else if(strcmp(_serviceManageRequestInfo.type,"NtpSave")==0)
  3722. {
  3723. _globalNTPManager.mode = atoi(_serviceManageRequestInfo._ntp_info.mode);
  3724. strcpy(_globalNTPManager.ntpaddress, _serviceManageRequestInfo._ntp_info.ntpaddress);
  3725. strcpy(_globalNTPManager.ntpName, _serviceManageRequestInfo._ntp_info.ntpName);
  3726. _globalNTPManager.port = atoi(_serviceManageRequestInfo._ntp_info.port);
  3727. _globalNTPManager.product_id = __globalDeviceManage._globalDevInfo.product.id;
  3728. strcpy(_globalNTPManager.time, _serviceManageRequestInfo._ntp_info.time);
  3729. const char* key = "GMT/UTC";
  3730. char* p = strstr(_serviceManageRequestInfo._ntp_info.tzone, key);
  3731. if (p == NULL)
  3732. {
  3733. log_e("time zone not contain GMT/UTC");
  3734. return;
  3735. }
  3736. p += strlen(key);
  3737. int value = atoi(p);
  3738. if (value > 12 || value < -12)
  3739. {
  3740. char buf[128];
  3741. memset(buf, 0, sizeof(buf));
  3742. sprintf(buf, "product id = %d timezone: %d, timezone err.", _globalNTPManager.product_id, value);
  3743. log_e(buf);
  3744. return;
  3745. }
  3746. _globalNTPManager.tzone = value;
  3747. value = atoi(_serviceManageRequestInfo._ntp_info.weekday);
  3748. if (value > 12 || value < -11)
  3749. {
  3750. char buf[128];
  3751. memset(buf, 0, sizeof(buf));
  3752. sprintf(buf, "product id = %d weekday err.", _globalNTPManager.product_id);
  3753. log_e(buf);
  3754. return;
  3755. }
  3756. _globalNTPManager.weekday = value;
  3757. if (dev_update_ntp_info(__globalDeviceManage.db,_globalNTPManager.product_id,&_globalNTPManager) != 0)
  3758. {
  3759. log_e("update ntp info err.");
  3760. }
  3761. if (_globalNTPManager.mode == 0)
  3762. {
  3763. char tempstr[10];
  3764. memset(tempstr, 0, sizeof(tempstr));
  3765. p = strstr(_globalNTPManager.time, "-");
  3766. if (p == NULL)
  3767. {
  3768. log_e("find year of local time failed");
  3769. return;
  3770. }
  3771. memset(tempstr, 0, sizeof(tempstr));
  3772. memcpy(tempstr, _globalNTPManager.time, p - _globalNTPManager.time);
  3773. struct tm tm_new;
  3774. tm_new.tm_year = atoi(tempstr);
  3775. char* p1 = strstr(&p[1], "-");
  3776. if (p1 == NULL)
  3777. {
  3778. log_e("find month of local time failed");
  3779. return;
  3780. }
  3781. int nSize= p1 - p - 1;
  3782. memset(tempstr, 0, sizeof(tempstr));
  3783. memcpy(tempstr, &p[1],nSize);
  3784. tm_new.tm_mon = atoi(tempstr);
  3785. p = p1;
  3786. p1 = strstr(&p[2], " ");//个位数第一位有可能是空格所以需要找空格后一个
  3787. if (p1 == NULL)
  3788. {
  3789. log_e("find mday of local time failed");
  3790. return;
  3791. }
  3792. memset(tempstr, 0, sizeof(tempstr));
  3793. memcpy(tempstr, &p[1], p1 - p - 1);
  3794. tm_new.tm_mday = atoi(tempstr);
  3795. p = p1;
  3796. p1 = strstr(&p[0], " ");
  3797. if (p1 == NULL)
  3798. {
  3799. log_e("find split of date and time failed");
  3800. return;
  3801. }
  3802. p = &p1[0];
  3803. p1 = strstr(p, ":");
  3804. if (p1 == NULL)
  3805. {
  3806. log_e("find hour of local time failed");
  3807. return;
  3808. }
  3809. memset(tempstr, 0, sizeof(tempstr));
  3810. memcpy(tempstr, &p[1], p1 - p);
  3811. tm_new.tm_hour = atoi(tempstr);
  3812. p = &p1[1];
  3813. p1 = strstr(p, ":");
  3814. if (p1 == NULL)
  3815. {
  3816. log_e("find minute of local time failed");
  3817. return;
  3818. }
  3819. memset(tempstr, 0, sizeof(tempstr));
  3820. memcpy(tempstr, &p[0], p1 - p);
  3821. tm_new.tm_min = atoi(tempstr);
  3822. p = &p1[1];
  3823. if (p == NULL)
  3824. {
  3825. log_e("find second of local time failed");
  3826. return;
  3827. }
  3828. memset(tempstr, 0, sizeof(tempstr));
  3829. memcpy(tempstr, &p[0], 2);
  3830. tm_new.tm_sec = atoi(tempstr);
  3831. set_time_zone(&tm_new, _globalNTPManager.tzone);
  3832. }
  3833. else if (_globalNTPManager.mode == 1)
  3834. {
  3835. set_zone(_globalNTPManager.tzone);
  3836. sleep(1);
  3837. extern int ntp_config(NTPManager *nm);
  3838. ntp_config(&_globalNTPManager);
  3839. }
  3840. char temp[100];
  3841. if (0==_globalNTPManager.mode)
  3842. {
  3843. //sprintf(temp,"$设置$|$时间$|%s", _globalNTPManager.time);
  3844. sprintf(temp,"Set Date & Time %s", _globalNTPManager.time);
  3845. }
  3846. else{
  3847. // sprintf(temp,"$设置$|$时间$|NTP %s", _globalNTPManager.ntpaddress);
  3848. sprintf(temp,"Set Date & Time sync from NTP %s", _globalNTPManager.ntpaddress);
  3849. }
  3850. dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  3851. ack_str = ctrl_sts_ack_to_json();
  3852. if (_serviceManageRequestInfo._ntp_info.mode) free(_serviceManageRequestInfo._ntp_info.mode);
  3853. if (_serviceManageRequestInfo._ntp_info.port) free(_serviceManageRequestInfo._ntp_info.port);
  3854. if (_serviceManageRequestInfo._ntp_info.tzone) free(_serviceManageRequestInfo._ntp_info.tzone);
  3855. if (_serviceManageRequestInfo._ntp_info.weekday) free(_serviceManageRequestInfo._ntp_info.weekday);
  3856. }
  3857. // 定时设置
  3858. else if (strcmp(_serviceManageRequestInfo.type, "listDsQuery") == 0)//||strcmp(_serviceManageRequestInfo.type, "listRlQuery") == 0)
  3859. {
  3860. // 查询通道信息
  3861. /*
  3862. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  3863. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  3864. {
  3865. if (_globalPowerMangerTemp->product_ch_id == chn_id)
  3866. break;
  3867. }
  3868. */
  3869. // 返回定时信息
  3870. lock_s_hold(LOCK_ID_POWER_TIMER);
  3871. ack_str = service_ds_all_ack_to_json(_serviceManageRequestInfo._ds_info.channelDate,
  3872. _serviceManageRequestInfo._ds_info.productChName,
  3873. _serviceManageRequestInfo._ds_info.productChId,
  3874. &__globalDeviceManage._globalPowerManger);
  3875. lock_s_release(LOCK_ID_POWER_TIMER);
  3876. }
  3877. else if (strcmp(_serviceManageRequestInfo.type, "dsSave") == 0)
  3878. {
  3879. lock_s_hold(LOCK_ID_POWER_TIMER);
  3880. int chn_id = -1;
  3881. for (int i = 0; i < 64; i++)
  3882. {
  3883. chn_id = _serviceManageRequestInfo._ds_info.productChIds[i];
  3884. if (chn_id <= 0)
  3885. {
  3886. break;
  3887. }
  3888. // 查询通道信息
  3889. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  3890. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  3891. {
  3892. if (_globalPowerMangerTemp->product_ch_id == chn_id)
  3893. break;
  3894. }
  3895. int start_year, start_month, start_day;
  3896. int end_year, end_month, end_day;
  3897. char current_date[11]; // 足够存储 "YYYY-MM-DD\0"
  3898. // 解析起始和结束日期
  3899. sscanf(_serviceManageRequestInfo._ds_info.StartDate, "%d-%d-%d", &start_year, &start_month, &start_day);
  3900. sscanf(_serviceManageRequestInfo._ds_info.Enddate, "%d-%d-%d", &end_year, &end_month, &end_day);
  3901. // 复制起始日期到current_date
  3902. strcpy(current_date, _serviceManageRequestInfo._ds_info.StartDate);
  3903. // 循环直到或超过结束日期
  3904. int done = 0;
  3905. while (!done)
  3906. {
  3907. // 当前日期
  3908. _PowerDSManage_t *new_data = (_PowerDSManage_t *)calloc(1, sizeof(_PowerDSManage_t));
  3909. if(!new_data) {
  3910. log_e("___ new_data calloc failed!\n");
  3911. break;
  3912. }
  3913. memset(new_data, 0, sizeof(_PowerDSManage_t));
  3914. strcpy(new_data->ds_date, current_date);
  3915. strcpy(new_data->ds_time, _serviceManageRequestInfo._ds_info.channelTime);
  3916. new_data->set_stats = atoi(_serviceManageRequestInfo._ds_info.setStatus);
  3917. new_data->ds_is_triggered = 0;
  3918. list_add_tail(&new_data->list, &_globalPowerMangerTemp->list_DS);
  3919. dev_insert_power_ds(__globalDeviceManage.db, 1, chn_id, new_data);
  3920. // 解析当前日期为年、月、日
  3921. sscanf(current_date, "%d-%d-%d", &start_year, &start_month, &start_day);
  3922. // 增加一天
  3923. add_day_to_date(current_date, &start_year, &start_month, &start_day);
  3924. // 检查是否超过结束日期
  3925. if (start_year > end_year ||
  3926. (start_year == end_year && start_month > end_month) ||
  3927. (start_year == end_year && start_month == end_month && start_day > end_day))
  3928. {
  3929. done = 1; // 超过结束日期,退出循环
  3930. }
  3931. }
  3932. }
  3933. lock_s_release(LOCK_ID_POWER_TIMER);
  3934. char temp[100];
  3935. //sprintf(temp, "$设置$|定时$");
  3936. sprintf(temp, "Add scheduled tasks");
  3937. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  3938. ack_str = ctrl_sts_ack_to_json();
  3939. }
  3940. //批量删除
  3941. else if (strcmp(_serviceManageRequestInfo.type, "deleteDs")==0)
  3942. {
  3943. int nChID = 0;
  3944. char pcDate[16];
  3945. char pcTime[16];
  3946. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  3947. _PowerDSManage_t* _powerDsManageTemp=NULL;;
  3948. char *pCharData;
  3949. lock_s_hold(LOCK_ID_POWER_TIMER);
  3950. for (int i = 0; i < 64; i++)
  3951. {
  3952. pCharData = _serviceManageRequestInfo._ds_info.idx[i];
  3953. if (pCharData == NULL || *pCharData == '\0')
  3954. {
  3955. break;
  3956. }
  3957. sscanf(pCharData, "%d %s %s", &nChID, pcDate, pcTime);
  3958. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  3959. {
  3960. if (_globalPowerMangerTemp->product_ch_id == nChID)
  3961. break;
  3962. }
  3963. list_for_each_entry(_powerDsManageTemp, &_globalPowerMangerTemp->list_DS, list)
  3964. {
  3965. if (strcmp(_powerDsManageTemp->ds_date, pcDate) == 0&&strcmp(_powerDsManageTemp->ds_time, pcTime) == 0)
  3966. {
  3967. dev_delete_power_ds(__globalDeviceManage.db, 1, _globalPowerMangerTemp->product_ch_id, _powerDsManageTemp);
  3968. list_del(&_powerDsManageTemp->list);
  3969. free(_powerDsManageTemp);
  3970. _powerDsManageTemp=NULL;
  3971. break;
  3972. }
  3973. }
  3974. }
  3975. lock_s_release(LOCK_ID_POWER_TIMER);
  3976. char temp[100];
  3977. // sprintf(temp, "$删除$|定时$");
  3978. sprintf(temp, "Delete scheduled tasks");
  3979. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  3980. ack_str = ctrl_sts_ack_to_json();
  3981. }
  3982. // 时序设置
  3983. else if (strcmp(_serviceManageRequestInfo.type, "listSxQuery") == 0) // 查询
  3984. {
  3985. bool bflag = false;
  3986. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  3987. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  3988. {
  3989. if (_globalPowerMangerTemp->product_id == atoi(_serviceManageRequestInfo._posix_info.productId))
  3990. {
  3991. bflag = true;
  3992. break;
  3993. }
  3994. }
  3995. if (bflag == false) return;
  3996. ack_str = service_sx_ack_to_json(_globalPowerMangerTemp->product_id, &__globalDeviceManage._globalPowerManger);
  3997. if (_serviceManageRequestInfo._posix_info.productId) free(_serviceManageRequestInfo._posix_info.productId);
  3998. }
  3999. else if(strcmp(_serviceManageRequestInfo.type,"sXTimeSave")==0)//保存时间间隔
  4000. {
  4001. int chn_id = atoi(_serviceManageRequestInfo._posix_info.productChId);
  4002. int id = atoi(_serviceManageRequestInfo._posix_info.productId);
  4003. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  4004. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4005. {
  4006. if (_globalPowerMangerTemp->product_id == id && _globalPowerMangerTemp->product_ch_id == chn_id)
  4007. {
  4008. int value = atoi(_serviceManageRequestInfo._posix_info.firingInterval);
  4009. if (value > 65535)
  4010. {
  4011. char buf[128];
  4012. memset(buf, 0, sizeof(buf));
  4013. sprintf(buf, "product id = %d, ch id = %d start delay err.", _globalPowerMangerTemp->product_id, _globalPowerMangerTemp->product_ch_id);
  4014. log_e(buf);
  4015. continue;
  4016. }
  4017. _globalPowerMangerTemp->product_ch_start_delay = value;
  4018. value = atoi(_serviceManageRequestInfo._posix_info.closeInterval);
  4019. if (value > 65535)
  4020. {
  4021. char buf[128];
  4022. memset(buf, 0, sizeof(buf));
  4023. sprintf(buf, "product id = %d, ch id = %d stop delay err.", _globalPowerMangerTemp->product_id, _globalPowerMangerTemp->product_ch_id);
  4024. log_e(buf);
  4025. continue;
  4026. }
  4027. _globalPowerMangerTemp->product_ch_stop_delay = value;
  4028. ret=g_switch_set_all_start_time_delay(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,_globalPowerMangerTemp->product_ch_addr,_globalPowerMangerTemp->product_ch_start_delay);
  4029. if (ret<0)
  4030. {
  4031. usleep(50000);
  4032. g_switch_set_all_start_time_delay(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,_globalPowerMangerTemp->product_ch_addr,_globalPowerMangerTemp->product_ch_start_delay);
  4033. /* code */
  4034. }
  4035. ret = g_switch_set_all_stop_time_delay(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,_globalPowerMangerTemp->product_ch_addr,_globalPowerMangerTemp->product_ch_stop_delay);
  4036. if (ret<0)
  4037. {
  4038. usleep(50000);
  4039. g_switch_set_all_stop_time_delay(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,_globalPowerMangerTemp->product_ch_addr,_globalPowerMangerTemp->product_ch_stop_delay);
  4040. /* code */
  4041. }
  4042. if (dev_update_power_manage_genera_info(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,_globalPowerMangerTemp->product_ch_id,_globalPowerMangerTemp) != 0)
  4043. {
  4044. log_e("update pwr general data err.");
  4045. }
  4046. break;
  4047. }
  4048. }
  4049. ack_str = ctrl_sts_ack_to_json();
  4050. if (_serviceManageRequestInfo._posix_info.productId) free(_serviceManageRequestInfo._posix_info.productId);
  4051. if (_serviceManageRequestInfo._posix_info.closeInterval) free(_serviceManageRequestInfo._posix_info.closeInterval);
  4052. if (_serviceManageRequestInfo._posix_info.firingInterval) free(_serviceManageRequestInfo._posix_info.firingInterval);
  4053. if (_serviceManageRequestInfo._posix_info.productChId) free(_serviceManageRequestInfo._posix_info.productChId);
  4054. }
  4055. // 分组设置
  4056. else if (strcmp(_serviceManageRequestInfo.type, "fzSave") == 0) // 分组设置
  4057. {
  4058. int nMode = atoi(_serviceManageRequestInfo._group_info.mode);
  4059. GlobalPowerManger* _globalPowerMangerTemp = NULL ;
  4060. int chNum= 0;
  4061. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4062. {
  4063. chNum++;
  4064. }
  4065. //初始化分组-(未析构)
  4066. GroupInfo group_list;
  4067. INIT_LIST_HEAD(&group_list.list);
  4068. if (0 == nMode)//相邻分组方式
  4069. {
  4070. int xlnum = atoi(_serviceManageRequestInfo._group_info.xlnumber);
  4071. if (xlnum > 0)
  4072. {
  4073. int pGroupNum = chNum/xlnum;
  4074. if (chNum % xlnum>0)
  4075. {
  4076. pGroupNum++;
  4077. }
  4078. dev_delete__group_info(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id);
  4079. for (size_t i = 0; i < pGroupNum; i++)
  4080. {
  4081. GroupInfo *new_group = (GroupInfo *)malloc(sizeof(GroupInfo));
  4082. // 清空数据
  4083. memset(new_group, 0, sizeof(GroupInfo));
  4084. if (!new_group)
  4085. {
  4086. break;
  4087. }
  4088. new_group->product_id = __globalDeviceManage._globalDevInfo.product.id;
  4089. new_group->GroupID = i + 1;
  4090. sprintf(new_group->GroupName, "Group%d", i + 1);
  4091. int j = 0;
  4092. GlobalPowerManger *_globalPowerMangerTemp1 = NULL;
  4093. list_for_each_entry(_globalPowerMangerTemp1, &__globalDeviceManage._globalPowerManger.list, list)
  4094. {
  4095. if (j >= i * xlnum && j < (i + 1) * xlnum)
  4096. {
  4097. char chTemp[64];
  4098. sprintf(chTemp, "%d,", _globalPowerMangerTemp1->product_ch_id);
  4099. strcat(new_group->GroupChList, chTemp);
  4100. }
  4101. j++;
  4102. }
  4103. list_add_tail(&new_group->list, &group_list.list);
  4104. dev_insert_group_info(__globalDeviceManage.db, new_group);
  4105. }
  4106. }
  4107. }
  4108. else if (1 == nMode)//间隔分组方式
  4109. {
  4110. int jgnum = atoi(_serviceManageRequestInfo._group_info.jgnumber);
  4111. int pGroupNum = jgnum+1;
  4112. if (pGroupNum>chNum)
  4113. {
  4114. pGroupNum=chNum;
  4115. }
  4116. if (jgnum >= 0 && pGroupNum> 0 )
  4117. {
  4118. dev_delete__group_info(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id);
  4119. for (size_t i = 0; i < pGroupNum; i++)
  4120. {
  4121. GroupInfo *new_group = (GroupInfo *)malloc(sizeof(GroupInfo));
  4122. // 清空数据
  4123. memset(new_group, 0, sizeof(GroupInfo));
  4124. if (!new_group)
  4125. {
  4126. break;
  4127. }
  4128. new_group->product_id = __globalDeviceManage._globalDevInfo.product.id;
  4129. new_group->GroupID = i + 1;
  4130. sprintf(new_group->GroupName, "Group%d", i + 1);
  4131. int j = i;
  4132. int m = 0;
  4133. GlobalPowerManger *_globalPowerMangerTemp1 = NULL;
  4134. list_for_each_entry(_globalPowerMangerTemp1, &__globalDeviceManage._globalPowerManger.list, list)
  4135. {
  4136. if (m==j)
  4137. {
  4138. char chTemp[64];
  4139. sprintf(chTemp, "%d,", _globalPowerMangerTemp1->product_ch_id);
  4140. strcat(new_group->GroupChList, chTemp);
  4141. j=j+jgnum+1;
  4142. }
  4143. m++;
  4144. }
  4145. list_add_tail(&new_group->list, &group_list.list);
  4146. dev_insert_group_info(__globalDeviceManage.db, new_group);
  4147. }
  4148. }
  4149. }
  4150. GroupInfo *node;
  4151. GroupInfo *next;
  4152. list_for_each_entry_safe(node, next, &group_list.list, list)
  4153. {
  4154. list_del(&node->list);
  4155. free(node);
  4156. }
  4157. ack_str = ctrl_sts_ack_to_json();
  4158. }
  4159. else if (strcmp(_serviceManageRequestInfo.type, "listFzQuery") == 0) // 分组查询
  4160. {
  4161. // 查询分组信息
  4162. GroupInfo group_list;
  4163. INIT_LIST_HEAD(&group_list.list);
  4164. if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
  4165. {
  4166. // 返回分组信息
  4167. ack_str = service_group_ack_to_json(&group_list);
  4168. }
  4169. GroupInfo *node;
  4170. GroupInfo *next;
  4171. list_for_each_entry_safe(node, next, &group_list.list, list)
  4172. {
  4173. list_del(&node->list);
  4174. free(node);
  4175. }
  4176. }
  4177. else if (strcmp(_serviceManageRequestInfo.type, "updateFz") == 0) // 分组修改
  4178. {
  4179. if (_serviceManageRequestInfo._group_info.opergroupid && _serviceManageRequestInfo._group_info.updategroupName)
  4180. {
  4181. int ngroup = atoi(_serviceManageRequestInfo._group_info.opergroupid);
  4182. // 查询分组信息
  4183. GroupInfo group_list;
  4184. INIT_LIST_HEAD(&group_list.list);
  4185. if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
  4186. {
  4187. GroupInfo *_GroupInfoTemp = NULL;
  4188. list_for_each_entry(_GroupInfoTemp, &group_list.list, list)
  4189. {
  4190. if (_GroupInfoTemp->GroupID == ngroup)
  4191. {
  4192. strncpy(_GroupInfoTemp->GroupName, _serviceManageRequestInfo._group_info.updategroupName, sizeof(_GroupInfoTemp->GroupName)-1);
  4193. _GroupInfoTemp->GroupName[sizeof(_GroupInfoTemp->GroupName) - 1] = '\0';
  4194. dev_update_group_info(__globalDeviceManage.db, _GroupInfoTemp);
  4195. }
  4196. }
  4197. }
  4198. GroupInfo *node;
  4199. GroupInfo *next;
  4200. list_for_each_entry_safe(node, next, &group_list.list, list)
  4201. {
  4202. list_del(&node->list);
  4203. free(node);
  4204. }
  4205. char temp[100];
  4206. // sprintf(temp, "$打开$|$分组$|%d", ngroup);
  4207. sprintf(temp, "Update group %d", ngroup);
  4208. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4209. }
  4210. ack_str = ctrl_sts_ack_to_json();
  4211. }
  4212. else if (strcmp(_serviceManageRequestInfo.type, "fzOpen") == 0) // 分组开
  4213. {
  4214. unsigned short pAddrCtrl[50];
  4215. memset(pAddrCtrl,0,sizeof(pAddrCtrl));
  4216. int ngroup= atoi(_serviceManageRequestInfo._group_info.opergroupid);
  4217. // 查询分组信息
  4218. GroupInfo group_list;
  4219. INIT_LIST_HEAD(&group_list.list);
  4220. if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
  4221. {
  4222. GroupInfo *_GroupInfoTemp = NULL;
  4223. list_for_each_entry(_GroupInfoTemp, &group_list.list, list)
  4224. {
  4225. if (_GroupInfoTemp->GroupID == ngroup)
  4226. {
  4227. if (__globalDeviceManage._globalDevInfo.product.pwr_type == 1)
  4228. { // 直流版本功能
  4229. char *pCHlist;
  4230. char *pCHListSave;
  4231. pCHlist=strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
  4232. while(pCHlist!=NULL)
  4233. {
  4234. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  4235. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4236. {
  4237. if (_globalPowerMangerTemp == NULL)
  4238. break;
  4239. if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
  4240. {
  4241. log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
  4242. pAddrCtrl[(int)_globalPowerMangerTemp->product_saddr] |= 1 << (_globalPowerMangerTemp->product_ch_addr - 1);
  4243. break;
  4244. }
  4245. }
  4246. pCHlist=strtok_r(NULL, ",",&pCHListSave);
  4247. }
  4248. for (size_t i = 0; i < 50; i++)
  4249. {
  4250. if (pAddrCtrl[i] != 0)
  4251. {
  4252. ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4253. i,
  4254. pAddrCtrl[i],
  4255. 1);
  4256. if (ret < 0)
  4257. {
  4258. ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4259. i,
  4260. pAddrCtrl[i], 1);
  4261. }
  4262. }
  4263. }
  4264. }
  4265. else
  4266. {
  4267. char *pCHlist;
  4268. char *pCHListSave;
  4269. pCHlist = strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
  4270. //其他版本功能
  4271. while (pCHlist!=NULL)
  4272. {
  4273. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  4274. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4275. {
  4276. if (_globalPowerMangerTemp == NULL)
  4277. break;
  4278. if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
  4279. {
  4280. log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
  4281. ret= g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4282. _globalPowerMangerTemp,
  4283. _globalPowerMangerTemp->product_saddr,
  4284. _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
  4285. 1,true);
  4286. if (ret < 0)
  4287. {
  4288. ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4289. _globalPowerMangerTemp,
  4290. _globalPowerMangerTemp->product_saddr,
  4291. _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
  4292. 1,true);
  4293. }
  4294. break;
  4295. }
  4296. }
  4297. pCHlist = strtok_r(NULL, ",",&pCHListSave);
  4298. }
  4299. }
  4300. }
  4301. }
  4302. }
  4303. GroupInfo *node;
  4304. GroupInfo *next;
  4305. list_for_each_entry_safe(node, next, &group_list.list, list)
  4306. {
  4307. list_del(&node->list);
  4308. free(node);
  4309. }
  4310. char temp[100];
  4311. //sprintf(temp, "$打开$|$分组$|%d", ngroup);
  4312. sprintf(temp, "Open group %d", ngroup);
  4313. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4314. ack_str = ctrl_sts_ack_to_json();
  4315. }
  4316. else if (strcmp(_serviceManageRequestInfo.type, "fzClose") == 0) // 分组关
  4317. {
  4318. unsigned short pAddrCtrl[50];
  4319. memset(pAddrCtrl, 0, sizeof(pAddrCtrl));
  4320. int ngroup = atoi(_serviceManageRequestInfo._group_info.opergroupid);
  4321. // 查询分组信息
  4322. GroupInfo group_list;
  4323. INIT_LIST_HEAD(&group_list.list);
  4324. if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
  4325. {
  4326. GroupInfo *_GroupInfoTemp = NULL;
  4327. list_for_each_entry(_GroupInfoTemp, &group_list.list, list)
  4328. {
  4329. if (_GroupInfoTemp->GroupID == ngroup)
  4330. {
  4331. /* code */
  4332. if (__globalDeviceManage._globalDevInfo.product.pwr_type == 1)
  4333. {
  4334. char *pCHlist;
  4335. char *pCHListSave;
  4336. pCHlist = strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
  4337. // 直流版本功能
  4338. while (pCHlist!=NULL)
  4339. {
  4340. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  4341. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4342. {
  4343. if (_globalPowerMangerTemp == NULL)
  4344. break;
  4345. if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
  4346. {
  4347. log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
  4348. pAddrCtrl[(int)_globalPowerMangerTemp->product_saddr] |= 1 << (_globalPowerMangerTemp->product_ch_addr-1);
  4349. break;
  4350. }
  4351. }
  4352. pCHlist = strtok_r(NULL, ",",&pCHListSave);
  4353. }
  4354. for (size_t i = 0; i < 50; i++)
  4355. {
  4356. if (pAddrCtrl[i] != 0)
  4357. {
  4358. ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4359. i,
  4360. pAddrCtrl[i],
  4361. 0);
  4362. if (ret < 0)
  4363. {
  4364. ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4365. i,
  4366. pAddrCtrl[i],
  4367. 0);
  4368. /* code */
  4369. }
  4370. }
  4371. }
  4372. }
  4373. else
  4374. {
  4375. char *pCHlist;
  4376. char *pCHListSave;
  4377. pCHlist=strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
  4378. while (pCHlist!=NULL)
  4379. {
  4380. GlobalPowerManger *_globalPowerMangerTemp = NULL;
  4381. list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4382. {
  4383. if (_globalPowerMangerTemp == NULL)
  4384. break;
  4385. if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
  4386. {
  4387. log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
  4388. ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4389. _globalPowerMangerTemp,
  4390. _globalPowerMangerTemp->product_saddr,
  4391. _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
  4392. 0,true);
  4393. if (ret < 0)
  4394. {
  4395. ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
  4396. _globalPowerMangerTemp,
  4397. _globalPowerMangerTemp->product_saddr,
  4398. _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
  4399. 0,true);
  4400. }
  4401. break;
  4402. }
  4403. }
  4404. pCHlist = strtok_r(NULL, ",",&pCHListSave);
  4405. }
  4406. }
  4407. }
  4408. }
  4409. }
  4410. GroupInfo *node;
  4411. GroupInfo *next;
  4412. list_for_each_entry_safe(node, next, &group_list.list, list)
  4413. {
  4414. list_del(&node->list);
  4415. free(node);
  4416. }
  4417. char temp[100];
  4418. // sprintf(temp, "$关闭$|$分组$|%d", ngroup);
  4419. sprintf(temp, "Close group %d", ngroup);
  4420. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4421. ack_str = ctrl_sts_ack_to_json();
  4422. }
  4423. else if (strcmp(_serviceManageRequestInfo.type, "gysbSave") == 0)
  4424. {
  4425. if (_serviceManageRequestInfo.productName)
  4426. {
  4427. /* code */
  4428. strncpy(__globalDeviceManage._globalDevInfo.product.name, _serviceManageRequestInfo.productName, sizeof(__globalDeviceManage._globalDevInfo.product.name) - 1);
  4429. __globalDeviceManage._globalDevInfo.product.name[sizeof(__globalDeviceManage._globalDevInfo.product.name) - 1] = '\0'; // 确保字符串以null结尾
  4430. paras_save();
  4431. char log_buff[256];
  4432. sprintf(log_buff, "Modify device information!");
  4433. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
  4434. }
  4435. ack_str = ctrl_sts_ack_to_json();
  4436. }
  4437. //cJSON_Delete(cjson);
  4438. http_reply(conn, 200, ack_str);
  4439. cJSON_free((void*)ack_str);
  4440. }
  4441. static void serviceManagementUpload(void *conn)
  4442. {
  4443. int idx,r=-1 ;
  4444. char path[200];
  4445. buf_t buf;
  4446. char *p,*tok="idx=";
  4447. mg_stream_t *s=(mg_stream_t*)conn;
  4448. struct mg_http_message *hm=s->ev_data;
  4449. GlobalDeviceManager *dm=&__globalDeviceManage;
  4450. if(strstr(hm->uri.buf, "type=importData")) { //firmware upload
  4451. sprintf(path, "%s/%s", DOWN_FOLDER, CERT_FILE);
  4452. r = upload_file(conn, path);
  4453. if(r==0) {
  4454. p = strstr(hm->uri.buf, tok);
  4455. if(p) {
  4456. idx = atoi(p+strlen(tok));
  4457. if(idx<0 || idx>=MQTT_SER_MAX) {
  4458. printf("_____ wrong idx, %d\n", idx);
  4459. return;
  4460. }
  4461. r = file_load(path, &buf);
  4462. if(r==0) {
  4463. mqtt_server_t *ser=&dm->mqttInfo.ser[idx];
  4464. if(buf.dlen<=sizeof(ser->cert)) {
  4465. memcpy(ser->cert, buf.buf, buf.dlen);
  4466. dev_mqtt_update_ser(dm->db, ser);
  4467. }
  4468. else {
  4469. printf("_____ cert is too long, %d\n", buf.dlen);
  4470. }
  4471. free(buf.buf);
  4472. }
  4473. }
  4474. }
  4475. }
  4476. }
  4477. /// @brief 系统管理
  4478. /// @param stream
  4479. static void systemManagent(void *conn)
  4480. {
  4481. int ret = -1 ;
  4482. cJSON *cjson;
  4483. _UserRoleManageInfo _UserRoleManageRequestInfo;
  4484. char* ack_str = NULL ;
  4485. mg_stream_t *s=(mg_stream_t*)conn;
  4486. struct mg_http_message *hm=s->ev_data;
  4487. char *rx_body_buf = (char*)hm->body.buf;
  4488. //转换json参数
  4489. cjson = json_to_user_manage(rx_body_buf,&_UserRoleManageRequestInfo);
  4490. if(cjson==NULL)
  4491. {
  4492. http_reply(conn, 400, NULL);
  4493. return ;
  4494. }
  4495. //获取角色信息
  4496. else if(strcmp(_UserRoleManageRequestInfo.type,"getUser")==0)
  4497. {
  4498. // 查询用户信息
  4499. UserInfo User_list;
  4500. INIT_LIST_HEAD(&User_list.list);
  4501. // 查询角色信息
  4502. RoleInfo Role_list;
  4503. INIT_LIST_HEAD(&Role_list.list);
  4504. ack_str = ctrl_sts_ack_to_json();
  4505. if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) && 0 == dev_get_role_info(__globalDeviceManage.db, &Role_list)
  4506. /*&& strcmp(_UserRoleManageRequestInfo.userName,"")!=0*/)
  4507. {
  4508. char *nName=getUserNamebyToken(_UserRoleManageRequestInfo.user_token);
  4509. if (nName!=NULL)
  4510. {
  4511. ack_str = service_userRole_ack_to_json(nName,&User_list, &Role_list);
  4512. }
  4513. }
  4514. UserInfo *node;
  4515. UserInfo *next;
  4516. list_for_each_entry_safe(node, next, &User_list.list, list)
  4517. {
  4518. list_del(&node->list);
  4519. free(node);
  4520. }
  4521. RoleInfo *node1;
  4522. RoleInfo *next1;
  4523. list_for_each_entry_safe(node1, next1, &Role_list.list, list)
  4524. {
  4525. list_del(&node1->list);
  4526. free(node1);
  4527. }
  4528. }
  4529. //查询用户列表
  4530. else if(strcmp(_UserRoleManageRequestInfo.type,"listUserQuery")==0)
  4531. {
  4532. // 查询用户信息
  4533. UserInfo User_list;
  4534. INIT_LIST_HEAD(&User_list.list);
  4535. // 查询角色信息
  4536. RoleInfo Role_list;
  4537. INIT_LIST_HEAD(&Role_list.list);
  4538. ack_str = ctrl_sts_ack_to_json();
  4539. if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) && 0 == dev_get_role_info(__globalDeviceManage.db, &Role_list)
  4540. /*&& strcmp(_UserRoleManageRequestInfo.userName,"")!=0*/)
  4541. {
  4542. char *nName = getUserNamebyToken(_UserRoleManageRequestInfo.user_token);
  4543. if (nName != NULL)
  4544. {
  4545. ack_str = service_userQuery_ack_to_json(nName, &User_list, &Role_list,_UserRoleManageRequestInfo.userName,_UserRoleManageRequestInfo.pagenum,_UserRoleManageRequestInfo.pagesize);
  4546. }
  4547. }
  4548. UserInfo *node;
  4549. UserInfo *next;
  4550. list_for_each_entry_safe(node, next, &User_list.list, list)
  4551. {
  4552. list_del(&node->list);
  4553. free(node);
  4554. }
  4555. RoleInfo *node1;
  4556. RoleInfo *next1;
  4557. list_for_each_entry_safe(node1, next1, &Role_list.list, list)
  4558. {
  4559. list_del(&node1->list);
  4560. free(node1);
  4561. }
  4562. }
  4563. //删除用户
  4564. else if(strcmp(_UserRoleManageRequestInfo.type,"deleteUser")==0)
  4565. {
  4566. bool CurUserFlag=false;
  4567. char *nName = getUserNamebyToken(_UserRoleManageRequestInfo.user_token);
  4568. if (nName != NULL)
  4569. {
  4570. UserInfo User_list;
  4571. INIT_LIST_HEAD(&User_list.list);
  4572. if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list))
  4573. {
  4574. UserInfo *_UserInfoTemp = NULL;
  4575. list_for_each_entry(_UserInfoTemp, &User_list.list, list)
  4576. {
  4577. if (strcmp(_UserInfoTemp->userName, nName) == 0)
  4578. {
  4579. if (_UserRoleManageRequestInfo.userIds == _UserInfoTemp->userID)
  4580. {
  4581. CurUserFlag = true;
  4582. break;
  4583. }
  4584. break;
  4585. }
  4586. }
  4587. }
  4588. UserInfo *node;
  4589. UserInfo *next;
  4590. list_for_each_entry_safe(node, next, &User_list.list, list)
  4591. {
  4592. list_del(&node->list);
  4593. free(node);
  4594. }
  4595. }
  4596. if (!CurUserFlag)
  4597. {
  4598. // char *pUserlist;
  4599. // char *pUserListSave;
  4600. // pUserlist = strtok_r(_UserRoleManageRequestInfo.userIds, ",", &pUserListSave);
  4601. // while (pUserlist != NULL)
  4602. //{
  4603. dev_delete_user_info(__globalDeviceManage.db, _UserRoleManageRequestInfo.userIds);
  4604. // pUserlist = strtok_r(NULL, ",", &pUserListSave);
  4605. // }
  4606. char temp[100];
  4607. // sprintf(temp, "$删除$|$用户$|%d", _UserRoleManageRequestInfo.userIds);
  4608. sprintf(temp, "Delete user %d", _UserRoleManageRequestInfo.userIds);
  4609. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4610. ack_str = ctrl_sts_ack_to_json();
  4611. }
  4612. else
  4613. {
  4614. ack_str = ctrl_breaker_err_ack_to_json("Can't delete logged in users!!!!");
  4615. }
  4616. }
  4617. //添加用户
  4618. else if(strcmp(_UserRoleManageRequestInfo.type,"addUser")==0)
  4619. {
  4620. //未析构
  4621. UserInfo *new_user = (UserInfo *)malloc(sizeof(UserInfo));
  4622. memset(new_user,0,sizeof(UserInfo));
  4623. new_user->product_id = __globalDeviceManage._globalDevInfo.product.id;
  4624. int nMaxUserNum=1;
  4625. bool existFlag=false;
  4626. UserInfo User_list;
  4627. INIT_LIST_HEAD(&User_list.list);
  4628. // 查询角色信息
  4629. RoleInfo Role_list;
  4630. INIT_LIST_HEAD(&Role_list.list);
  4631. if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) && 0 == dev_get_role_info(__globalDeviceManage.db, &Role_list))
  4632. {
  4633. UserInfo *_UserInfoTemp = NULL;
  4634. list_for_each_entry(_UserInfoTemp, &User_list.list, list)
  4635. {
  4636. if (_UserInfoTemp == NULL)
  4637. break;
  4638. if (_UserInfoTemp->userID >= nMaxUserNum)
  4639. {
  4640. nMaxUserNum=_UserInfoTemp->userID;
  4641. }
  4642. if (strcmp(_UserInfoTemp->userName, _UserRoleManageRequestInfo.userName) == 0)
  4643. {
  4644. existFlag = true;
  4645. break;
  4646. }
  4647. }
  4648. if (existFlag)
  4649. {
  4650. ack_str = ctrl_breaker_err_ack_to_json("User already exists!");
  4651. }
  4652. else
  4653. {
  4654. new_user->userID = nMaxUserNum + 1;
  4655. strcpy(new_user->userName, _UserRoleManageRequestInfo.userName);
  4656. strcpy(new_user->userPW, _UserRoleManageRequestInfo.password);
  4657. strcpy(new_user->nickName, _UserRoleManageRequestInfo.nickName);
  4658. new_user->roleID = _UserRoleManageRequestInfo.roleId; // GetRoleIDByName(_UserRoleManageRequestInfo.roleName,&Role_list);
  4659. struct tm *t;
  4660. struct timeval tv;
  4661. gettimeofday(&tv, NULL);
  4662. t = localtime(&tv.tv_sec);
  4663. sprintf(new_user->createTime, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  4664. dev_insert_user_info(__globalDeviceManage.db, new_user);
  4665. char temp[100];
  4666. // sprintf(temp, "$添加$|$用户$|%s", _UserRoleManageRequestInfo.userName);
  4667. sprintf(temp, "Add user %s", _UserRoleManageRequestInfo.userName);
  4668. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4669. ack_str=ctrl_sts_ack_to_json();
  4670. }
  4671. }
  4672. free(new_user);
  4673. UserInfo *node;
  4674. UserInfo *next;
  4675. list_for_each_entry_safe(node, next, &User_list.list, list)
  4676. {
  4677. list_del(&node->list);
  4678. free(node);
  4679. }
  4680. RoleInfo *node1;
  4681. RoleInfo *next1;
  4682. list_for_each_entry_safe(node1, next1, &Role_list.list, list)
  4683. {
  4684. list_del(&node1->list);
  4685. free(node1);
  4686. }
  4687. }
  4688. //更新用户信息
  4689. else if(strcmp(_UserRoleManageRequestInfo.type,"updateUser")==0)
  4690. {
  4691. //未析构
  4692. UserInfo *new_user = (UserInfo *)malloc(sizeof(UserInfo));
  4693. memset(new_user,0,sizeof(UserInfo));
  4694. new_user->product_id = __globalDeviceManage._globalDevInfo.product.id;
  4695. new_user->userID = _UserRoleManageRequestInfo.userId;
  4696. strcpy(new_user->userName , _UserRoleManageRequestInfo.userName);
  4697. strcpy(new_user->userPW , _UserRoleManageRequestInfo.password);
  4698. strcpy(new_user->nickName , _UserRoleManageRequestInfo.nickName);
  4699. new_user->roleID = _UserRoleManageRequestInfo.roleId;
  4700. strcpy(new_user->createTime , _UserRoleManageRequestInfo.createTime);
  4701. dev_update_user_info(__globalDeviceManage.db, new_user);
  4702. free(new_user);
  4703. char temp[100];
  4704. //sprintf(temp, "$修改$|$用户$|%s", _UserRoleManageRequestInfo.userName);
  4705. sprintf(temp, "Update user %s", _UserRoleManageRequestInfo.userName);
  4706. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4707. ack_str=ctrl_sts_ack_to_json();
  4708. }
  4709. cJSON_Delete(cjson);
  4710. http_reply(conn, 200, ack_str);
  4711. cJSON_free((void*)ack_str);
  4712. }
  4713. /// @brief 设备重启
  4714. /// @param stream
  4715. static void equipmentMintenance(void *conn)
  4716. {
  4717. int ret = -1 ;
  4718. char* ack_str = NULL ;
  4719. cJSON *cjson;
  4720. _OverBackStsInfo _over_back_sts_info; //设备总体状态信息
  4721. _Equipment_Mintenance _Over_equipment_mintenance;
  4722. mg_stream_t *s=(mg_stream_t*)conn;
  4723. struct mg_http_message *hm=s->ev_data;
  4724. char *rx_body_buf = (char*)hm->body.buf;
  4725. //转换json参数
  4726. cjson = json_to_over_equipment_mintenance(rx_body_buf,&_Over_equipment_mintenance);
  4727. if(cjson==NULL)
  4728. {
  4729. http_reply(conn, 400, NULL);
  4730. return ;
  4731. }
  4732. //查询设备列表
  4733. if(strcmp(_Over_equipment_mintenance.type,"listSbRestartQuery")==0 || strcmp(_Over_equipment_mintenance.type,"listSbWhQuery")==0)
  4734. {
  4735. _over_back_sts_info.product_name = __globalDeviceManage._globalDevInfo.product.name;
  4736. _over_back_sts_info.product_number = __globalDeviceManage._globalDevInfo.product.number;
  4737. _over_back_sts_info.product_id = __globalDeviceManage._globalDevInfo.product.id;
  4738. _over_back_sts_info.product_Code = __globalDeviceManage._globalDevInfo.product.pnumber;
  4739. // _over_back_sts_info.product_type = malloc(32);
  4740. //strcpy(_over_back_sts_info.product_type, "GC Series");
  4741. _over_back_sts_info.product_type=g_product_type_str[__globalDeviceManage._globalDevInfo.product.type];
  4742. _over_back_sts_info.productVersion = __globalDeviceManage._globalDevInfo.product.version;
  4743. _over_back_sts_info.product_sysTime = malloc(32);
  4744. struct tm *t;
  4745. struct timeval tv;
  4746. gettimeofday(&tv, NULL);
  4747. t = localtime(&tv.tv_sec);
  4748. sprintf(_over_back_sts_info.product_sysTime, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1,
  4749. t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
  4750. long diff =0;
  4751. struct timespec ts;
  4752. if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  4753. {
  4754. diff = ts.tv_sec;
  4755. }
  4756. _over_back_sts_info.product_runTime = malloc(32);
  4757. sprintf(_over_back_sts_info.product_runTime, "%02ld:%02ld:%02ld", diff / 3600, (diff % 3600) / 60, diff % 60);
  4758. _over_back_sts_info.productAddr = getLocalIpAddress("eth0");
  4759. _over_back_sts_info.product_mac = get_mac_address("eth0");
  4760. _over_back_sts_info.product_RS485Addr = __globalDeviceManage._globalDevInfo.cascade.addr;
  4761. //_over_back_sts_info.product_status = __globalDeviceManage._globalDevInfo.product.status;
  4762. _over_back_sts_info.product_status = malloc(16);
  4763. strcpy(_over_back_sts_info.product_status, "online");
  4764. _over_back_sts_info.product_type_id = __globalDeviceManage._globalDevInfo.product.pwr_type;
  4765. if (strcmp(_Over_equipment_mintenance.type, "listSbWhQuery") == 0)
  4766. {
  4767. ack_str = over_product_list_info_to_json(&__globalDeviceManage, &_over_back_sts_info, true);
  4768. }
  4769. else
  4770. ack_str = over_product_list_info_to_json(&__globalDeviceManage, &_over_back_sts_info, false);
  4771. }
  4772. else if(strcmp(_Over_equipment_mintenance.type,"listShfwQuery")==0)
  4773. {
  4774. GlobalDeviceInfo *dev=&__globalDeviceManage._globalDevInfo;
  4775. ack_str = vendor_info_to_json(dev);
  4776. }
  4777. else if(strcmp(_Over_equipment_mintenance.type,"sBRestart")==0) { //restart
  4778. char temp[100];
  4779. //sprintf(temp, "$系统$|$重启$");
  4780. sprintf(temp, "System Reboot!");
  4781. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  4782. sys_reboot();
  4783. }else if(strcmp(_Over_equipment_mintenance.type,"listKgSbQuery")==0) // part of device infomation
  4784. {
  4785. _OverBackBreakerInfo breaker_info;
  4786. printf("get listKgSbQuery infomation!!!\n");
  4787. breaker_info.product_id = __globalDeviceManage._globalDevInfo.product.id;
  4788. breaker_info.product_name = __globalDeviceManage._globalDevInfo.product.name;
  4789. breaker_info.product_number = __globalDeviceManage._globalDevInfo.product.number;
  4790. breaker_info.product_type = __globalDeviceManage._globalDevInfo.product.type;
  4791. ack_str = over_breaker_product_list_info_to_json(&__globalDeviceManage,&breaker_info,false);
  4792. }else if(strcmp(_Over_equipment_mintenance.type,"listKgTdQuery")==0)
  4793. {
  4794. _OverBreakerChnInfo _over_breaker_chn_info;
  4795. INIT_LIST_HEAD(&_over_breaker_chn_info.list);
  4796. //_OverBraekerChnInfo *temp = NULL;
  4797. GlobalPowerManger * _globalPowerTemp = NULL;
  4798. int cur_dev_addr = 0;
  4799. if(_Over_equipment_mintenance.product_id)
  4800. cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
  4801. if(cur_dev_addr) //slave
  4802. {
  4803. //
  4804. }else{ //zhuji
  4805. printf("get listKgTdQuery infomation!!!\n");
  4806. list_for_each_entry(_globalPowerTemp, &__globalDeviceManage._globalPowerManger.list, list)
  4807. {
  4808. _OverBreakerChnInfo *temp = (_OverBreakerChnInfo *) malloc(sizeof(_OverBreakerChnInfo));
  4809. temp->product_id = __globalDeviceManage._globalDevInfo.product.id;
  4810. strcpy(temp->product_name,__globalDeviceManage._globalDevInfo.product.name);
  4811. strcpy(temp->product_number,__globalDeviceManage._globalDevInfo.product.number);
  4812. strcpy(temp->product_ch_name,_globalPowerTemp->product_ch_name);
  4813. temp->product_ch_id = _globalPowerTemp->product_ch_id;
  4814. printf("%s %s %s %d\n",temp->product_name,temp->product_number,temp->product_ch_name,temp->product_ch_id);
  4815. list_add_tail(&temp->list,&_over_breaker_chn_info.list);
  4816. }
  4817. }
  4818. //参数转换json
  4819. ack_str = over_all_breaker_chn_info_to_json(&_over_breaker_chn_info,0);
  4820. //释放资源
  4821. _OverBreakerChnInfo* node;
  4822. _OverBreakerChnInfo* next;
  4823. list_for_each_entry_safe(node, next, &_over_breaker_chn_info.list, list)
  4824. {
  4825. list_del(&node->list);
  4826. free(node);
  4827. }
  4828. }else if(strcmp(_Over_equipment_mintenance.type,"listSwitchtatusQuery") == 0)
  4829. {
  4830. _OverBreakerSwitchInfo _over_breaker_switch_info;
  4831. INIT_LIST_HEAD(&_over_breaker_switch_info.list);
  4832. if(_Over_equipment_mintenance.product_id)
  4833. cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
  4834. if(cur_dev_addr) //slave
  4835. {
  4836. cmd_data_t cmd = {0};
  4837. int r=0;
  4838. cmd.cmd = CASCADE_CMD_BREAKER_GET_INFO;
  4839. cmd.obj = OBJ_OVERALL;
  4840. cascade_request(&cmd);
  4841. // cmd.cmd = CASCADE_CMD_BREAKER_QUERY;
  4842. // cmd.obj = OBJ_OVERALL;
  4843. // cascade_request(&cmd);
  4844. ack_str=ctrl_sts_ack_to_json();
  4845. // GlobalBreakerManager *temp = NULL;
  4846. // GlobalDeviceManager *dm = &__globalDeviceManage2;
  4847. // lock_s_hold(LOCK_ID_BREAKER);
  4848. // list_for_each_entry(temp, &dm->g_new_global_breaker.list, list)
  4849. // {
  4850. // char buff[120];
  4851. // printf("get listSwitchtatusQuery infomation!!!\n");
  4852. // _OverBreakerSwitchInfo *info = (_OverBreakerSwitchInfo *)
  4853. // malloc(sizeof(_OverBreakerSwitchInfo));
  4854. // memset(info,0,sizeof(_OverBreakerSwitchInfo));
  4855. // info->product_id = temp->product_id;
  4856. // sprintf(buff,"Breaker %d",temp->breaker_id);
  4857. // strcpy(info->product_ch_name,buff);
  4858. // sprintf(buff,"%s %d",__globalDeviceManage._globalDevInfo.product.name,temp->breaker_id);
  4859. // strcpy(info->productName,buff);
  4860. // strcpy(info->product_number,__globalDeviceManage._globalDevInfo.product.number);
  4861. // strcpy(info->productChids,temp->breaker_chns_name);
  4862. // info->switch_id = temp->breaker_id;
  4863. // info->status = temp->breaker_status;
  4864. // strcpy(info->cjfs,temp->breaker_gather_type);
  4865. // strcpy(info->switch_name,temp->breaker_switch_name);
  4866. // strcpy(info->comkh,temp->breaker_com);
  4867. // sprintf(buff,"%d",temp->breaker_gather_addr);
  4868. // strcpy(info->cjdz,buff);
  4869. // sprintf(buff,"%d",temp->breaker_chn);
  4870. // strcpy(info->jdh,buff);
  4871. // printf("%s %s %s %s %d %d %s %s %s %s\n",
  4872. // info->product_ch_name,info->productName,info->product_number,info->productChids,info->switch_id,
  4873. // info->status,info->cjfs,info->comkh,info->cjdz,info->jdh);
  4874. // list_add_tail(&info->list,&_over_breaker_switch_info.list);
  4875. // }
  4876. // lock_s_release(LOCK_ID_BREAKER);
  4877. // ack_str = over_all_breaker_switch_info_to_json(&_over_breaker_switch_info,0);
  4878. // _OverBreakerSwitchInfo* node;
  4879. // _OverBreakerSwitchInfo* next;
  4880. // list_for_each_entry_safe(node, next, &_over_breaker_switch_info.list, list)
  4881. // {
  4882. // list_del(&node->list);
  4883. // free(node);
  4884. //}
  4885. }else //current
  4886. {
  4887. GlobalBreakerManager *temp = NULL;
  4888. lock_s_hold(LOCK_ID_BREAKER);
  4889. list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
  4890. {
  4891. char buff[20];
  4892. //printf("get listSwitchtatusQuery infomation!!!\n");
  4893. _OverBreakerSwitchInfo *info = (_OverBreakerSwitchInfo *)
  4894. malloc(sizeof(_OverBreakerSwitchInfo));
  4895. memset(info,0,sizeof(_OverBreakerSwitchInfo));
  4896. info->product_id = temp->product_id;
  4897. sprintf(buff,"Breaker %d",temp->breaker_id);
  4898. strcpy(info->product_ch_name,buff);
  4899. strcpy(info->productName,__globalDeviceManage._globalDevInfo.product.name);
  4900. strcpy(info->product_number,__globalDeviceManage._globalDevInfo.product.number);
  4901. strcpy(info->productChids,temp->breaker_chns_name);
  4902. info->switch_id = temp->breaker_id;
  4903. info->status = temp->breaker_status;
  4904. strcpy(info->cjfs,temp->breaker_gather_type);
  4905. strcpy(info->switch_name,temp->breaker_switch_name);
  4906. strcpy(info->comkh,temp->breaker_com);
  4907. sprintf(buff,"%d",temp->breaker_gather_addr);
  4908. strcpy(info->cjdz,buff);
  4909. sprintf(buff,"%d",temp->breaker_chn);
  4910. strcpy(info->jdh,buff);
  4911. // printf("%s %s %s %s %d %d %s %s %s %s\n",
  4912. // info->product_ch_name,info->productName,info->product_number,info->productChids,info->switch_id,
  4913. // info->status,info->cjfs,info->comkh,info->cjdz,info->jdh);
  4914. list_add_tail(&info->list,&_over_breaker_switch_info.list);
  4915. }
  4916. lock_s_release(LOCK_ID_BREAKER);
  4917. printf("_glos_asdsd addr=0x%x\n",(uint32_t)(&__globalDeviceManage));
  4918. ack_str = over_all_breaker_switch_info_to_json(&_over_breaker_switch_info,0);
  4919. //释放资源
  4920. _OverBreakerSwitchInfo* node;
  4921. _OverBreakerSwitchInfo* next;
  4922. list_for_each_entry_safe(node, next, &_over_breaker_switch_info.list, list)
  4923. {
  4924. list_del(&node->list);
  4925. free(node);
  4926. }
  4927. }
  4928. }else if(strcmp(_Over_equipment_mintenance.type,"switchSave") == 0)
  4929. {
  4930. if(_Over_equipment_mintenance.product_id)
  4931. cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
  4932. if(cur_dev_addr) //slave
  4933. {
  4934. cmd_data_t cmd;
  4935. cmd.datas.data.cjdz = atoi(_Over_equipment_mintenance.cjdz);
  4936. cmd.datas.data.jdh = atoi(_Over_equipment_mintenance.jdh);
  4937. strcpy(cmd.datas.data.breaker_chns,_Over_equipment_mintenance.productChids);
  4938. strcpy(cmd.datas.data.cjfs,_Over_equipment_mintenance.cjfs);
  4939. strcpy(cmd.datas.data.com,_Over_equipment_mintenance.comkh);
  4940. cmd.cmd = CASCADE_CMD_BREAKER_SAVE_ADD;
  4941. cmd.obj = OBJ_OVERALL;
  4942. cascade_request(&cmd);
  4943. ack_str=ctrl_sts_ack_to_json();
  4944. }else
  4945. {
  4946. char buff[124] = {0};
  4947. char id[MAX_BREAKER_ID] = {0};
  4948. //bianli shujujiegou
  4949. GlobalBreakerManager *temp = NULL;
  4950. int i = 1;
  4951. lock_s_hold(LOCK_ID_BREAKER);
  4952. list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
  4953. {
  4954. id[temp->breaker_id] = 1;
  4955. }
  4956. lock_s_release(LOCK_ID_BREAKER);
  4957. for(;i < MAX_BREAKER_ID;i++)
  4958. {
  4959. if(id[i] == 0)
  4960. break;
  4961. }
  4962. if(i != MAX_BREAKER_ID)
  4963. {
  4964. GlobalBreakerManager *breakerManager = (GlobalBreakerManager*) malloc(sizeof(GlobalBreakerManager));
  4965. memset(breakerManager,0,sizeof(GlobalBreakerManager));
  4966. breakerManager->product_id = __globalDeviceManage._globalDevInfo.product.id;
  4967. breakerManager->breaker_gather_addr = atoi(_Over_equipment_mintenance.cjdz);
  4968. strcpy(breakerManager->breaker_com,_Over_equipment_mintenance.comkh);
  4969. strcpy(breakerManager->breaker_gather_type,_Over_equipment_mintenance.cjfs);
  4970. strcpy(breakerManager->breaker_chns_name,_Over_equipment_mintenance.productChids);
  4971. sprintf(buff,"Breadker_%d",breakerManager->breaker_id);
  4972. strcpy(breakerManager->breaker_switch_name,buff);
  4973. breakerManager->breaker_id = i;
  4974. uint32_t jdh = atoi(_Over_equipment_mintenance.jdh);
  4975. if(breakerManager->breaker_gather_addr >= BREADER_485_ADDR &&
  4976. breakerManager->breaker_gather_addr <= (BREADER_485_ADDR + BREAKER_BOARD_MAX))
  4977. {
  4978. if(jdh <= MAX_BREAKER_CHN && jdh >0)
  4979. {
  4980. char jdh_flag = 0;
  4981. char chn[MAX_BREAKER_CHN] = {0};
  4982. lock_s_hold(LOCK_ID_BREAKER);
  4983. list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
  4984. {
  4985. if(temp->breaker_gather_addr == breakerManager->breaker_gather_addr)
  4986. {
  4987. chn[temp->breaker_chn-1] = 1;
  4988. }
  4989. }
  4990. lock_s_release(LOCK_ID_BREAKER);
  4991. if(chn[jdh-1] != 1)
  4992. {
  4993. breakerManager->breaker_chn = jdh;
  4994. }else
  4995. {
  4996. jdh_flag = 1;
  4997. }
  4998. if(jdh_flag == 1)
  4999. {
  5000. free(breakerManager);
  5001. breakerManager = NULL;
  5002. ack_str = ctrl_breaker_err_ack_to_json("channel or id is existing!!!!");
  5003. }else{
  5004. dev_insert_breaker_genera_manage(__globalDeviceManage.db,breakerManager);
  5005. lock_s_hold(LOCK_ID_BREAKER);
  5006. list_add_tail(&breakerManager->list,&__globalDeviceManage.g_new_global_breaker.list);
  5007. lock_s_release(LOCK_ID_BREAKER);
  5008. ack_str=ctrl_sts_ack_to_json();
  5009. }
  5010. }else {
  5011. free(breakerManager);
  5012. breakerManager = NULL;
  5013. ack_str = ctrl_breaker_err_ack_to_json("jdh error!!!!");
  5014. }
  5015. }else if(breakerManager->breaker_gather_addr >= 1 && breakerManager->breaker_gather_addr <=30) //controller
  5016. {
  5017. if(jdh <= 2 && jdh > 0)
  5018. {
  5019. char jdh_flag = 0;
  5020. char chn[2] = {0};
  5021. lock_s_hold(LOCK_ID_BREAKER);
  5022. list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
  5023. {
  5024. if(temp->breaker_gather_addr == breakerManager->breaker_gather_addr)
  5025. {
  5026. chn[temp->breaker_chn-1] = 1;
  5027. }
  5028. }
  5029. lock_s_release(LOCK_ID_BREAKER);
  5030. if(chn[jdh-1] != 1)
  5031. {
  5032. breakerManager->breaker_chn = jdh;
  5033. }else
  5034. {
  5035. jdh_flag = 1;
  5036. }
  5037. if(jdh_flag == 1)
  5038. {
  5039. free(breakerManager);
  5040. breakerManager = NULL;
  5041. ack_str = ctrl_breaker_err_ack_to_json("channel or id is existing!!!!");
  5042. }else{
  5043. dev_insert_breaker_genera_manage(__globalDeviceManage.db,breakerManager);
  5044. lock_s_hold(LOCK_ID_BREAKER);
  5045. list_add_tail(&breakerManager->list,&__globalDeviceManage.g_new_global_breaker.list);
  5046. lock_s_release(LOCK_ID_BREAKER);
  5047. ack_str=ctrl_sts_ack_to_json();
  5048. }
  5049. }else
  5050. {
  5051. free(breakerManager);
  5052. breakerManager = NULL;
  5053. ack_str = ctrl_breaker_err_ack_to_json("jdh error!!!!");
  5054. }
  5055. }else if(breakerManager->breaker_gather_addr == 0) //current support one breaker
  5056. {
  5057. if(jdh == 1)
  5058. {
  5059. char jdh_flag = 0;
  5060. lock_s_hold(LOCK_ID_BREAKER);
  5061. list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
  5062. {
  5063. if(temp->breaker_gather_addr == breakerManager->breaker_gather_addr)
  5064. {
  5065. jdh_flag = 1;
  5066. }
  5067. }
  5068. lock_s_release(LOCK_ID_BREAKER);
  5069. if(jdh_flag == 1)
  5070. {
  5071. free(breakerManager);
  5072. breakerManager = NULL;
  5073. ack_str = ctrl_breaker_err_ack_to_json("channel or id is existing!!!!");
  5074. }else{
  5075. breakerManager->breaker_chn = jdh;
  5076. dev_insert_breaker_genera_manage(__globalDeviceManage.db,breakerManager);
  5077. lock_s_hold(LOCK_ID_BREAKER);
  5078. list_add_tail(&breakerManager->list,&__globalDeviceManage.g_new_global_breaker.list);
  5079. lock_s_release(LOCK_ID_BREAKER);
  5080. ack_str=ctrl_sts_ack_to_json();
  5081. }
  5082. }else
  5083. {
  5084. free(breakerManager);
  5085. breakerManager = NULL;
  5086. ack_str = ctrl_breaker_err_ack_to_json("jdh error!!!!");
  5087. }
  5088. }else
  5089. {
  5090. free(breakerManager);
  5091. breakerManager = NULL;
  5092. ack_str = ctrl_breaker_err_ack_to_json("not surpport base addr !!");
  5093. }
  5094. }else
  5095. {
  5096. ack_str = ctrl_breaker_err_ack_to_json("ID More 48 !!!!");
  5097. }
  5098. }
  5099. }else if(strcmp(_Over_equipment_mintenance.type,"switchUpdate") == 0)
  5100. {
  5101. if(_Over_equipment_mintenance.product_id)
  5102. cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
  5103. if(cur_dev_addr) ///slave
  5104. {
  5105. cmd_data_t cmd;
  5106. cmd.datas.data1.switch_id = _Over_equipment_mintenance.switchId;
  5107. strcpy(cmd.datas.data1.breaker_chns,_Over_equipment_mintenance.productChids);
  5108. strcpy(cmd.datas.data1.switch_name,_Over_equipment_mintenance.switchName);
  5109. cmd.cmd = CASCADE_CMD_BREAKER_SAVE_UPDATE;
  5110. cmd.obj = OBJ_OVERALL;
  5111. cascade_request(&cmd);
  5112. ack_str=ctrl_sts_ack_to_json();
  5113. }else
  5114. {
  5115. GlobalBreakerManager *temp = NULL;
  5116. int switch_id = _Over_equipment_mintenance.switchId;
  5117. lock_s_hold(LOCK_ID_BREAKER);
  5118. list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
  5119. {
  5120. if(temp->breaker_id == switch_id)
  5121. {
  5122. strcpy(temp->breaker_switch_name,_Over_equipment_mintenance.switchName);
  5123. strcpy(temp->breaker_chns_name,_Over_equipment_mintenance.productChids);
  5124. dev_update_breaker_genera_manage(__globalDeviceManage.db,
  5125. __globalDeviceManage._globalDevInfo.product.id,switch_id,temp);
  5126. break;
  5127. }
  5128. }
  5129. lock_s_release(LOCK_ID_BREAKER);
  5130. ack_str=ctrl_sts_ack_to_json();
  5131. }
  5132. }else if(strcmp(_Over_equipment_mintenance.type,"switchDelete") == 0)
  5133. {
  5134. if(_Over_equipment_mintenance.product_id)
  5135. cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
  5136. if(cur_dev_addr) ///slave
  5137. {
  5138. cmd_data_t cmd;
  5139. cmd.datas.data2.switch_id = _Over_equipment_mintenance.switchId;
  5140. cmd.cmd = CASCADE_CMD_BREAKER_SAVE_DELETE;
  5141. cmd.obj = OBJ_OVERALL;
  5142. cascade_request(&cmd);
  5143. GlobalBreakerManager* node = NULL;
  5144. GlobalBreakerManager* next = NULL;
  5145. lock_s_hold(LOCK_ID_BREAKER);
  5146. list_for_each_entry_safe(node,next,&__globalDeviceManage2.g_new_global_breaker.list, list)
  5147. {
  5148. if(node->breaker_id == _Over_equipment_mintenance.switchId)
  5149. {
  5150. // dev_delete_breaker_manage(__globalDeviceManage2.db,__globalDeviceManage._globalDevInfo.product.id,
  5151. // _Over_equipment_mintenance.switchId);
  5152. list_del(&node->list);
  5153. free(node);
  5154. break;
  5155. }
  5156. }
  5157. lock_s_release(LOCK_ID_BREAKER);
  5158. ack_str=ctrl_sts_ack_to_json();
  5159. }else
  5160. {
  5161. int switch_id = _Over_equipment_mintenance.switchId;
  5162. //GlobalBreakerManager *temp = NULL;
  5163. GlobalBreakerManager* node;
  5164. GlobalBreakerManager* next;
  5165. lock_s_hold(LOCK_ID_BREAKER);
  5166. list_for_each_entry_safe(node,next,&__globalDeviceManage.g_new_global_breaker.list, list)
  5167. {
  5168. if(node->breaker_id == switch_id)
  5169. {
  5170. dev_delete_breaker_manage(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id,switch_id);
  5171. list_del(&node->list);
  5172. free(node);
  5173. node = NULL;
  5174. break;
  5175. }
  5176. }
  5177. lock_s_release(LOCK_ID_BREAKER);
  5178. ack_str=ctrl_sts_ack_to_json();
  5179. }
  5180. }
  5181. else if(strcmp(_Over_equipment_mintenance.type,"restoreFactorySettings")==0) { //restore to factory
  5182. char temp[100];
  5183. GlobalDeviceManager *dm=&__globalDeviceManage;
  5184. PowerTotal_t *ptotal=&dm->_globalDevInfo.ptotal;
  5185. //sprintf(temp, "$系统$|$恢复出厂设置$");
  5186. sprintf(temp, "System Reset!");
  5187. dev_insert_alarm_ctrl(dm->db, dm->_globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
  5188. for (size_t i = 0; i <= ptotal->brdMax; i++)
  5189. {
  5190. if (dm->pCtrlBoard[i].product_saddr <= 0)
  5191. {
  5192. continue;
  5193. }
  5194. g_switch_get_all_reset(&dm->_globalRelaySampManger,dm->pCtrlBoard[i].product_type,__globalDeviceManage.pCtrlBoard[i].product_saddr);
  5195. }
  5196. sys_set_factory();
  5197. }
  5198. else if (strcmp(_Over_equipment_mintenance.type,"resetChannel") == 0)
  5199. {
  5200. http_reply(conn, 200, ack_str);
  5201. //__globalDeviceManage._ReloadChm_flag=true;
  5202. lock_s_hold(LOCK_ID_POWER_UPDATE);
  5203. ResetChmData(1);
  5204. lock_s_release(LOCK_ID_POWER_UPDATE);
  5205. char log_buff[256];
  5206. sprintf(log_buff, "Rescan channel!");
  5207. dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
  5208. //__globalDeviceManage._ReloadChm_flag=false;
  5209. upg_board_all_update();
  5210. }
  5211. else if (strcmp(_Over_equipment_mintenance.type,"cleanUp") == 0)
  5212. {
  5213. if (_Over_equipment_mintenance.cleanSelectItem!=NULL)
  5214. {
  5215. char *pItemlist;
  5216. char *pItemListSave;
  5217. pItemlist = strtok_r(_Over_equipment_mintenance.cleanSelectItem, ",", &pItemListSave);
  5218. while (pItemlist != NULL)
  5219. {
  5220. int ItemID=atoi(pItemlist) ;
  5221. if (1==ItemID)
  5222. {
  5223. log_i("CleanUp all power history Information!\n");
  5224. dev_delete_ac_power_info_all(__globalDeviceManage.db);
  5225. dev_delete_power_info_all(__globalDeviceManage.db);
  5226. }
  5227. else if (2==ItemID)
  5228. {
  5229. log_i("CleanUp all sensor history Information!\n");
  5230. dev_delete_sensor_info_all(__globalDeviceManage.db);
  5231. }
  5232. else if (3==ItemID)
  5233. {
  5234. log_i("CleanUp all alarm log Information!\n");
  5235. dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_POWER);
  5236. dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_SENSOR);
  5237. dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_NETWORK);
  5238. }
  5239. else if (4==ItemID)
  5240. {
  5241. log_i("CleanUp all operation log Information!\n");
  5242. dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_OPRATION);
  5243. }
  5244. else if (5==ItemID)
  5245. {
  5246. log_i("CleanUp all system log Information!\n");
  5247. dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_RUNNING);
  5248. }
  5249. pItemlist = strtok_r(NULL, ",", &pItemListSave);
  5250. }
  5251. }
  5252. dev_sql_VACCUM(__globalDeviceManage.db);
  5253. log_i("Database SQLite VACCUM!\n");
  5254. ack_str=ctrl_sts_ack_to_json();
  5255. }
  5256. else if (strcmp(_Over_equipment_mintenance.type,"listRtHisQuery") == 0) {
  5257. ack_str = upgrade_to_json();
  5258. }
  5259. cJSON_Delete(cjson);
  5260. http_reply(conn, 200, ack_str);
  5261. cJSON_free((void*)ack_str);
  5262. }
  5263. static void equipmentMintenanceUpload(void *conn)
  5264. {
  5265. int r = -1 ;
  5266. char* ack_str=NULL ;
  5267. mg_stream_t *s=(mg_stream_t*)conn;
  5268. struct mg_http_message *hm=s->ev_data;
  5269. char path[200],tmp[60];
  5270. if(strstr(hm->uri.buf, "uploadType=gjsj")) { //firmware upload
  5271. file_io(conn, FILE_HOST_FW, FILE_IMPORT, NULL);
  5272. }
  5273. else if(strstr(hm->uri.buf, "uploadType=shfw")) { //logo upload
  5274. file_io(conn, FILE_LOGO, FILE_IMPORT, NULL);
  5275. }
  5276. else if(strstr(hm->uri.buf, "uploadType=0")) {
  5277. file_io(conn, FILE_ALARM_CFG, FILE_IMPORT, NULL);
  5278. }
  5279. else if(strstr(hm->uri.buf, "uploadType=1")) {
  5280. file_io(conn, FILE_SERV_CFG, FILE_IMPORT, NULL);
  5281. }
  5282. else if(strstr(hm->uri.buf, "uploadType=2")) {
  5283. file_io(conn, FILE_SENSOR_CFG, FILE_IMPORT, NULL);
  5284. }
  5285. else if(strstr(hm->uri.buf, "uploadType=3")) {
  5286. file_io(conn, FILE_SYS_CFG, FILE_IMPORT, NULL);
  5287. }
  5288. ack_str = ctrl_sts_ack_to_json();
  5289. http_reply(conn, 200, ack_str);
  5290. cJSON_free(ack_str);
  5291. }
  5292. static void equipmentMintenanceExport(void *conn)
  5293. {
  5294. int r = -1 ;
  5295. char* ack_str=NULL ;
  5296. mg_stream_t *s=(mg_stream_t*)conn;
  5297. struct mg_http_message *hm=s->ev_data;
  5298. if(strstr(hm->uri.buf, "export=0")) {
  5299. file_io(conn, FILE_ALARM_CFG, FILE_EXPORT, NULL);
  5300. }
  5301. else if(strstr(hm->uri.buf, "export=1")) {
  5302. file_io(conn, FILE_SERV_CFG, FILE_EXPORT, NULL);
  5303. }
  5304. else if(strstr(hm->uri.buf, "export=2")) {
  5305. file_io(conn, FILE_SENSOR_CFG, FILE_EXPORT, NULL);
  5306. }
  5307. else if(strstr(hm->uri.buf, "export=3")) {
  5308. file_io(conn, FILE_SYS_CFG, FILE_EXPORT, NULL);
  5309. }
  5310. }
  5311. static void controlBoardUpgrade(void *conn)
  5312. {
  5313. int r = -1 ;
  5314. char* ack_str=NULL ;
  5315. mg_stream_t *s=(mg_stream_t*)conn;
  5316. struct mg_http_message *hm=s->ev_data;
  5317. if(strstr(hm->uri.buf, "listCbQuery")) {
  5318. handle_t l=upg_board_all_get();
  5319. ack_str = board_to_json(l);
  5320. http_reply(conn, 200, ack_str);
  5321. cJSON_free(ack_str);
  5322. }
  5323. }
  5324. static void controlBoardUpgradeUpload(void *conn)
  5325. {
  5326. int r = -1 ;
  5327. char* ack_str=NULL ;
  5328. mg_stream_t *s=(mg_stream_t*)conn;
  5329. struct mg_http_message *hm=s->ev_data;
  5330. if(strstr(hm->uri.buf, "type=importData")) {
  5331. char path[100];
  5332. sprintf(path, "%s/%s", DOWN_FOLDER, "fw.upg");
  5333. r = upload_file(conn, path);
  5334. if(r==0) {
  5335. upg_board_list_set(hm->uri.buf);
  5336. printf("_____ board upgrade now ...\n");
  5337. upg_board_start(path);
  5338. }
  5339. }
  5340. }
  5341. static int webSetCallback(void)
  5342. {
  5343. httpAddCallback("/login", loginFunc);//登录
  5344. httpAddCallback("/logout", loginOutFunc);//登出
  5345. httpAddCallback("/getInfo", getUserInfo);//获取用户信息
  5346. httpAddCallback("/pdu/changeLanguages", changeLanguages);//切换语言
  5347. httpAddCallback("/pdu/systemConfiguration", systemConfiguration);//登录
  5348. httpAddCallback("/getRouters", getUserRouters);//获取菜单接口
  5349. httpAddCallback("/pdu/overallStatusMonitoring", overallStatusMonitoring);//设备电源监控信息
  5350. httpAddCallback("/pdu/channelStatusMonitoring", channelStatusMonitoring);//设备通道电源监控信息
  5351. httpAddCallback("/pdu/sensorStatusMonitoring", sensorStatusMonitoring);//传感器总体请求信息
  5352. httpAddCallback("/pdu/alarmProtect", alarmProtect);//告警历史信息
  5353. httpAddCallback("/pdu/logInformation", logInformation);//日志
  5354. httpAddCallback("/pdu/communicationManagement", communicationManagement);//通信设置
  5355. httpAddCallback("/pdu/serviceManagement", serviceManagement);//服务管理
  5356. httpAddCallback("/pdu/serviceManagementUpload", serviceManagementUpload);//服务管理
  5357. httpAddCallback("/pdu/system", systemManagent);//用户管理
  5358. httpAddCallback("/pdu/equipmentMintenance", equipmentMintenance);//设备维护
  5359. httpAddCallback("/pdu/equipmentMintenanceUpload", equipmentMintenanceUpload); //固件升级
  5360. httpAddCallback("/pdu/equipmentMintenanceExport", equipmentMintenanceExport);
  5361. httpAddCallback("/pdu/controlBoardUpgradeUpload", controlBoardUpgradeUpload);
  5362. httpAddCallback("/pdu/controlBoardUpgrade", controlBoardUpgrade);
  5363. return 0;
  5364. }
  5365. int appweb_init(void)
  5366. {
  5367. thread_start(THREAD_ID_WEB, web_thread, &__globalDeviceManage);
  5368. return 0;
  5369. }