// File: /workspace/github_cpp_makefile_star2_O2/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 0xa7e3(%rip), %rsi      # 0x2d4f6
leaq 0x100(%rsp), %r14
movq %r14, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2050
pushq $0x3b
popq %rsi
movq %r14, %rdi
callq 0x21080
testb %al, %al
je 0x22d84
leaq 0x240(%rsp), %rax
cvtss2sd 0x5e0(%rax), %xmm0
cvtss2sd 0x5e4(%rax), %xmm1
movslq %ebp, %rcx
imulq $0x14, %rcx, %rcx
leaq (%rax,%rcx), %rdx
addq $0x664, %rdx            # imm = 0x664
leaq 0xa79f(%rip), %rsi      # 0x2d506
movq %rsp, %r14
leaq 0x100(%rsp), %rcx
movq %r14, %rdi
movb $0x2, %al
callq 0x2050
movq %r14, %rdi
callq 0x2210
leaq 0xa7d1(%rip), %rsi      # 0x2d55c
xorl %ebp, %ebp
movq %rsp, %r14
leaq 0x100(%rsp), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
callq 0x2210
leaq 0xa7d2(%rip), %rdi      # 0x2d583
leaq 0xaaa0(%rip), %rsi      # 0x2d858
callq 0x22f0
testq %rax, %rax
je 0x22ebf
movq %rax, %r14
movq %rsp, %r15
leaq 0x100(%rsp), %r12
movq %r15, %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdx
callq 0x2470
testq %rax, %rax
je 0x22ebf
movq %r15, %rdi
callq 0x2100
cmpq $0xb, %rax
jb 0x22dd4
movq %rax, %r13
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
xorl %esi, %esi
callq 0x2150
movslq %r13d, %rax
xorl %edx, %edx
xorl %ecx, %ecx
cmpq %rax, %rdx
jge 0x22e30
testq %rcx, %rcx
jne 0x22e30
cmpb $0x22, (%rsp,%rdx)
movl $0x0, %ecx
cmoveq %rdx, %rcx
incq %rdx
jmp 0x22e14
leaq 0x1(%rcx), %rsi
movslq %esi, %rdi
xorl %edx, %edx
testl %edx, %edx
jne 0x22e53
cmpq %rax, %rdi
jge 0x22e53
cmpb $0x22, (%rsp,%rdi)
movl $0x0, %edx
cmovel %edi, %edx
incq %rdi
jmp 0x22e39
notl %ecx
addl %ecx, %edx
cmpl $0x100, %edx            # imm = 0x100
jae 0x22e6c
movl %edx, %edx
addq %rsp, %rsi
movq %r12, %rdi
callq 0x21f0
movq %r12, %rdi
callq 0x2300
movq %rax, %rcx
movq (%rbx), %rdi
movq 0x8(%rbx), %rax
subq %rdi, %rax
cqto
pushq $0x18
popq %rsi
idivq %rsi
movslq %ebp, %rsi
imulq $0x18, %rsi, %rdx
addq %rdi, %rdx
movq (%rdx), %r8
leaq 0x1(%rsi), %rdi
cmpq %rcx, %r8
jge 0x22eab
addq $0x18, %rdx
cmpq %rsi, %rax
movq %rdi, %rsi
ja 0x22e93
xorl %eax, %eax
cmpq %rcx, %r8
sete %al
leaq (%rax,%rdi), %rbp
decq %rbp
jmp 0x22dd4
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq