Customizing 123solar (and 485solar-get)

485solar-get protocol http://sourceforge.net/projects/solarget/
Post Reply
ZigmundRat
Posts: 3
Joined: Tue Aug 30, 2016 11:41 pm

Customizing 123solar (and 485solar-get)

Post by ZigmundRat » Wed Aug 31, 2016 12:21 am

This might run a little long, so bear with me... I'm new to the 123solar application and am working on getting things working with my circa 2007 SMA SunnyBoy SB1800u. I have run into some problems :)

The first problem is that 485solar-get does not work - it segfaults at worst, or after some work returns unusable values:
Detect daemon...
Trying to connect...
No daemon found
Selected Options:
debug :1 data :1 events :0
alarms :0 info :0 comments :0
help :0 initcommport :1 status :0
synchrone :0 DeviceNr :0 StartDaemon :0
DaemonState :0 RequestDaemon:0 monitor :0
Version :0 ExitDaemon :0
yasdiMasterInitialize /etc/yasdi.ini
yasdiMasterSetDriverOnline
DoStartDeviceDetection asyc ...
Wait for GetDeviceHandles 1
Found device: 1 WR18UW4E SN:1554101887

Stopping async device detection...
GetChannelHandle for Upv-Ist
GetChannelValue for 56 :Communication ERROR
DC Power value not in SMA, set to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
Efficiency value not in SMA, set to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
dummy, set output to 0
Calculated DC Power: -0.000000
Calculate Efficiency: 0.000000

The various channel names are different in my non-German SunnyBoy. So I have no Upv-Ist, Iac-Ist, etc. I have Vpv, Ipv, etc. (One of the reasons for the segfaults in 485solar-get.) So I need to modify 485solar-get to retrieve the proper channels values. This I can do. But I need to map the values I have available from my inverter to the array used by 123solar.php. Here is what I have:
------------------------------------------------------------
Channel handle | Channel name | Channel value (Unit) |
------------------------------------------------------------
55 | ' Vpv' | '134.000' (V)
57 | ' Vpv-Setpoint' | '649.000' (V)
58 | ' Iac' | '0.000' (mA)
60 | ' Vac' | '119.000' (V)
61 | ' Fac' | '59.940' (Hz)
62 | ' Pac' | '0.000' (W)
63 | ' Zac' | '0.000' (Ohm)
65 | ' Vpv-_PE' | '0.000' (V)
71 | ' Ipv' | '82.000' (mA)
76 | ' E-Total' | '11087.040' (kWh)
77 | ' h-Total' | '39136.373' (h)
78 | ' Power On' | '14074.000'
80 | ' Serial Number' | '1554101887.000'
81 | ' Mode' | 'Waiting'
83 | ' Error' | '-------'

In 485solar-get.php we see an array defined with various values, and this is also seen in 123solar.php. But what do the $I1V, $I1A, $I1P, $I2A/V/P, $G1V, $G1A, etc. represent? I'd like to know specifically what the array passed from the protocol to 123solar.php is supposed to look like so I can populate the right values in the right order. I can infer only so much from the short names. Thanks!

jeanmarc
Posts: 1434
Joined: Thu Aug 29, 2013 7:16 am

Re: Customizing 123solar (and 485solar-get)

Post by jeanmarc » Wed Aug 31, 2016 6:59 am

Hi,
You better contact Roland to adjust 485solar-get to your inverter model.
For the protocols explanation you can read here.
You can create a simple php test file to return the array like this.
Keep us informed

ZigmundRat
Posts: 3
Joined: Tue Aug 30, 2016 11:41 pm

Re: Customizing 123solar (and 485solar-get)

Post by ZigmundRat » Sun Sep 04, 2016 2:36 am

Thanks for the pointers. Along with digging a little deeper, those postings helped me identify things and make the changes required to start collecting data. So I'm on my way. Next is to get the data collection started on system boot. Don't like seeing those data gaps :)

I DO have some other things that might be useful on the 485solar-get side of things which I'll take up over there (additional data channels, parallel MySQL logging, etc.).

Thanks for the help!

frohro
Posts: 2
Joined: Thu Oct 12, 2017 12:35 am

Re: Customizing 123solar (and 485solar-get)

Post by frohro » Thu Oct 12, 2017 12:39 am

Hi,

I too am running into core-dumps from 485solar-get, and am using US versions of the SMA inverters (SB8000US). Would you by chance share your modified 485solar-get?

Thanks,

Rob

jeanmarc
Posts: 1434
Joined: Thu Aug 29, 2013 7:16 am

Re: Customizing 123solar (and 485solar-get)

Post by jeanmarc » Thu Oct 12, 2017 9:13 am

Hi,
Mind that you better get in touch with Roland to adjust his application.

