pw_quality.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: pw_quality.c
  4. 文件版本: 01.01
  5. 创建作者:xxxxxx
  6. 创建日期: 2014-09-04
  7. 功能说明: DTU电能质量处理函数
  8. 其它说明:
  9. 修改记录:
  10. */
  11. /*------------------------------- 头文件 --------------------------------------
  12. */
  13. #include "head.h"
  14. /*------------------------------- 宏定义 --------------------------------------
  15. */
  16. #define FILE_VERSION 0X01010101 //文件版本
  17. enum
  18. {
  19. ENERGY_FILE, //电度文件
  20. EXTREMA_FILE, //极值记录文件
  21. MEARCD_FILE, //定点遥测记录文件
  22. CAP_FILE, //电容器管理文件
  23. UDCRCD_FILE, //电池电压记录文件
  24. FILE_NUM
  25. };
  26. enum
  27. {
  28. YEAR=1,
  29. MONTH,
  30. DAY,
  31. };
  32. /*------------------------------ 全局变量 -------------------------------------
  33. */
  34. struct pw_quality_file
  35. {
  36. s8 * name;
  37. int offset;
  38. struct file * handle;
  39. };
  40. struct pw_quality_file g_pw_quality_file[] =
  41. {
  42. {"/app/data/energy.bin", 0,0},
  43. {"/app/data/extrema.bin", 0,0},
  44. {"/app/data/mearcd.bin", 0,0},
  45. {"/app/data/cap.bin", 0,0},
  46. {"/app/data/udcrcd.bin", 0,0},
  47. };
  48. struct pw_quality g_energy;
  49. struct pw_quality g_extrema;
  50. struct pw_quality g_mearcd;
  51. struct pw_quality g_capmanage;
  52. struct pw_quality g_udcrcd;
  53. struct energy_rcd g_energy_rcd[2];
  54. struct extrema_rcd g_extrema_rcd[2];
  55. struct mea_rcd g_mea_rcd[2];
  56. struct cap_rcd g_cap_rcd[2];
  57. struct udc_rcd g_udc_rcd[2];
  58. int g_pw_init;
  59. /*------------------------------ 函数声明 -------------------------------------
  60. */
  61. int energy_create_file(void);
  62. int extrema_create_file(void);
  63. int mearcd_create_file(void);
  64. int capmanage_create_file(void);
  65. int udcrcd_create_file(void);
  66. int pw_quality_file_init(u8 file_no);
  67. int pw_quality_file_rcd(u8 file_no, u32 index, u32 rcd_num, u32 len, u32 size, char *new_rcd, char *nul_rcd );
  68. static int _index_check(u32 num, u32 len, u8 *buf);
  69. static void _get_ac_name(u16 owner,u16 index,char *name);
  70. /*------------------------------ 外部函数 -------------------------------------
  71. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  72. */
  73. /******************************************************************************
  74. 函数名称:energy_init
  75. 函数版本: 01.01
  76. 创建作者:xxxxxx
  77. 创建日期: 2014-09-05
  78. 函数说明:电度初始化
  79. 参数说明:
  80. 返回值:
  81. 0: 成功
  82. 其它: 失败
  83. 修改记录:
  84. */
  85. int energy_init(void)
  86. {
  87. int i;
  88. g_energy.change_set = 0;
  89. for(i=0;i<g_sw_num;i++)
  90. {
  91. if(g_ui[PUB_AC_NUM+SW_AC_IA+i*SW_AC_NUM].chn_index == CFG_ADC_CHANNEL_ZERO)
  92. {
  93. g_energy.sw_num = i;
  94. break;
  95. }
  96. }
  97. g_energy.len = FILE_ADDR_ALGIN(8 + g_energy.sw_num * 16);
  98. g_energy.mea_num = g_energy.sw_num;
  99. if (pRunSet->wDDRL)
  100. {
  101. g_energy.rcd_num = ((pRunSet->wDDRL * 24 * 60) / pRunSet->wDDJG) + 1;//7天,5~60分钟间隔
  102. }
  103. g_energy.delay_s = pRunSet->wDDJG * 60;
  104. memset(g_energy_rcd, 0, sizeof(g_energy_rcd));
  105. memset(&g_energy_rcd[0], 0xFF, sizeof(struct energy_rcd));
  106. i = pw_quality_file_init(ENERGY_FILE);
  107. if (i<0 && pRunSet->wDDRL)
  108. {
  109. rt_printf("iret1 = %d\r\n", i);
  110. i = energy_create_file();
  111. rt_file_del("/tmp/energy.csv");
  112. rt_printf("初始化电度文件, ret = %d\r\n", i);
  113. }
  114. //当开关数量、时间间隔定值有变动时,需重新创建文件
  115. return 0;
  116. }
  117. /******************************************************************************
  118. 函数名称:energy_create_file
  119. 函数版本: 01.01
  120. 创建作者:xxxxxx
  121. 创建日期: 2014-09-05
  122. 函数说明:创建电度记录文件
  123. 参数说明:
  124. 返回值:
  125. 0: 成功
  126. 其它: 失败
  127. 修改记录:
  128. */
  129. int energy_create_file(void)
  130. {
  131. unsigned int addr,file_length;
  132. char *p;
  133. loff_t pos;
  134. struct energy_file_head_data energy_file;
  135. // 写文件头
  136. memset(&energy_file,0,sizeof(energy_file));
  137. energy_file.cfh.signature = SIG_ENERGY_FILE;
  138. energy_file.cfh.version = FILE_VERSION;
  139. energy_file.interval = g_energy.delay_s;
  140. energy_file.size = pRunSet->wDDRL; //默认存储7天
  141. energy_file.swnum = g_energy.mea_num;
  142. energy_file.addr = FILE_ADDR_ALGIN(sizeof(energy_file));
  143. energy_file.num = g_energy.rcd_num;
  144. // 得到文件长度
  145. addr = energy_file.addr + energy_file.num * g_energy.len;
  146. file_length = FILE_ADDR_ALGIN(addr);
  147. // 分配并初始化空间
  148. p = rt_malloc(file_length);
  149. if(!p)
  150. {
  151. return -1;
  152. }
  153. memset(p,0,file_length);
  154. // 写入文件头
  155. memcpy(p, (char *)&energy_file, sizeof(energy_file));
  156. // 写电度记录
  157. memset(p+energy_file.addr, 0xFF, g_energy.len);//无效记录为全1
  158. // 创建数据文件
  159. g_pw_quality_file[ENERGY_FILE].handle = rt_file_open(g_pw_quality_file[ENERGY_FILE].name,O_CREAT|O_RDWR,0);
  160. if(IS_ERR(g_pw_quality_file[ENERGY_FILE].handle))
  161. {
  162. g_pw_quality_file[ENERGY_FILE].handle = 0;
  163. rt_printf("%s open error!\r\n",g_pw_quality_file[ENERGY_FILE].name);
  164. rt_free(p);
  165. return -2;
  166. }
  167. pos = 0;
  168. rt_file_write(g_pw_quality_file[ENERGY_FILE].handle,p,file_length,&pos);
  169. g_energy.index = 0;
  170. rt_free(p);
  171. return 0;
  172. }
  173. /******************************************************************************
  174. 函数名称:energy_calc
  175. 函数版本: 01.01
  176. 创建作者:xxxxxx
  177. 创建日期: 2014-09-05
  178. 函数说明:电度值计算
  179. 参数说明:
  180. 返回值:
  181. 0: 成功
  182. 其它: 失败
  183. 修改记录:
  184. */
  185. int energy_calc(void)
  186. {
  187. int i=0;
  188. float data_p, data_q;
  189. //算法
  190. for (i=0; i<g_energy.sw_num; i++)
  191. {
  192. data_p = (float)g_sw[i].ac_in[SW_AC_IN_P] / Q16_BASE;
  193. data_q = (float)g_sw[i].ac_in[SW_AC_IN_Q] / Q16_BASE;
  194. //计算时不断累加,满足间隔后取值清0
  195. if (data_p > 0)
  196. {
  197. g_energy_rcd[1].mea[i].energy_p_p += data_p;
  198. }
  199. else
  200. {
  201. g_energy_rcd[1].mea[i].energy_p_n += data_p;
  202. }
  203. if (data_q > 0)
  204. {
  205. g_energy_rcd[1].mea[i].energy_q_p += data_q;
  206. }
  207. else
  208. {
  209. g_energy_rcd[1].mea[i].energy_q_n += data_q;
  210. }
  211. }
  212. return 0;
  213. }
  214. /******************************************************************************
  215. 函数名称:energy_record
  216. 函数版本: 01.01
  217. 创建作者:xxxxxx
  218. 创建日期: 2014-09-05
  219. 函数说明:电度值记录
  220. 参数说明:
  221. 返回值:
  222. 0: 成功
  223. 其它: 失败
  224. 修改记录:
  225. */
  226. int energy_record(void)
  227. {
  228. int i=0;
  229. struct timespec ts;
  230. u32 index_new;
  231. //获取当前时间
  232. clk_time_get(&ts);
  233. timespec_to_rtc(ts,&g_energy_rcd[1].timemark,1);
  234. //索引存储位置
  235. index_new = g_energy.index;
  236. g_energy.index++;
  237. if (g_energy.index >= g_energy.rcd_num)
  238. {
  239. g_energy.index=0;
  240. }
  241. //更新记录
  242. i = pw_quality_file_rcd (ENERGY_FILE,
  243. g_energy.len,
  244. sizeof(struct energy_file_head_data),
  245. index_new,
  246. g_energy.index,
  247. (char*)&g_energy_rcd[1],
  248. (char*)&g_energy_rcd[0]);
  249. /*rt_printf("当前时间: %04d-%02d-%02d %02d:%02d:%02d:%09d!\r\n",
  250. g_energy_rcd[1].timemark.year + 2000,
  251. g_energy_rcd[1].timemark.month,
  252. g_energy_rcd[1].timemark.day,
  253. g_energy_rcd[1].timemark.hour,
  254. g_energy_rcd[1].timemark.min,
  255. g_energy_rcd[1].timemark.ms/1000,
  256. ts.tv_nsec
  257. );
  258. rt_printf("ret = %d, energy_index = %d\r\n\r\n\r\n", i, g_energy.index);*/
  259. memset(&g_energy_rcd[1], 0, sizeof(struct energy_rcd));
  260. return 0;
  261. }
  262. /******************************************************************************
  263. 函数名称:energy_create_csv
  264. 函数版本: 01.01
  265. 创建作者:xxxxxx
  266. 创建日期: 2014-09-05
  267. 函数说明:电度值记录转换为CSV文件
  268. 参数说明:
  269. 返回值:
  270. 0: 成功
  271. 其它: 失败
  272. 修改记录:
  273. */
  274. struct energy_rcd en_rcd;
  275. int energy_create_csv(void)
  276. {
  277. unsigned int addr, len1=0;
  278. int i, j,loop;
  279. char *buf;
  280. struct energy_rcd *rcd;
  281. char p[512];
  282. loff_t pos;
  283. struct file * pfile;
  284. // 分配并初始化空间
  285. len1 = g_energy.len*g_energy.rcd_num;
  286. buf = rt_malloc(len1);
  287. if(!buf)
  288. {
  289. return -1;
  290. }
  291. memset(buf,0,len1);
  292. addr = sizeof(struct energy_file_head_data);
  293. pos = addr;
  294. if(rt_file_read(g_pw_quality_file[ENERGY_FILE].handle,buf,len1,&pos) != len1)
  295. {
  296. rt_free(buf);
  297. return -2;
  298. }
  299. //需考虑打印文件时,同步存储记录的问题
  300. // 创建数据文件
  301. pos = 0;
  302. pfile = rt_file_open("/tmp/energy.csv",O_CREAT|O_RDWR,0);
  303. if(IS_ERR(pfile))
  304. {
  305. rt_free(buf);
  306. return -3;
  307. }
  308. //打印抬头
  309. sprintf(p, "%s,电度记录文件,\r\n", EQUIP_TYPE_STRING);
  310. rt_file_write(pfile,p,strlen(p),&pos);
  311. //打印表格标题
  312. sprintf(p, "记录时间,");
  313. rt_file_write(pfile,p,strlen(p),&pos);
  314. for(i=1; i<g_energy.sw_num+1; i++) // 遥测
  315. {
  316. sprintf(p,"Ep正_%d,Ep负_%d,Eq正_%d,Eq负_%d,", i, i, i, i);
  317. rt_file_write(pfile,p,strlen(p),&pos);
  318. }
  319. sprintf(p, "\r\n");
  320. rt_file_write(pfile,p,strlen(p),&pos);
  321. //打印极值记录
  322. rcd = &en_rcd;
  323. for(loop=g_energy.rcd_num,i=g_energy.index+1;loop>0; loop--,i++)
  324. {
  325. if(i>= g_energy.rcd_num)
  326. {
  327. i=0;
  328. }
  329. //找出标示行下的最老记录行
  330. memcpy(&en_rcd,buf+i* g_energy.len,g_energy.len);
  331. if (rcd->timemark.month < 1|| rcd->timemark.month >12) //如果月份为0,非1~12之间,判定该条记录非法
  332. {
  333. continue;
  334. }
  335. sprintf(p, "%04d-%02d-%02d_%02d:%02d:%02d-%03d,",
  336. rcd->timemark.year+2000,
  337. rcd->timemark.month,
  338. rcd->timemark.day,
  339. rcd->timemark.hour,
  340. rcd->timemark.min,
  341. rcd->timemark.ms/1000,
  342. rcd->timemark.ms%1000);
  343. rt_file_write(pfile,p,strlen(p),&pos);
  344. for (j=0; j<g_energy.sw_num; j++)
  345. {
  346. sprintf(p, "%4.3f,%4.3f,%4.3f,%4.3f,",
  347. rcd->mea[j].energy_p_p,
  348. rcd->mea[j].energy_p_n,
  349. rcd->mea[j].energy_q_p,
  350. rcd->mea[j].energy_q_n);
  351. rt_file_write(pfile,p,strlen(p),&pos);
  352. }
  353. sprintf(p, "\r\n");
  354. rt_file_write(pfile,p,strlen(p),&pos);
  355. }
  356. // 关闭文件
  357. rt_file_close(pfile,0);
  358. rt_free(buf);
  359. rt_printf("电度文件生成成功\r\n");
  360. return 0;
  361. }
  362. /******************************************************************************
  363. 函数名称:energy_init
  364. 函数版本: 01.01
  365. 创建作者:xxxxxx
  366. 创建日期: 2014-09-05
  367. 函数说明:电度初始化
  368. 参数说明:
  369. 返回值:
  370. 0: 成功
  371. 其它: 失败
  372. 修改记录:
  373. */
  374. int extrema_init(void)
  375. {
  376. int i;
  377. struct timespec ts;
  378. struct rtc_time_t time; //时标
  379. g_extrema.change_set = 0;
  380. g_extrema.len = FILE_ADDR_ALGIN(8 + g_table_head->ac_num * 24);
  381. g_extrema.rcd_num = pRunSet->wJZRL+1;//30天日极值
  382. g_extrema.mea_num = g_table_head->ac_num;
  383. memset(g_extrema_rcd, 0, sizeof(g_extrema_rcd));
  384. memset(&g_extrema_rcd[0], 0xFF, sizeof(struct extrema_rcd));
  385. i = pw_quality_file_init(EXTREMA_FILE);
  386. if (i<0 && pRunSet->wJZRL)
  387. {
  388. rt_printf("iret2 = %d\r\n", i);
  389. i = extrema_create_file();
  390. rt_file_del("/tmp/extrema.csv");
  391. rt_printf("初始化极值记录文件, ret=%d\r\n", i);
  392. //获取当前时间
  393. clk_time_get(&ts);
  394. timespec_to_rtc(ts,&time,1);
  395. g_extrema_rcd[1].timemark = time;
  396. for (i=0; i<g_table_head->ac_num; i++)
  397. {
  398. g_extrema_rcd[1].extrema_mea[i].timemark_h = time;
  399. g_extrema_rcd[1].extrema_mea[i].timemark_l = time;
  400. }
  401. }
  402. return 0;
  403. }
  404. /******************************************************************************
  405. 函数名称:energy_create_file
  406. 函数版本: 01.01
  407. 创建作者:xxxxxx
  408. 创建日期: 2014-09-05
  409. 函数说明:创建极值记录文件
  410. 参数说明:
  411. 返回值:
  412. 0: 成功
  413. 其它: 失败
  414. 修改记录:
  415. */
  416. int extrema_create_file(void)
  417. {
  418. unsigned int addr,file_length, i;
  419. char *p;
  420. loff_t pos;
  421. struct extrema_file_head_data extrema_file;
  422. struct rsc *extrema_rsc;
  423. // 写文件头
  424. memset(&extrema_file,0,sizeof(extrema_file));
  425. extrema_file.cfh.signature = SIG_EXTREMA_FILE;
  426. extrema_file.cfh.version = FILE_VERSION;
  427. extrema_file.interval = DAY;
  428. extrema_file.size = pRunSet->wJZRL; //默认存储30天
  429. extrema_file.mea_num = g_extrema.mea_num;
  430. extrema_file.addr_desc = FILE_ADDR_ALGIN(sizeof(extrema_file));
  431. extrema_file.num_desc = g_extrema.mea_num;
  432. extrema_file.addr = FILE_ADDR_ALGIN(sizeof(extrema_file) + g_table_head->ac_num*sizeof(struct rsc));
  433. extrema_file.num = g_extrema.rcd_num;//刚创建时仅存储一条无效电度记录
  434. extrema_file.rsc_crc = g_crc_rectable;
  435. // 得到文件长度
  436. addr = extrema_file.addr + extrema_file.num * g_extrema.len;
  437. file_length = FILE_ADDR_ALGIN(addr);
  438. // 分配并初始化空间
  439. p = rt_malloc(file_length);
  440. if(!p)
  441. {
  442. return -1;
  443. }
  444. memset(p,0,file_length);
  445. // 写入文件头
  446. memcpy(p, (char *)&extrema_file, sizeof(extrema_file));
  447. extrema_rsc = (struct rsc *)(p+sizeof(extrema_file));
  448. //写点表描述
  449. for(i=0;i<g_table_head->ac_num;i++)
  450. {
  451. memcpy (&extrema_rsc[i], &g_ac_table[i], 5);
  452. extrema_rsc[i].normal = g_ac_table[i].normal;
  453. _get_ac_name(g_ac_table[i].owner, g_ac_table[i].indexno, extrema_rsc[i].name);
  454. }
  455. // 写极值记录
  456. memset(p+extrema_file.addr, 0xFF, g_extrema.len);//无效记录为全1
  457. // 创建数据文件
  458. g_pw_quality_file[EXTREMA_FILE].handle = rt_file_open(g_pw_quality_file[EXTREMA_FILE].name,O_CREAT|O_RDWR,0);
  459. if(IS_ERR(g_pw_quality_file[EXTREMA_FILE].handle))
  460. {
  461. g_pw_quality_file[EXTREMA_FILE].handle = 0;
  462. rt_printf("%s open error!\r\n",g_pw_quality_file[EXTREMA_FILE].name);
  463. rt_free(p);
  464. return -2;
  465. }
  466. pos = 0;
  467. rt_file_write(g_pw_quality_file[EXTREMA_FILE].handle,p,file_length,&pos);
  468. g_extrema.index = 0;
  469. rt_free(p);
  470. return 0;
  471. }
  472. /******************************************************************************
  473. 函数名称:extrema_calc
  474. 函数版本: 01.01
  475. 创建作者:xxxxxx
  476. 创建日期: 2014-09-05
  477. 函数说明:极值计算
  478. 参数说明:
  479. 返回值:
  480. 0: 成功
  481. 其它: 失败
  482. 修改记录:
  483. */
  484. int extrema_calc(void)
  485. {
  486. static int first = 1;
  487. int i,time_changed=0;
  488. struct timespec ts;
  489. struct rtc_time_t time; //时标
  490. u32 index_data;
  491. //获取当前时间
  492. clk_time_get(&ts);
  493. timespec_to_rtc(ts,&time,1);
  494. //日期不对应时,保存旧值
  495. if(first ==1)
  496. {
  497. g_extrema_rcd[1].timemark = time;
  498. time_changed = 1;
  499. first = 0;
  500. }
  501. else if (time.day != g_extrema_rcd[1].timemark.day)
  502. {
  503. //更新存储位置
  504. index_data = g_extrema.index;
  505. g_extrema.index++;
  506. if (g_extrema.index >= g_extrema.rcd_num)
  507. {
  508. g_extrema.index=0;
  509. }
  510. //保存旧记录入文件
  511. pw_quality_file_rcd (EXTREMA_FILE,
  512. g_extrema.len,
  513. sizeof(struct extrema_file_head_data)+g_table_head->ac_num*sizeof(struct rsc),
  514. index_data,
  515. g_extrema.index,
  516. (char *)&g_extrema_rcd[1],
  517. (char *)&g_extrema_rcd[0]);
  518. /*rt_printf("当前时间2: %04d-%02d-%02d %02d:%02d:%02d:%09d!\r\n",
  519. g_extrema_rcd[1].timemark.year + 2000,
  520. g_extrema_rcd[1].timemark.month,
  521. g_extrema_rcd[1].timemark.day,
  522. g_extrema_rcd[1].timemark.hour,
  523. g_extrema_rcd[1].timemark.min,
  524. g_extrema_rcd[1].timemark.ms/1000,
  525. ts.tv_nsec
  526. );*/
  527. // 更新时间
  528. g_extrema_rcd[1].timemark = time;
  529. time_changed = 1;
  530. //rt_printf("extrema_index = %d\r\n\r\n\r\n", g_extrema.index);
  531. }
  532. //更新极大值、极小值
  533. for (i=0; i<g_table_head->ac_num; i++)
  534. {
  535. u8 no=g_ac_table[i].indexno;
  536. if(no>0)
  537. {
  538. float data;
  539. long lmeaval;
  540. float f_65536=65536.0;
  541. if(g_ac_table[i].normal==0)f_65536=1.0;
  542. no-=1;
  543. lmeaval=GetRmtMeaVal(g_ac_table[i].owner,no,NULL);
  544. data =((float)lmeaval)/f_65536; //转为浮点数
  545. //日期不对应时,重新初始化记录
  546. if (time_changed)
  547. {
  548. g_extrema_rcd[1].extrema_mea[i].extrema_h = data;
  549. g_extrema_rcd[1].extrema_mea[i].extrema_l = data;
  550. g_extrema_rcd[1].extrema_mea[i].timemark_h = time;
  551. g_extrema_rcd[1].extrema_mea[i].timemark_l = time;
  552. g_extrema_rcd[1].timemark = time;
  553. }
  554. else
  555. {
  556. //极大值
  557. if (data > g_extrema_rcd[1].extrema_mea[i].extrema_h)
  558. {
  559. g_extrema_rcd[1].extrema_mea[i].extrema_h = data;
  560. g_extrema_rcd[1].extrema_mea[i].timemark_h = time;
  561. }
  562. //极小值
  563. if (data < g_extrema_rcd[1].extrema_mea[i].extrema_l)
  564. {
  565. g_extrema_rcd[1].extrema_mea[i].extrema_l = data;
  566. g_extrema_rcd[1].extrema_mea[i].timemark_l = time;
  567. }
  568. }
  569. }
  570. else //备用点号,数值上送0
  571. {
  572. g_extrema_rcd[1].extrema_mea[i].extrema_h = 0;
  573. g_extrema_rcd[1].extrema_mea[i].extrema_l = 0;
  574. memset (&g_extrema_rcd[1].extrema_mea[i].timemark_h, 0, sizeof(struct rtc_time_t));
  575. memset (&g_extrema_rcd[1].extrema_mea[i].timemark_l, 0, sizeof(struct rtc_time_t));
  576. }
  577. }
  578. return 0;
  579. }
  580. /******************************************************************************
  581. 函数名称:extrema_create_csv
  582. 函数版本: 01.01
  583. 创建作者:xxxxxx
  584. 创建日期: 2014-09-05
  585. 函数说明:极值记录转换为CSV文件
  586. 参数说明:
  587. 返回值:
  588. 0: 成功
  589. 其它: 失败
  590. 修改记录:
  591. */
  592. struct extrema_rcd ex_rcd;
  593. int extrema_create_csv(void)
  594. {
  595. unsigned int addr, len1=0;
  596. int i, j,loop;
  597. char *buf;
  598. struct extrema_rcd *rcd;
  599. char p[512];
  600. loff_t pos;
  601. struct file * pfile;
  602. // 分配并初始化空间
  603. len1 = g_extrema.len*g_extrema.rcd_num;
  604. buf = rt_malloc(len1);
  605. if(!buf)
  606. {
  607. return -1;
  608. }
  609. memset(buf,0,len1);
  610. addr = sizeof(struct extrema_file_head_data) + g_extrema.mea_num*sizeof(struct rsc);
  611. pos = addr;
  612. if(rt_file_read(g_pw_quality_file[EXTREMA_FILE].handle,buf,len1,&pos) != len1)
  613. {
  614. rt_free(buf);
  615. return -2;
  616. }
  617. //需考虑打印文件时,同步存储记录的问题
  618. // 创建数据文件
  619. pos = 0;
  620. pfile = rt_file_open("/tmp/extrema.csv",O_CREAT|O_RDWR,0);
  621. if(IS_ERR(pfile))
  622. {
  623. rt_free(buf);
  624. return -3;
  625. }
  626. //打印抬头
  627. sprintf(p, "%s,极值记录文件,\r\n", EQUIP_TYPE_STRING);
  628. rt_file_write(pfile,p,strlen(p),&pos);
  629. //打印表格标题
  630. sprintf(p, "记录时间,");
  631. rt_file_write(pfile,p,strlen(p),&pos);
  632. for(i=0; i<g_table_head->ac_num; i++) // 遥测
  633. {
  634. if(g_ac_table[i].indexno!=0)
  635. {
  636. _get_ac_name(g_ac_table[i].owner,g_ac_table[i].indexno,p);
  637. }
  638. else
  639. {
  640. sprintf(p,"备用");
  641. }
  642. sprintf(p+strlen(p), ",,");
  643. rt_file_write(pfile,p,strlen(p),&pos);
  644. }
  645. sprintf(p, "\r\n");
  646. rt_file_write(pfile,p,strlen(p),&pos);
  647. //打印极值记录
  648. rcd = &ex_rcd;
  649. for(loop =g_extrema.rcd_num,i=g_extrema.index+1;loop>0; loop--,i++)
  650. {
  651. //
  652. if(i>= g_extrema.rcd_num)
  653. {
  654. i=0;
  655. }
  656. //找出标示行下的最老记录行
  657. //rcd = (struct extrema_rcd *) (buf+i* g_extrema.len);
  658. memcpy(&ex_rcd,buf+i* g_extrema.len,g_extrema.len);
  659. if (rcd->timemark.month < 1|| rcd->timemark.month >12) //如果月份为0,非1~12之间,判定该条记录非法
  660. {
  661. continue;
  662. }
  663. sprintf(p, "%04d-%02d-%02d,",
  664. rcd->timemark.year+2000,
  665. rcd->timemark.month,
  666. rcd->timemark.day);
  667. rt_file_write(pfile,p,strlen(p),&pos);
  668. for (j=0; j<g_table_head->ac_num; j++)
  669. {
  670. sprintf(p, "%4.3f,", rcd->extrema_mea[j].extrema_h);
  671. rt_file_write(pfile,p,strlen(p),&pos);
  672. sprintf(p, "%02d:%02d:%02d-%03d,",
  673. rcd->extrema_mea[j].timemark_h.hour,
  674. rcd->extrema_mea[j].timemark_h.min,
  675. rcd->extrema_mea[j].timemark_h.ms/1000,
  676. rcd->extrema_mea[j].timemark_h.ms%1000);
  677. rt_file_write(pfile,p,strlen(p),&pos);
  678. }
  679. sprintf(p, "\r\n,");
  680. rt_file_write(pfile,p,strlen(p),&pos);
  681. for (j=0; j<g_table_head->ac_num; j++)
  682. {
  683. sprintf(p, "%4.3f,", rcd->extrema_mea[j].extrema_l);
  684. rt_file_write(pfile,p,strlen(p),&pos);
  685. sprintf(p, "%02d:%02d:%02d-%03d,",
  686. rcd->extrema_mea[j].timemark_l.hour,
  687. rcd->extrema_mea[j].timemark_l.min,
  688. rcd->extrema_mea[j].timemark_l.ms/1000,
  689. rcd->extrema_mea[j].timemark_l.ms%1000);
  690. rt_file_write(pfile,p,strlen(p),&pos);
  691. }
  692. sprintf(p, "\r\n");
  693. rt_file_write(pfile,p,strlen(p),&pos);
  694. }
  695. // 关闭文件
  696. rt_file_close(pfile,0);
  697. rt_free(buf);
  698. rt_printf("极值文件生成成功\r\n");
  699. return 0;
  700. }
  701. /******************************************************************************
  702. 函数名称:mea_rcd_init
  703. 函数版本: 01.01
  704. 创建作者:xxxxxx
  705. 创建日期: 2014-09-05
  706. 函数说明:定点遥测初始化
  707. 参数说明:
  708. 返回值:
  709. 0: 成功
  710. 其它: 失败
  711. 修改记录:
  712. */
  713. int mearcd_init(void)
  714. {
  715. int i;
  716. g_mearcd.change_set = 0;
  717. g_mearcd.len = FILE_ADDR_ALGIN(8 + g_table_head->ac_num * 4);
  718. if (pRunSet->wZDRL)
  719. {
  720. g_mearcd.rcd_num = ((pRunSet->wZDRL * 24 * 60) / pRunSet->wZDJG) + 1;//7天,5~60分钟间隔
  721. }
  722. g_mearcd.delay_s = pRunSet->wZDJG * 60;//5~60分钟间隔
  723. g_mearcd.mea_num = g_table_head->ac_num;
  724. memset(g_mea_rcd, 0, sizeof(g_mea_rcd));
  725. memset(&g_mea_rcd[0], 0xFF, sizeof(struct mea_rcd));
  726. i = pw_quality_file_init(MEARCD_FILE);
  727. if (i<0 && pRunSet->wZDRL)
  728. {
  729. rt_printf("iret3 = %d\r\n", i);
  730. i = mearcd_create_file();
  731. rt_file_del("/tmp/mearcd.csv");
  732. rt_printf("初始化定点遥测记录文件, ret=%d\r\n", i);
  733. }
  734. return 0;
  735. }
  736. /******************************************************************************
  737. 函数名称:mearcd_create_file
  738. 函数版本: 01.01
  739. 创建作者:xxxxxx
  740. 创建日期: 2014-09-05
  741. 函数说明:创建定点遥测记录文件
  742. 参数说明:
  743. 返回值:
  744. 0: 成功
  745. 其它: 失败
  746. 修改记录:
  747. */
  748. int mearcd_create_file(void)
  749. {
  750. unsigned int addr,file_length, i;
  751. char *p;
  752. loff_t pos;
  753. struct mearcd_file_head_data mearcd_file;
  754. struct rsc *mearcd_rsc;
  755. // 写文件头
  756. memset(&mearcd_file,0,sizeof(mearcd_file));
  757. mearcd_file.cfh.signature = SIG_MEARCD_FILE;
  758. mearcd_file.cfh.version = FILE_VERSION;
  759. mearcd_file.interval = g_mearcd.delay_s;
  760. mearcd_file.size = pRunSet->wZDRL; //默认存储30天
  761. mearcd_file.mea_num = g_mearcd.mea_num;
  762. mearcd_file.addr_desc = FILE_ADDR_ALGIN(sizeof(mearcd_file));
  763. mearcd_file.num_desc = g_mearcd.mea_num;
  764. mearcd_file.addr = FILE_ADDR_ALGIN(sizeof(mearcd_file) + g_table_head->ac_num*sizeof(struct rsc));
  765. mearcd_file.num = g_mearcd.rcd_num;//刚创建时仅存储一条无效电度记录
  766. mearcd_file.rsc_crc = g_crc_rectable;
  767. // 得到文件长度
  768. addr = mearcd_file.addr + mearcd_file.num * g_mearcd.len;
  769. file_length = FILE_ADDR_ALGIN(addr);
  770. // 分配并初始化空间
  771. p = rt_malloc(file_length);
  772. if(!p)
  773. {
  774. return -1;
  775. }
  776. memset(p,0,file_length);
  777. // 写入文件头
  778. memcpy(p, (char *)&mearcd_file, sizeof(mearcd_file));
  779. mearcd_rsc = (struct rsc *)(p+sizeof(mearcd_file));
  780. //写点表描述
  781. for(i=0;i<g_table_head->ac_num;i++)
  782. {
  783. memcpy (&mearcd_rsc[i], &g_ac_table[i], 5);
  784. mearcd_rsc[i].normal = g_ac_table[i].normal;
  785. _get_ac_name(g_ac_table[i].owner, g_ac_table[i].indexno, mearcd_rsc[i].name);
  786. }
  787. // 写极值记录
  788. memset(p+mearcd_file.addr, 0xFF, g_mearcd.len);//无效记录为全1
  789. // 创建数据文件
  790. g_pw_quality_file[MEARCD_FILE].handle = rt_file_open(g_pw_quality_file[MEARCD_FILE].name,O_CREAT|O_RDWR,0);
  791. if(IS_ERR(g_pw_quality_file[MEARCD_FILE].handle))
  792. {
  793. g_pw_quality_file[MEARCD_FILE].handle = 0;
  794. rt_printf("%s open error!\r\n",g_pw_quality_file[MEARCD_FILE].name);
  795. rt_free(p);
  796. return -2;
  797. }
  798. pos = 0;
  799. rt_file_write(g_pw_quality_file[MEARCD_FILE].handle,p,file_length,&pos);
  800. g_mearcd.index = 0;
  801. rt_free(p);
  802. return 0;
  803. }
  804. /******************************************************************************
  805. 函数名称:extrema_calc
  806. 函数版本: 01.01
  807. 创建作者:xxxxxx
  808. 创建日期: 2014-09-05
  809. 函数说明:极值计算
  810. 参数说明:
  811. 返回值:
  812. 0: 成功
  813. 其它: 失败
  814. 修改记录:
  815. */
  816. int mearcd_record(void)
  817. {
  818. int i;
  819. struct timespec ts;
  820. struct rtc_time_t time; //时标
  821. u32 index_data;
  822. //获取当前时间
  823. clk_time_get(&ts);
  824. timespec_to_rtc(ts,&time,1);
  825. g_mea_rcd[1].timemark = time;
  826. //更新值
  827. for (i=0; i<g_table_head->ac_num; i++)
  828. {
  829. u8 no=g_ac_table[i].indexno;
  830. if(no>0)
  831. {
  832. float data;
  833. long lmeaval;
  834. float f_65536=65536.0;
  835. if(g_ac_table[i].normal==0)f_65536=1.0;
  836. no-=1;
  837. lmeaval=GetRmtMeaVal(g_ac_table[i].owner,no,NULL);
  838. data =((float)lmeaval)/f_65536; //转为浮点数
  839. g_mea_rcd[1].data[i] = data;
  840. }
  841. else //备用点号,数值上送0
  842. {
  843. g_mea_rcd[1].data[i] = 0;
  844. memset (&g_mea_rcd[1].timemark, 0, sizeof(struct rtc_time_t));
  845. }
  846. }
  847. //更新存储位置
  848. index_data = g_mearcd.index;
  849. g_mearcd.index++;
  850. if (g_mearcd.index >= g_mearcd.rcd_num)
  851. {
  852. g_mearcd.index=0;
  853. }
  854. //保存旧记录入文件
  855. pw_quality_file_rcd (MEARCD_FILE,
  856. g_mearcd.len,
  857. sizeof(struct mearcd_file_head_data)+g_table_head->ac_num*sizeof(struct rsc),
  858. index_data,
  859. g_mearcd.index,
  860. (char *)&g_mea_rcd[1],
  861. (char *)&g_mea_rcd[0]);
  862. /*rt_printf("当前时间2: %04d-%02d-%02d %02d:%02d:%02d:%09d!\r\n",
  863. g_mea_rcd[1].timemark.year + 2000,
  864. g_mea_rcd[1].timemark.month,
  865. g_mea_rcd[1].timemark.day,
  866. g_mea_rcd[1].timemark.hour,
  867. g_mea_rcd[1].timemark.min,
  868. g_mea_rcd[1].timemark.ms/1000,
  869. ts.tv_nsec
  870. );
  871. rt_printf("mearcd_index = %d\r\n\r\n\r\n", g_mearcd.index);*/
  872. return 0;
  873. }
  874. /******************************************************************************
  875. 函数名称:mearcd_create_csv
  876. 函数版本: 01.01
  877. 创建作者:xxxxxx
  878. 创建日期: 2014-09-05
  879. 函数说明:定点遥测记录转换为CSV文件
  880. 参数说明:
  881. 返回值:
  882. 0: 成功
  883. 其它: 失败
  884. 修改记录:
  885. */
  886. struct mea_rcd mearcd;
  887. int mearcd_create_csv(void)
  888. {
  889. unsigned int addr, len1=0;
  890. int i, j,loop;
  891. char *buf;
  892. struct mea_rcd *rcd;
  893. char p[512];
  894. loff_t pos;
  895. struct file * pfile;
  896. // 分配并初始化空间
  897. len1 = g_mearcd.len * g_mearcd.rcd_num;
  898. buf = rt_malloc(len1);
  899. if(!buf)
  900. {
  901. return -1;
  902. }
  903. memset(buf,0,len1);
  904. addr = sizeof(struct mearcd_file_head_data) + g_mearcd.mea_num*sizeof(struct rsc);
  905. pos = addr;
  906. if(rt_file_read(g_pw_quality_file[MEARCD_FILE].handle,buf,len1,&pos) != len1)
  907. {
  908. rt_free(buf);
  909. return -2;
  910. }
  911. //需考虑打印文件时,同步存储记录的问题
  912. // 创建数据文件
  913. pos = 0;
  914. pfile = rt_file_open("/tmp/mearcd.csv",O_CREAT|O_RDWR,0);
  915. if(IS_ERR(pfile))
  916. {
  917. rt_free(buf);
  918. return -3;
  919. }
  920. //打印抬头
  921. sprintf(p, "%s,定点遥测文件,\r\n", EQUIP_TYPE_STRING);
  922. rt_file_write(pfile,p,strlen(p),&pos);
  923. //打印表格标题
  924. sprintf(p, "记录时间,");
  925. rt_file_write(pfile,p,strlen(p),&pos);
  926. for(i=0; i<g_table_head->ac_num; i++) // 遥测
  927. {
  928. if(g_ac_table[i].indexno!=0)
  929. {
  930. _get_ac_name(g_ac_table[i].owner,g_ac_table[i].indexno,p);
  931. }
  932. else
  933. {
  934. sprintf(p,"备用");
  935. }
  936. sprintf(p+strlen(p), ",");
  937. rt_file_write(pfile,p,strlen(p),&pos);
  938. }
  939. sprintf(p, "\r\n");
  940. rt_file_write(pfile,p,strlen(p),&pos);
  941. //打印极值记录
  942. rcd = &mearcd;
  943. for(loop=g_mearcd.rcd_num,i=g_mearcd.index+1;loop>0; loop--,i++)
  944. {
  945. //
  946. if(i >= g_mearcd.rcd_num)
  947. {
  948. i=0;
  949. }
  950. //找出标示行下的最老记录行
  951. memcpy(&mearcd, buf+i*g_mearcd.len, g_mearcd.len);
  952. if (rcd->timemark.month < 1|| rcd->timemark.month >12) //如果月份为0,非1~12之间,判定该条记录非法
  953. {
  954. continue;
  955. }
  956. sprintf(p, "%04d-%02d-%02d_%02d:%02d:%02d-%03d,",
  957. rcd->timemark.year+2000,
  958. rcd->timemark.month,
  959. rcd->timemark.day,
  960. rcd->timemark.hour,
  961. rcd->timemark.min,
  962. rcd->timemark.ms/1000,
  963. rcd->timemark.ms%1000);
  964. rt_file_write(pfile,p,strlen(p),&pos);
  965. for (j=0; j<g_table_head->ac_num; j++)
  966. {
  967. sprintf(p, "%4.3f,", rcd->data[j]);
  968. rt_file_write(pfile,p,strlen(p),&pos);
  969. }
  970. sprintf(p, "\r\n");
  971. rt_file_write(pfile,p,strlen(p),&pos);
  972. }
  973. // 关闭文件
  974. rt_file_close(pfile,0);
  975. rt_free(buf);
  976. rt_printf("定点遥测文件生成成功\r\n");
  977. return 0;
  978. }
  979. /******************************************************************************
  980. 函数名称:capmanage_init
  981. 函数版本: 01.01
  982. 创建作者:xxxxxx
  983. 创建日期: 2014-09-05
  984. 函数说明:电容器管理初始化
  985. 参数说明:
  986. 返回值:
  987. 0: 成功
  988. 其它: 失败
  989. 修改记录:
  990. */
  991. int capmanage_init(void)
  992. {
  993. int i;
  994. g_capmanage.change_set = 0;
  995. for(i=0;i<g_sw_num;i++)
  996. {
  997. if(g_ui[PUB_AC_NUM+SW_AC_IA+i*SW_AC_NUM].chn_index == CFG_ADC_CHANNEL_ZERO)
  998. {
  999. g_capmanage.sw_num = i;
  1000. break;
  1001. }
  1002. }
  1003. g_capmanage.len = FILE_ADDR_ALGIN(8 + g_capmanage.sw_num*2*8 + 6*8);
  1004. g_capmanage.rcd_num = pRunSet->wCAPRL + 1;//30条
  1005. g_capmanage.delay_s = pRunSet->wCAPJG;//1~300秒钟间隔
  1006. g_capmanage.mea_num = g_capmanage.sw_num*2+6;//两组线电压,开关倍数的Q、COS
  1007. memset(g_cap_rcd, 0, sizeof(g_cap_rcd));
  1008. memset(&g_cap_rcd[0], 0xFF, sizeof(struct cap_rcd));
  1009. i = pw_quality_file_init(CAP_FILE);
  1010. if (i<0 && pRunSet->wCAPRL)
  1011. {
  1012. rt_printf("iret4 = %d\r\n", i);
  1013. i = capmanage_create_file();
  1014. rt_file_del("/tmp/cap_manage.csv");
  1015. rt_printf("初始化电容器管理文件, ret=%d\r\n", i);
  1016. }
  1017. return 0;
  1018. }
  1019. /******************************************************************************
  1020. 函数名称:capmanage_create_file
  1021. 函数版本: 01.01
  1022. 创建作者:xxxxxx
  1023. 创建日期: 2014-09-05
  1024. 函数说明:创建电容器管理文件
  1025. 参数说明:
  1026. 返回值:
  1027. 0: 成功
  1028. 其它: 失败
  1029. 修改记录:
  1030. */
  1031. int capmanage_create_file(void)
  1032. {
  1033. unsigned int addr,file_length, i, j=0;
  1034. char *p;
  1035. loff_t pos;
  1036. struct capmanage_file_head_data caprcd_file;
  1037. struct rsc *caprcd_rsc;
  1038. // 写文件头
  1039. memset(&caprcd_file,0,sizeof(caprcd_file));
  1040. caprcd_file.cfh.signature = SIG_CAPRCD_FILE;
  1041. caprcd_file.cfh.version = FILE_VERSION;
  1042. caprcd_file.size = pRunSet->wCAPRL; //默认存储30条
  1043. caprcd_file.meanum = g_capmanage.mea_num;
  1044. caprcd_file.addr_desc = FILE_ADDR_ALGIN(sizeof(caprcd_file));
  1045. caprcd_file.num_desc = g_capmanage.mea_num;
  1046. caprcd_file.addr = FILE_ADDR_ALGIN(caprcd_file.addr_desc + caprcd_file.num_desc *sizeof(struct rsc));
  1047. caprcd_file.num = g_capmanage.rcd_num;
  1048. caprcd_file.rsc_crc = g_crc_rectable;
  1049. // 得到文件长度
  1050. addr = caprcd_file.addr + caprcd_file.num * g_capmanage.len;
  1051. file_length = FILE_ADDR_ALGIN(addr);
  1052. // 分配并初始化空间
  1053. p = rt_malloc(file_length);
  1054. if(!p)
  1055. {
  1056. return -1;
  1057. }
  1058. memset(p,0,file_length);
  1059. // 写入文件头
  1060. memcpy(p, (char *)&caprcd_file, sizeof(caprcd_file));
  1061. caprcd_rsc = (struct rsc *)(p+sizeof(caprcd_file));
  1062. //写点表描述
  1063. for(i=0; i<g_table_head->ac_num; i++)
  1064. {
  1065. if (g_ac_table[i].owner == 0)
  1066. {
  1067. if (((g_ac_table[i].indexno-1)>=PUB_AC_UAB1 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA1)
  1068. || ((g_ac_table[i].indexno-1)>=PUB_AC_UAB2 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA2))
  1069. {
  1070. memcpy (&caprcd_rsc[j], &g_ac_table[i], 5);
  1071. caprcd_rsc[j].normal = g_ac_table[i].normal;
  1072. strcpy(caprcd_rsc[j].name, g_pub_ac_desc[g_ac_table[i].indexno-1].name);
  1073. j++;
  1074. }
  1075. }
  1076. else
  1077. {
  1078. if ((g_ac_table[i].indexno-1)==(SW_AC_IN_Q) || (g_ac_table[i].indexno-1)==(SW_AC_IN_COS))
  1079. {
  1080. memcpy (&caprcd_rsc[j], &g_ac_table[i], 5);
  1081. caprcd_rsc[j].normal = g_ac_table[i].normal;
  1082. sprintf(caprcd_rsc[j].name,"%s_%d", g_sw_ac_desc[g_ac_table[i].indexno-1].name, g_ac_table[i].owner);
  1083. j++;
  1084. }
  1085. }
  1086. }
  1087. // 写极值记录
  1088. memset(p+caprcd_file.addr, 0xFF, g_capmanage.len);//无效记录为全1
  1089. // 创建数据文件
  1090. g_pw_quality_file[CAP_FILE].handle = rt_file_open(g_pw_quality_file[CAP_FILE].name,O_CREAT|O_RDWR,0);
  1091. if(IS_ERR(g_pw_quality_file[CAP_FILE].handle))
  1092. {
  1093. g_pw_quality_file[CAP_FILE].handle = 0;
  1094. rt_printf("%s open error!\r\n",g_pw_quality_file[CAP_FILE].name);
  1095. rt_free(p);
  1096. return -2;
  1097. }
  1098. pos = 0;
  1099. rt_file_write(g_pw_quality_file[CAP_FILE].handle,p,file_length,&pos);
  1100. g_capmanage.index = 0;
  1101. rt_free(p);
  1102. return 0;
  1103. }
  1104. /******************************************************************************
  1105. 函数名称:capmanage_calc
  1106. 函数版本: 01.01
  1107. 创建作者:xxxxxx
  1108. 创建日期: 2014-09-05
  1109. 函数说明:电容器管理取值
  1110. 参数说明:
  1111. 返回值:
  1112. 0: 成功
  1113. 其它: 失败
  1114. 修改记录:
  1115. */
  1116. int capmanage_calc(void)
  1117. {
  1118. int i, j=0, capdata_rcd = 0;
  1119. float data;
  1120. long lmeaval;
  1121. //更新值
  1122. for (i=0; i<g_table_head->ac_num; i++)
  1123. {
  1124. u8 no=g_ac_table[i].indexno;
  1125. if (g_ac_table[i].owner == 0)
  1126. {
  1127. if (((g_ac_table[i].indexno-1)>=PUB_AC_UAB1 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA1)
  1128. || ((g_ac_table[i].indexno-1)>=PUB_AC_UAB2 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA2))
  1129. {
  1130. capdata_rcd = 1;
  1131. }
  1132. }
  1133. else
  1134. {
  1135. if ((g_ac_table[i].indexno-1)==(SW_AC_IN_Q) || (g_ac_table[i].indexno-1)==(SW_AC_IN_COS))
  1136. {
  1137. capdata_rcd = 1;
  1138. }
  1139. }
  1140. if (capdata_rcd)
  1141. {
  1142. float f_65536=65536.0;
  1143. if(g_ac_table[i].normal==0)f_65536=1.0;
  1144. no-=1;
  1145. lmeaval=GetRmtMeaVal(g_ac_table[i].owner,no,NULL);
  1146. data =((float)lmeaval)/f_65536; //转为浮点数
  1147. g_cap_rcd[1].data[j].old_data = g_cap_rcd[1].data[j].new_data;
  1148. g_cap_rcd[1].data[j].new_data = data;
  1149. capdata_rcd = 0;
  1150. j++;
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. int capmanage_record(void)
  1156. {
  1157. u32 index_data;
  1158. struct timespec ts;
  1159. struct rtc_time_t time; //时标
  1160. //获取当前时间
  1161. clk_time_get(&ts);
  1162. timespec_to_rtc(ts,&time,1);
  1163. g_cap_rcd[1].action_timemark = time;
  1164. //更新存储位置
  1165. index_data = g_capmanage.index;
  1166. g_capmanage.index++;
  1167. if (g_capmanage.index >= g_capmanage.rcd_num)
  1168. {
  1169. g_capmanage.index=0;
  1170. }
  1171. //保存旧记录入文件
  1172. pw_quality_file_rcd (CAP_FILE,
  1173. g_capmanage.len,
  1174. sizeof(struct capmanage_file_head_data)+g_capmanage.mea_num*sizeof(struct rsc),
  1175. index_data,
  1176. g_capmanage.index,
  1177. (char *)&g_cap_rcd[1],
  1178. (char *)&g_cap_rcd[0]);
  1179. /*rt_printf("当前时间2: %04d-%02d-%02d %02d:%02d:%02d:%09d!\r\n",
  1180. g_cap_rcd[1].action_timemark.year + 2000,
  1181. g_cap_rcd[1].action_timemark.month,
  1182. g_cap_rcd[1].action_timemark.day,
  1183. g_cap_rcd[1].action_timemark.hour,
  1184. g_cap_rcd[1].action_timemark.min,
  1185. g_cap_rcd[1].action_timemark.ms/1000,
  1186. ts.tv_nsec
  1187. );
  1188. rt_printf("capmanage_index = %d\r\n\r\n\r\n", g_capmanage.index);*/
  1189. return 0;
  1190. }
  1191. /******************************************************************************
  1192. 函数名称:mearcd_create_csv
  1193. 函数版本: 01.01
  1194. 创建作者:xxxxxx
  1195. 创建日期: 2014-09-05
  1196. 函数说明:定点遥测记录转换为CSV文件
  1197. 参数说明:
  1198. 返回值:
  1199. 0: 成功
  1200. 其它: 失败
  1201. 修改记录:
  1202. */
  1203. struct cap_rcd caprcd;
  1204. int capmanage_create_csv(void)
  1205. {
  1206. unsigned int addr, len1=0;
  1207. int i, j,loop;
  1208. char *buf;
  1209. struct cap_rcd *rcd;
  1210. char p[512];
  1211. loff_t pos;
  1212. struct file * pfile;
  1213. // 分配并初始化空间
  1214. len1 = g_capmanage.len * g_capmanage.rcd_num;
  1215. buf = rt_malloc(len1);
  1216. if(!buf)
  1217. {
  1218. return -1;
  1219. }
  1220. memset(buf,0,len1);
  1221. addr = sizeof(struct capmanage_file_head_data) + g_capmanage.mea_num*sizeof(struct rsc);
  1222. pos = addr;
  1223. if(rt_file_read(g_pw_quality_file[CAP_FILE].handle,buf,len1,&pos) != len1)
  1224. {
  1225. rt_free(buf);
  1226. return -2;
  1227. }
  1228. //需考虑打印文件时,同步存储记录的问题
  1229. // 创建数据文件
  1230. pos = 0;
  1231. pfile = rt_file_open("/tmp/cap_manage.csv",O_CREAT|O_RDWR,0);
  1232. if(IS_ERR(pfile))
  1233. {
  1234. rt_free(buf);
  1235. return -3;
  1236. }
  1237. //打印抬头
  1238. sprintf(p, "%s,电容器管理文件,\r\n", EQUIP_TYPE_STRING);
  1239. rt_file_write(pfile,p,strlen(p),&pos);
  1240. //打印表格标题
  1241. sprintf(p, "记录时间,");
  1242. rt_file_write(pfile,p,strlen(p),&pos);
  1243. for(i=0; i<g_table_head->ac_num; i++) // 遥测
  1244. {
  1245. if (g_ac_table[i].owner == 0)
  1246. {
  1247. if (((g_ac_table[i].indexno-1)>=PUB_AC_UAB1 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA1)
  1248. || ((g_ac_table[i].indexno-1)>=PUB_AC_UAB2 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA2))
  1249. {
  1250. strcpy(p, g_pub_ac_desc[g_ac_table[i].indexno-1].name);
  1251. sprintf(p+strlen(p), ",");
  1252. rt_file_write(pfile,p,strlen(p),&pos);
  1253. }
  1254. }
  1255. else
  1256. {
  1257. if ((g_ac_table[i].indexno-1)==(SW_AC_IN_Q) || (g_ac_table[i].indexno-1)==(SW_AC_IN_COS))
  1258. {
  1259. sprintf(p,"%s_%d", g_sw_ac_desc[g_ac_table[i].indexno-1].name, g_ac_table[i].owner);
  1260. sprintf(p+strlen(p), ",");
  1261. rt_file_write(pfile,p,strlen(p),&pos);
  1262. }
  1263. }
  1264. }
  1265. sprintf(p, "\r\n");
  1266. rt_file_write(pfile,p,strlen(p),&pos);
  1267. //打印极值记录
  1268. rcd = &caprcd;
  1269. for(loop=g_capmanage.rcd_num,i=g_capmanage.index+1;loop>0; loop--,i++)
  1270. {
  1271. //
  1272. if(i >= g_capmanage.rcd_num)
  1273. {
  1274. i=0;
  1275. }
  1276. //找出标示行下的最老记录行
  1277. memcpy(&caprcd, buf+i*g_capmanage.len, g_capmanage.len);
  1278. if (rcd->action_timemark.month < 1|| rcd->action_timemark.month >12) //如果月份为0,非1~12之间,判定该条记录非法
  1279. {
  1280. continue;
  1281. }
  1282. sprintf(p, "%04d-%02d-%02d_%02d:%02d:%02d-%03d,",
  1283. rcd->action_timemark.year+2000,
  1284. rcd->action_timemark.month,
  1285. rcd->action_timemark.day,
  1286. rcd->action_timemark.hour,
  1287. rcd->action_timemark.min,
  1288. rcd->action_timemark.ms/1000,
  1289. rcd->action_timemark.ms%1000);
  1290. rt_file_write(pfile,p,strlen(p),&pos);
  1291. for (j=0; j<g_capmanage.mea_num; j++)
  1292. {
  1293. sprintf(p, "%4.3f,", rcd->data[j].old_data);
  1294. rt_file_write(pfile,p,strlen(p),&pos);
  1295. }
  1296. sprintf(p, "\r\n,");
  1297. rt_file_write(pfile,p,strlen(p),&pos);
  1298. for (j=0; j<g_capmanage.mea_num; j++)
  1299. {
  1300. sprintf(p, "%4.3f,", rcd->data[j].new_data);
  1301. rt_file_write(pfile,p,strlen(p),&pos);
  1302. }
  1303. sprintf(p, "\r\n");
  1304. rt_file_write(pfile,p,strlen(p),&pos);
  1305. }
  1306. // 关闭文件
  1307. rt_file_close(pfile,0);
  1308. rt_free(buf);
  1309. rt_printf("电容器管理文件生成成功\r\n");
  1310. return 0;
  1311. }
  1312. /******************************************************************************
  1313. 函数名称:udcrcd_init
  1314. 函数版本: 01.01
  1315. 创建作者:xxxxxx
  1316. 创建日期: 2014-09-05
  1317. 函数说明:电池电压初始化
  1318. 参数说明:
  1319. 返回值:
  1320. 0: 成功
  1321. 其它: 失败
  1322. 修改记录:
  1323. */
  1324. int udcrcd_init(void)
  1325. {
  1326. int i;
  1327. g_udcrcd.change_set = 0;
  1328. g_udcrcd.len = FILE_ADDR_ALGIN(sizeof(struct udc_rcd));
  1329. if (pRunSet->wUDCRL)
  1330. {
  1331. g_udcrcd.rcd_num = pRunSet->wUDCRL + 1;//30天
  1332. }
  1333. g_udcrcd.delay_s = 1;
  1334. g_udcrcd.no = 30;
  1335. memset(g_udc_rcd, 0, sizeof(g_udc_rcd));
  1336. memset(&g_udc_rcd[0], 0xFF, sizeof(struct udc_rcd));
  1337. i = pw_quality_file_init(UDCRCD_FILE);
  1338. if (i<0 && pRunSet->wUDCRL)
  1339. {
  1340. rt_printf("iret1 = %d\r\n", i);
  1341. i = udcrcd_create_file();
  1342. rt_file_del("/tmp/udcrcd.csv");
  1343. rt_printf("初始化电池电压记录文件, ret = %d\r\n", i);
  1344. }
  1345. return 0;
  1346. }
  1347. /******************************************************************************
  1348. 函数名称:udcrcd_create_file
  1349. 函数版本: 01.01
  1350. 创建作者:xxxxxx
  1351. 创建日期: 2014-09-05
  1352. 函数说明:创建电池电压记录文件
  1353. 参数说明:
  1354. 返回值:
  1355. 0: 成功
  1356. 其它: 失败
  1357. 修改记录:
  1358. */
  1359. int udcrcd_create_file(void)
  1360. {
  1361. unsigned int addr,file_length;
  1362. char *p;
  1363. loff_t pos;
  1364. struct udcrcd_file_head_data udcrcd_file;
  1365. // 写文件头
  1366. memset(&udcrcd_file,0,sizeof(udcrcd_file));
  1367. udcrcd_file.cfh.signature = SIG_UDCRCD_FILE;
  1368. udcrcd_file.cfh.version = FILE_VERSION;
  1369. udcrcd_file.size = pRunSet->wUDCRL;
  1370. udcrcd_file.addr = FILE_ADDR_ALGIN(sizeof(udcrcd_file));
  1371. udcrcd_file.num = g_udcrcd.rcd_num;
  1372. // 得到文件长度
  1373. addr = udcrcd_file.addr + udcrcd_file.num * g_udcrcd.len;
  1374. file_length = FILE_ADDR_ALGIN(addr);
  1375. // 分配并初始化空间
  1376. p = rt_malloc(file_length);
  1377. if(!p)
  1378. {
  1379. return -1;
  1380. }
  1381. memset(p,0,file_length);
  1382. // 写入文件头
  1383. memcpy(p, (char *)&udcrcd_file, sizeof(udcrcd_file));
  1384. // 写电度记录
  1385. memset(p+udcrcd_file.addr, 0xFF, g_udcrcd.len);//无效记录为全1
  1386. // 创建数据文件
  1387. g_pw_quality_file[UDCRCD_FILE].handle = rt_file_open(g_pw_quality_file[UDCRCD_FILE].name,O_CREAT|O_RDWR,0);
  1388. if(IS_ERR(g_pw_quality_file[UDCRCD_FILE].handle))
  1389. {
  1390. g_pw_quality_file[UDCRCD_FILE].handle = 0;
  1391. rt_printf("%s open error!\r\n",g_pw_quality_file[UDCRCD_FILE].name);
  1392. rt_free(p);
  1393. return -2;
  1394. }
  1395. pos = 0;
  1396. rt_file_write(g_pw_quality_file[UDCRCD_FILE].handle,p,file_length,&pos);
  1397. g_udcrcd.index = 0;
  1398. rt_free(p);
  1399. return 0;
  1400. }
  1401. /******************************************************************************
  1402. 函数名称:dcrcd_record
  1403. 函数版本: 01.01
  1404. 创建作者:xxxxxx
  1405. 创建日期: 2014-09-05
  1406. 函数说明:电池电压记录
  1407. 参数说明:
  1408. 返回值:
  1409. 0: 成功
  1410. 其它: 失败
  1411. 修改记录:
  1412. */
  1413. int udcrcd_record(u32 no)
  1414. {
  1415. int i=0;
  1416. struct timespec ts;
  1417. static u32 index_new;
  1418. //获取当前时间
  1419. if (no == 0)
  1420. {
  1421. clk_time_get(&ts);
  1422. timespec_to_rtc(ts,&g_udc_rcd[1].timemark,1);
  1423. //索引存储位置
  1424. index_new = g_udcrcd.index;
  1425. g_udcrcd.index++;
  1426. if (g_udcrcd.index >= g_udcrcd.rcd_num)
  1427. {
  1428. g_udcrcd.index=0;
  1429. }
  1430. }
  1431. g_udc_rcd[1].data[no] = rt_round((float)g_sw_pub.ac_in[PUB_AC_IN_UDC]/Q16_BASE);
  1432. if (no < (g_udcrcd.no-1))
  1433. {
  1434. return 0;
  1435. }
  1436. //更新记录
  1437. i = pw_quality_file_rcd (UDCRCD_FILE,
  1438. g_udcrcd.len,
  1439. sizeof(struct udcrcd_file_head_data),
  1440. index_new,
  1441. g_udcrcd.index,
  1442. (char*)&g_udc_rcd[1],
  1443. (char*)&g_udc_rcd[0]);
  1444. /*rt_printf("当前时间: %04d-%02d-%02d %02d:%02d:%02d:%09d!\r\n",
  1445. g_udc_rcd[1].timemark.year + 2000,
  1446. g_udc_rcd[1].timemark.month,
  1447. g_udc_rcd[1].timemark.day,
  1448. g_udc_rcd[1].timemark.hour,
  1449. g_udc_rcd[1].timemark.min,
  1450. g_udc_rcd[1].timemark.ms/1000,
  1451. g_udc_rcd[1].timemark.ms%1000
  1452. );
  1453. rt_printf("ret = %d, udcrcd_index = %d\r\n\r\n\r\n", i, g_udcrcd.index);*/
  1454. memset(&g_udc_rcd[1], 0, sizeof(struct udc_rcd));
  1455. return 0;
  1456. }
  1457. /******************************************************************************
  1458. 函数名称:udcrcd_create_csv
  1459. 函数版本: 01.01
  1460. 创建作者:xxxxxx
  1461. 创建日期: 2014-09-05
  1462. 函数说明:电池电压记录转换为CSV文件
  1463. 参数说明:
  1464. 返回值:
  1465. 0: 成功
  1466. 其它: 失败
  1467. 修改记录:
  1468. */
  1469. struct udc_rcd udcrcd;
  1470. int udcrcd_create_csv(void)
  1471. {
  1472. unsigned int addr, len1=0;
  1473. int i, j,loop;
  1474. char *buf;
  1475. struct udc_rcd *rcd;
  1476. char p[512];
  1477. loff_t pos;
  1478. struct file * pfile;
  1479. // 分配并初始化空间
  1480. len1 = g_udcrcd.len * g_udcrcd.rcd_num;
  1481. buf = rt_malloc(len1);
  1482. if(!buf)
  1483. {
  1484. return -1;
  1485. }
  1486. memset(buf,0,len1);
  1487. addr = sizeof(struct udcrcd_file_head_data);
  1488. pos = addr;
  1489. if(rt_file_read(g_pw_quality_file[UDCRCD_FILE].handle,buf,len1,&pos) != len1)
  1490. {
  1491. rt_free(buf);
  1492. return -2;
  1493. }
  1494. //需考虑打印文件时,同步存储记录的问题
  1495. // 创建数据文件
  1496. pos = 0;
  1497. pfile = rt_file_open("/tmp/udcrcd.csv",O_CREAT|O_RDWR,0);
  1498. if(IS_ERR(pfile))
  1499. {
  1500. rt_free(buf);
  1501. return -3;
  1502. }
  1503. //打印抬头
  1504. sprintf(p, "%s,电池电压记录文件,\r\n", EQUIP_TYPE_STRING);
  1505. rt_file_write(pfile,p,strlen(p),&pos);
  1506. //打印表格标题
  1507. sprintf(p, "记录时间,");
  1508. rt_file_write(pfile,p,strlen(p),&pos);
  1509. for(i=0; i<g_udcrcd.no; i++)
  1510. {
  1511. sprintf(p,"%ds,", i);
  1512. rt_file_write(pfile,p,strlen(p),&pos);
  1513. }
  1514. sprintf(p, "\r\n");
  1515. rt_file_write(pfile,p,strlen(p),&pos);
  1516. //打印记录
  1517. rcd = &udcrcd;
  1518. for(loop=g_udcrcd.rcd_num,i=g_udcrcd.index+1;loop>0; loop--,i++)
  1519. {
  1520. if(i >= g_udcrcd.rcd_num)
  1521. {
  1522. i=0;
  1523. }
  1524. //找出标示行下的最老记录行
  1525. memcpy(&udcrcd,buf+i* g_udcrcd.len,g_udcrcd.len);
  1526. if (rcd->timemark.month < 1|| rcd->timemark.month >12) //如果月份为0,非1~12之间,判定该条记录非法
  1527. {
  1528. continue;
  1529. }
  1530. sprintf(p, "%04d-%02d-%02d_%02d:%02d:%02d-%03d,",
  1531. rcd->timemark.year+2000,
  1532. rcd->timemark.month,
  1533. rcd->timemark.day,
  1534. rcd->timemark.hour,
  1535. rcd->timemark.min,
  1536. rcd->timemark.ms/1000,
  1537. rcd->timemark.ms%1000);
  1538. rt_file_write(pfile,p,strlen(p),&pos);
  1539. for (j=0; j<g_udcrcd.no; j++)
  1540. {
  1541. sprintf(p, "%2.3f,",rcd->data[j]);
  1542. rt_file_write(pfile,p,strlen(p),&pos);
  1543. }
  1544. sprintf(p, "\r\n");
  1545. rt_file_write(pfile,p,strlen(p),&pos);
  1546. }
  1547. // 关闭文件
  1548. rt_file_close(pfile,0);
  1549. rt_free(buf);
  1550. rt_printf("电池电压文件生成成功\r\n");
  1551. return 0;
  1552. }
  1553. /******************************************************************************
  1554. 函数名称:pw_quality_init
  1555. 函数版本: 01.01
  1556. 创建作者:xxxxxx
  1557. 创建日期: 2014-09-05
  1558. 函数说明:电能质量初始化
  1559. 参数说明:
  1560. 返回值:
  1561. 0: 成功
  1562. 其它: 失败
  1563. 修改记录:
  1564. */
  1565. int pw_quality_init(void)
  1566. {
  1567. g_pw_init = 1;
  1568. return 0;
  1569. }
  1570. //退出函数
  1571. int pw_quality_exit(void)
  1572. {
  1573. int i;
  1574. //循环关闭文件
  1575. for(i = 0; i< FILE_NUM; i++)
  1576. {
  1577. if(g_pw_quality_file[i].handle != 0)
  1578. {
  1579. rt_file_close(g_pw_quality_file[i].handle,0);
  1580. g_pw_quality_file[i].handle = 0;
  1581. }
  1582. }
  1583. return 0;
  1584. }
  1585. /******************************************************************************
  1586. 函数名称: pw_quality_file_init
  1587. 函数版本: 01.01
  1588. 创建作者: xxxxxx
  1589. 创建日期: 2013-08-08
  1590. 函数说明:初始化记录文件
  1591. 参数说明:
  1592. 返回值:
  1593. 0: 成功
  1594. 其它: 失败
  1595. 修改记录:
  1596. */
  1597. int pw_quality_file_init(u8 file_no)
  1598. {
  1599. u32 len;
  1600. int i,j=0, iret=0;
  1601. u8 *buf;
  1602. u8 *tmp;
  1603. loff_t pos;
  1604. // 打开文件
  1605. g_pw_quality_file[file_no].handle = rt_file_open(g_pw_quality_file[file_no].name,O_RDWR ,0);
  1606. if(IS_ERR(g_pw_quality_file[file_no].handle))
  1607. {
  1608. return -1;
  1609. }
  1610. // 得到文件长度
  1611. rt_file_llseek(g_pw_quality_file[file_no].handle,0,SEEK_END);
  1612. len = g_pw_quality_file[file_no].handle->f_pos;
  1613. rt_file_llseek(g_pw_quality_file[file_no].handle,0,SEEK_SET);
  1614. // 分配内存
  1615. buf = rt_malloc(len);
  1616. if((buf) == NULL)
  1617. {
  1618. rt_file_close(g_pw_quality_file[file_no].handle,0);
  1619. return -2;
  1620. }
  1621. //保存原指针,便于以后释放内存
  1622. tmp = buf;
  1623. // 读出内容
  1624. pos = 0;
  1625. if(rt_file_read(g_pw_quality_file[file_no].handle,buf,len,&pos) != len)
  1626. {
  1627. rt_file_close(g_pw_quality_file[file_no].handle,0);
  1628. rt_free(buf);
  1629. return -3;
  1630. }
  1631. switch(file_no)
  1632. {
  1633. case ENERGY_FILE:
  1634. {
  1635. struct energy_file_head_data *brh;
  1636. // 检查文件签名
  1637. brh = (struct energy_file_head_data*)buf;
  1638. if(brh->cfh.signature != SIG_ENERGY_FILE)
  1639. {
  1640. rt_free(buf);
  1641. return -4;
  1642. }
  1643. // 检查文件长度
  1644. if(len < sizeof(*brh) + brh->num*g_energy.len)
  1645. {
  1646. rt_free(buf);
  1647. return -5;
  1648. }
  1649. if (pRunSet->wDDRL == 0)
  1650. {
  1651. rt_file_del(g_pw_quality_file[file_no].name);
  1652. rt_file_del("/tmp/energy.csv");
  1653. rt_printf("删除电度记录文件\r\n");
  1654. rt_free(buf);
  1655. return 1;
  1656. }
  1657. if ((brh->interval != g_energy.delay_s)
  1658. || (brh->size != pRunSet->wDDRL)
  1659. || (brh->swnum != g_energy.mea_num))
  1660. {
  1661. rt_free(buf);
  1662. return -6;
  1663. }
  1664. buf += brh->addr;
  1665. g_energy.index = _index_check(brh->num, g_energy.len, buf);
  1666. }
  1667. break;
  1668. case EXTREMA_FILE:
  1669. {
  1670. struct extrema_file_head_data *brh;
  1671. struct rsc *extrema_rsc;
  1672. // 检查文件签名
  1673. brh = (struct extrema_file_head_data*)buf;
  1674. if(brh->cfh.signature != SIG_EXTREMA_FILE)
  1675. {
  1676. rt_free(buf);
  1677. return -7;
  1678. }
  1679. // 检查文件长度
  1680. if(len < sizeof(*brh) + g_table_head->ac_num*sizeof(struct rsc) + brh->num*g_extrema.len)
  1681. {
  1682. rt_free(buf);
  1683. return -8;
  1684. }
  1685. if (pRunSet->wJZRL == 0)
  1686. {
  1687. rt_file_del(g_pw_quality_file[file_no].name);
  1688. rt_file_del("/tmp/extrema.csv");
  1689. rt_printf("删除极值记录文件\r\n");
  1690. rt_free(buf);
  1691. return 2;
  1692. }
  1693. if ((brh->size != pRunSet->wJZRL)
  1694. || (brh->mea_num != g_extrema.mea_num))
  1695. {
  1696. rt_free(buf);
  1697. return -9;
  1698. }
  1699. //检查点表CRC和内容
  1700. if (brh->rsc_crc != g_crc_rectable)
  1701. {
  1702. extrema_rsc = (struct rsc *)(buf+brh->addr_desc);
  1703. for (i=0; i<g_extrema.mea_num; i++)
  1704. {
  1705. iret = memcmp(&extrema_rsc[i], &g_ac_table[i], 5);
  1706. if (iret != 0)
  1707. {
  1708. rt_free(buf);
  1709. return -10;
  1710. }
  1711. }
  1712. }
  1713. //检索无效记录的索引号
  1714. buf += brh->addr;
  1715. g_extrema.index = _index_check(brh->num, g_extrema.len, buf);
  1716. }
  1717. break;
  1718. case MEARCD_FILE:
  1719. {
  1720. struct mearcd_file_head_data *brh;
  1721. struct rsc *mec_rsc;
  1722. // 检查文件签名
  1723. brh = (struct mearcd_file_head_data*)buf;
  1724. if(brh->cfh.signature != SIG_MEARCD_FILE)
  1725. {
  1726. rt_free(buf);
  1727. return -11;
  1728. }
  1729. // 检查文件长度
  1730. if(len < sizeof(*brh) + g_table_head->ac_num*sizeof(struct rsc) + brh->num*g_mearcd.len)
  1731. {
  1732. rt_free(buf);
  1733. return -12;
  1734. }
  1735. if (pRunSet->wZDRL == 0)
  1736. {
  1737. rt_file_del(g_pw_quality_file[file_no].name);
  1738. rt_file_del("/tmp/mearcd.csv");
  1739. rt_printf("删除定点遥测记录文件\r\n");
  1740. rt_free(buf);
  1741. return 3;
  1742. }
  1743. if ((brh->interval != g_mearcd.delay_s)
  1744. || (brh->size != pRunSet->wZDRL)
  1745. || (brh->mea_num != g_mearcd.mea_num))
  1746. {
  1747. rt_free(buf);
  1748. return -13;
  1749. }
  1750. //检查点表CRC和内容
  1751. if (brh->rsc_crc != g_crc_rectable)
  1752. {
  1753. mec_rsc = (struct rsc *)(buf+brh->addr_desc);
  1754. for (i=0; i<g_mearcd.mea_num; i++)
  1755. {
  1756. iret = memcmp(&mec_rsc[i], &g_ac_table[i], 5);
  1757. if (iret != 0)
  1758. {
  1759. rt_free(buf);
  1760. return -14;
  1761. }
  1762. }
  1763. }
  1764. //检索无效记录的索引号
  1765. buf += brh->addr;
  1766. g_mearcd.index = _index_check(brh->num, g_mearcd.len, buf);
  1767. }
  1768. break;
  1769. case CAP_FILE:
  1770. {
  1771. struct capmanage_file_head_data *brh;
  1772. struct rsc *cap_rsc;
  1773. // 检查文件签名
  1774. brh = (struct capmanage_file_head_data*)buf;
  1775. if(brh->cfh.signature != SIG_CAPRCD_FILE)
  1776. {
  1777. rt_free(buf);
  1778. return -15;
  1779. }
  1780. // 检查文件长度
  1781. if(len < sizeof(*brh) + g_capmanage.mea_num*sizeof(struct rsc) + brh->num*g_capmanage.len)
  1782. {
  1783. rt_free(buf);
  1784. return -16;
  1785. }
  1786. if (pRunSet->wCAPRL == 0)
  1787. {
  1788. rt_file_del(g_pw_quality_file[file_no].name);
  1789. rt_file_del("/tmp/cap_manage.csv");
  1790. rt_printf("删除电容器管理文件\r\n");
  1791. rt_free(buf);
  1792. return 4;
  1793. }
  1794. if ((brh->size != pRunSet->wCAPRL)
  1795. || (brh->meanum != g_capmanage.mea_num))
  1796. {
  1797. rt_free(buf);
  1798. return -17;
  1799. }
  1800. //检查点表CRC和内容
  1801. if (brh->rsc_crc != g_crc_rectable)
  1802. {
  1803. cap_rsc = (struct rsc *)(buf+brh->addr_desc);
  1804. for (i=0; i<g_mearcd.mea_num; i++)
  1805. {
  1806. if (g_ac_table[i].owner == 0)
  1807. {
  1808. if (((g_ac_table[i].indexno-1)>=PUB_AC_UAB1 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA1)
  1809. || ((g_ac_table[i].indexno-1)>=PUB_AC_UAB2 && (g_ac_table[i].indexno-1)<=PUB_AC_UCA2))
  1810. {
  1811. iret = memcmp(&cap_rsc[j], &g_ac_table[j], 5);
  1812. j++;
  1813. }
  1814. }
  1815. else
  1816. {
  1817. if ((g_ac_table[i].indexno-1)==(SW_AC_IN_Q) || (g_ac_table[i].indexno-1)==(SW_AC_IN_COS))
  1818. {
  1819. iret = memcmp(&cap_rsc[j], &g_ac_table[j], 5);
  1820. j++;
  1821. }
  1822. }
  1823. if (iret != 0)
  1824. {
  1825. rt_free(buf);
  1826. return -18;
  1827. }
  1828. }
  1829. }
  1830. //检索无效记录的索引号
  1831. buf += brh->addr;
  1832. g_capmanage.index = _index_check(brh->num, g_capmanage.len, buf);
  1833. }
  1834. break;
  1835. case UDCRCD_FILE:
  1836. {
  1837. struct udcrcd_file_head_data *brh;
  1838. // 检查文件签名
  1839. brh = (struct udcrcd_file_head_data*)buf;
  1840. if(brh->cfh.signature != SIG_UDCRCD_FILE)
  1841. {
  1842. rt_free(buf);
  1843. return -4;
  1844. }
  1845. // 检查文件长度
  1846. if(len < sizeof(*brh) + brh->num*g_udcrcd.len)
  1847. {
  1848. rt_free(buf);
  1849. return -5;
  1850. }
  1851. if (pRunSet->wUDCRL == 0)
  1852. {
  1853. rt_file_del(g_pw_quality_file[file_no].name);
  1854. rt_file_del("/tmp/udcrcd.csv");
  1855. rt_printf("删除电池电压记录文件\r\n");
  1856. rt_free(buf);
  1857. return 1;
  1858. }
  1859. if (brh->size != pRunSet->wUDCRL)
  1860. {
  1861. rt_free(buf);
  1862. return -6;
  1863. }
  1864. buf += brh->addr;
  1865. g_udcrcd.index = _index_check(brh->num, g_udcrcd.len, buf);
  1866. }
  1867. break;
  1868. }
  1869. buf = tmp;
  1870. //释放内存
  1871. rt_free(buf);
  1872. return 0;
  1873. }
  1874. /******************************************************************************
  1875. 函数名称:pw_quality_file_rcd
  1876. 函数版本: 01.01
  1877. 创建作者:xxxxxx
  1878. 创建日期: 2014-09-05
  1879. 函数说明:更新记录
  1880. 参数说明:
  1881. 返回值:
  1882. 0: 成功
  1883. 其它: 失败
  1884. 修改记录:
  1885. */
  1886. int pw_quality_file_rcd(u8 file_no, u32 len, u32 size, u32 index_new, u32 index_null, char *new_rcd, char *nul_rcd )
  1887. {
  1888. loff_t pos;
  1889. if (g_pw_quality_file[file_no].handle == 0
  1890. || new_rcd == 0 || nul_rcd == 0)
  1891. {
  1892. return -1;
  1893. }
  1894. // 写无效记录
  1895. pos = size + index_null * len;
  1896. //rt_printf("index_null = %d, pos1 = %d\r\n", index_null, (int)pos);
  1897. if(rt_file_write(g_pw_quality_file[file_no].handle, nul_rcd, len, &pos) != len)
  1898. {
  1899. return -3;
  1900. }
  1901. // 写新记录
  1902. pos = size + index_new * len;
  1903. //rt_printf("index_new = %d, pos2 = %d\r\n", index_new, (int)pos);
  1904. if(rt_file_write(g_pw_quality_file[file_no].handle,new_rcd, len, &pos) != len)
  1905. {
  1906. return -4;
  1907. }
  1908. return 0;
  1909. }
  1910. /******************************************************************************
  1911. 函数名称:pw_quality_app
  1912. 函数版本: 01.01
  1913. 创建作者:xxxxxx
  1914. 创建日期: 2014-09-05
  1915. 函数说明:电能质量应用程序
  1916. 参数说明:
  1917. 返回值:
  1918. 0: 成功
  1919. 其它: 失败
  1920. 修改记录:
  1921. */
  1922. static u32 extrema_delay=0;
  1923. static u32 time100us=0;
  1924. static u32 energy_p_delay=0;
  1925. static u32 energy_p_rcddelay=0;
  1926. static u32 mearcd_delay=0;
  1927. static u32 cap_delay=0;
  1928. static u32 caprcd_flag = 0;
  1929. static u32 udcrcd_delay = 0;
  1930. static u32 udcrcd_no=0;
  1931. static u32 init_count = 5*10;
  1932. int pw_quality_app(void)
  1933. {
  1934. u32 us0;
  1935. if((g_pw_init == 0) || (!pRunSet->bTT_Pwquality))
  1936. {
  1937. return 0;
  1938. }
  1939. while(init_count)
  1940. {
  1941. energy_p_delay = ustimer_get_origin();
  1942. init_count--;
  1943. return 0;
  1944. }
  1945. time100us++;
  1946. us0 = ustimer_get_origin();
  1947. if ((int)(us0 - energy_p_delay) >= 0)
  1948. {
  1949. if (pRunSet->wDDRL)
  1950. {
  1951. energy_calc();
  1952. }
  1953. energy_p_delay += USTIMER_SEC;
  1954. energy_p_rcddelay++;
  1955. extrema_delay++;
  1956. mearcd_delay++;
  1957. cap_delay++;
  1958. udcrcd_delay++;
  1959. }
  1960. if (energy_p_rcddelay >= g_energy.delay_s)
  1961. {
  1962. if (pRunSet->wDDRL)
  1963. {
  1964. energy_record();
  1965. }
  1966. energy_p_rcddelay = 0;
  1967. }
  1968. if (extrema_delay >= 1)
  1969. {
  1970. if (pRunSet->wJZRL)
  1971. {
  1972. extrema_calc();
  1973. }
  1974. extrema_delay = 0;
  1975. }
  1976. if (mearcd_delay >= g_mearcd.delay_s)
  1977. {
  1978. if (pRunSet->wZDRL)
  1979. {
  1980. mearcd_record();
  1981. }
  1982. mearcd_delay = 0;
  1983. }
  1984. if (GetRmtYxVal(0,PUB_DI_CAP+1) == SW_DI_TYPE_ON)
  1985. {
  1986. caprcd_flag = 1;
  1987. }
  1988. if (!caprcd_flag)
  1989. {
  1990. capmanage_calc();
  1991. cap_delay = 0;
  1992. }
  1993. if (caprcd_flag && (cap_delay >= g_capmanage.delay_s))
  1994. {
  1995. if (pRunSet->wCAPRL)
  1996. {
  1997. capmanage_calc();
  1998. capmanage_record();
  1999. caprcd_flag = 0;
  2000. }
  2001. cap_delay = 0;
  2002. }
  2003. if (udcrcd_delay >= g_udcrcd.delay_s)
  2004. {
  2005. if (g_udcrcd.begin)
  2006. {
  2007. udcrcd_record(udcrcd_no);
  2008. udcrcd_no++;
  2009. if (udcrcd_no == g_udcrcd.no)
  2010. {
  2011. g_udcrcd.begin = 0;
  2012. udcrcd_no = 0;
  2013. }
  2014. }
  2015. udcrcd_delay = 0;
  2016. }
  2017. return 0;
  2018. }
  2019. /*------------------------------ 内部函数 -------------------------------------
  2020. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  2021. */
  2022. static int _index_check(u32 num, u32 len, u8 *buf)
  2023. {
  2024. int i, index;
  2025. for (i=0; i<num; i++)
  2026. {
  2027. struct rtc_time_t *time;
  2028. time = (struct rtc_time_t *)buf;
  2029. if (time->month < 1|| time->month >12) //做一个判断日期非法的通用函数,
  2030. {
  2031. index = i;
  2032. break;
  2033. }
  2034. else
  2035. {
  2036. buf += len;
  2037. }
  2038. }
  2039. return index;
  2040. }
  2041. static void _get_ac_name(u16 owner,u16 index,char *name)
  2042. {
  2043. if(owner==0)
  2044. {
  2045. strcpy(name, g_pub_ac_desc[index-1].name);
  2046. }
  2047. else
  2048. {
  2049. sprintf(name,"%s_%d", g_sw_ac_desc[index-1].name, owner);
  2050. }
  2051. }
  2052. /*------------------------------ 文件结束 -------------------------------------
  2053. */