Advertisement
Guest User

sbfspot_json1

a guest
Nov 1st, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.71 KB | None | 0 0
  1. diff -u SBFspot/SBFspot.cpp ../SBFspot/SBFspot.cpp
  2. --- SBFspot/SBFspot.cpp 2016-05-31 23:01:18.000000000 +0200
  3. +++ ../SBFspot/SBFspot.cpp 2016-11-01 11:59:13.324047366 +0100
  4. @@ -108,6 +108,8 @@
  5. char msg[80];
  6.  
  7. int rc = 0;
  8. + int prevJSObj = 0;
  9. + int prevJSArrayObj = 0;
  10.  
  11. Config cfg;
  12.  
  13. @@ -255,23 +257,30 @@
  14. return rc;
  15. }
  16.  
  17. + //json start tag
  18. + if (cfg.json == 1)
  19. + printf("{\n");
  20. +
  21. // Synchronize plant time with system time
  22. // Only BT connected devices and if enabled in config _or_ requested by 123Solar
  23. // Most probably Speedwire devices get their time from the local IP network
  24. if ((ConnType == CT_BLUETOOTH) && (cfg.synchTime > 0 || cfg.s123 == S123_SYNC ))
  25. - if ((rc = SetPlantTime(cfg.synchTime, cfg.synchTimeLow, cfg.synchTimeHigh)) != E_OK)
  26. + if ((rc = SetPlantTime(cfg.synchTime, cfg.synchTimeLow, cfg.synchTimeHigh)) != E_OK && (cfg.json != 1))
  27. printf("SetPlantTime returned an error: %d\n", rc);
  28.  
  29. - if ((rc = getInverterData(Inverters, sbftest)) != 0)
  30. + if ((rc = getInverterData(Inverters, sbftest)) != 0 && (cfg.json != 1))
  31. printf("getInverterData(sbftest) returned an error: %d\n", rc);
  32.  
  33. - if ((rc = getInverterData(Inverters, SoftwareVersion)) != 0)
  34. + if ((rc = getInverterData(Inverters, SoftwareVersion)) != 0 && (cfg.json != 1))
  35. printf("getSoftwareVersion returned an error: %d\n", rc);
  36.  
  37. - if ((rc = getInverterData(Inverters, TypeLabel)) != 0)
  38. + if ((rc = getInverterData(Inverters, TypeLabel)) != 0 && (cfg.json != 1))
  39. printf("getTypeLabel returned an error: %d\n", rc);
  40. else
  41. {
  42. + if (cfg.json == 1)
  43. + printf(" \"device\": [\n");
  44. +
  45. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  46. {
  47. if (VERBOSE_NORMAL)
  48. @@ -283,13 +292,41 @@
  49. printf("Software Version: %s\n", Inverters[inv]->SWVersion);
  50. printf("Serial number: %lu\n", Inverters[inv]->Serial);
  51. }
  52. + else if (cfg.json == 1)
  53. + {
  54. + if (inv > 0)
  55. + printf(",\n {\n");
  56. + else
  57. + printf(" {\n");
  58. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  59. + printf(" \"deviceName\": \"%s\",\n", Inverters[inv]->DeviceName);
  60. + printf(" \"deviceClass\": \"%s\",\n", Inverters[inv]->DeviceClass);
  61. + printf(" \"deviceType\": \"%s\",\n", Inverters[inv]->DeviceType);
  62. + printf(" \"softwareVersion\": \"%s\",\n", Inverters[inv]->SWVersion);
  63. + printf(" \"serialNumber\": %lu\n", Inverters[inv]->Serial);
  64. + printf(" }\n");
  65. + }
  66. + }
  67. +
  68. + if (cfg.json == 1)
  69. + {
  70. + printf(" ]");
  71. + prevJSObj = 1;
  72. }
  73. }
  74.  
  75. - if ((rc = getInverterData(Inverters, BatteryChargeStatus)) != 0)
  76. + if ((rc = getInverterData(Inverters, BatteryChargeStatus)) != 0 && (cfg.json != 1))
  77. printf("getBatteryChargeStatus returned an error: %d\n", rc);
  78. else
  79. {
  80. + if (cfg.json == 1)
  81. + {
  82. + if (prevJSObj)
  83. + printf(",\n \"batteryChargingStatus\": [\n");
  84. + else
  85. + printf(" \"batteryChargingStatus\": [\n");
  86. + }
  87. +
  88. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  89. {
  90. if (Inverters[inv]->DevClass == BatteryInverter)
  91. @@ -299,14 +336,42 @@
  92. printf("SUSyID: %d - SN: %lu\n", Inverters[inv]->SUSyID, Inverters[inv]->Serial);
  93. printf("Batt. Charging Status: %lu%%\n", Inverters[inv]->BatChaStt);
  94. }
  95. + else if (cfg.json == 1)
  96. + {
  97. + if (inv > 0 && prevJSArrayObj)
  98. + printf(",\n {\n");
  99. + else
  100. + {
  101. + printf(" {\n");
  102. + prevJSArrayObj = 1;
  103. + }
  104. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  105. + printf(" \"status\": %lu%%\n", Inverters[inv]->BatChaStt);
  106. + printf(" }\n");
  107. + }
  108. }
  109. }
  110. +
  111. + if (cfg.json == 1)
  112. + {
  113. + printf(" ]");
  114. + prevJSObj = 1;
  115. + prevJSArrayObj = 0;
  116. + }
  117. }
  118.  
  119. - if ((rc = getInverterData(Inverters, BatteryInfo)) != 0)
  120. + if ((rc = getInverterData(Inverters, BatteryInfo)) != 0 && (cfg.json != 1))
  121. printf("getBatteryInfo returned an error: %d\n", rc);
  122. else
  123. {
  124. + if (cfg.json == 1)
  125. + {
  126. + if (prevJSObj)
  127. + printf(",\n \"batteryInfo\": [\n");
  128. + else
  129. + printf(" \"batteryInfo\": [\n");
  130. + }
  131. +
  132. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  133. {
  134. if (Inverters[inv]->DevClass == BatteryInverter)
  135. @@ -318,14 +383,44 @@
  136. printf("Batt. Voltage : %3.2fV\n", toVolt(Inverters[inv]->BatVol));
  137. printf("Batt. Current : %2.3fA\n", toAmp(Inverters[inv]->BatAmp));
  138. }
  139. + else if (cfg.json == 1)
  140. + {
  141. + if (inv > 0 && prevJSArrayObj)
  142. + printf(",\n {\n");
  143. + else
  144. + {
  145. + printf(" {\n");
  146. + prevJSArrayObj = 1;
  147. + }
  148. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  149. + printf(" \"temperature\": %3.1f,\n", (float)(Inverters[inv]->BatTmpVal / 10));
  150. + printf(" \"voltage\": %3.2f,\n", toVolt(Inverters[inv]->BatVol));
  151. + printf(" \"current\": %2.3f\n", toAmp(Inverters[inv]->BatAmp));
  152. + printf(" }\n");
  153. + }
  154. }
  155. }
  156. +
  157. + if (cfg.json == 1)
  158. + {
  159. + printf(" ]");
  160. + prevJSObj = 1;
  161. + prevJSArrayObj = 0;
  162. + }
  163. }
  164.  
  165. - if ((rc = getInverterData(Inverters, DeviceStatus)) != 0)
  166. + if ((rc = getInverterData(Inverters, DeviceStatus)) != 0 && (cfg.json != 1))
  167. printf("getDeviceStatus returned an error: %d\n", rc);
  168. else
  169. {
  170. + if (cfg.json == 1)
  171. + {
  172. + if (prevJSObj)
  173. + printf(",\n \"deviceStatus\": [\n");
  174. + else
  175. + printf(" \"deviceStatus\": [\n");
  176. + }
  177. +
  178. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  179. {
  180. if (VERBOSE_NORMAL)
  181. @@ -333,13 +428,37 @@
  182. printf("SUSyID: %d - SN: %lu\n", Inverters[inv]->SUSyID, Inverters[inv]->Serial);
  183. printf("Device Status: %s\n", tagdefs.getDesc(Inverters[inv]->DeviceStatus, "?").c_str());
  184. }
  185. + else if (cfg.json == 1)
  186. + {
  187. + if (inv > 0)
  188. + printf(",\n {\n");
  189. + else
  190. + printf(" {\n");
  191. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  192. + printf(" \"status\": \"%s\"\n", tagdefs.getDesc(Inverters[inv]->DeviceStatus, "?").c_str());
  193. + printf(" }\n");
  194. + }
  195. + }
  196. +
  197. + if (cfg.json == 1)
  198. + {
  199. + printf(" ]");
  200. + prevJSObj = 1;
  201. }
  202. }
  203.  
  204. - if ((rc = getInverterData(Inverters, InverterTemperature)) != 0)
  205. + if ((rc = getInverterData(Inverters, InverterTemperature)) != 0 && (cfg.json != 1))
  206. printf("getInverterTemperature returned an error: %d\n", rc);
  207. else
  208. {
  209. + if (cfg.json == 1)
  210. + {
  211. + if (prevJSObj)
  212. + printf(",\n \"inverterTemperature\": [\n");
  213. + else
  214. + printf(" \"inverterTemperature\": [\n");
  215. + }
  216. +
  217. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  218. {
  219. if (VERBOSE_NORMAL)
  220. @@ -347,15 +466,39 @@
  221. printf("SUSyID: %d - SN: %lu\n", Inverters[inv]->SUSyID, Inverters[inv]->Serial);
  222. printf("Device Temperature: %3.1f%sC\n", ((float)Inverters[inv]->Temperature / 100), SYM_DEGREE); // degree symbol is different on windows/linux
  223. }
  224. + else if (cfg.json == 1)
  225. + {
  226. + if (inv > 0)
  227. + printf(",\n {\n");
  228. + else
  229. + printf(" {\n");
  230. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  231. + printf(" \"deviceTemperature\": %3.1f\n", ((float)Inverters[inv]->Temperature / 100));
  232. + printf(" }\n");
  233. + }
  234. + }
  235. +
  236. + if (cfg.json == 1)
  237. + {
  238. + printf(" ]");
  239. + prevJSObj = 1;
  240. }
  241. }
  242.  
  243. if (Inverters[0]->DevClass == SolarInverter)
  244. {
  245. - if ((rc = getInverterData(Inverters, GridRelayStatus)) != 0)
  246. + if ((rc = getInverterData(Inverters, GridRelayStatus)) != 0 && (cfg.json != 1))
  247. printf("getGridRelayStatus returned an error: %d\n", rc);
  248. else
  249. {
  250. + if (cfg.json == 1)
  251. + {
  252. + if (prevJSObj)
  253. + printf(",\n \"gridRelayStatus\": [\n");
  254. + else
  255. + printf(" \"gridRelayStatus\": [\n");
  256. + }
  257. +
  258. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  259. {
  260. if (Inverters[inv]->DevClass == SolarInverter)
  261. @@ -365,20 +508,48 @@
  262. printf("SUSyID: %d - SN: %lu\n", Inverters[inv]->SUSyID, Inverters[inv]->Serial);
  263. printf("GridRelay Status: %s\n", tagdefs.getDesc(Inverters[inv]->GridRelayStatus, "?").c_str());
  264. }
  265. + else if (cfg.json == 1)
  266. + {
  267. + if (inv > 0 && prevJSArrayObj)
  268. + printf(",\n {\n");
  269. + else
  270. + {
  271. + printf(" {\n");
  272. + prevJSArrayObj = 1;
  273. + }
  274. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  275. + printf(" \"status\": \"%s\"\n", tagdefs.getDesc(Inverters[inv]->GridRelayStatus, "?").c_str());
  276. + printf(" }\n");
  277. + }
  278. }
  279. }
  280. +
  281. + if (cfg.json == 1)
  282. + {
  283. + printf(" ]");
  284. + prevJSObj = 1;
  285. + prevJSArrayObj = 0;
  286. + }
  287. }
  288. }
  289.  
  290. - if ((rc = getInverterData(Inverters, MaxACPower)) != 0)
  291. + if ((rc = getInverterData(Inverters, MaxACPower)) != 0 && (cfg.json != 1))
  292. printf("getMaxACPower returned an error: %d\n", rc);
  293. else
  294. {
  295. //TODO: REVIEW THIS PART (getMaxACPower & getMaxACPower2 should be 1 function)
  296. - if ((Inverters[0]->Pmax1 == 0) && (rc = getInverterData(Inverters, MaxACPower2)) != 0)
  297. + if ((Inverters[0]->Pmax1 == 0) && (rc = getInverterData(Inverters, MaxACPower2)) != 0 && (cfg.json != 1))
  298. printf("getMaxACPower2 returned an error: %d\n", rc);
  299. else
  300. {
  301. + if (cfg.json == 1)
  302. + {
  303. + if (prevJSObj)
  304. + printf(",\n \"maxACPower\": [\n");
  305. + else
  306. + printf(" \"maxACPower\": [\n");
  307. + }
  308. +
  309. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  310. {
  311. if (VERBOSE_NORMAL)
  312. @@ -388,17 +559,43 @@
  313. printf("Pac max phase 2: %luW\n", Inverters[inv]->Pmax2);
  314. printf("Pac max phase 3: %luW\n", Inverters[inv]->Pmax3);
  315. }
  316. + else if (cfg.json == 1)
  317. + {
  318. + if (inv > 0)
  319. + printf(",\n {\n");
  320. + else
  321. + printf(" {\n");
  322. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  323. + printf(" \"pMaxPhase1\": %lu,\n", Inverters[inv]->Pmax1);
  324. + printf(" \"pMaxPhase2\": %lu,\n", Inverters[inv]->Pmax2);
  325. + printf(" \"pMaxPhase3\": %lu\n", Inverters[inv]->Pmax3);
  326. + printf(" }\n");
  327. + }
  328. + }
  329. +
  330. + if (cfg.json == 1)
  331. + {
  332. + printf(" ]");
  333. + prevJSObj = 1;
  334. }
  335. }
  336. }
  337.  
  338. - if ((rc = getInverterData(Inverters, EnergyProduction)) != 0)
  339. + if ((rc = getInverterData(Inverters, EnergyProduction)) != 0 && (cfg.json != 1))
  340. printf("getEnergyProduction returned an error: %d\n", rc);
  341.  
  342. - if ((rc = getInverterData(Inverters, OperationTime)) != 0)
  343. + if ((rc = getInverterData(Inverters, OperationTime)) != 0 && (cfg.json != 1))
  344. printf("getOperationTime returned an error: %d\n", rc);
  345. else
  346. {
  347. + if (cfg.json == 1)
  348. + {
  349. + if (prevJSObj)
  350. + printf(",\n \"energyProduction\": [\n");
  351. + else
  352. + printf(" \"energyProduction\": [\n");
  353. + }
  354. +
  355. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  356. {
  357. if (VERBOSE_NORMAL)
  358. @@ -410,19 +607,46 @@
  359. printf("\tOperation Time: %.2fh\n", toHour(Inverters[inv]->OperationTime));
  360. printf("\tFeed-In Time : %.2fh\n", toHour(Inverters[inv]->FeedInTime));
  361. }
  362. + else if (cfg.json == 1)
  363. + {
  364. + if (inv > 0)
  365. + printf(",\n {\n");
  366. + else
  367. + printf(" {\n");
  368. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  369. + printf(" \"eToday\": %lld,\n", Inverters[inv]->EToday);
  370. + printf(" \"eTotal\": %lld,\n", Inverters[inv]->ETotal);
  371. + printf(" \"operationTime\": %.2f,\n", toHour(Inverters[inv]->OperationTime));
  372. + printf(" \"feedInTime\": %.2f\n", toHour(Inverters[inv]->FeedInTime));
  373. + printf(" }\n");
  374. + }
  375. + }
  376. +
  377. + if (cfg.json == 1)
  378. + {
  379. + printf(" ]");
  380. + prevJSObj = 1;
  381. }
  382. }
  383.  
  384. - if ((rc = getInverterData(Inverters, SpotDCPower)) != 0)
  385. + if ((rc = getInverterData(Inverters, SpotDCPower)) != 0 && (cfg.json != 1))
  386. printf("getSpotDCPower returned an error: %d\n", rc);
  387.  
  388. - if ((rc = getInverterData(Inverters, SpotDCVoltage)) != 0)
  389. + if ((rc = getInverterData(Inverters, SpotDCVoltage)) != 0 && (cfg.json != 1))
  390. printf("getSpotDCVoltage returned an error: %d\n", rc);
  391.  
  392. //Calculate missing DC Spot Values
  393. if (cfg.calcMissingSpot == 1)
  394. CalcMissingSpot(Inverters[0]);
  395.  
  396. + if (cfg.json == 1)
  397. + {
  398. + if (prevJSObj)
  399. + printf(",\n \"dcSpotData\": [\n");
  400. + else
  401. + printf(" \"dcSpotData\": [\n");
  402. + }
  403. +
  404. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  405. {
  406. Inverters[inv]->calPdcTot = Inverters[inv]->Pdc1 + Inverters[inv]->Pdc2;
  407. @@ -433,21 +657,54 @@
  408. printf("\tString 1 Pdc: %7.3fkW - Udc: %6.2fV - Idc: %6.3fA\n", tokW(Inverters[inv]->Pdc1), toVolt(Inverters[inv]->Udc1), toAmp(Inverters[inv]->Idc1));
  409. printf("\tString 2 Pdc: %7.3fkW - Udc: %6.2fV - Idc: %6.3fA\n", tokW(Inverters[inv]->Pdc2), toVolt(Inverters[inv]->Udc2), toAmp(Inverters[inv]->Idc2));
  410. }
  411. + else if (cfg.json == 1)
  412. + {
  413. + if (inv > 0)
  414. + printf(",\n {\n");
  415. + else
  416. + printf(" {\n");
  417. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  418. + printf(" \"string1\": {\n");
  419. + printf(" \"p\": %ld,\n", Inverters[inv]->Pdc1);
  420. + printf(" \"u\": %6.2f,\n", toVolt(Inverters[inv]->Udc1));
  421. + printf(" \"i\": %6.3f\n", toAmp(Inverters[inv]->Idc1));
  422. + printf(" },\n");
  423. + printf(" \"string2\": {\n");
  424. + printf(" \"p\": %ld,\n", Inverters[inv]->Pdc2);
  425. + printf(" \"u\": %6.2f,\n", toVolt(Inverters[inv]->Udc2));
  426. + printf(" \"i\": %6.3f\n", toAmp(Inverters[inv]->Idc2));
  427. + printf(" }\n");
  428. + printf(" }\n");
  429. + }
  430. + }
  431. +
  432. + if (cfg.json == 1)
  433. + {
  434. + printf(" ]");
  435. + prevJSObj = 1;
  436. }
  437.  
  438. - if ((rc = getInverterData(Inverters, SpotACPower)) != 0)
  439. + if ((rc = getInverterData(Inverters, SpotACPower)) != 0 && (cfg.json != 1))
  440. printf("getSpotACPower returned an error: %d\n", rc);
  441.  
  442. - if ((rc = getInverterData(Inverters, SpotACVoltage)) != 0)
  443. + if ((rc = getInverterData(Inverters, SpotACVoltage)) != 0 && (cfg.json != 1))
  444. printf("getSpotACVoltage returned an error: %d\n", rc);
  445.  
  446. - if ((rc = getInverterData(Inverters, SpotACTotalPower)) != 0)
  447. + if ((rc = getInverterData(Inverters, SpotACTotalPower)) != 0 && (cfg.json != 1))
  448. printf("getSpotACTotalPower returned an error: %d\n", rc);
  449.  
  450. //Calculate missing AC Spot Values
  451. if (cfg.calcMissingSpot == 1)
  452. CalcMissingSpot(Inverters[0]);
  453.  
  454. + if (cfg.json == 1)
  455. + {
  456. + if (prevJSObj)
  457. + printf(",\n \"acSpotData\": [\n");
  458. + else
  459. + printf(" \"acSpotData\": [\n");
  460. + }
  461. +
  462. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  463. {
  464. if (VERBOSE_NORMAL)
  465. @@ -459,12 +716,51 @@
  466. printf("\tPhase 3 Pac : %7.3fkW - Uac: %6.2fV - Iac: %6.3fA\n", tokW(Inverters[inv]->Pac3), toVolt(Inverters[inv]->Uac3), toAmp(Inverters[inv]->Iac3));
  467. printf("\tTotal Pac : %7.3fkW\n", tokW(Inverters[inv]->TotalPac));
  468. }
  469. + else if (cfg.json == 1)
  470. + {
  471. + if (inv > 0)
  472. + printf(",\n {\n");
  473. + else
  474. + printf(" {\n");
  475. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  476. + printf(" \"phase1\": {\n");
  477. + printf(" \"p\": %ld,\n", Inverters[inv]->Pac1);
  478. + printf(" \"u\": %6.2f,\n", toVolt(Inverters[inv]->Uac1));
  479. + printf(" \"i\": %6.3f\n", toAmp(Inverters[inv]->Iac1));
  480. + printf(" },\n");
  481. + printf(" \"phase2\": {\n");
  482. + printf(" \"p\": %ld,\n", Inverters[inv]->Pac2);
  483. + printf(" \"u\": %6.2f,\n", toVolt(Inverters[inv]->Uac2));
  484. + printf(" \"i\": %6.3f\n", toAmp(Inverters[inv]->Iac2));
  485. + printf(" },\n");
  486. + printf(" \"phase3\": {\n");
  487. + printf(" \"p\": %ld,\n", Inverters[inv]->Pac3);
  488. + printf(" \"u\": %6.2f,\n", toVolt(Inverters[inv]->Uac3));
  489. + printf(" \"i\": %6.3f\n", toAmp(Inverters[inv]->Iac3));
  490. + printf(" },\n");
  491. + printf(" \"pTotal\": %ld\n", Inverters[inv]->TotalPac);
  492. + printf(" }\n");
  493. + }
  494. }
  495.  
  496. - if ((rc = getInverterData(Inverters, SpotGridFrequency)) != 0)
  497. + if (cfg.json == 1)
  498. + {
  499. + printf(" ]");
  500. + prevJSObj = 1;
  501. + }
  502. +
  503. + if ((rc = getInverterData(Inverters, SpotGridFrequency)) != 0 && (cfg.json != 1))
  504. printf("getSpotGridFrequency returned an error: %d\n", rc);
  505. else
  506. {
  507. + if (cfg.json == 1)
  508. + {
  509. + if (prevJSObj)
  510. + printf(",\n \"gridFrequency\": [\n");
  511. + else
  512. + printf(" \"gridFrequency\": [\n");
  513. + }
  514. +
  515. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  516. {
  517. if (VERBOSE_NORMAL)
  518. @@ -472,11 +768,35 @@
  519. printf("SUSyID: %d - SN: %lu\n", Inverters[inv]->SUSyID, Inverters[inv]->Serial);
  520. printf("Grid Freq. : %.2fHz\n", toHz(Inverters[inv]->GridFreq));
  521. }
  522. + else if (cfg.json == 1)
  523. + {
  524. + if (inv > 0)
  525. + printf(",\n {\n");
  526. + else
  527. + printf(" {\n");
  528. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  529. + printf(" \"frequency\": %.2f\n", toHz(Inverters[inv]->GridFreq));
  530. + printf(" }\n");
  531. + }
  532. + }
  533. +
  534. + if (cfg.json == 1)
  535. + {
  536. + printf(" ]");
  537. + prevJSObj = 1;
  538. }
  539. }
  540.  
  541. if (Inverters[0]->DevClass == SolarInverter)
  542. {
  543. + if (cfg.json == 1)
  544. + {
  545. + if (prevJSObj)
  546. + printf(",\n \"timeInfo\": [\n");
  547. + else
  548. + printf(" \"timeInfo\": [\n");
  549. + }
  550. +
  551. for (int inv=0; Inverters[inv]!=NULL && inv<MAX_INVERTERS; inv++)
  552. {
  553. if (VERBOSE_NORMAL)
  554. @@ -491,7 +811,25 @@
  555. if (Inverters[inv]->SleepTime > 0)
  556. printf("Inverter Sleep Time : %s\n", strftime_t(cfg.DateTimeFormat, Inverters[inv]->SleepTime));
  557. }
  558. + else if (cfg.json == 1)
  559. + {
  560. + if (inv > 0)
  561. + printf(",\n {\n");
  562. + else
  563. + printf(" {\n");
  564. + printf(" \"id\": %d,\n", Inverters[inv]->SUSyID);
  565. + printf(" \"currentInverterTime\": \"%s\",\n", strftime_t(cfg.DateTimeFormat, Inverters[inv]->InverterDatetime));
  566. + printf(" \"inverterWakeUpTime\": \"%s\",\n", strftime_t(cfg.DateTimeFormat, Inverters[inv]->WakeupTime));
  567. + printf(" \"inverterSleepTime\": \"%s\"\n", strftime_t(cfg.DateTimeFormat, Inverters[inv]->SleepTime));
  568. + printf(" }\n");
  569. + }
  570. }
  571. +
  572. + if (cfg.json == 1)
  573. + {
  574. + printf(" ]");
  575. + prevJSObj = 1;
  576. + }
  577. }
  578.  
  579. if (Inverters[0]->DevClass == SolarInverter)
  580. @@ -521,7 +859,15 @@
  581. logoffSMAInverter(Inverters[0]);
  582. freemem(Inverters);
  583. bthClose();
  584. - printf("Terminating here... Dealing with Battery Inverter.\n");
  585. + if (cfg.json == 1)
  586. + {
  587. + //json end tag
  588. + printf("\n}\n");
  589. + }
  590. + else
  591. + {
  592. + printf("Terminating here... Dealing with Battery Inverter.\n");
  593. + }
  594. exit(0);
  595. }
  596. }
  597. @@ -551,7 +897,7 @@
  598.  
  599. for (int count=0; count<cfg.archDays; count++)
  600. {
  601. - if ((rc = ArchiveDayData(Inverters, arch_time)) != E_OK)
  602. + if ((rc = ArchiveDayData(Inverters, arch_time)) != E_OK && cfg.json != 1)
  603. {
  604. if (rc != E_ARCHNODATA) printf("ArchiveDayData returned an error: %d\n", rc);
  605. }
  606. @@ -702,6 +1048,12 @@
  607.  
  608. if (VERBOSE_NORMAL) print_error(stdout, PROC_INFO, "Done.\n");
  609.  
  610. + if (cfg.json == 1)
  611. + {
  612. + //json end tag
  613. + printf("\n}\n");
  614. + }
  615. +
  616. return 0;
  617. }
  618.  
  619. @@ -1751,6 +2103,7 @@
  620. cfg->nocsv = 0;
  621. cfg->nospot = 0;
  622. cfg->nosql = 0;
  623. + cfg->json = 0;
  624. // 123Solar Web Solar logger support(http://www.123solar.org/)
  625. // This is an undocumented feature and should only be used for 123solar
  626. cfg->s123 = S123_NOP;
  627. @@ -1768,6 +2121,17 @@
  628. }
  629. }
  630.  
  631. + //Set json mode
  632. + for (int i = 1; i < argc; i++)
  633. + {
  634. + if (stricmp(argv[i], "-json") == 0)
  635. + {
  636. + cfg->quiet = 1;
  637. + cfg->json = 1;
  638. + break;
  639. + }
  640. + }
  641. +
  642. // Get path of executable
  643. cfg->AppPath = argv[0];
  644. size_t pos = cfg->AppPath.find_last_of("/\\");
  645. @@ -2024,6 +2388,8 @@
  646. cfg->verbose = 2;
  647.  
  648. cfg->forceInq = 1;
  649. + // Disable json when syncing time
  650. + cfg->json = 0;
  651. }
  652.  
  653. //Disable verbose/debug modes when silent
  654. @@ -2033,6 +2399,14 @@
  655. cfg->debug = 0;
  656. }
  657.  
  658. + //Disable all other output for -json (quite already set to 1 above)
  659. + if (cfg->json == 1)
  660. + {
  661. + cfg->nocsv = 1;
  662. + cfg->nosql = 1;
  663. + cfg->nospot = 1;
  664. + }
  665. +
  666. return 0;
  667. }
  668.  
  669. @@ -2075,7 +2449,8 @@
  670. std::cout << " -password:xxxx Installer password\n";
  671. std::cout << " -loadlive Use predefined settings for manual upload to pvoutput.org\n";
  672. std::cout << " -startdate:YYYYMMDD Set start date for historic data retrieval\n";
  673. - std::cout << " -settime Sync inverter time with host time\n" << std::endl;
  674. + std::cout << " -settime Sync inverter time with host time\n";
  675. + std::cout << " -json Print data in json format implies -q, -nocsv, -nosql, -sp0 and -v0\n" << std::endl;
  676. }
  677. }
  678.  
  679. diff -u SBFspot/SBFspot.h ../SBFspot/SBFspot.h
  680. --- SBFspot/SBFspot.h 2016-05-20 22:22:28.000000000 +0200
  681. +++ ../SBFspot/SBFspot.h 2016-11-01 12:13:28.228591256 +0100
  682. @@ -258,6 +258,7 @@
  683. int nospot; // -sp0 Disables Spot CSV export
  684. int nosql; // -nosql Disables SQL export
  685. int loadlive; // -loadlive Force settings to prepare for live loading to http://pvoutput.org/loadlive.jsp
  686. + int json; // -json json output to std out
  687. time_t startdate; // -startdate Start reading of historic data at the given date (YYYYMMDD)
  688. S123_COMMAND s123; // -123s 123Solar Web Solar logger support(http://www.123solar.org/)
  689. int settime; // -settime Set plant time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement