more G-Labs products

Author Topic: ACPUPSD support  (Read 9018 times)

December 20, 2014, 04:54:01 AM
Reply #15

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I got the HG code working correctly.  I had 2 issues.  The first was missing the capital "S" as mentioned before.  The second was that my web browser wouldn't let me specify the capitalization I needed (it would change "power.off" to "Power.Off" on its own).  After I figured this out, the script is working great.

December 20, 2014, 05:15:35 PM
Reply #16

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
Here is the first cut at a HG script for receiving apcupsd events.  It will email the recipient as specified in the settings and display a notification of the event.  When/if I figure out how to parse the "apcaccess status", I'll update the script and post it in a app submission topic.  If anyone wants to take the lead on the parsing and post the code, I'd greatly appreciate!

December 21, 2014, 02:47:41 AM
Reply #17

drpepper

  • *
  • Information
  • Newbie
  • Posts: 23
Good work! Can you post the output of your apcaccess status and any detail about what you'd like to see from it? Looks like different models have different data available, but all models have at least UPSNAME, STATUS, UPSMODE so those would be good starters.

Something like this works for example output. If you need a status widget that's just another line or two away.
Code: [Select]

var proc = new System.Diagnostics.Process {
    StartInfo = new System.Diagnostics.ProcessStartInfo {
        FileName = "apcaccess",
        Arguments = "status",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
  };
  proc.Start();
  string model = "", status = "";
 
  while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
string[] output = ((string)line).Split(':');
 
switch (output[0].Trim()) {
    case "MODEL":
      model = output[1];
      break;
    case "STATUS":
      status = output[1];
      break;
    }
  }
  Program.Notify(model,status);


EDIT: Wow it hates my indenting preferences. And there is no syntax highlighting.  :P

December 21, 2014, 04:00:59 AM
Reply #18

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
This is what was output right before the battery died during some functionality testing:

Code: [Select]
pi@raspberrypi ~ $ apcaccess status
APC      : 001,035,0904
DATE     : 2014-12-20 18:36:49 -0800
HOSTNAME : raspberrypi
VERSION  : 3.14.10 (13 September 2011) debian
UPSNAME  : BE550G
CABLE    : USB Cable
DRIVER   : USB UPS Driver
UPSMODE  : Stand Alone
STARTTIME: 2014-12-20 11:42:41 -0800
MODEL    : Back-UPS ES 550
STATUS   : ONBATT
LINEV    : 000.0 Volts
LOADPCT  :   0.0 Percent Load Capacity
BCHARGE  : 013.0 Percent
TIMELEFT :   5.1 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
SENSE    : Medium
LOTRANS  : 092.0 Volts
HITRANS  : 139.0 Volts
ALARMDEL : No alarm
BATTV    : 12.0 Volts
LASTXFER : Low line voltage
NUMXFERS : 1
XONBATT  : 2014-12-20 12:32:04 -0800
TONBATT  : 21887 seconds
CUMONBATT: 21887 seconds
XOFFBATT : N/A
STATFLAG : 0x07060010 Status Flag
SERIALNO : <deleted>
BATTDATE : 2009-06-14
NOMINV   : 115 Volts
NOMBATTV :  12.0 Volts
FIRMWARE : 843.K1 .D USB FW:K1
END APC  : 2014-12-20 18:36:51 -0800

I would think things that would be useful would be:
STATUS   : ONBATT
LOADPCT  :   0.0 Percent Load Capacity
BCHARGE  : 013.0 Percent
TIMELEFT :   5.1 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes

There might be others, but these are the minimum that seem crucial.

December 21, 2014, 04:11:03 AM
Reply #19

drpepper

  • *
  • Information
  • Newbie
  • Posts: 23
Cool, then the code I posted in the last post should work fine, just add switch cases for those items. Wasn't sure what your goal of that particular data was. Like polling apcaccess once a minute and working that load data into the Analyze graphs of HG or just polling for status/bcharge and displaying it in a widget, etc.

December 21, 2014, 04:39:08 AM
Reply #20

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I hadn't thought about using charts, but that might be nice for some things.  I'll think about that and let you know.

December 21, 2014, 05:35:56 AM
Reply #21

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
This is the new code.  I'd love to see a widget if you know how to code one.

December 21, 2014, 03:49:25 PM
Reply #22

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
When I originally thought of incorporating this into HG, my original thought was to have email notification (done), automatic shutdown and restart or the Raspi (done), and a widget of some kind that showed pertinent information (TBD).  What I originally envisioned was something that looked like the original app embedded in HG.  I have no capability to do that nor know if that is realistic in any way.  A simple widget that's updated on a periodic basis showing numbers is ok, but this is what I was originally thinking about.  Alas, I only have limited coding skills (self taught on current languages) and no experience with anything HTML related.


http://sourceforge.net/projects/apcupsd/

