core.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. /******************************************************************
  2. ** Copyright (c) 2009 Wuhan Mozit Technology Co., Ltd .
  3. ** FileName: core.c
  4. ** Author: BigHead
  5. ** Mail: jsrenyw@icloud.com
  6. ** Editor: bighead
  7. ** Date: 2020-11-10 13:34
  8. ** Version: 2020-11-10
  9. ** File Description: 业务逻辑核心代码实现部分
  10. ******************************************************************/
  11. #include "core.h"
  12. #include "cmd.h"
  13. //指令发送表device_command:
  14. #define CTR_RRS 1 //远程重启
  15. #define CTR_SFR 2 //设备恢复出厂设置
  16. #define CTR_RS 3 //服务器跳转
  17. #define CTR_OTA 4 //ota升级
  18. #define CTR_RDC 5 //远程手动调光
  19. #define CTR_RSC 6 //远程开关控制 需回复
  20. #define CTR_RCN 7 //删除子节点指令 需回复
  21. #define CFG_BKS 8 //备份服务器配置 需回复
  22. #define CFG_ULI 9 //设备数据上传间隔配置 需回复
  23. #define CFG_STS 10 //开关时间段设置 需回复
  24. #define CFG_SRV 11 //额定电压设置 需回复
  25. #define CFG_SRC 12 //额定电流设置 需回复
  26. #define CFG_NAT 13 //下发节点信息表配置
  27. #define CFG_GNV 14 //获取子节点版本号
  28. #define CFG_SCM 15 //开关控制模式(拉合闸)设置 需回复
  29. #define CFG_DGS 16 //调光节点分组设置
  30. #define CFG_DGM 17 //调光节点分组数据修改
  31. #define CFG_MGM 18 //电表节点分组设置 需回复
  32. #define CFG_SAT 19 //传感器报警阀值设置 需回复
  33. #define CTR_RGC 20 //远程开关编组控制 需回复
  34. //继电器状态:
  35. #define SW_ON 0 //0x0000:继电器合闸
  36. #define SW_MANUAL_CONTROL 32 //0x0020:继电器远程拉闸
  37. #define SW_TIMER_CONTROL 16 //0x0010: 继电器自动时间段拉闸
  38. #define SW_TEMPERATURE_HUMIDITY_ALARM 513 //0x0201:继电器温湿度报警拉闸 需报警
  39. #define SW_FIRE_ALARM 514 //0x0202:继电器烟感报警拉闸 需报警
  40. #define SW_TILT_ALARM 515 //0x0203:继电器倾斜报警拉闸 需报警
  41. #define SW_LOCK_ALARM 516 //0x0204:继电器锁具报警拉闸 需报警
  42. #define SW_LINE_TEMPERATURE_ALARM 517 //0x0205:继电器线温报警拉闸 需报警
  43. #define SW_OVERVOLTAGE 1 //0x0001:过压拉闸 需报警
  44. #define SW_UNDERVOLTAGE 2 //0x0002:欠压拉闸 需报警
  45. #define SW_OVERLOAD_ALARM 4 //0x0004: 过载拉闸 需报警
  46. #define SW_ELECTRIC_EXCESS 8 //0x0008:用电超额拉闸
  47. #define SW_OVER_SWITCH_OUT_COUNT 64 //0x0040:超自动合闸次数
  48. #define SW_FAST_CURRENT 128 //0x0080:快速电流拉闸
  49. int sw[] = {513,514,515,516,517,1,2,4};
  50. typedef enum {false = 0,true =1} bool;
  51. static pthread_t threads[32];
  52. #define CRC32_POLYNOMIAL 0xEDB88320
  53. list_node* file_list = NULL; //文件链表
  54. list_node* user_list = NULL; //设备链表
  55. int have_table = 0;
  56. static uint32_t crc_table32[256]; //CRC查询表
  57. unsigned short ota_file_packet_len = 1024;//ota文件每包的数据长度
  58. //按照大端模式解析数据
  59. //将1个整型数据填充到缓存中
  60. #define INT_TO_BUFF(buff, pos, val) \
  61. buff[(pos)] = (val)&0x000000FF; \
  62. buff[(pos) + 1] = ((val)&0x0000FF00) >> 8; \
  63. buff[(pos) + 2] = ((val)&0x00FF0000) >> 16; \
  64. buff[(pos) + 3] = ((val)&0xFF000000) >> 24
  65. //从缓存中提取1个整型数据
  66. #define INT_FROM_BUFF(buff, pos) ((buff[(pos) + 3] << 24) + (buff[(pos) + 2] << 16) + (buff[(pos) + 1] << 8) + buff[(pos)])
  67. //从缓存中提取1个整型数据
  68. #define SHORT_FROM_BUFF(buff, pos) ((buff[(pos) + 1] << 8) + buff[(pos)])
  69. /******************************************************************
  70. * Function Name: db_init
  71. * Arguments:
  72. * Return Value: void
  73. * Date: 2020-11-10
  74. * Editor: bighead
  75. * Description: 程序启动时刻,调用该初始化函数多次,初始化不同mysql 连接
  76. ******************************************************************/
  77. int db_init(MYSQL *db)
  78. {
  79. int ret = 1;
  80. if (db)
  81. {
  82. MYSQL *_db = db;
  83. ret = mysqlConnetInit(_db) ? 1 : 0;
  84. }
  85. else
  86. {
  87. elog("MYSQL.var is NULL \n ");
  88. exit(0);
  89. }
  90. return ret;
  91. }
  92. /******************************************************************
  93. * Function Name: core_init
  94. * Arguments:
  95. * Return Value: void
  96. * Date: 2020-11-10
  97. * Editor: bighead
  98. * Description: 程序启动时刻,调用该初始化函数仅 一次,该函数不允许重复调用
  99. ******************************************************************/
  100. int core_init(void)
  101. {
  102. int ret = 0;
  103. // 加载Topic服务配置
  104. initTopicConf();
  105. //创建队列
  106. //m_pque = CreateQueue(1000);
  107. // init mqtt.client
  108. init_mqtt_client();
  109. sleep(1);
  110. // 注册Topic
  111. regTopicFromTable();
  112. //创建处理任务队列线程
  113. //if(NUM_THREADS_UPLOAD_g>0)
  114. // CreateDbHandThread();
  115. return ret;
  116. }
  117. /******************************************************************
  118. * Function Name: core_heart
  119. * Arguments:
  120. * Return Value: void
  121. * Date: 2020-11-10
  122. * Editor: bighead
  123. * Description: 每次主线程心跳调用, 约200ms一次, 可以调整频率在decode.c
  124. ******************************************************************/
  125. int core_heart(unsigned int nowTime)
  126. {
  127. int ret=0;
  128. ret=nowTime;
  129. return ret;
  130. }
  131. /******************************************************************
  132. * Function Name: core_heart_1s
  133. * Arguments:
  134. * Return Value: void
  135. * Date: 2020-11-10
  136. * Editor: bighead
  137. * Description: 每次主线程心跳调用, 约1s一次,不超过2s, 在core_heart心跳函数之后调用
  138. * 可以调整频率在decode.c
  139. ******************************************************************/
  140. int core_heart_1s(unsigned int nowTime)
  141. {
  142. int ret = 0;
  143. ret = nowTime;
  144. //时间同步广播
  145. ret = TimeSyncBroadcast();
  146. //从数据库取指令
  147. SendCmdFromDb();
  148. return ret;
  149. }
  150. //初始化时候进行部分测试,可以随意添加
  151. int core_do_test(void)
  152. {
  153. //decode_msg_handle("TEST", NULL, NULL);
  154. print_stats();
  155. return 0;
  156. }
  157. //定期调用会话统计信息, 分钟级别
  158. int core_stats(void)
  159. {
  160. print_stats();
  161. return 0;
  162. }
  163. /******************************************************************
  164. * Function Name: core_alarm_gateway
  165. * Arguments:
  166. * Return Value: void
  167. * Date: 2023-12-5
  168. * Editor: cc
  169. * Description: 扫描网关设备数据
  170. ******************************************************************/
  171. int CoreAlarmGateway(void)
  172. {
  173. static MYSQL *conn = NULL;
  174. MYSQL_RES* res = NULL;
  175. MYSQL_ROW row;
  176. char query[1024] = {0};
  177. int row_count;
  178. if (!conn)
  179. {
  180. // 先申请内存再初始化
  181. conn = malloc(sizeof(MYSQL));
  182. db_init(conn);
  183. }
  184. sprintf(query,"SELECT t1.device_mac,t1.gateway_mac,t2.device_name,t2.control_mode,t1.update_time,"
  185. "t1.current_on_off,t1.current_electric,t1.original_relay,t2.swith_time1,t2.swith_time2,t2.swith_time3,t2.swith_time4,"
  186. "t2.electric_wave_set,t2.electric_wave_min,t2.electric_wave_max,t1.command_update_time "
  187. "FROM dev_status AS t1 "
  188. "INNER JOIN dev_info_gateway AS t2 "
  189. "ON t1.device_mac = t2.device_mac "
  190. "WHERE TIMESTAMPDIFF(MINUTE, t1.update_time, NOW()) < 10 "
  191. "AND t1.current_online=1 "
  192. "AND t1.original_relay IS NOT NULL "
  193. "AND t1.current_electric IS NOT NULL "
  194. "AND t1.electric_update_flag=1 "
  195. "AND t1.alarm_enable=1 ");
  196. excuteSql(conn,query);
  197. res = mysql_store_result(conn);
  198. if (NULL == res)
  199. {
  200. debug("NULL == res \n");
  201. return 1;
  202. }
  203. //获取行数
  204. row_count = mysql_num_rows(res);
  205. if(row_count<1)
  206. {
  207. mysql_free_result(res);
  208. debug("row_count<1 查询记录为0\n");
  209. return 1;
  210. }
  211. debug("CoreAlarmGateway row_count:%d \r\n",row_count);
  212. //循环取出
  213. while((row = mysql_fetch_row(res)))
  214. {
  215. char device_mac[30] = {0}; //0节点
  216. char gateway_mac[30] = {0}; //1网关
  217. char device_name[256] = {0}; //2设备名称
  218. int control_mode = 0; //3控制模式
  219. char update_time[20]; //4更新时间
  220. int current_on_off = 0; //5开关状态
  221. int current_electric = 0; //6电流
  222. int original_relay = 0; //7原始开关值
  223. char switch_time1[10] = {0}; //8开关时间1
  224. char switch_time2[10] = {0}; //9开关时间2
  225. char switch_time3[10] = {0}; //10开关时间3
  226. char switch_time4[10] = {0}; //11开关时间4
  227. int electric_wave_set = 0; //12电流波峰值
  228. double electric_wave_min = 0.0; //13电流波峰值
  229. double electric_wave_max = 0.0; //14电流波峰值
  230. char command_update_time[20]; //15指令更新时间
  231. struct tm tm_time;
  232. struct tm tm_time2;
  233. char timeString[20];
  234. int result,result1,result2,result3,result4;
  235. char update_sql[100]={0};
  236. char strTemp[256] ={0};
  237. //取出数据
  238. strcpy(device_mac,row[0]);
  239. strcpy(gateway_mac,row[1]);
  240. strcpy(device_name,row[2]);
  241. control_mode = atoi(row[3]);
  242. strptime(row[4], "%Y-%m-%d %H:%M:%S", &tm_time);
  243. strftime(update_time, sizeof(update_time), "%Y-%m-%d %H:%M:%S", &tm_time);
  244. current_on_off = atoi(row[5]);
  245. current_electric = atoi(row[6]);
  246. original_relay = atoi(row[7]);
  247. if(row[8]) strcpy(switch_time1,row[8]);
  248. if(row[9]) strcpy(switch_time2,row[9]);
  249. if(row[10]) strcpy(switch_time3,row[10]);
  250. if(row[11]) strcpy(switch_time4,row[11]);
  251. if(row[12]) electric_wave_set = atoi(row[12]);
  252. if(row[13]) electric_wave_min = atof(row[13]);
  253. if(row[14]) electric_wave_max = atof(row[14]);
  254. if(row[15]) strptime(row[15], "%Y-%m-%d %H:%M:%S", &tm_time2);
  255. strftime(command_update_time, sizeof(command_update_time), "%Y-%m-%d %H:%M:%S", &tm_time2);
  256. // log("CoreAlarmGateway : device_mac:'%s',gateway_mac:'%s',device_name:%s,control_mode:%d,update_time:%s,current_on_off:%d,"
  257. // "current_electric:%d,original_relay:%d,switch_time1:%s,switch_time2:%s,switch_time3:%s,switch_time4:%s,"
  258. // "electric_wave_set:%d,electric_wave_min:%f,electric_wave_max:%f \n",
  259. // device_mac,gateway_mac,device_name,control_mode,update_time,current_on_off,current_electric,original_relay,
  260. // switch_time1,switch_time2,switch_time3,switch_time4,electric_wave_set,electric_wave_min,electric_wave_max);
  261. //更新数据取出标记
  262. sprintf(update_sql,"update dev_status set electric_update_flag=2 where device_mac='%s' and gateway_mac='%s' ",device_mac,gateway_mac);
  263. excuteSql(conn,update_sql);
  264. //判断继电器数据,有报警跳过后续判断
  265. result = RelayAlarm(gateway_mac,"0F0000000001",original_relay,conn);
  266. if(result==0) continue;
  267. //判断更新的数据是否在指令下发的2minute内
  268. if(row[15])
  269. {
  270. tm_time2.tm_min +=2; //将指令更新时间加2分钟
  271. mktime(&tm_time2);//重新计算时间
  272. int result = compareModifiedTimes(tm_time2,tm_time);
  273. if(result>=0)//指令更新时间在2分钟内跳过后续判断
  274. {
  275. debug("网关:%s,compareModifiedTimes tm_time2>=tm_time ------------>skip\n",device_name);
  276. continue;
  277. }
  278. }
  279. strftime(timeString, sizeof(timeString), "%H:%M", &tm_time);
  280. //判断是否在时间段点的2min以内
  281. if(compareWithUpdateTime(timeString,switch_time1,switch_time2,switch_time3,switch_time4)) continue;
  282. if(!row[8] && !row[9]) continue;
  283. debug("timeString:%s \n",timeString);
  284. //判断自动模式
  285. if(control_mode==0)
  286. {
  287. int isInTime1 = 0;
  288. int isInTime2 = 0;
  289. if(row[8]&&row[9]&&(strlen(switch_time1)>4)&&(strlen(switch_time2)>4))//第1个时间段不为空
  290. {
  291. debug("网关:%s,strlen(switch_time1)=%zu,strlen(switch_time2)=%zu\n",device_name,strlen(switch_time1),strlen(switch_time2));
  292. result1 = compareTimes(switch_time1,timeString);
  293. result2 = compareTimes(timeString,switch_time2);
  294. //时间正常顺序
  295. if(compareTimes(switch_time1,switch_time2)<=0)
  296. {
  297. debug("网关:%s,时间正常顺序,compareTimes switch_time1<=switch_time2\n",device_name);
  298. //合闸时间段
  299. if (result1<=0 && result2<=0) isInTime1=1;
  300. }
  301. //时间非正常顺序
  302. else
  303. {
  304. debug("网关:%s,时间非正常顺序,compareTimes switch_time1>=switch_time2\n",device_name);
  305. //合闸时间段
  306. if (result1<=0 || result2<=0) isInTime1=1;
  307. }
  308. }
  309. else //第1个时间段为空
  310. {
  311. isInTime1=2;
  312. }
  313. if(row[10]&&row[11]&&(strlen(switch_time3)>4)&&(strlen(switch_time4)>4))//第2个时间段不为空
  314. {
  315. debug("网关:%s,strlen(switch_time3)=%zu,strlen(switch_time4)=%zu\n",device_name,strlen(switch_time3),strlen(switch_time4));
  316. result3 = compareTimes(switch_time3,timeString);
  317. result4 = compareTimes(timeString,switch_time4);
  318. //时间正常顺序
  319. if(compareTimes(switch_time3,switch_time4)<=0)
  320. {
  321. debug("网关:%s,时间正常顺序,compareTimes switch_time3<=switch_time4\n",device_name);
  322. //合闸时间段
  323. if (result3<=0 && result4<=0) isInTime2=1;
  324. }
  325. //时间非正常顺序
  326. else
  327. {
  328. debug("网关:%s,时间非正常顺序,compareTimes switch_time3>=switch_time4\n",device_name);
  329. //合闸时间段
  330. if (result3<=0 || result4<=0) isInTime2=1;
  331. }
  332. }
  333. else //第1个时间段为空
  334. {
  335. isInTime2=2;
  336. }
  337. if((isInTime1==1||isInTime2==1) && current_on_off==0)
  338. {
  339. debug("网关:%s,合闸时间段继电器拉闸 \n",device_name);
  340. int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1101,"合闸时间段继电器拉闸",conn);
  341. if(ret==0) continue;
  342. }
  343. else if((isInTime1==0||isInTime2==0) && current_on_off==1)
  344. {
  345. debug("网关:%s,拉闸时间段继电器合闸 \n",device_name);
  346. int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1102,"拉闸时间段继电器合闸",conn);
  347. if(ret==0) continue;
  348. }
  349. }
  350. //判断继电器拉闸状态有电流
  351. if(current_on_off==0 && current_electric>100)
  352. {
  353. debug("网关:%s,继电器拉闸状态有电流 \n",device_name);
  354. int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1103,"继电器拉闸状态有电流",conn);
  355. if(ret==0) continue;
  356. }
  357. //判断继电器合闸状态无电流
  358. if(current_on_off==1 && current_electric<100)
  359. {
  360. debug("网关:%s,继电器合闸状态无电流 \n",device_name);
  361. int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1104,"继电器合闸状态无电流",conn);
  362. if(ret==0) continue;
  363. }
  364. //判断继电器合闸状态电流偏离正常阈值
  365. if(current_on_off==1 && electric_wave_set==3)
  366. {
  367. double fcurrent_electric = current_electric/10000.0;
  368. if(fcurrent_electric<(electric_wave_min*0.9)|| fcurrent_electric>(electric_wave_max*1.1))
  369. {
  370. debug("网关:%s,继电器合闸状态电流偏离正常值 \n",device_name);
  371. sprintf(strTemp,"继电器合闸状态电流偏离正常值,当前值:%.3f,阈值:%.3f-%.3f",fcurrent_electric,electric_wave_min,electric_wave_max);
  372. int ret=ElectricAlarm(gateway_mac,"0F0000000001",device_name,device_name,1105,strTemp,conn);
  373. if(ret==0) continue;
  374. }
  375. }
  376. }
  377. mysql_free_result(res);
  378. return 0;
  379. }
  380. /******************************************************************
  381. * Function Name: CoreAlarmNode
  382. * Arguments:
  383. * Return Value: void
  384. * Date: 2023-12-8
  385. * Editor: cc
  386. * Description: 扫描节点设备数据
  387. ******************************************************************/
  388. int CoreAlarmNode(void)
  389. {
  390. static MYSQL *conn2 = NULL;
  391. MYSQL_RES* res = NULL;
  392. MYSQL_ROW row;
  393. char query[1024] = {0};
  394. int row_count;
  395. if (!conn2)
  396. {
  397. // 先申请内存再初始化
  398. conn2 = malloc(sizeof(MYSQL));
  399. db_init(conn2);
  400. }
  401. sprintf(query,"SELECT t1.device_mac,t1.gateway_mac,t3.device_name,t2.device_name,t2.control_mode,t1.update_time,t1.current_on_off,t1.current_electric,"
  402. "t1.original_relay,t2.swith_time1,t2.swith_time2,t2.swith_time3,t2.swith_time4,t2.electric_wave_set,t2.electric_wave_min,t2.electric_wave_max,t1.command_update_time "
  403. "FROM dev_status AS t1 "
  404. "INNER JOIN dev_info_node AS t2 "
  405. "ON t1.device_mac = t2.device_mac "
  406. "AND t1.gateway_mac=t2.gateway_mac "
  407. "INNER JOIN dev_info_gateway AS t3 "
  408. "ON t2.gateway_mac = t3.device_mac "
  409. "WHERE TIMESTAMPDIFF(MINUTE, t1.update_time, NOW()) < 10 "
  410. "AND t1.current_online=1 "
  411. "AND t1.original_relay IS NOT NULL "
  412. "AND t1.current_electric IS NOT NULL "
  413. "AND t1.electric_update_flag=1 "
  414. "AND t1.alarm_enable=1 ");
  415. excuteSql(conn2,query);
  416. res = mysql_store_result(conn2);
  417. if (NULL == res)
  418. {
  419. debug("NULL == res \n");
  420. return 1;
  421. }
  422. //获取行数
  423. row_count = mysql_num_rows(res);
  424. if(row_count<1)
  425. {
  426. mysql_free_result(res);
  427. debug("row_count<1 查询记录为0 \n");
  428. return 1;
  429. }
  430. debug("CoreAlarmNode row_count:%d \r\n",row_count);
  431. //循环取出
  432. while((row = mysql_fetch_row(res)))
  433. {
  434. char device_mac[30] = {0}; //0节点
  435. char gateway_mac[30] = {0}; //1网关
  436. char gateway_name[256] = {0}; //2设备名称
  437. char node_name[256] = {0}; //3节点名称
  438. int control_mode = 0; //4控制模式
  439. char update_time[20]; //5更新时间
  440. int current_on_off = 0; //6开关状态
  441. int current_electric = 0; //7电流
  442. int original_relay = 0; //8原始开关值
  443. char switch_time1[10] = {0}; //9开关时间1
  444. char switch_time2[10] = {0}; //10开关时间2
  445. char switch_time3[10] = {0}; //11开关时间3
  446. char switch_time4[10] = {0}; //12开关时间4
  447. int electric_wave_set = 0; //13电流波峰值
  448. double electric_wave_min = 0.0; //14电流波峰值
  449. double electric_wave_max = 0.0; //15电流波峰值
  450. struct tm tm_time;
  451. struct tm tm_time2;
  452. char timeString[20];
  453. int result,result1,result2,result3,result4;
  454. char update_sql[100]={0};
  455. char strTemp[256] ={0};
  456. //取出数据
  457. strcpy(device_mac,row[0]);
  458. strcpy(gateway_mac,row[1]);
  459. strcpy(gateway_name,row[2]);
  460. strcpy(node_name,row[3]);
  461. control_mode = atoi(row[4]);
  462. strptime(row[5], "%Y-%m-%d %H:%M:%S", &tm_time);
  463. strftime(update_time, sizeof(update_time), "%Y-%m-%d %H:%M:%S", &tm_time);
  464. current_on_off = atoi(row[6]);
  465. current_electric = atoi(row[7]);
  466. original_relay = atoi(row[8]);
  467. if(row[9]) strcpy(switch_time1,row[9]);
  468. if(row[10]) strcpy(switch_time2,row[10]);
  469. if(row[11]) strcpy(switch_time3,row[11]);
  470. if(row[12]) strcpy(switch_time4,row[12]);
  471. if(row[13]) electric_wave_set = atoi(row[13]);
  472. if(row[14]) electric_wave_min = atof(row[14]);
  473. if(row[15]) electric_wave_max = atof(row[15]);
  474. if(row[16]) strptime(row[15], "%Y-%m-%d %H:%M:%S", &tm_time2);
  475. // log("CoreAlarmNode : device_mac:'%s',gateway_mac:'%s',gateway_name:%s,node_name:%s,control_mode:%d,update_time:%s,current_on_off:%d,"
  476. // "current_electric:%d,original_relay:%d,switch_time1:%s,switch_time2:%s,switch_time3:%s,switch_time4:%s,"
  477. // "electric_wave_set:%d,electric_wave_min:%f,electric_wave_max:%f \n",
  478. // device_mac,gateway_mac,gateway_name,node_name,control_mode,update_time,current_on_off,current_electric,original_relay,
  479. // switch_time1,switch_time2,switch_time3,switch_time4,electric_wave_set,electric_wave_min,electric_wave_max);
  480. //更新数据取出标记
  481. sprintf(update_sql,"update dev_status set electric_update_flag=2 where device_mac='%s' and gateway_mac='%s' ",device_mac,gateway_mac);
  482. excuteSql(conn2,update_sql);
  483. //判断继电器数据
  484. result = RelayAlarm(gateway_mac,device_mac,original_relay,conn2);
  485. if(result==0) continue;
  486. //判断更新的数据是否在指令下发的2minute内
  487. if(row[16])
  488. {
  489. tm_time2.tm_min +=2; //将指令更新时间加2分钟
  490. mktime(&tm_time2);//重新计算时间
  491. int result = compareModifiedTimes(tm_time2,tm_time);
  492. if(result>=0)//指令更新时间在2分钟内跳过后续判断
  493. {
  494. debug("网关:%s,节点:%s,compareModifiedTimes tm_time2>=tm_time------------>skip\n",gateway_name,node_name);
  495. continue;
  496. }
  497. }
  498. strftime(timeString, sizeof(timeString), "%H:%M", &tm_time);
  499. //判断是否在时间段点的2min以内
  500. if(compareWithUpdateTime(timeString,switch_time1,switch_time2,switch_time3,switch_time4)) continue;
  501. if(!row[9] || !row[10] || !row[11] || !row[12]) continue;
  502. debug("timeString:%s \n",timeString);
  503. //判断自动模式
  504. if(control_mode==0)
  505. {
  506. int isInTime1 = 0;
  507. int isInTime2 = 0;
  508. if(row[9]&&row[10]&&(strlen(switch_time1)>4)&&(strlen(switch_time2)>4))//第1个时间段不为空
  509. {
  510. debug("网关:%s,节点:%s,strlen(switch_time1)=%zu,strlen(switch_time2)=%zu\n",gateway_name,node_name,strlen(switch_time1),strlen(switch_time2));
  511. result1 = compareTimes(switch_time1,timeString);
  512. result2 = compareTimes(timeString,switch_time2);
  513. //时间正常顺序
  514. if(compareTimes(switch_time1,switch_time2)<=0)
  515. {
  516. debug("网关:%s,节点:%s,compareTimes switch_time1<=switch_time2\n",gateway_name,node_name);
  517. //合闸时间段
  518. if (result1<=0 && result2<=0) isInTime1=1;
  519. }
  520. //时间非正常顺序
  521. else
  522. {
  523. debug("网关:%s,节点:%s,compareTimes switch_time1>=switch_time2\n",gateway_name,node_name);
  524. //合闸时间段
  525. if (result1<=0 || result2<=0) isInTime1=1;
  526. }
  527. }
  528. else //第1个时间段为空
  529. {
  530. isInTime1=2;
  531. }
  532. if(row[11]&&row[12]&&(strlen(switch_time3)>4)&&(strlen(switch_time4)>4))//第2个时间段不为空
  533. {
  534. debug("网关:%s,节点:%s,strlen(switch_time3)=%zu,strlen(switch_time4)=%zu\n",gateway_name,node_name,strlen(switch_time3),strlen(switch_time4));
  535. result3 = compareTimes(switch_time3,timeString);
  536. result4 = compareTimes(timeString,switch_time4);
  537. //时间正常顺序
  538. if(compareTimes(switch_time3,switch_time4)<=0)
  539. {
  540. debug("网关:%s,节点:%s,compareTimes switch_time3<=switch_time4\n",gateway_name,node_name);
  541. //合闸时间段
  542. if (result3<=0 && result4<=0) isInTime2=1;
  543. }
  544. //时间非正常顺序
  545. else
  546. {
  547. debug("网关:%s,节点:%s,compareTimes switch_time3>=switch_time4\n",gateway_name,node_name);
  548. //合闸时间段
  549. if (result3<=0 || result4<=0) isInTime2=1;
  550. }
  551. }
  552. else //第1个时间段为空
  553. {
  554. isInTime2=2;
  555. }
  556. if((isInTime1==1||isInTime2==1) && current_on_off==0)
  557. {
  558. debug("网关:%s,节点:%s,合闸时间段继电器拉闸 \n",gateway_name,node_name);
  559. int ret=ElectricAlarm(gateway_mac,device_mac,gateway_name,node_name,1101,"合闸时间段继电器拉闸",conn2);
  560. if(ret==0) continue;
  561. }
  562. else if((isInTime1==0||isInTime2==0) && current_on_off==1)
  563. {
  564. debug("网关:%s,节点:%s,拉闸时间段继电器合闸 \n",gateway_name,node_name);
  565. int ret=ElectricAlarm(gateway_mac,device_mac,gateway_name,node_name,1102,"拉闸时间段继电器合闸",conn2);
  566. if(ret==0) continue;
  567. }
  568. }
  569. //判断继电器拉闸状态有电流
  570. if(current_on_off==0 && current_electric>100)
  571. {
  572. debug("网关:%s,节点:%s,继电器拉闸状态有电流 \n",gateway_name,node_name);
  573. int ret=ElectricAlarm(gateway_mac,device_mac,gateway_name,node_name,1103,"继电器拉闸状态有电流",conn2);
  574. if(ret==0) continue;
  575. }
  576. //判断继电器合闸状态无电流
  577. if(current_on_off==1 && current_electric<100)
  578. {
  579. debug("网关:%s,节点:%s,继电器合闸状态无电流 \n",gateway_name,node_name);
  580. int ret=ElectricAlarm(gateway_mac,device_mac,gateway_name,node_name,1104,"继电器合闸状态无电流",conn2);
  581. if(ret==0) continue;
  582. }
  583. //判断继电器合闸状态电流偏离正常阈值
  584. if(current_on_off==1 && electric_wave_set==3)
  585. {
  586. double fcurrent_electric = current_electric/10000.0;
  587. if(fcurrent_electric<(electric_wave_min*0.9)|| fcurrent_electric>(electric_wave_max*1.1))
  588. {
  589. debug("网关:%s,节点:%s,继电器合闸状态电流偏离正常值 \n",gateway_name,node_name);
  590. sprintf(strTemp,"继电器合闸状态电流偏离正常值,当前值:%.3f,阈值:%.3f-%.3f",fcurrent_electric,electric_wave_min,electric_wave_max);
  591. int ret=ElectricAlarm(gateway_mac,device_mac,gateway_name,node_name,1105,strTemp,conn2);
  592. if(ret==0) continue;
  593. }
  594. }
  595. }
  596. mysql_free_result(res);
  597. return 0;
  598. }
  599. /******************************************************************
  600. * Function Name: ElectricAlarm
  601. * Arguments:
  602. * Return Value: void
  603. * Date: 2023-12-7
  604. * Editor: cc
  605. * Description: 电流数据告警判断
  606. ******************************************************************/
  607. int ElectricAlarm(char* mac,char* subMac,char* gatewayName,char* nodeName,int alarmCode,char* alarmStr,MYSQL* _db)
  608. {
  609. //判断是否需要报警
  610. MYSQL_RES* res = NULL;
  611. MYSQL_ROW row;
  612. char querySql[256] = {0};
  613. char strName[256] = {0};
  614. char strAlarm[512] = {0};
  615. char updateSql[1024] = {0};
  616. char insertSql[512] = {0};
  617. int queryRow = 0;
  618. int fault_state=0;
  619. int fault_count=0;
  620. struct tm tm_time;
  621. //网关或节点名称
  622. if (strcmp(subMac,"0F0000000001") == 0)//网关
  623. {
  624. sprintf(strName,"设备名称:%s",gatewayName);
  625. }
  626. else
  627. {
  628. sprintf(strName,"设备名称:%s,节点:%s",gatewayName,nodeName);
  629. }
  630. //格式化报警内容
  631. sprintf(strAlarm,"%s,%s,时间:%s \r\n",strName,alarmStr,GetCurrentTime());
  632. //查询当前报警类型是否正在报警 fault_state 1: 未处理 2: 故障正在处理中 3: 处理完成
  633. sprintf(querySql,"select fault_state,fault_count,fault_time from dev_fault where device_mac = '%s' and device_mac_node = '%s' and relay_code = %d ",mac,subMac,alarmCode);
  634. excuteSql(_db,querySql);
  635. res = mysql_store_result(_db);
  636. queryRow = mysql_num_rows(res);
  637. if (res && queryRow>0)//有记录
  638. {
  639. debug("res && queryRow>0 querySql:%s\n",querySql);
  640. row = mysql_fetch_row(res);
  641. fault_state = atoi(row[0]);
  642. fault_count = atoi(row[1]);
  643. if(row[2]) strptime(row[2], "%Y-%m-%d %H:%M:%S", &tm_time);
  644. debug("fault_state:%d fault_count:%d\n",fault_state,fault_count);
  645. if(fault_state == 1 || fault_state == 2)//正在报警
  646. {
  647. log("查询到当前报警类型正在报警,不再重复报警,alarmStr:%s\n",strAlarm);
  648. mysql_free_result(res);
  649. return 1;
  650. }
  651. if(alarmCode>1100)//电流报警进行3次判断
  652. {
  653. //在更新次数前检查是否是连续的告警次数,否则重新计数,通过检查上一次告警时间是否在5min以内
  654. if(row[2])
  655. {
  656. tm_time.tm_min +=5; //将指令更新时间加5分钟
  657. mktime(&tm_time);//重新计算时间
  658. int result=compareWithCurrentTime(tm_time);
  659. if(result>=0)//如果在5min以内
  660. {
  661. if(fault_count >= 2)//加上本次报警次数达到3次
  662. {
  663. //更新到报警故障表
  664. sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 1,fault_time = now(),fault_count=0 where "
  665. "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,mac,subMac,alarmCode);
  666. log("触发告警(电流): %s\n",strAlarm);
  667. }
  668. else
  669. {
  670. fault_count = fault_count + 1;
  671. //更新报警次数,并不报警
  672. sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 3,fault_time = now(),fault_count=%d where "
  673. "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,fault_count,mac,subMac,alarmCode);
  674. debug("网关:%s,节点:%s,电流告警更新次数(不报警)\n",gatewayName,nodeName);
  675. }
  676. }
  677. else//不在5min以内 计数重新开始
  678. {
  679. //更新到报警故障表
  680. sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 3,fault_time = now(),fault_count=1 where "
  681. "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,mac,subMac,alarmCode);
  682. debug("不在5min以内 计数重新开始(电流): %s\n",strAlarm);
  683. }
  684. }
  685. else //没有时间字段 计数重新开始
  686. {
  687. //更新到报警故障表
  688. sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 3,fault_time = now(),fault_count=1 where "
  689. "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,mac,subMac,alarmCode);
  690. debug("没有时间字段 计数重新开始(电流): %s\n",strAlarm);
  691. }
  692. }
  693. else
  694. {
  695. //更新到报警故障表
  696. sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 1,fault_time = now() where "
  697. "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,mac,subMac,alarmCode);
  698. log("触发告警: %s\n",strAlarm);
  699. }
  700. debug("updateSql: %s\n",updateSql);
  701. excuteSql(_db,updateSql);
  702. }
  703. else//没有查询到记录
  704. {
  705. debug("没有记录 querySql:%s",querySql);
  706. sprintf(insertSql,"insert into dev_fault(device_mac,device_mac_node,fault_details,relay_code,fault_state,fault_time)values('%s','%s','%s',%d,1,now())",
  707. mac,subMac,strAlarm,alarmCode);
  708. log("触发告警(插入): %s\n",strAlarm);
  709. excuteSql(_db,insertSql);
  710. }
  711. mysql_free_result(res);
  712. // //更新到报警故障表
  713. // sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 1,fault_time = now() where "
  714. // "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,mac,subMac,alarmCode);
  715. // excuteSql(_db,updateSql);
  716. // affectedRows = mysql_affected_rows(_db);
  717. // if (affectedRows < 1)
  718. // {
  719. // sprintf(insertSql,"insert into dev_fault(device_mac,device_mac_node,fault_details,relay_code,fault_state,fault_time)values('%s','%s','%s',%d,1,now())",
  720. // mac,subMac,strAlarm,alarmCode);
  721. // //log("strAlarm:%s\n",strAlarm);
  722. // excuteSql(_db,insertSql);
  723. // }
  724. return 0;
  725. }
  726. //扫描数据库发送指令
  727. int SendCmdFromDb(void)
  728. {
  729. static MYSQL *_db = NULL;
  730. MYSQL_RES* res = NULL;
  731. MYSQL_ROW row;
  732. char query[1024] = {0};
  733. char deleteSql[256] = {0};
  734. char updateSql[256] = {0};
  735. int row_count;
  736. if (!_db)
  737. {
  738. // 先申请内存再初始化
  739. _db = malloc(sizeof(MYSQL));
  740. db_init(_db);
  741. }
  742. sprintf(query,"select id,device_mac,device_mac_node,device_command,device_group,device_on_off,"
  743. "device_luminance,dimming_mode,node_type,control_mode from dev_cmd_send where device_mac is not null and current_online = 1 and device_type < 10");
  744. excuteSql(_db,query);
  745. res = mysql_store_result(_db);
  746. if (NULL == res)
  747. {
  748. return 1;
  749. }
  750. //获取行数
  751. row_count = mysql_num_rows(res);
  752. if(row_count<1)
  753. {
  754. //debug("%s 没有查询到指令 \n",mac);
  755. mysql_free_result(res);
  756. return 1;
  757. }
  758. //循环取出
  759. while((row = mysql_fetch_row(res)))
  760. {
  761. int id = 0;
  762. char device_mac[30] = {0}; //网关mac 1
  763. char device_mac_node[30] = {0}; //节点mac 2
  764. int device_command = 0; //指令类型 3
  765. int device_group = 0; //组号 4
  766. int device_on_off = 0; //开关 5
  767. int device_luminance = 0; //亮度 6
  768. int dimming_mode = 0; //调光类型 7
  769. int node_type = 0; //节点类型 8
  770. int control_mode = 0; //开关控制模式 9
  771. int ret = 0;
  772. if(row[0])
  773. id = atoi(row[0]);
  774. if(!row[1] || strlen((char*)row[1])!=24)
  775. {
  776. //删除指令
  777. sprintf(deleteSql,"delete from dev_cmd_send where id = %d",id);
  778. excuteSql(_db,deleteSql);
  779. debug("网关mac不正确!");
  780. continue;
  781. }
  782. strcpy(device_mac,row[1]);
  783. if(row[2])
  784. strcpy(device_mac_node,row[2]);
  785. if(row[3])
  786. device_command = atoi(row[3]);
  787. if(row[4])
  788. device_group = atoi(row[4]);
  789. if(row[5])
  790. device_on_off = atoi(row[5]);
  791. if(row[6])
  792. device_luminance = atoi(row[6]);
  793. if(row[7])
  794. dimming_mode = atoi(row[7]);
  795. if(row[8])
  796. node_type = atoi(row[8]);
  797. if(row[9])
  798. control_mode = atoi(row[9]);
  799. debug("SendCmdFromDb 行数:%d ,device_mac:'%s',device_mac_node:'%s',device_command:%d,device_group:%d,device_on_off:%d,"
  800. "device_luminance:%d,dimming_mode:%d,node_type:'%d',control_mode:%d \n",
  801. row_count,device_mac,device_mac_node,device_command,device_group,device_on_off,device_luminance,dimming_mode,node_type,control_mode);
  802. //删除指令
  803. sprintf(deleteSql,"delete from dev_cmd_send where id = %d",id);
  804. excuteSql(_db,deleteSql);
  805. switch (device_command)
  806. {
  807. case CTR_RRS: //远程重启
  808. {
  809. ret = SendRestart(device_mac);
  810. }
  811. break;
  812. case CTR_SFR: //设备恢复出厂设置
  813. {
  814. ret = SendFactoryReset(device_mac);
  815. }
  816. break;
  817. case CTR_RS: //服务器跳转
  818. {
  819. ret = SendServerJump(device_mac,_db);
  820. }
  821. break;
  822. case CTR_OTA: //ota升级
  823. {
  824. ret = SendRemoteUpgrade(device_mac,_db);
  825. }
  826. break;
  827. case CTR_RDC: //远程手动调光
  828. {
  829. if(row[4] && row[6]) //row[7] 调光类型判断暂去掉
  830. {
  831. ret = SendManualDimming(device_mac,device_group,device_luminance,dimming_mode);
  832. }
  833. }
  834. break;
  835. case CTR_RSC: //远程开关控制 需回复
  836. {
  837. if (row[2] && (strlen(device_mac_node) == 12) && row[5] && row[8]) //节点
  838. {
  839. ret = SendSwitchControl(device_mac,device_mac_node,node_type,device_on_off);
  840. sprintf(updateSql,"update dev_status set command_update_time = now() where gateway_mac = '%s' and device_mac='%s'",device_mac,device_mac_node);
  841. }
  842. else if(!row[2] && row[5] && row[8]) //网关
  843. {
  844. ret = SendSwitchControl(device_mac,"",node_type,device_on_off);
  845. sprintf(updateSql,"update dev_status set command_update_time = now() where gateway_mac = '%s' and device_mac='%s'",device_mac,device_mac);
  846. }
  847. if(ret == 0)
  848. {
  849. excuteSql(_db,updateSql);
  850. }
  851. }
  852. break;
  853. case CTR_RCN: //删除子节点指令 需回复
  854. {
  855. if(row[2] && (strlen(device_mac_node) == 12) && row[8])
  856. {
  857. ret = SendDeleteNode(device_mac,device_mac_node,node_type);
  858. }
  859. }
  860. break;
  861. case CFG_BKS: //备份服务器配置 需回复
  862. {
  863. ret = SendBakIP(device_mac,_db);
  864. }
  865. break;
  866. case CFG_ULI: //设备数据上传间隔配置 需回复
  867. {
  868. ret = SendDataInterval(device_mac,_db);
  869. }
  870. break;
  871. case CFG_STS: //开关时间段设置 需回复
  872. {
  873. if (row[2] && (strlen(device_mac_node) == 12)) //节点
  874. {
  875. ret = SendOnOffTimeMuilt(device_mac,device_mac_node,_db);
  876. sprintf(updateSql,"update dev_status set command_update_time = now() where gateway_mac = '%s' and device_mac='%s'",device_mac,device_mac_node);
  877. }
  878. else if(!row[2]) //网关
  879. {
  880. ret = SendOnOffTimeMuilt(device_mac,"",_db);
  881. sprintf(updateSql,"update dev_status set command_update_time = now() where gateway_mac = '%s' and device_mac='%s'",device_mac,device_mac);
  882. }
  883. if(ret == 0)
  884. {
  885. excuteSql(_db,updateSql);
  886. }
  887. }
  888. break;
  889. case CFG_SRV: //额定电压设置 需回复
  890. {
  891. if (row[2] && (strlen(device_mac_node) == 12)) //节点
  892. {
  893. ret = SendRatedVoltage(device_mac,device_mac_node,_db);
  894. }
  895. else if(!row[2]) //网关
  896. {
  897. ret = SendRatedVoltage(device_mac,"",_db);
  898. }
  899. }
  900. break;
  901. case CFG_SRC: //额定电流设置 需回复
  902. {
  903. if (row[2] && (strlen(device_mac_node) == 12)) //节点
  904. {
  905. ret = SendRatedCurrent(device_mac,device_mac_node,_db);
  906. }
  907. else if(!row[2]) //网关
  908. {
  909. ret = SendRatedCurrent(device_mac,"",_db);
  910. }
  911. }
  912. break;
  913. case CFG_NAT: //下发节点信息表配置
  914. {
  915. ret = SendSubInfo(device_mac,_db);
  916. }
  917. break;
  918. case CFG_GNV: //获取子节点版本号
  919. {
  920. if (row[2] && (strlen(device_mac_node) == 12)) //节点
  921. {
  922. ret = SendGetSubVersion(device_mac,device_mac_node);
  923. }
  924. }
  925. break;
  926. case CFG_SCM: //开关控制模式(拉合闸)设置 需回复
  927. {
  928. if (row[2] && (strlen(device_mac_node) == 12)) //节点
  929. {
  930. ret = SendSwitchModeFromDB(device_mac,device_mac_node,_db);
  931. }
  932. else if(!row[2]) //网关
  933. {
  934. ret = SendSwitchModeFromDB(device_mac,"",_db);
  935. }
  936. }
  937. break;
  938. case CFG_DGS: //调光节点分组设置
  939. {
  940. if (row[2] && (strlen(device_mac_node) == 12)) //节点
  941. {
  942. ret = SendDimmingNodeGroup(device_mac,device_mac_node,_db);
  943. }
  944. }
  945. break;
  946. case CFG_DGM: //调光节点分组数据修改
  947. {
  948. if(row[4])
  949. {
  950. ret = SendDimmingNodeGroupModify(device_mac,device_group,_db);
  951. }
  952. }
  953. break;
  954. case CFG_MGM: //电表节点分组设置 需回复
  955. {
  956. }
  957. break;
  958. case CFG_SAT: //传感器报警阀值设置 需回复
  959. {
  960. ret = SendSensorThreshold(device_mac,_db);
  961. }
  962. break;
  963. case CTR_RGC: //远程开关编组控制 需回复
  964. {
  965. if(row[4] && row[5])
  966. {
  967. ret = SendSwitchGroupControl(device_mac,device_group,2,device_on_off);
  968. }
  969. }
  970. break;
  971. default:
  972. debug("指令不能识别:%d",device_command);
  973. break;
  974. }
  975. }
  976. mysql_free_result(res);
  977. return 0;
  978. }
  979. //时间同步广播
  980. int TimeSyncBroadcast(void)
  981. {
  982. //TIME_SYNC_INTERVAL_g 配置文件读取
  983. if(timeGloble_g%TIME_SYNC_INTERVAL_g==0)
  984. {
  985. int ret = 0;
  986. ret = SendNTP("");
  987. debug("时间同步广播结果:%d,当前时间同步间隔 %d \n",ret,TIME_SYNC_INTERVAL_g);
  988. }
  989. return 0;
  990. }
  991. //1.字节流转换为十六进制字符串
  992. void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen)
  993. {
  994. short i;
  995. unsigned char highByte, lowByte;
  996. for (i = 0; i < sourceLen; i++)
  997. {
  998. highByte = source[i] >> 4;
  999. lowByte = source[i] & 0x0f ;
  1000. highByte += 0x30;
  1001. if (highByte > 0x39)
  1002. dest[i * 2] = highByte + 0x07;
  1003. else
  1004. dest[i * 2] = highByte;
  1005. lowByte += 0x30;
  1006. if (lowByte > 0x39)
  1007. dest[i * 2 + 1] = lowByte + 0x07;
  1008. else
  1009. dest[i * 2 + 1] = lowByte;
  1010. }
  1011. return ;
  1012. }
  1013. //2.字节流转换为十六进制字符串
  1014. void Hex2Str( const char *sSrc, char *sDest, int nSrcLen )
  1015. {
  1016. int i;
  1017. char szTmp[3];
  1018. for( i = 0; i < nSrcLen; i++ )
  1019. {
  1020. sprintf( szTmp, "%02X", (unsigned char) sSrc[i] );
  1021. memcpy( &sDest[i * 2], szTmp, 2 );
  1022. }
  1023. return ;
  1024. }
  1025. //十六进制字符串转换为字节流
  1026. void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
  1027. {
  1028. short i;
  1029. unsigned char highByte, lowByte;
  1030. for (i = 0; i < sourceLen; i += 2)
  1031. {
  1032. highByte = toupper(source[i]);
  1033. lowByte = toupper(source[i + 1]);
  1034. if (highByte > 0x39)
  1035. highByte -= 0x37;
  1036. else
  1037. highByte -= 0x30;
  1038. if (lowByte > 0x39)
  1039. lowByte -= 0x37;
  1040. else
  1041. lowByte -= 0x30;
  1042. dest[i / 2] = (highByte << 4) | lowByte;
  1043. }
  1044. return ;
  1045. }
  1046. //改变字节顺序
  1047. void EndianSwap(unsigned char *pData, int startIndex, int length)
  1048. {
  1049. int i,cnt,end,start;
  1050. cnt = length / 2;
  1051. start = startIndex;
  1052. end = startIndex + length - 1;
  1053. unsigned char tmp;
  1054. for (i = 0; i < cnt; i++)
  1055. {
  1056. tmp = pData[start+i];
  1057. pData[start+i] = pData[end-i];
  1058. pData[end-i] = tmp;
  1059. }
  1060. return ;
  1061. }
  1062. //获取当前月份历史表
  1063. char* GetCurrentNYTable(void)
  1064. {
  1065. time_t t = time(0);
  1066. static char str[64] = {0};
  1067. strftime(str,sizeof(str),"dev_status_history_%Y%m",localtime(&t));
  1068. return str;
  1069. }
  1070. //获取系统时间年月日 时分秒
  1071. char* GetCurrentTime(void)
  1072. {
  1073. time_t t = time(0);
  1074. static char str[64] = {0};
  1075. strftime(str,sizeof(str),"%Y%m%d %H:%M:%S",localtime(&t));
  1076. return str;
  1077. }
  1078. //字符串中查找指定字符的个数
  1079. int find_char(char str[], char substr[])
  1080. {
  1081. int count = 0,i,j,check;
  1082. int len = strlen(str);
  1083. int sublen = strlen(substr);
  1084. for(i = 0; i < len; i++)
  1085. {
  1086. check = 1;
  1087. for(j = 0; j + i < len && j < sublen; j++)
  1088. {
  1089. if(str[i+j] != substr[j])
  1090. {
  1091. check = 0;
  1092. break;
  1093. }
  1094. }
  1095. if(check == 1)
  1096. {
  1097. count++;
  1098. i = i + sublen;
  1099. }
  1100. }
  1101. return count;
  1102. }
  1103. //从主题中查找指定'/'分割的段
  1104. char* find_string(char* topicName, int section)
  1105. {
  1106. static char tmp[50][50];
  1107. memset(tmp,0x0,2500);
  1108. char* p1 = (char*)malloc(1024);
  1109. int i = 0;
  1110. while ((p1 = strchr(topicName, '/')) != NULL)
  1111. {
  1112. strncpy(tmp[i], topicName,strlen(topicName)-strlen(p1));
  1113. topicName = p1 + 1;
  1114. i++;
  1115. }
  1116. strncpy(tmp[i], topicName, strlen(topicName));
  1117. free(p1);
  1118. return tmp[section];
  1119. }
  1120. //判断是否属于报警code
  1121. int IsRelayAlarmCode(int relayCode)
  1122. {
  1123. int i = 0;
  1124. for(i = 0;i<(int)(sizeof(sw)/sizeof(int));i++)
  1125. {
  1126. if(relayCode == sw[i])
  1127. return 1;
  1128. }
  1129. return -1;
  1130. }
  1131. //继电器报警
  1132. int RelayAlarm(char* mac,char* subMac,int relayCode,MYSQL* _db)
  1133. {
  1134. MYSQL_RES* res = NULL;
  1135. MYSQL_ROW row;
  1136. char querySql[256] = {0};
  1137. char updateSql[256] = {0};
  1138. char insertSql[256] = {0};
  1139. char strAlarm[256] = {0};
  1140. bool isExist = false;
  1141. char strAlarmType[256] = {0};
  1142. char strName[256] = {0};
  1143. char macName[256] = {0};
  1144. char subMacName[256] = {0};
  1145. int affectedRows = 0;
  1146. int queryRow = 0;
  1147. //判断是否需要报警
  1148. int i = 0;
  1149. for(i = 0;i<(int)(sizeof(sw)/sizeof(int));i++)
  1150. {
  1151. if(relayCode == sw[i])
  1152. isExist = true;
  1153. }
  1154. if(!isExist)
  1155. return 1;
  1156. //查询当前报警类型是否正在报警 fault_state 1: 未处理 2: 故障正在处理中 3: 处理完成
  1157. sprintf(querySql,"select * from dev_fault where device_mac = '%s' and device_mac_node = '%s' and relay_code = %d and (fault_state = 1 or fault_state = 2)",mac,subMac,relayCode);
  1158. excuteSql(_db,querySql);
  1159. res = mysql_store_result(_db);
  1160. queryRow = mysql_num_rows(res);
  1161. if (res && queryRow>0)
  1162. {
  1163. //debug("res && queryRow>0 querySql:%s",querySql);
  1164. mysql_free_result(res);
  1165. return 1;
  1166. }
  1167. mysql_free_result(res);
  1168. //查询网关节点名称
  1169. if (strcmp(subMac,"0F0000000001") == 0)//网关
  1170. {
  1171. sprintf(querySql,"select device_name from dev_info_gateway where device_mac = '%s' ",mac);
  1172. excuteSql(_db,querySql);
  1173. res = mysql_store_result(_db);
  1174. if (NULL == res)
  1175. {
  1176. return 1;
  1177. }
  1178. row = mysql_fetch_row(res);
  1179. if (NULL == row)
  1180. {
  1181. mysql_free_result(res);
  1182. return 1;
  1183. }
  1184. if(!row[0])
  1185. {
  1186. debug("device_name 为空");
  1187. mysql_free_result(res);
  1188. return 1;
  1189. }
  1190. strcpy(macName,row[0]);
  1191. sprintf(strName,"设备名称:%s",macName);
  1192. mysql_free_result(res);
  1193. }
  1194. else
  1195. {
  1196. sprintf(querySql,"select a.device_name as 'gateway_name',b.device_name as 'node_name' from dev_info_gateway a,dev_info_node b "
  1197. "where a.device_mac = b.gateway_mac and b.gateway_mac = '%s' and b.device_mac = '%s'",mac,subMac);
  1198. excuteSql(_db,querySql);
  1199. res = mysql_store_result(_db);
  1200. if (NULL == res)
  1201. {
  1202. return 1;
  1203. }
  1204. row = mysql_fetch_row(res);
  1205. if (NULL == row)
  1206. {
  1207. mysql_free_result(res);
  1208. return 1;
  1209. }
  1210. if(!row[0] || !row[1])
  1211. {
  1212. debug("gateway_name 或者 node_name 为空 ");
  1213. mysql_free_result(res);
  1214. return 1;
  1215. }
  1216. strcpy(macName,row[0]);
  1217. strcpy(subMacName,row[1]);
  1218. mysql_free_result(res);
  1219. sprintf(strName,"设备:%s,节点:%s",macName,subMacName);
  1220. }
  1221. //判断报警类型
  1222. if(relayCode == SW_TEMPERATURE_HUMIDITY_ALARM)
  1223. {
  1224. strcpy(strAlarmType,"温湿度报警拉闸");
  1225. }
  1226. else if(relayCode == SW_FIRE_ALARM)
  1227. {
  1228. strcpy(strAlarmType,"烟感报警拉闸");
  1229. }
  1230. else if(relayCode == SW_TILT_ALARM)
  1231. {
  1232. strcpy(strAlarmType,"倾斜报警拉闸");
  1233. }
  1234. else if(relayCode == SW_LOCK_ALARM)
  1235. {
  1236. strcpy(strAlarmType,"锁具报警拉闸");
  1237. }
  1238. else if(relayCode == SW_LINE_TEMPERATURE_ALARM)
  1239. {
  1240. strcpy(strAlarmType,"线温报警拉闸");
  1241. }
  1242. else if(relayCode == SW_OVERVOLTAGE)
  1243. {
  1244. strcpy(strAlarmType,"过压报警拉闸");
  1245. }
  1246. else if(relayCode == SW_UNDERVOLTAGE)
  1247. {
  1248. strcpy(strAlarmType,"欠压报警拉闸");
  1249. }
  1250. else if(relayCode == SW_OVERLOAD_ALARM)
  1251. {
  1252. strcpy(strAlarmType,"过载报警拉闸");
  1253. }
  1254. else if(relayCode == SW_ELECTRIC_EXCESS)
  1255. {
  1256. strcpy(strAlarmType,"用电超额报警拉闸");
  1257. }
  1258. else if(relayCode == SW_OVER_SWITCH_OUT_COUNT)
  1259. {
  1260. strcpy(strAlarmType,"超自动合闸次数报警拉闸");
  1261. }
  1262. else if(relayCode == SW_FAST_CURRENT)
  1263. {
  1264. strcpy(strAlarmType,"快速电流报警拉闸");
  1265. }
  1266. else
  1267. {
  1268. debug("无法识别报警!");
  1269. return 1;
  1270. }
  1271. //格式化报警内容
  1272. sprintf(strAlarm,"%s,%s,时间:%s \n",strName,strAlarmType,GetCurrentTime());
  1273. log(strAlarm);
  1274. //更新到报警故障表
  1275. sprintf(updateSql,"update dev_fault set fault_details = '%s',fault_state = 1,fault_time = now() where "
  1276. "device_mac = '%s' and device_mac_node = '%s' and relay_code = %d",strAlarm,mac,subMac,relayCode);
  1277. excuteSql(_db,updateSql);
  1278. affectedRows = mysql_affected_rows(_db);
  1279. if (affectedRows < 1)
  1280. {
  1281. sprintf(insertSql,"insert into dev_fault(device_mac,device_mac_node,fault_details,relay_code,fault_state,fault_time)values('%s','%s','%s',%d,1,now())",
  1282. mac,subMac,strAlarm,relayCode);
  1283. excuteSql(_db,insertSql);
  1284. }
  1285. return 0;
  1286. }
  1287. //比较两个时间字符串的大小
  1288. int compareTimes(const char *time1, const char *time2)
  1289. {
  1290. struct tm tm_base, tm_time1, tm_time2;
  1291. // 设置基准日期为当前日期
  1292. time_t current_time = time(NULL);
  1293. localtime_r(&current_time, &tm_base);
  1294. // 解析时间字符串1
  1295. if (strptime(time1, "%H:%M", &tm_time1) == 0)
  1296. {
  1297. fprintf(stderr, "Failed to parse time1\n");
  1298. return 0; // or handle the error in an appropriate way
  1299. }
  1300. // 解析时间字符串2
  1301. if (strptime(time2, "%H:%M", &tm_time2) == 0)
  1302. {
  1303. fprintf(stderr, "Failed to parse time2\n");
  1304. return 0; // or handle the error in an appropriate way
  1305. }
  1306. // 设置日期部分为基准日期的日期部分
  1307. tm_time1.tm_year = tm_base.tm_year;
  1308. tm_time1.tm_mon = tm_base.tm_mon;
  1309. tm_time1.tm_mday = tm_base.tm_mday;
  1310. tm_time2.tm_year = tm_base.tm_year;
  1311. tm_time2.tm_mon = tm_base.tm_mon;
  1312. tm_time2.tm_mday = tm_base.tm_mday;
  1313. // 将 struct tm 结构转换为 time_t 类型
  1314. time_t t1 = mktime(&tm_time1);
  1315. time_t t2 = mktime(&tm_time2);
  1316. // 检查转换是否成功
  1317. if (t1 == -1 || t2 == -1)
  1318. {
  1319. fprintf(stderr, "Failed to convert time to time_t\n");
  1320. return 0; // or handle the error in an appropriate way
  1321. }
  1322. // 比较时间并返回相应的结果
  1323. if (t1 < t2)
  1324. {
  1325. return -1;
  1326. } else if (t1 > t2)
  1327. {
  1328. return 1;
  1329. } else
  1330. {
  1331. return 0;
  1332. }
  1333. }
  1334. //比较两个时间结构体的的大小
  1335. int compareModifiedTimes(struct tm t1, struct tm t2)
  1336. {
  1337. // 将时间结构体转换为秒数
  1338. time_t time1 = mktime(&t1);
  1339. time_t time2 = mktime(&t2);
  1340. // 比较秒数
  1341. if (time1 < time2) {
  1342. return -1;
  1343. } else if (time1 > time2) {
  1344. return 1;
  1345. } else {
  1346. return 0;
  1347. }
  1348. }
  1349. //比较给定时间和系统当前时间的函数
  1350. int compareWithCurrentTime(struct tm t1)
  1351. {
  1352. // 获取系统当前时间
  1353. time_t currentTime;
  1354. time(&currentTime);
  1355. // 将给定时间结构体转换为秒数
  1356. time_t givenTime = mktime(&t1);
  1357. // 比较秒数
  1358. if (givenTime < currentTime) {
  1359. return -1;
  1360. } else if (givenTime > currentTime) {
  1361. return 1;
  1362. } else {
  1363. return 0;
  1364. }
  1365. }
  1366. //更新时间和拉合闸的时间点比较
  1367. int compareWithUpdateTime(const char *timeString,const char *time1,const char *time2,const char *time3,const char *time4)
  1368. {
  1369. struct tm tm_base, tm_timeString, tm_time1, tm_time2,tm_time3, tm_time4;
  1370. // 设置基准日期为当前日期
  1371. time_t current_time = time(NULL);
  1372. localtime_r(&current_time, &tm_base);
  1373. // 解析timeString
  1374. if (strptime(timeString, "%H:%M", &tm_timeString) == 0)
  1375. {
  1376. fprintf(stderr, "Failed to parse tm_timeString\n");
  1377. return 0; // or handle the error in an appropriate way
  1378. }
  1379. // 设置日期部分为基准日期的日期部分
  1380. tm_timeString.tm_year = tm_base.tm_year;
  1381. tm_timeString.tm_mon = tm_base.tm_mon;
  1382. tm_timeString.tm_mday = tm_base.tm_mday;
  1383. // 将 struct tm 结构转换为 time_t 类型
  1384. time_t t0 = mktime(&tm_timeString);//重新计算时间
  1385. // 解析时间字符串1
  1386. if (strlen(time1)==5 && strptime(time1, "%H:%M", &tm_time1))
  1387. {
  1388. tm_time1.tm_year = tm_base.tm_year;
  1389. tm_time1.tm_mon = tm_base.tm_mon;
  1390. tm_time1.tm_mday = tm_base.tm_mday;
  1391. time_t t1 = mktime(&tm_time1);
  1392. // 计算两个时间的差值
  1393. double timeDifference = difftime(t0,t1);
  1394. debug("time1:%s和更新时间:%s,差值为%.2f\n",time1,timeString,timeDifference);
  1395. if(fabs(timeDifference)<120.0)//更新时间在2分钟内
  1396. {
  1397. debug("time1更新时间在2分钟内\n");
  1398. return 1;
  1399. }
  1400. }
  1401. // 解析时间字符串2
  1402. if (strlen(time2)==5 && strptime(time2, "%H:%M", &tm_time2))
  1403. {
  1404. tm_time2.tm_year = tm_base.tm_year;
  1405. tm_time2.tm_mon = tm_base.tm_mon;
  1406. tm_time2.tm_mday = tm_base.tm_mday;
  1407. time_t t2 = mktime(&tm_time2);
  1408. // 计算两个时间的差值
  1409. double timeDifference = difftime(t0,t2);
  1410. debug("time2:%s和更新时间:%s,差值为%.2f\n",time2,timeString,timeDifference);
  1411. if(fabs(timeDifference)<120.0)//更新时间在2分钟内
  1412. {
  1413. debug("time2更新时间在2分钟内\n");
  1414. return 1;
  1415. }
  1416. }
  1417. // 解析时间字符串3
  1418. if (strlen(time3)==5 && strptime(time3, "%H:%M", &tm_time3))
  1419. {
  1420. tm_time3.tm_year = tm_base.tm_year;
  1421. tm_time3.tm_mon = tm_base.tm_mon;
  1422. tm_time3.tm_mday = tm_base.tm_mday;
  1423. time_t t3 = mktime(&tm_time3);
  1424. // 计算两个时间的差值
  1425. double timeDifference = difftime(t0,t3);
  1426. debug("time3:%s和更新时间:%s,差值为%.2f\n",time3,timeString,timeDifference);
  1427. if(fabs(timeDifference)<120.0)//更新时间在2分钟内
  1428. {
  1429. debug("time3更新时间在2分钟内\n");
  1430. return 1;
  1431. }
  1432. }
  1433. // 解析时间字符串4
  1434. if (strlen(time4)==5 && strptime(time4, "%H:%M", &tm_time4))
  1435. {
  1436. tm_time4.tm_year = tm_base.tm_year;
  1437. tm_time4.tm_mon = tm_base.tm_mon;
  1438. tm_time4.tm_mday = tm_base.tm_mday;
  1439. time_t t4 = mktime(&tm_time4);
  1440. // 计算两个时间的差值
  1441. double timeDifference = difftime(t0,t4);
  1442. debug("time4:%s和更新时间:%s,差值为%.2f\n",time4,timeString,timeDifference);
  1443. if(fabs(timeDifference)<120.0)//更新时间在2分钟内
  1444. {
  1445. debug("time4更新时间在2分钟内\n");
  1446. return 1;
  1447. }
  1448. }
  1449. return 0;
  1450. }
  1451. //将需要处理sql放入队列
  1452. void* sendtoqueue(ArrQueue *pQueue,char* stringSql)
  1453. {
  1454. ElemType *elememt = (ElemType *)malloc(sizeof(ElemType));
  1455. if(elememt==NULL)
  1456. {
  1457. printf("0 malloc(sizeof(ElemType)) \n");
  1458. exit(0);
  1459. }
  1460. memset(elememt,0,sizeof(ElemType));
  1461. if(strlen(stringSql)>1024)
  1462. {
  1463. printf("stringSql length is too long \n");
  1464. exit(0);
  1465. }
  1466. elememt->len = strlen(stringSql);
  1467. strcpy(elememt->buf,stringSql);
  1468. EnQueue(pQueue,elememt);
  1469. return NULL;
  1470. }
  1471. //循环从队列中取出数据处理
  1472. void* getqueue(void *data)
  1473. {
  1474. MYSQL *_db = NULL;
  1475. ElemType *elememt;
  1476. if (!_db)
  1477. {
  1478. // 先申请内存再初始化
  1479. _db = malloc(sizeof(MYSQL));
  1480. db_init(_db);
  1481. }
  1482. struct my_thread_info *info = data;
  1483. while(1)
  1484. {
  1485. usleep(1000*1);
  1486. DeQueue(m_pque,&elememt);
  1487. if(elememt == NULL)
  1488. {
  1489. printf("elememt == NULL,number:%d\n",info->which);
  1490. continue;
  1491. }
  1492. excuteSql(_db,elememt->buf);
  1493. free(elememt);
  1494. //log("getqueue sql number:%d \n",info->which);
  1495. }
  1496. free(info);
  1497. return NULL;
  1498. }
  1499. //创建任务线程处理数据库执行
  1500. void CreateDbHandThread(void)
  1501. {
  1502. int rc;
  1503. long t;
  1504. struct my_thread_info *info;
  1505. sleep(1);
  1506. for (t = 0; t < NUM_THREADS_UPLOAD_g; t++)
  1507. {
  1508. info = malloc(sizeof(struct my_thread_info));
  1509. info->which = t;
  1510. rc = pthread_create(&threads[t], NULL, getqueue, info);
  1511. if (rc)
  1512. {
  1513. printf("ERROR; return code from pthread_create() is %d\n", rc);
  1514. exit(-1);
  1515. }
  1516. printf("pthread_create() threadid:%lu is started \n",threads[t]);
  1517. sleep(1);
  1518. }
  1519. // for (t = 0; t < NUM_THREADS; t++)
  1520. // {
  1521. // pthread_join(threads[t], NULL);
  1522. // }
  1523. }
  1524. //打印队列状态,队列处理线程状态,10min打印一次
  1525. int PrintQueueThreadState(void)
  1526. {
  1527. if(timeGloble_g%600 == 0 && NUM_THREADS_UPLOAD_g>0)
  1528. {
  1529. int i = 0;
  1530. char strState[256] = {0};
  1531. int pthread_kill_err;
  1532. memset(strState,0x0,256);
  1533. sprintf(strState,"pid:%d,队列大小:%d",getpid(),GetLength(m_pque));
  1534. for(i=0;i<NUM_THREADS_UPLOAD_g;i++)
  1535. {
  1536. pthread_kill_err = pthread_kill(threads[i],0);
  1537. sprintf(strState,"%s,tid:%lu(%s)",strState,threads[i],(pthread_kill_err==0)?"alive":"death");
  1538. }
  1539. log("%s \n",strState);
  1540. }
  1541. return 0;
  1542. }
  1543. //关于远程升级
  1544. //生成CRC查询表
  1545. void init_crc32_tab(void)
  1546. {
  1547. int32_t i, j;
  1548. uint32_t crc;
  1549. if (have_table)
  1550. {
  1551. return;
  1552. }
  1553. have_table = 1;
  1554. for(i = 0; i < 256; i++)
  1555. {
  1556. crc = (uint32_t )i;
  1557. for(j = 0; j < 8; j++)
  1558. {
  1559. if(crc & 0X00000001L)
  1560. {
  1561. crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
  1562. }
  1563. else
  1564. {
  1565. crc = crc >> 1;
  1566. }
  1567. }
  1568. crc_table32[i] = crc;
  1569. }
  1570. }
  1571. //获得CRC
  1572. uint32_t get_crc32(uint32_t crcinit, uint8_t *bs, uint32_t bssize)
  1573. {
  1574. uint32_t crc = crcinit ^ 0XFFFFFFFF;
  1575. while(bssize--)
  1576. {
  1577. crc = (crc>>8)^crc_table32[(crc&0XFF)^*bs++];
  1578. }
  1579. return crc ^ 0XFFFFFFFF;
  1580. }
  1581. //根据mac查询对应升级文件名
  1582. char* GetFileNameByMac(list_node* u_list,char* mac)
  1583. {
  1584. list_node* list = u_list;
  1585. if(list == NULL) return NULL;
  1586. while(list)
  1587. {
  1588. upgrade_user_info* info = (upgrade_user_info*)list->data;
  1589. if(strcmp(info->mac,mac) == 0)
  1590. {
  1591. return info->ota_file_name;
  1592. }
  1593. list = list->next;
  1594. }
  1595. return NULL;
  1596. }
  1597. //根据文件名和包序号查询分包数据
  1598. ota_info* GetOtaInfoByFileName(list_node* f_list,char* filename)
  1599. {
  1600. list_node* list = f_list;
  1601. if(list == NULL) return NULL;
  1602. while(list)
  1603. {
  1604. ota_info* info = (ota_info*)list->data;
  1605. if(strcmp(info->new_file_name,filename) == 0)
  1606. {
  1607. return info;
  1608. }
  1609. list = list->next;
  1610. }
  1611. return NULL;
  1612. }
  1613. /******************************************************************
  1614. * Function Name: UpgradeFileAddFromDB
  1615. * Arguments:
  1616. * Return Value: void
  1617. * Date: 2022-04-02
  1618. * Editor: chw
  1619. * Description: 新增升级文件映射节点
  1620. ******************************************************************/
  1621. void UpgradeFileAddFromDB(list_node* f_list,char* filename,MYSQL *_db,int ota_mode)
  1622. {
  1623. char logstr[512] = {0};
  1624. unsigned short ota_file_total_packets = 0;
  1625. int file_length_bytes = 0;
  1626. unsigned int ota_length_bytes = 0;
  1627. list_node* list = f_list;
  1628. ota_info* info = (ota_info*)malloc(sizeof(ota_info));
  1629. memset(info,0x0,sizeof(ota_info));
  1630. ota_info* s = GetOtaInfoByFileName(file_list,filename);
  1631. if(s != NULL)
  1632. {
  1633. log("有文件节点,不新增! \n");
  1634. free(info);
  1635. info = NULL;
  1636. return;
  1637. }
  1638. //读取数据库升级文件
  1639. unsigned char *fp = readfile_from_db(filename,_db,&file_length_bytes);
  1640. if (file_length_bytes <= 0)
  1641. {
  1642. log("%d file_length_bytes <= 0 ! \n",file_length_bytes);
  1643. free(info);
  1644. info = NULL;
  1645. return;
  1646. }
  1647. //计算数据库升级文件crc32校验
  1648. init_crc32_tab();
  1649. uint32_t crc_value = 0;
  1650. if(calc_crc32_from_buff((const char*)fp,file_length_bytes,&crc_value) == 0)
  1651. {
  1652. log("crc_value = %x\n",crc_value);
  1653. }
  1654. //全量
  1655. if(ota_mode == 1)
  1656. {
  1657. ota_length_bytes = file_length_bytes;
  1658. memcpy(info->buf,fp,file_length_bytes);
  1659. crc_value = get_crc32(0,info->buf,file_length_bytes);//ota文件crc校验
  1660. info->ota_mode = 1;//模式
  1661. strcpy(info->new_file_name,filename);//新文件名
  1662. info->new_file_len = (uint32_t)file_length_bytes;//新文件长度
  1663. info->ota_file_len = (uint32_t)file_length_bytes;//升级文件长度
  1664. info->new_file_crc = crc_value;//新文件crc32校验
  1665. info->ota_file_crc = info->new_file_crc;//升级文件crc32校验
  1666. ota_file_total_packets = (uint32_t)file_length_bytes/ota_file_packet_len + 1;
  1667. info->ota_file_total_packets = ota_file_total_packets;//升级文件总包数
  1668. info->ota_file_packet_len = ota_file_packet_len;//升级文件每包数据长度
  1669. sprintf(logstr,"新文件名:%s,新文件长度:%d,升级文件长度:%d,升级文件总包数:%d,升级文件每包数据长度:%d,新文件crc32校验:%X,升级文件crc32校验:%X \n",
  1670. info->new_file_name,info->new_file_len,info->ota_file_len,info->ota_file_total_packets,info->ota_file_packet_len,info->new_file_crc,info->ota_file_crc);
  1671. log(logstr);
  1672. }
  1673. //差分
  1674. else
  1675. {
  1676. ota_length_bytes = (unsigned int)file_length_bytes;
  1677. memcpy(info->buf,fp,ota_length_bytes);
  1678. crc_value = get_crc32(0,info->buf,ota_length_bytes);//ota文件crc校验
  1679. info->ota_mode = 0;//模式
  1680. memcpy(info->old_file_name,fp,48);//旧文件名
  1681. info->old_file_len = INT_FROM_BUFF(fp,48);//旧文件长度
  1682. info->old_file_crc = INT_FROM_BUFF(fp,52);//旧文件crc32 校验值
  1683. memcpy(info->new_file_name,fp+56,48);//新文件名
  1684. info->new_file_len = INT_FROM_BUFF(fp,104);//新文件的长度
  1685. info->new_file_crc = INT_FROM_BUFF(fp,108);//新文件crc32 校验值
  1686. info->ota_state = (char)0;//升级状态
  1687. info->ota_file_len = ota_length_bytes;//ota 文件的长度
  1688. info->ota_file_crc = crc_value;//ota 文件的crc32校验值
  1689. info->ota_file_total_packets = ota_length_bytes/ota_file_packet_len + 1;//ota文件的总的包数
  1690. info->ota_file_packet_len = ota_file_packet_len;//ota文件每包的数据长度
  1691. log("info->buf 模式:%d,旧文件名:%s,旧文件长度:%d,旧文件crc32:%X,新文件名:%s,新文件的长度:%d,新文件crc32:%X,ota状态:%d,ota文件的长度:%d,"
  1692. "ota文件的crc32:%X,ota文件的总的包数:%d,ota文件每包的数据长度:%d \n", info->ota_mode,info->old_file_name,info->old_file_len,
  1693. info->old_file_crc,info->new_file_name,info->new_file_len,info->new_file_crc,info->ota_state,info->ota_file_len,info->ota_file_crc,
  1694. info->ota_file_total_packets,info->ota_file_packet_len);
  1695. }
  1696. if(fp != NULL)
  1697. {
  1698. free(fp);
  1699. fp = NULL;
  1700. }
  1701. //第一个文件
  1702. if(list == NULL)
  1703. {
  1704. file_list = list_create((void*)info);
  1705. log("当前为文件映射第一个节点 \n");
  1706. }
  1707. //没有文件节点
  1708. else
  1709. {
  1710. list_insert_end(file_list,(void*)info);
  1711. log("没有文件节点,加入! \n");
  1712. }
  1713. return;
  1714. }
  1715. //数据库查询升级文件名
  1716. void GetUpgradeFileFromDB(char *mac,MYSQL *_db,char* filename)
  1717. {
  1718. MYSQL_RES* res = NULL;
  1719. MYSQL_ROW row;
  1720. char query[1024] = {0};
  1721. char ota_file[256] = {0};
  1722. int ota_mode = 0;
  1723. sprintf(query,"select file_name,ota_mode from dev_info_gateway where device_mac ='%s'",mac);
  1724. excuteSql(_db,query);
  1725. res = mysql_store_result(_db);
  1726. if (NULL == res)
  1727. {
  1728. return ;
  1729. }
  1730. row = mysql_fetch_row(res);
  1731. if (NULL == row)
  1732. {
  1733. mysql_free_result(res);
  1734. return ;
  1735. }
  1736. if(!row[0] || !row[1])
  1737. {
  1738. log("%s file_name、ota_mode为空 \n",mac);
  1739. mysql_free_result(res);
  1740. return ;
  1741. }
  1742. strcpy(ota_file,row[0]);
  1743. ota_mode = atoi(row[1]);
  1744. strcpy(filename,ota_file);
  1745. mysql_free_result(res);
  1746. //log("mac:%s,file_name:%s \n",mac,ota_file);
  1747. return ;
  1748. }
  1749. /******************************************************************
  1750. * Function Name: readfile_from_db
  1751. * Arguments:
  1752. * Return Value: void
  1753. * Date: 2022-04-29
  1754. * Editor: chw
  1755. * Description: 从数据库读取升级文件
  1756. ******************************************************************/
  1757. unsigned char *readfile_from_db(const char *filename, MYSQL *_db,int *file_length_bytes)
  1758. {
  1759. //MYSQL_RES* res = NULL;
  1760. //MYSQL_ROW row;
  1761. char sql[512] = {0};
  1762. int ret = 0;
  1763. //unsigned long *lengths;
  1764. unsigned char* data;
  1765. MYSQL_BIND result;
  1766. MYSQL_STMT* stmt = mysql_stmt_init(_db);
  1767. assert(NULL!=stmt);
  1768. sprintf(sql,"select file_src from dev_upgrade_file where file_name ='%s'",filename);
  1769. ret = mysql_stmt_prepare(stmt, sql, strlen(sql));
  1770. assert(0==ret);
  1771. ret = mysql_stmt_execute(stmt);
  1772. assert(0==ret);
  1773. memset(&result,0x0,sizeof(result));
  1774. unsigned long total_length = 0;
  1775. result.buffer_type = MYSQL_TYPE_LONG_BLOB;
  1776. result.length = &total_length;
  1777. ret = mysql_stmt_bind_result(stmt, &result);
  1778. assert(0==ret);
  1779. ret = mysql_stmt_store_result(stmt);
  1780. assert(0==ret);
  1781. ret = mysql_stmt_fetch(stmt);
  1782. if(total_length <= 0)
  1783. {
  1784. log("total_length <= 0!\n");
  1785. mysql_stmt_close(stmt);
  1786. return NULL;
  1787. }
  1788. unsigned long start = 0;
  1789. //char buf[1024] = {0};
  1790. //lengths = mysql_fetch_lengths(stmt);
  1791. data = (unsigned char*)malloc(total_length);
  1792. *file_length_bytes = total_length;
  1793. if(data == NULL)
  1794. {
  1795. log("malloc data failed!");
  1796. return NULL;
  1797. }
  1798. log("readfile_from_db函数,total_length=%lu\n", total_length);
  1799. while (start<total_length)
  1800. {
  1801. result.buffer = (data+start);
  1802. if(total_length < 1024)
  1803. {
  1804. result.buffer_length = total_length;
  1805. }
  1806. else if((start+1024) < total_length)
  1807. {
  1808. result.buffer_length = 1024;
  1809. }
  1810. else
  1811. {
  1812. result.buffer_length = total_length % 1024;
  1813. }
  1814. ret = mysql_stmt_fetch_column(stmt, &result, 0, start);
  1815. if (ret!=0)
  1816. {
  1817. log("code=%d, msg=%s", (int)mysql_errno(_db), mysql_error(_db));
  1818. free(data);
  1819. mysql_stmt_close(stmt);
  1820. return NULL;
  1821. }
  1822. start += result.buffer_length;
  1823. }
  1824. //log("%.*s\n", total_length, data);
  1825. //free(data);
  1826. mysql_stmt_close(stmt);
  1827. return data;
  1828. }
  1829. /******************************************************************
  1830. * Function Name: 获取缓存中的crc32
  1831. * Arguments:
  1832. * Return Value: void
  1833. * Date: 2022-04-29
  1834. * Editor: chw
  1835. * Description: 获取缓存中的crc32
  1836. ******************************************************************/
  1837. int32_t calc_crc32_from_buff(const char *buff,int len, uint32_t *uiCrcValue)
  1838. {
  1839. const uint32_t size = 2*1024;
  1840. uint32_t crc = 0; // CRC初始值为0
  1841. int remain = len; //剩余未计算长度
  1842. int index = 0;
  1843. while(remain>0)
  1844. {
  1845. if(remain > (int)size)
  1846. {
  1847. crc = get_crc32(crc, (uint8_t*)(buff + index), size);
  1848. index += size;
  1849. remain = remain-(int)size;
  1850. }
  1851. else
  1852. {
  1853. crc = get_crc32(crc, (uint8_t*)(buff + index), remain);
  1854. index += remain;
  1855. remain = 0;
  1856. }
  1857. }
  1858. *uiCrcValue = crc;
  1859. return 0;
  1860. }
  1861. #ifdef UPGRADE_BY_FILE
  1862. //获取文件长度
  1863. int GetFileLength(char* file_path)
  1864. {
  1865. FILE* pFile = fopen(file_path,"r");
  1866. if(pFile == NULL)
  1867. {
  1868. return -1;
  1869. }
  1870. fseek(pFile,0,SEEK_END);
  1871. unsigned int file_length_bytes = ftell(pFile);
  1872. fclose(pFile);
  1873. pFile = NULL;
  1874. return file_length_bytes;
  1875. }
  1876. //获得文件CRC
  1877. int32_t calc_file_crc32(const char *pFileName, uint32_t *uiCrcValue)
  1878. {
  1879. if(!pFileName || !uiCrcValue)
  1880. {
  1881. log("bad parameter\n");
  1882. return -1;
  1883. }
  1884. if(access(pFileName, F_OK|R_OK)!=0)
  1885. {
  1886. log("file not exist or reading file has not permission\n");
  1887. return -1;
  1888. }
  1889. const uint32_t size = 2*1024;
  1890. uint8_t crcbuf[size];
  1891. uint32_t rdlen;
  1892. uint32_t crc = 0; // CRC初始值为0
  1893. FILE *fd = NULL;
  1894. if((fd = fopen(pFileName, "r"))==NULL)
  1895. {
  1896. log("to do crc 32 check, open file error\n");
  1897. return -1;
  1898. }
  1899. while((rdlen=fread(crcbuf, sizeof(uint8_t), size, fd))>0)
  1900. {
  1901. crc = get_crc32(crc, crcbuf, rdlen);
  1902. }
  1903. log("calc_file_crc32:%X \n",crc);
  1904. *uiCrcValue = crc;
  1905. fclose(fd);
  1906. return 0;
  1907. }
  1908. //读取文件
  1909. // unsigned char *read_myfile(const char *path, int *file_size)
  1910. // {
  1911. // if(!path||!file_size)
  1912. // {
  1913. // log("bad parameter\n");
  1914. // return NULL;
  1915. // }
  1916. // //access
  1917. // if(access(path, F_OK|R_OK))
  1918. // {
  1919. // log("access test file error\n");
  1920. // return NULL;
  1921. // }
  1922. // //read file
  1923. // struct stat calib_stat;
  1924. // int ret = stat(path, &calib_stat);
  1925. // if(ret!=0)
  1926. // {
  1927. // log("File error\n");
  1928. // return NULL;
  1929. // }
  1930. // FILE *fd = fopen(path, "r");
  1931. // if(NULL == fd)
  1932. // {
  1933. // log("Open test file error\n");
  1934. // return NULL;
  1935. // }
  1936. // unsigned char *buffer = malloc(calib_stat.st_size);
  1937. // if(!buffer)
  1938. // {
  1939. // log("malloc error\n");
  1940. // fclose(fd);
  1941. // return NULL;
  1942. // }
  1943. // memset(buffer, 0, calib_stat.st_size);
  1944. // int totalSize = 0, readed = 0;
  1945. // while((readed = fread(buffer + totalSize,1, 1024,fd)) > 0)
  1946. // {
  1947. // totalSize += readed;
  1948. // }
  1949. // *file_size = totalSize;
  1950. // fclose(fd);
  1951. // return buffer;
  1952. // }
  1953. // //增加用户
  1954. // void UpgradeUserAdd(list_node* u_list,char* mac,char* file_name)
  1955. // {
  1956. // list_node* list = u_list;
  1957. // upgrade_user_info* info = (upgrade_user_info*)malloc(sizeof(upgrade_user_info));
  1958. // strcpy(info->mac,mac);
  1959. // strcpy(info->ota_file_name,file_name);
  1960. // //第一个用户
  1961. // if(list == NULL)
  1962. // {
  1963. // user_list = list_create((void*)info);
  1964. // }
  1965. // else
  1966. // {
  1967. // //没查询到用户
  1968. // if(GetFileNameByMac(u_list,mac) == NULL)
  1969. // {
  1970. // list_insert_end(u_list,(void*)info);
  1971. // }
  1972. // //查询到用户,修改
  1973. // else
  1974. // {
  1975. // while(list)
  1976. // {
  1977. // upgrade_user_info* old_info = (upgrade_user_info*)list->data;
  1978. // if(strcmp(old_info->mac,mac) == 0)
  1979. // {
  1980. // list->data = (void*)info;
  1981. // free(old_info);
  1982. // old_info = NULL;
  1983. // break;
  1984. // }
  1985. // list = list->next;
  1986. // }
  1987. // }
  1988. // }
  1989. // return;
  1990. // }
  1991. // //新增升级文件映射节点
  1992. // void UpgradeFileAdd(list_node* f_list,char* filename,int ota_mode)
  1993. // {
  1994. // char file_path[256];
  1995. // char logstr[512];
  1996. // unsigned short ota_file_total_packets = 0;
  1997. // unsigned int file_length_bytes = 0;
  1998. // unsigned int ota_length_bytes = 0;
  1999. // list_node* list = f_list;
  2000. // ota_info* info = (ota_info*)malloc(sizeof(ota_info));
  2001. // memset(info,0x0,sizeof(ota_info));
  2002. // sprintf(file_path,"/home/cao/ota/%s",filename);
  2003. // //计算文件crc32校验
  2004. // init_crc32_tab();
  2005. // uint32_t crc_value = 0;
  2006. // if(calc_file_crc32(file_path, &crc_value) == 0)
  2007. // {
  2008. // log("file_path:%s,crc_value = %x\n", file_path,crc_value);
  2009. // }
  2010. // //读取文件
  2011. // unsigned char *fp = read_myfile(file_path,(int*)&file_length_bytes);
  2012. // if (file_length_bytes <= 0)
  2013. // {
  2014. // log("%s file_length_bytes <= 0 ! \n",file_path);
  2015. // free(info);
  2016. // info = NULL;
  2017. // return;
  2018. // }
  2019. // //全量
  2020. // if(ota_mode == 1)
  2021. // {
  2022. // ota_length_bytes = file_length_bytes;
  2023. // memcpy(info->buf,fp,file_length_bytes);
  2024. // crc_value = get_crc32(0,info->buf,file_length_bytes);//ota文件crc校验
  2025. // info->ota_mode = 1;//模式
  2026. // strcpy(info->new_file_name,filename);//新文件名
  2027. // info->new_file_len = file_length_bytes;//新文件长度
  2028. // info->ota_file_len = file_length_bytes;//升级文件长度
  2029. // info->new_file_crc = crc_value;//新文件crc32校验
  2030. // info->ota_file_crc = info->new_file_crc;//升级文件crc32校验
  2031. // ota_file_total_packets = file_length_bytes/ota_file_packet_len + 1;
  2032. // info->ota_file_total_packets = ota_file_total_packets;//升级文件总包数
  2033. // info->ota_file_packet_len = ota_file_packet_len;//升级文件每包数据长度
  2034. // sprintf(logstr,"新文件名:%s,新文件长度:%d,升级文件长度:%d,升级文件总包数:%d,升级文件每包数据长度:%d,新文件crc32校验:%X,升级文件crc32校验:%X \n",
  2035. // info->new_file_name,info->new_file_len,info->ota_file_len,info->ota_file_total_packets,info->ota_file_packet_len,info->new_file_crc,info->ota_file_crc);
  2036. // log(logstr);
  2037. // }
  2038. // //差分
  2039. // else
  2040. // {
  2041. // ota_length_bytes = file_length_bytes-130;
  2042. // memcpy(info->buf,fp+130,ota_length_bytes);
  2043. // //crc_value = get_crc32(0,info->buf,file_length_bytes-130);//ota文件crc校验
  2044. // info->ota_mode = 0;//模式
  2045. // memcpy(info->old_file_name,fp+1,48);//旧文件名
  2046. // info->old_file_len = INT_FROM_BUFF(fp,49);//旧文件长度
  2047. // info->old_file_crc = INT_FROM_BUFF(fp,53);//旧文件crc32 校验值
  2048. // memcpy(info->new_file_name,fp+58,48);//新文件名
  2049. // info->new_file_len = INT_FROM_BUFF(fp,105);//新文件的长度
  2050. // info->new_file_crc = INT_FROM_BUFF(fp,109);//新文件crc32 校验值
  2051. // info->ota_state = (char)*(fp+113);//ota 状态
  2052. // info->ota_file_len = INT_FROM_BUFF(fp,114);//ota 文件的长度
  2053. // info->ota_file_crc = INT_FROM_BUFF(fp,118);//ota 文件的crc32校验值
  2054. // info->ota_file_total_packets = SHORT_FROM_BUFF(fp,122);//ota文件的总的包数
  2055. // info->ota_file_packet_len = SHORT_FROM_BUFF(fp,124);//ota文件每包的数据长度
  2056. // info->boot_addr = INT_FROM_BUFF(fp,126);//app 程序的启动地址
  2057. // log("info->buf 模式:%d,旧文件名:%s,旧文件长度:%d,旧文件crc32:%X,新文件名:%s,新文件的长度:%d,新文件crc32:%X,ota状态:%d,ota文件的长度:%d,"
  2058. // "ota文件的crc32:%X,ota文件的总的包数:%d,ota文件每包的数据长度:%d,app程序的启动地址:%X \n", info->ota_mode,info->old_file_name,info->old_file_len,
  2059. // info->old_file_crc,info->new_file_name,info->new_file_len,info->new_file_crc,info->ota_state,info->ota_file_len,info->ota_file_crc,
  2060. // info->ota_file_total_packets,info->ota_file_packet_len,info->boot_addr);
  2061. // }
  2062. // //log("info->buf crc = %x ,file_length_bytes = %d,ota_length_bytes = %d \n", crc_value,file_length_bytes,ota_length_bytes);
  2063. // if(fp != NULL)
  2064. // {
  2065. // free(fp);
  2066. // }
  2067. // //第一个文件
  2068. // if(list == NULL)
  2069. // {
  2070. // file_list = list_create((void*)info);
  2071. // log("当前为文件映射第一个节点 \n");
  2072. // }
  2073. // else
  2074. // {
  2075. // ota_info* s = GetOtaInfoByFileName(file_list,filename);
  2076. // if(s == NULL)
  2077. // {
  2078. // list_insert_end(file_list,(void*)info);
  2079. // log("没有文件节点,加入! \n");
  2080. // }
  2081. // else
  2082. // {
  2083. // log("有文件节点,不新增! \n");
  2084. // }
  2085. // }
  2086. // return;
  2087. // }
  2088. #endif