mycontroller-org / mycontroller-v1-legacy

The Open Source Controller
http://www.MyController.org
Apache License 2.0
148 stars 90 forks source link

Just a suggestion: for Operation #235

Open jkandasa opened 8 years ago

jkandasa commented 8 years ago

Reported by @cimba007 at https://forum.mysensors.org/topic/4431/mycontroller-0-0-3-alpha2-released/15 image

Send notification would include all further kinds of notifications you might include but grouping this is not that important. The current way it is is already very easy to understand so a change might not be necessary.

Send payload might be better split into three different points as it would be very clear from the beginning what you are dealing with.

cimba007 commented 8 years ago

Hello, on my current configuration the rules system does not show any meaningfull values on "autocomplete".

image

Althrough I have one node with 2 sensors which I would like to check against some threshold.

image

Am I doing something wrong?! I can't select operations based on these 2 sensor values neither.

cimba007 commented 8 years ago

I noticed the option to enable/disable operations via operations is not possible. With this I would very conveniently enable/disable my notification mechanism.

image

Rule and Timer is possible, Operations is missing.

cimba007 commented 8 years ago

I tried a workaround with scripts but I have no idea how to list all operations with their correspondig ID so I could use a script to disable the specific operation via scripting.

Could you provide a very small example on how to list all operations with ID and how to disable/enable an operation? Thank you in advance!

I think I have an working example for displaying all operations:

Script:

var HashMap = Java.type('java.util.HashMap');
var map = new HashMap();

var myoperations2 = mcApi.operation().getAllRaw(map);

Template:

${( myoperations2.toString() )}

Result:

image

jkandasa commented 8 years ago

@cimba007

Rule and Timer is possible, Operations is missing.

Operations inside operation might create recursive loop, hence I disable that option.

Could you provide a very small example on how to list all operations with ID and how to disable/enable an operation?

To list operations on Dashboard.

Script: JavaScript
var myImports = new JavaImporter(java.io, java.lang, java.util);

with(myImports) {
  var filters = new HashMap();
  filters.put("orderBy", "name");
  filters.put("order", "asc");
  filters.put("pageLimit", new Long(10));
  //Get operations data
  var operations = mcApi.operation().getAll(filters);
}
Template:
<table class="table table-hover table-bordered table-striped mc-table">
  <thead>
    <th>{{ 'STATUS' | translate }}</th>
    <th>{{ 'ID' | translate }}</th>
    <th>{{ 'NAME' | translate }}</th>
    <th>{{ 'OPERATION' | translate }}</th>
    <th>{{ 'LAST_EXECUTION' | translate }}</th>
  </thead>
  <tbody>
    <#list operations.data as item>
      <tr ui-sref="operationsAddEdit({id: ${item.id} })">
        <td class="text-center">
          <#if item.enabled>
            <i class="text-success fa fa-circle fa-lg" uib-tooltip="{{ 'ENABLED' | translate }}"></i>
          <#else>
            <i class="text-danger fa fa-circle-o fa-lg" uib-tooltip="{{ 'DISABLED' | translate }}"></i>
          </#if>
        </td>
        <td>${item.id}</td>
        <td>${(item.name)!}</td>
        <td ng-bind-html="'${(item.operationString)!}' | mcResourceRepresentation"></td>
        <td><span uib-tooltip="{{ ${(item.lastExecution?c)!"Never"} | date:mchelper.cfg.dateFormat:mchelper.cfg.timezone}}" tooltip-placement="left" am-time-ago="${(item.lastExecution?c)!"Never"}"></span></td>
      </tr>
    </#list>
  </tbody>
</table>

To enable/disable list of operations:

JavaScript

Script bindings: {options:{name:"operation-name", enable:false}} Important: It is important to pass Script bindings when executing this script. In the binding you can include your operation name to filter. Please note in Mycontroller all filters are working with case sensitive. In the name filed you can put any words where operation name contains.

For example I want to enable or disable following operations,

In the above names I have notification word in common. So I can use this name in filter to get these operations exactly.

To disable:

{options:{name:"notification", enable:false}}

To enable:

{options:{name:"notification", enable:true}}

var myImports = new JavaImporter(java.io, java.lang, java.util);

with(myImports) {
  var filters = new HashMap();
  var names = new ArrayList();
  names.add(options.name);

  filters.put("orderBy", "name");
  filters.put("order", "asc");
  filters.put("pageLimit", new Long(10));
  filters.put("name", names)

  //Get operations data
  var operations = mcApi.operation().getAll(filters);

  var operationIds = new ArrayList();
  for(var count =0 ; count<operations.data.length;count++){
    //You can do some filter here
     operationIds.add(operations.data[count].id);
  }

  if(options.enable){
   mcApi.operation().enableIds(operationIds);
  }else{
   mcApi.operation().disableIds(operationIds);
  }
}
cimba007 commented 8 years ago

Thank you very much. This looks much more refined as my little "hack" :dancer:

I think this will do exactly what I want.

Maybe I should take my time to sum up what I intent to do with the possibility to enable/disable operations.

My Plan:

I plan to send pushbullet notifications depending on my sensor threshold. This should be pretty straightforward and is currently working as expected.

To disable these notifications at night it would be possible to disable every rulde which leads to a notification. I thought it would be much easyier to disable the notification, leaving all rules intact.

To disable the operation I would then use a simple rule and an V_ARMED switch which would execute my custom script to disable the notification operation.

I am not totally sure if it is flawless but having the possibility to disable operations from script is very nice and I think will help me a lot.

@jkandasa: I think your example is very elaborated and very good. If you/I close this issue the information would be "Lost" for other users. Maybe you could add this and the statistics example from earlyer to the documentation?! It would help a lot to get a quickstart for custom widgets for other users!

cimba007 commented 8 years ago

I now successfully implemented enabling/disabling operation via switch thanks to your excellent example code:

image

image

jkandasa commented 8 years ago

@cimba007 great! This is awesome!

@jkandasa: I think your example is very elaborated and very good. If you/I close this issue the information would be "Lost" for other users. Maybe you could add this and the statistics example from earlyer to the documentation?! It would help a lot to get a quickstart for custom widgets for other users!

Yes, you are right I will create entry on http://forum.mycontroller.org and close this one.

To disable these notifications at night it would be possible to disable every rulde which leads to a notification. I thought it would be much easyier to disable the notification, leaving all rules intact.

Yes, for that reason I gave an option for operations also. We may use operations on timer, rule(multiple places). When disabling operation directly never get executed by any rules, timers.

If you have only requirement that to disable operations on night time, simply schedule a cron, other other timer to execute enable/disable operations script. Which is easier than manual switch. but it is up to you.

cimba007 commented 8 years ago

If you create the entry on the forum please add a link to the forum post to the documentation too! http://www.mycontroller.org/#/documents/user/0.0.3.Alpha2.html => Script examples

People will most likely have a look at the documentation first so this would ensure this valuable information can be easylie found.

jkandasa commented 8 years ago

updated in forum, http://forum.mycontroller.org/topic/46/control-display-operations-via-script

If you create the entry on the forum please add a link to the forum post to the documentation too! http://www.mycontroller.org/#/documents/user/0.0.3.Alpha2.html => Script examples

Sure, I will add this on 0.0.3.Final release document.

cimba007 commented 8 years ago

I even thought if it would be usefull to include the example in the actual mycontroller version as EXAMPLE_SCRIPT with the disabled flag set. It would be even easier to find a quickstart to scripting ;-)