http://a.fsdn.com/con/app/proj/apcupsd/screenshots/59404.jpg
http://a.fsdn.com/con/app/proj/apcupsd/screenshots/59406.jpg
http://a.fsdn.com/con/app/proj/apcupsd/screenshots/59408.jpg
http://a.fsdn.com/con/app/proj/apcupsd/screenshots/68863.jpg


I doubt something close to that can be generated, but a single widget with some of that info would be great.  Maybe even a second widget with the events.  Not sure how feasible that would be, so I'm ok with a very simple display widget with just the critical stuff.  I didn't see where in the data it provides info on last outage, last outage duration, how many outages, total duration, etc.  That must be stored by the program based on every recorded event.  Of course, every event is known by HG now too, so perhaps that would be a matter of storing the delta times and counting outages within HG.
« Last Edit: December 21, 2014, 04:00:59 PM by bkenobi »

January 09, 2015, 06:38:22 PM
Reply #23

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I modified my script slightly since it was sending several notification emails for every power event.  I still have 2 outstanding issues that I may need help with though.

1) When power goes out, the Raspi is shut down correctly and reboots correctly.  However, when shutting down, it appears to kill HG too quickly for an email notification to be sent out.  I'm not sure how to address this since apcupsd sends the system shutdown command and stops all programs on it's own.  So maybe I can add a wait statement in the system kill script somehow.  Also, when the system comes back up I don't know what event to use to send a notification.

2) I don't have a clue how to make a widget.  I checked the HG widget tool out, but between json and html, I'm stuck before I start.  I only really want to display a few items that I believe are available either directly in apcaccess or by logging events that HG already sees.  But, actually creating the widget to display this basic text is not something I'm clear about.

January 09, 2015, 09:19:46 PM
Reply #24

drpepper

  • *
  • Information
  • Newbie
  • Posts: 23
When do the When.SystemStarted and When.SystemStopping events get hit? Not sure if apcupsd shuts down the services, or which signal it sends to the process (SIGTERM/SIGKILL) so check when those events get hit. You could also try moving the homegenie to the end of the line in /etc/rc*.d/ (keeping in mind dependencies of course)

http://www.homegenie.it/docs/doxy/d4/dd7/class_home_genie_1_1_automation_1_1_scripting_1_1_events_helper.html#afcf0d379d8dd2da00c2adf1c3c9996f3

January 10, 2015, 03:24:37 AM
Reply #25

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I've used Linux for years, but primarily as an end user.  I've only worked with the administering minimally (primarily with set top boxes/tivo/raspi).  That being said, I don't understand the difference between the different /etc/rc*.d directories.  If you can elaborate where to look and what to change, I'd gladly look into changing it.

January 10, 2015, 03:37:32 AM
Reply #26

drpepper

  • *
  • Information
  • Newbie
  • Posts: 23
/etc/rc0.d would be the shutdown runlevel, when the system gets the call to shutdown it will stop all of the services in the order assigned in this directory, such as /etc/rc0.d/K20homegenie. By changing that to say K99homegenie you'd be putting homegenie at the end of the line of things to be stopped (unrealistic because the network service would stop before homegenie and it wouldn't be able to send a notification)

First, please test and ensure the When.System events don't meet your needs, that would be the best way to do things. Changing the rc.d order of things can be risky since the system sets the order based on dependencies. This should be solved in HG/mono for a more universal solution (i.e. a system or application exit event handler)

January 10, 2015, 04:33:03 AM
Reply #27

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I tried adding the following code to the end of my script:

Code: [Select]
When.SystemStopping( () =>
{
  Net.SendMessage( recipients, "HomeGenie shutting down", "notification");
  // returning true will route this event to other listeners
  return true;
});

Unfortunately, it did not send anything.  I shut down the service and got nothing from this function.  I then shut down and restarted the machine.  I did receive an email after reboot, but that was based on my IP check function detecting the machine coming back online.  I didn't see anything from the SystemStopping code.  I then added SystemStarting and didn't see anything either.  Perhaps I'm not using the function correctly, but I don't know how since I simply copied the cod off the documentation.

January 10, 2015, 05:18:37 AM
Reply #28

drpepper

  • *
  • Information
  • Newbie
  • Posts: 23
This may be due to HG's startup script running HomeGenie.exe with mono instead of mono-service (which would handle and relay the SIGTERM correctly, which is what actually fires those events). You can test this by stopping HG, then
Code: [Select]
$ cd /usr/local/bin/homegenie && sudo mono-service --debug HomeGenie.exe
then if things look okay and your program is set up to catch the SystemStopping event

Code: [Select]
$ kill `sudo cat /tmp/HomeGenie.exe.lock`

January 10, 2015, 09:36:25 PM
Reply #29

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I know very little about mono.  I tried the command and it looks like mono-service is not avalable:
Code: [Select]
pi@raspberrypi ~ $ cd /usr/local/bin/homegenie
pi@raspberrypi /usr/local/bin/homegenie $ sudo mono-service --debug HomeGenie.exe
sudo: mono-service: command not found