// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movl %esi, %ebp
movq %rdi, %rbx
leaq 0xbe11(%rip), %rsi      # 0x326ce
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
movl $0x3b, %esi
callq 0x249c0
testb %al, %al
je 0x2692d
leaq 0x240(%rsp), %rax
cvtss2sd 0x5e0(%rax), %xmm0
cvtss2sd 0x5e4(%rax), %xmm1
movslq %ebp, %rcx
leaq (%rcx,%rcx,4), %rcx
leaq 0x664(%rax,%rcx,4), %rdx
leaq 0xbdce(%rip), %rsi      # 0x326de
movq %rsp, %r14
leaq 0x100(%rsp), %rcx
movq %r14, %rdi
movb $0x2, %al
callq 0x2050
movq %r14, %rdi
callq 0x2210
leaq 0xbe00(%rip), %rsi      # 0x32734
movq %rsp, %r14
leaq 0x100(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x2210
leaq 0xbe03(%rip), %rdi      # 0x3275b
leaq 0xc0d1(%rip), %rsi      # 0x32a30
callq 0x22e0
testq %rax, %rax
je 0x26ac9
movq %rax, %r14
movq %rsp, %rdi
movl $0x100, %esi            # imm = 0x100
movq %rax, %rdx
callq 0x2440
testq %rax, %rax
je 0x26ac9
xorl %ebp, %ebp
movq %rsp, %r15
leaq 0x100(%rsp), %r12
movq %r15, %rdi
callq 0x2100
cmpq $0xb, %rax
jb 0x26ab0
movq %rax, %r13
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
xorl %esi, %esi
callq 0x2140
testl %r13d, %r13d
jle 0x269e4
movl %r13d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorl %edx, %edx
cmpb $0x22, (%rsp,%rdx)
movq %rdx, %rax
je 0x269d5
xorl %eax, %eax
incq %rdx
cmpq %rcx, %rdx
jae 0x269e6
testq %rax, %rax
je 0x269ca
jmp 0x269e6
xorl %eax, %eax
leaq 0x1(%rax), %rcx
xorl %edx, %edx
cmpl %r13d, %ecx
jge 0x26a1c
movslq %ecx, %rdi
movslq %r13d, %rsi
incq %rdi
movl %ecx, %r8d
cmpb $0x22, -0x1(%rsp,%rdi)
movl %r8d, %edx
je 0x26a09
xorl %edx, %edx
testl %edx, %edx
jne 0x26a1c
leaq 0x1(%rdi), %r9
incl %r8d
cmpq %rsi, %rdi
movq %r9, %rdi
jl 0x269fd
notl %eax
addl %edx, %eax
cmpl $0x100, %eax            # imm = 0x100
jae 0x26a35
movl %eax, %edx
leaq (%rsp,%rcx), %rsi
movq %r12, %rdi
callq 0x21f0
movq %r12, %rdi
callq 0x22f0
movq (%rbx), %rsi
movslq %ebp, %rdx
leaq (%rdx,%rdx,2), %r8
movq (%rsi,%r8,8), %rcx
cmpq %rax, %rcx
jge 0x26aa4
movq 0x8(%rbx), %rbp
subq %rsi, %rbp
sarq $0x3, %rbp
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdi, %rbp
cmpq %rdx, %rbp
cmovbeq %rdx, %rbp
jbe 0x26aa4
incq %rdx
movq %rbp, %rdi
negq %rdi
leaq (%rsi,%r8,8), %rsi
addq $0x18, %rsi
movq (%rsi), %rcx
cmpq %rax, %rcx
jge 0x26aa1
leaq (%rdi,%rdx), %r8
incq %r8
incq %rdx
addq $0x18, %rsi
cmpq $0x1, %r8
jne 0x26a83
jmp 0x26aa4
movq %rdx, %rbp
xorl %edx, %edx
cmpq %rax, %rcx
sete %dl
addl %ebp, %edx
movl %edx, %ebp
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2440
testq %rax, %rax
jne 0x26996
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq