// File: /workspace/github_cpp_makefile_star2_O2/Eba-M[P]E3DC-Control/RscpExampleMain.cpp
// Function: shellyem_get(int&, long&)
int shellyem_get(int &power,int64_t &total){
    FILE *fp;
    char line[256];
    char EM[] = "EM";
    char EMData[] = "EMData";
    char Data[20];
    int WP_status,status;
    char path[4096];
    static time_t shellytimer = 0;
    memset(line,0,sizeof(line));
    memset(path,0,sizeof(path));
    int x2=0;
    if  (t - shellytimer >= 60)
    {
        x2 = 1;
        shellytimer = t;
    }
    else
        x2=0;

    
    
//    if (shellytimer+10<t&&strcmp(e3dc_config.shellyEM_ip,"0.0.0.0")!=0)
    if (strcmp(e3dc_config.shellyEM_ip,"0.0.0.0")!=0)
    for(int x1=0;x1<=x2;x1++)
        
    {
            fp = NULL;
            memset(Data,0,sizeof(Data));

            if (x1 == 0) strcpy(Data, EM);
            if (x1 == 1) strcpy(Data, EMData);

            sprintf(line,"curl -X POST -s -d  '{\"id\":1,\"method\":\"%s.GetStatus\",\"params\":{\"id\":0}}' http://%s/rpc",Data,e3dc_config.shellyEM_ip);
//            system(line);

            fp = popen(line, "r");

//        fp = fopen("shellyem1.txt","r");
        
            const cJSON *item = NULL;
            const cJSON *item1 = NULL;
            const cJSON *item2 = NULL;
            if (fp != NULL)
                if (fgets(path, sizeof(path), fp) != NULL)
                    
                {
//                    printf("\n%s\n",path);
                if (e3dc_config.debug)
                printf("%s\n",path);
                    
                    std::string feld;
//                    cJSON *wolf_json = cJSON_Parse(path);
                    cJSON *wolf_json = cJSON_ParseWithLength(path,sizeof(path));
                    feld = "result";
                    char * c = &feld[0];
                    item = cJSON_GetObjectItemCaseSensitive(wolf_json, c );
                    feld = "total_act_power";
                    char * d = &feld[0];
                    item1 = cJSON_GetObjectItemCaseSensitive(item, d );
//                    char * d = &feld[0];
                    feld = "total_act";
                    item2 = cJSON_GetObjectItemCaseSensitive(item, d );
                }
            status = pclose(fp);
            
        if (item1!=NULL)
        {
            power = item1->valueint;
            if (e3dc_config.debug)
                printf("power %i\n",power);
        }
        if (item2!=NULL)
        {
            total = item2->valuedouble*3600;
            if (e3dc_config.debug)
                printf("total %2.2f\n",item2->valuedouble);
            if (e3dc_config.debug)
                printf("total Ws %lld\n",total);

        }
    }
/*    power = 550;
    if (total<0)
        total = 844441200;
    else
        total = total + power;
*/
    return(-2);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1158, %rsp           # imm = 0x1158
movq %rsi, 0x8(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x2150
leaq 0x150(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x2150
movq 0x30c37(%rip), %rax     # 0x37bd8
movq %rax, %rcx
subq 0x31f7d(%rip), %rcx     # 0x38f28
cmpq $0x3c, %rcx
jl 0x6fbc
movq %rax, 0x31f70(%rip)     # 0x38f28
pushq $0x2
jmp 0x6fbe
pushq $0x1
popq %rax
movl %eax, 0x4(%rsp)
movabsq $0x302e302e302e30, %rax # imm = 0x302E302E302E30
cmpq %rax, 0x2fa3e(%rip)     # 0x36a12
je 0x71c4
leaq 0x50(%rsp), %r13
leaq 0x150(%rsp), %rbp
xorl %ebx, %ebx
cmpl %ebx, 0x4(%rsp)
je 0x71c4
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andl $0x0, 0x20(%rsp)
cmpl $0x1, %ebx
je 0x7019
testl %ebx, %ebx
jne 0x7029
movb $0x0, 0x12(%rsp)
movw $0x4d45, 0x10(%rsp)     # imm = 0x4D45
jmp 0x7029
movl $0x617461, 0x13(%rsp)   # imm = 0x617461
movl $0x61444d45, 0x10(%rsp) # imm = 0x61444D45
movq %r13, %rdi
leaq 0x24d94(%rip), %rsi     # 0x2bdc7
leaq 0x10(%rsp), %rdx
leaq 0x2f9d3(%rip), %rcx     # 0x36a12
xorl %eax, %eax
callq 0x2050
movq %r13, %rdi
leaq 0x26808(%rip), %rsi     # 0x2d858
callq 0x20a0
movq %rax, %r15
testq %rax, %rax
je 0x712b
movq %rbp, %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r15, %rdx
callq 0x2470
testq %rax, %rax
je 0x712b
cmpb $0x1, 0x2fdc4(%rip)     # 0x36e45
jne 0x708b
movq %rbp, %rdi
callq 0x2440
leaq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
movl $0x1000, %esi           # imm = 0x1000
movq %rbp, %rdi
callq 0x28722
movq %rax, %r14
leaq 0x28(%rsp), %rdi
leaq 0x24d62(%rip), %rsi     # 0x2be1e
callq 0x2530
movq %rbp, %r13
movq 0x28(%rsp), %rsi
movq %r14, %rdi
callq 0x28e9b
movq %rax, %r12
leaq 0x28(%rsp), %rdi
leaq 0x24d45(%rip), %rsi     # 0x2be25
callq 0x2530
movq 0x28(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x28e9b
movq %rax, %r14
leaq 0x28(%rsp), %rdi
leaq 0x24d31(%rip), %rsi     # 0x2be35
callq 0x2530
movq %r12, %rdi
movq %rbp, %rsi
callq 0x28e9b
movq %rax, %r12
leaq 0x28(%rsp), %rdi
callq 0x2578
movq %r13, %rbp
leaq 0x50(%rsp), %r13
jmp 0x7131
xorl %r14d, %r14d
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x23e0
testq %r14, %r14
je 0x7160
movl 0x28(%r14), %esi
movq 0x48(%rsp), %rax
movl %esi, (%rax)
cmpb $0x1, 0x2fcf5(%rip)     # 0x36e45
jne 0x7160
leaq 0x24ce6(%rip), %rdi     # 0x2be3f
xorl %eax, %eax
callq 0x2040
testq %r12, %r12
je 0x71bd
movsd 0x30(%r12), %xmm0
movapd %xmm0, %xmm1
mulsd 0x24030(%rip), %xmm1    # 0x2b1a8
cvttsd2si %xmm1, %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movb 0x2fcba(%rip), %al      # 0x36e45
cmpb $0x1, %al
jne 0x71a3
leaq 0x24cb3(%rip), %rdi     # 0x2be49
movb $0x1, %al
callq 0x2040
movb 0x2fca2(%rip), %al      # 0x36e45
testb $0x1, %al
je 0x71bd
movq 0x8(%rsp), %rax
movq (%rax), %rsi
leaq 0x24ca0(%rip), %rdi     # 0x2be56
xorl %eax, %eax
callq 0x2040
incl %ebx
jmp 0x6fe9
pushq $-0x2
popq %rax
addq $0x1158, %rsp           # imm = 0x1158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x71dd
jmp 0x71dd
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2578
movq %rbx, %rdi
callq 0x24d0