| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861 |
- #include "cJSON.h"
- #include "common.h"
- #include "json_handle.h"
- #include "sqlite_handle.h"
- #include "cJSON.h"
- #include "switch_ctrl.h"
- #include "sys.h"
- #include "thread.h"
- #include "cfg.h"
- #include "cascade.h"
- #include "upg.h"
- #include "lock.h"
- #include "file.h"
- #include "paras.h"
- #include "mongoose.h"
- #include "language_common.h"
- #include "websocket_handle.h"
- #include "breaker_detection.h"
- #define TOTAL_COUNT 60
- #define FN_MAX 30
- enum {
- FILE_IMPORT=0,
- FILE_EXPORT,
- };
- enum {
- FILE_SYS_CFG=0,
- FILE_SERV_CFG,
- FILE_ALARM_CFG,
- FILE_SENSOR_CFG,
- FILE_POWER_ALL,
- FILE_POWER_CHN,
-
- FILE_LOG_RUN,
- FILE_LOG_OPERA,
- FILE_LOG_ALARM,
- FILE_LOGO,
- FILE_HOST_FW,
- };
- typedef void (*http_fn_t)(void *arg);
- typedef struct mg_tls_opts mg_tls_t;
- typedef struct {
- struct mg_connection *c;
- int ev;
- void *ev_data;
- }mg_stream_t;
- typedef struct {
- http_fn_t f;
- char *uri;
- }mg_fn_t;
- typedef struct {
- int cnt;
- mg_fn_t fn[FN_MAX];
- char rootDir[MG_PATH_MAX];
- mg_tls_t tls;
- pthread_t tid;
- }mg_handle_t;
- typedef struct {
- _OverAllPwrAckInfo all;
- _OverAllPwrAckInfo chn;
- }status_data_t;
- typedef struct {
- mg_handle_t mg;
- pthread_t tid;
- status_data_t stt;
- }web_handle_t;
- static char* g_product_board_cpu[] = {
- "xxx",
- "T113"
- };
- static char* g_product_type_str[] = {
- "SmartPDU(DC)",
- "SmartPDU(AC)",
- "SmartPDU(Tree_AC_Tree)",
- "SmartPDU(Tree_AC_One)",
- };
- static char* g_product_pwr_type_str[] = {
- "xxx",
- "直流",
- "交流单相",
- "交流三相三出",
- "交流三相单出",
- };
- static web_handle_t webHandle;
- static int webSetCallback(void);
- ///////////////////////////////////////////////////
- static int upload_file(void *conn, char *path)
- {
- int r=-1,flag=0;
- char name[200];
- struct mg_http_part part;
- mg_stream_t *s=(mg_stream_t*)conn;
- size_t pos = 0, total_bytes = 0;
- struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
-
- memset(name, 0, sizeof(name));
- while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
- memcpy(name, part.filename.buf, part.filename.len);
- printf("filename: %s length: %lu bytes\n", name, (unsigned long) part.body.len);
- if(flag==0) {
- file_remove(path); flag = 1;
- }
- r = file_append(path, part.body.buf, part.body.len);
- if (r==0) {
- total_bytes += part.body.len;
- }
- }
- printf("___ upload file to %s %d\n", path, total_bytes);
- return r;
- }
- static int download_file(void *conn, char *fpath)
- {
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
- struct mg_http_serve_opts opts={.root_dir=NULL};
- mg_http_serve_file2(s->c, hm, fpath, &opts);
- return 0;
- }
- static int http_reply(void *conn, int status, char *ack)
- {
- int r=0;
- mg_stream_t *s=(mg_stream_t*)conn;
- r = mg_http_reply2(s->c, status, "", ack);
- return r;
- }
- static void httpAddCallback(char *uri, http_fn_t f)
- {
- mg_handle_t *h=&webHandle.mg;
- if(h->cnt<FN_MAX-1) {
- h->fn[h->cnt].uri = uri;
- h->fn[h->cnt].f = f;
- h->cnt++;
- }
- }
- static time_t gettime(char *timestr)
- {
- int r;
- struct tm tm={0};
- //2024-10-29 05:19:09
- 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);
- if(r<6) {
- return -1;
- }
- tm.tm_year -= 1900;
- tm.tm_mon -= 1;
- return mktime(&tm);
- }
- static int gettime2(char *buf, char *tok, char *timestr)
- {
- char *p=strstr(buf, tok);
- if(!p) {
- return -1;
- }
- p += strlen(tok);
- memcpy(timestr, p, 10); timestr[10] = ' ';
- memcpy(timestr+11, p+13, 8); timestr[19] = 0;
- return 0;
- }
- static int get_range(time_t *from, time_t *to, char *start, char *end, int max_days)
- {
- time_t cur=gettime(start);
- time_t pre=gettime(end);
- time_t sss=mktime0(),secs=3600*24*max_days;
- if(cur<=sss) {
- cur = sss;
- }
- if(pre<=sss) {
- pre = sss;
- }
- if(abs(cur-pre)>=secs) {
- if(cur>pre) {
- *from = cur-secs;
- *to = cur;
- }
- else {
- *from = pre-secs;
- *to = pre;
- }
- }
- else {
- if(cur>pre) {
- *from = pre;
- *to = cur;
- }
- else {
- *from = cur;
- *to = pre;
- }
- }
-
- return 0;
- }
- static int power_export(char *path, char *start, char *end)
- {
- int r;
- time_t from,to;
- char tmp1[100],tmp2[100];
- GlobalDeviceManager *dm=&__globalDeviceManage;
- get_range(&from, &to, start, end, 7);
- get_time_str2(tmp1,sizeof(tmp1),from);
- get_time_str2(tmp2,sizeof(tmp2),to);
- sprintf(path, "/tmp/power_%s_%s.csv", tmp1, tmp2);
- return dev_power_xport(from, to, path);
- }
- static int file_io(void *conn, int ftype, int io, char *buf)
- {
- int r,flag=0;
- char tmp[200],path[200];
- web_handle_t *wh=&webHandle;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
- switch(ftype) {
- case FILE_SYS_CFG:
- {
- sprintf(tmp, "/tmp/config.ini");
- sys_get_path(path, INI_FILE_NAME);
- if(io==FILE_IMPORT) {
- r = upload_file(conn, tmp);
- if(r==0) {
- r = paras_check(tmp);
- if(r==0) {
- paras_copy(path, tmp);
- file_remove(tmp);
- //clear some database table
- exit(0); //restart smartPDU
- }
- file_remove(tmp);
- }
- }
- else {
- file_copy(tmp, path);
- download_file(conn, tmp);
- file_remove(tmp);
- }
- }
- break;
- case FILE_SERV_CFG:
- {
- sprintf(path, "/tmp/service.ini");
- if(io==FILE_IMPORT) {
- r = upload_file(conn, path);
- if(r==0) {
- //thread_stop(THREAD_ID_POWER);
- r = paras_serv_xport(path, PARAS_IMPORT);
- file_remove(path);
- if(r==0) exit(0);
- //thread_start2(THREAD_ID_POWER);
- }
- }
- else {
- r = paras_serv_xport(path, PARAS_EXPORT);
- if(r==0) {
- r = download_file(conn, path);
- }
- file_remove(path);
- }
- }
- break;
- case FILE_ALARM_CFG:
- {
- sprintf(path, "/tmp/alarm.ini");
- if(io==FILE_IMPORT) {
- r = upload_file(conn, path);
- if(r==0) {
- r = paras_alarm_xport(path, PARAS_IMPORT);
- file_remove(path);
- if(r==0) exit(0);
- }
- }
- else {
- r = paras_alarm_xport(path, PARAS_EXPORT);
- if(r==0) {
- r = download_file(conn, path);
- file_remove(path);
- }
- }
- }
- break;
- case FILE_SENSOR_CFG:
- {
- sprintf(path, "/tmp/sensor.ini");
- if(io==FILE_IMPORT) {
- r = upload_file(conn, path);
- if(r==0) {
- r = paras_sensor_xport(path, PARAS_IMPORT);
- file_remove(path);
- if(r==0) exit(0);
- }
- }
- else {
- r = paras_sensor_xport(path, PARAS_EXPORT);
- if(r==0) {
- r = download_file(conn, path);
- file_remove(path);
- }
- }
- }
- break;
- case FILE_POWER_ALL:
- case FILE_POWER_CHN:
- {
- if(io==FILE_EXPORT) {
- char start[100],end[100];
- gettime2(buf, "startTime=", start);
- gettime2(buf, "endTime=", end);
- r = power_export(path, start, end);
- if(r==0) {
- download_file(conn, path);
- file_remove(path);
- }
- }
- }
- break;
-
- case FILE_LOG_RUN:
- {
- if(io==FILE_EXPORT) {
- //download_file(path);
- }
- }
- break;
- case FILE_LOG_OPERA:
- {
- if(io==FILE_EXPORT) {
- //download_file(path);
- }
- }
- break;
- case FILE_LOG_ALARM:
- {
- if(io==FILE_EXPORT) {
- //download_file(path);
- }
- }
- break;
- case FILE_LOGO:
- {
- if(io==FILE_IMPORT) {
- char tmp[100];
- sprintf(tmp, "%s/%s", IMAGE_FOLDER, LOGO_FILE);
- sys_get_path(path, tmp);
- upload_file(conn, path);
- }
- }
- break;
- case FILE_HOST_FW:
- {
- if(io==FILE_IMPORT) {
- sprintf(path, "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
- r = upload_file(conn, path);
- if(r==0) {
- printf("_____ upg_start\n");
- upg_host_send(UPGRADE_EVT_UPLOAD_FINISH);
- }
- }
- }
- break;
- default:
- return -1;
- }
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////
- static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
- {
- int i,j,flag=0;
- mg_handle_t *h=&webHandle.mg;
- mg_stream_t s={c,ev,ev_data};
- if (ev == MG_EV_HTTP_MSG) {
- struct mg_http_message *hm = ev_data;
-
- for(i=0; i<h->cnt; i++) {
- if (h->fn[i].f && h->fn[i].uri && mg_match(hm->uri, mg_str(h->fn[i].uri), NULL)) {
- h->fn[i].f(&s); flag = 1; break;
- }
- }
- if (flag==0) {
- struct mg_http_serve_opts opts = {.root_dir = h->rootDir};
- mg_http_serve_dir(c, ev_data, &opts);
- }
- }
- else if(ev == MG_EV_ACCEPT) {
- if (c->fn_data) {
- mg_tls_init(c, &h->tls);
- }
- }
- }
- static void *web_thread(void *arg)
- {
- struct mg_mgr mgr;
- struct mg_connection *c;
- mg_handle_t *mh=&webHandle.mg;
- thread_handle_t *h=(thread_handle_t*)arg;
- const char *http_addr="http://[::]:80";
- const char *https_addr="https://[::]:443";
- //mg_log_set(MG_LL_DEBUG);
- mg_mgr_init(&mgr);
- //sys_get_path(mh->rootDir, WEB_ROOT);
- strcpy(mh->rootDir, "/root/run/web");
-
- if (mg_http_listen(&mgr, http_addr, http_mg_cb, NULL) == NULL) {
- MG_ERROR(("cannot listen on %s\n", http_addr));
- return NULL;
- }
- if (mg_http_listen(&mgr, https_addr, http_mg_cb, (void*)1) == NULL) {
- MG_ERROR(("cannot listen on %s\n", https_addr));
- return NULL;
- }
- webSetCallback();
- printf("_____mgHandle.cnt: %d, rootdir: %s\n", mh->cnt, mh->rootDir);
- mh->tls.cert = mg_str(file_load2("/root/run/app/cert/server.crt",0));
- mh->tls.key = mg_str(file_load2("/root/run/app/cert/server.key",0));
- //mh->tls.name = mg_url_host(https_addr);
- while(h->quit==0) {
- mg_mgr_poll(&mgr, 200);
- }
- mg_mgr_free(&mgr);
-
- pthread_exit(NULL);
- }
- /// @brief 系统信息
- /// @param stream
- static void systemConfiguration(void* conn)
- {
- char *ack_str = NULL;
- char *g_token = NULL;
- mg_stream_t *s=(mg_stream_t *)conn;
- struct mg_http_message *hm=s->ev_data;
- char* rx_Head_buf=(char *)hm->head.buf;
- int tls=s->c->is_tls;
- g_token = json_to_Token_info(rx_Head_buf);
- // 检查token是否有效
- if (g_token != NULL && !CheckLoginStatus(g_token))
- {
- ack_str = log_AthFailedinfo_to_json();
- }
- else
- {
- ack_str = json_to_system_config_info(tls);
- }
- http_reply(conn, 200, ack_str);
- if (g_token != NULL)
- {
- free(g_token);
- }
- cJSON_free((void *)ack_str);
- }
- /// @brief 多语言切换
- /// @param stream
- static void changeLanguages(void* conn)
- {
- char* ack_str = NULL;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char* rx_body_buf=(char*)hm->body.buf;
- json_to_switch_language(rx_body_buf);
- paras_save();
- ack_str = ctrl_sts_ack_to_json();
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 登录信息
- /// @param stream
- static void loginFunc(void* conn)
- {
- char* ack_str = NULL ;
- char g_token[64] = {0};
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf=hm->body.buf;
- //用户信息创建-未析构
- _User_Log *_newUserLogin = (_User_Log *)malloc(sizeof(_User_Log));
- memset(_newUserLogin, 0, sizeof(_User_Log));
- //转换json参数
- if(json_to_User_login_info(rx_body_buf,_newUserLogin)!=0)
- {
- http_reply(conn, 400, NULL);
- return;
- }
- bool LoginFlag = false;
- // 查询用户信息
- UserInfo User_list;
- INIT_LIST_HEAD(&User_list.list);
- if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list))
- {
- UserInfo *_UserInfoTemp = NULL;
- list_for_each_entry(_UserInfoTemp, &User_list.list, list)
- {
- if (_UserInfoTemp == NULL)
- break;
- if (strcmp(_UserInfoTemp->userName,_newUserLogin->user_name)==0 && strcmp(_UserInfoTemp->userPW,_newUserLogin->user_password)==0)
- {
- LoginFlag=true;
- _GlobalUserInfoNew = *_UserInfoTemp;
- break;
- }
- }
- }
- //if (strcmp(_newUserLogin.user_name, "gowone") == 0 && strcmp(_newUserLogin.user_password,"gowone520") == 0)
- //{
- // LoginFlag = true;
- //}
- //获取随机token
- get_rand_str(g_token,32);
- //记录token
- strcpy(_newUserLogin->user_token,g_token);
- int ack_code=0;
- if (!LoginFlag)
- {
- ack_str = log_LoginFailedinfo_to_json();
- ack_code = 201;
- }
- else
- {
- strcpy(_GlobalUserToken,g_token);
- list_add_tail(&_newUserLogin->list, &_GlobalUserLoginInfo.list);
- ack_str = log_info_to_json(g_token);
- ack_code = 200;
- }
- http_reply(conn, ack_code, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 注销信息
- /// @param stream
- static void loginOutFunc(void* conn)
- {
- char* ack_str = NULL ;
- char* g_token;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf = (char*)hm->body.buf;
- //转换json参数
- if(json_to_User_loginOut_info(rx_body_buf,&g_token)!=0)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
-
- ack_str = log_info_to_json(g_token);
- http_reply(conn, 200, ack_str);
- free(g_token);
- cJSON_free((void*)ack_str);
- }
- /// @brief 获取用户信息
- /// @param stream
- static void getUserInfo(void* conn)
- {
- char* ack_str = NULL ;
- ack_str = user_info_to_json();
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 获取菜单数据
- /// @param stream
- static void getUserRouters(void* conn)
- {
- char* ack_str = NULL ;
- char* g_token=NULL;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_Head_buf = (char*)hm->head.buf;
- // 查询用户信息
- UserInfo User_list;
- INIT_LIST_HEAD(&User_list.list);
- // 查询角色信息
- RoleInfo Role_list;
- INIT_LIST_HEAD(&Role_list.list);
- g_token = json_to_Token_info(rx_Head_buf);
- // 根据token获取用户及用户权限
- int nRoleID=3;
- if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) )
- {
- char *nName = getUserNamebyToken(g_token);
- if (nName != NULL)
- {
- nRoleID = GetRoleIDByUserName(nName,&User_list);
- }
- }
- SetMenuAuthority(nRoleID);
- ack_str = user_routers_info_to_json();
- http_reply(conn, 200, ack_str);
- if (g_token != NULL)
- {
- free(g_token);
- }
- cJSON_free((void*)ack_str);
- }
- /// @brief 设备总体信息
- /// @param stream
- static void overallStatusMonitoring(void* conn)
- {
- int ret = -1 ;
- cJSON *cjson=NULL;
- cmd_data_t cmd;
- _Over_All_Sts_Monitor _over_all_sts_monitor={0};
- _OverBackStsInfo _over_back_sts_info; //设备总体状态信息
- _OverAllPwrAckInfo _over_all_pwr_back_info; //设备电源信息
- _OverAllPwrAckInfo *pall=&webHandle.stt.all;
- char* ack_str=NULL;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf = (char*)hm->body.buf;
- //转换json参数
- cjson = json_to_overall_status_monitor(rx_body_buf,&_over_all_sts_monitor);
- if(cjson==NULL)
- {
- if (strstr(hm->uri.buf,"type=export")) {
- file_io(conn, FILE_POWER_ALL, FILE_EXPORT, hm->uri.buf);
- }
- else {
- http_reply(conn, 400, NULL);
- }
- return;
- }
- //总电压电流信息
- if(strcmp(_over_all_sts_monitor.type,"listQuery")==0)
- {
- cur_dev_addr = _over_all_sts_monitor.product_id;
- if(cur_dev_addr>0) {
- int r=0;
- cmd.cmd = CASCADE_CMD_GET_INFO;
- cmd.obj = OBJ_OVERALL;
- r = cascade_request(&cmd);
- if(r) {
- goto quit;
- }
- cmd.cmd = CASCADE_CMD_QUERY_CH;
- cascade_request(&cmd);
- goto quit;
- }
- else {
- GlobalPowerInfo __globalPowerInfo;
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- _over_all_pwr_back_info.product_name = __globalDeviceManage._globalDevInfo.product.name;
- _over_all_pwr_back_info.product_id = __globalDeviceManage._globalDevInfo.product.id;
- _over_all_pwr_back_info.product_number = __globalDeviceManage._globalDevInfo.product.number;
- //查询数据库总电源信息
- if (__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
- )
- {
- if (strcmp(_over_all_sts_monitor.product_phOrder, "L1") == 0) // a相
- {
- __globalDeviceManage.nTreeACLshow = 0;
- dev_search_latest_t_ac_power_statistic_info(0, 0, &_over_all_pwr_back_info,&__globalDeviceManage);
- ack_str = over_all_pwr_monitor_to_json(0,&_over_all_pwr_back_info);
- }
- else if (strcmp(_over_all_sts_monitor.product_phOrder, "L2") == 0) // b相
- {
- __globalDeviceManage.nTreeACLshow = 1;
- dev_search_latest_t_ac_power_statistic_info(0, 1, &_over_all_pwr_back_info,&__globalDeviceManage);
- ack_str = over_all_pwr_monitor_to_json(1,&_over_all_pwr_back_info);
- }
- else if (strcmp(_over_all_sts_monitor.product_phOrder, "L3") == 0) // c相
- {
- __globalDeviceManage.nTreeACLshow = 2;
- dev_search_latest_t_ac_power_statistic_info(0, 2, &_over_all_pwr_back_info,&__globalDeviceManage);
- ack_str = over_all_pwr_monitor_to_json(2,&_over_all_pwr_back_info);
- }
- }
- else {
- if (dev_search_latest_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, 0, &__globalPowerInfo) != 0)
- {
- log_e("serach total pwr err.");
- }
- _over_all_pwr_back_info.voltage = __globalPowerInfo._power_info.voltage ;
- _over_all_pwr_back_info.current = __globalPowerInfo._power_info.current ;
- _over_all_pwr_back_info.power = __globalPowerInfo._power_info.power ;
- _over_all_pwr_back_info.freq = __globalPowerInfo._power_info.freq ;
- _over_all_pwr_back_info.consumption = __globalPowerInfo._power_info.consumption ;
- _over_all_pwr_back_info.factor = __globalPowerInfo._power_info.factor ;
- }
- ack_str = over_all_pwr_monitor_to_json(0,&_over_all_pwr_back_info);
- }
- }
- //打开所有开关
- else if(strcmp(_over_all_sts_monitor.type,"open")==0)
- {
- log_i("over all request:open");
- //总开
- int nSendAddr=0;
- char buff[256];
- if(cur_dev_addr>0) {
- cmd.cmd = CASCADE_CMD_OPEN;
- cmd.obj = OBJ_OVERALL;
- cmd.chId = 0xff;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if(_globalPowerMangerTemp==NULL)
- break;
- if(_globalPowerMangerTemp->product_saddr==nSendAddr)continue;
- nSendAddr=_globalPowerMangerTemp->product_saddr;
- log_i("Open All address Start");
- int rec=g_switch_set_all_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,1);
- if(rec<0)log_i("Open All address:%d rec:%d:%s",nSendAddr,rec,modbus_strerror(errno));
-
- }
- sprintf(buff,"$开启$|$所有$|$通道$");
- dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
- ack_str = ctrl_sts_ack_to_json();
- }
- //关闭所有开关
- else if(strcmp(_over_all_sts_monitor.type,"close")==0)
- {
- log_i("over all request:close");
- if(cur_dev_addr>0) {
- cmd.cmd = CASCADE_CMD_CLOSE;
- cmd.obj = OBJ_OVERALL;
- cmd.chId = 0xff;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
- //总关
- int nSendAddr=0;
- char buff[256];
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if(_globalPowerMangerTemp==NULL)
- break;
- if(_globalPowerMangerTemp->product_saddr==nSendAddr)continue;
- nSendAddr=_globalPowerMangerTemp->product_saddr;
- log_i("Close All address Start");
- int rec=g_switch_set_all_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp->product_ch_type,_globalPowerMangerTemp->product_saddr,0);
- if(rec<0)log_i("Close All address:%d rec:%d:%s",nSendAddr,rec,modbus_strerror(errno));
- }
- sprintf(buff,"$关闭$|$所有$|$通道$");
- dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
- ack_str = ctrl_sts_ack_to_json();
- }
- //查询历史记录
- //ok
- else if(strcmp(_over_all_sts_monitor.type,"listHisQuery")==0)
- {
- #if 0
- log_i("over all request:listHisQuery");
- int ret = 0 ;
- int size = 0 ;
- int page_num = 0 ;
- int page_size = 0 ;
- page_num = atoi(_over_all_sts_monitor.page_num);
- page_size = atoi(_over_all_sts_monitor.page_size);
- GlobalPowerInfo _globalPowerInfo;
-
- INIT_LIST_HEAD(&_globalPowerInfo.list);
- ret = dev_search_power_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- 0,
- _over_all_sts_monitor.start_time,
- _over_all_sts_monitor.end_time,
- page_num,
- page_size,
- &size,
- &_globalPowerInfo);
- if(ret!=0)
- {
- log_e("dev all status history get error.\n");
- }
- //返回应答
- ack_str = overall_power_history_to_json(&_globalPowerInfo,&__globalDeviceManage);
- //释放资源
- GlobalPowerInfo* node;
- GlobalPowerInfo* next;
- list_for_each_entry_safe(node, next, &_globalPowerInfo.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- #endif
- }
- //电压统计
- //1分钟1个点 6个小时 共360个点
- else if(strcmp(_over_all_sts_monitor.type,"listDyTotalQuery")==0)
- {
- //申请电压统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
- {
- ack_str = over_all_data_t_ac_to_json(pall,&_over_all_sts_monitor, TOTAL_VOLTAGE_TYPE);
- }
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
- {
- ph_id=2;
- }
-
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total voltage err.");
- }
- //else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
- pall->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
- }
- // 返回电压统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_VOLTAGE_TYPE);
- }
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total voltage err.");
- }
- //else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
- pall->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
- }
- // 返回电压统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_VOLTAGE_TYPE);
- }
- }
- }
- //电流统计
- else if(strcmp(_over_all_sts_monitor.type,"listDlTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listDlTotalQuery\n");
- #endif
- //申请电流统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
- {
- ack_str = over_all_data_t_ac_to_json(pall, &_over_all_sts_monitor, TOTAL_CURRENT_TYPE);
- }
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total current err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
- pall->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
- }
- // 返回电流统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_CURRENT_TYPE);
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total current err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
- pall->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
- }
- // 返回电流统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_CURRENT_TYPE);
- }
- }
- //功率统计
- else if(strcmp(_over_all_sts_monitor.type,"listGlTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listGlTotalQuery\n");
- #endif
- //申请功率统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
- {
- ack_str = over_all_data_t_ac_to_json(pall, &_over_all_sts_monitor, TOTAL_POWER_TYPE);
- }
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
- pall->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
- }
- // 返回功率统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_TYPE);
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
- pall->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
- }
- // 返回功率统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_TYPE);
- }
- }
- //耗电量统计
- else if(strcmp(_over_all_sts_monitor.type,"listHdlTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listHdlTotalQuery\n");
- #endif
- //申请耗电量统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- if (_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_Tree
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One
- ||_over_all_sts_monitor.product_pwr_type==SmartPDU_Tree_AC_One_B)
- {
- ack_str = over_all_data_t_ac_to_json(pall, &_over_all_sts_monitor, TOTAL_POWER_CONS_TYPE);
- }
- else if (_over_all_sts_monitor.product_pwr_type >= 0)
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power consumption err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
- pall->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
- }
- // 返回耗电量统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_CONS_TYPE);
- }
- 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)
- &&(__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) )
- {
- int ph_id=-1;
- if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power consumption err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
- pall->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
- }
- // 返回耗电量统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_CONS_TYPE);
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power consumption err.");
- }
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pall->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
- pall->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
- }
- // 返回耗电量统计
- ack_str = over_all_total_info_to_json(pall, TOTAL_POWER_CONS_TYPE);
- }
- }
- //查询系统总数情况
- else if(strcmp(_over_all_sts_monitor.type,"listTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listTotalQuery\n");
- #endif
- //初始化系统总数结构体
- SystemTotalInfo __system_total_info;
- memset(&__system_total_info, 0, sizeof(SystemTotalInfo));
- struct tm *t;
- struct timeval tv;
- char start_time[32] = {0};
- char stop_time[32] = {0};
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon+1, t->tm_mday, 0,0,0);
- //sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", 1970, 1, 1, 0,0,0);
- log_d(start_time);
- sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon+1, t->tm_mday, 23,59, 59);
- //sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", 1970, 1, 1, 23,59, 59);
- log_d(stop_time);
- //从数据库获取参数信息
- if(dev_get_system_total_info(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id,0,&__system_total_info,start_time,stop_time)!=0)
- {
- log_e("serach total System total err.");
- }
- int nSumChannel=0;
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- nSumChannel+=1;
- }
- __system_total_info.channel_total = nSumChannel ;
- //获取通道总数
- // __GetSystemTotalInfo.channel_total = _globalDeviceManager->useSlaveCount;
- //获取系统总数情况
- _over_back_sts_info.system_total.channel_total = __system_total_info.channel_total;
- _over_back_sts_info.system_total.log_total = __system_total_info.log_total;
- _over_back_sts_info.system_total.sensor_total = __system_total_info.sensor_total;
- _over_back_sts_info.system_total.warn_total = __system_total_info.warn_total;
- //返回系统总数情况
- ack_str = over_system_total_monitor_to_json(&_over_back_sts_info);
- }
- //功率趋势图接口
- else if(strcmp(_over_all_sts_monitor.type,"listGlAllTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listGlAllTotalQuery\n");
- #endif
- //初始化功率趋势图参数
- GlobalPowerInfo __PowerTrendInfo;
- //清0
- memset(&__PowerTrendInfo,0,sizeof(GlobalPowerInfo));
- #if 1
- 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)
- &&(__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) )
- {
- int ph_id=-1;
- if (strcmp(_over_all_sts_monitor.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_all_sts_monitor.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_all_sts_monitor.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power consumption err.");
- }
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power consumption err.");
- }
- }
- //传值给总管理结构体
- for(int i=0;i< TOTAL_COUNT;i++)
- {
- strcpy(_over_all_pwr_back_info._Power_Trend_Info.xAxis[i],__PowerTrendInfo._power_trend_info.xAxis[i]);
- _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_MERITORIOUS_TYPE][i] = __PowerTrendInfo._power_trend_info.ydata[POWER_MERITORIOUS_TYPE][i];
- _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_REACTIVE_TYPE][i] = __PowerTrendInfo._power_trend_info.ydata[POWER_REACTIVE_TYPE][i];
- _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_APPARENT_TYPE][i] = __PowerTrendInfo._power_trend_info.ydata[POWER_APPARENT_TYPE][i];
- }
- #else
- for(int i=0;i< TOTAL_COUNT;i++)
- {
- strcpy(_over_all_pwr_back_info._Power_Trend_Info.xAxis[i],"00:00 ");
- _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_MERITORIOUS_TYPE][i] = 10+i;
- _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_REACTIVE_TYPE][i] = 20+i;
- _over_all_pwr_back_info._Power_Trend_Info.ydata[POWER_APPARENT_TYPE][i] = 30+i;
- }
- #endif
- //返回功率趋势
- ack_str = over_power_trend_to_json(&_over_all_pwr_back_info);
- }
- quit:
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 电源通道
- /// @param stream
- static void channelStatusMonitoring(void* conn)
- {
- int ret = -1 ;
- cJSON *cjson;
- cmd_data_t cmd;
- _OverChnPwrRequest _over_chn_pwr_request;
- _OverAllPwrAckInfo _over_all_pwr_back_info;
- _OverAllPwrAckInfo *pch=&webHandle.stt.chn;
- char* ack_str = NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf=(char*)hm->body.buf;
- //转换json参数
- memset(&_over_chn_pwr_request,0,sizeof(_OverChnPwrRequest));
- cjson = json_to_chn_pwr_status_monitor(rx_body_buf,&_over_chn_pwr_request);
- if(cjson==NULL)
- {
- if (strstr(hm->uri.buf,"type=export")) {
- file_io(conn, FILE_POWER_CHN, FILE_EXPORT, hm->uri.buf);
- }
- return;
- }
- if (_over_chn_pwr_request.Zerotype!=NULL)
- {
- if (strcmp(_over_chn_pwr_request.Zerotype, "open") == 0) // 零线开
- {
- int ret = 0;
- int product_chn_id = 0;
- char buff[256];
- log_i("channel NF status request:open");
- product_chn_id = _over_chn_pwr_request.productChId;
- if (cur_dev_addr > 0)
- {
- cmd.cmd = CASCADE_CMD_OPEN_NF;
- cmd.obj = OBJ_CHANNEL;
- cmd.chId = product_chn_id;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- // log_d("channel:%d", _globalPowerMangerTemp->product_ch_id);
- if (_globalPowerMangerTemp == NULL)
- break;
- if (product_chn_id == _globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d", _globalPowerMangerTemp->product_ch_id);
- ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr,
- 1);
- if (ret < 0)
- {
- ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr,
- 1);
- }
- sprintf(buff, "$开启$|$通道$|%d Naught wire!", _globalPowerMangerTemp->product_ch_id);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, _globalPowerMangerTemp->product_id, ALARM_TYPE_OPRATION, buff);
- break;
- }
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- else if (strcmp(_over_chn_pwr_request.Zerotype, "close") == 0) // 零线关
- {
- int ret = 0;
- int product_chn_id = 0;
- char buff[256];
- log_i("channel NF status request:open");
- product_chn_id = _over_chn_pwr_request.productChId;
- if (cur_dev_addr > 0)
- {
- cmd.cmd = CASCADE_CMD_CLOSE_NF;
- cmd.obj = OBJ_CHANNEL;
- cmd.chId = product_chn_id;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- // log_d("channel:%d", _globalPowerMangerTemp->product_ch_id);
- if (_globalPowerMangerTemp == NULL)
- break;
- if (product_chn_id == _globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d", _globalPowerMangerTemp->product_ch_id);
- ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr,
- 0);
- if (ret < 0)
- {
- ret = g_switch_set_t_ac_chn_NF_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr,
- 0);
- }
- sprintf(buff, "$关闭$|$通道$|%d Naught wire!", _globalPowerMangerTemp->product_ch_id);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, _globalPowerMangerTemp->product_id, ALARM_TYPE_OPRATION, buff);
- break;
- }
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- }
-
- if (_over_chn_pwr_request.type==NULL)
- {
- goto quit;
- }
-
- if(strcmp(_over_chn_pwr_request.type,"listQuery")==0||strcmp(_over_chn_pwr_request.type,"resetChannel")==0)
- {
- //log_i("channel status request:listQuery");
- //log_d(rx_body_buf);
- if (strcmp(_over_chn_pwr_request.type,"resetChannel")==0)
- {
- //__globalDeviceManage._ReloadChm_flag=true;
- //ResetChmData();
- //__globalDeviceManage._ReloadChm_flag=false;
- }
- GlobalPowerInfo _powerInfo;
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- int product_chn_id = 0 ;
- _OverChnPwrAckInfo _over_chn_pwr_back_info;
- INIT_LIST_HEAD(&_over_chn_pwr_back_info.list);
- product_chn_id = _over_chn_pwr_request.productChId;
- cur_dev_addr = _over_chn_pwr_request.product_id;
- if(cur_dev_addr>0) {
- int r;
- cmd.cmd = CASCADE_CMD_GET_INFO;
- cmd.obj = OBJ_CHANNEL;
- r = cascade_request(&cmd);
- if(r) {
- goto quit;
- }
- cmd.cmd = CASCADE_CMD_QUERY_CH;
- r = cascade_request(&cmd);
- if(r) {
- goto quit;
- }
- cascade_get_ch(&_over_chn_pwr_back_info);
- }
- else {
- if(product_chn_id==0)
- {
- lock_s_hold(LOCK_ID_POWER_UPDATE);
- dev_search_latest_power_All_info(&__globalDeviceManage,&_over_chn_pwr_back_info, cur_dev_addr);
- lock_s_release(LOCK_ID_POWER_UPDATE);
- }
- else //指定通道
- {
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if(product_chn_id==_globalPowerMangerTemp->product_ch_id)
- {
- break;
- }
- }
- //更新数据
-
- //log_d("serach ch%d pwr ok.",product_chn_id);
- _OverChnPwrAckInfo* _overChnPwrBackInfoTemp = (_OverChnPwrAckInfo*)malloc(sizeof(_OverChnPwrAckInfo));
- if(_overChnPwrBackInfoTemp==NULL)
- {
- log_e("_overChnPwrBackInfoTemp malloc err.");
- }
- //查询数据库总电源信息
- if(dev_search_latest_power_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id,
- product_chn_id, &_powerInfo)!=0)
- {
- log_e("serach total pwr err.");
- }
- {
- //填数据
- _overChnPwrBackInfoTemp->product_name = __globalDeviceManage._globalDevInfo.product.name;
- _overChnPwrBackInfoTemp->product_number = __globalDeviceManage._globalDevInfo.product.number;
- _overChnPwrBackInfoTemp->product_id = __globalDeviceManage._globalDevInfo.product.id;
- _overChnPwrBackInfoTemp->productChName = _globalPowerMangerTemp->product_ch_name;
- _overChnPwrBackInfoTemp->productChId = _globalPowerMangerTemp->product_ch_id;
- _overChnPwrBackInfoTemp->status = _powerInfo._power_info.status;
- _overChnPwrBackInfoTemp->NF_status = _powerInfo._power_info.NF_status;
- _overChnPwrBackInfoTemp->voltage = _powerInfo._power_info.voltage;
- _overChnPwrBackInfoTemp->current = _powerInfo._power_info.current;
- _overChnPwrBackInfoTemp->power = _powerInfo._power_info.power ;
- _overChnPwrBackInfoTemp->freq = _powerInfo._power_info.freq ;
- _overChnPwrBackInfoTemp->consumption = _powerInfo._power_info.consumption ;
- _overChnPwrBackInfoTemp->factor = _powerInfo._power_info.factor ;
- list_add_tail(&_overChnPwrBackInfoTemp->list,&_over_chn_pwr_back_info.list);
- }
- }
- ack_str = over_chn_pwr_monitor_to_json(&_over_chn_pwr_back_info);
- _OverChnPwrAckInfo *node,*next;
- list_for_each_entry_safe(node, next, &_over_chn_pwr_back_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- }
- else if(strcmp(_over_chn_pwr_request.type,"open")==0)//通道开
- {
- int ret=0;
- int product_chn_id = 0 ;
- char buff[256];
- log_i("channel status request:open");
- /*
- * todo:添加控制
- */
- product_chn_id = _over_chn_pwr_request.productChId;
- if(cur_dev_addr>0) {
- cmd.cmd = CASCADE_CMD_OPEN;
- cmd.obj = OBJ_CHANNEL;
- cmd.chId = product_chn_id;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
-
- log_d("channel id:%d", product_chn_id);
- // product_chn_id = strtol(_over_chn_pwr_request.productChId,NULL,10);
- // log_d("productChId:%d",product_chn_id);
-
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- //log_d("channel:%d", _globalPowerMangerTemp->product_ch_id);
- if(_globalPowerMangerTemp==NULL)
- break;
- if(product_chn_id==_globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d",_globalPowerMangerTemp->product_ch_id);
- ret=g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr,
- 1,false);
- if (ret < 0)
- {
- ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr,
- 1,false);
- }
- char buff[256];
- sprintf(buff,"$开启$|$通道$|%d",_globalPowerMangerTemp->product_ch_id);
- dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
- break;
- }
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- else if(strcmp(_over_chn_pwr_request.type,"close")==0)//通道关
- {
- log_i("channel status request:close");
- /*
- * todo:添加控制
- */
- char buff[256];
- int product_chn_id = 0 ;
- product_chn_id = _over_chn_pwr_request.productChId;
- if(cur_dev_addr>0) {
- cmd.cmd = CASCADE_CMD_CLOSE;
- cmd.obj = OBJ_CHANNEL;
- cmd.chId = product_chn_id;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
- // product_chn_id = strtol(_over_chn_pwr_request.productChId,NULL,10);
- // log_d("productChId:%d",product_chn_id);
-
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if(_globalPowerMangerTemp==NULL)
- break;
- if(product_chn_id==_globalPowerMangerTemp->product_ch_id)
- {
- //log_d("switch ctrl:%d",_globalPowerMangerTemp->product_ch_id);
- ret=g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,_globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr ,//多版端口余8
- 0,false);
- if (ret < 0)
- {
- ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
- 0,false);
- /* code */
- }
- sprintf(buff,"$关闭$|$通道$|%d",_globalPowerMangerTemp->product_ch_id);
- dev_insert_alarm_ctrl(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,ALARM_TYPE_OPRATION,buff);
- break;
- }
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- else if(strcmp(_over_chn_pwr_request.type,"save")==0)//负载
- {
- log_i("channel status request:save");
- if(cur_dev_addr>0) {
- cmd.cmd = CASCADE_CMD_SAVE;
- cmd.obj = OBJ_CHANNEL;
- cmd.chId = _over_chn_pwr_request.productChId;
- cascade_request(&cmd);
- ack_str = ctrl_sts_ack_to_json();
- goto quit;
- }
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_over_chn_pwr_request.productChId == _globalPowerMangerTemp->product_ch_id)
- {
- break;
- }
- }
- if (_globalPowerMangerTemp != NULL)
- {
- if (strlen(_over_chn_pwr_request.productChName)<32)
- {
- strcpy(_globalPowerMangerTemp->product_ch_name, _over_chn_pwr_request.productChName);
- if (dev_update_power_manage_genera_info(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, _globalPowerMangerTemp->product_ch_id, _globalPowerMangerTemp) != 0)
- {
- log_e("update pwr general data err.");
- }
- }else{
- log_e("ChName too long.");
- }
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- else if(strcmp(_over_chn_pwr_request.type,"listHisQuery")==0) //历史记录查询
- {
- #if 0
- int ret = 0 ;
- int size = 0 ;
- int h_pwrchn = 0 ;
- int page_num = 0 ;
- int page_size = 0 ;
- GlobalPowerInfo _globalPowerInfo;
- INIT_LIST_HEAD(&_globalPowerInfo.list);
- h_pwrchn = strtol(_over_chn_pwr_request.productChId,NULL,10);
- page_num = atoi(_over_chn_pwr_request.pageNum);
- page_size = atoi(_over_chn_pwr_request.pageSize);
- //查询数据
- ret = dev_search_power_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- h_pwrchn,
- _over_chn_pwr_request.start_time,
- _over_chn_pwr_request.end_time,
- page_num,
- page_size,
- &size,
- &_globalPowerInfo);
- if(ret!=0)
- {
- log_e("dev all status history get error.\n");
- }
- //应答
- ack_str = over_channel_power_history_to_json(&_globalPowerInfo,
- &__globalDeviceManage._globalDevInfo,
- _over_chn_pwr_request.productChName,
- h_pwrchn);
- //释放资源
- GlobalPowerInfo* node;
- GlobalPowerInfo* next;
- list_for_each_entry_safe(node, next, &_globalPowerInfo.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- #endif
- }
- else if(strcmp(_over_chn_pwr_request.type,"listDyTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listDyTotalQuery\n");
- #endif
- int product_chn_id = 0 ;
- product_chn_id = _over_chn_pwr_request.productChId;
- /*
- log_d("productChId:%d",product_chn_id);
- log_i("channel status request:listDyTotalQuery");
- log_d("productChId:%d",_over_chn_pwr_request.productChId);
- log_d("start_time:%s",_over_chn_pwr_request.start_time);
- log_d("end_time:%s",_over_chn_pwr_request.end_time);
- log_d("lType:%s",_over_chn_pwr_request.lType);
- log_d("type:%s",_over_chn_pwr_request.type);
- log_d("user_token:%s",_over_chn_pwr_request.user_token);
- */
- //ack_str = ctrl_sts_ack_to_json();
- //申请电压统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- //查询数据库通道电源信息 传给所申请的参数结构体
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power voltage err.");
- }
- // else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
- pch->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
- }
- // 返回电压统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_VOLTAGE_TYPE);
- }
- }
- else
- {
- 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)
- {
- log_e("serach total voltage err.");
- }
- // else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_voltage.xAxis[i], __globalPowerInfo._total_voltage.xAxis[i]);
- pch->_total_voltage.ydata[i] = __globalPowerInfo._total_voltage.ydata[i];
- }
- // 返回电压统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_VOLTAGE_TYPE);
- }
- }
- }
- //电流统计
- else if(strcmp(_over_chn_pwr_request.type,"listDlTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listDlTotalQuery\n");
- #endif
- int product_chn_id = 0 ;
- log_i("channel status request:listDlTotalQuery");
- product_chn_id = _over_chn_pwr_request.productChId;
- log_d("productChId:%d",product_chn_id);
- //申请电流统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- //查询数据库通道电源信息 传给所申请的参数结构体
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power current err.");
- }
- // else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
- pch->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
- }
- // 返回电流统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_CURRENT_TYPE);
- }
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total current err.");
- }
- //else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_current.xAxis[i], __globalPowerInfo._total_current.xAxis[i]);
- pch->_total_current.ydata[i] = __globalPowerInfo._total_current.ydata[i];
- }
- // 返回电流统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_CURRENT_TYPE);
- }
- }
- }
- //功率统计
- else if(strcmp(_over_chn_pwr_request.type,"listGlTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listGlTotalQuery\n");
- #endif
- int product_chn_id = 0 ;
- log_i("channel status request:listGlTotalQuery");
- product_chn_id = _over_chn_pwr_request.productChId;
- log_d("productChId:%d",product_chn_id);
- //申请功率统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- //查询数据库通道电源信息 传给所申请的参数结构体
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power err.");
- }
- //else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
- pch->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
- }
- // 返回功率统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_TYPE);
- }
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power err.");
- }
- //else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_power.xAxis[i], __globalPowerInfo._total_power.xAxis[i]);
- pch->_total_power.ydata[i] = __globalPowerInfo._total_power.ydata[i];
- }
- // 返回功率统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_TYPE);
- }
- }
- }
- //耗电量统计
- else if(strcmp(_over_chn_pwr_request.type,"listHdlTotalQuery")==0)
- {
- #if DBUG_INFO
- log_i("over all request: listHdlTotalQuery\n");
- #endif
- int product_chn_id = 0 ;
- log_i("channel status request:listHdlTotalQuery");
- product_chn_id = _over_chn_pwr_request.productChId;
- log_d("productChId:%d",product_chn_id);
- //申请耗电量统计信息参数
- GlobalPowerInfo __globalPowerInfo;
- //清0
- memset(&__globalPowerInfo,0,sizeof(GlobalPowerInfo));
- //查询数据库通道电源信息 传给所申请的参数结构体
- 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))
- {
- int ph_id=-1;
- if (strcmp(_over_chn_pwr_request.lType,"L1")==0)
- {
- ph_id=0;
- }
- else if (strcmp(_over_chn_pwr_request.lType,"L2")==0 )
- {
- ph_id=1;
- }
- else if(strcmp(_over_chn_pwr_request.lType,"L3")==0 )
- {
- ph_id=2;
- }
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power err.");
- }
- // else
- {
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
- pch->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
- }
- // 返回耗电量统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_CONS_TYPE);
- }
- }
- else
- {
- // 查询数据库总电源信息 传给所申请的参数结构体
- 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)
- {
- log_e("serach total power consumption err.");
- }
- // else
- {
- // 传值给总管理结构体
- for (int i = 0; i < TOTAL_COUNT; i++)
- {
- strcpy(pch->_total_consumption.xAxis[i], __globalPowerInfo._total_consumption.xAxis[i]);
- pch->_total_consumption.ydata[i] = __globalPowerInfo._total_consumption.ydata[i];
- }
- // 返回耗电量统计
- ack_str = over_all_total_info_to_json(pch, TOTAL_POWER_CONS_TYPE);
- }
- }
- }
- else if(strcmp(_over_chn_pwr_request.type,"threeTypeSave")==0)
- {
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- if(cur_dev_addr>0) {
- cmd.cmd = CASCADE_CMD_SAVE3;
- cmd.obj = OBJ_CHANNEL;
- cmd.chId = _over_chn_pwr_request.productChId;
- cascade_request(&cmd);
- goto quit;
- }
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_over_chn_pwr_request.productChId == _globalPowerMangerTemp->product_ch_id)
- {
- break;
- }
- }
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One
- ||__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One_B)
- {
- if (_over_chn_pwr_request.outputType == 1)
- {
- http_reply(conn, 201, ack_str);
- cJSON_free((void *)ack_str);
- return;
- }
- }
- if (_globalPowerMangerTemp != NULL)
- {
- GlobalTreeACManager *_globalTACManager = NULL;
- list_for_each_entry(_globalTACManager, &_globalPowerMangerTemp->list_Tree_AC, list_Tree_AC)
- {
- if (_over_chn_pwr_request.outputType == 2)
- {
- if (strcmp(g_product_t_ac_type_str[_globalTACManager->product_ph_type], _over_chn_pwr_request.productPhOrder) == 0)
- {
- _globalTACManager->product_ph_outputStatus = 1;
- }
- else
- {
- _globalTACManager->product_ph_outputStatus = 2;
- }
- }
- else
- {
- _globalTACManager->product_ph_outputStatus = 1;
- }
- _globalTACManager->product_ph_outputType=_over_chn_pwr_request.outputType;
- if (dev_update_t_ac_power_manage_info(__globalDeviceManage.db, _globalTACManager) != 0)
- {
- log_e("update pwr general data err.");
- }
- }
- }
- }
- else if (strcmp(_over_chn_pwr_request.type, "batteryReset") == 0) // 耗电量复位
- {
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- char *pCHlist;
- char *pCHListSave;
- pCHlist = strtok_r(_over_chn_pwr_request.strProductChIds, ",", &pCHListSave);
- // 其他版本功能
- while (pCHlist != NULL)
- {
- int g_pwr_ch_id = atoi(pCHlist);
- _globalPowerMangerTemp=NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (g_pwr_ch_id != _globalPowerMangerTemp->product_ch_id)
- {
- continue;
- }
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_DC)
- {
- if(g_switch_set_dcpdu_chn_reset(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr-1)<0)
- {
- log_e("Error: g_switch_set_dcpdu_chn_reset");
- }
- }
- else
- {
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_AC)
- {
- if (g_switch_set_t_ac_chn_reset(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr - 1) < 0)
- {
- log_e("Error: g_switch_set_t_ac_chn_reset");
- }
- }
- else
- {
- GlobalTreeACManager *_globalTACManager = NULL;
- list_for_each_entry(_globalTACManager, &_globalPowerMangerTemp->list_Tree_AC, list_Tree_AC)
- {
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree)
- {
- if (g_switch_set_t_ac_phchn_reset(&__globalDeviceManage._globalRelaySampManger, _globalTACManager->product_saddr, _globalTACManager->product_ch_addr - 1) < 0)
- {
- log_e("Error: g_switch_set_t_ac_phchn_reset");
- }
- }
- else
- {
- if (g_switch_set_t_ac_chn_reset(&__globalDeviceManage._globalRelaySampManger, _globalTACManager->product_saddr, _globalTACManager->product_ch_addr - 1) < 0)
- {
- log_e("Error: g_switch_set_t_ac_chn_reset");
- }
- }
- }
- }
- }
- }
- pCHlist = strtok_r(NULL, ",", &pCHListSave);
- }
- char log_buff[256];
- sprintf(log_buff, "Reset power consumption!");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
- }
- quit:
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 传感器状态请求应答
- /// @param stream
- static void sensorStatusMonitoring(void* conn)
- {
- int ret = -1 ;
- cJSON *cjson;
- _OverSensorRequest _over_all_sensor_request;
- _OverSensorBackInfo _over_all_sensor_back_info;
- GlobalSensorManger _globalSensorMangerTemp ;
- GlobalSensorInfo _globalSensorInfo;
- char* ack_str = NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf=(char*)hm->body.buf;
- //转换json参数
- cjson = json_to_sensor_manage(rx_body_buf,&_over_all_sensor_request);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
- //查询json参数
- if(strcmp(_over_all_sensor_request.type,"listQuery")==0)
- {
- log_d("start listQuery handle.");
-
- #if(1)
- lock_s_hold(LOCK_ID_SENSOR);
- INIT_LIST_HEAD(&_over_all_sensor_back_info.list);
- //获取传感器当前的最新值
- GlobalSensorManger* _globalSensorManagerTemp = NULL;
- list_for_each_entry(_globalSensorManagerTemp, &__globalDeviceManage._globalSensorManger.list, list)
- {
- _OverSensorBackInfo* _overSensorBackInfoTemp = (_OverSensorBackInfo*)malloc(sizeof(_OverSensorBackInfo));
- if(_overSensorBackInfoTemp==NULL)
- log_e("_OverSensorBackInfo malloc error.");
- memset(_overSensorBackInfoTemp,0,sizeof(_OverSensorBackInfo));
- //基本信息
- _overSensorBackInfoTemp->sensorId = _globalSensorManagerTemp->sensor_id;
- //strcpy(_overSensorBackInfoTemp->sensorName,language_sensor_name[__globalDeviceManage._globalDevInfo.product.language][(int)_globalSensorManagerTemp->sensor_type]);
- strcpy(_overSensorBackInfoTemp->sensorName,_globalSensorManagerTemp->sensor_name);
-
- _overSensorBackInfoTemp->sensorType = _globalSensorManagerTemp->sensor_type;
- memcpy(&_overSensorBackInfoTemp->comSlogan,&_globalSensorManagerTemp->sensor_port_name,sizeof(_overSensorBackInfoTemp->comSlogan));
- //485独有
- _overSensorBackInfoTemp->connectionStatus = _globalSensorManagerTemp->sensor_status;
- _overSensorBackInfoTemp->nodeNumber = __globalDeviceManage._globalSensorManger.sensor_node_number;
- if(_globalSensorManagerTemp->sensor_interface_type==1)
- {
- _overSensorBackInfoTemp->sensorAddr = _globalSensorManagerTemp->sensor_addr ;
- _overSensorBackInfoTemp->sensorBaud = _globalSensorManagerTemp->sensor_baud;
- strcpy(_overSensorBackInfoTemp->sensorvaluech,"RS485");
- }
- else
- {
- //_overSensorBackInfoTemp->connectionStatus = -1;
- strcpy(_overSensorBackInfoTemp->sensorvaluech,_overSensorBackInfoTemp->comSlogan);
- _overSensorBackInfoTemp->nodeNumber =_globalSensorManagerTemp->sensor_addr;
- }
- //拿数据
- memset(&_globalSensorInfo, 0, sizeof(_globalSensorInfo));
- switch(_globalSensorManagerTemp->sensor_type)
- {
- case 0://温湿度传感器 2个值
- {
- _overSensorBackInfoTemp->temperatureValue = _globalSensorManagerTemp->Cur_sensor_info.val1;
- _overSensorBackInfoTemp->humidityValue =_globalSensorManagerTemp->Cur_sensor_info.val2;
- }break;
- case 1://烟雾传感器 1个值
- {
-
- _overSensorBackInfoTemp->alarmStatus = ! (bool) _globalSensorManagerTemp->Cur_sensor_info.val1; // 1/0 报警未报警
- }break;
- case 2://水浸传感器 1个值 c
- {
- _overSensorBackInfoTemp->waterImmersionState = !(bool)_globalSensorManagerTemp->Cur_sensor_info.val1; // 1/0 报警未报警
- }break;
- case 3://门禁传感器
- {
- _overSensorBackInfoTemp->accessControlStatus = !(bool)_globalSensorManagerTemp->Cur_sensor_info.val1; // 1/0 报警未报警
- }break;
- case 4: //气体传感器 1个值
- {
- _overSensorBackInfoTemp->gasConcentration = _globalSensorManagerTemp->Cur_sensor_info.val1; // 气体浓度
- }break;
- case 5://气压气流传感器 2个值
- {
- _overSensorBackInfoTemp->airPressureValue = _globalSensorManagerTemp->Cur_sensor_info.val1; // 气压
- _overSensorBackInfoTemp->airflowValue = _globalSensorManagerTemp->Cur_sensor_info.val2; // 气流
- };break;
- case 6://空调遥控器 没有值
- {
- }break;
- case 7://双头温度传感器 2个值
- {
- _overSensorBackInfoTemp->temperatureValue = _globalSensorManagerTemp->Cur_sensor_info.val1;
- _overSensorBackInfoTemp->temperatureValue2 =_globalSensorManagerTemp->Cur_sensor_info.val2;
- }break;
- }
- //入队
- list_add_tail(&_overSensorBackInfoTemp->list,&_over_all_sensor_back_info.list);
- }
- lock_s_release(LOCK_ID_SENSOR);
- //参数转换json
- ack_str = over_all_sensor_info_to_json(&_over_all_sensor_back_info,0);
- //释放资源
- _OverSensorBackInfo* node;
- _OverSensorBackInfo* next;
- list_for_each_entry_safe(node, next, &_over_all_sensor_back_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- #endif
- }
- //删除传感器
- else if(strcmp(_over_all_sensor_request.type,"delete")==0)
- {
- log_d("start delete handle.");
- int sensor_id_temp = _over_all_sensor_request.sensorId;
-
- if(dev_delete_sensor_manage(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id,sensor_id_temp)!=0)
- {
- log_e("sensor delete failed.");
- }
- else
- {
- // 维护列表也需要删除
- lock_s_hold(LOCK_ID_SENSOR);
- GlobalSensorManger *node;
- GlobalSensorManger *next;
- list_for_each_entry_safe(node, next, &__globalDeviceManage._globalSensorManger.list, list)
- {
- if (node->sensor_id == sensor_id_temp)
- {
- list_del(&node->list);
- free(node);
- break;
- }
- }
- lock_s_release(LOCK_ID_SENSOR);
- }
- char temp[100];
- //sprintf(temp,"$删除$|$传感器$|%d",sensor_id_temp);
- sprintf(temp,"Delete sensor %d",sensor_id_temp);
- dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- //更新传感器
- else if(strcmp(_over_all_sensor_request.type,"update")==0)
- {
- log_d("start update handle.");
- int sensor_id_temp = _over_all_sensor_request.sensorId;
-
- GlobalSensorManger *node = NULL;
- GlobalSensorManger *next = NULL;
- list_for_each_entry_safe(node, next, &__globalDeviceManage._globalSensorManger.list, list)
- {
- if (node->sensor_id == sensor_id_temp)
- {
- break;
- }
- }
- if (node)
- {
- #if (1)
- GlobalSensorManger *_globalSensorMangerTemp = node;
- // 填充数据
- _globalSensorMangerTemp->sensor_type = strtol(_over_all_sensor_request.sensorType, NULL, 10);
- if (_over_all_sensor_request.sensorName)
- {
- int nSize=strlen(_over_all_sensor_request.sensorName);
- if (nSize<=64)
- {
- strcpy(_globalSensorMangerTemp->sensor_name, _over_all_sensor_request.sensorName);
- }
- else{
- //响应
- //stream->errorMsg="Edit Name Too Long";
- http_reply(conn, 500, ack_str);
- //释放资源
- cJSON_free((void*)ack_str);
- return ;
- }
- }
- if(_over_all_sensor_request.cgqtd)
- {
- #ifdef IN_INDEX_MAP
- char index_map[4]=IN_INDEX_MAP;
- #else
- char index_map[4]={0,1,3,2};
- #endif
- if (strcmp(_over_all_sensor_request.cgqtd, "RS485") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 1;
- if (_over_all_sensor_request.sensorAddr)
- {
- _globalSensorMangerTemp->sensor_addr = strtol(_over_all_sensor_request.sensorAddr,NULL,10);
- }
- //前端未传com口信息,暂且不改
- //if (_over_all_sensor_request.comSlogan)
- // {
- // strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.comSlogan);
- // }
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN1") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[0];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN2") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[1];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN3") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[2];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN4") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[3];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- }
- }
-
- //_globalSensorMangerTemp->sensor_baud = strtol(_over_all_sensor_request.sensorBaud, NULL, 10);
- //_globalSensorMangerTemp->sensor_addr = strtol(_over_all_sensor_request.sensorAddr, NULL, 10);
- //_globalSensorMangerTemp->sensor_status = 0;
- switch (_globalSensorMangerTemp->sensor_type)
- {
- case 0:
- _globalSensorMangerTemp->sensor_val_count = 2;
- break; // 温湿度传感器 2个值
- case 1:
- _globalSensorMangerTemp->sensor_val_count = 1;
- break; // 烟雾传感器 1个值
- case 2:
- _globalSensorMangerTemp->sensor_val_count = 1;
- break; // 水浸传感器 1个值
- case 3:
- _globalSensorMangerTemp->sensor_val_count = 1;
- break; // 门禁传感器 没有值
- case 4:
- _globalSensorMangerTemp->sensor_val_count = 1;
- break; // 气体传感器 1个值
- case 5:
- _globalSensorMangerTemp->sensor_val_count = 2;
- break; // 气压气流传感器 2个值
- case 6:
- _globalSensorMangerTemp->sensor_val_count = 0;
- break; // 空调遥控器 没有值
- case 7:
- _globalSensorMangerTemp->sensor_val_count = 2;
- break; // 双头温度传感器 2个值
- }
- // 插入数据库
- if (dev_update_sensor_genera_manage(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- _globalSensorMangerTemp->sensor_id,
- _globalSensorMangerTemp) != 0)
- {
- log_e("dev update error.");
- }
- #endif
- }
- char temp[100];
- // sprintf(temp,"$修改$|$传感器$|%d",sensor_id_temp);
- sprintf(temp,"Update sensor %d",sensor_id_temp);
- dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- //添加传感器
- else if(strcmp(_over_all_sensor_request.type,"add")==0) //正常跑过
- {
- log_d("start add handle.");
- int wrong_param=0;
- #ifdef IN_INDEX_MAP
- char index_map[4]=IN_INDEX_MAP;
- #else
- char index_map[4]={0,1,3,2};
- #endif
- GlobalSensorManger* _globalSensorMangerTemp = (GlobalSensorManger*)malloc(sizeof(GlobalSensorManger));
- //清空数据
- memset(_globalSensorMangerTemp, 0, sizeof(GlobalSensorManger));
- //填充数据
- _globalSensorMangerTemp->product_id = __globalDeviceManage._globalDevInfo.product.id ;
- int nmaxID=0;
- lock_s_hold(LOCK_ID_SENSOR);
- GlobalSensorManger *node;
- GlobalSensorManger *next;
- list_for_each_entry_safe(node, next, &__globalDeviceManage._globalSensorManger.list, list)
- {
- if (_over_all_sensor_request.cgqtd)
- {
- if (strcmp(_over_all_sensor_request.cgqtd, "RS485") == 0 && node->sensor_interface_type == 1)
- {
- if (strtol(_over_all_sensor_request.sensorAddr, NULL, 10) == node->sensor_addr)
- {
- wrong_param = 1;
- }
- }
- if (strcmp(_over_all_sensor_request.cgqtd, "IN1") == 0 && node->sensor_interface_type == 2)
- {
- if (index_map[0] == node->sensor_addr)
- {
- wrong_param = 1;
- }
- }
- if (strcmp(_over_all_sensor_request.cgqtd, "IN2") == 0 && node->sensor_interface_type == 2)
- {
- if (index_map[1] == node->sensor_addr)
- {
- wrong_param = 1;
- }
- }
- if (strcmp(_over_all_sensor_request.cgqtd, "IN3") == 0 && node->sensor_interface_type == 2)
- {
- if (index_map[2] == node->sensor_addr)
- {
- wrong_param = 1;
- }
- }
- if (strcmp(_over_all_sensor_request.cgqtd, "IN4") == 0 && node->sensor_interface_type == 2)
- {
- if (index_map[3] == node->sensor_addr)
- {
- wrong_param = 1;
- }
- }
- }
- if (node->sensor_id > nmaxID)
- {
- nmaxID = node->sensor_id;
- }
- }
- lock_s_release(LOCK_ID_SENSOR);
- _globalSensorMangerTemp->sensor_id = nmaxID+1;
- strcpy( _globalSensorMangerTemp->sensor_name,_over_all_sensor_request.sensorName);
- if(_over_all_sensor_request.sensorType && strlen(_over_all_sensor_request.sensorType)) {
- _globalSensorMangerTemp->sensor_type = strtol(_over_all_sensor_request.sensorType,NULL,10);
- }
- else {
- wrong_param = 1;
- }
- if (_over_all_sensor_request.cgqtd)
- {
- if (strcmp(_over_all_sensor_request.cgqtd, "RS485") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 1;
- if (_over_all_sensor_request.sensorAddr)
- {
- _globalSensorMangerTemp->sensor_addr = strtol(_over_all_sensor_request.sensorAddr, NULL, 10);
- }
- // if (_over_all_sensor_request.sensorBaud && strlen(_over_all_sensor_request.sensorBaud))
- // {
- _globalSensorMangerTemp->sensor_baud = 9600; // strtol(_over_all_sensor_request.sensorBaud,NULL,10);//默认9600
- // }
- // 前端未传com口信息,暂且不改
- // if (_over_all_sensor_request.comSlogan)
- // {
- strcpy(_globalSensorMangerTemp->sensor_port_name, "COM");
- // }
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN1") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[0];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- _globalSensorMangerTemp->sensor_baud = 0;
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN2") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[1];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- _globalSensorMangerTemp->sensor_baud = 0;
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN3") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr =index_map[2];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- _globalSensorMangerTemp->sensor_baud = 0;
- }
- else if (strcmp(_over_all_sensor_request.cgqtd, "IN4") == 0)
- {
- _globalSensorMangerTemp->sensor_interface_type = 2;
- _globalSensorMangerTemp->sensor_addr = index_map[3];
- strcpy(_globalSensorMangerTemp->sensor_port_name, _over_all_sensor_request.cgqtd);
- _globalSensorMangerTemp->sensor_baud = 0;
- }
- }
- else
- {
- wrong_param = 1;
- }
- if(wrong_param) {
- cJSON_Delete(cjson);
- ack_str = ctrl_breaker_err_ack_to_json("Sensor parameter error!");
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- return ;
- }
-
- _globalSensorMangerTemp->sensor_status = 0;
- switch( _globalSensorMangerTemp->sensor_type)
- {
- case 0:_globalSensorMangerTemp->sensor_val_count = 2 ;break; //温湿度传感器 2个值
- case 1:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //烟雾传感器 1个值
- case 2:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //水浸传感器 1个值
- case 3:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //门禁传感器 没有值
- case 4:_globalSensorMangerTemp->sensor_val_count = 1 ;break; //气体传感器 1个值
- case 5:_globalSensorMangerTemp->sensor_val_count = 2 ;break; //气压气流传感器 2个值
- case 6:_globalSensorMangerTemp->sensor_val_count = 0 ;break; //空调遥控器 没有值
- case 7:_globalSensorMangerTemp->sensor_val_count = 2 ;break; //双头温度传感器 2个值
- default:break;
- }
- //插入数据库
- if(dev_insert_sensor_genera_manage(__globalDeviceManage.db,_globalSensorMangerTemp)!=0)
- {
- log_e("dev insert error.");
- }
- //全局接口体中也需要添加相关参数
- lock_s_hold(LOCK_ID_SENSOR);
- list_add_tail(&_globalSensorMangerTemp->list,&__globalDeviceManage._globalSensorManger.list);
- lock_s_release(LOCK_ID_SENSOR);
- char temp[100];
- // sprintf(temp,"$新增$|$传感器$|%d",_over_all_sensor_request.sensorId);
- sprintf(temp,"Add sensor %d",_over_all_sensor_request.sensorId);
- dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- //烟雾报警复位
- else if(strcmp(_over_all_sensor_request.type,"reset")==0)
- {
- ack_str = ctrl_sts_ack_to_json();
- }
- //门禁释放
- else if(strcmp(_over_all_sensor_request.type,"release")==0)
- {
- ack_str = ctrl_sts_ack_to_json();
- }
- //打开空调开关
- else if(strcmp(_over_all_sensor_request.type,"open")==0)
- {
- ack_str = ctrl_sts_ack_to_json();
- }
- //关闭空调开关
- else if(strcmp(_over_all_sensor_request.type,"close")==0)
- {
- ack_str = ctrl_sts_ack_to_json();
- }
- #if(1)
- //历史信息
- else if(strcmp(_over_all_sensor_request.type,"listHisQuery")==0)
- {
- int rsize = 0 ;
- int g_sensor_id = 0 ;
- int g_sensor_type = -1;
- GlobalSensorManger* _globalSensorMangerSTemp;
- int page_num = 0;
- int page_size = 60;
- char start_time[32] = {0};
- char stop_time[32] = {0};
- INIT_LIST_HEAD(&_over_all_sensor_back_info.list);
- if (_over_all_sensor_request.sensorType)
- {
- g_sensor_type = atoi(_over_all_sensor_request.sensorType);
- }
- if ( _over_all_sensor_request.startTime == NULL || _over_all_sensor_request.endTime == NULL)
- {
- struct tm *t;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 23, 59, 59);
- log_d(stop_time);
- sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 0, 0, 0);
- log_d(start_time);
- }
- else
- {
- strncpy(start_time, _over_all_sensor_request.startTime , sizeof(start_time) );
- strncpy(stop_time, _over_all_sensor_request.endTime , sizeof(stop_time) );
- }
- // log_d("page_num:%d",page_num);
- // log_d("page_size:%d",page_size);
- lock_s_hold(LOCK_ID_SENSOR);
- // 查找管理表找到关联信息
- list_for_each_entry(_globalSensorMangerSTemp, &__globalDeviceManage._globalSensorManger.list, list)
- {
- // ID相等 记录信息
- if (_globalSensorMangerSTemp->sensor_type != g_sensor_type)
- {
- continue;
- }
- // 查询数据
- GlobalSensorInfo SensorInfoList;
- INIT_LIST_HEAD(&SensorInfoList.list);
- ret = dev_search_sensor_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- _globalSensorMangerSTemp->sensor_id,
- start_time,
- stop_time,
- page_num,
- page_size,
- &rsize,
- &SensorInfoList);
- if (ret != 0)
- {
- log_e("get sensor history error.");
- continue;
- }
- _OverSensorBackInfo *_overSensorBackInfoTemp = (_OverSensorBackInfo *)malloc(sizeof(_OverSensorBackInfo));
- if (_overSensorBackInfoTemp == NULL)
- log_e("_OverSensorBackInfo malloc error.");
- memset(_overSensorBackInfoTemp, 0, sizeof(_OverSensorBackInfo));
- // 基本信息
- _overSensorBackInfoTemp->sensorId = _globalSensorMangerSTemp->sensor_id;
- strcpy(_overSensorBackInfoTemp->sensorName,language_sensor_name[__globalDeviceManage._globalDevInfo.product.language][(int)_globalSensorMangerSTemp->sensor_type]);
-
- _overSensorBackInfoTemp->sensorType = _globalSensorMangerSTemp->sensor_type;
- strcpy(_overSensorBackInfoTemp->comSlogan, _globalSensorMangerSTemp->sensor_port_name);
- // 485独有
- if (_globalSensorMangerSTemp->sensor_interface_type == 1)
- {
- _overSensorBackInfoTemp->sensorAddr = _globalSensorMangerSTemp->sensor_addr;
- _overSensorBackInfoTemp->sensorBaud = _globalSensorMangerSTemp->sensor_baud;
- }
- _overSensorBackInfoTemp->connectionStatus = _globalSensorMangerSTemp->sensor_status;
- _overSensorBackInfoTemp->nodeNumber = _globalSensorMangerSTemp->sensor_node_number;
- // 组返回数据
- int _TempCount=0;
- GlobalSensorInfo* _globalSensorInfoTemp;
- list_for_each_entry(_globalSensorInfoTemp, &SensorInfoList.list, list)
- {
- if (_TempCount>=TOTAL_COUNT)
- {
- break;
- }
-
- // 判断sensor type 填充数据
- switch (_globalSensorMangerSTemp->sensor_type)
- {
- case SENSOR_TYPE_TEMPERATURE: // 温湿度传感器 2个值
- {
- _overSensorBackInfoTemp->temp_humi_sensor.ydata[0][_TempCount] = _globalSensorInfoTemp->val1;
- _overSensorBackInfoTemp->temp_humi_sensor.ydata[1][_TempCount] = _globalSensorInfoTemp->val2;
- strcpy(_overSensorBackInfoTemp->temp_humi_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
- }
- break;
- case SENSOR_TYPE_SMOKE: // 烟雾传感器 1个值
- {
- _overSensorBackInfoTemp->alarmStatus =! (bool)_globalSensorInfoTemp->val1; // 1/0 报警未报警
- }
- break;
- case SENSOR_TYPE_WATER: // 水浸传感器
- {
- _overSensorBackInfoTemp->waterImmersionState =! (bool)_globalSensorInfoTemp->val1; // 1/0 报警未报警
- }
- break;
- case SENSOR_TYPE_DOOR: // 门禁传感器 没有值
- {
- _overSensorBackInfoTemp->accessControlStatus =! (bool)_globalSensorInfoTemp->val1; // 1/0 报警未报警
- }
- break;
- case SENSOR_TYPE_GAS: // 气体传感器 1个值
- {
- _overSensorBackInfoTemp->gas_sensor.ydata[_TempCount] = _globalSensorInfoTemp->val1; // 气体浓度
- strcpy(_overSensorBackInfoTemp->gas_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
- }
- break;
- case SENSOR_TYPE_AIR_PRESS:
- {
- _overSensorBackInfoTemp->air_press_sensor.ydata[0][_TempCount] = _globalSensorInfoTemp->val1;
- _overSensorBackInfoTemp->air_press_sensor.ydata[1][_TempCount] = _globalSensorInfoTemp->val2;
- strcpy(_overSensorBackInfoTemp->air_press_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
- }
- break;
- case 6: // 空调遥控器 没有值
- {
- }
- break;
- case SENSOR_TYPE_DoubleT: // 温湿度传感器 2个值
- {
- _overSensorBackInfoTemp->temp_humi_sensor.ydata[0][_TempCount] = _globalSensorInfoTemp->val1;
- _overSensorBackInfoTemp->temp_humi_sensor.ydata[1][_TempCount] = _globalSensorInfoTemp->val2;
- strcpy(_overSensorBackInfoTemp->temp_humi_sensor.xAxis[_TempCount], _globalSensorInfoTemp->samp_time);
- }
- break;
- }
- _TempCount++;
- }
- // 入队
- list_add_tail(&_overSensorBackInfoTemp->list, &_over_all_sensor_back_info.list);
- // 释放资源
- GlobalSensorInfo *node;
- GlobalSensorInfo *next;
- list_for_each_entry_safe(node, next, &SensorInfoList.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- lock_s_release(LOCK_ID_SENSOR);
-
- if(strcmp(_over_all_sensor_request.sensorType,"0")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
- }
- else if(strcmp(_over_all_sensor_request.sensorType,"4")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
- }
- else if(strcmp(_over_all_sensor_request.sensorType,"5")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
- }
- else if(strcmp(_over_all_sensor_request.sensorType,"1")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
- }
- else if(strcmp(_over_all_sensor_request.sensorType,"2")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
- }
- else if(strcmp(_over_all_sensor_request.sensorType,"3")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
- }
- else if (strcmp(_over_all_sensor_request.sensorType, "6") == 0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_LIST);
- }
- else if(strcmp(_over_all_sensor_request.sensorType,"7")==0)
- {
- ack_str = over_sensor_history_info_to_json(&_over_all_sensor_back_info, SENSOR_TYPE_VALUE);
- }
- else ack_str = ctrl_sts_ack_to_json();
- // //参数转换json
- // ack_str = over_all_sensor_info_to_json(&_over_all_sensor_back_info);
- // 释放资源
- _OverSensorBackInfo *node;
- _OverSensorBackInfo *next;
- list_for_each_entry_safe(node, next, &_over_all_sensor_back_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- #endif
-
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /**
- * @brief 设置通道报警阈值
- *
- * 根据给定的通道ID,设置报警请求的通道阈值。
- *
- * @param _alarm_request_info 报警请求信息指针
- * @param nChID 通道ID
- *
- * @return 指向设置后的通道阈值字符串的指针
- */
- char* setThresholdToCh(_AlarmRequestInfo* _alarm_request_info,int nChID)
- {
- int ret = -1 ;
- char *ack_str = NULL;
- GlobalPowerManger *_globalPowerMangerSaveTemp;
- // int g_pwr_ch_id = atoi(_alarm_request_info->_pwr_info.productChId);
- int g_pwr_ch_id = nChID;
- if (g_pwr_ch_id == 0)
- {
- _globalPowerMangerSaveTemp = &__globalDeviceManage._globalPowerManger;
- }
- else
- {
- list_for_each_entry(_globalPowerMangerSaveTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- // ID相等 记录信息
- if (_globalPowerMangerSaveTemp->product_ch_id == nChID)
- {
- break;
- }
- }
- }
- // 更新参数,使能全部放在控制板上判断,表头不做判断,统一发1
- if (_alarm_request_info->_pwr_info.voltageUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold = atof(_alarm_request_info->_pwr_info.voltageUpperValue);
- }
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 1;
- /*if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold != 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 1;
- }
- else
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 0;
- }*/
- if (_alarm_request_info->_pwr_info.voltageDownValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold = atof(_alarm_request_info->_pwr_info.voltageDownValue);
- }
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 1;
- /*if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold != 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 1;
- }
- else
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 0;
- }*/
- if (_alarm_request_info->_pwr_info.currentUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold = atof(_alarm_request_info->_pwr_info.currentUpperValue);
- }
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 1;
- /*if (_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold != 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 1;
- }
- else
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 0;
- }*/
- if (_alarm_request_info->_pwr_info.powerUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold = atof(_alarm_request_info->_pwr_info.powerUpperValue);
- }
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 1;
- /*if (_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold != 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 1;
- }
- else
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 0;
- }*/
- if (_alarm_request_info->_pwr_info.alarmContentUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold = atof(_alarm_request_info->_pwr_info.alarmContentUpperValue);
- }
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1;
- /* if (_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold != 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1;
- }
- else
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 0;
- }*/
- //_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1 ;
- char Alarm_Ctrl_type[16];
- memset(Alarm_Ctrl_type, 0, sizeof(Alarm_Ctrl_type));
- sprintf(Alarm_Ctrl_type, "%d", ALARM_CTRL_CLOSE_CH);
- if (_alarm_request_info->_pwr_info.currentUpperAction && strcmp(_alarm_request_info->_pwr_info.currentUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info->_pwr_info.voltageUpperAction && strcmp(_alarm_request_info->_pwr_info.voltageUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info->_pwr_info.voltageDownAction && strcmp(_alarm_request_info->_pwr_info.voltageDownAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info->_pwr_info.powerUpperAction && strcmp(_alarm_request_info->_pwr_info.powerUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info->_pwr_info.alarmContentUpperAction && strcmp(_alarm_request_info->_pwr_info.alarmContentUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
- {
- GlobalTreeACManager *_pTreeACPowerMangerTemp = NULL;
- list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
- {
- _pTreeACPowerMangerTemp->global_over_manager = _globalPowerMangerSaveTemp->global_over_manager;
- ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerSaveTemp->product_ch_type,
- _pTreeACPowerMangerTemp->product_saddr,
- _pTreeACPowerMangerTemp->product_ch_addr,
- _globalPowerMangerSaveTemp);
- }
- }
- else
- {
- ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerSaveTemp->product_ch_type,
- _globalPowerMangerSaveTemp->product_saddr,
- _globalPowerMangerSaveTemp->product_ch_addr,
- _globalPowerMangerSaveTemp);
- }
- if (ret < 0)
- {
- log_e("power threshold update error.ret:%d,%s", ret, modbus_strerror(errno));
- /* code */
- }
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
- {
- // 写入数据库
- ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- _globalPowerMangerSaveTemp->product_ch_id,
- _globalPowerMangerSaveTemp);
- if (ret != 0)
- log_e("power threshold update error.");
- GlobalTreeACManager *_pTreeACPowerMangerTemp = NULL;
- list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
- {
- _pTreeACPowerMangerTemp->global_over_manager = _globalPowerMangerSaveTemp->global_over_manager;
- ret = dev_update_t_ac_power_manage_over_info(__globalDeviceManage.db, _pTreeACPowerMangerTemp);
- }
- }
- else
- {
- // 写入数据库
- ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- _globalPowerMangerSaveTemp->product_ch_id,
- _globalPowerMangerSaveTemp);
- if (ret != 0)
- log_e("power threshold update error.");
- }
- // 返回数据
- ack_str = ctrl_sts_ack_to_json();
- return ack_str;
- }
- /// @brief 告警保护
- /// @param stream
- static void alarmProtect(void *conn)
- {
- int ret = -1 ;
- cJSON *cjson;
- _AlarmRequestInfo _alarm_request_info;
- _AlarmAckInfo _alarm_ack_info; //填充数据
- char* ack_str = NULL ;
- int nAlarmNumber=0;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf=(char*)hm->body.buf;
- memset(&_alarm_request_info, 0, sizeof(_alarm_request_info));
- //转换json参数
- cjson = json_to_alarm_manage(rx_body_buf,&_alarm_request_info);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
- //查询json参数
- if(strcmp(_alarm_request_info.type,"listAllQuery")==0) //告警信息查询
- {
- int i = 0 ;
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- //查询数据库,最近的10个报警信息
- char start_time[32] = {0};
- char stop_time[32] = {0};
- if (_alarm_request_info.startTime == NULL || _alarm_request_info.endTime == NULL)
- {
- struct tm *t;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(stop_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 23, 59, 59);
- log_d(stop_time);
- sprintf(start_time, "%04d-%02d-%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, 0, 0, 0);
- log_d(start_time);
- _alarm_request_info.startTime = start_time;
- _alarm_request_info.endTime = stop_time;
- }
- ret = dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- -1,_alarm_request_info.startTime,_alarm_request_info.endTime,
- 1,10,
- &_alarm_ack_info,&nAlarmNumber);
- if(ret!=0) {
- log_e("get power alarm error.");
- }
- else {
- ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- }
- else if(strcmp(_alarm_request_info.type,"powerAlarmList")==0) //电源告警信息查询
- {
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- //查询数据库
- ret = dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_POWER,_alarm_request_info.startTime,_alarm_request_info.endTime,
- _alarm_request_info.pagenum,_alarm_request_info.pagesize,
- &_alarm_ack_info,&nAlarmNumber);
- if(ret!=0) {
- log_e("get power alarm error.");
- }
- else {
- ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- }
- else if(strcmp(_alarm_request_info.type,"sensorAlarmList")==0) //传感器告警信息查询
- {
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- //查询数据库
- ret = dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_SENSOR,_alarm_request_info.startTime,_alarm_request_info.endTime,
- _alarm_request_info.pagenum,_alarm_request_info.pagesize,
- &_alarm_ack_info,&nAlarmNumber);
- if(ret!=0) {
- log_e("get sensor alarm error.");
- }
- else {
- ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- }
- else if(strcmp(_alarm_request_info.type,"networAlarmkList")==0) //网络告警信息查询
- {
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- ret = dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_NETWORK,_alarm_request_info.startTime,_alarm_request_info.endTime,
- _alarm_request_info.pagenum,_alarm_request_info.pagesize,
- &_alarm_ack_info,&nAlarmNumber);
- if(ret!=0) {
- log_e("get net alarm error.");
- }
- else {
- //转换参数
- ack_str = alarm_info_to_json(&_alarm_ack_info,nAlarmNumber);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- }
- else if(strcmp(_alarm_request_info.type,"powerThresholdList")==0)//电源阈值查询
- {
- //查询每个通道阈值
- ack_str = alarm_power_threshold_to_json(&__globalDeviceManage._globalPowerManger);
- }
- else if(strcmp(_alarm_request_info.type,"save")==0)//电源阈值保存
- {
- if (!_alarm_request_info.user_token)
- {
- return;
- }
-
- //拿到参数保存
- //GlobalPowerManger* _globalPowerMangerSaveTemp ;
- // int g_pwr_ch_id = atoi(_alarm_request_info._pwr_info.productChId);
- int g_pwr_ch_id = _alarm_request_info._pwr_info.productChId;
- ack_str=setThresholdToCh(&_alarm_request_info,g_pwr_ch_id);
- char log_buff[256];
- if (0==g_pwr_ch_id)
- {
- sprintf(log_buff, "Update power threshold ALL_CH!");
- }
- else sprintf(log_buff, "Update power threshold CH%d!",g_pwr_ch_id);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
- /*
- if (g_pwr_ch_id ==0)
- {
- _globalPowerMangerSaveTemp=&__globalDeviceManage._globalPowerManger;
- }
- else
- {
- list_for_each_entry(_globalPowerMangerSaveTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- // ID相等 记录信息
- if (_globalPowerMangerSaveTemp->product_ch_id == g_pwr_ch_id)
- {
- break;
- }
- }
- }
- //更新参数
- if (_alarm_request_info._pwr_info.voltageUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold =atof(_alarm_request_info._pwr_info.voltageUpperValue);
- }
- if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 1 ;
- }else{_globalPowerMangerSaveTemp->global_over_manager.product_vol_upper_enable = 0;}
- if (_alarm_request_info._pwr_info.voltageDownValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold =atof(_alarm_request_info._pwr_info.voltageDownValue);
- }
- if (_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 1 ;
- }else{_globalPowerMangerSaveTemp->global_over_manager.product_vol_lower_enable = 0 ;}
- if (_alarm_request_info._pwr_info.currentUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold = atof(_alarm_request_info._pwr_info.currentUpperValue);
- }
- if (_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 1 ;
- }else{_globalPowerMangerSaveTemp->global_over_manager.product_cur_upper_enable = 0 ;}
- if (_alarm_request_info._pwr_info.powerUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold = atof(_alarm_request_info._pwr_info.powerUpperValue);
- }
- if (_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 1 ;
- }else{_globalPowerMangerSaveTemp->global_over_manager.product_pwr_upper_enable = 0 ;}
- if (_alarm_request_info._pwr_info.alarmContentUpperValue)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold = atof(_alarm_request_info._pwr_info.alarmContentUpperValue);
- }
- if (_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_threshold!=0){_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1 ;
- }else{_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 0 ;}
- //_globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_upper_enable = 1 ;
- char Alarm_Ctrl_type[16];
- memset(Alarm_Ctrl_type, 0, sizeof(Alarm_Ctrl_type));
- sprintf(Alarm_Ctrl_type, "%d", ALARM_CTRL_CLOSE_CH);
- if (_alarm_request_info._pwr_info.currentUpperAction && strcmp(_alarm_request_info._pwr_info.currentUpperAction,Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_cur_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._pwr_info.voltageUpperAction && strcmp(_alarm_request_info._pwr_info.voltageUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._pwr_info.voltageDownAction && strcmp(_alarm_request_info._pwr_info.voltageDownAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_vol_over_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._pwr_info.powerUpperAction && strcmp(_alarm_request_info._pwr_info.powerUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_pwr_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._pwr_info.alarmContentUpperAction && strcmp(_alarm_request_info._pwr_info.alarmContentUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- }
- else
- _globalPowerMangerSaveTemp->global_over_manager.product_pwrcon_over_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
- {
-
- GlobalTreeACManager* _pTreeACPowerMangerTemp = NULL ;
- list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
- {
- memcpy(_pTreeACPowerMangerTemp->global_over_manager,_globalPowerMangerSaveTemp->global_over_manager,sizeof(GlobalOverManager));
- ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerSaveTemp->product_ch_type,
- _pTreeACPowerMangerTemp->product_saddr,
- _pTreeACPowerMangerTemp->product_ch_addr,
- _globalPowerMangerSaveTemp);
- }
- }
- else
- {
- ret = g_switch_set_all_single_threshold(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerSaveTemp->product_ch_type,
- _globalPowerMangerSaveTemp->product_saddr,
- _globalPowerMangerSaveTemp->product_ch_addr,
- _globalPowerMangerSaveTemp);
- }
- if (ret<0)
- {
- log_e("power threshold update error.ret:%d,%s",ret,modbus_strerror(errno));
- }
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_Tree || __globalDeviceManage._globalDevInfo.product.pwr_type == SmartPDU_Tree_AC_One)
- {
- // 写入数据库
- ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- _globalPowerMangerSaveTemp->product_ch_id,
- _globalPowerMangerSaveTemp);
- if (ret != 0)
- log_e("power threshold update error.");
- GlobalTreeACManager *_pTreeACPowerMangerTemp = NULL;
- list_for_each_entry(_pTreeACPowerMangerTemp, &_globalPowerMangerSaveTemp->list_Tree_AC, list_Tree_AC)
- {
- memcpy(_pTreeACPowerMangerTemp->global_over_manager, _globalPowerMangerSaveTemp->global_over_manager, sizeof(GlobalOverManager));
- ret = dev_update_t_ac_power_manage_over_info(__globalDeviceManage.db,_pTreeACPowerMangerTemp);
- }
- }
- else
- {
- // 写入数据库
- ret = dev_update_power_manage_over_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- _globalPowerMangerSaveTemp->product_ch_id,
- _globalPowerMangerSaveTemp);
- if (ret != 0)
- log_e("power threshold update error.");
- }
- //返回数据
- ack_str = ctrl_sts_ack_to_json();*/
- }
- else if (strcmp(_alarm_request_info.type, "batchSave") == 0) // 批量设置
- {
- if (!_alarm_request_info.user_token)
- {
- return;
- }
- char *pCHlist;
- char *pCHListSave;
- pCHlist = strtok_r(_alarm_request_info.strProductChIds, ",", &pCHListSave);
- // 其他版本功能
- while (pCHlist != NULL)
- {
- int g_pwr_ch_id = atoi(pCHlist);
- ack_str = setThresholdToCh(&_alarm_request_info, g_pwr_ch_id);
- pCHlist = strtok_r(NULL, ",", &pCHListSave);
- //usleep(30000);//延时30ms,等待控制板串口写完
- }
- char log_buff[256];
- sprintf(log_buff, "Batch setting power threshold!");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
- }
- else if(strcmp(_alarm_request_info.type,"sensorThresholdList")==0)//传感器阈值查询
- {
- ack_str = alarm_sensor_threshold_to_json(&__globalDeviceManage._globalSensorManger);
- }
- else if(strcmp(_alarm_request_info.type,"update")==0)//传感器阈值保存
- {
- GlobalSensorManger* _globalSensorMangerSaveTemp;
- int g_sensor_id = _alarm_request_info._sensor_info.sensorId;
- list_for_each_entry(_globalSensorMangerSaveTemp, &__globalDeviceManage._globalSensorManger.list, list)
- {
- //ID相等 记录信息
- if(_globalSensorMangerSaveTemp->sensor_id== g_sensor_id)
- {
- break;
- }
- }
- char Alarm_Ctrl_type[16];
- memset(Alarm_Ctrl_type, 0, sizeof(Alarm_Ctrl_type));
- sprintf(Alarm_Ctrl_type, "%d", ALARM_CTRL_CLOSE_CH);
- switch (_globalSensorMangerSaveTemp->sensor_type)
- {
- case SENSOR_TYPE_TEMPERATURE:
- case SENSOR_TYPE_DoubleT:
- {
- char strName1[100]={0};
- char strName2[100]={0};
- sprintf(strName1, "%s%d", language_temperature[__globalDeviceManage._globalDevInfo.product.language], 1);
- sprintf(strName2, "%s%d", language_temperature[__globalDeviceManage._globalDevInfo.product.language], 2);
- if (strcmp(_alarm_request_info._sensor_info.thresholdType, language_temperature[__globalDeviceManage._globalDevInfo.product.language]) == 0||strcmp(_alarm_request_info._sensor_info.thresholdType, strName1) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold = atof(_alarm_request_info._sensor_info.thresholdUpperValue);
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold = atof(_alarm_request_info._sensor_info.thresholdDownValue);
- if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold>=-0.05 )
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 0;
- }
- else _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 1;
-
- if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold>=-0.05 )
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled = 0;
- }
- else _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled =1;
-
- if (_alarm_request_info._sensor_info.thresholdUpperAction && strcmp(_alarm_request_info._sensor_info.thresholdUpperAction,Alarm_Ctrl_type) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- if(_alarm_request_info._sensor_info.thresholdUpperChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdUpperChanel);
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._sensor_info.thresholdDownAction && strcmp(_alarm_request_info._sensor_info.thresholdDownAction, Alarm_Ctrl_type) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- if(_alarm_request_info._sensor_info.thresholdDownChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdDownChanel);
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- }
- 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)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold = atof(_alarm_request_info._sensor_info.thresholdUpperValue);
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold = atof(_alarm_request_info._sensor_info.thresholdDownValue);
- if (_globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold>=-0.05 )
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_enabled = 0;
- }
- else _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_enabled = 1;
- if (_globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold<=0.05 &&_globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold>=-0.05 )
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_enabled = 0;
- }
- else _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_enabled = 1;
- if (_alarm_request_info._sensor_info.thresholdUpperAction && strcmp(_alarm_request_info._sensor_info.thresholdUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- if(_alarm_request_info._sensor_info.thresholdUpperChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdUpperChanel);
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._sensor_info.thresholdDownAction && strcmp(_alarm_request_info._sensor_info.thresholdDownAction, Alarm_Ctrl_type) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- if(_alarm_request_info._sensor_info.thresholdDownChanel)strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold_ctrl_para,_alarm_request_info._sensor_info.thresholdDownChanel);
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val2_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- }
- }
- break;
- case SENSOR_TYPE_SMOKE:
- case SENSOR_TYPE_WATER:
- case SENSOR_TYPE_DOOR:
- break;
- case SENSOR_TYPE_GAS:
- {
- char strName1[100] = {0};
- char strName2[100] = {0};
- sprintf(strName1, "%s%d", language_atmosphere_gas[__globalDeviceManage._globalDevInfo.product.language], 1);
- sprintf(strName2, "%s%d", language_atmosphere_gas[__globalDeviceManage._globalDevInfo.product.language], 2);
- 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)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold = atof(_alarm_request_info._sensor_info.thresholdUpperValue);
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold = atof(_alarm_request_info._sensor_info.thresholdDownValue);
- if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold <= 0.05 && _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold >= -0.05)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 0;
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_enabled = 1;
- if (_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold <= 0.05 && _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold >= -0.05)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled = 0;
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_enabled = 1;
- if (_alarm_request_info._sensor_info.thresholdUpperAction && strcmp(_alarm_request_info._sensor_info.thresholdUpperAction, Alarm_Ctrl_type) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- if (_alarm_request_info._sensor_info.thresholdUpperChanel)
- strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl_para, _alarm_request_info._sensor_info.thresholdUpperChanel);
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_upper_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- if (_alarm_request_info._sensor_info.thresholdDownAction && strcmp(_alarm_request_info._sensor_info.thresholdDownAction, Alarm_Ctrl_type) == 0)
- {
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_CLOSE_CH;
- if (_alarm_request_info._sensor_info.thresholdDownChanel)
- strcpy(_globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl_para, _alarm_request_info._sensor_info.thresholdDownChanel);
- }
- else
- _globalSensorMangerSaveTemp->over_manager.sensor_val1_lower_threshold_ctrl = ALARM_CTRL_ALARM_ONLY;
- }
- }
- break;
- case SENSOR_TYPE_AIR_PRESS:
- default:
- break;
- }
-
- //更新参数
- ret = dev_update_sensor_over_manage(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- g_sensor_id,
- _globalSensorMangerSaveTemp);
- if(ret!=0)
- log_e("power threshold update error.");
- char log_buff[256];
- sprintf(log_buff, "Update sensor threshold ID%d!",g_sensor_id);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
- //拿到参数保存
- ack_str = ctrl_sts_ack_to_json();
- }
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 查询日志
- /// @param stream
- static void logInformation(void *conn)
- {
- int ret = -1 ;
- char* ack_str = NULL ;
- int nAlarmNum = 0;
- cJSON *cjson;
- _LogInformationRequestInfo __logInformationRequestInfo;
- _AlarmAckInfo _alarm_ack_info; //填充数据
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf=(char*)hm->body.buf;
-
- memset(&__logInformationRequestInfo,0,sizeof(_LogInformationRequestInfo));
- cjson = json_to_loginformation_manage(rx_body_buf,&__logInformationRequestInfo);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
- //查询json参数
- if(strcmp(__logInformationRequestInfo.type,"listJgrzQuery")==0) //告警日志
- {
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- if(__logInformationRequestInfo.startTime==NULL || __logInformationRequestInfo.endtime==NULL)
- {
- struct tm* t ;
- struct timeval tv;
- char start_time[32] = {0};
- char stop_time[32] = {0};
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(stop_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,23,59,59);
- log_d(stop_time);
- sprintf(start_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,0,0,0);
- log_d(start_time);
-
- dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- 0, //注意注意 此处需要查询的参数是0 1 2
- start_time,//启动时间
- stop_time, //结束时间
- __logInformationRequestInfo.pagenum, //页
- __logInformationRequestInfo.pagesize, //条
- &_alarm_ack_info,&nAlarmNum);
- ack_str = alarm_type_info_to_json(&_alarm_ack_info,nAlarmNum);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- else
- {
- dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- 0, //注意注意 此处需要查询的参数是0 1 2
- __logInformationRequestInfo.startTime,//开始时间
- __logInformationRequestInfo.endtime, //结束时间
- __logInformationRequestInfo.pagenum, //页
- __logInformationRequestInfo.pagesize, //条
- &_alarm_ack_info,&nAlarmNum);
- ack_str = alarm_type_info_to_json(&_alarm_ack_info,nAlarmNum);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- //转换参数
- //ack_str = alarm_type_info_to_json(&_alarm_ack_info);
- }
-
- else if(strcmp(__logInformationRequestInfo.type,"listCzrzQuery")==0) //操作日志
- {
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- //默认查当天日志
- if(__logInformationRequestInfo.startTime==NULL || __logInformationRequestInfo.endtime==NULL)
- {
- struct tm* t ;
- struct timeval tv;
- char start_time[32] = {0};
- char stop_time[32] = {0};
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(stop_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,23,59,59);
- log_d(stop_time);
- sprintf(start_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,0,0,0);
- log_d(start_time);
- dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_OPRATION, // alarm_type, 操作日志
- start_time,
- stop_time,
- __logInformationRequestInfo.pagenum,
- __logInformationRequestInfo.pagesize,
- &_alarm_ack_info, &nAlarmNum);
- }
- else
- {
- dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_OPRATION,// alarm_type, 操作日志
- __logInformationRequestInfo.startTime,
- __logInformationRequestInfo.endtime,
- __logInformationRequestInfo.pagenum,
- __logInformationRequestInfo.pagesize,
- &_alarm_ack_info,&nAlarmNum);
- }
- //转换参数
- ack_str = alarm_ctrl_type_info_to_json(&_alarm_ack_info,nAlarmNum);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- else if(strcmp(__logInformationRequestInfo.type,"listYxrzQuery")==0) //运行日志
- {
- //初始化表
- INIT_LIST_HEAD(&_alarm_ack_info.list);
- //默认查当天日志
- if(__logInformationRequestInfo.startTime==NULL || __logInformationRequestInfo.endtime==NULL)
- {
- struct tm *t;
- struct timeval tv;
- char start_time[32] = {0};
- char stop_time[32] = {0};
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(stop_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,23,59,59);
- log_d(stop_time);
- sprintf(start_time,"%04d-%02d-%02d %02d:%02d:%02d",t->tm_year+1900,t->tm_mon+1,t->tm_mday,0,0,0);
- log_d(start_time);
- dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_RUNNING, // alarm_type, 运行日志
- start_time,
- stop_time,
- __logInformationRequestInfo.pagenum,
- __logInformationRequestInfo.pagesize,
- &_alarm_ack_info, &nAlarmNum);
- }
- else
- {
- dev_search_alarm_info(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,
- ALARM_TYPE_RUNNING, // alarm_type, 运行日志
- __logInformationRequestInfo.startTime,
- __logInformationRequestInfo.endtime,
- __logInformationRequestInfo.pagenum,
- __logInformationRequestInfo.pagesize,
- &_alarm_ack_info, &nAlarmNum);
- }
- // 转换参数
- ack_str = alarm_type_info_to_json(&_alarm_ack_info, nAlarmNum);
- //释放资源
- _AlarmAckInfo* node;
- _AlarmAckInfo* next;
- list_for_each_entry_safe(node, next, &_alarm_ack_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- else if(strcmp(__logInformationRequestInfo.type,"exportCzrz")==0) //导出操作日志
- {
- }
- else if(strcmp(__logInformationRequestInfo.type,"exportYxrz")==0) //导出运行日志
- {
- }
- else if(strcmp(__logInformationRequestInfo.type,"exportJgrz")==0) //导出告警日志
- {
- //
- }
-
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 通信网口设置
- /// @param stream
- static void communicationManagement(void *conn)
- {
- int r = -1 ;
- char* ack_str = NULL ;
- cJSON *cjson;
- _CommunicationManageRequestInfo _communicationManageRequestInfo;
- _CommunicationManageRequestInfo reqInfo;
- GlobalDeviceInfo *devInfo=&__globalDeviceManage._globalDevInfo;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf=(char*)hm->body.buf;
- //转换json参数
- cjson = json_to_communication_manage(rx_body_buf,&reqInfo);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
- //查询网络参数
- if(strcmp(reqInfo.type,"listWkIPV4Query")==0 || strcmp(reqInfo.type,"listWkIPV6Query")==0)//缃戠粶鍙傛暟鏌ヨ
- {
- NetworkInfo_t info={0};
- int r,ipv=(strstr(reqInfo.type, "listWkIPV4Query")?IP_V4:IP_V6);
- r = sys_get_net(&info, ipv);
- if(r==0) {
- ack_str = communication_net_ack_to_json(&info);
- }
- }
- else if(strcmp(reqInfo.type,"WkIPV4Save")==0 || strcmp(reqInfo.type,"WkIPV6Save")==0 ) //淇濆瓨鏁版嵁
- {
- NetworkInfo_t* info = &devInfo->network;
- _NET_CommunicationRequestInfo *info2=&reqInfo._net_info;
- int ipv=(strstr(reqInfo.type, "WkIPV4Save")?IP_V4:IP_V6);
-
- int mode = atoi(info2->mode);
- if(ipv==IP_V4 || (ipv==IP_V6 && mode==IP_STATIC)) {
- info->mode = mode;
-
- if(info2->ipaddr) strcpy(info->ip_address, info2->ipaddr);
- if(info2->maskaddr) strcpy(info->mask, info2->maskaddr);
- if(info2->gateway) strcpy(info->gateway, info2->gateway);
- if(info2->dns1) strcpy(info->dns1, info2->dns1);
- if(info2->dns2) strcpy(info->dns2, info2->dns2);
- r = sys_set_net(info, ipv);
- //print_net("set", info);
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- //查询设置RS485参数
- else if(strcmp(reqInfo.type,"listRsQuery")==0)//modbus鍙傛暟鏌ヨ
- {
- ModbusInfo_t* _modbusInfo_temp = &devInfo->cascade;
- ack_str = communication_rs_ack_to_json(_modbusInfo_temp);
- }
- else if(strcmp(reqInfo.type,"RsSave")==0)//淇濆瓨modbus鏁版嵁
- {
- ModbusInfo_t* info = &devInfo->cascade;
- bool isStrMaster=strcmp(reqInfo._rs_info.mode,language_alarm_RS485_master[__globalDeviceManage._globalDevInfo.product.language])==0;
- bool isStrSlave=strcmp(reqInfo._rs_info.mode,language_alarm_RS485_slave[__globalDeviceManage._globalDevInfo.product.language])==0;
- if (isStrMaster||isStrSlave)
- {
- if (isStrMaster)
- {
- info->mode = MODBUS_MASTER;
- }
- else
- {
- info->mode =MODBUS_SLAVE;
- }
- }
- else
- {
- info->mode = atoi(reqInfo._rs_info.mode);
- }
- info->baudrate = atoi(reqInfo._rs_info.baud);
- info->addr = atoi(reqInfo._rs_info.addr);
-
- ///////////////////////////////////////////////////////////////////////////////
- r = cascade_set_modbus(info);
- if(r==0) {
- paras_save();
- }
- char buff[256];
- if (!info->mode)
- {
- sprintf(buff, "$%s$|$%s$|BD:%d", language_RS485_install[0],language_alarm_RS485_master[0],info->baudrate);
- }
- else
- {
- sprintf(buff, "$%s$|$%s$|addr:%d BD:%d",language_RS485_install[0],language_alarm_RS485_slave[0],info->addr,info->baudrate);
- }
- dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalPowerManger.product_id,ALARM_TYPE_OPRATION,buff);
- ack_str = ctrl_sts_ack_to_json();
- }
-
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 服务管理
- /// @param stream
- static void serviceManagement(void *conn)
- {
- int ret = -1 ;
- cJSON *cjson;
- _ServiceManageRequestInfo _serviceManageRequestInfo;
- char* ack_str = NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf = (char*)hm->body.buf;
- GlobalDeviceManager *gdm=&__globalDeviceManage;
- //转换json参数
- memset(&_serviceManageRequestInfo, 0, sizeof(_serviceManageRequestInfo));
- cjson = json_to_service_manage(rx_body_buf,&_serviceManageRequestInfo);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
-
- if(strcmp(_serviceManageRequestInfo.type,"listSmtpQuery")==0)
- {
- ack_str = service_mail_ack_to_json(&gdm->mailInfo);
- }
- else if(strcmp(_serviceManageRequestInfo.type,"SmtpSave")==0)
- {
- mail_info_t mailInfo=gdm->mailInfo;
- mail_send_t *send=&mailInfo.send;
- mail_recv_t *recv=mailInfo.recv;
- _MAIL_ServerRequestInfo *info=&_serviceManageRequestInfo._smtp_info;
- if(info->mode) {
- int save_flag=0;
- send->mode = atoi(info->mode);
- if(send->mode) {
- if(info->account && info->password && info->server && info->port && info->auth) {
- if(strcmp(send->account, info->account)) {
- strcpy(send->account, info->account); save_flag = 1;
- }
- if(strcmp(send->password, info->password)) {
- strcpy(send->password, info->password); save_flag = 1;
- }
- if(strcmp(send->server, info->server)) {
- strcpy(send->server, info->server);;
- }
- if(strcmp(send->port, info->port)) {
- strcpy(send->port, info->port);;
- }
- if(strcmp(send->auth, info->auth)) {
- strcpy(send->auth, info->auth);;
- }
- }
- }
- else {
- memset(send, 0, sizeof(mail_send_t));;
- }
-
- if(memcmp(&mailInfo.send, &gdm->mailInfo.send, sizeof(mail_send_t))) {
- dev_mail_update_send(gdm->db, send);
- gdm->mailInfo.send = mailInfo.send;
- }
- for(int i=0; i<MAIL_RECV_MAX; i++) {
- recv[i].id = i;
- if(info->recvAccount[i]) {
- recv[i].use = 1;
- strcpy(recv[i].account, info->recvAccount[i]);
- }
- else{
- recv[i].use = 0;
- strcpy(recv[i].account,"");
- }
- }
- if(memcmp(mailInfo.recv, gdm->mailInfo.recv, sizeof(mail_recv_t)*MAIL_RECV_MAX)) {
- dev_mail_update_recv(gdm->db, recv);
- memcpy(gdm->mailInfo.recv, mailInfo.recv, sizeof(mail_recv_t)*MAIL_RECV_MAX);
- }
- }
- }
- #ifdef USE_MQTT
- else if(strcmp(_serviceManageRequestInfo.type,"listMqttQuery")==0)
- {
- ack_str = service_mqtt_ack_to_json(&gdm->mqttInfo);
- }
- else if(strcmp(_serviceManageRequestInfo.type,"mqttSave")==0)
- {
- int save_flag=0;
- _MQTT_ServerRequestInfo *req=&_serviceManageRequestInfo._mqtt_info;
- mqtt_server_t *ser=&gdm->mqttInfo.ser[req->idx];
- if(req->idx |= ser->id) { ser->id = req->idx; save_flag=1; }
- if(req->name) { strcpy(ser->name, req->name); save_flag=1; }
- if(req->server) { strcpy(ser->server, req->server); save_flag=1; }
- if(req->port) { strcpy(ser->port, req->port); save_flag=1; }
- if(req->user) { strcpy(ser->user, req->user); save_flag=1; }
- if(req->password) { strcpy(ser->password, req->password); save_flag=1; }
- if(req->cid) { strcpy(ser->cid, req->cid); save_flag=1; }
- if(save_flag) {
- dev_mqtt_update_ser(gdm->db, ser);
- }
- }
- else if(strcmp(_serviceManageRequestInfo.type,"mqttOpen")==0 ||
- strcmp(_serviceManageRequestInfo.type,"mqttClose")==0)
- {
- int save_flag=0;
- _MQTT_ServerRequestInfo *req=&_serviceManageRequestInfo._mqtt_info;
- mqtt_server_t *ser=&gdm->mqttInfo.ser[req->idx];
- if(req->mode!=ser->mode) {
- ser->mode = req->mode;
- dev_mqtt_update_ser(gdm->db, ser);
- }
- }
- #endif
- else if(strcmp(_serviceManageRequestInfo.type,"listSnmpQuery")==0)
- {
- // 查询SNMP信息
- if (0 == dev_get_snmp_info(__globalDeviceManage.db, &_globalSNMPManager))
- {
- ack_str = service_snmp_ack_to_json(&_globalSNMPManager);
- }
- }
- else if(strcmp(_serviceManageRequestInfo.type,"SnmpSave")==0)
- {
- int len=0;
- _globalSNMPManager.mode = atoi(_serviceManageRequestInfo._snmp_info.mode);
- if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V1")==0)
- {
- _globalSNMPManager.version=0;
- }
- else if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V2c")==0)
- {
- _globalSNMPManager.version=1;
- }
- else if (strcmp(_serviceManageRequestInfo._snmp_info.version,"SNMP V3")==0)
- {
- _globalSNMPManager.version=2;
- }
- else
- {
- _globalSNMPManager.version=-1;
- }
- strcpy(_globalSNMPManager.public, _serviceManageRequestInfo._snmp_info.public);
- _globalSNMPManager.trapmode = atoi(_serviceManageRequestInfo._snmp_info.trapmode);
- if (_serviceManageRequestInfo._snmp_info.nms_IP) strcpy(_globalSNMPManager.nmsIP, _serviceManageRequestInfo._snmp_info.nms_IP);
-
-
- crypt_t *pc=&_globalSNMPManager.crypt;
- _SNMP_ServerRequestInfo *info=&_serviceManageRequestInfo._snmp_info;
- if (_globalSNMPManager.version == 2)
- {
- if (info->user)
- {
- len = strlen(info->user);
- if (len < 8 || len > 32)
- {
- log_e("snmp user length %d is wrong! correct is [8~32]", len);
- return;
- }
- strcpy(pc->user, info->user);
- if (info->auth_alg)
- {
- strcpy(pc->auth.alg, info->auth_alg);
- if (strlen(info->auth_alg) > 1 && info->auth_passwd)
- {
- len = strlen(info->auth_passwd);
- if (len < 8 || len > 32)
- {
- log_e("snmp auth passwd length %d is wrong! correct is [8~32]", len);
- return;
- }
- strcpy(pc->auth.passwd, info->auth_passwd);
- }
- }
- if (info->priv_alg)
- {
- strcpy(pc->priv.alg, info->priv_alg);
- if (strlen(info->priv_alg) > 1 && info->priv_passwd)
- {
- len = strlen(info->priv_passwd);
- if (len < 8 || len > 32)
- {
- log_e("snmp priv passwd length %d is wrong! correct is [8~32]", len);
- return;
- }
- strcpy(pc->priv.passwd, info->priv_passwd);
- }
- }
- }
- }
- _globalSNMPManager.product_id = __globalDeviceManage._globalDevInfo.product.id;
- if (dev_update_snmp_info(__globalDeviceManage.db,_globalSNMPManager.product_id,&_globalSNMPManager) != 0)
- {
- log_e("update snmp info err.");
- }
- else {
- extern int snmp_config(SNMPManager *m, int quit);
- snmp_config(&_globalSNMPManager, 1);
- }
- }
- //查询snmp_trap信息
- else if(strcmp(_serviceManageRequestInfo.type,"listStQuery")==0)
- {
- }
- else if(strcmp(_serviceManageRequestInfo.type,"StSave")==0)
- {
- }
- //查询ntp信息
- else if(strcmp(_serviceManageRequestInfo.type,"listNtpQuery")==0)
- {
- // 查询NTP信息
- if (0 == dev_get_ntp_info(__globalDeviceManage.db,&_globalNTPManager))
- {
- ack_str = service_ntp_ack_to_json(&_globalNTPManager);
- }
- }
- else if(strcmp(_serviceManageRequestInfo.type,"NtpSave")==0)
- {
- _globalNTPManager.mode = atoi(_serviceManageRequestInfo._ntp_info.mode);
- strcpy(_globalNTPManager.ntpaddress, _serviceManageRequestInfo._ntp_info.ntpaddress);
- strcpy(_globalNTPManager.ntpName, _serviceManageRequestInfo._ntp_info.ntpName);
- _globalNTPManager.port = atoi(_serviceManageRequestInfo._ntp_info.port);
- _globalNTPManager.product_id = __globalDeviceManage._globalDevInfo.product.id;
- strcpy(_globalNTPManager.time, _serviceManageRequestInfo._ntp_info.time);
- const char* key = "GMT/UTC";
- char* p = strstr(_serviceManageRequestInfo._ntp_info.tzone, key);
- if (p == NULL)
- {
- log_e("time zone not contain GMT/UTC");
- return;
- }
- p += strlen(key);
-
- int value = atoi(p);
- if (value > 12 || value < -12)
- {
- char buf[128];
- memset(buf, 0, sizeof(buf));
- sprintf(buf, "product id = %d timezone: %d, timezone err.", _globalNTPManager.product_id, value);
- log_e(buf);
- return;
- }
- _globalNTPManager.tzone = value;
- value = atoi(_serviceManageRequestInfo._ntp_info.weekday);
- if (value > 12 || value < -11)
- {
- char buf[128];
- memset(buf, 0, sizeof(buf));
- sprintf(buf, "product id = %d weekday err.", _globalNTPManager.product_id);
- log_e(buf);
- return;
- }
- _globalNTPManager.weekday = value;
- if (dev_update_ntp_info(__globalDeviceManage.db,_globalNTPManager.product_id,&_globalNTPManager) != 0)
- {
- log_e("update ntp info err.");
- }
- if (_globalNTPManager.mode == 0)
- {
- char tempstr[10];
- memset(tempstr, 0, sizeof(tempstr));
- p = strstr(_globalNTPManager.time, "-");
- if (p == NULL)
- {
- log_e("find year of local time failed");
- return;
- }
- memset(tempstr, 0, sizeof(tempstr));
- memcpy(tempstr, _globalNTPManager.time, p - _globalNTPManager.time);
- struct tm tm_new;
- tm_new.tm_year = atoi(tempstr);
- char* p1 = strstr(&p[1], "-");
- if (p1 == NULL)
- {
- log_e("find month of local time failed");
- return;
- }
- int nSize= p1 - p - 1;
- memset(tempstr, 0, sizeof(tempstr));
- memcpy(tempstr, &p[1],nSize);
- tm_new.tm_mon = atoi(tempstr);
- p = p1;
- p1 = strstr(&p[2], " ");//个位数第一位有可能是空格所以需要找空格后一个
- if (p1 == NULL)
- {
- log_e("find mday of local time failed");
- return;
- }
- memset(tempstr, 0, sizeof(tempstr));
- memcpy(tempstr, &p[1], p1 - p - 1);
- tm_new.tm_mday = atoi(tempstr);
- p = p1;
- p1 = strstr(&p[0], " ");
- if (p1 == NULL)
- {
- log_e("find split of date and time failed");
- return;
- }
- p = &p1[0];
- p1 = strstr(p, ":");
- if (p1 == NULL)
- {
- log_e("find hour of local time failed");
- return;
- }
- memset(tempstr, 0, sizeof(tempstr));
- memcpy(tempstr, &p[1], p1 - p);
- tm_new.tm_hour = atoi(tempstr);
- p = &p1[1];
- p1 = strstr(p, ":");
- if (p1 == NULL)
- {
- log_e("find minute of local time failed");
- return;
- }
- memset(tempstr, 0, sizeof(tempstr));
- memcpy(tempstr, &p[0], p1 - p);
- tm_new.tm_min = atoi(tempstr);
- p = &p1[1];
- if (p == NULL)
- {
- log_e("find second of local time failed");
- return;
- }
- memset(tempstr, 0, sizeof(tempstr));
- memcpy(tempstr, &p[0], 2);
- tm_new.tm_sec = atoi(tempstr);
- set_time_zone(&tm_new, _globalNTPManager.tzone);
- }
- else if (_globalNTPManager.mode == 1)
- {
- set_zone(_globalNTPManager.tzone);
- sleep(1);
- extern int ntp_config(NTPManager *nm);
- ntp_config(&_globalNTPManager);
- }
- char temp[100];
- if (0==_globalNTPManager.mode)
- {
- //sprintf(temp,"$设置$|$时间$|%s", _globalNTPManager.time);
- sprintf(temp,"Set Date & Time %s", _globalNTPManager.time);
- }
- else{
- // sprintf(temp,"$设置$|$时间$|NTP %s", _globalNTPManager.ntpaddress);
- sprintf(temp,"Set Date & Time sync from NTP %s", _globalNTPManager.ntpaddress);
- }
- dev_insert_alarm_ctrl(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- if (_serviceManageRequestInfo._ntp_info.mode) free(_serviceManageRequestInfo._ntp_info.mode);
- if (_serviceManageRequestInfo._ntp_info.port) free(_serviceManageRequestInfo._ntp_info.port);
- if (_serviceManageRequestInfo._ntp_info.tzone) free(_serviceManageRequestInfo._ntp_info.tzone);
- if (_serviceManageRequestInfo._ntp_info.weekday) free(_serviceManageRequestInfo._ntp_info.weekday);
- }
- // 定时设置
- else if (strcmp(_serviceManageRequestInfo.type, "listDsQuery") == 0)//||strcmp(_serviceManageRequestInfo.type, "listRlQuery") == 0)
- {
- // 查询通道信息
- /*
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp->product_ch_id == chn_id)
- break;
- }
- */
- // 返回定时信息
- lock_s_hold(LOCK_ID_POWER_TIMER);
- ack_str = service_ds_all_ack_to_json(_serviceManageRequestInfo._ds_info.channelDate,
- _serviceManageRequestInfo._ds_info.productChName,
- _serviceManageRequestInfo._ds_info.productChId,
- &__globalDeviceManage._globalPowerManger);
- lock_s_release(LOCK_ID_POWER_TIMER);
- }
- else if (strcmp(_serviceManageRequestInfo.type, "dsSave") == 0)
- {
- lock_s_hold(LOCK_ID_POWER_TIMER);
- int chn_id = -1;
- for (int i = 0; i < 64; i++)
- {
- chn_id = _serviceManageRequestInfo._ds_info.productChIds[i];
- if (chn_id <= 0)
- {
- break;
- }
- // 查询通道信息
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp->product_ch_id == chn_id)
- break;
- }
- int start_year, start_month, start_day;
- int end_year, end_month, end_day;
- char current_date[11]; // 足够存储 "YYYY-MM-DD\0"
- // 解析起始和结束日期
- sscanf(_serviceManageRequestInfo._ds_info.StartDate, "%d-%d-%d", &start_year, &start_month, &start_day);
- sscanf(_serviceManageRequestInfo._ds_info.Enddate, "%d-%d-%d", &end_year, &end_month, &end_day);
- // 复制起始日期到current_date
- strcpy(current_date, _serviceManageRequestInfo._ds_info.StartDate);
- // 循环直到或超过结束日期
- int done = 0;
- while (!done)
- {
- // 当前日期
- _PowerDSManage_t *new_data = (_PowerDSManage_t *)calloc(1, sizeof(_PowerDSManage_t));
- if(!new_data) {
- log_e("___ new_data calloc failed!\n");
- break;
- }
- memset(new_data, 0, sizeof(_PowerDSManage_t));
- strcpy(new_data->ds_date, current_date);
- strcpy(new_data->ds_time, _serviceManageRequestInfo._ds_info.channelTime);
- new_data->set_stats = atoi(_serviceManageRequestInfo._ds_info.setStatus);
- new_data->ds_is_triggered = 0;
- list_add_tail(&new_data->list, &_globalPowerMangerTemp->list_DS);
- dev_insert_power_ds(__globalDeviceManage.db, 1, chn_id, new_data);
- // 解析当前日期为年、月、日
- sscanf(current_date, "%d-%d-%d", &start_year, &start_month, &start_day);
- // 增加一天
- add_day_to_date(current_date, &start_year, &start_month, &start_day);
- // 检查是否超过结束日期
- if (start_year > end_year ||
- (start_year == end_year && start_month > end_month) ||
- (start_year == end_year && start_month == end_month && start_day > end_day))
- {
- done = 1; // 超过结束日期,退出循环
- }
- }
- }
- lock_s_release(LOCK_ID_POWER_TIMER);
- char temp[100];
- //sprintf(temp, "$设置$|定时$");
- sprintf(temp, "Add scheduled tasks");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- //批量删除
- else if (strcmp(_serviceManageRequestInfo.type, "deleteDs")==0)
- {
-
- int nChID = 0;
- char pcDate[16];
- char pcTime[16];
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- _PowerDSManage_t* _powerDsManageTemp=NULL;;
- char *pCharData;
- lock_s_hold(LOCK_ID_POWER_TIMER);
- for (int i = 0; i < 64; i++)
- {
- pCharData = _serviceManageRequestInfo._ds_info.idx[i];
- if (pCharData == NULL || *pCharData == '\0')
- {
- break;
- }
- sscanf(pCharData, "%d %s %s", &nChID, pcDate, pcTime);
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp->product_ch_id == nChID)
- break;
- }
- list_for_each_entry(_powerDsManageTemp, &_globalPowerMangerTemp->list_DS, list)
- {
- if (strcmp(_powerDsManageTemp->ds_date, pcDate) == 0&&strcmp(_powerDsManageTemp->ds_time, pcTime) == 0)
- {
- dev_delete_power_ds(__globalDeviceManage.db, 1, _globalPowerMangerTemp->product_ch_id, _powerDsManageTemp);
- list_del(&_powerDsManageTemp->list);
- free(_powerDsManageTemp);
- _powerDsManageTemp=NULL;
- break;
- }
- }
- }
- lock_s_release(LOCK_ID_POWER_TIMER);
- char temp[100];
- // sprintf(temp, "$删除$|定时$");
- sprintf(temp, "Delete scheduled tasks");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- // 时序设置
- else if (strcmp(_serviceManageRequestInfo.type, "listSxQuery") == 0) // 查询
- {
- bool bflag = false;
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp->product_id == atoi(_serviceManageRequestInfo._posix_info.productId))
- {
- bflag = true;
- break;
- }
- }
- if (bflag == false) return;
-
- ack_str = service_sx_ack_to_json(_globalPowerMangerTemp->product_id, &__globalDeviceManage._globalPowerManger);
- if (_serviceManageRequestInfo._posix_info.productId) free(_serviceManageRequestInfo._posix_info.productId);
- }
- else if(strcmp(_serviceManageRequestInfo.type,"sXTimeSave")==0)//保存时间间隔
- {
- int chn_id = atoi(_serviceManageRequestInfo._posix_info.productChId);
- int id = atoi(_serviceManageRequestInfo._posix_info.productId);
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp->product_id == id && _globalPowerMangerTemp->product_ch_id == chn_id)
- {
- int value = atoi(_serviceManageRequestInfo._posix_info.firingInterval);
- if (value > 65535)
- {
- char buf[128];
- memset(buf, 0, sizeof(buf));
- sprintf(buf, "product id = %d, ch id = %d start delay err.", _globalPowerMangerTemp->product_id, _globalPowerMangerTemp->product_ch_id);
- log_e(buf);
- continue;
- }
- _globalPowerMangerTemp->product_ch_start_delay = value;
- value = atoi(_serviceManageRequestInfo._posix_info.closeInterval);
- if (value > 65535)
- {
- char buf[128];
- memset(buf, 0, sizeof(buf));
- sprintf(buf, "product id = %d, ch id = %d stop delay err.", _globalPowerMangerTemp->product_id, _globalPowerMangerTemp->product_ch_id);
- log_e(buf);
- continue;
- }
- _globalPowerMangerTemp->product_ch_stop_delay = value;
- 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);
- if (ret<0)
- {
- usleep(50000);
- 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);
- /* code */
- }
- 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);
- if (ret<0)
- {
- usleep(50000);
- 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);
- /* code */
- }
-
- if (dev_update_power_manage_genera_info(__globalDeviceManage.db,_globalPowerMangerTemp->product_id,_globalPowerMangerTemp->product_ch_id,_globalPowerMangerTemp) != 0)
- {
- log_e("update pwr general data err.");
- }
- break;
- }
- }
- ack_str = ctrl_sts_ack_to_json();
- if (_serviceManageRequestInfo._posix_info.productId) free(_serviceManageRequestInfo._posix_info.productId);
- if (_serviceManageRequestInfo._posix_info.closeInterval) free(_serviceManageRequestInfo._posix_info.closeInterval);
- if (_serviceManageRequestInfo._posix_info.firingInterval) free(_serviceManageRequestInfo._posix_info.firingInterval);
- if (_serviceManageRequestInfo._posix_info.productChId) free(_serviceManageRequestInfo._posix_info.productChId);
- }
- // 分组设置
- else if (strcmp(_serviceManageRequestInfo.type, "fzSave") == 0) // 分组设置
- {
- int nMode = atoi(_serviceManageRequestInfo._group_info.mode);
- GlobalPowerManger* _globalPowerMangerTemp = NULL ;
- int chNum= 0;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- chNum++;
- }
- //初始化分组-(未析构)
- GroupInfo group_list;
- INIT_LIST_HEAD(&group_list.list);
- if (0 == nMode)//相邻分组方式
- {
- int xlnum = atoi(_serviceManageRequestInfo._group_info.xlnumber);
-
- if (xlnum > 0)
- {
- int pGroupNum = chNum/xlnum;
- if (chNum % xlnum>0)
- {
- pGroupNum++;
- }
-
- dev_delete__group_info(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id);
- for (size_t i = 0; i < pGroupNum; i++)
- {
- GroupInfo *new_group = (GroupInfo *)malloc(sizeof(GroupInfo));
- // 清空数据
- memset(new_group, 0, sizeof(GroupInfo));
- if (!new_group)
- {
- break;
- }
- new_group->product_id = __globalDeviceManage._globalDevInfo.product.id;
- new_group->GroupID = i + 1;
- sprintf(new_group->GroupName, "Group%d", i + 1);
- int j = 0;
- GlobalPowerManger *_globalPowerMangerTemp1 = NULL;
- list_for_each_entry(_globalPowerMangerTemp1, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (j >= i * xlnum && j < (i + 1) * xlnum)
- {
- char chTemp[64];
- sprintf(chTemp, "%d,", _globalPowerMangerTemp1->product_ch_id);
- strcat(new_group->GroupChList, chTemp);
- }
- j++;
- }
- list_add_tail(&new_group->list, &group_list.list);
- dev_insert_group_info(__globalDeviceManage.db, new_group);
- }
- }
- }
- else if (1 == nMode)//间隔分组方式
- {
- int jgnum = atoi(_serviceManageRequestInfo._group_info.jgnumber);
- int pGroupNum = jgnum+1;
- if (pGroupNum>chNum)
- {
- pGroupNum=chNum;
- }
- if (jgnum >= 0 && pGroupNum> 0 )
- {
- dev_delete__group_info(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id);
- for (size_t i = 0; i < pGroupNum; i++)
- {
- GroupInfo *new_group = (GroupInfo *)malloc(sizeof(GroupInfo));
- // 清空数据
- memset(new_group, 0, sizeof(GroupInfo));
- if (!new_group)
- {
- break;
- }
- new_group->product_id = __globalDeviceManage._globalDevInfo.product.id;
- new_group->GroupID = i + 1;
- sprintf(new_group->GroupName, "Group%d", i + 1);
- int j = i;
- int m = 0;
- GlobalPowerManger *_globalPowerMangerTemp1 = NULL;
- list_for_each_entry(_globalPowerMangerTemp1, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (m==j)
- {
- char chTemp[64];
- sprintf(chTemp, "%d,", _globalPowerMangerTemp1->product_ch_id);
- strcat(new_group->GroupChList, chTemp);
- j=j+jgnum+1;
- }
- m++;
- }
- list_add_tail(&new_group->list, &group_list.list);
- dev_insert_group_info(__globalDeviceManage.db, new_group);
- }
- }
- }
- GroupInfo *node;
- GroupInfo *next;
- list_for_each_entry_safe(node, next, &group_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- else if (strcmp(_serviceManageRequestInfo.type, "listFzQuery") == 0) // 分组查询
- {
- // 查询分组信息
- GroupInfo group_list;
- INIT_LIST_HEAD(&group_list.list);
- if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
- {
- // 返回分组信息
- ack_str = service_group_ack_to_json(&group_list);
- }
- GroupInfo *node;
- GroupInfo *next;
- list_for_each_entry_safe(node, next, &group_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- else if (strcmp(_serviceManageRequestInfo.type, "updateFz") == 0) // 分组修改
- {
- if (_serviceManageRequestInfo._group_info.opergroupid && _serviceManageRequestInfo._group_info.updategroupName)
- {
- int ngroup = atoi(_serviceManageRequestInfo._group_info.opergroupid);
- // 查询分组信息
- GroupInfo group_list;
- INIT_LIST_HEAD(&group_list.list);
- if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
- {
- GroupInfo *_GroupInfoTemp = NULL;
- list_for_each_entry(_GroupInfoTemp, &group_list.list, list)
- {
- if (_GroupInfoTemp->GroupID == ngroup)
- {
- strncpy(_GroupInfoTemp->GroupName, _serviceManageRequestInfo._group_info.updategroupName, sizeof(_GroupInfoTemp->GroupName)-1);
- _GroupInfoTemp->GroupName[sizeof(_GroupInfoTemp->GroupName) - 1] = '\0';
- dev_update_group_info(__globalDeviceManage.db, _GroupInfoTemp);
- }
- }
- }
- GroupInfo *node;
- GroupInfo *next;
- list_for_each_entry_safe(node, next, &group_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- char temp[100];
- // sprintf(temp, "$打开$|$分组$|%d", ngroup);
- sprintf(temp, "Update group %d", ngroup);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- else if (strcmp(_serviceManageRequestInfo.type, "fzOpen") == 0) // 分组开
- {
- unsigned short pAddrCtrl[50];
- memset(pAddrCtrl,0,sizeof(pAddrCtrl));
- int ngroup= atoi(_serviceManageRequestInfo._group_info.opergroupid);
- // 查询分组信息
- GroupInfo group_list;
- INIT_LIST_HEAD(&group_list.list);
- if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
- {
- GroupInfo *_GroupInfoTemp = NULL;
- list_for_each_entry(_GroupInfoTemp, &group_list.list, list)
- {
- if (_GroupInfoTemp->GroupID == ngroup)
- {
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == 1)
- { // 直流版本功能
- char *pCHlist;
- char *pCHListSave;
- pCHlist=strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
- while(pCHlist!=NULL)
- {
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp == NULL)
- break;
- if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
- pAddrCtrl[(int)_globalPowerMangerTemp->product_saddr] |= 1 << (_globalPowerMangerTemp->product_ch_addr - 1);
- break;
- }
- }
- pCHlist=strtok_r(NULL, ",",&pCHListSave);
- }
- for (size_t i = 0; i < 50; i++)
- {
- if (pAddrCtrl[i] != 0)
- {
- ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
- i,
- pAddrCtrl[i],
- 1);
- if (ret < 0)
- {
- ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
- i,
- pAddrCtrl[i], 1);
- }
- }
- }
- }
- else
- {
- char *pCHlist;
- char *pCHListSave;
- pCHlist = strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
- //其他版本功能
- while (pCHlist!=NULL)
- {
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp == NULL)
- break;
- if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
- ret= g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
- 1,true);
- if (ret < 0)
- {
- ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
- 1,true);
- }
- break;
- }
- }
- pCHlist = strtok_r(NULL, ",",&pCHListSave);
- }
- }
- }
- }
- }
- GroupInfo *node;
- GroupInfo *next;
- list_for_each_entry_safe(node, next, &group_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- char temp[100];
- //sprintf(temp, "$打开$|$分组$|%d", ngroup);
- sprintf(temp, "Open group %d", ngroup);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- else if (strcmp(_serviceManageRequestInfo.type, "fzClose") == 0) // 分组关
- {
- unsigned short pAddrCtrl[50];
- memset(pAddrCtrl, 0, sizeof(pAddrCtrl));
- int ngroup = atoi(_serviceManageRequestInfo._group_info.opergroupid);
- // 查询分组信息
- GroupInfo group_list;
- INIT_LIST_HEAD(&group_list.list);
- if (0 == dev_get_group_info(__globalDeviceManage.db, &group_list))
- {
- GroupInfo *_GroupInfoTemp = NULL;
- list_for_each_entry(_GroupInfoTemp, &group_list.list, list)
- {
- if (_GroupInfoTemp->GroupID == ngroup)
- {
- /* code */
- if (__globalDeviceManage._globalDevInfo.product.pwr_type == 1)
- {
- char *pCHlist;
- char *pCHListSave;
- pCHlist = strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
- // 直流版本功能
- while (pCHlist!=NULL)
- {
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp == NULL)
- break;
- if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
- pAddrCtrl[(int)_globalPowerMangerTemp->product_saddr] |= 1 << (_globalPowerMangerTemp->product_ch_addr-1);
- break;
- }
- }
- pCHlist = strtok_r(NULL, ",",&pCHListSave);
- }
- for (size_t i = 0; i < 50; i++)
- {
- if (pAddrCtrl[i] != 0)
- {
- ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
- i,
- pAddrCtrl[i],
- 0);
- if (ret < 0)
- {
- ret = g_switch_set_dcpdu_chns_ctrl(&__globalDeviceManage._globalRelaySampManger,
- i,
- pAddrCtrl[i],
- 0);
- /* code */
- }
- }
- }
- }
- else
- {
- char *pCHlist;
- char *pCHListSave;
- pCHlist=strtok_r(_GroupInfoTemp->GroupChList, ",",&pCHListSave);
- while (pCHlist!=NULL)
- {
- GlobalPowerManger *_globalPowerMangerTemp = NULL;
- list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- if (_globalPowerMangerTemp == NULL)
- break;
- if (atoi(pCHlist) == _globalPowerMangerTemp->product_ch_id)
- {
- log_d("switch ctrl:%d,%d", _globalPowerMangerTemp->product_saddr, _globalPowerMangerTemp->product_ch_addr);
- ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
- 0,true);
- if (ret < 0)
- {
- ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger,
- _globalPowerMangerTemp,
- _globalPowerMangerTemp->product_saddr,
- _globalPowerMangerTemp->product_ch_addr, // 多版端口余8
- 0,true);
- }
- break;
- }
- }
- pCHlist = strtok_r(NULL, ",",&pCHListSave);
- }
- }
- }
- }
- }
- GroupInfo *node;
- GroupInfo *next;
- list_for_each_entry_safe(node, next, &group_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- char temp[100];
- // sprintf(temp, "$关闭$|$分组$|%d", ngroup);
- sprintf(temp, "Close group %d", ngroup);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- else if (strcmp(_serviceManageRequestInfo.type, "gysbSave") == 0)
- {
- if (_serviceManageRequestInfo.productName)
- {
- /* code */
- strncpy(__globalDeviceManage._globalDevInfo.product.name, _serviceManageRequestInfo.productName, sizeof(__globalDeviceManage._globalDevInfo.product.name) - 1);
- __globalDeviceManage._globalDevInfo.product.name[sizeof(__globalDeviceManage._globalDevInfo.product.name) - 1] = '\0'; // 确保字符串以null结尾
- paras_save();
- char log_buff[256];
- sprintf(log_buff, "Modify device information!");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
- }
- ack_str = ctrl_sts_ack_to_json();
- }
- //cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- static void serviceManagementUpload(void *conn)
- {
- int idx,r=-1 ;
- char path[200];
- buf_t buf;
- char *p,*tok="idx=";
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- GlobalDeviceManager *dm=&__globalDeviceManage;
-
- if(strstr(hm->uri.buf, "type=importData")) { //firmware upload
- sprintf(path, "%s/%s", DOWN_FOLDER, CERT_FILE);
- r = upload_file(conn, path);
- if(r==0) {
- p = strstr(hm->uri.buf, tok);
- if(p) {
- idx = atoi(p+strlen(tok));
- if(idx<0 || idx>=MQTT_SER_MAX) {
- printf("_____ wrong idx, %d\n", idx);
- return;
- }
- r = file_load(path, &buf);
- if(r==0) {
- mqtt_server_t *ser=&dm->mqttInfo.ser[idx];
- if(buf.dlen<=sizeof(ser->cert)) {
- memcpy(ser->cert, buf.buf, buf.dlen);
- dev_mqtt_update_ser(dm->db, ser);
- }
- else {
- printf("_____ cert is too long, %d\n", buf.dlen);
- }
- free(buf.buf);
- }
- }
- }
- }
- }
- /// @brief 系统管理
- /// @param stream
- static void systemManagent(void *conn)
- {
- int ret = -1 ;
- cJSON *cjson;
- _UserRoleManageInfo _UserRoleManageRequestInfo;
- char* ack_str = NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf = (char*)hm->body.buf;
- //转换json参数
- cjson = json_to_user_manage(rx_body_buf,&_UserRoleManageRequestInfo);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
- //获取角色信息
- else if(strcmp(_UserRoleManageRequestInfo.type,"getUser")==0)
- {
- // 查询用户信息
- UserInfo User_list;
- INIT_LIST_HEAD(&User_list.list);
- // 查询角色信息
- RoleInfo Role_list;
- INIT_LIST_HEAD(&Role_list.list);
- ack_str = ctrl_sts_ack_to_json();
- if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) && 0 == dev_get_role_info(__globalDeviceManage.db, &Role_list)
- /*&& strcmp(_UserRoleManageRequestInfo.userName,"")!=0*/)
- {
- char *nName=getUserNamebyToken(_UserRoleManageRequestInfo.user_token);
- if (nName!=NULL)
- {
- ack_str = service_userRole_ack_to_json(nName,&User_list, &Role_list);
- }
- }
- UserInfo *node;
- UserInfo *next;
- list_for_each_entry_safe(node, next, &User_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- RoleInfo *node1;
- RoleInfo *next1;
- list_for_each_entry_safe(node1, next1, &Role_list.list, list)
- {
- list_del(&node1->list);
- free(node1);
- }
- }
- //查询用户列表
- else if(strcmp(_UserRoleManageRequestInfo.type,"listUserQuery")==0)
- {
- // 查询用户信息
- UserInfo User_list;
- INIT_LIST_HEAD(&User_list.list);
- // 查询角色信息
- RoleInfo Role_list;
- INIT_LIST_HEAD(&Role_list.list);
- ack_str = ctrl_sts_ack_to_json();
- if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) && 0 == dev_get_role_info(__globalDeviceManage.db, &Role_list)
- /*&& strcmp(_UserRoleManageRequestInfo.userName,"")!=0*/)
- {
- char *nName = getUserNamebyToken(_UserRoleManageRequestInfo.user_token);
- if (nName != NULL)
- {
- ack_str = service_userQuery_ack_to_json(nName, &User_list, &Role_list,_UserRoleManageRequestInfo.userName,_UserRoleManageRequestInfo.pagenum,_UserRoleManageRequestInfo.pagesize);
- }
- }
- UserInfo *node;
- UserInfo *next;
- list_for_each_entry_safe(node, next, &User_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- RoleInfo *node1;
- RoleInfo *next1;
- list_for_each_entry_safe(node1, next1, &Role_list.list, list)
- {
- list_del(&node1->list);
- free(node1);
- }
- }
- //删除用户
- else if(strcmp(_UserRoleManageRequestInfo.type,"deleteUser")==0)
- {
- bool CurUserFlag=false;
- char *nName = getUserNamebyToken(_UserRoleManageRequestInfo.user_token);
- if (nName != NULL)
- {
- UserInfo User_list;
- INIT_LIST_HEAD(&User_list.list);
- if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list))
- {
- UserInfo *_UserInfoTemp = NULL;
- list_for_each_entry(_UserInfoTemp, &User_list.list, list)
- {
- if (strcmp(_UserInfoTemp->userName, nName) == 0)
- {
- if (_UserRoleManageRequestInfo.userIds == _UserInfoTemp->userID)
- {
- CurUserFlag = true;
- break;
- }
- break;
- }
- }
- }
- UserInfo *node;
- UserInfo *next;
- list_for_each_entry_safe(node, next, &User_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- if (!CurUserFlag)
- {
- // char *pUserlist;
- // char *pUserListSave;
- // pUserlist = strtok_r(_UserRoleManageRequestInfo.userIds, ",", &pUserListSave);
- // while (pUserlist != NULL)
- //{
- dev_delete_user_info(__globalDeviceManage.db, _UserRoleManageRequestInfo.userIds);
- // pUserlist = strtok_r(NULL, ",", &pUserListSave);
- // }
- char temp[100];
- // sprintf(temp, "$删除$|$用户$|%d", _UserRoleManageRequestInfo.userIds);
- sprintf(temp, "Delete user %d", _UserRoleManageRequestInfo.userIds);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str = ctrl_sts_ack_to_json();
- }
- else
- {
- ack_str = ctrl_breaker_err_ack_to_json("Can't delete logged in users!!!!");
- }
- }
- //添加用户
- else if(strcmp(_UserRoleManageRequestInfo.type,"addUser")==0)
- {
- //未析构
- UserInfo *new_user = (UserInfo *)malloc(sizeof(UserInfo));
- memset(new_user,0,sizeof(UserInfo));
- new_user->product_id = __globalDeviceManage._globalDevInfo.product.id;
- int nMaxUserNum=1;
- bool existFlag=false;
- UserInfo User_list;
- INIT_LIST_HEAD(&User_list.list);
- // 查询角色信息
- RoleInfo Role_list;
- INIT_LIST_HEAD(&Role_list.list);
- if (0 == dev_get_user_info(__globalDeviceManage.db, &User_list) && 0 == dev_get_role_info(__globalDeviceManage.db, &Role_list))
- {
- UserInfo *_UserInfoTemp = NULL;
- list_for_each_entry(_UserInfoTemp, &User_list.list, list)
- {
- if (_UserInfoTemp == NULL)
- break;
- if (_UserInfoTemp->userID >= nMaxUserNum)
- {
- nMaxUserNum=_UserInfoTemp->userID;
- }
- if (strcmp(_UserInfoTemp->userName, _UserRoleManageRequestInfo.userName) == 0)
- {
- existFlag = true;
- break;
- }
-
- }
- if (existFlag)
- {
- ack_str = ctrl_breaker_err_ack_to_json("User already exists!");
- }
- else
- {
- new_user->userID = nMaxUserNum + 1;
- strcpy(new_user->userName, _UserRoleManageRequestInfo.userName);
- strcpy(new_user->userPW, _UserRoleManageRequestInfo.password);
- strcpy(new_user->nickName, _UserRoleManageRequestInfo.nickName);
- new_user->roleID = _UserRoleManageRequestInfo.roleId; // GetRoleIDByName(_UserRoleManageRequestInfo.roleName,&Role_list);
- struct tm *t;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- 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);
- dev_insert_user_info(__globalDeviceManage.db, new_user);
- char temp[100];
- // sprintf(temp, "$添加$|$用户$|%s", _UserRoleManageRequestInfo.userName);
- sprintf(temp, "Add user %s", _UserRoleManageRequestInfo.userName);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
-
- ack_str=ctrl_sts_ack_to_json();
- }
- }
- free(new_user);
- UserInfo *node;
- UserInfo *next;
- list_for_each_entry_safe(node, next, &User_list.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- RoleInfo *node1;
- RoleInfo *next1;
- list_for_each_entry_safe(node1, next1, &Role_list.list, list)
- {
- list_del(&node1->list);
- free(node1);
- }
- }
- //更新用户信息
- else if(strcmp(_UserRoleManageRequestInfo.type,"updateUser")==0)
- {
- //未析构
- UserInfo *new_user = (UserInfo *)malloc(sizeof(UserInfo));
- memset(new_user,0,sizeof(UserInfo));
- new_user->product_id = __globalDeviceManage._globalDevInfo.product.id;
- new_user->userID = _UserRoleManageRequestInfo.userId;
- strcpy(new_user->userName , _UserRoleManageRequestInfo.userName);
- strcpy(new_user->userPW , _UserRoleManageRequestInfo.password);
- strcpy(new_user->nickName , _UserRoleManageRequestInfo.nickName);
- new_user->roleID = _UserRoleManageRequestInfo.roleId;
- strcpy(new_user->createTime , _UserRoleManageRequestInfo.createTime);
- dev_update_user_info(__globalDeviceManage.db, new_user);
- free(new_user);
- char temp[100];
- //sprintf(temp, "$修改$|$用户$|%s", _UserRoleManageRequestInfo.userName);
- sprintf(temp, "Update user %s", _UserRoleManageRequestInfo.userName);
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- ack_str=ctrl_sts_ack_to_json();
- }
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- /// @brief 设备重启
- /// @param stream
- static void equipmentMintenance(void *conn)
- {
- int ret = -1 ;
- char* ack_str = NULL ;
- cJSON *cjson;
- _OverBackStsInfo _over_back_sts_info; //设备总体状态信息
- _Equipment_Mintenance _Over_equipment_mintenance;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char *rx_body_buf = (char*)hm->body.buf;
- //转换json参数
- cjson = json_to_over_equipment_mintenance(rx_body_buf,&_Over_equipment_mintenance);
- if(cjson==NULL)
- {
- http_reply(conn, 400, NULL);
- return ;
- }
- //查询设备列表
- if(strcmp(_Over_equipment_mintenance.type,"listSbRestartQuery")==0 || strcmp(_Over_equipment_mintenance.type,"listSbWhQuery")==0)
- {
- _over_back_sts_info.product_name = __globalDeviceManage._globalDevInfo.product.name;
- _over_back_sts_info.product_number = __globalDeviceManage._globalDevInfo.product.number;
- _over_back_sts_info.product_id = __globalDeviceManage._globalDevInfo.product.id;
- _over_back_sts_info.product_Code = __globalDeviceManage._globalDevInfo.product.pnumber;
- // _over_back_sts_info.product_type = malloc(32);
- //strcpy(_over_back_sts_info.product_type, "GC Series");
- _over_back_sts_info.product_type=g_product_type_str[__globalDeviceManage._globalDevInfo.product.type];
- _over_back_sts_info.productVersion = __globalDeviceManage._globalDevInfo.product.version;
-
- _over_back_sts_info.product_sysTime = malloc(32);
- struct tm *t;
- struct timeval tv;
- gettimeofday(&tv, NULL);
- t = localtime(&tv.tv_sec);
- sprintf(_over_back_sts_info.product_sysTime, "%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);
- long diff =0;
- struct timespec ts;
- if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
- {
- diff = ts.tv_sec;
- }
- _over_back_sts_info.product_runTime = malloc(32);
- sprintf(_over_back_sts_info.product_runTime, "%02ld:%02ld:%02ld", diff / 3600, (diff % 3600) / 60, diff % 60);
- _over_back_sts_info.productAddr = getLocalIpAddress("eth0");
- _over_back_sts_info.product_mac = get_mac_address("eth0");
- _over_back_sts_info.product_RS485Addr = __globalDeviceManage._globalDevInfo.cascade.addr;
- //_over_back_sts_info.product_status = __globalDeviceManage._globalDevInfo.product.status;
- _over_back_sts_info.product_status = malloc(16);
- strcpy(_over_back_sts_info.product_status, "online");
- _over_back_sts_info.product_type_id = __globalDeviceManage._globalDevInfo.product.pwr_type;
- if (strcmp(_Over_equipment_mintenance.type, "listSbWhQuery") == 0)
- {
- ack_str = over_product_list_info_to_json(&__globalDeviceManage, &_over_back_sts_info, true);
- }
- else
- ack_str = over_product_list_info_to_json(&__globalDeviceManage, &_over_back_sts_info, false);
- }
- else if(strcmp(_Over_equipment_mintenance.type,"listShfwQuery")==0)
- {
- GlobalDeviceInfo *dev=&__globalDeviceManage._globalDevInfo;
- ack_str = vendor_info_to_json(dev);
- }
- else if(strcmp(_Over_equipment_mintenance.type,"sBRestart")==0) { //restart
- char temp[100];
- //sprintf(temp, "$系统$|$重启$");
- sprintf(temp, "System Reboot!");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- sys_reboot();
- }else if(strcmp(_Over_equipment_mintenance.type,"listKgSbQuery")==0) // part of device infomation
- {
- _OverBackBreakerInfo breaker_info;
- printf("get listKgSbQuery infomation!!!\n");
- breaker_info.product_id = __globalDeviceManage._globalDevInfo.product.id;
- breaker_info.product_name = __globalDeviceManage._globalDevInfo.product.name;
- breaker_info.product_number = __globalDeviceManage._globalDevInfo.product.number;
- breaker_info.product_type = __globalDeviceManage._globalDevInfo.product.type;
- ack_str = over_breaker_product_list_info_to_json(&__globalDeviceManage,&breaker_info,false);
- }else if(strcmp(_Over_equipment_mintenance.type,"listKgTdQuery")==0)
- {
- _OverBreakerChnInfo _over_breaker_chn_info;
- INIT_LIST_HEAD(&_over_breaker_chn_info.list);
- //_OverBraekerChnInfo *temp = NULL;
- GlobalPowerManger * _globalPowerTemp = NULL;
- int cur_dev_addr = 0;
- if(_Over_equipment_mintenance.product_id)
- cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
- if(cur_dev_addr) //slave
- {
- //
- }else{ //zhuji
- printf("get listKgTdQuery infomation!!!\n");
- list_for_each_entry(_globalPowerTemp, &__globalDeviceManage._globalPowerManger.list, list)
- {
- _OverBreakerChnInfo *temp = (_OverBreakerChnInfo *) malloc(sizeof(_OverBreakerChnInfo));
- temp->product_id = __globalDeviceManage._globalDevInfo.product.id;
- strcpy(temp->product_name,__globalDeviceManage._globalDevInfo.product.name);
- strcpy(temp->product_number,__globalDeviceManage._globalDevInfo.product.number);
- strcpy(temp->product_ch_name,_globalPowerTemp->product_ch_name);
- temp->product_ch_id = _globalPowerTemp->product_ch_id;
- printf("%s %s %s %d\n",temp->product_name,temp->product_number,temp->product_ch_name,temp->product_ch_id);
- list_add_tail(&temp->list,&_over_breaker_chn_info.list);
- }
- }
- //参数转换json
- ack_str = over_all_breaker_chn_info_to_json(&_over_breaker_chn_info,0);
- //释放资源
- _OverBreakerChnInfo* node;
- _OverBreakerChnInfo* next;
- list_for_each_entry_safe(node, next, &_over_breaker_chn_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }else if(strcmp(_Over_equipment_mintenance.type,"listSwitchtatusQuery") == 0)
- {
- _OverBreakerSwitchInfo _over_breaker_switch_info;
- INIT_LIST_HEAD(&_over_breaker_switch_info.list);
- if(_Over_equipment_mintenance.product_id)
- cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
- if(cur_dev_addr) //slave
- {
- cmd_data_t cmd = {0};
- int r=0;
- cmd.cmd = CASCADE_CMD_BREAKER_GET_INFO;
- cmd.obj = OBJ_OVERALL;
- cascade_request(&cmd);
- // cmd.cmd = CASCADE_CMD_BREAKER_QUERY;
- // cmd.obj = OBJ_OVERALL;
- // cascade_request(&cmd);
- ack_str=ctrl_sts_ack_to_json();
- // GlobalBreakerManager *temp = NULL;
- // GlobalDeviceManager *dm = &__globalDeviceManage2;
- // lock_s_hold(LOCK_ID_BREAKER);
- // list_for_each_entry(temp, &dm->g_new_global_breaker.list, list)
- // {
- // char buff[120];
- // printf("get listSwitchtatusQuery infomation!!!\n");
- // _OverBreakerSwitchInfo *info = (_OverBreakerSwitchInfo *)
- // malloc(sizeof(_OverBreakerSwitchInfo));
- // memset(info,0,sizeof(_OverBreakerSwitchInfo));
- // info->product_id = temp->product_id;
- // sprintf(buff,"Breaker %d",temp->breaker_id);
- // strcpy(info->product_ch_name,buff);
- // sprintf(buff,"%s %d",__globalDeviceManage._globalDevInfo.product.name,temp->breaker_id);
- // strcpy(info->productName,buff);
- // strcpy(info->product_number,__globalDeviceManage._globalDevInfo.product.number);
- // strcpy(info->productChids,temp->breaker_chns_name);
- // info->switch_id = temp->breaker_id;
- // info->status = temp->breaker_status;
- // strcpy(info->cjfs,temp->breaker_gather_type);
- // strcpy(info->switch_name,temp->breaker_switch_name);
- // strcpy(info->comkh,temp->breaker_com);
- // sprintf(buff,"%d",temp->breaker_gather_addr);
- // strcpy(info->cjdz,buff);
- // sprintf(buff,"%d",temp->breaker_chn);
- // strcpy(info->jdh,buff);
- // printf("%s %s %s %s %d %d %s %s %s %s\n",
- // info->product_ch_name,info->productName,info->product_number,info->productChids,info->switch_id,
- // info->status,info->cjfs,info->comkh,info->cjdz,info->jdh);
- // list_add_tail(&info->list,&_over_breaker_switch_info.list);
- // }
- // lock_s_release(LOCK_ID_BREAKER);
- // ack_str = over_all_breaker_switch_info_to_json(&_over_breaker_switch_info,0);
- // _OverBreakerSwitchInfo* node;
- // _OverBreakerSwitchInfo* next;
- // list_for_each_entry_safe(node, next, &_over_breaker_switch_info.list, list)
- // {
- // list_del(&node->list);
- // free(node);
- //}
- }else //current
- {
- GlobalBreakerManager *temp = NULL;
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
- {
- char buff[20];
- //printf("get listSwitchtatusQuery infomation!!!\n");
- _OverBreakerSwitchInfo *info = (_OverBreakerSwitchInfo *)
- malloc(sizeof(_OverBreakerSwitchInfo));
- memset(info,0,sizeof(_OverBreakerSwitchInfo));
- info->product_id = temp->product_id;
- sprintf(buff,"Breaker %d",temp->breaker_id);
- strcpy(info->product_ch_name,buff);
- strcpy(info->productName,__globalDeviceManage._globalDevInfo.product.name);
- strcpy(info->product_number,__globalDeviceManage._globalDevInfo.product.number);
- strcpy(info->productChids,temp->breaker_chns_name);
- info->switch_id = temp->breaker_id;
- info->status = temp->breaker_status;
- strcpy(info->cjfs,temp->breaker_gather_type);
- strcpy(info->switch_name,temp->breaker_switch_name);
- strcpy(info->comkh,temp->breaker_com);
- sprintf(buff,"%d",temp->breaker_gather_addr);
- strcpy(info->cjdz,buff);
- sprintf(buff,"%d",temp->breaker_chn);
- strcpy(info->jdh,buff);
- // printf("%s %s %s %s %d %d %s %s %s %s\n",
- // info->product_ch_name,info->productName,info->product_number,info->productChids,info->switch_id,
- // info->status,info->cjfs,info->comkh,info->cjdz,info->jdh);
- list_add_tail(&info->list,&_over_breaker_switch_info.list);
- }
- lock_s_release(LOCK_ID_BREAKER);
- printf("_glos_asdsd addr=0x%x\n",(uint32_t)(&__globalDeviceManage));
- ack_str = over_all_breaker_switch_info_to_json(&_over_breaker_switch_info,0);
- //释放资源
- _OverBreakerSwitchInfo* node;
- _OverBreakerSwitchInfo* next;
- list_for_each_entry_safe(node, next, &_over_breaker_switch_info.list, list)
- {
- list_del(&node->list);
- free(node);
- }
- }
- }else if(strcmp(_Over_equipment_mintenance.type,"switchSave") == 0)
- {
- if(_Over_equipment_mintenance.product_id)
- cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
-
- if(cur_dev_addr) //slave
- {
- cmd_data_t cmd;
- cmd.datas.data.cjdz = atoi(_Over_equipment_mintenance.cjdz);
- cmd.datas.data.jdh = atoi(_Over_equipment_mintenance.jdh);
- strcpy(cmd.datas.data.breaker_chns,_Over_equipment_mintenance.productChids);
- strcpy(cmd.datas.data.cjfs,_Over_equipment_mintenance.cjfs);
- strcpy(cmd.datas.data.com,_Over_equipment_mintenance.comkh);
- cmd.cmd = CASCADE_CMD_BREAKER_SAVE_ADD;
- cmd.obj = OBJ_OVERALL;
- cascade_request(&cmd);
- ack_str=ctrl_sts_ack_to_json();
- }else
- {
- char buff[124] = {0};
- char id[MAX_BREAKER_ID] = {0};
- //bianli shujujiegou
- GlobalBreakerManager *temp = NULL;
- int i = 1;
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
- {
- id[temp->breaker_id] = 1;
- }
- lock_s_release(LOCK_ID_BREAKER);
- for(;i < MAX_BREAKER_ID;i++)
- {
- if(id[i] == 0)
- break;
- }
- if(i != MAX_BREAKER_ID)
- {
- GlobalBreakerManager *breakerManager = (GlobalBreakerManager*) malloc(sizeof(GlobalBreakerManager));
- memset(breakerManager,0,sizeof(GlobalBreakerManager));
- breakerManager->product_id = __globalDeviceManage._globalDevInfo.product.id;
- breakerManager->breaker_gather_addr = atoi(_Over_equipment_mintenance.cjdz);
- strcpy(breakerManager->breaker_com,_Over_equipment_mintenance.comkh);
- strcpy(breakerManager->breaker_gather_type,_Over_equipment_mintenance.cjfs);
-
- strcpy(breakerManager->breaker_chns_name,_Over_equipment_mintenance.productChids);
- sprintf(buff,"Breadker_%d",breakerManager->breaker_id);
- strcpy(breakerManager->breaker_switch_name,buff);
- breakerManager->breaker_id = i;
- uint32_t jdh = atoi(_Over_equipment_mintenance.jdh);
- if(breakerManager->breaker_gather_addr >= BREADER_485_ADDR &&
- breakerManager->breaker_gather_addr <= (BREADER_485_ADDR + BREAKER_BOARD_MAX))
- {
- if(jdh <= MAX_BREAKER_CHN && jdh >0)
- {
- char jdh_flag = 0;
- char chn[MAX_BREAKER_CHN] = {0};
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
- {
- if(temp->breaker_gather_addr == breakerManager->breaker_gather_addr)
- {
- chn[temp->breaker_chn-1] = 1;
- }
- }
- lock_s_release(LOCK_ID_BREAKER);
- if(chn[jdh-1] != 1)
- {
- breakerManager->breaker_chn = jdh;
- }else
- {
- jdh_flag = 1;
- }
-
- if(jdh_flag == 1)
- {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("channel or id is existing!!!!");
-
- }else{
- dev_insert_breaker_genera_manage(__globalDeviceManage.db,breakerManager);
- lock_s_hold(LOCK_ID_BREAKER);
- list_add_tail(&breakerManager->list,&__globalDeviceManage.g_new_global_breaker.list);
- lock_s_release(LOCK_ID_BREAKER);
- ack_str=ctrl_sts_ack_to_json();
- }
- }else {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("jdh error!!!!");
- }
- }else if(breakerManager->breaker_gather_addr >= 1 && breakerManager->breaker_gather_addr <=30) //controller
- {
- if(jdh <= 2 && jdh > 0)
- {
- char jdh_flag = 0;
- char chn[2] = {0};
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
- {
- if(temp->breaker_gather_addr == breakerManager->breaker_gather_addr)
- {
- chn[temp->breaker_chn-1] = 1;
- }
- }
- lock_s_release(LOCK_ID_BREAKER);
- if(chn[jdh-1] != 1)
- {
- breakerManager->breaker_chn = jdh;
- }else
- {
- jdh_flag = 1;
- }
- if(jdh_flag == 1)
- {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("channel or id is existing!!!!");
-
- }else{
- dev_insert_breaker_genera_manage(__globalDeviceManage.db,breakerManager);
- lock_s_hold(LOCK_ID_BREAKER);
- list_add_tail(&breakerManager->list,&__globalDeviceManage.g_new_global_breaker.list);
- lock_s_release(LOCK_ID_BREAKER);
- ack_str=ctrl_sts_ack_to_json();
- }
- }else
- {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("jdh error!!!!");
- }
- }else if(breakerManager->breaker_gather_addr == 0) //current support one breaker
- {
- if(jdh == 1)
- {
- char jdh_flag = 0;
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
- {
- if(temp->breaker_gather_addr == breakerManager->breaker_gather_addr)
- {
- jdh_flag = 1;
- }
- }
- lock_s_release(LOCK_ID_BREAKER);
- if(jdh_flag == 1)
- {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("channel or id is existing!!!!");
-
- }else{
- breakerManager->breaker_chn = jdh;
- dev_insert_breaker_genera_manage(__globalDeviceManage.db,breakerManager);
- lock_s_hold(LOCK_ID_BREAKER);
- list_add_tail(&breakerManager->list,&__globalDeviceManage.g_new_global_breaker.list);
- lock_s_release(LOCK_ID_BREAKER);
- ack_str=ctrl_sts_ack_to_json();
- }
- }else
- {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("jdh error!!!!");
- }
- }else
- {
- free(breakerManager);
- breakerManager = NULL;
- ack_str = ctrl_breaker_err_ack_to_json("not surpport base addr !!");
- }
- }else
- {
- ack_str = ctrl_breaker_err_ack_to_json("ID More 48 !!!!");
- }
- }
- }else if(strcmp(_Over_equipment_mintenance.type,"switchUpdate") == 0)
- {
- if(_Over_equipment_mintenance.product_id)
- cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
- if(cur_dev_addr) ///slave
- {
- cmd_data_t cmd;
- cmd.datas.data1.switch_id = _Over_equipment_mintenance.switchId;
- strcpy(cmd.datas.data1.breaker_chns,_Over_equipment_mintenance.productChids);
- strcpy(cmd.datas.data1.switch_name,_Over_equipment_mintenance.switchName);
- cmd.cmd = CASCADE_CMD_BREAKER_SAVE_UPDATE;
- cmd.obj = OBJ_OVERALL;
- cascade_request(&cmd);
- ack_str=ctrl_sts_ack_to_json();
- }else
- {
- GlobalBreakerManager *temp = NULL;
- int switch_id = _Over_equipment_mintenance.switchId;
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry(temp, &__globalDeviceManage.g_new_global_breaker.list, list)
- {
- if(temp->breaker_id == switch_id)
- {
- strcpy(temp->breaker_switch_name,_Over_equipment_mintenance.switchName);
- strcpy(temp->breaker_chns_name,_Over_equipment_mintenance.productChids);
- dev_update_breaker_genera_manage(__globalDeviceManage.db,
- __globalDeviceManage._globalDevInfo.product.id,switch_id,temp);
- break;
- }
- }
- lock_s_release(LOCK_ID_BREAKER);
- ack_str=ctrl_sts_ack_to_json();
- }
- }else if(strcmp(_Over_equipment_mintenance.type,"switchDelete") == 0)
- {
- if(_Over_equipment_mintenance.product_id)
- cur_dev_addr = atoi(_Over_equipment_mintenance.product_id);
- if(cur_dev_addr) ///slave
- {
- cmd_data_t cmd;
- cmd.datas.data2.switch_id = _Over_equipment_mintenance.switchId;
- cmd.cmd = CASCADE_CMD_BREAKER_SAVE_DELETE;
- cmd.obj = OBJ_OVERALL;
- cascade_request(&cmd);
- GlobalBreakerManager* node = NULL;
- GlobalBreakerManager* next = NULL;
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry_safe(node,next,&__globalDeviceManage2.g_new_global_breaker.list, list)
- {
- if(node->breaker_id == _Over_equipment_mintenance.switchId)
- {
- // dev_delete_breaker_manage(__globalDeviceManage2.db,__globalDeviceManage._globalDevInfo.product.id,
- // _Over_equipment_mintenance.switchId);
- list_del(&node->list);
- free(node);
- break;
- }
- }
- lock_s_release(LOCK_ID_BREAKER);
- ack_str=ctrl_sts_ack_to_json();
- }else
- {
- int switch_id = _Over_equipment_mintenance.switchId;
- //GlobalBreakerManager *temp = NULL;
- GlobalBreakerManager* node;
- GlobalBreakerManager* next;
- lock_s_hold(LOCK_ID_BREAKER);
- list_for_each_entry_safe(node,next,&__globalDeviceManage.g_new_global_breaker.list, list)
- {
- if(node->breaker_id == switch_id)
- {
- dev_delete_breaker_manage(__globalDeviceManage.db,__globalDeviceManage._globalDevInfo.product.id,switch_id);
- list_del(&node->list);
- free(node);
- node = NULL;
- break;
- }
- }
- lock_s_release(LOCK_ID_BREAKER);
- ack_str=ctrl_sts_ack_to_json();
- }
- }
- else if(strcmp(_Over_equipment_mintenance.type,"restoreFactorySettings")==0) { //restore to factory
- char temp[100];
- GlobalDeviceManager *dm=&__globalDeviceManage;
- PowerTotal_t *ptotal=&dm->_globalDevInfo.ptotal;
- //sprintf(temp, "$系统$|$恢复出厂设置$");
- sprintf(temp, "System Reset!");
- dev_insert_alarm_ctrl(dm->db, dm->_globalDevInfo.product.id, ALARM_TYPE_OPRATION, temp);
- for (size_t i = 0; i <= ptotal->brdMax; i++)
- {
- if (dm->pCtrlBoard[i].product_saddr <= 0)
- {
- continue;
- }
- g_switch_get_all_reset(&dm->_globalRelaySampManger,dm->pCtrlBoard[i].product_type,__globalDeviceManage.pCtrlBoard[i].product_saddr);
- }
- sys_set_factory();
- }
- else if (strcmp(_Over_equipment_mintenance.type,"resetChannel") == 0)
- {
- http_reply(conn, 200, ack_str);
- //__globalDeviceManage._ReloadChm_flag=true;
- lock_s_hold(LOCK_ID_POWER_UPDATE);
- ResetChmData(1);
- lock_s_release(LOCK_ID_POWER_UPDATE);
- char log_buff[256];
- sprintf(log_buff, "Rescan channel!");
- dev_insert_alarm_ctrl(__globalDeviceManage.db, __globalDeviceManage._globalDevInfo.product.id, ALARM_TYPE_OPRATION, log_buff);
- //__globalDeviceManage._ReloadChm_flag=false;
- upg_board_all_update();
- }
- else if (strcmp(_Over_equipment_mintenance.type,"cleanUp") == 0)
- {
- if (_Over_equipment_mintenance.cleanSelectItem!=NULL)
- {
- char *pItemlist;
- char *pItemListSave;
- pItemlist = strtok_r(_Over_equipment_mintenance.cleanSelectItem, ",", &pItemListSave);
- while (pItemlist != NULL)
- {
- int ItemID=atoi(pItemlist) ;
- if (1==ItemID)
- {
- log_i("CleanUp all power history Information!\n");
- dev_delete_ac_power_info_all(__globalDeviceManage.db);
- dev_delete_power_info_all(__globalDeviceManage.db);
- }
- else if (2==ItemID)
- {
- log_i("CleanUp all sensor history Information!\n");
- dev_delete_sensor_info_all(__globalDeviceManage.db);
- }
- else if (3==ItemID)
- {
- log_i("CleanUp all alarm log Information!\n");
- dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_POWER);
- dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_SENSOR);
- dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_NETWORK);
- }
- else if (4==ItemID)
- {
- log_i("CleanUp all operation log Information!\n");
- dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_OPRATION);
- }
- else if (5==ItemID)
- {
- log_i("CleanUp all system log Information!\n");
- dev_delete_alarm_info_all(__globalDeviceManage.db,ALARM_TYPE_RUNNING);
- }
- pItemlist = strtok_r(NULL, ",", &pItemListSave);
- }
- }
- dev_sql_VACCUM(__globalDeviceManage.db);
- log_i("Database SQLite VACCUM!\n");
- ack_str=ctrl_sts_ack_to_json();
- }
- else if (strcmp(_Over_equipment_mintenance.type,"listRtHisQuery") == 0) {
- ack_str = upgrade_to_json();
- }
- cJSON_Delete(cjson);
- http_reply(conn, 200, ack_str);
- cJSON_free((void*)ack_str);
- }
- static void equipmentMintenanceUpload(void *conn)
- {
- int r = -1 ;
- char* ack_str=NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- char path[200],tmp[60];
-
- if(strstr(hm->uri.buf, "uploadType=gjsj")) { //firmware upload
- file_io(conn, FILE_HOST_FW, FILE_IMPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "uploadType=shfw")) { //logo upload
- file_io(conn, FILE_LOGO, FILE_IMPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "uploadType=0")) {
- file_io(conn, FILE_ALARM_CFG, FILE_IMPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "uploadType=1")) {
- file_io(conn, FILE_SERV_CFG, FILE_IMPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "uploadType=2")) {
- file_io(conn, FILE_SENSOR_CFG, FILE_IMPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "uploadType=3")) {
- file_io(conn, FILE_SYS_CFG, FILE_IMPORT, NULL);
- }
- ack_str = ctrl_sts_ack_to_json();
- http_reply(conn, 200, ack_str);
- cJSON_free(ack_str);
- }
- static void equipmentMintenanceExport(void *conn)
- {
- int r = -1 ;
- char* ack_str=NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
-
- if(strstr(hm->uri.buf, "export=0")) {
- file_io(conn, FILE_ALARM_CFG, FILE_EXPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "export=1")) {
- file_io(conn, FILE_SERV_CFG, FILE_EXPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "export=2")) {
- file_io(conn, FILE_SENSOR_CFG, FILE_EXPORT, NULL);
- }
- else if(strstr(hm->uri.buf, "export=3")) {
- file_io(conn, FILE_SYS_CFG, FILE_EXPORT, NULL);
- }
- }
- static void controlBoardUpgrade(void *conn)
- {
- int r = -1 ;
- char* ack_str=NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- if(strstr(hm->uri.buf, "listCbQuery")) {
- handle_t l=upg_board_all_get();
- ack_str = board_to_json(l);
-
- http_reply(conn, 200, ack_str);
- cJSON_free(ack_str);
- }
- }
- static void controlBoardUpgradeUpload(void *conn)
- {
- int r = -1 ;
- char* ack_str=NULL ;
- mg_stream_t *s=(mg_stream_t*)conn;
- struct mg_http_message *hm=s->ev_data;
- if(strstr(hm->uri.buf, "type=importData")) {
- char path[100];
- sprintf(path, "%s/%s", DOWN_FOLDER, "fw.upg");
- r = upload_file(conn, path);
- if(r==0) {
- upg_board_list_set(hm->uri.buf);
- printf("_____ board upgrade now ...\n");
- upg_board_start(path);
- }
- }
- }
- static int webSetCallback(void)
- {
- httpAddCallback("/login", loginFunc);//登录
- httpAddCallback("/logout", loginOutFunc);//登出
- httpAddCallback("/getInfo", getUserInfo);//获取用户信息
- httpAddCallback("/pdu/changeLanguages", changeLanguages);//切换语言
- httpAddCallback("/pdu/systemConfiguration", systemConfiguration);//登录
- httpAddCallback("/getRouters", getUserRouters);//获取菜单接口
- httpAddCallback("/pdu/overallStatusMonitoring", overallStatusMonitoring);//设备电源监控信息
- httpAddCallback("/pdu/channelStatusMonitoring", channelStatusMonitoring);//设备通道电源监控信息
- httpAddCallback("/pdu/sensorStatusMonitoring", sensorStatusMonitoring);//传感器总体请求信息
- httpAddCallback("/pdu/alarmProtect", alarmProtect);//告警历史信息
- httpAddCallback("/pdu/logInformation", logInformation);//日志
- httpAddCallback("/pdu/communicationManagement", communicationManagement);//通信设置
- httpAddCallback("/pdu/serviceManagement", serviceManagement);//服务管理
- httpAddCallback("/pdu/serviceManagementUpload", serviceManagementUpload);//服务管理
- httpAddCallback("/pdu/system", systemManagent);//用户管理
- httpAddCallback("/pdu/equipmentMintenance", equipmentMintenance);//设备维护
- httpAddCallback("/pdu/equipmentMintenanceUpload", equipmentMintenanceUpload); //固件升级
- httpAddCallback("/pdu/equipmentMintenanceExport", equipmentMintenanceExport);
- httpAddCallback("/pdu/controlBoardUpgradeUpload", controlBoardUpgradeUpload);
- httpAddCallback("/pdu/controlBoardUpgrade", controlBoardUpgrade);
-
- return 0;
- }
- int appweb_init(void)
- {
- thread_start(THREAD_ID_WEB, web_thread, &__globalDeviceManage);
-
- return 0;
- }
|