600c853c0ae0356a6f2a3be779e2f7d5fc2fd745.svn-base 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Web;
  9. using System.Web.Http;
  10. using System.Web.Http.Description;
  11. using DevExpress.Office.Utils;
  12. using iBemsDataService.Model;
  13. using iBemsDataService.Util;
  14. using iBemsDataService.Controllers.Bems.Common;
  15. using iBemsDataService.Controllers.Bems.ScheduledTask;
  16. using System.Diagnostics;
  17. using System.Transactions;
  18. using System.Collections.Specialized;
  19. namespace iBemsDataService.Controllers
  20. {
  21. public class BemsMonitoringPointHistoryController : ApiController
  22. {
  23. private iBemsEntities db;
  24. private PointHistoryValueManager pointHistoryValueManager;
  25. private Boolean IS_DEBUG = false;
  26. //static private List<MonitoringPointSType> m_listSType = new List<MonitoringPointSType>(); // 2016 09 19
  27. //private List<MonitoringPointSType> m_listSType;
  28. //static private List<CalculationValueSType[]> m_valuesListMonth = new List<CalculationValueSType[]>();
  29. //static private List<CalculationValueSType[]> m_valuesListDay = new List<CalculationValueSType[]>();
  30. //private String debugPath = "e:\\monitoring.txt";
  31. public BemsMonitoringPointHistoryController()
  32. {
  33. db = new iBemsEntities();
  34. pointHistoryValueManager = new PointHistoryValueManager( db );
  35. debug();
  36. }
  37. private void debug()
  38. {
  39. if (IS_DEBUG)
  40. {
  41. //var listener = new TextWriterTraceListener(debugPath);
  42. //Trace.Listeners.Add(listener);
  43. //Trace.AutoFlush = true;
  44. }
  45. }
  46. [ActionName( "Location" )]
  47. public CalculationValue[] GetBemsMonitoringPointHistory()
  48. {
  49. /* ControlBems bemsJob = new ControlBems();
  50. bemsJob.Execute(null); */
  51. Uri uri = Request.RequestUri;
  52. var uriQuery = HttpUtility.ParseQueryString( uri.Query );
  53. string uriTimeIntervalType = uriQuery.Get( "TimeIntervalType" );
  54. string uriStartDate = uriQuery.Get( "StartDate" );
  55. string uriEndDate = uriQuery.Get( "EndDate" );
  56. int siteId;
  57. int buildingId = -1; int fuelTypeId = -1;
  58. int floorId, zoneId, timeInterval = -1;
  59. DateTime startDate, endDate;
  60. if( int.TryParse( uriQuery.Get( "SiteId" ) , out siteId ) == false ||
  61. int.TryParse( uriQuery.Get( "BuildingId" ) , out buildingId ) == false ||
  62. int.TryParse( uriTimeIntervalType , out timeInterval ) == false ||
  63. DateTime.TryParse( uriStartDate , out startDate ) == false ||
  64. DateTime.TryParse( uriEndDate , out endDate ) == false ||
  65. int.TryParse( uriQuery.Get( "FuelTypeId" ) , out fuelTypeId ) == false )
  66. {
  67. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  68. siteId, buildingId, uriTimeIntervalType, uriStartDate, uriEndDate, fuelTypeId));
  69. }
  70. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  71. int? nullableFloorId = null;
  72. int? nullableZoneId = null;
  73. if( int.TryParse( uriQuery.Get( "FloorId" ) , out floorId ) )
  74. {
  75. nullableFloorId = floorId;
  76. if( int.TryParse( uriQuery.Get( "ZoneId" ) , out zoneId ) )
  77. {
  78. nullableZoneId = zoneId;
  79. }
  80. }
  81. List<MonitoringPoint> list = new List<MonitoringPoint>();
  82. if (buildingId == 0) // 사이트 전체면 사이트 전체가 있는 지 먼저 확인 하고 아닌 경우 건물들 합을 구한다.
  83. {
  84. IQueryable<MonitoringPoint> querySite;
  85. querySite = from p in db.BemsMonitoringPoint where p.SiteId == siteId && p.BuildingId == 0 && p.FuelTypeId == fuelTypeId
  86. select new MonitoringPoint
  87. {
  88. SiteId = p.SiteId,
  89. FacilityCode = p.FacilityCode ,
  90. PropertyId = p.PropertyId ,
  91. };
  92. if (querySite != null && querySite.Count() > 0)
  93. {
  94. list = querySite.ToList();
  95. }
  96. else
  97. {
  98. IQueryable<CmBuilding> query;
  99. query = from x in db.CmBuilding where x.SiteId == siteId select x;
  100. foreach (CmBuilding b in query)
  101. {
  102. AddPointToListInLocation(list, siteId, fuelTypeId, b.BuildingId, null, null);
  103. }
  104. }
  105. }
  106. else
  107. {
  108. AddPointToListInLocation(list, siteId, fuelTypeId, buildingId, nullableFloorId, nullableZoneId);
  109. }
  110. try
  111. {
  112. //AddPointToListInLocation( list , siteId, fuelTypeId , buildingId , nullableFloorId , nullableZoneId );
  113. var values = CreateCalculationValues( startDate , endDate , timeIntervalType );
  114. foreach( var point in list )
  115. {
  116. var pointValues = pointHistoryValueManager.GetPointValues(
  117. siteId , point.FacilityCode , point.PropertyId ,
  118. timeIntervalType , startDate , endDate );
  119. if( pointValues == null )
  120. continue;
  121. int index = 0;
  122. CalculationValue p = null;
  123. CalculationValue v = null;
  124. for( var i = 0 ; i < values.Length ; i++ )
  125. {
  126. v = values[i];
  127. if( p == null )
  128. {
  129. if( pointValues.Length <= index ) break;
  130. p = pointValues[index];
  131. }
  132. if( p.DateTime == v.DateTime )
  133. {
  134. v.Value += p.Value;
  135. p = null;
  136. index++;
  137. if( pointValues.Length <= index ) break;
  138. }
  139. }
  140. }
  141. return values;
  142. }
  143. catch( Exception )
  144. {
  145. throw;
  146. }
  147. }
  148. //[ActionName("ServiceType")] //
  149. /*public CalculationValueSType[] GetBemsMonitoringPointHistoryService()
  150. {
  151. Uri uri = Request.RequestUri;
  152. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  153. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  154. string uriStartDate = uriQuery.Get("StartDate");
  155. string uriEndDate = uriQuery.Get("EndDate");
  156. int siteId;
  157. int buildingId, floorId, zoneId, fuelTypeId, timeInterval = -1;
  158. DateTime startDate, endDate;
  159. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  160. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  161. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  162. DateTime.TryParse(uriStartDate, out startDate) == false ||
  163. DateTime.TryParse(uriEndDate, out endDate) == false ||
  164. int.TryParse(uriQuery.Get("FuelTypeId"), out fuelTypeId) == false)
  165. {
  166. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2}",
  167. uriTimeIntervalType, uriStartDate, uriEndDate));
  168. }
  169. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  170. int? nullableFloorId = null;
  171. int? nullableZoneId = null;
  172. if (int.TryParse(uriQuery.Get("FloorId"), out floorId))
  173. {
  174. nullableFloorId = floorId;
  175. if (int.TryParse(uriQuery.Get("ZoneId"), out zoneId))
  176. {
  177. nullableZoneId = zoneId;
  178. }
  179. }
  180. List<MonitoringPointSType> list = new List<MonitoringPointSType>();
  181. if (buildingId == 0)
  182. {
  183. IQueryable<CmBuilding> query;
  184. query = from x in db.CmBuilding where x.SiteId == siteId select x;
  185. foreach (CmBuilding b in query)
  186. {
  187. //AddPointToListInLocationSType(list, siteId, fuelTypeId, b.BuildingId, null, null);
  188. AddPointToListInLocationSType_New(list, siteId, fuelTypeId, b.BuildingId);
  189. }
  190. }
  191. else
  192. {
  193. //AddPointToListInLocationSType(list, siteId, fuelTypeId, buildingId, nullableFloorId, nullableZoneId);
  194. AddPointToListInLocationSType_New(list, siteId, fuelTypeId, buildingId);
  195. }
  196. try
  197. {
  198. var values = CreateCalculationValuesSType(startDate, endDate, timeIntervalType);
  199. foreach (var point in list)
  200. {
  201. var pointValues = pointHistoryValueManager.GetPointValues(siteId, point.FacilityCode, point.PropertyId, timeIntervalType, startDate, endDate);
  202. if (pointValues == null)
  203. continue;
  204. int index = 0;
  205. CalculationValue p = null;
  206. CalculationValueSType v = null;
  207. for (var i = point.ServiceTypeId - 1; i < values.Length; i +=12)
  208. {
  209. v = values[i];
  210. if (p == null)
  211. {
  212. if (pointValues.Length <= index)
  213. break;
  214. p = pointValues[index];
  215. }
  216. //if (p.DateTime == v.DateTime && point.ServiceTypeId == v.ServiceType)
  217. if (p.DateTime == v.DateTime)
  218. {
  219. //if
  220. v.Value += p.Value;
  221. p = null;
  222. index++;
  223. if (pointValues.Length <= index)
  224. break;
  225. }
  226. }
  227. }
  228. return values;
  229. }
  230. catch (Exception)
  231. {
  232. throw;
  233. }
  234. }
  235. */
  236. //DB update
  237. //private void MakeMonthCalcData(int siteId, TimeInterval timeIntervalType, DateTime startDate, DateTime endDate, List<CalculationValueSType[]> valuesListReturn)
  238. private void SaveCalcData(int siteId, TimeInterval timeIntervalType, DateTime startDate, DateTime endDate, CalculationValueSType[] valuesList)
  239. {
  240. using (var transaction = new TransactionScope())
  241. {
  242. try
  243. {
  244. string sqlDel = (timeIntervalType == TimeInterval.Month) ?
  245. String.Format("delete from CmServiceEnergyCalcMonth where SiteId = {0} and calDate >= '{1}' and calDate < '{2}'", siteId, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd")) :
  246. String.Format("delete from CmServiceEnergyCalcDay where SiteId = {0} and calDate >= '{1}' and calDate < '{2}'", siteId, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"));
  247. db.Database.ExecuteSqlCommand(sqlDel);
  248. //db.Database.ExecuteSqlCommand("delete from CmServiceEnergyCalcMonth where SiteId = @siteId and calDate >= @start and calDate <= @end",
  249. //new SqlParameter ("@siteId", siteId), startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"));
  250. if (timeIntervalType == TimeInterval.Month) // Month처리
  251. {
  252. foreach (var k in valuesList)
  253. {
  254. var newData = new CmServiceEnergyCalcMonth
  255. {
  256. SiteId = siteId,
  257. calDate = k.DateTime,
  258. ServiceTypeId = (short)k.ServiceType,
  259. Value = k.Value
  260. };
  261. db.CmServiceEnergyCalcMonth.Add(newData);
  262. }
  263. db.SaveChanges();
  264. }
  265. else // day 처리
  266. {
  267. foreach (var k in valuesList)
  268. {
  269. var newData = new CmServiceEnergyCalcDay
  270. {
  271. SiteId = siteId,
  272. calDate = k.DateTime,
  273. ServiceTypeId = (short)k.ServiceType,
  274. Value = k.Value
  275. };
  276. db.CmServiceEnergyCalcDay.Add(newData);
  277. }
  278. db.SaveChanges();
  279. }
  280. }
  281. catch (Exception) { throw; }
  282. transaction.Complete();
  283. }
  284. }
  285. //private List<CalculationValueSType[]> LoadCalcData(int siteId, TimeInterval timeIntervalType, DateTime startDate, DateTime endDate)
  286. private CalculationValueSType[] LoadCalcData(int siteId, TimeInterval timeIntervalType, DateTime startDate, DateTime endDate)
  287. {
  288. //List<CalculationValueSType[]> valuesListDB = new List<CalculationValueSType[]>();
  289. CalculationValueSType[] list;
  290. if (timeIntervalType == TimeInterval.Month)
  291. {
  292. list =
  293. (from x in db.CmServiceEnergyCalcMonth
  294. where x.SiteId == siteId && x.calDate >= startDate && x.calDate <= endDate
  295. select new CalculationValueSType
  296. {
  297. DateTime = x.calDate,
  298. ServiceType = x.ServiceTypeId,
  299. Value = (double)x.Value
  300. }).ToArray<CalculationValueSType>();
  301. }
  302. else
  303. {
  304. list =
  305. (from x in db.CmServiceEnergyCalcDay
  306. where x.SiteId == siteId && x.calDate >= startDate && x.calDate <= endDate
  307. select new CalculationValueSType
  308. {
  309. DateTime = x.calDate,
  310. ServiceType = x.ServiceTypeId,
  311. Value = (double)x.Value
  312. }).ToArray<CalculationValueSType>();
  313. }
  314. //valuesListDB.Add(list);
  315. //return valuesListDB;
  316. return list;
  317. }
  318. //private CalculationValueSType[] MakeCalcData(int siteId, int buildingId, TimeInterval timeIntervalType, DateTime startDate, DateTime endDate, List<CalculationValueSType[]> valuesListReturn)
  319. private CalculationValueSType[] MakeCalcData(List<MonitoringPointSType> listSType, int siteId, int buildingId, TimeInterval timeIntervalType, DateTime startDate, DateTime endDate) // 2017 02 22 listSType 로컬로 변경
  320. {
  321. //if (m_listSType.Count() <= 0)
  322. {
  323. if (buildingId == 0)
  324. {
  325. IQueryable<CmBuilding> query;
  326. query = from x in db.CmBuilding where x.SiteId == siteId select x;
  327. foreach (CmBuilding b in query)
  328. {
  329. AddPointToListInLocationSType_New2(listSType, siteId, b.BuildingId);
  330. }
  331. }
  332. else
  333. {
  334. AddPointToListInLocationSType_New2(listSType, siteId, buildingId);
  335. }
  336. }
  337. List<CalculationValueSType[]> valuesListReturn = new List<CalculationValueSType[]>();
  338. if (timeIntervalType == TimeInterval.Month)
  339. valuesListReturn.Add(CreateCalculationValuesSType(startDate, startDate, timeIntervalType)); // startDate 2개가 맞다. 한달씩, 하기 떄문이다
  340. else
  341. valuesListReturn.Add(CreateCalculationValuesSType(startDate, endDate, timeIntervalType));
  342. List<CalculationValueSType[]> valuesList = new List<CalculationValueSType[]>();
  343. IQueryable<BemsFactorToe> queryTOE;
  344. queryTOE = from x in db.BemsFactorToe where x.FuelTypeId < 3 select x;
  345. foreach (BemsFactorToe TOE in queryTOE)
  346. {
  347. if (timeIntervalType == TimeInterval.Month)
  348. valuesList.Add(CreateCalculationValuesSType(startDate, startDate, timeIntervalType)); // startDate 2개가 맞다. 한달씩, 또는 하루씩 하기 떄문이다
  349. else
  350. valuesList.Add(CreateCalculationValuesSType(startDate, endDate, timeIntervalType));
  351. }
  352. try
  353. {
  354. foreach (var point in listSType)
  355. {
  356. if (point.FuelTypeId <= 0) continue; // 2017 02 17 방어코드 추가
  357. var pointValues = pointHistoryValueManager.GetPointValues(siteId, point.FacilityCode, point.PropertyId, timeIntervalType, startDate, endDate);
  358. if (pointValues == null)
  359. continue;
  360. var values = valuesList[point.FuelTypeId - 1];
  361. int index = 0;
  362. CalculationValue p = null;
  363. CalculationValueSType v = null;
  364. for (var i = point.ServiceTypeId - 1; i < values.Length; i += 12)
  365. {
  366. v = values[i];
  367. if (p == null)
  368. {
  369. if (pointValues.Length <= index)
  370. break;
  371. p = pointValues[index];
  372. }
  373. if (p.DateTime == v.DateTime)
  374. {
  375. v.Value += p.Value;
  376. p = null;
  377. index++;
  378. if (pointValues.Length <= index)
  379. break;
  380. }
  381. }
  382. }
  383. foreach (BemsFactorToe TOE in queryTOE)
  384. {
  385. int r = 0;
  386. foreach (var k in valuesList[TOE.FuelTypeId - 1])
  387. {
  388. k.Value = k.Value * (double)TOE.kcal;
  389. valuesListReturn[0][r].Value += k.Value; // 연료별이 kcal로 환산되어 합쳐졌기에 리스트 하나만 가면 된다, DB저장을 위해서도 하나가 편리하다.
  390. r++;
  391. }
  392. }
  393. //DB update
  394. DateTime currentTime = DateTime.Now;
  395. if (timeIntervalType == TimeInterval.Month) // 이번달이나
  396. {
  397. currentTime = new DateTime(currentTime.Year, currentTime.Month, 1, 0, 0, 0);
  398. if (startDate < currentTime)
  399. SaveCalcData(siteId, timeIntervalType, startDate, endDate, valuesListReturn[0]);
  400. }
  401. else // 오늘은 DB에 쓰지 않는다. 오늘은 불려지지 않는다. -> 아예 현재달은 쓰지 않는다.
  402. {
  403. if (currentTime.Year == startDate.Year && currentTime.Month == startDate.Month)
  404. { }
  405. else
  406. SaveCalcData(siteId, timeIntervalType, startDate, endDate, valuesListReturn[0]);
  407. }
  408. return valuesListReturn[0]; // 연료별이 kcal로 환산되어 합쳐졌기에 리스트 하나만 가면 된다, DB저장을 위해서도 하나가 편리하다.
  409. }
  410. catch (Exception)
  411. {
  412. throw;
  413. }
  414. }
  415. private CalculationValueSType[] MakeCalcDataToday(List<MonitoringPointSType> listSType, int siteId, int buildingId)
  416. {
  417. //if (m_listSType.Count() <= 0)
  418. {
  419. if (buildingId == 0)
  420. {
  421. IQueryable<CmBuilding> query;
  422. query = from x in db.CmBuilding where x.SiteId == siteId select x;
  423. foreach (CmBuilding b in query)
  424. {
  425. AddPointToListInLocationSType_New2(listSType, siteId, b.BuildingId);
  426. }
  427. }
  428. else
  429. {
  430. AddPointToListInLocationSType_New2(listSType, siteId, buildingId);
  431. }
  432. }
  433. //DB update
  434. DateTime currentTime = DateTime.Now;
  435. DateTime startDate = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0);
  436. DateTime endDate = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59);
  437. List<CalculationValueSType[]> valuesListReturn = new List<CalculationValueSType[]>();
  438. valuesListReturn.Add(CreateCalculationValuesSType(startDate, endDate, TimeInterval.Day));
  439. List<CalculationValueSType[]> valuesList = new List<CalculationValueSType[]>();
  440. IQueryable<BemsFactorToe> queryTOE;
  441. queryTOE = from x in db.BemsFactorToe where x.FuelTypeId < 3 select x;
  442. foreach (BemsFactorToe TOE in queryTOE)
  443. {
  444. valuesList.Add(CreateCalculationValuesSType(startDate, endDate, TimeInterval.Day));
  445. }
  446. try
  447. {
  448. foreach (var point in listSType)
  449. {
  450. if (point.FuelTypeId <= 0) continue; // 2017 02 17 방어코드 추가
  451. var pointValues = pointHistoryValueManager.GetPointValues(siteId, point.FacilityCode, point.PropertyId, TimeInterval.Day, startDate, endDate);
  452. if (pointValues == null)
  453. continue;
  454. var values = valuesList[point.FuelTypeId - 1];
  455. int index = 0;
  456. CalculationValue p = null;
  457. CalculationValueSType v = null;
  458. for (var i = point.ServiceTypeId - 1; i < values.Length; i += 12)
  459. {
  460. v = values[i];
  461. if (p == null)
  462. {
  463. if (pointValues.Length <= index)
  464. break;
  465. p = pointValues[index];
  466. }
  467. if (p.DateTime == v.DateTime)
  468. {
  469. v.Value += p.Value;
  470. p = null;
  471. index++;
  472. if (pointValues.Length <= index)
  473. break;
  474. }
  475. }
  476. }
  477. foreach (BemsFactorToe TOE in queryTOE)
  478. {
  479. int r = 0;
  480. foreach (var k in valuesList[TOE.FuelTypeId - 1])
  481. {
  482. k.Value = k.Value * (double)TOE.kcal;
  483. valuesListReturn[0][r].Value += k.Value; // 연료별이 kcal로 환산되어 합쳐졌기에 리스트 하나만 가면 된다, DB저장을 위해서도 하나가 편리하다.
  484. r++;
  485. }
  486. }
  487. return valuesListReturn[0]; // 연료별이 kcal로 환산되어 합쳐졌기에 리스트 하나만 가면 된다, DB저장을 위해서도 하나가 편리하다.
  488. }
  489. catch (Exception)
  490. {
  491. throw;
  492. }
  493. }
  494. [ActionName("ServiceType")] //
  495. //public CalculationValueSType[] GetBemsMonitoringPointHistoryService()
  496. public List<CalculationValueSType[]> GetBemsMonitoringPointHistoryService()
  497. {
  498. Uri uri = Request.RequestUri;
  499. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  500. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  501. string uriStartDate = uriQuery.Get("StartDate");
  502. string uriEndDate = uriQuery.Get("EndDate");
  503. int siteId;
  504. int buildingId, floorId, zoneId, fuelTypeId, timeInterval = -1;
  505. DateTime startDate, endDate;
  506. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  507. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  508. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  509. DateTime.TryParse(uriStartDate, out startDate) == false ||
  510. DateTime.TryParse(uriEndDate, out endDate) == false ||
  511. int.TryParse(uriQuery.Get("FuelTypeId"), out fuelTypeId) == false)
  512. {
  513. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2}",
  514. uriTimeIntervalType, uriStartDate, uriEndDate));
  515. }
  516. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  517. int? nullableFloorId = null;
  518. int? nullableZoneId = null;
  519. if (int.TryParse(uriQuery.Get("FloorId"), out floorId))
  520. {
  521. nullableFloorId = floorId;
  522. if (int.TryParse(uriQuery.Get("ZoneId"), out zoneId))
  523. {
  524. nullableZoneId = zoneId;
  525. }
  526. }
  527. List<MonitoringPointSType> listSType = new List<MonitoringPointSType>(); // 2017 02 22 로컬로 변경
  528. List<CalculationValueSType[]> valuesListReturn = new List<CalculationValueSType[]>();
  529. CalculationValueSType[] cv = CreateCalculationValuesSType(startDate, endDate, timeIntervalType);
  530. // 있는지 판단해서 있으면 보낸다
  531. // 이번달 또는 오늘은 제외
  532. //
  533. DateTime dtStart = new DateTime(startDate.Year, startDate.Month, startDate.Day, 0, 0, 0);
  534. DateTime dtEnd = new DateTime(startDate.Year, startDate.Month, startDate.Day, 23, 59, 59);
  535. if (timeIntervalType == TimeInterval.Month)
  536. {
  537. CalculationValueSType[] list;
  538. while (dtEnd < endDate)
  539. {
  540. // 먼저 이미 DB에 있는지 보고 있으면 바로 받고
  541. list = LoadCalcData(siteId, timeIntervalType, dtStart, dtEnd);
  542. if (list.Length <= 0) list = MakeCalcData(listSType, siteId, buildingId, timeIntervalType, dtStart, dtStart.AddMonths(1)); // 없으면 만들고 받는다
  543. foreach (CalculationValueSType s in list)
  544. {
  545. foreach (CalculationValueSType v in cv)
  546. {
  547. if (v.ServiceType == s.ServiceType && v.DateTime == s.DateTime)
  548. {
  549. v.Value = s.Value;
  550. break;
  551. }
  552. }
  553. }
  554. dtStart = dtStart.AddMonths(1);
  555. dtEnd = dtEnd.AddMonths(1);
  556. }
  557. valuesListReturn.Add(cv);
  558. return valuesListReturn;
  559. }
  560. else
  561. {
  562. DateTime current= DateTime.Now;
  563. CalculationValueSType[] list;
  564. // 현재달이면 오늘은 제외한다
  565. //dtEnd = (endDate.Year == current.Year && endDate.Month == current.Month) ? current.AddDays(-1) : endDate; 현재달의 day는 아예 안쓰기로 하면서 오늘처리는 필요 없어진다
  566. dtEnd = endDate;
  567. list = LoadCalcData(siteId, timeIntervalType, dtStart, dtEnd);
  568. if (list.Length <= 0) list = MakeCalcData(listSType, siteId, buildingId, timeIntervalType, dtStart, dtEnd); // 없으면 만들고 받는다. 이떄 현재달이며 오늘은 만들지 않는다.
  569. foreach (CalculationValueSType s in list)
  570. {
  571. foreach (CalculationValueSType v in cv)
  572. {
  573. if (v.ServiceType == s.ServiceType && v.DateTime == s.DateTime)
  574. {
  575. v.Value = s.Value;
  576. break;
  577. }
  578. }
  579. }
  580. /*
  581. if (endDate.Year == current.Year && endDate.Month == current.Month)
  582. {
  583. list = MakeCalcDataToday(siteId, buildingId);
  584. foreach (CalculationValueSType s in list)
  585. {
  586. foreach (CalculationValueSType v in cv)
  587. {
  588. if (v.ServiceType == s.ServiceType && v.DateTime == s.DateTime)
  589. {
  590. v.Value = s.Value;
  591. break;
  592. }
  593. }
  594. }
  595. } */
  596. //if (dtStart >= current)
  597. //break;
  598. //}
  599. valuesListReturn.Add(cv);
  600. return valuesListReturn;
  601. }
  602. //return valuesListReturn;
  603. }
  604. [ActionName("Location_ZoneTempHumi")]
  605. //public CalculationValue[] GetBemsMonitoringPointHistoryZoneTempHumi()
  606. public List<CalculationValue[]> GetBemsMonitoringPointHistoryZoneTempHumi()
  607. {
  608. Uri uri = Request.RequestUri;
  609. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  610. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  611. string uriStartDate = uriQuery.Get("StartDate");
  612. string uriEndDate = uriQuery.Get("EndDate");
  613. int siteId;
  614. int buildingId = -1; int fuelTypeId = -1;
  615. int floorId, zoneId, timeInterval = -1;
  616. DateTime startDate, endDate;
  617. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  618. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  619. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  620. DateTime.TryParse(uriStartDate, out startDate) == false ||
  621. DateTime.TryParse(uriEndDate, out endDate) == false ||
  622. int.TryParse(uriQuery.Get("FuelTypeId"), out fuelTypeId) == false)
  623. {
  624. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  625. siteId, buildingId, uriTimeIntervalType, uriStartDate, uriEndDate, fuelTypeId));
  626. }
  627. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  628. int? nullableFloorId = null;
  629. int? nullableZoneId = null;
  630. if (int.TryParse(uriQuery.Get("FloorId"), out floorId))
  631. {
  632. nullableFloorId = floorId;
  633. if (int.TryParse(uriQuery.Get("ZoneId"), out zoneId))
  634. {
  635. nullableZoneId = zoneId;
  636. }
  637. }
  638. List<MonitoringPoint> list = new List<MonitoringPoint>();
  639. AddPointToListInLocation_ZoneTempHumi(list, siteId, buildingId, nullableFloorId, nullableZoneId); // 2015 08 03 FType 91,92로 가려야 한다.
  640. // 온도를 습도들 CO2 들로 따로 처리하는 것으로 변경한다 2016 05 23
  641. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  642. try
  643. {
  644. /*var THSetQuery = from x in db.CmZoneTempHumiSet
  645. where x.SiteId == siteId && x.BuildingId == buildingId && x.FloorId == floorId && x.ZoneId == nullableZoneId
  646. select x;
  647. ControlBems cb = new ControlBems();
  648. cb.GetTempSetValue(1, */
  649. foreach (var point in list) // 온도,습도 2개가 존재해야 한다. + CO2 농도 (2016 04 18 추가됨 즉 3개
  650. {
  651. var pointValues = pointHistoryValueManager.GetPointValues(
  652. siteId, point.FacilityCode, point.PropertyId,
  653. timeIntervalType, startDate, endDate);
  654. if (pointValues == null)
  655. continue;
  656. int index = 0;
  657. CalculationValue p = null;
  658. CalculationValue v = null;
  659. var values = CreateCalculationValues(startDate, endDate, timeIntervalType); // 위치 안으로 이동 2015 08 04 hcLee
  660. for (var i = 0; i < values.Length; i++)
  661. {
  662. v = values[i];
  663. if (p == null)
  664. {
  665. if (pointValues.Length <= index) break;
  666. p = pointValues[index];
  667. }
  668. if (p.DateTime == v.DateTime)
  669. {
  670. //v.Value += p.Value;
  671. v.Value = p.Value;
  672. p = null;
  673. index++;
  674. if (pointValues.Length <= index) break;
  675. }
  676. }
  677. listCalc.Add(values);
  678. }
  679. //return values;
  680. return listCalc;
  681. }
  682. catch (Exception)
  683. {
  684. throw;
  685. }
  686. }
  687. [ActionName("FacilityRunTime")]
  688. //public CalculationValue[] GetBemsMonitoringPointHistoryZoneTempHumi()
  689. public List<CalculationValue[]> GetBemsMonitoringPointHistoryRunTime()
  690. {
  691. Uri uri = Request.RequestUri;
  692. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  693. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  694. string uriStartDate = uriQuery.Get("StartDate");
  695. string uriEndDate = uriQuery.Get("EndDate");
  696. int siteId;
  697. int facilityCode = -1, timeInterval = -1;
  698. DateTime startDate, endDate;
  699. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  700. int.TryParse(uriQuery.Get("FacilityCode"), out facilityCode) == false ||
  701. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  702. DateTime.TryParse(uriStartDate, out startDate) == false ||
  703. DateTime.TryParse(uriEndDate, out endDate) == false)
  704. {
  705. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4}",
  706. siteId, facilityCode, uriTimeIntervalType, uriStartDate, uriEndDate));
  707. }
  708. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  709. List<MonitoringPoint> list = new List<MonitoringPoint>();
  710. AddPointToList_RunTime(list, siteId, facilityCode); // 2015 08 03 FType 91,92로 가려야 한다.
  711. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  712. try
  713. {
  714. foreach (var point in list) // 가동시간 point 한개가 존재한다.
  715. {
  716. var pointValues = pointHistoryValueManager.GetPointValues(
  717. siteId, point.FacilityCode, point.PropertyId,
  718. timeIntervalType, startDate, endDate);
  719. if (pointValues == null)
  720. continue;
  721. int index = 0;
  722. CalculationValue p = null;
  723. CalculationValue v = null;
  724. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  725. for (var i = 0; i < values.Length; i++)
  726. {
  727. v = values[i];
  728. if (p == null)
  729. {
  730. if (pointValues.Length <= index) break;
  731. p = pointValues[index];
  732. }
  733. if (p.DateTime == v.DateTime)
  734. {
  735. //v.Value += p.Value;
  736. v.Value = p.Value;
  737. p = null;
  738. index++;
  739. if (pointValues.Length <= index) break;
  740. }
  741. }
  742. listCalc.Add(values);
  743. }
  744. //return values;
  745. return listCalc;
  746. }
  747. catch (Exception)
  748. {
  749. throw;
  750. }
  751. }
  752. // jhLee 2016-04-22
  753. [ActionName("FacilityCostRunTime")]
  754. public List<CalculationValue> GetBemsMonitoringPointHistoryCostRunTime()
  755. {
  756. Uri uri = Request.RequestUri;
  757. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  758. int siteId, facilityCode = -1, timeInterval = -1;
  759. DateTime startDate, endDate;
  760. SetFacilityRunTimeParam(uriQuery, ref facilityCode, ref timeInterval, out siteId, out startDate, out endDate);
  761. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  762. try
  763. {
  764. FacilityCostInfo facilityCostInfo = GetFacilityCostInfo(siteId, facilityCode);
  765. List<int> propertyIdList = GetPropertyIdList(siteId, facilityCode, (short)facilityCostInfo.FuelTypeId);
  766. var resultGroupByHour = GetHourRunTimeCostValues(siteId, facilityCode, propertyIdList, startDate, endDate, facilityCostInfo);
  767. var result = GetGroupedRuntimeCostValue(resultGroupByHour, timeIntervalType);
  768. return result == null ? new List<CalculationValue>() : result.ToList();
  769. }
  770. catch
  771. {
  772. return new List<CalculationValue>();
  773. }
  774. }
  775. private FacilityCostInfo GetFacilityCostInfo(int siteId, int facilityCode)
  776. {
  777. FacilityCostInfo facilityCostInfo
  778. = (from x in db.CmFacility
  779. where x.SiteId == siteId && x.FacilityCode == facilityCode
  780. select new FacilityCostInfo
  781. {
  782. SiteId = x.SiteId,
  783. FacilityCode = x.FacilityCode,
  784. FuelTypeId = x.FuelTypeId,
  785. ContractType = x.ContractType,
  786. RatedPowerConsumption = x.RatedPowerConsumption
  787. }).First();
  788. CheckFacilityCostInfo(facilityCostInfo);
  789. return facilityCostInfo;
  790. }
  791. private static void CheckFacilityCostInfo(FacilityCostInfo facilityCostInfo)
  792. {
  793. if (facilityCostInfo.FuelTypeId == null)
  794. {
  795. throw new Exception("FuelTypeId is null");
  796. }
  797. if (facilityCostInfo.ContractType == null)
  798. {
  799. throw new Exception("ContractType is null");
  800. }
  801. }
  802. private void SetFacilityRunTimeParam(NameValueCollection uriQuery, ref int facilityCode, ref int timeInterval, out int siteId, out DateTime startDate, out DateTime endDate)
  803. {
  804. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  805. string uriStartDate = uriQuery.Get("StartDate");
  806. string uriEndDate = uriQuery.Get("EndDate");
  807. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  808. int.TryParse(uriQuery.Get("FacilityCode"), out facilityCode) == false ||
  809. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  810. DateTime.TryParse(uriStartDate, out startDate) == false ||
  811. DateTime.TryParse(uriEndDate, out endDate) == false)
  812. {
  813. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4}",
  814. siteId, facilityCode, uriTimeIntervalType, uriStartDate, uriEndDate));
  815. }
  816. }
  817. private List<int> GetPropertyIdList(int siteId, int facilityCode, Nullable<short> fuelTypeId)
  818. {
  819. return (from x in db.BemsMonitoringPoint
  820. where x.SiteId == siteId &&
  821. x.FacilityCode == facilityCode &&
  822. x.FuelTypeId == fuelTypeId // 원래방식인 가동시간을 이용할려면 fuelTypeId는 사용하지말고 ServiceTypeId = 120 만 이용해야 한다.
  823. //hcLee 2016 07 11 두소장 요청 가동시간계산이 아닌 전력량 계산으로 변경
  824. && x.ServiceTypeId > 0 && x.ServiceTypeId < 100 && x.BuildingId > 0
  825. select x.PropertyId).ToList();
  826. }
  827. private IQueryable<CalculationValue> GetHourRunTimeCostValues(int siteId, int facilityCode, List<int> propertyIdList, DateTime startDate, DateTime endDate, FacilityCostInfo facilityCostInfo)
  828. {
  829. short fuelTypeId = (short)facilityCostInfo.FuelTypeId;
  830. short contractType = (short)facilityCostInfo.ContractType;
  831. string ratedPowerConsumption = facilityCostInfo.RatedPowerConsumption == null ? "" : facilityCostInfo.RatedPowerConsumption;
  832. // DateTime기준으로 묶은 시간별 모니터링 관제점 기록
  833. var query
  834. = (from x in db.BemsMonitoringPointHistoryHourly
  835. where
  836. x.SiteId == siteId &&
  837. x.FacilityCode == facilityCode &&
  838. propertyIdList.Contains(x.PropertyId) &&
  839. startDate <= x.CreatedDateTime && x.CreatedDateTime <= endDate
  840. select new CalculationValue
  841. {
  842. DateTime = x.CreatedDateTime,
  843. Value = x.CurrentValue
  844. })
  845. .GroupBy(x => x.DateTime).Select(y => new CalculationValue
  846. {
  847. DateTime = y.Key,
  848. //Value = y.Sum(item => item.Value) * ratedPowerConsumption / 60
  849. Value = y.Sum(item => item.Value) // 2016 07 11 hcLee
  850. }).OrderBy(x => x.DateTime);
  851. var bemsNoticePriceDetail
  852. = from x in db.BemsNoticePriceDetail
  853. where x.SiteId == siteId &&
  854. x.FuelTypeId == fuelTypeId &&
  855. x.ContractType == contractType
  856. select x;
  857. // 관제점 기록의 시간값을 기준으로 비용을 곱한값
  858. var resultGroupByHour = query
  859. .AsEnumerable()
  860. .Select(x => new CalculationValue
  861. {
  862. DateTime = x.DateTime,
  863. Value = x.Value * GetNoticePriceFromDateTime(bemsNoticePriceDetail, x.DateTime, fuelTypeId) // 2016 07 11 hcLee
  864. });
  865. return resultGroupByHour.AsQueryable();
  866. }
  867. // timeInterval 기준으로 Group by 한다
  868. private IQueryable<CalculationValue> GetGroupedRuntimeCostValue(IQueryable<CalculationValue> resultGroupByHour, TimeInterval timeInterval)
  869. {
  870. switch (timeInterval)
  871. {
  872. case TimeInterval.Year:
  873. return from x in resultGroupByHour
  874. group x by new { Year = x.DateTime.Year }
  875. into g
  876. select new CalculationValue
  877. {
  878. DateTime = new DateTime(g.Key.Year, 1, 1),
  879. Value = g.Sum(x => x.Value)
  880. };
  881. case TimeInterval.Month:
  882. return from x in resultGroupByHour
  883. group x by new { Year = x.DateTime.Year, Month = x.DateTime.Month }
  884. into g
  885. select new CalculationValue
  886. {
  887. DateTime = new DateTime(g.Key.Year, g.Key.Month, 1),
  888. Value = g.Sum(x => x.Value)
  889. };
  890. case TimeInterval.Day:
  891. return from x in resultGroupByHour
  892. group x by new { Year = x.DateTime.Year, Month = x.DateTime.Month, Day = x.DateTime.Day }
  893. into g
  894. select new CalculationValue
  895. {
  896. DateTime = new DateTime(g.Key.Year, g.Key.Month, g.Key.Day),
  897. Value = g.Sum(x => x.Value)
  898. };
  899. case TimeInterval.Hour:
  900. return resultGroupByHour;
  901. default:
  902. return null;
  903. }
  904. }
  905. // 해당 dateTime보다 작은 Max 값을 하나 가져와서 시간 / 월에 해당하는 가격을 리턴
  906. private double GetNoticePriceFromDateTime(IQueryable<BemsNoticePriceDetail> bemsNoticePriceDetail, DateTime datetime, short fuelTypeId) // hcLee 2016 07 11
  907. {
  908. int hour = 0;
  909. if (fuelTypeId == 1)
  910. {
  911. hour = datetime.Hour;
  912. }
  913. BemsNoticePriceDetail noticePrice
  914. = (from x in bemsNoticePriceDetail
  915. where x.DataId == hour
  916. && x.ApplyDate <= datetime
  917. orderby x.ApplyDate descending
  918. select x).First();
  919. if (noticePrice == null) return 0;
  920. String columnNameFromMonth = String.Format("P{0:D2}", datetime.Month);
  921. Nullable<Double> price = (Nullable<Double>)noticePrice.GetType().GetProperty(columnNameFromMonth).GetValue(noticePrice, null);
  922. return price == null ? 0 : (double)price;
  923. }
  924. // hcLee 2016 05 25
  925. [ActionName("FloorPointValue")]
  926. public List<double> GetBemsMonitoringPointHistoryFloorPointValue()
  927. //public List<CalculationValue[]> GetBemsMonitoringPointHistoryZoneTempHumi()
  928. {
  929. Uri uri = Request.RequestUri;
  930. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  931. //string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  932. //string uriStartDate = uriQuery.Get("StartDate");
  933. //string uriEndDate = uriQuery.Get("EndDate");
  934. int siteId;
  935. int buildingId = -1; int floorId = -1;
  936. //DateTime startDate, endDate;
  937. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  938. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  939. int.TryParse(uriQuery.Get("FloorId"), out floorId) == false )
  940. {
  941. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2}", siteId, buildingId, floorId));
  942. }
  943. List<MonitoringPoint> list = new List<MonitoringPoint>();
  944. AddPointToListInLocation_FloorTempHumi(list, siteId, buildingId, floorId);
  945. List<double> listValue = new List<double>();
  946. //List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  947. try
  948. {
  949. foreach (var point in list)
  950. {
  951. IEnumerable<BemsMonitoringPointHistory15min> lastData = (from data in db.BemsMonitoringPointHistory15min
  952. where data.SiteId == siteId && data.FacilityCode == point.FacilityCode && data.PropertyId == point.PropertyId
  953. orderby data.CreatedDateTime descending select data).Take(1);
  954. if (lastData == null || lastData.Count() == 0)
  955. {
  956. //instance.cd = string.Format("{0:D10}", 1);
  957. listValue.Add(-9999); // 데이터가 없는 경우 처리
  958. }
  959. else
  960. {
  961. BemsMonitoringPointHistory15min current = lastData.First();
  962. listValue.Add(current.CurrentValue);
  963. }
  964. }
  965. //return values;
  966. return listValue;
  967. }
  968. catch (Exception)
  969. {
  970. throw;
  971. }
  972. }
  973. // kgpark 2019 06 26
  974. [ActionName("FloorEachPointValue")]
  975. public List<double> GetBemsMonitoringPointHistoryFloorEachPointValue()
  976. //public List<CalculationValue[]> GetBemsMonitoringPointHistoryZoneTempHumi()
  977. {
  978. Uri uri = Request.RequestUri;
  979. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  980. //string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  981. //string uriStartDate = uriQuery.Get("StartDate");
  982. //string uriEndDate = uriQuery.Get("EndDate");
  983. int siteId; int floorId = -1;
  984. //DateTime startDate, endDate;
  985. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  986. int.TryParse(uriQuery.Get("FloorId"), out floorId) == false)
  987. {
  988. throw new Exception(string.Format("Not Found Parameters: {0},{1}", siteId, floorId));
  989. }
  990. List<MonitoringPoint> list = new List<MonitoringPoint>();
  991. AddEachPointToListInLocation_FloorTempHumi(list, siteId, floorId);
  992. List<double> listValue = new List<double>();
  993. //List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  994. try
  995. {
  996. foreach (var point in list)
  997. {
  998. IEnumerable<BemsMonitoringPointHistory15min> lastData = (from data in db.BemsMonitoringPointHistory15min
  999. where data.SiteId == siteId && data.FacilityCode == point.FacilityCode && data.PropertyId == point.PropertyId
  1000. orderby data.CreatedDateTime descending
  1001. select data).Take(1);
  1002. if (lastData == null || lastData.Count() == 0)
  1003. {
  1004. //instance.cd = string.Format("{0:D10}", 1);
  1005. listValue.Add(-9999); // 데이터가 없는 경우 처리
  1006. }
  1007. else
  1008. {
  1009. BemsMonitoringPointHistory15min current = lastData.First();
  1010. listValue.Add(current.CurrentValue);
  1011. }
  1012. }
  1013. //return values;
  1014. return listValue;
  1015. }
  1016. catch (Exception)
  1017. {
  1018. throw;
  1019. }
  1020. }
  1021. [ActionName("RuntimeREFRIGERATOR")]
  1022. public List<CalculationValue[]> GetBemsMonitoringPointHistoryRumtimeREFRIGERATOR()
  1023. {
  1024. Uri uri = Request.RequestUri;
  1025. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1026. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  1027. string uriStartDate = uriQuery.Get("StartDate");
  1028. string uriEndDate = uriQuery.Get("EndDate");
  1029. int siteId;
  1030. int buildingId = -1; int fuelTypeId = -1;
  1031. int floorId, zoneId, timeInterval = -1;
  1032. DateTime startDate, endDate;
  1033. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1034. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  1035. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  1036. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1037. DateTime.TryParse(uriEndDate, out endDate) == false ||
  1038. int.TryParse(uriQuery.Get("FuelTypeId"), out fuelTypeId) == false)
  1039. {
  1040. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  1041. siteId, buildingId, uriTimeIntervalType, uriStartDate, uriEndDate, fuelTypeId));
  1042. }
  1043. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1044. int? nullableFloorId = null;
  1045. int? nullableZoneId = null;
  1046. if (int.TryParse(uriQuery.Get("FloorId"), out floorId))
  1047. {
  1048. nullableFloorId = floorId;
  1049. if (int.TryParse(uriQuery.Get("ZoneId"), out zoneId))
  1050. {
  1051. nullableZoneId = zoneId;
  1052. }
  1053. }
  1054. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1055. AddPointToList_Runtime_REFRIGERATOR(list, siteId); // 냉동기 가동시간
  1056. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1057. try
  1058. {
  1059. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1060. foreach (var point in list)
  1061. {
  1062. var pointValues = pointHistoryValueManager.GetPointValues(
  1063. siteId, point.FacilityCode, point.PropertyId,
  1064. timeIntervalType, startDate, endDate);
  1065. if (pointValues == null)
  1066. continue;
  1067. int index = 0;
  1068. CalculationValue p = null;
  1069. CalculationValue v = null;
  1070. for (var i = 0; i < values.Length; i++)
  1071. {
  1072. v = values[i];
  1073. if (p == null)
  1074. {
  1075. if (pointValues.Length <= index) break;
  1076. p = pointValues[index];
  1077. }
  1078. if (p.DateTime == v.DateTime)
  1079. {
  1080. v.Value += p.Value;
  1081. p = null;
  1082. index++;
  1083. if (pointValues.Length <= index) break;
  1084. }
  1085. }
  1086. listCalc.Add(values);
  1087. }
  1088. return listCalc;
  1089. }
  1090. catch (Exception)
  1091. {
  1092. throw;
  1093. }
  1094. }
  1095. [ActionName("RuntimeBOILER")]
  1096. public List<CalculationValue[]> GetBemsMonitoringPointHistoryRumtimeBOILER()
  1097. {
  1098. Uri uri = Request.RequestUri;
  1099. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1100. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  1101. string uriStartDate = uriQuery.Get("StartDate");
  1102. string uriEndDate = uriQuery.Get("EndDate");
  1103. int siteId;
  1104. int buildingId = -1; int fuelTypeId = -1;
  1105. int floorId, zoneId, timeInterval = -1;
  1106. DateTime startDate, endDate;
  1107. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1108. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  1109. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  1110. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1111. DateTime.TryParse(uriEndDate, out endDate) == false ||
  1112. int.TryParse(uriQuery.Get("FuelTypeId"), out fuelTypeId) == false)
  1113. {
  1114. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  1115. siteId, buildingId, uriTimeIntervalType, uriStartDate, uriEndDate, fuelTypeId));
  1116. }
  1117. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1118. int? nullableFloorId = null;
  1119. int? nullableZoneId = null;
  1120. if (int.TryParse(uriQuery.Get("FloorId"), out floorId))
  1121. {
  1122. nullableFloorId = floorId;
  1123. if (int.TryParse(uriQuery.Get("ZoneId"), out zoneId))
  1124. {
  1125. nullableZoneId = zoneId;
  1126. }
  1127. }
  1128. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1129. AddPointToList_Runtime_BOILER(list, siteId); // 보일러 가동시간
  1130. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1131. try
  1132. {
  1133. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1134. foreach (var point in list)
  1135. {
  1136. var pointValues = pointHistoryValueManager.GetPointValues(
  1137. siteId, point.FacilityCode, point.PropertyId,
  1138. timeIntervalType, startDate, endDate);
  1139. if (pointValues == null)
  1140. continue;
  1141. int index = 0;
  1142. CalculationValue p = null;
  1143. CalculationValue v = null;
  1144. for (var i = 0; i < values.Length; i++)
  1145. {
  1146. v = values[i];
  1147. if (p == null)
  1148. {
  1149. if (pointValues.Length <= index) break;
  1150. p = pointValues[index];
  1151. }
  1152. if (p.DateTime == v.DateTime)
  1153. {
  1154. v.Value += p.Value;
  1155. p = null;
  1156. index++;
  1157. if (pointValues.Length <= index) break;
  1158. }
  1159. }
  1160. listCalc.Add(values);
  1161. }
  1162. return listCalc;
  1163. }
  1164. catch (Exception)
  1165. {
  1166. throw;
  1167. }
  1168. }
  1169. [ActionName("MonthMoney")]
  1170. //public CalculationValue[] GetBemsMonitoringPointHistoryZoneTempHumi()
  1171. public List<CalculationValue[]> GetBemsMonitoringPointHistoryMonthMoney()
  1172. {
  1173. Uri uri = Request.RequestUri;
  1174. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1175. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  1176. string uriStartDate = uriQuery.Get("StartDate");
  1177. string uriEndDate = uriQuery.Get("EndDate");
  1178. string uriType = uriQuery.Get("Type"); // 10 전기 11 수도 12 가스
  1179. int siteId;
  1180. int timeInterval = -1; int nType = -1;
  1181. DateTime startDate, endDate;
  1182. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1183. //int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  1184. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  1185. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1186. DateTime.TryParse(uriEndDate, out endDate) == false ||
  1187. int.TryParse(uriType, out nType) == false)
  1188. {
  1189. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4}",
  1190. siteId, uriTimeIntervalType, uriStartDate, uriEndDate, uriType));
  1191. }
  1192. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1193. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1194. try
  1195. {
  1196. var MM = from x in db.FmsBudgetDetailExecution
  1197. where x.SiteId == siteId && x.BudgetClassId == nType && (x.Year >= startDate.Year || x.Month >= startDate.Month) &&
  1198. (x.Year <= endDate.Year || x.Month <= endDate.Month)
  1199. select x;
  1200. var values = CreateCalculationValues(startDate, endDate, timeIntervalType); // 위치 안으로 이동 2015 08 04 hcLee
  1201. foreach (var point in MM)
  1202. {
  1203. CalculationValue v = null;
  1204. for (var i = 0; i < values.Length; i++)
  1205. {
  1206. v = values[i];
  1207. if (point.Year == v.DateTime.Year && point.Month == v.DateTime.Month)
  1208. {
  1209. //v.Value += p.Value;
  1210. v.Value = point.MonthlyExecution;
  1211. //p = null;
  1212. //index++;
  1213. listCalc.Add(values);
  1214. break;
  1215. //if (pointValues.Length <= index) break;
  1216. }
  1217. }
  1218. }
  1219. //return values;
  1220. }
  1221. catch (Exception)
  1222. {
  1223. throw;
  1224. }
  1225. return listCalc;
  1226. }
  1227. [ActionName("CurrentValueFrom15min")]
  1228. public List<CalculationValue[]> GetBemsMonitoringPointHistoryCurrentValue()
  1229. {
  1230. Uri uri = Request.RequestUri;
  1231. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1232. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1233. //string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  1234. //string uriStartDate = uriQuery.Get("StartDate");
  1235. //string uriEndDate = uriQuery.Get("EndDate");
  1236. int siteId;
  1237. int ValueType = -1;
  1238. //DateTime startDate, endDate;
  1239. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1240. int.TryParse(uriQuery.Get("ValueType"), out ValueType) == false)
  1241. {
  1242. throw new Exception(string.Format("Not Found Parameters: {0},{1}", siteId, ValueType));
  1243. //return listCalc;
  1244. }
  1245. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1246. AddPointToList_ControlPoint(list, siteId, ValueType); // valueType이 특정인 포인트들만 (설정제어용)
  1247. try
  1248. {
  1249. //var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1250. //int v = 0;
  1251. foreach (var point in list)
  1252. {
  1253. CalculationValue[] values = new CalculationValue[2];
  1254. IEnumerable<BemsMonitoringPointHistory15min> lastData = (from data in db.BemsMonitoringPointHistory15min
  1255. where data.SiteId == siteId && data.FacilityCode == point.FacilityCode && data.PropertyId == point.PropertyId
  1256. orderby data.CreatedDateTime descending select data).Take(1);
  1257. values[0] = new CalculationValue
  1258. {
  1259. DateTime = new DateTime(),
  1260. Value = -9999
  1261. };
  1262. if (lastData == null || lastData.Count() == 0)
  1263. {
  1264. //instance.cd = string.Format("{0:D10}", 1);
  1265. }
  1266. else
  1267. {
  1268. BemsMonitoringPointHistory15min current = lastData.First();
  1269. values[0].Value = current.CurrentValue;
  1270. }
  1271. IEnumerable<BemsControlPointHistory> lastData2 = (from data in db.BemsControlPointHistory
  1272. where data.SiteId == siteId && data.FacilityCode == point.FacilityCode && data.PropertyId == point.PropertyId
  1273. orderby data.CreateDateTime descending select data).Take(1);
  1274. values[1] = new CalculationValue
  1275. {
  1276. DateTime = new DateTime(),
  1277. Value = -9999
  1278. };
  1279. if (lastData2 == null || lastData2.Count() == 0)
  1280. {
  1281. //instance.cd = string.Format("{0:D10}", 1);
  1282. }
  1283. else
  1284. {
  1285. BemsControlPointHistory current = lastData2.First();
  1286. values[1].Value = current.ControlValue;
  1287. }
  1288. listCalc.Add(values);
  1289. }
  1290. return listCalc;
  1291. }
  1292. catch (Exception)
  1293. {
  1294. throw;
  1295. }
  1296. //return listCalc;
  1297. }
  1298. //**************** 2019.05.30. 가상설비, 일반설비 데이터 가져오기******************//
  1299. [ActionName("VirtualNormalFacilityData")]
  1300. public List<CalculationValue[]> GetBemsMonitoringPointHistoryVirtualNormalCurrentValue()
  1301. {
  1302. Uri uri = Request.RequestUri;
  1303. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1304. string uriSiteId = uriQuery.Get("SiteId");
  1305. string uriFacilityTypeId = uriQuery.Get("FacilityTypeId");
  1306. string uriFacilityCode = uriQuery.Get("FacilityCode");
  1307. string uriPropertyId = uriQuery.Get("PropertyId");
  1308. string uriTimeInterval = uriQuery.Get("TimeIntervalType");
  1309. string uriStartDate = uriQuery.Get("StartDate");
  1310. string uriEndDate = uriQuery.Get("EndDate");
  1311. int siteId = -1, facilityTypeId = -1, propertyId = -1, timeInterval = -1, facilityCode = -1;
  1312. DateTime startDate, endDate;
  1313. if (int.TryParse(uriSiteId, out siteId) == false ||
  1314. int.TryParse(uriFacilityTypeId, out facilityTypeId) == false ||
  1315. int.TryParse(uriPropertyId, out propertyId) == false ||
  1316. int.TryParse(uriTimeInterval, out timeInterval) == false ||
  1317. int.TryParse(uriFacilityCode, out facilityCode) == false ||
  1318. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1319. DateTime.TryParse(uriEndDate, out endDate) == false)
  1320. {
  1321. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  1322. siteId, facilityTypeId, propertyId, uriTimeInterval, uriStartDate, uriEndDate));
  1323. }
  1324. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1325. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1326. AddPointToList_ControlPoint2(list, siteId, propertyId, facilityTypeId, facilityCode); // 2019.05.30. 관제점 정보 확보
  1327. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1328. try
  1329. {
  1330. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1331. foreach (var point in list)
  1332. {
  1333. var pointValues = pointHistoryValueManager.GetPointValues(
  1334. siteId, point.FacilityCode, point.PropertyId,
  1335. timeIntervalType, startDate, endDate);
  1336. if (pointValues == null)
  1337. continue;
  1338. int index = 0;
  1339. CalculationValue p = null;
  1340. CalculationValue v = null;
  1341. for (var i = 0; i < values.Length; i++)
  1342. {
  1343. v = values[i];
  1344. if (p == null)
  1345. {
  1346. if (pointValues.Length <= index) break;
  1347. p = pointValues[index];
  1348. }
  1349. if (p.DateTime == v.DateTime)
  1350. {
  1351. v.Value += p.Value;
  1352. p = null;
  1353. index++;
  1354. if (pointValues.Length <= index) break;
  1355. }
  1356. }
  1357. listCalc.Add(values);
  1358. }
  1359. return listCalc;
  1360. }
  1361. catch (Exception)
  1362. {
  1363. throw;
  1364. }
  1365. }
  1366. //**************** 2019.05.30. 가상설비, 일반설비 데이터 가져오기****************** kgpark
  1367. //**************** 2020.05. 예측 데이터 가져오기****************** kgpark
  1368. [ActionName("ForecastingDayAheadData")]
  1369. public List<CalculationValue[]> GetBemsMonitoringPointHistoryForecastingDayAheadValue()
  1370. {
  1371. Uri uri = Request.RequestUri;
  1372. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1373. string uriSiteId = uriQuery.Get("SiteId");
  1374. string uriFacilityTypeId = uriQuery.Get("FacilityTypeId");
  1375. string uriFacilityCode = uriQuery.Get("FacilityCode");
  1376. string uriPropertyId = uriQuery.Get("PropertyId");
  1377. string uriTimeInterval = uriQuery.Get("TimeIntervalType");
  1378. string uriStartDate = uriQuery.Get("StartDate");
  1379. string uriEndDate = uriQuery.Get("EndDate");
  1380. int siteId = -1, facilityTypeId = -1, propertyId = -1, timeInterval = -1, facilityCode = -1;
  1381. DateTime startDate, endDate;
  1382. if (int.TryParse(uriSiteId, out siteId) == false ||
  1383. int.TryParse(uriFacilityTypeId, out facilityTypeId) == false ||
  1384. int.TryParse(uriPropertyId, out propertyId) == false ||
  1385. int.TryParse(uriTimeInterval, out timeInterval) == false ||
  1386. int.TryParse(uriFacilityCode, out facilityCode) == false ||
  1387. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1388. DateTime.TryParse(uriEndDate, out endDate) == false)
  1389. {
  1390. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  1391. siteId, facilityTypeId, propertyId, uriTimeInterval, uriStartDate, uriEndDate));
  1392. }
  1393. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1394. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1395. AddPointToList_ControlPoint2(list, siteId, propertyId, facilityTypeId, facilityCode); // 관제점 정보 확보
  1396. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1397. try
  1398. {
  1399. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1400. foreach (var point in list)
  1401. {
  1402. // 여기서 데이터 받아옴
  1403. var pointValues = pointHistoryValueManager.GetPointForecastingDayAheadValues(
  1404. siteId, point.FacilityCode, point.PropertyId,
  1405. timeIntervalType, startDate, endDate);
  1406. if (pointValues == null)
  1407. continue;
  1408. int index = 0;
  1409. CalculationValue p = null;
  1410. CalculationValue v = null;
  1411. for (var i = 0; i < values.Length; i++)
  1412. {
  1413. v = values[i];
  1414. if (p == null)
  1415. {
  1416. if (pointValues.Length <= index) break;
  1417. p = pointValues[index];
  1418. }
  1419. if (p.DateTime == v.DateTime)
  1420. {
  1421. v.Value += p.Value;
  1422. p = null;
  1423. index++;
  1424. if (pointValues.Length <= index) break;
  1425. }
  1426. }
  1427. listCalc.Add(values);
  1428. }
  1429. return listCalc;
  1430. }
  1431. catch (Exception)
  1432. {
  1433. throw;
  1434. }
  1435. //return listCalc;
  1436. }
  1437. [ActionName("ForecastingHourAheadData")]
  1438. public List<CalculationValue[]> GetBemsMonitoringPointHistoryForecastingHourAheadValue()
  1439. {
  1440. Uri uri = Request.RequestUri;
  1441. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1442. string uriSiteId = uriQuery.Get("SiteId");
  1443. string uriFacilityTypeId = uriQuery.Get("FacilityTypeId");
  1444. string uriFacilityCode = uriQuery.Get("FacilityCode");
  1445. string uriPropertyId = uriQuery.Get("PropertyId");
  1446. string uriTimeInterval = uriQuery.Get("TimeIntervalType");
  1447. string uriStartDate = uriQuery.Get("StartDate");
  1448. string uriEndDate = uriQuery.Get("EndDate");
  1449. int siteId = -1, facilityTypeId = -1, propertyId = -1, timeInterval = -1, facilityCode = -1;
  1450. DateTime startDate, endDate;
  1451. if (int.TryParse(uriSiteId, out siteId) == false ||
  1452. int.TryParse(uriFacilityTypeId, out facilityTypeId) == false ||
  1453. int.TryParse(uriPropertyId, out propertyId) == false ||
  1454. int.TryParse(uriTimeInterval, out timeInterval) == false ||
  1455. int.TryParse(uriFacilityCode, out facilityCode) == false ||
  1456. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1457. DateTime.TryParse(uriEndDate, out endDate) == false)
  1458. {
  1459. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}",
  1460. siteId, facilityTypeId, propertyId, uriTimeInterval, uriStartDate, uriEndDate));
  1461. }
  1462. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1463. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1464. AddPointToList_ControlPoint2(list, siteId, propertyId, facilityTypeId, facilityCode); // 관제점 정보 확보
  1465. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1466. try
  1467. {
  1468. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1469. foreach (var point in list)
  1470. {
  1471. // 여기서 데이터 받아옴
  1472. var pointValues = pointHistoryValueManager.GetPointForecastingHourAheadValues(
  1473. siteId, point.FacilityCode, point.PropertyId,
  1474. timeIntervalType, startDate, endDate);
  1475. if (pointValues == null)
  1476. continue;
  1477. int index = 0;
  1478. CalculationValue p = null;
  1479. CalculationValue v = null;
  1480. for (var i = 0; i < values.Length; i++)
  1481. {
  1482. v = values[i];
  1483. if (p == null)
  1484. {
  1485. if (pointValues.Length <= index) break;
  1486. p = pointValues[index];
  1487. }
  1488. if (p.DateTime == v.DateTime)
  1489. {
  1490. v.Value += p.Value;
  1491. p = null;
  1492. index++;
  1493. if (pointValues.Length <= index) break;
  1494. }
  1495. }
  1496. listCalc.Add(values);
  1497. }
  1498. return listCalc;
  1499. }
  1500. catch (Exception)
  1501. {
  1502. throw;
  1503. }
  1504. //return listCalc;
  1505. }
  1506. //**************** 2020.05. 예측 데이터 가져오기****************** kgpark
  1507. //**************** 2020.08. 축열조 시뮬레이터 관련 데이터 가져오기****************** kgpark
  1508. [ActionName("IceThermalStorageSimulation")]
  1509. public List<CalculationValue[]> GetIceThermalStorageSimulationValue()
  1510. {
  1511. Uri uri = Request.RequestUri;
  1512. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1513. string uriSiteId = uriQuery.Get("SiteId");
  1514. string uriFacilityTypeId = uriQuery.Get("FacilityTypeId");
  1515. string uriFacilityCode = uriQuery.Get("FacilityCode");
  1516. string uriPropertyId = uriQuery.Get("PropertyId");
  1517. string uriSimulationCase = uriQuery.Get("SimulationCase");
  1518. string uriTimeInterval = uriQuery.Get("TimeIntervalType");
  1519. string uriStartDate = uriQuery.Get("StartDate");
  1520. string uriEndDate = uriQuery.Get("EndDate");
  1521. int siteId = -1, facilityTypeId = -1, propertyId = -1, timeInterval = -1, facilityCode = -1, simulationCase = -1;
  1522. DateTime startDate, endDate;
  1523. if (int.TryParse(uriSiteId, out siteId) == false ||
  1524. int.TryParse(uriFacilityTypeId, out facilityTypeId) == false ||
  1525. int.TryParse(uriPropertyId, out propertyId) == false ||
  1526. int.TryParse(uriSimulationCase, out simulationCase) == false ||
  1527. int.TryParse(uriTimeInterval, out timeInterval) == false ||
  1528. int.TryParse(uriFacilityCode, out facilityCode) == false ||
  1529. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1530. DateTime.TryParse(uriEndDate, out endDate) == false)
  1531. {
  1532. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5},{6}",
  1533. siteId, facilityTypeId, propertyId, uriTimeInterval, uriStartDate, uriEndDate));
  1534. }
  1535. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1536. List<MonitoringPoint> list = new List<MonitoringPoint>();
  1537. AddPointToList_ControlPoint2(list, siteId, propertyId, facilityTypeId, facilityCode); // 관제점 정보 확보
  1538. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1539. try
  1540. {
  1541. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  1542. foreach (var point in list)
  1543. {
  1544. // 여기서 데이터 받아옴
  1545. var pointValues = pointHistoryValueManager.GetPointIceThermalStorageSimulationValues(
  1546. siteId, point.FacilityCode, point.PropertyId, simulationCase,
  1547. timeIntervalType, startDate, endDate);
  1548. if (pointValues == null)
  1549. continue;
  1550. int index = 0;
  1551. CalculationValue p = null;
  1552. CalculationValue v = null;
  1553. for (var i = 0; i < values.Length; i++)
  1554. {
  1555. v = values[i];
  1556. if (p == null)
  1557. {
  1558. if (pointValues.Length <= index) break;
  1559. p = pointValues[index];
  1560. }
  1561. if (p.DateTime == v.DateTime)
  1562. {
  1563. v.Value += p.Value;
  1564. p = null;
  1565. index++;
  1566. if (pointValues.Length <= index) break;
  1567. }
  1568. }
  1569. listCalc.Add(values);
  1570. }
  1571. return listCalc;
  1572. }
  1573. catch (Exception)
  1574. {
  1575. throw;
  1576. }
  1577. //return listCalc;
  1578. }
  1579. //**************** 2020.08. 축열조 시뮬레이터 관련 데이터 가져오기****************** kgpark
  1580. [ResponseType(typeof(void))]
  1581. [ActionName("ControlPoint")]
  1582. public IHttpActionResult PostControlPoint()
  1583. {
  1584. Uri uri = Request.RequestUri;
  1585. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1586. string uriSiteId = uriQuery.Get("SiteId");
  1587. string uriFacilityTypeId = uriQuery.Get("FacilityTypeId");
  1588. string uriFacilityCode = uriQuery.Get("FacilityCode");
  1589. string uriPropertyId = uriQuery.Get("PropertyId");
  1590. string uriControlValue = uriQuery.Get("ControlValue");
  1591. int siteId = -1, facilityTypeId = -1, propertyId = -1, facilityCode = -1;
  1592. double controlValue = 0;
  1593. if (int.TryParse(uriSiteId, out siteId) == false ||
  1594. int.TryParse(uriFacilityTypeId, out facilityTypeId) == false ||
  1595. int.TryParse(uriPropertyId, out propertyId) == false ||
  1596. int.TryParse(uriFacilityCode, out facilityCode) == false ||
  1597. double.TryParse(uriControlValue, out controlValue) == false)
  1598. {
  1599. throw new Exception(string.Format("Not Found FFFF Parameters: {0},{1},{2},{3},{4} ", uriSiteId, uriFacilityTypeId, facilityCode, uriPropertyId, uriControlValue));
  1600. }
  1601. DateTime currentTime = DateTime.Now;
  1602. using (var transaction = new TransactionScope())
  1603. {
  1604. try
  1605. {
  1606. {
  1607. var control = new BemsControlPointHistory
  1608. {
  1609. SiteId = siteId,
  1610. FacilityTypeId = facilityTypeId, // 2015 08 05 DB 문제로 일단 99로 테스트함, 정상코드는 아래줄이다.
  1611. FacilityCode = facilityCode,
  1612. PropertyId = propertyId, // ???? hcLee
  1613. CreateDateTime = currentTime,
  1614. ControlValue = controlValue,
  1615. WriteServiceName = null,
  1616. ActionDateTime = null
  1617. };
  1618. db.BemsControlPointHistory.Add(control);
  1619. db.SaveChanges();
  1620. }
  1621. }
  1622. catch (Exception e)
  1623. {
  1624. Trace.WriteLine(e.Message);
  1625. //throw e;
  1626. throw;
  1627. }
  1628. transaction.Complete();
  1629. }
  1630. return StatusCode(HttpStatusCode.NoContent);
  1631. }
  1632. [ActionName("LCCViewPreSum")]
  1633. public List<double> GetBemsMonitoringPointHistoryLCCViewPreSum()
  1634. {
  1635. Uri uri = Request.RequestUri;
  1636. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1637. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  1638. string uriStartDate = uriQuery.Get("StartDate");
  1639. string uriEndDate = uriQuery.Get("EndDate");
  1640. //string uriType = uriQuery.Get("Type"); // 10 전기 11 수도 12 가스
  1641. int siteId;
  1642. int timeInterval = -1;
  1643. DateTime startDate, endDate;
  1644. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1645. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  1646. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1647. DateTime.TryParse(uriEndDate, out endDate) == false)
  1648. {
  1649. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3}", siteId, uriTimeIntervalType, uriStartDate, uriEndDate));
  1650. }
  1651. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1652. List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1653. List<FmsBudgetCodeClass> listCode = new List<FmsBudgetCodeClass>();
  1654. var dd = from x in db.FmsBudgetCodeClass where x.Depth == 0 select x;
  1655. listCode = dd.ToList();
  1656. List<double> listTotalSum = new List<double>();
  1657. foreach (FmsBudgetCodeClass code in listCode)
  1658. {
  1659. var MM = from x in db.FmsBudgetDetailExecution
  1660. where x.SiteId == siteId && x.Year >= 1000 && x.Month >= 0 && // 최초부터
  1661. x.Year < startDate.Year || (x.Year == startDate.Year && x.Month < startDate.Month)
  1662. from c in db.FmsBudgetCodeClass
  1663. where c.BudgetSeq.Substring(0, 2) == code.BudgetSeq && x.BudgetClassId == c.BudgetClassId
  1664. select x;
  1665. double sum = new double();
  1666. foreach (var point in MM)
  1667. {
  1668. sum += point.MonthlyExecution;
  1669. }
  1670. listTotalSum.Add(sum);
  1671. }
  1672. return listTotalSum;
  1673. }
  1674. [ActionName("LCCView")]
  1675. public List<CalculationValueBudget[]> GetBemsMonitoringPointHistoryLCCView()
  1676. {
  1677. Uri uri = Request.RequestUri;
  1678. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1679. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  1680. string uriStartDate = uriQuery.Get("StartDate");
  1681. string uriEndDate = uriQuery.Get("EndDate");
  1682. //string uriType = uriQuery.Get("Type"); // 10 전기 11 수도 12 가스
  1683. int siteId;
  1684. int timeInterval = -1;
  1685. DateTime startDate, endDate;
  1686. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1687. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  1688. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1689. DateTime.TryParse(uriEndDate, out endDate) == false)
  1690. {
  1691. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3}", siteId, uriTimeIntervalType, uriStartDate, uriEndDate));
  1692. }
  1693. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  1694. List<CalculationValueBudget[]> listCalc = new List<CalculationValueBudget[]>();
  1695. List<FmsBudgetCodeClass> listCode = new List<FmsBudgetCodeClass>();
  1696. var dd = from x in db.FmsBudgetCodeClass where x.Depth == 0 select x;
  1697. listCode = dd.ToList();
  1698. foreach (FmsBudgetCodeClass code in listCode)
  1699. {
  1700. var MM = from x in db.FmsBudgetDetailExecution
  1701. where x.SiteId == siteId && x.Year >= startDate.Year && x.Month >= startDate.Month &&
  1702. x.Year < endDate.Year || (x.Year == endDate.Year && x.Month <= endDate.Month)
  1703. from c in db.FmsBudgetCodeClass
  1704. where c.BudgetSeq.Substring(0, 2) == code.BudgetSeq && x.BudgetClassId == c.BudgetClassId
  1705. select x;
  1706. var values = CreateCalculationValuesBudget(startDate, endDate, timeIntervalType); // 위치 안으로 이동 2015 08 04 hcLee
  1707. foreach (var point in MM)
  1708. {
  1709. CalculationValueBudget v = null;
  1710. for (var i = 0; i < values.Length; i++)
  1711. {
  1712. v = values[i];
  1713. if (point.Year == v.DateTime.Year && point.Month == v.DateTime.Month)
  1714. {
  1715. v.Value += point.MonthlyExecution;
  1716. v.Value2 += point.MonthlyBudget;
  1717. break;
  1718. }
  1719. }
  1720. }
  1721. listCalc.Add(values);
  1722. }
  1723. return listCalc;
  1724. }
  1725. [ActionName("FmsHouseStock")]
  1726. public List<FmsHouseStockEx> GetBemsMonitoringFmsHouseStock()
  1727. {
  1728. Uri uri = Request.RequestUri;
  1729. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1730. string uriBusinessId = uriQuery.Get("BusinessId");
  1731. int siteId;
  1732. int BusinessId = -1;
  1733. //DateTime startDate, endDate;
  1734. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1735. int.TryParse(uriBusinessId, out BusinessId) == false)
  1736. {
  1737. throw new Exception(string.Format("Not Found Parameters: {0},{1}", siteId, uriBusinessId));
  1738. }
  1739. List<FmsMaterialWarehouse> listHouse = new List<FmsMaterialWarehouse>();
  1740. var hh = from h in db.FmsMaterialWarehouse where h.SiteId == siteId select h;
  1741. listHouse = hh.ToList();
  1742. List<FmsHouseStockEx> listEx = new List<FmsHouseStockEx>();
  1743. foreach (FmsMaterialWarehouse h in listHouse)
  1744. {
  1745. var query = from data in db.FmsMaterialStored
  1746. //var query = from data in db.FmsMaterialEx
  1747. //where (data.SiteId == siteId && data.WarehouseId == h.WarehouseId && M.MaterialId == data.MaterialId)
  1748. where (data.SiteId == siteId && data.WarehouseId == h.WarehouseId)
  1749. from M in db.FmsMaterial
  1750. where M.SiteId == siteId && M.MaterialId == data.MaterialId
  1751. group data by new { data.MaterialId, M.MaterialCode, M.Name } into g
  1752. select new
  1753. {
  1754. HouseName = h.Name,
  1755. MaterialId = g.Key.MaterialId,
  1756. StockCnt = g.Sum(data => data.RemainStoredCount),
  1757. MName = g.Key.Name,
  1758. MaterialCode = g.Key.MaterialCode
  1759. };
  1760. foreach (var v in query)
  1761. {
  1762. FmsHouseStockEx ex = new FmsHouseStockEx();
  1763. ex.MaterialId = v.MaterialId;
  1764. ex.MaterialCode= v.MaterialCode;
  1765. ex.MName = v.MName;
  1766. ex.HouseName = v.HouseName;
  1767. ex.StockCnt = v.StockCnt;
  1768. listEx.Add(ex);
  1769. }
  1770. }
  1771. return listEx;
  1772. /*
  1773. List<double> listTotalSum = new List<double>();
  1774. foreach (FmsBudgetCodeClass code in listCode)
  1775. {
  1776. var MM = from x in db.FmsBudgetDetailExecution
  1777. where x.SiteId == siteId && x.Year >= 1000 && x.Month >= 0 && // 최초부터
  1778. x.Year < startDate.Year || (x.Year == startDate.Year && x.Month < startDate.Month)
  1779. from c in db.FmsBudgetCodeClass
  1780. where c.BudgetSeq.Substring(0, 2) == code.BudgetSeq && x.BudgetClassId == c.BudgetClassId
  1781. select x;
  1782. double sum = new double();
  1783. foreach (var point in MM)
  1784. {
  1785. sum += point.MonthlyExecution;
  1786. }
  1787. listTotalSum.Add(sum);
  1788. }
  1789. return listTotalSum;*/
  1790. }
  1791. [ActionName("GetCenterSiteData")]
  1792. public List<CenterSiteData> PostGetCenterSiteData(GetCenterSiteData_Param param)
  1793. {
  1794. int timeIntervalType = param.TimeIntervalType;
  1795. DateTime startDate = param.startDate, endDate = param.endDate;
  1796. DateTime currentTime = DateTime.Now;
  1797. DateTime todayStart = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0);
  1798. List<CenterSiteData> list = new List<CenterSiteData>();
  1799. IEnumerable<CmSite> sites = from data in db.CmSite where data.SiteId > 0 select data;
  1800. BemsFormulaController FC = new BemsFormulaController();
  1801. //1.100.1.1 3
  1802. foreach (CmSite s in sites)
  1803. {
  1804. CenterSiteData cd = new CenterSiteData();
  1805. cd.siteid = s.SiteId;
  1806. cd.name = s.Name;
  1807. //List<CalculationValue> listC = new List<CalculationValue>();
  1808. //listC.Add(new CalculationValue());
  1809. cd.e_value_list = FC.GetBemsFormulaReal(s.SiteId, 100, 1, 1, todayStart, currentTime, 2); // hour
  1810. cd.g_value_list = FC.GetBemsFormulaReal(s.SiteId, 101, 2, 1, todayStart, currentTime, 2); // hour
  1811. List<CalculationResult> listEYear = FC.GetBemsFormulaReal(s.SiteId, 100, 1, 1, startDate, endDate, timeIntervalType);
  1812. cd.elec_current = (listEYear.Count() <= 0) ? 0 : listEYear[0].Value;
  1813. List<CalculationResult> listGYear = FC.GetBemsFormulaReal(s.SiteId, 101, 2, 1, startDate, endDate, timeIntervalType);
  1814. cd.gas_current = (listGYear.Count() <= 0) ? 0 : listGYear[0].Value;
  1815. //cd.kcal_target = 300;
  1816. var G = from x in db.BemsEnergyDaily
  1817. where (x.SiteId == s.SiteId && x.FuelTypeId == 1 && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1818. group x by new { x.SiteId } into g
  1819. select new { P = g.Sum(x => x.Goal) };
  1820. //listPrediction.Add(G.Count() > 0 ? (double)G.FirstOrDefault().P : 0);
  1821. cd.elec_target = G.Count() > 0 ? (double)G.FirstOrDefault().P : 0;
  1822. G = from x in db.BemsEnergyDaily
  1823. where (x.SiteId == s.SiteId && x.FuelTypeId == 2 && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1824. group x by new { x.SiteId } into g
  1825. select new { P = g.Sum(x => x.Goal) };
  1826. cd.gas_target = G.Count() > 0 ? (double)G.FirstOrDefault().P : 0;
  1827. list.Add(cd);
  1828. }
  1829. return list;
  1830. }
  1831. // 차후를 생각해 BuildingId와 FuelType, 날짜를 받지만 현재 의미는 없다 hcLee 2016 03 18
  1832. [ActionName("GetPrediction")]
  1833. public List<double> PostGetPrediction(GetPrediction_Param param)
  1834. {
  1835. /* Uri uri = Request.RequestUri;
  1836. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  1837. //string uriBuildingId = uriQuery.Get("BuildingId");
  1838. string uriFuelType = uriQuery.Get("FuelType");
  1839. string uriStartDate = uriQuery.Get("StartDate");
  1840. string uriEndDate = uriQuery.Get("EndDate");
  1841. //string uriType = uriQuery.Get("Type"); // 10 전기 11 수도 12 가스
  1842. int siteId;
  1843. int fueltype = -1; int buildingId = 0;
  1844. DateTime startDate, endDate;
  1845. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  1846. int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  1847. int.TryParse(uriFuelType, out fueltype) == false ||
  1848. DateTime.TryParse(uriStartDate, out startDate) == false ||
  1849. DateTime.TryParse(uriEndDate, out endDate) == false)
  1850. {
  1851. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2},{3},{4},{5}", siteId, buildingId, uriFuelType, uriStartDate, uriEndDate));
  1852. }*/
  1853. //int timeIntervalType = (TimeInterval)timeInterval;
  1854. //List<CalculationValue[]> listCalc = new List<CalculationValue[]>();
  1855. List<double> listPrediction = new List<double>();
  1856. DateTime startDate, endDate;
  1857. int siteId = param.SiteId;
  1858. DateTime currentTime = DateTime.Now;
  1859. startDate = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 0, 0, 0);
  1860. endDate = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59);
  1861. for (int i = 0; i < 3; i++)
  1862. {
  1863. // 오늘의 전기 가스 수도 TOE
  1864. var G = from x in db.BemsEnergyDaily
  1865. where (x.SiteId == siteId && x.FuelTypeId == 1 && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1866. group x by new { x.SiteId } into g
  1867. select new { P = g.Sum(x => x.Goal) };
  1868. listPrediction.Add(G.Count() > 0 ? (double)G.FirstOrDefault().P : 0);
  1869. G = from x in db.BemsEnergyDaily
  1870. where (x.SiteId == siteId && x.FuelTypeId == 2 && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1871. group x by new { x.SiteId } into g
  1872. select new { P = g.Sum(x => x.Goal) };
  1873. listPrediction.Add(G.Count() > 0 ? (double)G.FirstOrDefault().P : 0);
  1874. G = from x in db.BemsEnergyDaily
  1875. where (x.SiteId == siteId && x.FuelTypeId == 3 && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1876. group x by new { x.SiteId } into g
  1877. select new { P = g.Sum(x => x.Goal) };
  1878. listPrediction.Add(G.Count() > 0 ? (double)G.FirstOrDefault().P : 0);
  1879. /*
  1880. G = from x in db.BemsEnergyGoalDaily
  1881. where (x.SiteId == siteId && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1882. group x by new { x.SiteId } into g select new { P = g.Sum(x => x.Goal) };
  1883. listPrediction.Add(G.Count() > 0 ? (double)G.FirstOrDefault().P : 0); */
  1884. //Why(listPrediction, siteId, startDate, endDate); 2016 07 15 테이블 삭제
  1885. if (i == 0) // 두번쨰 루프시작전 이번달
  1886. {
  1887. startDate = new DateTime(currentTime.Year, currentTime.Month, 1, 0, 0, 0);
  1888. endDate = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59);
  1889. }
  1890. else if (i == 1) // 세번쨰 루프시작전 금년
  1891. {
  1892. startDate = new DateTime(currentTime.Year, 1, 1, 0, 0, 0);
  1893. endDate = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 23, 59, 59);
  1894. }
  1895. }
  1896. return listPrediction; // 4 x 3 12개의 값 -> 9
  1897. }
  1898. /* {}
  1899. private void Why(List<double> list, int siteId, DateTime startDate, DateTime endDate)
  1900. {
  1901. //var G = from x in db.BemsEnergyGoalDaily 2016 07 15
  1902. var G = from x in db.BemsEnergyDaily
  1903. where (x.SiteId == siteId && x.CreatedDate >= startDate && x.CreatedDate <= endDate)
  1904. group x by new { x.SiteId } into g select new { P = g.Sum(x => x.Goal) };
  1905. list.Add(G.Count() > 0 ? (double)G.FirstOrDefault().P : 0);
  1906. } */
  1907. private void NextTimeFromTimeInterval( ref DateTime datetime, TimeInterval timeIntervalType )
  1908. {
  1909. switch( timeIntervalType )
  1910. {
  1911. case TimeInterval.QuarterMin:
  1912. datetime = datetime.AddMinutes( 15 );
  1913. break;
  1914. case TimeInterval.Hour:
  1915. datetime = datetime.AddHours( 1 );
  1916. break;
  1917. case TimeInterval.Day:
  1918. datetime = datetime.AddDays( 1 );
  1919. break;
  1920. case TimeInterval.Month:
  1921. datetime = datetime.AddMonths( 1 );
  1922. break;
  1923. default: // TimeInterval.Year:
  1924. datetime = datetime.AddYears( 1 );
  1925. break;
  1926. }
  1927. }
  1928. private int GetCountInTimeRange( DateTime startDate , DateTime endDate , TimeInterval timeIntervalType )
  1929. {
  1930. TimeSpan ts = endDate - startDate;
  1931. switch( timeIntervalType )
  1932. {
  1933. case TimeInterval.QuarterMin:
  1934. return (int)ts.TotalMinutes / 15 + 1;
  1935. case TimeInterval.Hour:
  1936. return (int)ts.TotalHours + 1;
  1937. case TimeInterval.Day:
  1938. return (int)ts.TotalDays + 1;
  1939. case TimeInterval.Month:
  1940. {
  1941. int count = 0;
  1942. DateTime date = startDate;
  1943. //while (date.Year <= endDate.Year && date.Month <= endDate.Month) // hcLee 헐~
  1944. while (date <= endDate) // hcLee
  1945. {
  1946. count++;
  1947. date = date.AddMonths( 1 );
  1948. }
  1949. return count;
  1950. }
  1951. default: // TimeInterval.Year:
  1952. {
  1953. int count = 0;
  1954. DateTime date = startDate;
  1955. while( date.Year <= endDate.Year)
  1956. {
  1957. count++;
  1958. date = date.AddYears( 1 );
  1959. }
  1960. return count;
  1961. }
  1962. }
  1963. }
  1964. private CalculationValue[] CreateCalculationValues( DateTime startDate , DateTime endDate , TimeInterval timeIntervalType )
  1965. {
  1966. DateTime datetime = startDate;
  1967. int count = GetCountInTimeRange( startDate , endDate , timeIntervalType );
  1968. CalculationValue[] values = new CalculationValue[count];
  1969. for( var i = 0 ; i < values.Length ; i++ )
  1970. {
  1971. values[i] = new CalculationValue
  1972. {
  1973. DateTime = datetime ,
  1974. Value = 0
  1975. };
  1976. NextTimeFromTimeInterval( ref datetime , timeIntervalType );
  1977. }
  1978. return values;
  1979. }
  1980. private CalculationValueBudget[] CreateCalculationValuesBudget(DateTime startDate, DateTime endDate, TimeInterval timeIntervalType)
  1981. {
  1982. DateTime datetime = startDate;
  1983. int count = GetCountInTimeRange(startDate, endDate, timeIntervalType);
  1984. CalculationValueBudget[] values = new CalculationValueBudget[count];
  1985. for (var i = 0; i < values.Length; i++)
  1986. {
  1987. values[i] = new CalculationValueBudget
  1988. {
  1989. DateTime = datetime,
  1990. Value = 0,
  1991. Value2 = 0
  1992. };
  1993. NextTimeFromTimeInterval(ref datetime, timeIntervalType);
  1994. }
  1995. return values;
  1996. }
  1997. private CalculationValueSType[] CreateCalculationValuesSType(DateTime startDate, DateTime endDate, TimeInterval timeIntervalType)
  1998. {
  1999. DateTime datetime = startDate;
  2000. int count = GetCountInTimeRange(startDate, endDate, timeIntervalType);
  2001. var query = from x in db.BemsServiceType where x.ServiceTypeId < 100 && x.ServiceTypeId > 0 select x;
  2002. //foreach (CmBuilding b in query)
  2003. CalculationValueSType[] values = new CalculationValueSType[count * query.Count()];
  2004. //CalculationValueSType[] values = new CalculationValueSType[count][query.Count()];
  2005. int j = 0;
  2006. for (var i = 0; i < count; i++)
  2007. {
  2008. foreach (BemsServiceType S in query)
  2009. {
  2010. values[j] = new CalculationValueSType
  2011. {
  2012. DateTime = datetime,
  2013. Value = 0,
  2014. ServiceType = S.ServiceTypeId
  2015. };
  2016. j++;
  2017. }
  2018. NextTimeFromTimeInterval(ref datetime, timeIntervalType);
  2019. //j += query.Count();
  2020. }
  2021. return values;
  2022. }
  2023. private void AddPointToListInLocation( List<MonitoringPoint> list ,
  2024. int siteId , int fuelTypeId , int buildingId , int? floorId , int? zoneId )
  2025. {
  2026. {
  2027. bool bEnd = false;
  2028. IQueryable<MonitoringPoint> query;
  2029. // floorId 나 zoneId 가 null를 가지고 있어도, 비교시에는 null로 비교하지 않는다.
  2030. // 그래서 강제적으로 조건에 따라 null를 비교하도록 수정
  2031. if( floorId == null && zoneId == null ) // 건물처리
  2032. {
  2033. query = from x in db.BemsMonitoringPoint
  2034. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId &&
  2035. x.BuildingId == buildingId && x.FloorId == null && x.ZoneId == null
  2036. select new MonitoringPoint
  2037. {
  2038. SiteId = x.SiteId,
  2039. FacilityCode = x.FacilityCode,
  2040. PropertyId = x.PropertyId,
  2041. };
  2042. //throw new Exception(string.Format("case1"));
  2043. }
  2044. else if (floorId != null && zoneId == null) // 층처리 층이고 zone이 null인 즉 층전체포인트가 있으면
  2045. {
  2046. query = from x in db.BemsMonitoringPoint
  2047. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId &&
  2048. x.BuildingId == buildingId && x.FloorId == floorId && x.ZoneId == null
  2049. select new MonitoringPoint
  2050. {
  2051. SiteId = x.SiteId,
  2052. FacilityCode = x.FacilityCode,
  2053. PropertyId = x.PropertyId,
  2054. };
  2055. }
  2056. else // 구역처리
  2057. {
  2058. query = from x in db.BemsMonitoringPoint
  2059. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId &&
  2060. x.BuildingId == buildingId && x.FloorId == floorId && x.ZoneId == zoneId
  2061. select new MonitoringPoint
  2062. {
  2063. SiteId = x.SiteId ,
  2064. FacilityCode = x.FacilityCode ,
  2065. PropertyId = x.PropertyId ,
  2066. };
  2067. bEnd = true;
  2068. //throw new Exception(string.Format("case4"));
  2069. }
  2070. //int n = query.Count();
  2071. if( query.Any() )
  2072. {
  2073. foreach( var data in query )
  2074. {
  2075. list.Add( data );
  2076. }
  2077. return; // 있으면 끝
  2078. }
  2079. if (bEnd) return;
  2080. }
  2081. {
  2082. if( floorId == null ) //
  2083. {
  2084. var query = from x in db.CmFloor
  2085. where x.SiteId == siteId && x.BuildingId == buildingId
  2086. select x.FloorId;
  2087. if (query.Any())
  2088. {
  2089. foreach (var floor in query)
  2090. {
  2091. AddPointToListInLocation(list, siteId, fuelTypeId, buildingId, floor, null);
  2092. }
  2093. }
  2094. }
  2095. //else if( zoneId == null )
  2096. //if( zoneId == null )
  2097. if (zoneId == null)
  2098. //else
  2099. {
  2100. var query = from x in db.CmZone
  2101. where x.SiteId == siteId && x.BuildingId == buildingId && x.FloorId == floorId
  2102. select x.ZoneId;
  2103. if (query.Any())
  2104. {
  2105. foreach (var zone in query)
  2106. {
  2107. AddPointToListInLocation(list, siteId, fuelTypeId, buildingId, floorId, zone);
  2108. }
  2109. }
  2110. }
  2111. }
  2112. }
  2113. private void AddPointToListInLocationSType_New(List<MonitoringPointSType> list, int siteId, int buildingId, int fuelTypeId)
  2114. {
  2115. var q = from x in db.BemsServiceType
  2116. where x.ServiceTypeId > 0 && x.ServiceTypeId < 100
  2117. select x;
  2118. IQueryable<MonitoringPointSType> query;
  2119. foreach (var stype in q)
  2120. {
  2121. //AddPointToListInLocation(list, siteId, fuelTypeId, buildingId, floorId, zone);
  2122. query = from x in db.BemsMonitoringPoint
  2123. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId == stype.ServiceTypeId &&
  2124. x.BuildingId == buildingId && x.FloorId == null
  2125. select new MonitoringPointSType
  2126. {
  2127. SiteId = x.SiteId,
  2128. FacilityCode = x.FacilityCode,
  2129. PropertyId = x.PropertyId,
  2130. ServiceTypeId = (int)x.ServiceTypeId,
  2131. };
  2132. //int n = query.Count();
  2133. if (query.Any())
  2134. {
  2135. foreach (var data in query)
  2136. {
  2137. list.Add(data);
  2138. }
  2139. }
  2140. else // 빌딩만 매핑이 없으면 각 층
  2141. {
  2142. IQueryable<CmFloor> qF;
  2143. qF = from x in db.CmFloor
  2144. where x.SiteId == siteId && x.BuildingId == buildingId
  2145. select x;
  2146. foreach (var floor in qF)
  2147. {
  2148. query = from x in db.BemsMonitoringPoint
  2149. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId == stype.ServiceTypeId &&
  2150. x.BuildingId == buildingId && x.FloorId == floor.FloorId && x.ZoneId == null
  2151. select new MonitoringPointSType
  2152. {
  2153. SiteId = x.SiteId,
  2154. FacilityCode = x.FacilityCode,
  2155. PropertyId = x.PropertyId,
  2156. ServiceTypeId = (int)x.ServiceTypeId,
  2157. };
  2158. //n = query.Count();
  2159. if (query.Any())
  2160. {
  2161. foreach (var data in query)
  2162. {
  2163. list.Add(data);
  2164. }
  2165. }
  2166. else
  2167. {
  2168. query = from x in db.BemsMonitoringPoint
  2169. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId == stype.ServiceTypeId &&
  2170. x.BuildingId == buildingId && x.FloorId == floor.FloorId && x.ZoneId != null
  2171. select new MonitoringPointSType
  2172. {
  2173. SiteId = x.SiteId,
  2174. FacilityCode = x.FacilityCode,
  2175. PropertyId = x.PropertyId,
  2176. ServiceTypeId = (int)x.ServiceTypeId,
  2177. };
  2178. if (query.Any())
  2179. {
  2180. foreach (var data in query)
  2181. {
  2182. list.Add(data);
  2183. }
  2184. }
  2185. }
  2186. }
  2187. }
  2188. }
  2189. }
  2190. private void AddPointToListInLocationSType_New2(List<MonitoringPointSType> list, int siteId, int buildingId)
  2191. {
  2192. var q = from x in db.BemsServiceType
  2193. where x.ServiceTypeId > 0 && x.ServiceTypeId < 100 select x;
  2194. IQueryable<MonitoringPointSType> query;
  2195. foreach (var stype in q)
  2196. {
  2197. query = from x in db.BemsMonitoringPoint
  2198. where x.SiteId == siteId && x.ServiceTypeId == stype.ServiceTypeId &&
  2199. x.FuelTypeId > 0 && // 2017 02 17 hcLee 방어코드 추가, 보라매현장 DB입력 오류 (서비스타입은 있는데 Fuel이 0인 포인트 존재) 회피코드
  2200. x.BuildingId == buildingId && x.FloorId == null
  2201. select new MonitoringPointSType
  2202. {
  2203. SiteId = x.SiteId,
  2204. FacilityCode = x.FacilityCode,
  2205. PropertyId = x.PropertyId,
  2206. ServiceTypeId = (int)x.ServiceTypeId,
  2207. FuelTypeId = (int)x.FuelTypeId,
  2208. };
  2209. //int n = query.Count();
  2210. if (query.Any())
  2211. {
  2212. foreach (var data in query)
  2213. {
  2214. list.Add(data);
  2215. }
  2216. }
  2217. else // 빌딩만 매핑이 없으면 각 층
  2218. {
  2219. IQueryable<CmFloor> qF;
  2220. qF = from x in db.CmFloor
  2221. where x.SiteId == siteId && x.BuildingId == buildingId select x;
  2222. foreach (var floor in qF)
  2223. {
  2224. query = from x in db.BemsMonitoringPoint
  2225. //where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId == stype.ServiceTypeId &&
  2226. where x.SiteId == siteId && x.ServiceTypeId == stype.ServiceTypeId &&
  2227. x.BuildingId == buildingId && x.FloorId == floor.FloorId && x.ZoneId == null
  2228. select new MonitoringPointSType
  2229. {
  2230. SiteId = x.SiteId,
  2231. FacilityCode = x.FacilityCode,
  2232. PropertyId = x.PropertyId,
  2233. ServiceTypeId = (int)x.ServiceTypeId,
  2234. FuelTypeId = (int)x.FuelTypeId,
  2235. };
  2236. //n = query.Count();
  2237. if (query.Any())
  2238. {
  2239. foreach (var data in query)
  2240. {
  2241. list.Add(data);
  2242. }
  2243. }
  2244. else
  2245. {
  2246. query = from x in db.BemsMonitoringPoint
  2247. //where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId == stype.ServiceTypeId &&
  2248. where x.SiteId == siteId && x.ServiceTypeId == stype.ServiceTypeId &&
  2249. x.BuildingId == buildingId && x.FloorId == floor.FloorId && x.ZoneId != null
  2250. select new MonitoringPointSType
  2251. {
  2252. SiteId = x.SiteId,
  2253. FacilityCode = x.FacilityCode,
  2254. PropertyId = x.PropertyId,
  2255. ServiceTypeId = (int)x.ServiceTypeId,
  2256. FuelTypeId = (int)x.FuelTypeId,
  2257. };
  2258. if (query.Any())
  2259. {
  2260. foreach (var data in query)
  2261. {
  2262. list.Add(data);
  2263. }
  2264. }
  2265. }
  2266. }
  2267. }
  2268. }
  2269. }
  2270. private void AddPointToListInLocationSType(List<MonitoringPointSType> list,
  2271. int siteId, int fuelTypeId, int buildingId, int? floorId, int? zoneId)
  2272. {
  2273. {
  2274. IQueryable<MonitoringPointSType> query;
  2275. // floorId 나 zoneId 가 null를 가지고 있어도, 비교시에는 null로 비교하지 않는다.
  2276. // 그래서 강제적으로 조건에 따라 null를 비교하도록 수정
  2277. if (floorId != null && zoneId == null) // 층처리 층이고 zone이 null인 즉 층전체포인트가 있으면
  2278. {
  2279. query = from x in db.BemsMonitoringPoint
  2280. //where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId != 0 && 2015 08 04 추가 ServiceType에 온도와 습도가 포함되면서 아래로 변경함.
  2281. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId != 0 && x.ServiceTypeId < 100 &&
  2282. x.BuildingId == buildingId && x.FloorId == floorId && x.ZoneId == null
  2283. select new MonitoringPointSType
  2284. {
  2285. SiteId = x.SiteId,
  2286. FacilityCode = x.FacilityCode,
  2287. PropertyId = x.PropertyId,
  2288. ServiceTypeId = (int)x.ServiceTypeId,
  2289. };
  2290. //throw new Exception(string.Format("case1"));
  2291. }
  2292. else if (floorId == null && zoneId == null) // 건물처리
  2293. {
  2294. query = from x in db.BemsMonitoringPoint
  2295. //where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId != 0 &&
  2296. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId != 0 && x.ServiceTypeId < 100 &&
  2297. x.BuildingId == buildingId && x.FloorId == null && x.ZoneId == null
  2298. select new MonitoringPointSType
  2299. {
  2300. SiteId = x.SiteId,
  2301. FacilityCode = x.FacilityCode,
  2302. PropertyId = x.PropertyId,
  2303. ServiceTypeId = (int)x.ServiceTypeId,
  2304. };
  2305. }
  2306. else // 구역처리
  2307. {
  2308. query = from x in db.BemsMonitoringPoint
  2309. //where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId != 0 &&
  2310. where x.SiteId == siteId && x.FuelTypeId == fuelTypeId && x.ServiceTypeId != 0 && x.ServiceTypeId < 100 &&
  2311. x.BuildingId == buildingId && x.FloorId == floorId && x.ZoneId == zoneId
  2312. select new MonitoringPointSType
  2313. {
  2314. SiteId = x.SiteId,
  2315. FacilityCode = x.FacilityCode,
  2316. PropertyId = x.PropertyId,
  2317. ServiceTypeId = (int)x.ServiceTypeId,
  2318. };
  2319. //throw new Exception(string.Format("case4"));
  2320. }
  2321. //int n = query.Count();
  2322. if (query.Any())
  2323. {
  2324. foreach (var data in query)
  2325. {
  2326. list.Add(data);
  2327. }
  2328. return; // 있으면 끝
  2329. }
  2330. }
  2331. {
  2332. if (floorId == null) //
  2333. {
  2334. var query = from x in db.CmFloor
  2335. where x.SiteId == siteId && x.BuildingId == buildingId
  2336. select x.FloorId;
  2337. foreach (var floor in query)
  2338. {
  2339. AddPointToListInLocationSType(list, siteId, fuelTypeId, buildingId, floor, null);
  2340. }
  2341. }
  2342. //else if( zoneId == null )
  2343. if (zoneId == null)
  2344. //else
  2345. {
  2346. var query = from x in db.CmZone
  2347. where x.SiteId == siteId && x.BuildingId == buildingId && x.FloorId == floorId
  2348. select x.ZoneId;
  2349. foreach (var zone in query)
  2350. {
  2351. AddPointToListInLocationSType(list, siteId, fuelTypeId, buildingId, floorId, zone);
  2352. }
  2353. }
  2354. }
  2355. }
  2356. //구역에 맵핑된 온도, 습도 포인트만 찾아낸다. // 한가지씩찾아서 평균으로 수정필요함
  2357. private void AddPointToListInLocation_ZoneTempHumi(List<MonitoringPoint> list,
  2358. int siteId, int buildingId, int? floorId, int? zoneId)
  2359. {
  2360. {
  2361. IQueryable<MonitoringPoint> query;
  2362. // floorId 나 zoneId 가 null를 가지고 있어도, 비교시에는 null로 비교하지 않는다.
  2363. // 그래서 강제적으로 조건에 따라 null를 비교하도록 수정
  2364. // x.FacilityTypeId 91, 92로 가려야 한다. 2015 08 03 hcLee -> x
  2365. {
  2366. query = from x in db.BemsMonitoringPoint
  2367. //where x.SiteId == siteId && (x.PropertyId == 1000 ||x.PropertyId == 1001) && 2015 08 04 hcLee 서비스타입으로 변경함.
  2368. //where x.SiteId == siteId && (x.ServiceTypeId == 100 || x.ServiceTypeId == 110) && // 2016 04 18 co2농도도 추가
  2369. where x.SiteId == siteId && (x.ServiceTypeId == 100 || x.ServiceTypeId == 110 || x.ServiceTypeId == 130) &&
  2370. x.BuildingId == buildingId && x.FloorId == floorId && x.ZoneId == zoneId
  2371. select new MonitoringPoint
  2372. {
  2373. SiteId = x.SiteId,
  2374. FacilityCode = x.FacilityCode,
  2375. PropertyId = x.PropertyId,
  2376. };
  2377. //throw new Exception(string.Format("case4"));
  2378. }
  2379. //int n = query.Count();
  2380. if (query.Any())
  2381. {
  2382. foreach (var data in query)
  2383. {
  2384. list.Add(data);
  2385. }
  2386. return; // 있으면 끝
  2387. }
  2388. }
  2389. }
  2390. //2016 05 25 hcLee
  2391. //층에 맵핑된 온도(100), 습도(110), CO2(130), CO(140) 포인트만 찾아낸다. 평면도 그리는 화면 에너지현황/실내온습도 상태감시 화면에서 사용
  2392. private void AddPointToListInLocation_FloorTempHumi(List<MonitoringPoint> list, int siteId, int buildingId, int? floorId)
  2393. {
  2394. {
  2395. IQueryable<MonitoringPoint> query;
  2396. {
  2397. query = from x in db.BemsMonitoringPoint
  2398. where x.SiteId == siteId && x.FacilityTypeId == 99 && (x.ServiceTypeId == 100 || x.ServiceTypeId == 110 || x.ServiceTypeId == 130 || x.ServiceTypeId == 140 || x.ServiceTypeId == 150 || x.ServiceTypeId == 160) &&
  2399. x.BuildingId == buildingId && x.FloorId == floorId orderby x.Name
  2400. select new MonitoringPoint
  2401. {
  2402. SiteId = x.SiteId,
  2403. FacilityCode = x.FacilityCode,
  2404. PropertyId = x.PropertyId,
  2405. };
  2406. //throw new Exception(string.Format("case4"));
  2407. }
  2408. //int n = query.Count();
  2409. if (query.Any())
  2410. {
  2411. foreach (var data in query)
  2412. {
  2413. list.Add(data);
  2414. }
  2415. return; // 있으면 끝
  2416. }
  2417. }
  2418. }
  2419. //2019 06 26 kgpark
  2420. //층에 맵핑된 온도(100), 습도(110), CO2(130), CO(140), 미세먼지(150), O3(160) 포인트만 찾아낸다. 평면도 그리는 화면 에너지현황/실내온습도 상태감시 화면에서 사용
  2421. private void AddEachPointToListInLocation_FloorTempHumi(List<MonitoringPoint> list, int siteId, int? floorId)
  2422. {
  2423. {
  2424. IQueryable<MonitoringPoint> query;
  2425. {
  2426. query = from x in db.BemsFloorStatus
  2427. where x.SiteId == siteId && x.FacilityTypeId == 99 && x.FloorId == floorId
  2428. select new MonitoringPoint
  2429. {
  2430. SiteId = x.SiteId,
  2431. FacilityCode = x.FacilityCode,
  2432. PropertyId = x.PropertyId,
  2433. };
  2434. //throw new Exception(string.Format("case4"));
  2435. }
  2436. //int n = query.Count();
  2437. if (query.Any())
  2438. {
  2439. foreach (var data in query)
  2440. {
  2441. list.Add(data);
  2442. }
  2443. return; // 있으면 끝
  2444. }
  2445. }
  2446. }
  2447. private void AddPointToList_RunTime(List<MonitoringPoint> list, int siteId, int facilityCode)
  2448. {
  2449. {
  2450. IQueryable<MonitoringPoint> query;
  2451. // floorId 나 zoneId 가 null를 가지고 있어도, 비교시에는 null로 비교하지 않는다.
  2452. // 그래서 강제적으로 조건에 따라 null를 비교하도록 수정
  2453. // x.FacilityTypeId 91, 92로 가려야 한다. 2015 08 03 hcLee -> x
  2454. {
  2455. query = from x in db.BemsMonitoringPoint
  2456. //where x.SiteId == siteId && (x.PropertyId == 1000 ||x.PropertyId == 1001) && 2015 08 04 hcLee 서비스타입으로 변경함.
  2457. where x.SiteId == siteId && x.ServiceTypeId == 120 && x.FacilityCode == facilityCode
  2458. select new MonitoringPoint
  2459. {
  2460. SiteId = x.SiteId,
  2461. FacilityCode = x.FacilityCode,
  2462. PropertyId = x.PropertyId,
  2463. };
  2464. //throw new Exception(string.Format("case4"));
  2465. }
  2466. //int n = query.Count();
  2467. if (query.Any())
  2468. {
  2469. foreach (var data in query)
  2470. {
  2471. list.Add(data);
  2472. }
  2473. return; // 있으면 끝
  2474. }
  2475. }
  2476. }
  2477. private void AddPointToList_Runtime_REFRIGERATOR(List<MonitoringPoint> list, int siteId)
  2478. {
  2479. {
  2480. IQueryable<MonitoringPoint> query;
  2481. {
  2482. query = from x in db.BemsMonitoringPoint
  2483. where x.SiteId == siteId && x.FacilityTypeId == 2 && x.PropertyId == 17
  2484. select new MonitoringPoint
  2485. {
  2486. SiteId = x.SiteId,
  2487. FacilityCode = x.FacilityCode,
  2488. PropertyId = x.PropertyId,
  2489. };
  2490. //throw new Exception(string.Format("case4"));
  2491. }
  2492. //int n = query.Count();
  2493. if (query.Any())
  2494. {
  2495. foreach (var data in query)
  2496. {
  2497. list.Add(data);
  2498. }
  2499. return; // 있으면 끝
  2500. }
  2501. }
  2502. }
  2503. private void AddPointToList_Runtime_BOILER(List<MonitoringPoint> list, int siteId)
  2504. {
  2505. {
  2506. IQueryable<MonitoringPoint> query;
  2507. {
  2508. query = from x in db.BemsMonitoringPoint
  2509. where x.SiteId == siteId && ((x.FacilityTypeId == 8 && x.PropertyId == 9) || (x.FacilityTypeId == 14 && x.PropertyId == 7))
  2510. select new MonitoringPoint
  2511. {
  2512. SiteId = x.SiteId,
  2513. FacilityCode = x.FacilityCode,
  2514. PropertyId = x.PropertyId,
  2515. };
  2516. //throw new Exception(string.Format("case4"));
  2517. }
  2518. //int n = query.Count();
  2519. if (query.Any())
  2520. {
  2521. foreach (var data in query)
  2522. {
  2523. list.Add(data);
  2524. }
  2525. return; // 있으면 끝
  2526. }
  2527. }
  2528. }
  2529. private void AddPointToList_ControlPoint(List<MonitoringPoint> list, int siteId, int ValueType)
  2530. {
  2531. {
  2532. IQueryable<MonitoringPoint> query;
  2533. {
  2534. query = from x in db.BemsMonitoringPoint
  2535. //2016 01 18
  2536. //where x.SiteId == siteId && x.ValueType == ValueType
  2537. where x.SiteId == siteId && (x.ValueType == 1 || x.ValueType == 3)
  2538. select new MonitoringPoint
  2539. {
  2540. SiteId = x.SiteId,
  2541. FacilityCode = x.FacilityCode,
  2542. PropertyId = x.PropertyId,
  2543. };
  2544. //throw new Exception(string.Format("case4"));
  2545. }
  2546. //int n = query.Count();
  2547. if (query.Any())
  2548. {
  2549. foreach (var data in query)
  2550. {
  2551. list.Add(data);
  2552. }
  2553. return; // 있으면 끝
  2554. }
  2555. }
  2556. }
  2557. //2020.05.****************
  2558. private void AddPointToList_ControlPoint2(List<MonitoringPoint> list, int siteId, int propertyId, int facilityTypeId, int facilitycode)
  2559. {
  2560. {
  2561. IQueryable<MonitoringPoint> query;
  2562. {
  2563. query = from x in db.BemsMonitoringPoint
  2564. where x.SiteId == siteId && x.PropertyId == propertyId && x.FacilityTypeId == facilityTypeId && x.FacilityCode == facilitycode
  2565. select new MonitoringPoint
  2566. {
  2567. SiteId = x.SiteId,
  2568. FacilityCode = x.FacilityCode,
  2569. PropertyId = x.PropertyId,
  2570. };
  2571. //throw new Exception(string.Format("case4"));
  2572. }
  2573. //int n = query.Count();
  2574. if (query.Any())
  2575. {
  2576. foreach (var data in query)
  2577. {
  2578. list.Add(data);
  2579. }
  2580. return; // 있으면 끝
  2581. }
  2582. }
  2583. }
  2584. //*******************2020.05.
  2585. /*
  2586. [ActionName("ServiceType")]
  2587. public CalculationValue[] GetBemsMonitoringPointHistoryServiceType()
  2588. {
  2589. Uri uri = Request.RequestUri;
  2590. var uriQuery = HttpUtility.ParseQueryString(uri.Query);
  2591. string uriTimeIntervalType = uriQuery.Get("TimeIntervalType");
  2592. string uriStartDate = uriQuery.Get("StartDate");
  2593. string uriEndDate = uriQuery.Get("EndDate");
  2594. int siteId;
  2595. int buildingId, floorId, zoneId, fuelTypeId, timeInterval = -1;
  2596. buildingId = 1;
  2597. DateTime startDate, endDate;
  2598. if (int.TryParse(uriQuery.Get("SiteId"), out siteId) == false ||
  2599. // int.TryParse(uriQuery.Get("BuildingId"), out buildingId) == false ||
  2600. int.TryParse(uriTimeIntervalType, out timeInterval) == false ||
  2601. DateTime.TryParse(uriStartDate, out startDate) == false ||
  2602. DateTime.TryParse(uriEndDate, out endDate) == false ||
  2603. int.TryParse(uriQuery.Get("FuelTypeId"), out fuelTypeId) == false)
  2604. {
  2605. throw new Exception(string.Format("Not Found Parameters: {0},{1},{2}",
  2606. uriTimeIntervalType, uriStartDate, uriEndDate));
  2607. }
  2608. TimeInterval timeIntervalType = (TimeInterval)timeInterval;
  2609. int? nullableFloorId = null;
  2610. int? nullableZoneId = null;
  2611. if (int.TryParse(uriQuery.Get("FloorId"), out floorId))
  2612. {
  2613. nullableFloorId = floorId;
  2614. if (int.TryParse(uriQuery.Get("ZoneId"), out zoneId))
  2615. {
  2616. nullableZoneId = zoneId;
  2617. }
  2618. }
  2619. List<MonitoringPoint> list = new List<MonitoringPoint>();
  2620. try
  2621. {
  2622. AddPointToListInLocation(list, siteId, fuelTypeId, buildingId, nullableFloorId, nullableZoneId);
  2623. var values = CreateCalculationValues(startDate, endDate, timeIntervalType);
  2624. foreach (var point in list)
  2625. {
  2626. var pointValues = pointHistoryValueManager.GetPointValues(
  2627. siteId, point.FacilityCode, point.PropertyId,
  2628. timeIntervalType, startDate, endDate);
  2629. if (pointValues == null)
  2630. continue;
  2631. int index = 0;
  2632. CalculationValue p = null;
  2633. CalculationValue v = null;
  2634. for (var i = 0; i < values.Length; i++)
  2635. {
  2636. v = values[i];
  2637. if (p == null)
  2638. {
  2639. if (pointValues.Length <= index) break;
  2640. p = pointValues[index];
  2641. }
  2642. if (p.DateTime == v.DateTime)
  2643. {
  2644. v.Value += p.Value;
  2645. p = null;
  2646. index++;
  2647. if (pointValues.Length <= index) break;
  2648. }
  2649. }
  2650. }
  2651. return values;
  2652. }
  2653. catch (Exception)
  2654. {
  2655. throw;
  2656. }
  2657. }
  2658. */
  2659. }
  2660. // hcLee 2015 03 30
  2661. /*
  2662. public class Building
  2663. {
  2664. public int SiteId { get; set; }
  2665. public int BuildingId { get; set; }
  2666. public int Name { get; set; }
  2667. }; */
  2668. public class MonitoringPoint
  2669. {
  2670. public int SiteId { get; set; }
  2671. public int FacilityCode { get; set; }
  2672. public int PropertyId { get; set; }
  2673. };
  2674. // hcLee 2015 03 31
  2675. public class MonitoringPointSType
  2676. {
  2677. public int SiteId { get; set; }
  2678. public int FacilityCode { get; set; }
  2679. public int PropertyId { get; set; }
  2680. public int ServiceTypeId { get; set; }
  2681. public int FuelTypeId { get; set; } // 2016 09 18 hcLee 추가
  2682. };
  2683. public class PointToLocation
  2684. {
  2685. public int SiteId { get; set; }
  2686. public int FacilityCode { get; set; }
  2687. public int PropertyId { get; set; }
  2688. public int? BuildingId { get; set; }
  2689. public int? FloorId { get; set; }
  2690. public int? ZoneId { get; set; }
  2691. };
  2692. /*
  2693. //hcLee 2015 07 16
  2694. public class ZoneTempHumiData
  2695. {
  2696. public int SiteId { get; set; }
  2697. public int? BuildingId { get; set; }
  2698. public int? FloorId { get; set; }
  2699. public int? ZoneId { get; set; }
  2700. };
  2701. */
  2702. public class GetCenterSiteData_Param
  2703. {
  2704. public int TimeIntervalType { get; set; }
  2705. public DateTime startDate { get; set; }
  2706. public DateTime endDate { get; set; }
  2707. //public Nullable<int> FacilityCode { get; set; }
  2708. //public int FacilityCode { get; set; }
  2709. };
  2710. public class GetPrediction_Param
  2711. {
  2712. public int SiteId { get; set; }
  2713. public int BuildingId { get; set; }
  2714. public int FuelType { get; set; }
  2715. public DateTime startDate { get; set; }
  2716. public DateTime endDate{ get; set; }
  2717. //public Nullable<int> FacilityCode { get; set; }
  2718. //public int FacilityCode { get; set; }
  2719. };
  2720. // jhLee 2016-04-22
  2721. public class FacilityCostInfo
  2722. {
  2723. public int SiteId { get; set; }
  2724. public int FacilityCode { get; set; }
  2725. public string RatedPowerConsumption { get; set; }
  2726. public Nullable<short> FuelTypeId { get; set; }
  2727. public Nullable<short> ContractType { get; set; }
  2728. };
  2729. //2016 07 01 hcLee
  2730. public class CenterSiteData
  2731. {
  2732. public int siteid { get; set; }
  2733. public string name { get; set; }
  2734. public List<CalculationResult> e_value_list { get; set; } // 오늘 전기
  2735. public List<CalculationResult> g_value_list { get; set; } // 오늘 가스
  2736. public Nullable<double> elec_current { get; set; } // 현재전기사용량
  2737. public Nullable<double> elec_target { get; set; } // 현재전기목표
  2738. public Nullable<double> gas_current { get; set; } // 현재가스사용량
  2739. public Nullable<double> gas_target { get; set; } // 현재가스목표
  2740. //public Nullable<double> kcal_target { get; set; } // 현재기준목표 kcal(전기+가스)
  2741. };
  2742. }