2025-01-16 15:06:18 +08:00
using Backend.Models ;
using BackendWorkerService.Models ;
using Microsoft.Extensions.Logging ;
using Newtonsoft.Json ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Net ;
using System.Threading.Tasks ;
using System.Xml ;
using Repository.Models ;
using System.Linq ;
using System.Web ;
using Repository.BackendRepository.Interface ;
using System.Text ;
using Newtonsoft.Json.Linq ;
namespace BackendWorkerService.Services.Implement
{
public class SolarService
{
public async Task < bool > SyncDevice ( ILogger logger , List < device > data , ObixApiConfig obixApiConfig , string bql , List < site > sites )
{
bool result = false ;
try
{
string encoded = System . Convert . ToBase64String ( System . Text . Encoding . GetEncoding ( "ISO-8859-1" ) . GetBytes ( obixApiConfig . UserName + ":" + obixApiConfig . Password ) ) ;
HttpWebRequest Postrequest = ( HttpWebRequest ) WebRequest . Create ( $"{obixApiConfig.ApiBase}obix/config/Program/ObixQuery/query/" ) ;
Postrequest . Method = "POST" ;
Postrequest . Headers . Add ( "Authorization" , "Basic " + encoded ) ;
Postrequest . PreAuthenticate = true ;
using ( var streamWriter = new StreamWriter ( Postrequest . GetRequestStream ( ) ) )
{
string json = "<str val='" + bql + "'/>" ;
streamWriter . Write ( json ) ;
}
HttpWebResponse response = ( HttpWebResponse ) Postrequest . GetResponse ( ) ;
var responseString = new StreamReader ( response . GetResponseStream ( ) ) . ReadToEnd ( ) ;
XmlDocument xmlDoc = new XmlDocument ( ) ;
xmlDoc . LoadXml ( responseString ) ;
string jsonText = JsonConvert . SerializeXmlNode ( xmlDoc ) ;
var jsonData = Welcome . FromJson ( jsonText ) ;
if ( jsonData . Obj ! = null & & jsonData . Obj . Str ! = null )
{
foreach ( var jd in jsonData . Obj . Str )
{
var value = jd . Val . Split ( "," ) ;
if ( value . Length > 0 )
{
var device = new device ( ) ;
string tmpPath = value [ 0 ] . Substring ( 0 , value [ 0 ] . Length - 1 ) ;
int lastIndex = tmpPath . LastIndexOf ( '/' ) ;
device . path_n4 = tmpPath . Substring ( 0 , lastIndex + 1 ) ;
device . site_id = value [ 0 ] . Split ( "/" ) [ 2 ] . Trim ( ) ;
device . device_sys_tag = value [ 0 ] . Split ( "/" ) [ 3 ] . Trim ( ) ;
device . device_id = value [ 0 ] . Split ( "/" ) [ value [ 0 ] . Split ( "/" ) . Length - 3 ] . Trim ( ) ;
device . site_name = sites . Where ( x = > x . site_id = = device . site_id ) . FirstOrDefault ( ) . site_name ? ? "" ;
if ( ! data . Any ( x = > x . site_id = = device . site_id & & x . device_sys_tag = = device . device_sys_tag & & x . device_id = = device . device_id ) )
data . Add ( device ) ;
}
}
}
result = true ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【SyncDevice】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > ImportDevice ( ILogger logger , IBackgroundServicePostgresqlRepository backgroundServiceRepository , List < device > data )
{
bool result = false ;
try
{
string sql = string . Empty ;
#region clear data
sql = "delete from import_device" ;
await backgroundServiceRepository . ExecuteSql ( sql ) ;
#endregion
#region insert data
if ( data . Any ( ) )
{
sql = @ $"insert into import_device (site_id, site_name, device_sys_tag, path_n4, device_id, created_at) values " ;
int index = 1 ;
foreach ( var d in data )
{
sql + = $"('{d.site_id}', '{d.site_name}', '{d.device_sys_tag}', '{d.path_n4}', '{d.device_id}', now()){(index == data.Count ? " ; " : " , ")}" ;
index + + ;
}
await backgroundServiceRepository . ExecuteSql ( sql ) ;
}
#endregion
#region compare device data with import_device
//update
sql = @ "UPDATE device
SET is_link = idev . is_link , site_name = idev . site_name
FROM (
select d . site_id , d . device_sys_tag , d . device_id , CASE WHEN idev . site_id IS NULL THEN b '0' ELSE b '1' END is_link , idev . site_name
from device d
left join import_device idev on d . site_id = idev . site_id and d . device_sys_tag = idev . device_sys_tag and d . device_id = idev . device_id
) idev
WHERE device . site_id = idev . site_id
AND device . device_sys_tag = idev . device_sys_tag
AND device . device_id = idev . device_id ; ";
await backgroundServiceRepository . ExecuteSql ( sql ) ;
//insert
2025-01-24 12:05:03 +08:00
sql = @ "insert into device (site_id, site_name, device_sys_tag, archive_lastdate, path_n4, device_Id, is_link, created_at)
select idev . site_id , idev . site_name , idev . device_sys_tag , now ( ) , idev . path_n4 , idev . device_Id , b '1' , now ( )
2025-01-16 15:06:18 +08:00
from import_device idev
left join device d on d . site_id = idev . site_id and d . device_sys_tag = idev . device_sys_tag and d . device_id = idev . device_id
where d . site_id is null ;
update device set is_link = b '0' where is_link is null or is_link = ' ' ; ";
await backgroundServiceRepository . ExecuteSql ( sql ) ;
#endregion
result = true ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【ImportDevice】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > SyncData ( ILogger logger , IBackgroundServicePostgresqlRepository backgroundServiceRepository , ObixApiConfig obixApiConfig ,
List < device > deviceData , List < device_point > devicePointData , string startTimeStamp , string endTimeStamp , string intervalValue ,
List < data_value > dataValue , bool isRecord )
{
bool result = true ;
string sql = string . Empty ;
string path = string . Empty ;
try
{
XmlDocument xmlDocument = new XmlDocument ( ) ;
sql = "select system_value from variable where deleted = 0 and system_type = 'obixConfig' and system_key = 'niagara status'" ;
string station = await backgroundServiceRepository . GetOneAsync < string > ( sql ) ;
string encoded = System . Convert . ToBase64String ( System . Text . Encoding . GetEncoding ( "ISO-8859-1" ) . GetBytes ( obixApiConfig . UserName + ":" + obixApiConfig . Password ) ) ;
var data = new List < data > ( ) ;
var historyQueryFilter = $ @ "<obj is='obix: HistoryFilter'>
< abstime name = ' start ' val = ' { startTimeStamp } ' / >
< abstime name = ' end ' val = ' { endTimeStamp } ' / >
< reltime name = ' interval ' val = ' { intervalValue } ' / >
< / obj > ";
foreach ( var dd in deviceData )
{
#region update device archive_lastActionDate
sql = @ $ "update device set archive_lastactiondate = now()
where site_id = ' { dd . site_id } ' and device_sys_tag = ' { dd . device_sys_tag } ' and device_id = ' { dd . device_id } ' ";
await backgroundServiceRepository . ExecuteSql ( sql ) ;
#endregion
#region main process
foreach ( var dpd in devicePointData . Where ( x = > x . site_id = = dd . site_id & & x . device_sys_tag = = dd . device_sys_tag & & x . device_id = = dd . device_id ) )
{
path = dd . site_id + "_" + dd . device_sys_tag + ( dd . device_id = = dd . device_sys_tag ? "" : "_" + dd . device_id ) + "_" + dpd . point ;
HttpWebRequest archiveHourRequest = ( HttpWebRequest ) WebRequest . Create ( $"{obixApiConfig.ApiBase}obix/histories/{station}/{path}/~historyRollup/" ) ;
archiveHourRequest . Method = "POST" ;
archiveHourRequest . Headers . Add ( "Authorization" , "Basic " + encoded ) ;
archiveHourRequest . PreAuthenticate = true ;
byte [ ] byteArray = Encoding . UTF8 . GetBytes ( historyQueryFilter ) ;
using ( Stream reqStream = archiveHourRequest . GetRequestStream ( ) )
{
reqStream . Write ( byteArray , 0 , byteArray . Length ) ;
}
HttpWebResponse archiveHourResponse = ( HttpWebResponse ) archiveHourRequest . GetResponse ( ) ;
var archiveHourResponseContent = new StreamReader ( archiveHourResponse . GetResponseStream ( ) ) . ReadToEnd ( ) ;
xmlDocument . LoadXml ( archiveHourResponseContent ) ;
string archiveHourJson = JsonConvert . SerializeXmlNode ( xmlDocument ) ;
JObject jsonResult = ( JObject ) JsonConvert . DeserializeObject ( archiveHourJson ) ;
if ( jsonResult . ContainsKey ( "err" ) ) //抓取錯誤
{
result = false ;
}
else
{
var rawdateCount = Convert . ToInt32 ( jsonResult [ "obj" ] [ "int" ] [ "@val" ] . ToString ( ) ) ;
var histories = jsonResult [ "obj" ] [ "list" ] [ "obj" ] ;
if ( rawdateCount > 1 )
{
foreach ( var history in histories )
{
var d = new data ( ) ;
d . site_id = dd . site_id ;
d . device_id = dd . device_id ;
d . device_sys_tag = dd . device_sys_tag ;
d . value = new List < Dictionary < string , decimal > > ( ) ;
var realValue = new Dictionary < string , decimal > ( ) ;
if ( history [ "abstime" ] ! = null & & history [ "abstime" ] . HasValues )
{
foreach ( var abstime in history [ "abstime" ] )
{
var name = abstime [ "@name" ] . ToString ( ) ;
switch ( name )
{
case "start" :
d . timestamp = Convert . ToDateTime ( abstime [ "@val" ] . ToString ( ) ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
break ;
}
}
}
if ( history [ "real" ] ! = null & & history [ "real" ] . HasValues )
{
foreach ( var real in history [ "real" ] )
{
var name = real [ "@name" ] . ToString ( ) ;
if ( name = = ( dataValue . Where ( x = > x . site_id = = dd . site_id & & x . device_id = = dd . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . point = = dpd . point ) . FirstOrDefault ( ) ? . value_unit ? ? "" ) )
{
var value = Decimal . Parse ( real [ "@val" ] . ToString ( ) , System . Globalization . NumberStyles . Float ) ;
realValue = new Dictionary < string , decimal > ( ) { { dpd . point . ToUpper ( ) , value } } ;
}
}
}
if ( data . Any ( x = > x . site_id = = d . site_id & & x . device_id = = d . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . timestamp = = d . timestamp ) )
data . Where ( x = > x . site_id = = d . site_id & & x . device_id = = d . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . timestamp = = d . timestamp ) . FirstOrDefault ( ) . value . Add ( realValue ) ;
else
{
d . value . Add ( realValue ) ;
data . Add ( d ) ;
}
}
}
else
{
var d = new data ( ) ;
d . site_id = dd . site_id ;
d . device_id = dd . device_id ;
d . device_sys_tag = dd . device_sys_tag ;
d . value = new List < Dictionary < string , decimal > > ( ) ;
var realValue = new Dictionary < string , decimal > ( ) ;
if ( histories [ "abstime" ] ! = null & & histories [ "abstime" ] . HasValues )
{
foreach ( var abstime in histories [ "abstime" ] )
{
var name = abstime [ "@name" ] . ToString ( ) ;
switch ( name )
{
case "start" :
d . timestamp = Convert . ToDateTime ( abstime [ "@val" ] . ToString ( ) ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
break ;
}
}
}
if ( histories [ "real" ] ! = null & & histories [ "real" ] . HasValues )
{
foreach ( var real in histories [ "real" ] )
{
var name = real [ "@name" ] . ToString ( ) ;
if ( name = = ( dataValue . Where ( x = > x . site_id = = dd . site_id & & x . device_id = = dd . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . point = = dpd . point ) . FirstOrDefault ( ) ? . value_unit ? ? "" ) )
{
var value = Decimal . Parse ( real [ "@val" ] . ToString ( ) , System . Globalization . NumberStyles . Float ) ;
realValue = new Dictionary < string , decimal > ( ) { { dpd . point . ToUpper ( ) , value } } ;
}
}
}
if ( data . Any ( x = > x . site_id = = d . site_id & & x . device_id = = d . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . timestamp = = d . timestamp ) )
data . Where ( x = > x . site_id = = d . site_id & & x . device_id = = d . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . timestamp = = d . timestamp ) . FirstOrDefault ( ) . value . Add ( realValue ) ;
else
{
d . value . Add ( realValue ) ;
data . Add ( d ) ;
}
}
}
}
#endregion
#region update device archive_lastDate
if ( isRecord & & ( DateTime . Parse ( endTimeStamp ) - DateTime . Parse ( startTimeStamp ) ) . Days = = 1 )
{
#region check range of archive_lastdate
sql = $@"select archive_lastdate from device where site_id = '{dd.site_id}' and device_sys_tag = '{dd.device_sys_tag}' and device_id = '{dd.device_id}'" ;
var archive_lastdate = await backgroundServiceRepository . GetOneAsync < string > ( sql ) ;
if ( ! string . IsNullOrEmpty ( archive_lastdate ) )
{
int index = ( DateTime . Parse ( startTimeStamp ) - DateTime . Parse ( archive_lastdate ) ) . Days ;
var now = DateTime . Parse ( archive_lastdate ) ;
while ( index > 0 )
{
var preHour = now . AddDays ( - 1 ) ; //取得前一天
var chkStartTimestamp = $"{preHour.ToString(" yyyy - MM - dd ")}T00:00:00.000+08:00" ;
var chkEndTimestamp = $"{now.ToString(" yyyy - MM - dd ")}T00:00:00.000+08:00" ;
var chkIntervalValue = "PT1H" ;
await SyncData ( logger , backgroundServiceRepository , obixApiConfig , new List < device > ( ) { dd } , devicePointData , chkStartTimestamp , chkEndTimestamp , chkIntervalValue , dataValue , false ) ;
index - - ;
now = now . AddDays ( 1 ) ;
}
}
#endregion
sql = @ $ "update device set archive_lastDate = now()
where site_id = ' { dd . site_id } ' and device_sys_tag = ' { dd . device_sys_tag } ' and device_id = ' { dd . device_id } ' ";
await backgroundServiceRepository . ExecuteSql ( sql ) ;
}
#endregion
}
if ( data . Any ( ) )
{
var types = data . GroupBy ( x = > x . device_sys_tag ) . Select ( x = > x . Key . ToLower ( ) ) ;
sql = "" ;
foreach ( var t in types )
{
foreach ( var d in data . Where ( x = > x . device_sys_tag . ToLower ( ) = = t ) )
{
if ( t = = "inverter" )
{
sql + = @ $ "WITH updated_rows AS (
UPDATE inverter_history_hour
SET
"" Irradiance "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Irradiance" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Irradiance" . ToUpper ( ) ) ) [ "Irradiance" . ToUpper ( ) ] ) } ,
"" ACV1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV1" . ToUpper ( ) ) ) [ "ACV1" . ToUpper ( ) ] ) } ,
"" ACA1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA1" . ToUpper ( ) ) ) [ "ACA1" . ToUpper ( ) ] ) } ,
"" ACW1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW1" . ToUpper ( ) ) ) [ "ACW1" . ToUpper ( ) ] ) } ,
"" AC1F "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1F" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1F" . ToUpper ( ) ) ) [ "AC1F" . ToUpper ( ) ] ) } ,
"" AC1WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1WH" . ToUpper ( ) ) ) [ "AC1WH" . ToUpper ( ) ] ) } ,
"" ACV2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV2" . ToUpper ( ) ) ) [ "ACV2" . ToUpper ( ) ] ) } ,
"" ACA2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA2" . ToUpper ( ) ) ) [ "ACA2" . ToUpper ( ) ] ) } ,
"" ACW2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW2" . ToUpper ( ) ) ) [ "ACW2" . ToUpper ( ) ] ) } ,
"" AC2F "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2F" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2F" . ToUpper ( ) ) ) [ "AC2F" . ToUpper ( ) ] ) } ,
"" AC2WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2WH" . ToUpper ( ) ) ) [ "AC2WH" . ToUpper ( ) ] ) } ,
"" ACV3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV3" . ToUpper ( ) ) ) [ "ACV3" . ToUpper ( ) ] ) } ,
"" ACA3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA3" . ToUpper ( ) ) ) [ "ACA3" . ToUpper ( ) ] ) } ,
"" ACW3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW3" . ToUpper ( ) ) ) [ "ACW3" . ToUpper ( ) ] ) } ,
"" AC3F "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3F" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3F" . ToUpper ( ) ) ) [ "AC3F" . ToUpper ( ) ] ) } ,
"" AC3WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3WH" . ToUpper ( ) ) ) [ "AC3WH" . ToUpper ( ) ] ) } ,
"" DCV1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV1" . ToUpper ( ) ) ) [ "DCV1" . ToUpper ( ) ] ) } ,
"" DCA1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA1" . ToUpper ( ) ) ) [ "DCA1" . ToUpper ( ) ] ) } ,
"" DCW1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW1" . ToUpper ( ) ) ) [ "DCW1" . ToUpper ( ) ] ) } ,
"" DC1KW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1KW" . ToUpper ( ) ) ) [ "DC1KW" . ToUpper ( ) ] ) } ,
"" DC1WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1WH" . ToUpper ( ) ) ) [ "DC1WH" . ToUpper ( ) ] ) } ,
"" DCV2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV2" . ToUpper ( ) ) ) [ "DCV2" . ToUpper ( ) ] ) } ,
"" DCA2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA2" . ToUpper ( ) ) ) [ "DCA2" . ToUpper ( ) ] ) } ,
"" DCW2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW2" . ToUpper ( ) ) ) [ "DCW2" . ToUpper ( ) ] ) } ,
"" DC2KW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2KW" . ToUpper ( ) ) ) [ "DC2KW" . ToUpper ( ) ] ) } ,
"" DC2WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2WH" . ToUpper ( ) ) ) [ "DC2WH" . ToUpper ( ) ] ) } ,
"" DCV3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV3" . ToUpper ( ) ) ) [ "DCV3" . ToUpper ( ) ] ) } ,
"" DCA3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA3" . ToUpper ( ) ) ) [ "DCA3" . ToUpper ( ) ] ) } ,
"" DCW3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW3" . ToUpper ( ) ) ) [ "DCW3" . ToUpper ( ) ] ) } ,
"" DC3KW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3KW" . ToUpper ( ) ) ) [ "DC3KW" . ToUpper ( ) ] ) } ,
"" DC3WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3WH" . ToUpper ( ) ) ) [ "DC3WH" . ToUpper ( ) ] ) } ,
"" DC4V "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4V" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4V" . ToUpper ( ) ) ) [ "DC4V" . ToUpper ( ) ] ) } ,
"" DC4A "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4A" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4A" . ToUpper ( ) ) ) [ "DC4A" . ToUpper ( ) ] ) } ,
"" DC4W "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4W" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4W" . ToUpper ( ) ) ) [ "DC4W" . ToUpper ( ) ] ) } ,
"" DC4KW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4KW" . ToUpper ( ) ) ) [ "DC4KW" . ToUpper ( ) ] ) } ,
"" DC4WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4WH" . ToUpper ( ) ) ) [ "DC4WH" . ToUpper ( ) ] ) } ,
"" DCV5 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV5" . ToUpper ( ) ) ) [ "DCV5" . ToUpper ( ) ] ) } ,
"" DCA5 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA5" . ToUpper ( ) ) ) [ "DCA5" . ToUpper ( ) ] ) } ,
"" DCW5 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW5" . ToUpper ( ) ) ) [ "DCW5" . ToUpper ( ) ] ) } ,
"" DC5KW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5KW" . ToUpper ( ) ) ) [ "DC5KW" . ToUpper ( ) ] ) } ,
"" DC5WH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5WH" . ToUpper ( ) ) ) [ "DC5WH" . ToUpper ( ) ] ) } ,
"" PR "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) [ "PR" . ToUpper ( ) ] ) } ,
"" RA1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA1" . ToUpper ( ) ) ) [ "RA1" . ToUpper ( ) ] ) } ,
"" RA2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA2" . ToUpper ( ) ) ) [ "RA2" . ToUpper ( ) ] ) } ,
"" RA3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA3" . ToUpper ( ) ) ) [ "RA3" . ToUpper ( ) ] ) } ,
"" RA4 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA4" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA4" . ToUpper ( ) ) ) [ "RA4" . ToUpper ( ) ] ) } ,
"" RA5 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA5" . ToUpper ( ) ) ) [ "RA5" . ToUpper ( ) ] ) } ,
"" DCKW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCKW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCKW" . ToUpper ( ) ) ) [ "DCKW" . ToUpper ( ) ] ) } ,
"" ACKW "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACKW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACKW" . ToUpper ( ) ) ) [ "ACKW" . ToUpper ( ) ] ) } ,
"" KWH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) [ "KWH" . ToUpper ( ) ] ) } ,
"" TODAYKWH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) [ "TODAYKWH" . ToUpper ( ) ] ) } ,
"" TOTALKWH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) [ "TOTALKWH" . ToUpper ( ) ] ) } ,
"" KWHKWP "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) [ "KWHKWP" . ToUpper ( ) ] ) }
WHERE site_id = ' { d . site_id } ' and "" device_Id "" = ' { d . device_id } ' and "" TIMESTAMP "" = ' { d . timestamp } '
RETURNING *
)
insert into inverter_history_hour ( site_id , "" device_Id "" , "" TIMESTAMP "" , "" Irradiance "" , "" ACV1 "" , "" ACA1 "" , "" ACW1 "" , "" AC1F "" , "" AC1WH "" , "" ACV2 "" , "" ACA2 "" , "" ACW2 "" , "" AC2F "" , "" AC2WH "" , "" ACV3 "" , "" ACA3 "" , "" ACW3 "" , "" AC3F "" ,
"" AC3WH "" , "" DCV1 "" , "" DCA1 "" , "" DCW1 "" , "" DC1KW "" , "" DC1WH "" , "" DCV2 "" , "" DCA2 "" , "" DCW2 "" , "" DC2KW "" , "" DC2WH "" , "" DCV3 "" , "" DCA3 "" , "" DCW3 "" , "" DC3KW "" , "" DC3WH "" , "" DC4V "" , "" DC4A "" , "" DC4W "" , "" DC4KW "" ,
"" DC4WH "" , "" DCV5 "" , "" DCA5 "" , "" DCW5 "" , "" DC5KW "" , "" DC5WH "" , "" PR "" , "" RA1 "" , "" RA2 "" , "" RA3 "" , "" RA4 "" , "" RA5 "" , "" DCKW "" , "" ACKW "" , "" KWH "" , "" TODAYKWH "" , "" TOTALKWH "" , "" KWHKWP "" , "" CrdDate "" )
select ' { d . site_id } ' , ' { d . device_id } ' , ' { d . timestamp } ' ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Irradiance" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Irradiance" . ToUpper ( ) ) ) [ "Irradiance" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV1" . ToUpper ( ) ) ) [ "ACV1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA1" . ToUpper ( ) ) ) [ "ACA1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW1" . ToUpper ( ) ) ) [ "ACW1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1F" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1F" . ToUpper ( ) ) ) [ "AC1F" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC1WH" . ToUpper ( ) ) ) [ "AC1WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV2" . ToUpper ( ) ) ) [ "ACV2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA2" . ToUpper ( ) ) ) [ "ACA2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW2" . ToUpper ( ) ) ) [ "ACW2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2F" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2F" . ToUpper ( ) ) ) [ "AC2F" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC2WH" . ToUpper ( ) ) ) [ "AC2WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACV3" . ToUpper ( ) ) ) [ "ACV3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACA3" . ToUpper ( ) ) ) [ "ACA3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACW3" . ToUpper ( ) ) ) [ "ACW3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3F" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3F" . ToUpper ( ) ) ) [ "AC3F" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "AC3WH" . ToUpper ( ) ) ) [ "AC3WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV1" . ToUpper ( ) ) ) [ "DCV1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA1" . ToUpper ( ) ) ) [ "DCA1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW1" . ToUpper ( ) ) ) [ "DCW1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1KW" . ToUpper ( ) ) ) [ "DC1KW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC1WH" . ToUpper ( ) ) ) [ "DC1WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV2" . ToUpper ( ) ) ) [ "DCV2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA2" . ToUpper ( ) ) ) [ "DCA2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW2" . ToUpper ( ) ) ) [ "DCW2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2KW" . ToUpper ( ) ) ) [ "DC2KW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC2WH" . ToUpper ( ) ) ) [ "DC2WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV3" . ToUpper ( ) ) ) [ "DCV3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA3" . ToUpper ( ) ) ) [ "DCA3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW3" . ToUpper ( ) ) ) [ "DCW3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3KW" . ToUpper ( ) ) ) [ "DC3KW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC3WH" . ToUpper ( ) ) ) [ "DC3WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4V" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4V" . ToUpper ( ) ) ) [ "DC4V" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4A" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4A" . ToUpper ( ) ) ) [ "DC4A" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4W" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4W" . ToUpper ( ) ) ) [ "DC4W" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4KW" . ToUpper ( ) ) ) [ "DC4KW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC4WH" . ToUpper ( ) ) ) [ "DC4WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCV5" . ToUpper ( ) ) ) [ "DCV5" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCA5" . ToUpper ( ) ) ) [ "DCA5" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCW5" . ToUpper ( ) ) ) [ "DCW5" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5KW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5KW" . ToUpper ( ) ) ) [ "DC5KW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5WH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DC5WH" . ToUpper ( ) ) ) [ "DC5WH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) [ "PR" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA1" . ToUpper ( ) ) ) [ "RA1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA2" . ToUpper ( ) ) ) [ "RA2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA3" . ToUpper ( ) ) ) [ "RA3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA4" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA4" . ToUpper ( ) ) ) [ "RA4" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA5" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "RA5" . ToUpper ( ) ) ) [ "RA5" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCKW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "DCKW" . ToUpper ( ) ) ) [ "DCKW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACKW" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "ACKW" . ToUpper ( ) ) ) [ "ACKW" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) [ "KWH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) [ "TODAYKWH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) [ "TOTALKWH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) [ "KWHKWP" . ToUpper ( ) ] ) } ,
now ( )
WHERE NOT EXISTS ( SELECT 1 FROM updated_rows ) ; ";
}
else if ( t = = "sensor" )
{
sql + = @ $ "WITH updated_rows AS (
UPDATE sensoravg_history_hour
SET
"" Totalsun "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Totalsun" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Totalsun" . ToUpper ( ) ) ) [ "Totalsun" . ToUpper ( ) ] ) } ,
"" Temp "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Temp" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Temp" . ToUpper ( ) ) ) [ "Temp" . ToUpper ( ) ] ) } ,
"" Wm2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Wm2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Wm2" . ToUpper ( ) ) ) [ "Wm2" . ToUpper ( ) ] ) } ,
"" SENSORAVG04 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG04" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG04" . ToUpper ( ) ) ) [ "SENSORAVG04" . ToUpper ( ) ] ) } ,
"" SENSORAVG05 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG05" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG05" . ToUpper ( ) ) ) [ "SENSORAVG05" . ToUpper ( ) ] ) } ,
"" SENSORAVG06 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG06" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG06" . ToUpper ( ) ) ) [ "SENSORAVG06" . ToUpper ( ) ] ) } ,
"" SENSORAVG07 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG07" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG07" . ToUpper ( ) ) ) [ "SENSORAVG07" . ToUpper ( ) ] ) } ,
"" SENSORAVG08 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG08" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG08" . ToUpper ( ) ) ) [ "SENSORAVG08" . ToUpper ( ) ] ) } ,
"" SENSORAVG09 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG09" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG09" . ToUpper ( ) ) ) [ "SENSORAVG09" . ToUpper ( ) ] ) } ,
"" SENSORAVG10 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG10" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG10" . ToUpper ( ) ) ) [ "SENSORAVG10" . ToUpper ( ) ] ) } ,
"" SENSORAVG11 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG11" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG11" . ToUpper ( ) ) ) [ "SENSORAVG11" . ToUpper ( ) ] ) } ,
"" SENSORAVG12 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG12" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG12" . ToUpper ( ) ) ) [ "SENSORAVG12" . ToUpper ( ) ] ) } ,
"" SENSORAVG13 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG13" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG13" . ToUpper ( ) ) ) [ "SENSORAVG13" . ToUpper ( ) ] ) } ,
"" SENSORAVG14 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG14" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG14" . ToUpper ( ) ) ) [ "SENSORAVG14" . ToUpper ( ) ] ) } ,
"" SENSORAVG15 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG15" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG15" . ToUpper ( ) ) ) [ "SENSORAVG15" . ToUpper ( ) ] ) } ,
"" SENSORAVG16 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG16" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG16" . ToUpper ( ) ) ) [ "SENSORAVG16" . ToUpper ( ) ] ) } ,
"" SENSORAVG17 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG17" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG17" . ToUpper ( ) ) ) [ "SENSORAVG17" . ToUpper ( ) ] ) } ,
"" SENSORAVG18 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG18" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG18" . ToUpper ( ) ) ) [ "SENSORAVG18" . ToUpper ( ) ] ) } ,
"" SENSORAVG19 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG19" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG19" . ToUpper ( ) ) ) [ "SENSORAVG19" . ToUpper ( ) ] ) } ,
"" SENSORAVG20 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG20" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG20" . ToUpper ( ) ) ) [ "SENSORAVG20" . ToUpper ( ) ] ) } ,
"" SENSORAVG21 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG21" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG21" . ToUpper ( ) ) ) [ "SENSORAVG21" . ToUpper ( ) ] ) } ,
"" SENSORAVG22 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG22" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG22" . ToUpper ( ) ) ) [ "SENSORAVG22" . ToUpper ( ) ] ) } ,
"" SENSORAVG23 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG23" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG23" . ToUpper ( ) ) ) [ "SENSORAVG23" . ToUpper ( ) ] ) } ,
"" SENSORAVG24 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG24" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG24" . ToUpper ( ) ) ) [ "SENSORAVG24" . ToUpper ( ) ] ) } ,
"" SENSORAVG25 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG25" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG25" . ToUpper ( ) ) ) [ "SENSORAVG25" . ToUpper ( ) ] ) } ,
"" SENSORAVG26 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG26" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG26" . ToUpper ( ) ) ) [ "SENSORAVG26" . ToUpper ( ) ] ) } ,
"" SENSORAVG27 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG27" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG27" . ToUpper ( ) ) ) [ "SENSORAVG27" . ToUpper ( ) ] ) } ,
"" SENSORAVG28 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG28" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG28" . ToUpper ( ) ) ) [ "SENSORAVG28" . ToUpper ( ) ] ) } ,
"" SENSORAVG29 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG29" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG29" . ToUpper ( ) ) ) [ "SENSORAVG29" . ToUpper ( ) ] ) } ,
"" SENSORAVG30 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG30" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG30" . ToUpper ( ) ) ) [ "SENSORAVG30" . ToUpper ( ) ] ) } ,
"" SENSORAVG31 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG31" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG31" . ToUpper ( ) ) ) [ "SENSORAVG31" . ToUpper ( ) ] ) } ,
"" SENSORAVG32 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG32" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG32" . ToUpper ( ) ) ) [ "SENSORAVG32" . ToUpper ( ) ] ) } ,
"" SENSORAVG33 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG33" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG33" . ToUpper ( ) ) ) [ "SENSORAVG33" . ToUpper ( ) ] ) } ,
"" SENSORAVG34 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG34" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG34" . ToUpper ( ) ) ) [ "SENSORAVG34" . ToUpper ( ) ] ) } ,
"" SENSORAVG35 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG35" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG35" . ToUpper ( ) ) ) [ "SENSORAVG35" . ToUpper ( ) ] ) } ,
"" SENSORAVG36 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG36" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG36" . ToUpper ( ) ) ) [ "SENSORAVG36" . ToUpper ( ) ] ) } ,
"" SENSORAVG37 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG37" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG37" . ToUpper ( ) ) ) [ "SENSORAVG37" . ToUpper ( ) ] ) } ,
"" SENSORAVG38 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG38" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG38" . ToUpper ( ) ) ) [ "SENSORAVG38" . ToUpper ( ) ] ) } ,
"" SENSORAVG39 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG39" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG39" . ToUpper ( ) ) ) [ "SENSORAVG39" . ToUpper ( ) ] ) } ,
"" SENSORAVG40 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG40" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG40" . ToUpper ( ) ) ) [ "SENSORAVG40" . ToUpper ( ) ] ) } ,
"" SENSORAVG41 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG41" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG41" . ToUpper ( ) ) ) [ "SENSORAVG41" . ToUpper ( ) ] ) } ,
"" SENSORAVG42 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG42" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG42" . ToUpper ( ) ) ) [ "SENSORAVG42" . ToUpper ( ) ] ) } ,
"" SENSORAVG43 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG43" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG43" . ToUpper ( ) ) ) [ "SENSORAVG43" . ToUpper ( ) ] ) } ,
"" SENSORAVG44 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG44" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG44" . ToUpper ( ) ) ) [ "SENSORAVG44" . ToUpper ( ) ] ) } ,
"" SENSORAVG45 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG45" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG45" . ToUpper ( ) ) ) [ "SENSORAVG45" . ToUpper ( ) ] ) } ,
"" SENSORAVG46 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG46" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG46" . ToUpper ( ) ) ) [ "SENSORAVG46" . ToUpper ( ) ] ) } ,
"" SENSORAVG47 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG47" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG47" . ToUpper ( ) ) ) [ "SENSORAVG47" . ToUpper ( ) ] ) } ,
"" SENSORAVG48 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG48" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG48" . ToUpper ( ) ) ) [ "SENSORAVG48" . ToUpper ( ) ] ) } ,
"" SENSORAVG49 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG49" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG49" . ToUpper ( ) ) ) [ "SENSORAVG49" . ToUpper ( ) ] ) } ,
"" SENSORAVG50 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG50" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG50" . ToUpper ( ) ) ) [ "SENSORAVG50" . ToUpper ( ) ] ) }
WHERE site_id = ' { d . site_id } ' and "" device_Id "" = ' { d . device_id } ' and "" TIMESTAMP "" = ' { d . timestamp } '
RETURNING *
)
insert into sensoravg_history_hour ( "" site_id "" , "" device_Id "" , "" TIMESTAMP "" , "" Totalsun "" , "" Temp "" , "" Wm2 "" , "" SENSORAVG04 "" , "" SENSORAVG05 "" , "" SENSORAVG06 "" , "" SENSORAVG07 "" , "" SENSORAVG08 "" ,
"" SENSORAVG09 "" , "" SENSORAVG10 "" , "" SENSORAVG11 "" , "" SENSORAVG12 "" , "" SENSORAVG13 "" , "" SENSORAVG14 "" , "" SENSORAVG15 "" , "" SENSORAVG16 "" , "" SENSORAVG17 "" ,
"" SENSORAVG18 "" , "" SENSORAVG19 "" , "" SENSORAVG20 "" , "" SENSORAVG21 "" , "" SENSORAVG22 "" , "" SENSORAVG23 "" , "" SENSORAVG24 "" , "" SENSORAVG25 "" , "" SENSORAVG26 "" ,
"" SENSORAVG27 "" , "" SENSORAVG28 "" , "" SENSORAVG29 "" , "" SENSORAVG30 "" , "" SENSORAVG31 "" , "" SENSORAVG32 "" , "" SENSORAVG33 "" , "" SENSORAVG34 "" , "" SENSORAVG35 "" ,
"" SENSORAVG36 "" , "" SENSORAVG37 "" , "" SENSORAVG38 "" , "" SENSORAVG39 "" , "" SENSORAVG40 "" , "" SENSORAVG41 "" , "" SENSORAVG42 "" , "" SENSORAVG43 "" , "" SENSORAVG44 "" ,
"" SENSORAVG45 "" , "" SENSORAVG46 "" , "" SENSORAVG47 "" , "" SENSORAVG48 "" , "" SENSORAVG49 "" , "" SENSORAVG50 "" )
select ' { d . site_id } ' , ' { d . device_id } ' , ' { d . timestamp } ' ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Totalsun" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Totalsun" . ToUpper ( ) ) ) [ "Totalsun" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Temp" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Temp" . ToUpper ( ) ) ) [ "Temp" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Wm2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Wm2" . ToUpper ( ) ) ) [ "Wm2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG04" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG04" . ToUpper ( ) ) ) [ "SENSORAVG04" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG05" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG05" . ToUpper ( ) ) ) [ "SENSORAVG05" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG06" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG06" . ToUpper ( ) ) ) [ "SENSORAVG06" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG07" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG07" . ToUpper ( ) ) ) [ "SENSORAVG07" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG08" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG08" . ToUpper ( ) ) ) [ "SENSORAVG08" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG09" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG09" . ToUpper ( ) ) ) [ "SENSORAVG09" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG10" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG10" . ToUpper ( ) ) ) [ "SENSORAVG10" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG11" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG11" . ToUpper ( ) ) ) [ "SENSORAVG11" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG12" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG12" . ToUpper ( ) ) ) [ "SENSORAVG12" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG13" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG13" . ToUpper ( ) ) ) [ "SENSORAVG13" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG14" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG14" . ToUpper ( ) ) ) [ "SENSORAVG14" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG15" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG15" . ToUpper ( ) ) ) [ "SENSORAVG15" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG16" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG16" . ToUpper ( ) ) ) [ "SENSORAVG16" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG17" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG17" . ToUpper ( ) ) ) [ "SENSORAVG17" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG18" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG18" . ToUpper ( ) ) ) [ "SENSORAVG18" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG19" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG19" . ToUpper ( ) ) ) [ "SENSORAVG19" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG20" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG20" . ToUpper ( ) ) ) [ "SENSORAVG20" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG21" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG21" . ToUpper ( ) ) ) [ "SENSORAVG21" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG22" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG22" . ToUpper ( ) ) ) [ "SENSORAVG22" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG23" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG23" . ToUpper ( ) ) ) [ "SENSORAVG23" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG24" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG24" . ToUpper ( ) ) ) [ "SENSORAVG24" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG25" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG25" . ToUpper ( ) ) ) [ "SENSORAVG25" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG26" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG26" . ToUpper ( ) ) ) [ "SENSORAVG26" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG27" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG27" . ToUpper ( ) ) ) [ "SENSORAVG27" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG28" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG28" . ToUpper ( ) ) ) [ "SENSORAVG28" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG29" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG29" . ToUpper ( ) ) ) [ "SENSORAVG29" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG30" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG30" . ToUpper ( ) ) ) [ "SENSORAVG30" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG31" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG31" . ToUpper ( ) ) ) [ "SENSORAVG31" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG32" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG32" . ToUpper ( ) ) ) [ "SENSORAVG32" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG33" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG33" . ToUpper ( ) ) ) [ "SENSORAVG33" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG34" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG34" . ToUpper ( ) ) ) [ "SENSORAVG34" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG35" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG35" . ToUpper ( ) ) ) [ "SENSORAVG35" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG36" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG36" . ToUpper ( ) ) ) [ "SENSORAVG36" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG37" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG37" . ToUpper ( ) ) ) [ "SENSORAVG37" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG38" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG38" . ToUpper ( ) ) ) [ "SENSORAVG38" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG39" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG39" . ToUpper ( ) ) ) [ "SENSORAVG39" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG40" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG40" . ToUpper ( ) ) ) [ "SENSORAVG40" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG41" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG41" . ToUpper ( ) ) ) [ "SENSORAVG41" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG42" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG42" . ToUpper ( ) ) ) [ "SENSORAVG42" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG43" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG43" . ToUpper ( ) ) ) [ "SENSORAVG43" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG44" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG44" . ToUpper ( ) ) ) [ "SENSORAVG44" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG45" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG45" . ToUpper ( ) ) ) [ "SENSORAVG45" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG46" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG46" . ToUpper ( ) ) ) [ "SENSORAVG46" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG47" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG47" . ToUpper ( ) ) ) [ "SENSORAVG47" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG48" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG48" . ToUpper ( ) ) ) [ "SENSORAVG48" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG49" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG49" . ToUpper ( ) ) ) [ "SENSORAVG49" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG50" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SENSORAVG50" . ToUpper ( ) ) ) [ "SENSORAVG50" . ToUpper ( ) ] ) }
WHERE NOT EXISTS ( SELECT 1 FROM updated_rows ) ; ";
}
else if ( t = = "meter" )
{
sql + = @ $ "WITH updated_rows AS (
UPDATE meter_history_hour
SET
"" V1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "V1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "V1" . ToUpper ( ) ) ) [ "V1" . ToUpper ( ) ] ) } ,
"" V2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "V2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "V2" . ToUpper ( ) ) ) [ "V2" . ToUpper ( ) ] ) } ,
"" V3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "V3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "V3" . ToUpper ( ) ) ) [ "V3" . ToUpper ( ) ] ) } ,
"" A1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "A1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "A1" . ToUpper ( ) ) ) [ "A1" . ToUpper ( ) ] ) } ,
"" A2 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "A2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "A2" . ToUpper ( ) ) ) [ "A2" . ToUpper ( ) ] ) } ,
"" A3 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "A3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "A3" . ToUpper ( ) ) ) [ "A3" . ToUpper ( ) ] ) } ,
"" W1 "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "W1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "W1" . ToUpper ( ) ) ) [ "W1" . ToUpper ( ) ] ) } ,
"" kwh "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "kwh" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "kwh" . ToUpper ( ) ) ) [ "kwh" . ToUpper ( ) ] ) } ,
"" PF "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "PF" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "PF" . ToUpper ( ) ) ) [ "PF" . ToUpper ( ) ] ) } ,
"" Q "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Q" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Q" . ToUpper ( ) ) ) [ "Q" . ToUpper ( ) ] ) } ,
"" Freq "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Freq" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Freq" . ToUpper ( ) ) ) [ "Freq" . ToUpper ( ) ] ) }
WHERE site_id = ' { d . site_id } ' and "" device_Id "" = ' { d . device_id } ' and "" TIMESTAMP "" = ' { d . timestamp } '
RETURNING *
)
insert into meter_history_hour ( "" site_id "" , "" device_Id "" , "" TIMESTAMP "" , "" V1 "" , "" V2 "" , "" V3 "" , "" A1 "" , "" A2 "" , "" A3 "" , "" W1 "" , "" kwh "" , "" PF "" , "" Q "" , "" Freq "" )
select ' { d . site_id } ' , ' { d . device_id } ' , ' { d . timestamp } ' ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "V1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "V1" . ToUpper ( ) ) ) [ "V1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "V2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "V2" . ToUpper ( ) ) ) [ "V2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "V3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "V3" . ToUpper ( ) ) ) [ "V3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "A1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "A1" . ToUpper ( ) ) ) [ "A1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "A2" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "A2" . ToUpper ( ) ) ) [ "A2" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "A3" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "A3" . ToUpper ( ) ) ) [ "A3" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "W1" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "W1" . ToUpper ( ) ) ) [ "W1" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "kwh" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "kwh" . ToUpper ( ) ) ) [ "kwh" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "PF" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "PF" . ToUpper ( ) ) ) [ "PF" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Q" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Q" . ToUpper ( ) ) ) [ "Q" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "Freq" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "Freq" . ToUpper ( ) ) ) [ "Freq" . ToUpper ( ) ] ) }
WHERE NOT EXISTS ( SELECT 1 FROM updated_rows ) ; ";
}
else if ( t = = "site" )
{
sql + = @ $ "WITH updated_rows AS (
UPDATE station_history_hour
SET
"" SITETYPE "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SITETYPE" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SITETYPE" . ToUpper ( ) ) ) [ "SITETYPE" . ToUpper ( ) ] ) } ,
"" KWH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) [ "KWH" . ToUpper ( ) ] ) } ,
"" TODAYKWH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) [ "TODAYKWH" . ToUpper ( ) ] ) } ,
"" TOTALKWH "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) [ "TOTALKWH" . ToUpper ( ) ] ) } ,
"" KWHKWP "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) [ "KWHKWP" . ToUpper ( ) ] ) } ,
"" PR "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) [ "PR" . ToUpper ( ) ] ) } ,
"" MP "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "MP" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "MP" . ToUpper ( ) ) ) [ "MP" . ToUpper ( ) ] ) } ,
"" SOLARHOUR "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SOLARHOUR" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SOLARHOUR" . ToUpper ( ) ) ) [ "SOLARHOUR" . ToUpper ( ) ] ) } ,
"" MONEY "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "MONEY" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "MONEY" . ToUpper ( ) ) ) [ "MONEY" . ToUpper ( ) ] ) } ,
"" TODAYMONEY "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYMONEY" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYMONEY" . ToUpper ( ) ) ) [ "TODAYMONEY" . ToUpper ( ) ] ) } ,
"" TOTALMONEY "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALMONEY" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALMONEY" . ToUpper ( ) ) ) [ "TOTALMONEY" . ToUpper ( ) ] ) } ,
"" CARBON "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "CARBON" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "CARBON" . ToUpper ( ) ) ) [ "CARBON" . ToUpper ( ) ] ) } ,
"" TODAYCARBON "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYCARBON" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYCARBON" . ToUpper ( ) ) ) [ "TODAYCARBON" . ToUpper ( ) ] ) } ,
"" TOTALCARBON "" = { ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALCARBON" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALCARBON" . ToUpper ( ) ) ) [ "TOTALCARBON" . ToUpper ( ) ] ) }
WHERE site_id = ' { d . site_id } ' and "" device_Id "" = ' { d . device_id } ' and "" TIMESTAMP "" = ' { d . timestamp } '
RETURNING *
)
insert into station_history_hour ( "" site_id "" , "" device_Id "" , "" TIMESTAMP "" , "" SITETYPE "" , "" KWH "" , "" TODAYKWH "" , "" TOTALKWH "" , "" KWHKWP "" , "" PR "" , "" MP "" , "" SOLARHOUR "" , "" MONEY "" ,
"" TODAYMONEY "" , "" TOTALMONEY "" , "" CARBON "" , "" TODAYCARBON "" , "" TOTALCARBON "" )
select ' { d . site_id } ' , ' { d . device_id } ' , ' { d . timestamp } ' ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SITETYPE" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SITETYPE" . ToUpper ( ) ) ) [ "SITETYPE" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWH" . ToUpper ( ) ) ) [ "KWH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYKWH" . ToUpper ( ) ) ) [ "TODAYKWH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALKWH" . ToUpper ( ) ) ) [ "TOTALKWH" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "KWHKWP" . ToUpper ( ) ) ) [ "KWHKWP" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "PR" . ToUpper ( ) ) ) [ "PR" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "MP" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "MP" . ToUpper ( ) ) ) [ "MP" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "SOLARHOUR" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "SOLARHOUR" . ToUpper ( ) ) ) [ "SOLARHOUR" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "MONEY" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "MONEY" . ToUpper ( ) ) ) [ "MONEY" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYMONEY" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYMONEY" . ToUpper ( ) ) ) [ "TODAYMONEY" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALMONEY" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALMONEY" . ToUpper ( ) ) ) [ "TOTALMONEY" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "CARBON" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "CARBON" . ToUpper ( ) ) ) [ "CARBON" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYCARBON" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TODAYCARBON" . ToUpper ( ) ) ) [ "TODAYCARBON" . ToUpper ( ) ] ) } ,
{ ( d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALCARBON" . ToUpper ( ) ) ) = = null ? 0 : d . value . FirstOrDefault ( x = > x . ContainsKey ( "TOTALCARBON" . ToUpper ( ) ) ) [ "TOTALCARBON" . ToUpper ( ) ] ) }
WHERE NOT EXISTS ( SELECT 1 FROM updated_rows ) ; ";
}
}
}
if ( ! string . IsNullOrWhiteSpace ( sql ) )
{
await backgroundServiceRepository . ExecuteSql ( sql , null ) ;
}
}
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【SyncData】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > SyncDevicePoint ( ILogger logger , IBackgroundServicePostgresqlRepository backgroundServiceRepository , List < device_point > data , ObixApiConfig obixApiConfig , string bql )
{
bool result = false ;
string sql = string . Empty ;
try
{
string encoded = System . Convert . ToBase64String ( System . Text . Encoding . GetEncoding ( "ISO-8859-1" ) . GetBytes ( obixApiConfig . UserName + ":" + obixApiConfig . Password ) ) ;
HttpWebRequest Postrequest = ( HttpWebRequest ) WebRequest . Create ( $"{obixApiConfig.ApiBase}obix/config/Program/ObixQuery/query/" ) ;
Postrequest . Method = "POST" ;
Postrequest . Headers . Add ( "Authorization" , "Basic " + encoded ) ;
Postrequest . PreAuthenticate = true ;
using ( var streamWriter = new StreamWriter ( Postrequest . GetRequestStream ( ) ) )
{
string json = "<str val='" + bql + "'/>" ;
streamWriter . Write ( json ) ;
}
HttpWebResponse response = ( HttpWebResponse ) Postrequest . GetResponse ( ) ;
var responseString = new StreamReader ( response . GetResponseStream ( ) ) . ReadToEnd ( ) ;
XmlDocument xmlDoc = new XmlDocument ( ) ;
xmlDoc . LoadXml ( responseString ) ;
string jsonText = JsonConvert . SerializeXmlNode ( xmlDoc ) ;
var jsonData = Welcome . FromJson ( jsonText ) ;
if ( jsonData . Obj ! = null & & jsonData . Obj . Str ! = null )
{
foreach ( var jd in jsonData . Obj . Str )
{
var value = jd . Val . Split ( "," ) ;
if ( value . Length > 0 )
{
var devicePoint = new device_point ( ) ;
devicePoint . site_id = value [ 0 ] . Split ( "/" ) [ 2 ] . Trim ( ) ;
devicePoint . device_sys_tag = value [ 0 ] . Split ( "/" ) [ 3 ] . Trim ( ) ;
devicePoint . point = value [ 0 ] . Split ( "/" ) [ value [ 0 ] . Split ( "/" ) . Length - 2 ] . Trim ( ) ;
devicePoint . device_id = value [ 0 ] . Split ( "/" ) [ value [ 0 ] . Split ( "/" ) . Length - 3 ] . Trim ( ) ;
devicePoint . history_path = value [ 0 ] . Trim ( ) ;
devicePoint . is_link = 1 ;
data . Add ( devicePoint ) ;
}
}
}
result = true ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【SyncDevicePoint】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > ImportDevicePoint ( ILogger logger , IBackgroundServicePostgresqlRepository backgroundServiceRepository , List < device_point > data )
{
bool result = false ;
try
{
string sql = string . Empty ;
#region clear data
sql = "delete from import_device_point" ;
await backgroundServiceRepository . ExecuteSql ( sql ) ;
#endregion
#region insert data
if ( data . Any ( ) )
{
sql = @ $"insert into import_device_point (device_sys_tag, point, site_id, device_id, history_path, created_at) values " ;
int index = 1 ;
foreach ( var d in data )
{
sql + = $"('{d.device_sys_tag}', '{d.point}', '{d.site_id}', '{d.device_id}', '{d.history_path}', now()){(index == data.Count ? " ; " : " , ")}" ;
index + + ;
}
await backgroundServiceRepository . ExecuteSql ( sql ) ;
}
#endregion
#region compare device data with import_device
//update
sql = @ "UPDATE device_point
SET is_link = idev . is_link
FROM (
select d . site_id , d . device_sys_tag , d . device_id , CASE WHEN idev . site_id IS NULL THEN b '0' ELSE b '1' END is_link
from device_point d
left join import_device_point idev on d . site_id = idev . site_id and d . device_sys_tag = idev . device_sys_tag and d . device_id = idev . device_id
) idev
WHERE device_point . site_id = idev . site_id
AND device_point . device_sys_tag = idev . device_sys_tag
AND device_point . device_id = idev . device_id ; ";
await backgroundServiceRepository . ExecuteSql ( sql ) ;
//insert
sql = @ "insert into device_point (device_sys_tag, point, site_id, device_id, history_path, is_link)
select idev . device_sys_tag , idev . point , idev . site_id , idev . device_id , idev . history_path , b '1'
from import_device_point idev
left join device_point d on d . site_id = idev . site_id and d . device_sys_tag = idev . device_sys_tag and d . device_id = idev . device_id
where d . site_id is null ;
update device_point set is_link = b '0' where is_link is null or is_link = ' ' ; ";
await backgroundServiceRepository . ExecuteSql ( sql ) ;
#endregion
result = true ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【ImportDevicePoint】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > SyncDataValue ( ILogger logger , List < data_value > data , ObixApiConfig obixApiConfig )
{
bool result = false ;
string [ ] bql = new string [ ] { "neql:solar:archive=\"max\" and solar:archivetime=\"minute\"" , "neql:solar:archive=\"avg\" and solar:archivetime=\"minute\"" ,
"neql:solar:archive=\"sum\" and solar:archivetime=\"minute\"" , "neql:solar:archive=\"max\" and solar:archivetime=\"hour\"" ,
"neql:solar:archive=\"avg\" and solar:archivetime=\"hour\"" , "neql:solar:archive=\"sum\" and solar:archivetime=\"hour\"" } ;
try
{
string encoded = System . Convert . ToBase64String ( System . Text . Encoding . GetEncoding ( "ISO-8859-1" ) . GetBytes ( obixApiConfig . UserName + ":" + obixApiConfig . Password ) ) ;
foreach ( var b in bql )
{
HttpWebRequest Postrequest = ( HttpWebRequest ) WebRequest . Create ( $"{obixApiConfig.ApiBase}obix/config/Program/ObixQuery/query/" ) ;
Postrequest . Method = "POST" ;
Postrequest . Headers . Add ( "Authorization" , "Basic " + encoded ) ;
Postrequest . PreAuthenticate = true ;
using ( var streamWriter = new StreamWriter ( Postrequest . GetRequestStream ( ) ) )
{
string json = "<str val='" + b + "'/>" ;
streamWriter . Write ( json ) ;
}
HttpWebResponse response = ( HttpWebResponse ) Postrequest . GetResponse ( ) ;
var responseString = new StreamReader ( response . GetResponseStream ( ) ) . ReadToEnd ( ) ;
XmlDocument xmlDoc = new XmlDocument ( ) ;
xmlDoc . LoadXml ( responseString ) ;
string jsonText = JsonConvert . SerializeXmlNode ( xmlDoc ) ;
var jsonData = Welcome . FromJson ( jsonText ) ;
if ( jsonData . Obj ! = null & & jsonData . Obj . Str ! = null )
{
foreach ( var jd in jsonData . Obj . Str )
{
var value = jd . Val . Split ( "," ) ;
if ( value . Length > 0 )
{
var devicePoint = new data_value ( ) ;
devicePoint . site_id = value [ 0 ] . Split ( "/" ) [ 2 ] . Trim ( ) ;
devicePoint . device_sys_tag = value [ 0 ] . Split ( "/" ) [ 3 ] . Trim ( ) ;
devicePoint . point = value [ 0 ] . Split ( "/" ) [ value [ 0 ] . Split ( "/" ) . Length - 2 ] . Trim ( ) ;
devicePoint . device_id = value [ 0 ] . Split ( "/" ) [ value [ 0 ] . Split ( "/" ) . Length - 3 ] . Trim ( ) ;
devicePoint . value_unit = b . Contains ( "\"max\"" ) ? "max"
: b . Contains ( "\"avg\"" ) ? "avg"
: b . Contains ( "\"sum\"" ) ? "sum"
: "" ;
data . Add ( devicePoint ) ;
}
}
}
}
result = true ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【SyncDataValue】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > SyncSiteInfo ( ILogger logger , List < site > data , ObixApiConfig obixApiConfig )
{
bool result = false ;
try
{
string encoded = System . Convert . ToBase64String ( System . Text . Encoding . GetEncoding ( "ISO-8859-1" ) . GetBytes ( obixApiConfig . UserName + ":" + obixApiConfig . Password ) ) ;
HttpWebRequest Postrequest = ( HttpWebRequest ) WebRequest . Create ( $"{obixApiConfig.ApiBase}obix/config/Program/ObixQuery/query/" ) ;
Postrequest . Method = "POST" ;
Postrequest . Headers . Add ( "Authorization" , "Basic " + encoded ) ;
Postrequest . PreAuthenticate = true ;
using ( var streamWriter = new StreamWriter ( Postrequest . GetRequestStream ( ) ) )
{
string json = "<str val='neql:solar:id and n:type=\"baja:Folder\"'/>" ;
streamWriter . Write ( json ) ;
}
HttpWebResponse response = ( HttpWebResponse ) Postrequest . GetResponse ( ) ;
var responseString = new StreamReader ( response . GetResponseStream ( ) ) . ReadToEnd ( ) ;
XmlDocument xmlDoc = new XmlDocument ( ) ;
xmlDoc . LoadXml ( responseString ) ;
string jsonText = JsonConvert . SerializeXmlNode ( xmlDoc ) ;
var jsonData = Welcome . FromJson ( jsonText ) ;
if ( jsonData . Obj ! = null & & jsonData . Obj . Str ! = null )
{
foreach ( var jd in jsonData . Obj . Str )
{
var value = jd . Val . Split ( "," ) ;
if ( value . Length > 0 )
{
var device = new site ( ) ;
device . site_id = value [ 0 ] . Split ( "/" ) [ 2 ] . Trim ( ) ;
device . site_name = HttpUtility . HtmlDecode ( value [ 1 ] . Split ( "=" ) [ 1 ] ? . Trim ( ) ? ? "" ) ;
data . Add ( device ) ;
}
}
}
result = true ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【SyncSiteInfo】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
public async Task < bool > CheckLastData ( ILogger logger , IBackgroundServicePostgresqlRepository backgroundServiceRepository , ObixApiConfig obixApiConfig ,
List < device > deviceData , List < device_point > devicePointData , string startTimeStamp , string endTimeStamp , string intervalValue ,
List < data_value > dataValue )
{
bool result = true ;
string sql = string . Empty ;
string path = string . Empty ;
try
{
XmlDocument xmlDocument = new XmlDocument ( ) ;
sql = "select system_value from variable where deleted = 0 and system_type = 'obixConfig' and system_key = 'niagara status'" ;
string station = await backgroundServiceRepository . GetOneAsync < string > ( sql ) ;
string encoded = System . Convert . ToBase64String ( System . Text . Encoding . GetEncoding ( "ISO-8859-1" ) . GetBytes ( obixApiConfig . UserName + ":" + obixApiConfig . Password ) ) ;
var data = new List < data > ( ) ;
var historyQueryFilter = $ @ "<obj is='obix: HistoryFilter'>
< abstime name = ' start ' val = ' { startTimeStamp } ' / >
< abstime name = ' end ' val = ' { endTimeStamp } ' / >
< reltime name = ' interval ' val = ' { intervalValue } ' / >
< / obj > ";
foreach ( var dd in deviceData )
{
result = true ;
#region main process
foreach ( var dpd in devicePointData . Where ( x = > x . site_id = = dd . site_id & & x . device_sys_tag = = dd . device_sys_tag & & x . device_id = = dd . device_id ) )
{
path = dd . site_id + "_" + dd . device_sys_tag + ( dd . device_id = = dd . device_sys_tag ? "" : "_" + dd . device_id ) + "_" + dpd . point ;
HttpWebRequest archiveHourRequest = ( HttpWebRequest ) WebRequest . Create ( $"{obixApiConfig.ApiBase}obix/histories/{station}/{path}/~historyRollup/" ) ;
archiveHourRequest . Method = "POST" ;
archiveHourRequest . Headers . Add ( "Authorization" , "Basic " + encoded ) ;
archiveHourRequest . PreAuthenticate = true ;
byte [ ] byteArray = Encoding . UTF8 . GetBytes ( historyQueryFilter ) ;
using ( Stream reqStream = archiveHourRequest . GetRequestStream ( ) )
{
reqStream . Write ( byteArray , 0 , byteArray . Length ) ;
}
HttpWebResponse archiveHourResponse = ( HttpWebResponse ) archiveHourRequest . GetResponse ( ) ;
var archiveHourResponseContent = new StreamReader ( archiveHourResponse . GetResponseStream ( ) ) . ReadToEnd ( ) ;
xmlDocument . LoadXml ( archiveHourResponseContent ) ;
string archiveHourJson = JsonConvert . SerializeXmlNode ( xmlDocument ) ;
JObject jsonResult = ( JObject ) JsonConvert . DeserializeObject ( archiveHourJson ) ;
if ( jsonResult . ContainsKey ( "err" ) ) //抓取錯誤
{
result = false ;
}
else
{
var rawdateCount = Convert . ToInt32 ( jsonResult [ "obj" ] [ "int" ] [ "@val" ] . ToString ( ) ) ;
var histories = jsonResult [ "obj" ] [ "list" ] [ "obj" ] ;
if ( rawdateCount > 1 )
{
foreach ( var history in histories )
{
var d = new data ( ) ;
d . site_id = dd . site_id ;
d . device_id = dd . device_id ;
d . device_sys_tag = dd . device_sys_tag ;
d . value = new List < Dictionary < string , decimal > > ( ) ;
var realValue = new Dictionary < string , decimal > ( ) ;
if ( history [ "real" ] ! = null & & history [ "real" ] . HasValues )
{
foreach ( var real in history [ "real" ] )
{
var name = real [ "@name" ] . ToString ( ) ;
if ( name = = ( dataValue . Where ( x = > x . site_id = = dd . site_id & & x . device_id = = dd . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . point = = dpd . point ) . FirstOrDefault ( ) ? . value_unit ? ? "" ) )
{
if ( result )
result = Decimal . Parse ( real [ "@val" ] . ToString ( ) , System . Globalization . NumberStyles . Float ) > 0 ;
}
}
}
}
}
else
{
var d = new data ( ) ;
d . site_id = dd . site_id ;
d . device_id = dd . device_id ;
d . device_sys_tag = dd . device_sys_tag ;
d . value = new List < Dictionary < string , decimal > > ( ) ;
var realValue = new Dictionary < string , decimal > ( ) ;
if ( histories [ "real" ] ! = null & & histories [ "real" ] . HasValues )
{
foreach ( var real in histories [ "real" ] )
{
var name = real [ "@name" ] . ToString ( ) ;
if ( name = = ( dataValue . Where ( x = > x . site_id = = dd . site_id & & x . device_id = = dd . device_id & & x . device_sys_tag = = dd . device_sys_tag & & x . point = = dpd . point ) . FirstOrDefault ( ) ? . value_unit ? ? "" ) )
{
if ( result )
result = Decimal . Parse ( real [ "@val" ] . ToString ( ) , System . Globalization . NumberStyles . Float ) > 0 ;
}
}
}
}
}
}
#endregion
if ( ! result )
{
deviceData . FirstOrDefault ( x = > x = = dd ) . isSuccess = result ;
}
}
deviceData . RemoveAll ( x = > ! x . isSuccess ) ;
}
catch ( Exception ex )
{
result = false ;
logger . LogError ( $"【ArchiveSolarHourJob】【SolarService】【CheckLastData】[Exception]: {ex.ToString()}" ) ;
}
return result ;
}
}
}