// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
movq %rdi, 0x48(%rsp)
leaq 0x50(%rsp), %rdi
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x2140
leaq 0x150(%rsp), %rdi
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
callq 0x2140
movq 0x33120(%rip), %rax     # 0x3abb8
movq %rax, %rcx
subq 0x34466(%rip), %rcx     # 0x3bf08
movl $0x1, %ebp
cmpq $0x3c, %rcx
jl 0x7ab9
movq %rax, 0x34454(%rip)     # 0x3bf08
movl $0x2, %ebp
movabsq $0x302e302e302e30, %rax # imm = 0x302E302E302E30
cmpq %rax, 0x31f28(%rip)     # 0x399f2
je 0x7cff
leaq 0x293c0(%rip), %r15     # 0x30e97
leaq 0x31f14(%rip), %r13     # 0x399f2
xorl %ebx, %ebx
movl %ebp, 0x24(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
movl $0x0, 0x40(%rsp)
cmpl $0x1, %ebx
je 0x7b0d
testl %ebx, %ebx
jne 0x7b1d
movb $0x0, 0x32(%rsp)
movw $0x4d45, 0x30(%rsp)     # imm = 0x4D45
jmp 0x7b1d
movl $0x617461, 0x33(%rsp)   # imm = 0x617461
movl $0x61444d45, 0x30(%rsp) # imm = 0x61444D45
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x2050
movq %r14, %rdi
leaq 0x2aeef(%rip), %rsi     # 0x32a30
callq 0x20a0
movq %rax, %r12
testq %rax, %rax
je 0x7ced
leaq 0x150(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
movq %r12, %rdx
callq 0x2440
testq %rax, %rax
je 0x7ced
cmpb $0x1, 0x322ae(%rip)     # 0x39e25
jne 0x7b86
leaq 0x150(%rsp), %rdi
callq 0x2400
leaq 0x10(%rsp), %rax
movq %rax, (%rsp)
movq $0x0, 0x8(%rsp)
movb $0x0, 0x10(%rsp)
movl $0x1000, %esi           # imm = 0x1000
leaq 0x150(%rsp), %rdi
callq 0x2c94c
movq %rax, %r14
movq 0x8(%rsp), %rdx
movl $0x6, %r8d
movq %rsp, %rdi
xorl %esi, %esi
leaq 0x29325(%rip), %rcx     # 0x30eee
callq 0x2410
movq %r15, %r13
movq (%rsp), %rsi
movq %r14, %rdi
callq 0x2d1d7
movq %rax, %r14
movq 0x8(%rsp), %rdx
movl $0xf, %r8d
movq %rsp, %rdi
xorl %esi, %esi
leaq 0x292fe(%rip), %rcx     # 0x30ef5
callq 0x2410
movq (%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x2d1d7
movq %rax, %rbp
movq 0x8(%rsp), %rdx
movl $0x9, %r8d
movq %rsp, %rdi
xorl %esi, %esi
leaq 0x292e0(%rip), %rcx     # 0x30f05
callq 0x2410
movq %r14, %rdi
movq %r15, %rsi
callq 0x2d1d7
movq %rax, %r14
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
movq %r13, %r15
je 0x7c56
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x22b0
movq %r12, %rdi
callq 0x23a0
testq %rbp, %rbp
leaq 0x31d8a(%rip), %r13     # 0x399f2
je 0x7c8b
movl 0x28(%rbp), %esi
movq 0x48(%rsp), %rax
movl %esi, (%rax)
cmpb $0x1, 0x321aa(%rip)     # 0x39e25
jne 0x7c8b
leaq 0x2928b(%rip), %rdi     # 0x30f0f
xorl %eax, %eax
callq 0x2040
testq %r14, %r14
movl 0x24(%rsp), %ebp
je 0x7cf5
movsd 0x30(%r14), %xmm0
movapd %xmm0, %xmm1
mulsd 0x285d2(%rip), %xmm1    # 0x30278
cvttsd2si %xmm1, %rax
movq 0x28(%rsp), %rcx
movq %rax, (%rcx)
movb 0x3216c(%rip), %al      # 0x39e25
cmpb $0x1, %al
jne 0x7cd1
leaq 0x29255(%rip), %rdi     # 0x30f19
movb $0x1, %al
callq 0x2040
movb 0x32154(%rip), %al      # 0x39e25
testb $0x1, %al
je 0x7cf5
movq 0x28(%rsp), %rax
movq (%rax), %rsi
leaq 0x29242(%rip), %rdi     # 0x30f26
xorl %eax, %eax
callq 0x2040
jmp 0x7cf5
movq %r12, %rdi
callq 0x23a0
incl %ebx
cmpl %ebx, %ebp
jne 0x7ae4
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addq $0x1158, %rsp           # imm = 0x1158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7d1a
jmp 0x7d1a
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x7d38
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x22b0
movq %rbx, %rdi
callq 0x24a0