// File: Eba-M[P]E3DC-Control/awattar.cpp
// Function: forecast(std::vector<watt_s, std::allocator<watt_s>>&, e3dc_config_t, int)
void forecast(std::vector<watt_s> &w, e3dc_config_t e3dc_config,int anlage)


{
    FILE * fp;
    char line[256];
    int x3,x2;
    char value[256];
    char var[256];
    sprintf(var,"forecast%i.json",anlage);
    if (Checkfile(var,59)){
        sprintf(line,"curl -s -X GET 'https://api.forecast.solar/estimate/%f/%f/%s?time=seconds'| jq . > %s",e3dc_config.hoehe,e3dc_config.laenge,e3dc_config.Forecast[anlage],var);
        
        int res = system(line);
    }
    sprintf(line,"jq .result %s| jq .watt_hours_period > forecast.json",var);

     int res = system(line);

    
    
    fp = fopen("forecast.json","r");
    if(fp)
    {
        int w1 = 0;
        while (fgets(line, sizeof(line), fp))
        {
            long x1 = 0;
            int x2,x3;
            int len = strlen(line);
            if (strlen(line) > 10)
            {
                memset(var, 0, sizeof(var));
                memset(value, 0, sizeof(value));
                for(int j = 0;j<len&&x1==0;j++)
                {
                    if (line[j]=='"') x1=j;
                }
                x2=0;
                for(int j=x1+1;j<len&&x2==0;j++)
                {
                    if (line[j]=='"') x2=j;
                }
                x3 = x2-x1-1;
                if (x3 < sizeof(line))
                memcpy(&var,&line[x1+1],x3);
                x1=0;
                for(int j=x2;j<len&&x1==0;j++)
                {
                    if (line[j]==':') x1=j;
                }
                x3=0;
                for(int j=x1;j<len&&x3==0;j++)
                {
                    if (line[j]==','||line[j]=='\n') x3=j;
                }
                memcpy(&value,&line[x1+1],x3-x1-1);
                
                x1 = atol(var);
                x2 = atoi(value);
                {
                    time_t time = w[w1].hh;
                    while (w[w1].hh<x1&&w1<w.size())
                        w1++;
                    
                    
                    if (w[w1].hh == x1)
                    {
// den PV-Ertrag immer der vorhergehende Stunde zuordnen
                        if (w1>0)
                        {
                            int x2 = x1%(24*3600)/60;
                            int x3 = x2 - sunriseAt;
                            float pv = atof(value);
                            float pv2 = wetter[w1-1].solar;
                            if (x3>0&&x3<=60)
                            {
                                if (x3 <=30)
                                    pv = pv/10; // die ersten 30min 10%
                                else;
                                if (x3 <=60)
                                {
                                    pv2 = pv / x3;
                                    pv = pv2 * 3 + pv2 * (x3-30)/2;
                                }
                            }
                            if (x3>60&&x3<=120)
                            {
                                x3 = 120-x3;
                                pv2 = pv/60; // die ersten 30min 10%
                                if (x3>30)
                                {
                                    pv = (x3-30)*pv2/10;
                                    pv = pv + 30*pv2/2;
                                }
                                if (x3<=30)
                                    pv = pv = x3*pv2/2;
                                pv = pv + (60 - x3)*pv2;
                            }

                            pv = pv/e3dc_config.speichergroesse/10;
/*
                            if (anlage == 0)
                                w[w1-1].solar = pv;
                            else
                                w[w1-1].solar = w[w1-1].solar + pv;
*/
                        }
                            w1++;
                    }
                }
            }
        }
    };
}

// Assembly:
subq $0x398, %rsp            # imm = 0x398
leaq 0x3a0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %rdi, 0x390(%rsp)
movl %esi, 0x38c(%rsp)
leaq 0x70(%rsp), %rdi
movl 0x38c(%rsp), %edx
leaq 0x1ab0a(%rip), %rsi     # 0x623b0
movb $0x0, %al
callq 0x2060
leaq 0x70(%rsp), %rdi
movl $0x3b, %esi
callq 0x436f0
testb $0x1, %al
jne 0x478c2
jmp 0x47921
movq 0x18(%rsp), %rdx
leaq 0x280(%rsp), %rdi
movss 0x5e0(%rdx), %xmm0
cvtss2sd %xmm0, %xmm0
movss 0x5e4(%rdx), %xmm1
cvtss2sd %xmm1, %xmm1
addq $0x664, %rdx            # imm = 0x664
movslq 0x38c(%rsp), %rax
imulq $0x14, %rax, %rax
addq %rax, %rdx
leaq 0x70(%rsp), %rcx
leaq 0x1aab7(%rip), %rsi     # 0x623c0
movb $0x2, %al
callq 0x2060
leaq 0x280(%rsp), %rdi
callq 0x2240
movl %eax, 0x6c(%rsp)
leaq 0x280(%rsp), %rdi
leaq 0x70(%rsp), %rdx
leaq 0x1aae1(%rip), %rsi     # 0x62416
movb $0x0, %al
callq 0x2060
leaq 0x280(%rsp), %rdi
callq 0x2240
movl %eax, 0x68(%rsp)
leaq 0x1aae9(%rip), %rdi     # 0x6243d
leaq 0x18ea8(%rip), %rsi     # 0x60803
callq 0x2300
movq %rax, 0x380(%rsp)
cmpq $0x0, 0x380(%rsp)
je 0x47ed5
movl $0x0, 0x64(%rsp)
leaq 0x280(%rsp), %rdi
movq 0x380(%rsp), %rdx
movl $0x100, %esi            # imm = 0x100
callq 0x2450
cmpq $0x0, %rax
je 0x47ed3
movq $0x0, 0x58(%rsp)
leaq 0x280(%rsp), %rdi
callq 0x2130
movl %eax, 0x4c(%rsp)
leaq 0x280(%rsp), %rdi
callq 0x2130
cmpq $0xa, %rax
jbe 0x47ece
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2170
leaq 0x170(%rsp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2170
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %ecx
xorl %eax, %eax
cmpl 0x4c(%rsp), %ecx
movb %al, 0x17(%rsp)
jge 0x47a1e
cmpq $0x0, 0x58(%rsp)
sete %al
movb %al, 0x17(%rsp)
movb 0x17(%rsp), %al
testb $0x1, %al
jne 0x47a28
jmp 0x47a53
movslq 0x48(%rsp), %rax
movsbl 0x280(%rsp,%rax), %eax
cmpl $0x22, %eax
jne 0x47a44
movslq 0x48(%rsp), %rax
movq %rax, 0x58(%rsp)
jmp 0x47a46
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x47a01
movl $0x0, 0x54(%rsp)
movq 0x58(%rsp), %rax
addq $0x1, %rax
movl %eax, 0x44(%rsp)
movl 0x44(%rsp), %ecx
xorl %eax, %eax
cmpl 0x4c(%rsp), %ecx
movb %al, 0x16(%rsp)
jge 0x47a84
cmpl $0x0, 0x54(%rsp)
sete %al
movb %al, 0x16(%rsp)
movb 0x16(%rsp), %al
testb $0x1, %al
jne 0x47a8e
jmp 0x47ab7
movslq 0x44(%rsp), %rax
movsbl 0x280(%rsp,%rax), %eax
cmpl $0x22, %eax
jne 0x47aa8
movl 0x44(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x47aaa
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x47a68
movslq 0x54(%rsp), %rax
subq 0x58(%rsp), %rax
subq $0x1, %rax
movl %eax, 0x50(%rsp)
movslq 0x50(%rsp), %rax
cmpq $0x100, %rax            # imm = 0x100
jae 0x47af9
movq 0x58(%rsp), %rax
addq $0x1, %rax
leaq 0x280(%rsp), %rsi
addq %rax, %rsi
movslq 0x50(%rsp), %rdx
leaq 0x70(%rsp), %rdi
callq 0x2220
movq $0x0, 0x58(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %ecx
xorl %eax, %eax
cmpl 0x4c(%rsp), %ecx
movb %al, 0x15(%rsp)
jge 0x47b27
cmpq $0x0, 0x58(%rsp)
sete %al
movb %al, 0x15(%rsp)
movb 0x15(%rsp), %al
testb $0x1, %al
jne 0x47b31
jmp 0x47b5c
movslq 0x40(%rsp), %rax
movsbl 0x280(%rsp,%rax), %eax
cmpl $0x3a, %eax
jne 0x47b4d
movslq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
jmp 0x47b4f
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x47b0a
movl $0x0, 0x50(%rsp)
movq 0x58(%rsp), %rax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %ecx
xorl %eax, %eax
cmpl 0x4c(%rsp), %ecx
movb %al, 0x14(%rsp)
jge 0x47b89
cmpl $0x0, 0x50(%rsp)
sete %al
movb %al, 0x14(%rsp)
movb 0x14(%rsp), %al
testb $0x1, %al
jne 0x47b93
jmp 0x47bce
movslq 0x3c(%rsp), %rax
movsbl 0x280(%rsp,%rax), %eax
cmpl $0x2c, %eax
je 0x47bb7
movslq 0x3c(%rsp), %rax
movsbl 0x280(%rsp,%rax), %eax
cmpl $0xa, %eax
jne 0x47bbf
movl 0x3c(%rsp), %eax
movl %eax, 0x50(%rsp)
jmp 0x47bc1
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x47b6d
movq 0x58(%rsp), %rax
addq $0x1, %rax
leaq 0x280(%rsp), %rsi
addq %rax, %rsi
movslq 0x50(%rsp), %rdx
subq 0x58(%rsp), %rdx
subq $0x1, %rdx
leaq 0x170(%rsp), %rdi
callq 0x2220
leaq 0x70(%rsp), %rdi
callq 0x2310
movq %rax, 0x58(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x2430
movl %eax, 0x54(%rsp)
movq 0x390(%rsp), %rdi
movslq 0x64(%rsp), %rsi
callq 0x1ce20
movq (%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x390(%rsp), %rdi
movslq 0x64(%rsp), %rsi
callq 0x1ce20
movq (%rax), %rcx
xorl %eax, %eax
cmpq 0x58(%rsp), %rcx
movb %al, 0x13(%rsp)
jge 0x47c82
movslq 0x64(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x390(%rsp), %rdi
callq 0x1ce80
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
setb %al
movb %al, 0x13(%rsp)
movb 0x13(%rsp), %al
testb $0x1, %al
jne 0x47c8c
jmp 0x47c99
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x47c37
movq 0x390(%rsp), %rdi
movslq 0x64(%rsp), %rsi
callq 0x1ce20
movq (%rax), %rax
cmpq 0x58(%rsp), %rax
jne 0x47ecc
cmpl $0x0, 0x64(%rsp)
jle 0x47ec1
movq 0x58(%rsp), %rax
movl $0x15180, %ecx          # imm = 0x15180
cqto
idivq %rcx
movq %rdx, %rax
movl $0x3c, %ecx
cqto
idivq %rcx
movl %eax, 0x2c(%rsp)
movl 0x2c(%rsp), %eax
subl 0x324c2(%rip), %eax     # 0x7a1b0
movl %eax, 0x28(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x2330
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rsp)
movl 0x64(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
leaq 0x323ee(%rip), %rdi     # 0x7a108
callq 0x1cca0
movss 0x1c(%rax), %xmm0
movss %xmm0, 0x20(%rsp)
cmpl $0x0, 0x28(%rsp)
jle 0x47dbb
cmpl $0x3c, 0x28(%rsp)
jg 0x47dbb
cmpl $0x1e, 0x28(%rsp)
jg 0x47d5d
movss 0x24(%rsp), %xmm0
movss 0x182d3(%rip), %xmm1    # 0x60024
divss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
jmp 0x47d5f
jmp 0x47d5f
cmpl $0x3c, 0x28(%rsp)
jg 0x47db9
movss 0x24(%rsp), %xmm0
cvtsi2ssl 0x28(%rsp), %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
movss 0x20(%rsp), %xmm0
movss 0x20(%rsp), %xmm1
movl 0x28(%rsp), %eax
subl $0x1e, %eax
cvtsi2ss %eax, %xmm2
mulss %xmm2, %xmm1
movss 0x18265(%rip), %xmm2    # 0x60004
divss %xmm2, %xmm1
movss 0x182c5(%rip), %xmm2    # 0x60070
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
jmp 0x47dbb
cmpl $0x3c, 0x28(%rsp)
jle 0x47e9c
cmpl $0x78, 0x28(%rsp)
jg 0x47e9c
movl $0x78, %eax
subl 0x28(%rsp), %eax
movl %eax, 0x28(%rsp)
movss 0x24(%rsp), %xmm0
movss 0x182a4(%rip), %xmm1    # 0x60090
divss %xmm1, %xmm0
movss %xmm0, 0x20(%rsp)
cmpl $0x1e, 0x28(%rsp)
jle 0x47e4a
movl 0x28(%rsp), %eax
subl $0x1e, %eax
cvtsi2ss %eax, %xmm0
mulss 0x20(%rsp), %xmm0
movss 0x1820e(%rip), %xmm1    # 0x60024
divss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
movss 0x24(%rsp), %xmm0
movss 0x18202(%rip), %xmm1    # 0x60030
mulss 0x20(%rsp), %xmm1
movss 0x181c8(%rip), %xmm2    # 0x60004
divss %xmm2, %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
cmpl $0x1e, 0x28(%rsp)
jg 0x47e75
cvtsi2ssl 0x28(%rsp), %xmm0
mulss 0x20(%rsp), %xmm0
movss 0x1819f(%rip), %xmm1    # 0x60004
divss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
movss %xmm0, 0x24(%rsp)
movss 0x24(%rsp), %xmm1
movl $0x3c, %eax
subl 0x28(%rsp), %eax
cvtsi2ss %eax, %xmm0
movss 0x20(%rsp), %xmm2
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
movq 0x18(%rsp), %rax
movss 0x24(%rsp), %xmm0
divss 0x5b0(%rax), %xmm0
movss 0x1816d(%rip), %xmm1    # 0x60024
divss %xmm1, %xmm0
movss %xmm0, 0x24(%rsp)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x47ece
jmp 0x4797f
jmp 0x47ed5
addq $0x398, %rsp            # imm = 0x398
retq
nopl (%rax)