ZigmundRat
Posts: 3
Joined: Tue Aug 30, 2016 11:41 pm

Re: Customizing 123solar (and 485solar-get)

Post by ZigmundRat » Tue Oct 17, 2017 8:28 pm

I will have to check my system and dig it up, make sure it still compiles, etc. I don't remember now if it was just the build environment or if I needed to modify the source. But I'll take a look at it.

Note that I'm not in a position to do much hand holding - you'll need to be able to build it yourself on whatever platform you are using. I am using a pi.

frohro
Posts: 2
Joined: Thu Oct 12, 2017 12:35 am

Re: Customizing 123solar (and 485solar-get)

Post by frohro » Tue Oct 24, 2017 8:14 pm

Thanks! I'm probably just redoing the things you had to do. I saw a really old post where this problem surfaced with sma-get in 2013, and Roland was going to modify things so that the program looks for English or German, but I don't see that in the source code I have. I modified the source to replace Upv-Ist with Vpv and Uac with Vac, and any other U in the labels with V and now I get a whole slew of data, but a similar thing is getting me down further. The problem is that FindChannelName() is returning a zero, which means it could not find anything. This error is not checked for, so it segfaults. At this point, it appears to work, but I don't know if it will work with 123Solar yet. Below I have copied my hacked 485solar-get.c for those who want to use English SMA inverters. Beware, that I just guessed what the English equivalent would be, and I may have some things untested wrong.

Thanks,
Rob

Code: Select all

/* 
  485solar-get.c 
 
  v1.000 2014-09-04 : New Project name, continued
                                   

  Project site: http://code.google.com/p/485solar-get/
 
  build by Roland Breedveld, based on libyasdi library's
  for SMA support with the 123Solar project http://www.123solar.org/
 
  mail: Roland@Breedveld.net
 
  yasdiconf should be placed here: /etc/yasdi.ini or specified with -f
 
  To run (or to compile) you need the libs from libyasdi_1_8_1
  http://www.sma.de/en/products/monitoring-systems/yasdi.html
 
  location of 485solar-get.c : /usr/local/src/libyasdi_1_8_1/485solar-get_<version>/485solar-get.c

  Compile command:
  gcc 485solar-get.c -I../include/ -I../smalib -I../libs -o 485solar-get -lyasdimaster
  SMA and SunnyBoy are registerd trademarks
  Licences:
  485solar-get : GNU GPL v3
  YASDI   : GNU GPL v2 written by SMA Solar Technology AG.
 */

#include "libyasdimaster.h"
#include "stdio.h"
#include "time.h"
#include "string.h"
#include "stdlib.h"
#include "unistd.h"
#include "errno.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/socket.h"
#include "sys/un.h"
#include "fcntl.h"
#define SOCK_PATH "/var/tmp/485solar-get_socket"

char configFile[128] = "/etc/yasdi.ini";


int print_help(char solar_get_version[7], char version_date[11] ) {
  solar_get_version[7]='\0';
  version_date[11]='\0';
  printf("\n%s\n", "485solar-get for 123Solar"); 
  printf("%s\n", "based on libyasdi from SMA Solar Technology ag"); 
  printf("%s %s %s\n\n", solar_get_version, version_date, "by roland@breedveld.net"); 
  printf("%s%s\n\n", "Config file: ", configFile); 
  printf("%s\n", "Options:"); 
  printf("%s\n", " -h show this help"); 
  printf("%s\n", " -d show data"); 
  printf("%s\n", " -b show debug information, written to errout"); 
  printf("%s\n", " -i show info"); 
  printf("%s\n", " -e show events"); 
  printf("%s\n", " -c show data with comments"); 
  printf("%s\n", " -a show alarms"); 
  printf("%s\n", " -s show possible status fields"); 
  printf("%s\n", " -r show state of Daemon"); 
  printf("%s\n", " -S synchrone detection, default is asynchrone"); 
  printf("%s\n", " -D start 485solar-get as daemon"); 
  printf("%s\n", " -m monitor loop"); 
  printf("%s\n", " -x send exit to daemon"); 
  printf("%s\n", " -v show version"); 
  printf("%s\n\n", " -n<0-9> deviceNr, default -n0"); 
  printf("%s\n", "485solar-get  project:  http://code.google.com/p/485solar-get"); 
  printf("%s\n", "123Solar project:  http://www.123solar.org"); 
  printf("%s\n", "YASDI    software: http://www.sma.de/en/products/monitoring-systems/yasdi.html");
  printf("%s\n\n", "SMA and SunyBoy are registered trademarks of SMA Technology AG");
  return 0;
}

