// File: 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:
subq $0x11e8, %rsp           # imm = 0x11E8
movq %rdi, 0x11e0(%rsp)
movq %rsi, 0x11d8(%rsp)
movw 0x583a7(%rip), %ax      # 0x60c55
movw %ax, 0x10cd(%rsp)
movb 0x5839b(%rip), %al      # 0x60c57
movb %al, 0x10cf(%rsp)
movl 0x5838f(%rip), %eax     # 0x60c58
movl %eax, 0x10c6(%rsp)
movw 0x58385(%rip), %ax      # 0x60c5c
movw %ax, 0x10ca(%rsp)
movb 0x58379(%rip), %al      # 0x60c5e
movb %al, 0x10cc(%rsp)
leaq 0x10d0(%rsp), %rdi
xorl %esi, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x2170
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2170
movl $0x0, 0x9c(%rsp)
movq 0x672d2(%rip), %rax     # 0x6fbf8
subq 0x6861b(%rip), %rax     # 0x70f48
cmpq $0x3c, %rax
jl 0x894e
movl $0x1, 0x9c(%rsp)
movq 0x672b3(%rip), %rax     # 0x6fbf8
movq %rax, 0x685fc(%rip)     # 0x70f48
jmp 0x8959
movl $0x0, 0x9c(%rsp)
leaq 0x660ce(%rip), %rdi     # 0x6ea2e
leaq 0x579b0(%rip), %rsi     # 0x60317
callq 0x23c0
cmpl $0x0, %eax
je 0x8cd7
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jg 0x8cd5
movq $0x0, 0x11d0(%rsp)
leaq 0x10b0(%rsp), %rdi
xorl %esi, %esi
movl $0x14, %edx
callq 0x2170
cmpl $0x0, 0x98(%rsp)
jne 0x89d3
leaq 0x10b0(%rsp), %rdi
leaq 0x10cd(%rsp), %rsi
callq 0x2290
cmpl $0x1, 0x98(%rsp)
jne 0x89f2
leaq 0x10b0(%rsp), %rdi
leaq 0x10c6(%rsp), %rsi
callq 0x2290
leaq 0x10d0(%rsp), %rdi
leaq 0x10b0(%rsp), %rdx
leaq 0x58256(%rip), %rsi     # 0x60c5f
leaq 0x65f24(%rip), %rcx     # 0x6e934
addq $0xfa, %rcx
movb $0x0, %al
callq 0x2060
leaq 0x10d0(%rsp), %rdi
leaq 0x57dd6(%rip), %rsi     # 0x60803
callq 0x20b0
movq %rax, 0x11d0(%rsp)
movq $0x0, 0x90(%rsp)
movq $0x0, 0x88(%rsp)
movq $0x0, 0x80(%rsp)
cmpq $0x0, 0x11d0(%rsp)
je 0x8bee
leaq 0xa0(%rsp), %rdi
movq 0x11d0(%rsp), %rdx
movl $0x1000, %esi           # imm = 0x1000
callq 0x2450
cmpq $0x0, %rax
je 0x8bec
testb $0x1, 0x663c9(%rip)     # 0x6ee61
je 0x8ab0
leaq 0xa0(%rsp), %rsi
leaq 0x59342(%rip), %rdi     # 0x61deb
movb $0x0, %al
callq 0x2040
leaq 0x60(%rsp), %rdi
callq 0x23a0
leaq 0xa0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x599f0
movq %rax, 0x30(%rsp)
jmp 0x8ad3
movq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
leaq 0x581d2(%rip), %rsi     # 0x60cb6
leaq 0x60(%rsp), %rdi
callq 0x2380
jmp 0x8af0
xorl %eax, %eax
movl %eax, %esi
leaq 0x60(%rsp), %rdi
callq 0x2550
movq %rax, 0x28(%rsp)
jmp 0x8b05
movq 0x28(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x58(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x5a2f0
movq %rax, 0x20(%rsp)
jmp 0x8b25
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x58184(%rip), %rsi     # 0x60cbd
leaq 0x60(%rsp), %rdi
callq 0x2380
jmp 0x8b45
xorl %eax, %eax
movl %eax, %esi
leaq 0x60(%rsp), %rdi
callq 0x2550
movq %rax, 0x18(%rsp)
jmp 0x8b5a
movq 0x18(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x90(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x5a2f0
movq %rax, 0x10(%rsp)
jmp 0x8b7d
movq 0x10(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x5813c(%rip), %rsi     # 0x60ccd
leaq 0x60(%rsp), %rdi
callq 0x2380
jmp 0x8b9d
movq 0x90(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x5a2f0
movq %rax, 0x8(%rsp)
jmp 0x8bb6
movq 0x8(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x2578
jmp 0x8bec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x50(%rsp)
movl %eax, 0x4c(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x2578
jmp 0x8ce4
jmp 0x8bee
movq 0x11d0(%rsp), %rdi
callq 0x23d0
movl %eax, 0x10a8(%rsp)
cmpq $0x0, 0x88(%rsp)
je 0x8c45
movq 0x88(%rsp), %rax
movl 0x28(%rax), %ecx
movq 0x11e0(%rsp), %rax
movl %ecx, (%rax)
testb $0x1, 0x66238(%rip)     # 0x6ee61
je 0x8c43
movq 0x11e0(%rsp), %rax
movl (%rax), %esi
leaq 0x5809b(%rip), %rdi     # 0x60cd7
movb $0x0, %al
callq 0x2040
jmp 0x8c45
cmpq $0x0, 0x80(%rsp)
je 0x8cbd
movq 0x80(%rsp), %rax
movsd 0x574b0(%rip), %xmm0    # 0x60110
mulsd 0x30(%rax), %xmm0
cvttsd2si %xmm0, %rcx
movq 0x11d8(%rsp), %rax
movq %rcx, (%rax)
testb $0x1, 0x661e5(%rip)     # 0x6ee61
je 0x8c99
movq 0x80(%rsp), %rax
movsd 0x30(%rax), %xmm0
leaq 0x5804f(%rip), %rdi     # 0x60ce1
movb $0x1, %al
callq 0x2040
testb $0x1, 0x661c1(%rip)     # 0x6ee61
je 0x8cbb
movq 0x11d8(%rsp), %rax
movq (%rax), %rsi
leaq 0x5803a(%rip), %rdi     # 0x60cee
movb $0x0, %al
callq 0x2040
jmp 0x8cbd
jmp 0x8cbf
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x8980
jmp 0x8cd7
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
addq $0x11e8, %rsp           # imm = 0x11E8
retq
movq 0x50(%rsp), %rdi
callq 0x24c0
nop