int test_daemon_socket(int start_daemon, int show_debug){
  int socket1, socket_len, return_s;

  if(show_debug==1) fprintf(stderr, "%s\n", "Detect daemon...");
  struct sockaddr_un remote;
  if ((socket1 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "%s\n", "Socket Error");
    return_s=1;
  }else{
    if(show_debug==1) fprintf(stderr, "%s\n", "Trying to connect...");
    remote.sun_family = AF_UNIX;
    strcpy(remote.sun_path, SOCK_PATH);
    socket_len = strlen(remote.sun_path) + sizeof(remote.sun_family);
    if (connect(socket1, (struct sockaddr *)&remote, socket_len) == -1) {
      if(show_debug==1) fprintf(stderr, "%s\n", "No daemon found");
      return_s=1;
    }else{
      if(show_debug==1) fprintf(stderr, "%s\n", "Connected to daemon");
      return_s=0;
      if(show_debug==1) fprintf(stderr, "%s\n", "Close socket");
      close(socket1);
      if(start_daemon==1){
        if(show_debug==1) fprintf(stderr, "%s\n", "Close socket");
        fprintf(stderr, "%s\n", "Daemon already running");
        return_s=1;
        exit(1);
      }
    }
  }
  return return_s;
}

int main(int argc, char *argv[])
{
  char solar_get_version[7]="v1.000";
  char version_date[11]="2014-09-04";
  solar_get_version[7]='\0';
  version_date[11]='\0';
  int i;
  DWORD channelHandle=0;  
  DWORD SerNr=0;          
  DWORD DeviceHandle[10]; 
  int device_nr=0; 
  int nr_of_devices=0; 
  int ires=0;             
  DWORD dwBDC=0;          
  double values[32];          
  char units[32][5];          
  char valuetext[17];     
  char st[30];
  size_t ft;
  struct tm tim;
  time_t now;
  char NameBuffer[50];
  int show_debug=0;
  int show_help=0;
  int show_data=0;
  int show_info=0;
  int show_events=0;
  int show_alarms=0;
  int show_comments=0;
  int show_status=0;
  int show_version=0;
  int start_daemon=0;
  int daemon_state=0;
  int request_daemon=0;
  int synchrone_detect=0;
  int return_code=0;
  int devdetect=0;
  int devdetectloop=0;
  int init_comm=0;
  int monitor=0;
  int exit_loop=0;
  int daemon_exit=0;
  int socket1, socket2, socket_size, socket_len;
  char socket_command[2];
  char socket_out[1024];
  int sn;
  int data_state;
  char StatText[30];
  int TxtCnt;

  struct sockaddr_un local, remote;


  /*
     sequence and dummy fields to be compatible with the aurora command output 
     aurora's show the data of 2 individual strings and the total values, didn't see this function with SMA's 
     so, sting 1 is the same as te total data.
     SMA don't have temperature, efficiency, day, week and month counters. 
     efficiency is calculated between panel-power and grid-power, it looks a lot lower than the efficiency with aurora's
     e.g. 98% on an aurora and 87% on a SunnyBoy 1200, don't know if the aurora Eff is right, calculating by hand will
     show lower values.... 

     Original aurora fields for data collection:
        date/time, Input 1 Voltage, Input 1 Current, Input 1 Power, Input 2 Voltage, Input 2 Current, Input 2 Power,
        Grid Voltage Reading, Grid Current Reading, Grid Power Reading, Frequency Reading,
        DC/AC Conversion Efficiency, Inverter Temperature, Booster Temperature,
        Daily Energy, Weekly Energy, Monthly Energy, Yearly Energy, Total Energy, Partial Energy

     Original aurora fields for extended information:
        Vbulk, Vbulk Mid, Vbulk +, Vbulk -, Vbulk (Dc/Dc), Ileak (Dc/Dc) Reading, Ileak (Inverter) Reading, 
        Isolation Resistance (Riso), Grid Voltage (Dc/Dc), Average Grid Voltage (VgridAvg), Grid Voltage neutral,
        Grid Frequency (Dc/Dc), Power Peak, Power Peak Today, Supervisor Temperature, Alim Temperature,
        Heak Sink Temperature, Temperature 1, Temperature 2, Temperature 3, Fan 1 Speed, Fan 2 Speed, Fan 3 Speed,
        Fan 4 Speed, Fan 5 Speed, Pin 1, Pin 2, Power Saturation limit (Der.), Reference Ring Bulk,
        Vpanel micro, Wind Generator Frequency
*/

  char data_set[20][20]={"Vpv", "Ipv", "Pdc", "dummy", "dummy", "dummy", "Vac", "Iac", "Pac", "Fac", "Effcy", "dummy", "dummy", "dummy", "dummy", "dummy", "dummy", "dummy", "E-Total", "dummy" }; 
  char event_set[31][20]={"Vpv","Vpv","dummy","dummy","Vpv-Ist","dummy","dummy","Riso","Vac","Vac","Vac" ,"Fac","Plimit","dummy","dummy","dummy","dummy","dummy","dummy","dummy","dummy","dummy","dummy","dummy","dummy","Pac","dummy","dummy","dummy","dummy","dummy"};

  if(argc<2) {
    fprintf(stderr, "\n%s\n", "ERROR: No options given, showning help");
    show_help=1;
    return_code=1;
    print_help(solar_get_version, version_date);
    return return_code;
  }

  for(i=1; i < argc; i++) {
    switch (argv[i][1]) {
      case 'h':
        show_help=1;
        break;
      case 'd':
        show_data=1;
        init_comm=1;
        break;
      case 'b':
        show_debug=1;
        break;
      case 'i':
        show_info=1;
        init_comm=1;
        break;
      case 'e':
        show_events=1;
        init_comm=1;
        break;
      case 'a':
        show_alarms=1;
        init_comm=1;
        break;
      case 'c':
        show_comments=1;
        break;
      case 'S':
        synchrone_detect=1;
        break;
      case 's':
        show_status=1;
        init_comm=1;
        break;
      case 'n':
        device_nr=atoi(&argv[i][2]);
        break;    
      case 'D':
        start_daemon=1;
        init_comm=1;
        break;
      case 'r':
        daemon_state=1;
        break;
      case 'm':
        monitor=1;
        break;
      case 'x':
        daemon_exit=1;
        break;
      case 'v':
        show_version=1;
        break;
      case 'f':
		memset(configFile, '\0', sizeof(configFile));
		strcpy(configFile,&argv[i][2]);
		//strcpy(configFile,"./yasdi.ini");
		break;
    }
  }
  if(start_daemon==1){
    nr_of_devices=device_nr; 
  }

  if (test_daemon_socket(start_daemon, show_debug) == 0){
    request_daemon=1;
    init_comm=0;
    sleep(1);
  }

  if(daemon_state == 1){
    if(request_daemon == 1){
      printf("%s\n", "Daemon is running"); 
    }else{
      printf("%s\n", "Daemon is down"); 
    }
  }

  if(show_debug==1){
    fprintf(stderr, "   Selected Options:\n" );
    fprintf(stderr, "      debug        :%d", show_debug);
    fprintf(stderr, "      data         :%d", show_data);
    fprintf(stderr, "      events       :%d\n", show_events);
    fprintf(stderr, "      alarms       :%d", show_alarms);
    fprintf(stderr, "      info         :%d", show_info);
    fprintf(stderr, "      comments     :%d\n", show_comments);
    fprintf(stderr, "      help         :%d", show_help);
    fprintf(stderr, "      initcommport :%d", init_comm);
    fprintf(stderr, "      status       :%d\n", show_status);
    fprintf(stderr, "      synchrone    :%d", synchrone_detect);
    fprintf(stderr, "      DeviceNr     :%d", device_nr);
    fprintf(stderr, "      StartDaemon  :%d\n", start_daemon);
    fprintf(stderr, "      DaemonState  :%d", daemon_state);
    fprintf(stderr, "      RequestDaemon:%d", request_daemon);
    fprintf(stderr, "      monitor      :%d\n", monitor);
    fprintf(stderr, "      Version      :%d", show_version);
    fprintf(stderr, "      ExitDaemon   :%d\n", daemon_exit);
  }

  if(show_help==1){
    print_help(solar_get_version, version_date);
  }
  if(show_version==1){
    printf("%s %s\n\n", solar_get_version, version_date); 
  }
  if(init_comm==1){
    if(show_debug==1) fprintf(stderr, "%s%s\n", "yasdiMasterInitialize", configFile);
    yasdiMasterInitialize(configFile,&dwBDC); 
    
    for(i=0; i < dwBDC; i++) {
      if(show_debug) fprintf(stderr, "%s\n", "yasdiMasterSetDriverOnline");
      yasdiMasterSetDriverOnline( i );
    }

    if (0==dwBDC){
      fprintf(stderr, "%s\n", "Warning: No YASDI driver was found.");
      return 1;
    }

    if(synchrone_detect==0)
    { 
      if(show_debug==1) fprintf(stderr, "%s\n", "DoStartDeviceDetection asyc ...");
      ires = DoStartDeviceDetection(device_nr+1, false);  /* set async with wait loop */
      while (devdetect!=1) {
        sleep(1);
        if(show_debug==1) fprintf(stderr, "%s ", "Wait for GetDeviceHandles");
        ires = GetDeviceHandles(DeviceHandle, 10 );
        if(show_debug==1) fprintf(stderr, "%i\n", ires);
        if (ires>device_nr) {
          if(show_debug==1) {
            fprintf(stderr, "%s", "Found device:");
            GetDeviceName(DeviceHandle[device_nr], NameBuffer, sizeof(NameBuffer)-1);
            fprintf(stderr, "%3lu %s\n\n", (unsigned long)DeviceHandle[device_nr], NameBuffer);
            fprintf(stderr, "%s", "Stopping async device detection...\n");
          }
          devdetect=1;
          DoStopDeviceDetection();
        } else {
          devdetectloop++;
          if (devdetectloop > 15) {
            fprintf(stderr, "%s\n", "Device detect timeout, or device_nr not found");
            return 1;
          }
        }
      }
    }else{
      if(show_debug==1) fprintf(stderr, "%s\n", "DoStartDeviceDetection syc ...");
      ires = DoStartDeviceDetection(device_nr+1, true); 
      if(show_debug==1) fprintf(stderr, "%s ", "wait for GetDeviceHandles");
      ires = GetDeviceHandles(DeviceHandle, 10 );
      if(show_debug==1) {
        fprintf(stderr, "%i\n", ires);
        fprintf(stderr, "%s", "Found device:");
        GetDeviceName(DeviceHandle[device_nr], NameBuffer, sizeof(NameBuffer)-1);
        fprintf(stderr, "%3lu %s\n\n", (unsigned long)DeviceHandle[device_nr], NameBuffer);
      }
    }
    if (ires) {
      while (exit_loop==0) {
        socket_out[0] = '\0';
        if(start_daemon==1){
          if(show_debug==1) fprintf(stderr, "%s\n", "Initializing Socket1");
          if((socket1 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
          }
          local.sun_family = AF_UNIX;
          strcpy(local.sun_path, SOCK_PATH);
          unlink(local.sun_path);
          socket_len = strlen(local.sun_path) + sizeof(local.sun_family);
          if (bind(socket1, (struct sockaddr *)&local, socket_len) == -1) {
             perror("bind");
             exit(1);
          }
          if (listen(socket1, 5) == -1) {
             perror("listen");
             exit(1);
          }
          socket_size = sizeof(remote);
          /*struct sockaddr_un local, remote;*/
          if(show_debug==1) fprintf(stderr, "%s\n", "Waiting for client connection...");
          if ((socket2 = accept(socket1, (struct sockaddr *)&remote, &socket_size)) == -1) {
            if(show_debug==1) fprintf(stderr, "%s\n", "accept\n");
            perror("accept");
            exit(1);
          }
          exit_loop=0;
          show_data=0;
          show_alarms=0;
          show_events=0;
          show_status=0;
          show_info=0;
          if(show_debug==1) fprintf(stderr, "%s\n", "Client Connected.");
          socket_command[1] = '\0';
          socket_command[0] = '\0';
          device_nr=0;
          sn = recv(socket2, socket_command, 2, 0);
          socket_command[sn] = '\0';
          if(show_debug==1) fprintf(stderr, "%s %s\n", "Received from socket:", socket_command);
          switch (socket_command[0]) {
            case 'x':
              exit_loop=1;
              break;
            case 'd':
              show_data=1;
              break;
            case 'a':
              show_alarms=1;
              break;
            case 'e':
              show_events=1;
              break;
            case 's':
              show_status=1;
              break;
            case 'i':
              show_info=1;
              break;
          }
          device_nr=atoi(&socket_command[1]);
          if(device_nr > nr_of_devices){
            if(show_debug==1) fprintf(stderr, "%s%i[%i]\n", "Requesting uninitialised deviceNr:", device_nr, nr_of_devices);
            show_data=0;
            show_alarms=0;
            show_events=0;
            show_status=0;
            show_info=0;
            if(show_debug==1) fprintf(stderr, "%s\n", "Output data to socket2");
            sprintf(socket_out, "%s\n", "0");
            if (send(socket2, socket_out, sn, 0) == -1){
              perror("send");
              exit(1);
            }
            socket_out[0] = '\0';
          }else{
            if(show_debug==1) fprintf(stderr, "%s %i\n", "Requesting deviceNr", device_nr);
          }
          socket_command[0] = '\0';
        }

        if(show_data==1){
          data_state=0;
          for(i=0; i < 20; i++) {
            units[i][0] = '\0';
            if(strcmp(data_set[i],"dummy") == 0) {
              if(show_debug==1) fprintf(stderr, "%s\n", "dummy, set output to 0");
              values[i]=0;
            } else if(strcmp(data_set[i],"Effcy") == 0) {
              if(show_debug==1) fprintf(stderr, "%s\n", "Efficiency value not in SMA, set to 0");
              values[i]=0;
            } else if(strcmp(data_set[i],"Pdc") == 0) {
              if(show_debug==1) fprintf(stderr, "%s\n", "DC Power value not in SMA, set to 0");
              values[i]=0;
            } else {
              if(data_state==1){
                values[i]=0;
              }else{
                if(show_debug==1) fprintf(stderr, "GetChannelHandle for %s\n ", data_set[i]);
                channelHandle = FindChannelName(DeviceHandle[device_nr],  data_set[i]);
                if(show_debug==1) fprintf(stderr, "GetChannelValue for %u :", channelHandle);
                if(channelHandle == 0) {
					fprintf(stderr, "Could not find the device at line 457.  Now exiting due to error. \n");
					exit(1);
				}
                ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[i], valuetext, 16, 5 );
                ires = GetChannelUnit(channelHandle, units[i], sizeof(units[i])-1 ); 
                units[i][sizeof(units[i])] = '\0';
                if (values[i] == -1){
                  data_state=1;
                  exit_loop=1;
                  if(show_debug==1) fprintf(stderr, "%s\n", "Communication ERROR");
                }else{
                  if(show_debug==1) fprintf(stderr, "%f %s\n", values[i], valuetext);
                }
                if(strcmp(units[i],"mA") == 0) {
                  values[i]=values[i]/1000;
                  strcpy(units[i], "A");
                  if(show_debug==1) fprintf(stderr, "%s %f\n", "mA to A: ",values[i]);
                }
              }
            }
          }

          values[2]=values[0] * values[1];
          if(show_debug==1) fprintf(stderr, "%s %f\n", "Calculated DC Power: ",values[2]);

          if ( values[7] > 0 ) {
            values[10]=100 * ((values[6] * values[7]) / (values[0] * values[1])) ; 
          } else {
            values[10]=0;
          }   
          if(show_debug==1) fprintf(stderr, "%s %f\n", "Calculate Efficiency: ",values[10]);
       
          now = time(NULL);
          tim = *(localtime(&now));
          ft = strftime(st,30,"%Y%m%d-%H:%M:%S",&tim);
          if(values[0] > 0) {
            if(show_comments==1) sprintf(socket_out, "%-15s ", "Date");
            sprintf(socket_out, "%s%s",socket_out, st);
            for(i=0; i < 20; i++) {
              if(show_comments==1) sprintf(socket_out, "%s\n %-15s %-6s",socket_out, data_set[i], units[i]);
              sprintf(socket_out, "%s %lf ", socket_out, values[i]);
            }
            if(show_comments==1) sprintf(socket_out, "%s\n %-15s",socket_out, "State");
            if(data_state==1){
              sprintf(socket_out, "%s %s\n", socket_out, "ERROR");
            }else{
              sprintf(socket_out, "%s %s\n", socket_out, "OK");
            }
            sn=strlen(socket_out);
            socket_out[sn] = '\0';
            if(start_daemon==1){
              if(show_debug==1) fprintf(stderr, "%s\n", "Output data to socket2");
              if (send(socket2, socket_out, sn, 0) == -1){
                perror("send");
                exit(1);
              }
            }else{
              if(show_debug==1) fprintf(stderr, "%s\n", "Output data to stdout");
              printf("%s", socket_out); 
            }
            if(show_debug==1) fprintf(stderr, "%s", socket_out);
            socket_out[0] = '\0';
          }
        }

        if(show_info==1){
           GetDeviceName(DeviceHandle[device_nr], NameBuffer, sizeof(NameBuffer)-1);
           sprintf(socket_out, "%-3lu %s\n", (unsigned long)DeviceHandle[device_nr], NameBuffer);
           channelHandle = FindChannelName(DeviceHandle[device_nr], "Software-BFR");
           ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
           sprintf(socket_out, "%s%s %lf %s\n",socket_out, "Software-BFR",values[0],valuetext);  
           channelHandle = FindChannelName(DeviceHandle[device_nr], "SMA-SN");
           ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
           sprintf(socket_out, "%s%s %lf %s\n",socket_out, "SMA-SN",values[0],valuetext);  
           channelHandle = FindChannelName(DeviceHandle[device_nr], "Default");
           ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
           sprintf(socket_out, "%s%s %lf %s\n",socket_out, "Default",values[0],valuetext);  
           /* Disabled: May result in a core dump with SB8000US.
            * channelHandle = FindChannelName(DeviceHandle[device_nr], "Status");
           if(channelHandle == 0) {
		      fprintf(stderr, "%s %i\n","channelHandle got: ",channelHandle); // Perhaps there is no "Status".
		      fprintf(stderr, "Could not find the device at line 534.  Now exiting due to error. \n");
			  exit(1);
				}
           ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
           sprintf(socket_out, "%s%s %s\n",socket_out, "Status",valuetext);
           channelHandle = FindChannelName(DeviceHandle[device_nr], "Betriebsart");
           ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
           sprintf(socket_out, "%s%s %s\n",socket_out, "Betriebsart",valuetext); 
           */  
           /* Disabled, wil result in a core-dump with some smas
             channelHandle = FindChannelName(DeviceHandle[device_nr], "Phase");
             ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
             sprintf(socket_out, "%s%s %s\n\n",socket_out, "Phase",valuetext);  
           */
           if(start_daemon==1){
             sprintf(socket_out, "%s%s %s %s\n",socket_out, "485solar-get", solar_get_version, "daemon: Running");  
           }else{
             sprintf(socket_out, "%s%s %s %s\n",socket_out, "485solar-get", solar_get_version, "daemon: Down");  
           }
           sn=strlen(socket_out);
           socket_out[sn] = '\0';
           if(start_daemon==1){
             if (send(socket2, socket_out, sn, 0) == -1){
               perror("send");
               exit(1);
              }
            }else{
              printf("%s", socket_out); 
            }
            socket_out[0] = '\0';
        }
        if(show_alarms==1){
           //channelHandle = FindChannelName(DeviceHandle[device_nr], "Fehler");
           channelHandle = FindChannelName(DeviceHandle[device_nr], "Error");
           ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[0], valuetext, 16, 5 );
           if(start_daemon==1){
             //sprintf(socket_out, "%s %s\n", "Fehler", valuetext);
             sprintf(socket_out, "%s %s\n", "Error", valuetext);  
             sn=strlen(socket_out);
             socket_out[sn] = '\0';
             if (send(socket2, socket_out, sn, 0) == -1){
               perror("send");
               exit(1);
             }
             if(show_debug==1) fprintf(stderr, "%s\n", "Output data to stdout");
             if(show_debug==1) fprintf(stderr, "%s\n", socket_out);
             socket_out[0] = '\0';
           }else{
             if(show_debug==1) fprintf(stderr, "%s\n", "Output data to stdout");
             //printf("\n%s %s\n", "Fehler", valuetext);
             printf("\n%s %s\n", "Error", valuetext);  
             //if(show_debug==1) fprintf(stderr, "%s %s\n", "Fehler", valuetext);
             if(show_debug==1) fprintf(stderr, "%s %s\n", "Error", valuetext);
           }
        }
        if(show_status==1){
          channelHandle = FindChannelName(DeviceHandle[device_nr], "Status");
          TxtCnt = GetChannelStatTextCnt( channelHandle );
          if (TxtCnt) {
            sprintf(socket_out, "%s\n", "All possible status texts for Status:");
            for(i=0;i<TxtCnt;i++) {
              GetChannelStatText(channelHandle, i, StatText, sizeof(StatText)-1);
              sprintf(socket_out, "%s(%2d): '%s'\n",socket_out,i,StatText);
            }
          }
          //channelHandle = FindChannelName(DeviceHandle[device_nr], "Fehler");
          channelHandle = FindChannelName(DeviceHandle[device_nr], "Error");
          TxtCnt = GetChannelStatTextCnt( channelHandle );
          if (TxtCnt) {
            //sprintf(socket_out, "%s%s\n",socket_out,"All possible status texts for Fehler:");
            sprintf(socket_out, "%s%s\n",socket_out,"All possible status texts for Error:");
            for(i=0;i<TxtCnt;i++) {
              GetChannelStatText(channelHandle, i, StatText, sizeof(StatText)-1);
              sprintf(socket_out, "%s(%2d): '%s'\n",socket_out,i,StatText);
            }
          }
          channelHandle = FindChannelName(DeviceHandle[device_nr], "Betriebsart");
          TxtCnt = GetChannelStatTextCnt( channelHandle );
          if (TxtCnt) {
            sprintf(socket_out, "%s%s\n",socket_out,"All possible status texts for Betriebsart:");
            for(i=0;i<TxtCnt;i++) {
              GetChannelStatText(channelHandle, i, StatText, sizeof(StatText)-1);
              sprintf(socket_out, "%s(%2d): '%s'\n",socket_out,i,StatText);
            }
          }
          sn=strlen(socket_out);
          socket_out[sn] = '\0';
          if(start_daemon==1){
            if (send(socket2, socket_out, sn, 0) == -1){
              perror("send");
              exit(1);
            }
          }else{
            printf("%s", socket_out);  
          }
          socket_out[0] = '\0';
        }
        if(show_events==1){
         for(i=0; i < 31; i++) {
           if(strcmp(event_set[i],"dummy") == 0) {
             if(show_debug==1) fprintf(stderr, "%s\n", "dummy, set output to 0");
             values[i]=0;
           } else if(strcmp(event_set[i],"Effcy") == 0) {
             if(show_debug==1) fprintf(stderr, "%s\n", "Efficiency not calcutated in SMA, set to 0");
             values[i]=0;
           } else {
             if(show_debug==1) fprintf(stderr, "GetChannelHandle for %s\n ", event_set[i]);
             channelHandle = FindChannelName(DeviceHandle[device_nr],  event_set[i]);
             if(show_debug==1) fprintf(stderr, "GetChannelValue for %u :", channelHandle);
             ires = GetChannelValue(channelHandle, DeviceHandle[device_nr], &values[i], valuetext, 16, 5 );
             if(show_debug==1) fprintf(stderr, "%f %s\n", values[i], valuetext);
           }
         }
         /* kohm to MOhm */
         values[7]=values[7]/1000;
   
         if(show_debug==1) fprintf(stderr, "%s\n", "Output data");
         if(values[0] > 0) {
           for(i=0; i < 31; i++) {
             if(start_daemon==1){
               sprintf(socket_out, "%s %lf ", socket_out, values[i]);
             }else{
               if(show_comments==1) printf("\n  %-15s", event_set[i]);
               printf(" %lf", values[i]);  
             }
           }
           if(start_daemon==1){
             sprintf(socket_out, "%s %s\n ", socket_out, "OK");
             sn=strlen(socket_out);
             socket_out[sn] = '\0';
             if (send(socket2, socket_out, sn, 0) == -1){
               perror("send");
               exit(1);
             }
             socket_out[0] = '\0';
           }else{
             if(show_comments==1) printf("\n  %-15s", "State");
             printf(" %s\n", "OK"); 
           }
         }
        }
        if((start_daemon==1)&&(exit_loop==1)){
          if(show_debug==1) fprintf(stderr, "%s\n", "Exit Daemon");
          exit_loop=1;
        }
        if(monitor==1){
          sleep(1);
        }else{
          if(start_daemon!=1){
            exit_loop=1;
          }
        }
        if((start_daemon==1)||(request_daemon==1)){
          if(show_debug==1) fprintf(stderr, "%s\n", "Close socket1");
          close(socket1);
          if(show_debug==1) fprintf(stderr, "%s\n", "Close socket2");
          close(socket2);
        }
      }
    } else {
      printf("no devices currently available\n");
    }
  }

  if(request_daemon==1){
    while (exit_loop==0) {
      if ((socket1 = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "%s\n", "Socket Error");
        exit(1);
      }else{
        if(show_debug==1) fprintf(stderr, "%s\n", "Trying to connect...");
        remote.sun_family = AF_UNIX;
        strcpy(remote.sun_path, SOCK_PATH);
        socket_len = strlen(remote.sun_path) + sizeof(remote.sun_family);
        if (connect(socket1, (struct sockaddr *)&remote, socket_len) == -1) {
          if(show_debug==1) fprintf(stderr, "%s\n", "No daemon found");
          exit(1);
        }else{
          if(show_debug==1) fprintf(stderr, "%s\n", "Connected to daemon");

          socket_command[0]='\0';
          if(show_alarms==1){
            strcpy(socket_command, "a");
          }
          if(show_events==1){
            strcpy(socket_command, "e");
          }
          if(show_status==1){
            strcpy(socket_command, "s");
          }
          if(show_info==1){
            strcpy(socket_command, "i");
          }
          if(show_data==1){
            strcpy(socket_command, "d");
          }
          if(daemon_exit==1){
            strcpy(socket_command, "x");
          }
          if(socket_command[0]=='\0'){
            if((show_debug==1)&&(show_help==0)) fprintf(stderr, "%s\n", "ERROR: No valid socket command");
          }else{
            sprintf(socket_command, "%s%i",socket_command, device_nr);
            if(show_debug==1) fprintf(stderr, "%s %s\n", "Send command to socket1:", socket_command);
            send(socket1, socket_command, 3, 0);
            if ((sn=recv(socket1, socket_out, 512, 0)) > 0) {
              socket_out[sn] = '\0';
              printf("%s", socket_out);
              if(show_debug==1) fprintf(stderr, "%s", socket_out);
              socket_out[0] = '\0';
            } else {
              if (sn < 0){
                perror("recv");
              }else{
                printf("Server closed connection\n");
                exit(1);
              }
            }
          }
          if(show_debug==1) fprintf(stderr, "%s\n", "Close socket1");
          close(socket1);
          if(show_debug==1) fprintf(stderr, "%s\n", "Close socket2");
          close(socket2);
          if(monitor==1){
            sleep(1);
          }else{
            exit_loop=1;
          }
        }
      }
    }
  }
                      
  if(init_comm==1){
     yasdiMasterShutdown( );
  }
   
  return return_code;
} 


Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest