1

Create notification for custom sensors

Topic: Create notification for custom sensors

I am trying to create a notification for a custom sensor, but I get a message back 'invalid input'.  Attached please find my code and an image of the console log.

Best regards
Annanda

  • Create notification for custom sensors
Post's attachments

Attachment icon CreateNotification.js 3.12 kb, 255 downloads since 2020-10-06 

2

Create notification for custom sensors

Re: Create notification for custom sensors

Also, I need to get all the notifications for a resource. Attached please find my code. (I see a similar question was asked August 2018, which is unanswered - subject: "search notification by Name")

I am also using the Javascript API.

Post's attachments

Attachment icon getAllNotifications.js 1.11 kb, 261 downloads since 2020-10-06 

3

Create notification for custom sensors

Re: Create notification for custom sensors

tyretrack wrote:

I am trying to create a notification for a custom sensor, but I get a message back 'invalid input'.  Attached please find my code and an image of the console log.

Best regards
Annanda

Hello Annanda!

First you need :
load library unitSensors  - sess.loadLibrary("unitSensors")
Add flags for unit item- wialon.item.Unit.dataFlag.sensors

( Here you can find all possible libres and flags which needs to upload for using items https://forum.gurtam.com/viewtopic.php? … 3#p161813)

for example,

var flags_res = wialon.item.Item.dataFlag.base
var flags_units = wialon.item.Item.dataFlag.base | wialon.item.Unit.dataFlag.sensors

sess.loadLibrary("unitSensors");
sess.loadLibrary("resourceNotifications");

    sess.updateDataFlags( // load items to current session
        [{type: "type", data: "avl_resource", flags: flags_res, mode: 0}, // Items (avl_resource) specification
        {type: "type", data: "avl_unit", flags: flags_units, mode: 0}], // Items (avl_unit) specification
        function (code) { // updateDataFlags callback
            if (code) { msg(wialon.core.Errors.getErrorText(code)); return; } // exit if error code 
            // get loaded 'avl_resource's items with edit notification access 
            var res = wialon.util.Helper.filterItems(sess.getItems("avl_resource"),
                wialon.item.Resource.accessFlag.editNotifications);
            for (var i = 0; i< res.length; i++) // construct Select list using found resources
                $("#res").append("<option value='"+ res[i].getId() +"'>"+ res[i].getName() +"</option>");
            $("#res").change( function(){ getZones(this.value); } ); // bind acion to select change
            getZones($("#res").val()); // get geofences for selected resource

            var units = sess.getItems("avl_unit"); // get loaded 'avl_unit's items
            for (var i = 0; i< units.length; i++) // construct Select list using found units
                $("#units").append("<option value='"+ units[i].getId() +"'>"+ units[i].getName() +"</option>");
    });

Regarding notification parameters it seems quite ok, but it's better to specife more correctly,
For example, parameter timezone (tz) you can change to value with DST time, for example timezone UTC+3 with no DST tz = 134228528, is more correct value. 
Also you can recheck parameters here in doc , or just create the same notification on monitoring site and look through request in console.

Diana Cheley
Wialon Hosting Expert
Gurtam
4

Create notification for custom sensors

Re: Create notification for custom sensors

tyretrack wrote:

Also, I need to get all the notifications for a resource. Attached please find my code. (I see a similar question was asked August 2018, which is unanswered - subject: "search notification by Name")

I am also using the Javascript API.

Yes, it's correct way. First you searh (get) resources - getItems("avl_resource"), then for each item(resource) execute getNotifications()

Diana Cheley
Wialon Hosting Expert
Gurtam
5

Create notification for custom sensors

Re: Create notification for custom sensors

If I am trying to create the notification this way, I get "Invalid input" in the console. What should the value for sensor_type be for a custom sensor?

trg: {// Notification trigger
                t: "sensor_value",
                p: { sensor_type: "22", sensor_name_mask: "sensorA1", type: "1", lower_bound: "2.5", upper_bound: "4.5" } // trigger parameters ( geozones ids, control type)
            }

6

Create notification for custom sensors

Re: Create notification for custom sensors

"Yes, it's correct way. First you searh (get) resources - getItems("avl_resource"), then for each item(resource) execute getNotifications()"

I've got a couple of notifications set up in Wialon, but using my code to get all the notifications I get "null" back.

7

Create notification for custom sensors

Re: Create notification for custom sensors

tyretrack wrote:

If I am trying to create the notification this way, I get "Invalid input" in the console. What should the value for sensor_type be for a custom sensor?

trg: {// Notification trigger
                t: "sensor_value",
                p: { sensor_type: "22", sensor_name_mask: "sensorA1", type: "1", lower_bound: "2.5", upper_bound: "4.5" } // trigger parameters ( geozones ids, control type)
            }

emtpy value for any sensor type - sensor_type: ""
custom sensor - sensor_type: "custom"

Diana Cheley
Wialon Hosting Expert
Gurtam
8

Create notification for custom sensors

Re: Create notification for custom sensors

tyretrack wrote:

"Yes, it's correct way. First you searh (get) resources - getItems("avl_resource"), then for each item(resource) execute getNotifications()"

I've got a couple of notifications set up in Wialon, but using my code to get all the notifications I get "null" back.

You mean the code in file  getAllNotifications.js  , yes?

Diana Cheley
Wialon Hosting Expert
Gurtam
9

Create notification for custom sensors

Re: Create notification for custom sensors

Yes, the code in  getAllNotifications.js.

The notifications that I've set up in Wialon is also for cutom sensors, and I figured if I can get these notifications I will be able to see exactly what parameter values Wialon uses for custom sensors, but for this code:
                var res = sess.getItems("avl_resource");
                for (var i = 0; i < res.length; i++) { // construct Select list using found resources   
                    let allNotifications = res(i).getNotifications();
                }
I get "null" back for "allNotifications ".

10

Create notification for custom sensors

Re: Create notification for custom sensors

I still can't create a notification, "Invalid input" in the console. I've changed the sensor_type to "custom" and also tried it with an empty string. I've also tried the sensor_name_mask with an empty string, but to no avail.  Below the values in the object.  What else could be wrong?

Object
act: Array(1)
0: {t: "message", p: {…}}
length: 1
__proto__: Array(0)
cdt: 10
cp: 3600
fl: 1
la: "en"
ma: 0
mast: 0
mmtd: 3600
mpst: 0
n: "js code new"
sch: {f1: 0, f2: 0, t1: 0, t2: 0, m: 0, …}
ta: 1602242951
td: 1602847751
trg:
p:
lower_bound: "2.5"
sensor_name_mask: "sensorA1"
sensor_type: "custom"
type: "1"
upper_bound: "4.5"
__proto__: Object
t: "sensor_value"
__proto__: Object
txt: "Test Notification Text"
tz: 10800
un: 21523455
__proto__: Object

11

Create notification for custom sensors

Re: Create notification for custom sensors

Hello!

There are not correct parameters for notification
Please look after you code the request svc=resource/update_notification is send with the following parameters :

params: {"ma":0,"fl":1,"tz":10800,"la":"en","act":[{"t":"message","p":{}}],"sch":{"f1":0,"f2":0,"t1":0,"t2":0,"m":0,"y":0,"w":0},"txt":"Test Notification Text","mmtd":3600,"cdt":10,"mast":0,"mpst":0,"cp":3600,"n":"js code new","un":19294541,"ta":1602248226,"td":1602853026,"trg":{"t":"sensor_value","p":{"sensor_type":"22","sensor_name_mask":"sensorA1","type":"1","lower_bound":"2.5","upper_bound":"4.5"}},"itemId":930849,"id":0,"callMode":"create"}

The server returns the error error: 4 reason: "VALIDATE_PARAMS_ERROR"  = Invalid input ,  so some parameters are wrong

Please look the example with correct parameters below and try to compare it and use .

{"n":"sensor example","ta":1602201600,"td":0,"tz":134217728,"la":"ru","ma":0,"sch":{"f1":0,"f2":0,"t1":0,"t2":0,"m":0,"y":0,"w":0,"fl":0},"ctrl_sch":{"f1":0,"f2":0,"t1":0,"t2":0,"m":0,"y":0,"w":0,"fl":0},"un":[21728414],"trg":{"t":"sensor_value","p":{"sensor_type":"","sensor_name_mask":"*sensos1*","lower_bound":2.5,"upper_bound":3.5,"prev_msg_diff":0,"merge":1,"type":0}},"d":"","act":[{"t":"message","p":{"name":"sensor example","url":"","color":"","blink":0}}],"txt":"some text.","fl":0,"mast":0,"mpst":0,"cdt":0,"mmtd":3600,"cp":3600,"id":132,"itemId":930849,"callMode":"update"}
Diana Cheley
Wialon Hosting Expert
Gurtam
12

Create notification for custom sensors

Re: Create notification for custom sensors

Hello Diana

My apologies for bugging you again.

I still can not get the function "getNotifications()" to work.  Below new code that I've tried (I've tried less flags):

function getAllNotifications() {
    var sess = wialon.core.Session.getInstance(); // get instance of current Session
    var flags = wialon.item.Resource.dataFlag.base | wialon.item.Resource.dataFlag.notifications;

    sess.loadLibrary("resourceNotifications"); // load Notification Library       

    sess.updateDataFlags( // load items to current session
        ({ type: "type", data: "avl_resource", flags: flags, mode: 0 }),
        function (code) { // updateDataFlags callback
            if (code) { console.log(window.wialon.core.Errors.getErrorText(code)); return; } // exit if error code
            var res = sess.getItems("avl_resource");
            for (var i = 0; i < res.length; i++) {    
                let allNotifications = res(i).getNotifications();
                console.log(allNotifications);
            }
        });
}

Do I perhaps need a parameter for the "getNotifications()" function?

In one of your earlier posts you've mentioned that for the "createNotification()" function I could just create the same notification on the monitoring site and look through request in console. Can you tell me how to this? However, I have now managed to create a notification, (thank you so much for your help), but looking through a request in the console could be a great tool for me to use in future. I just don't know how to look for this in the Wialon code. If you can guide me through this, I would really appreciate it.

The reason why my "createNotification()" function didn't work was because I've used strings(text) for the lower-bound and upper-bound parameter values, while it should have been Integers or Floats, but according to the documentation (here in doc), I thought it should be strings(text)

        "lower_bound":<text>,        /* sensor value from */
        "merge":<text>,            /* similar sensors: 0 - calculate separately, 1 - sum up values */
        "prev_msg_diff":<text>,         /* this flag allows to form boundaries for the current value according to previous value(prev) in such a way:
        "sensor_name_mask":<text>,    /* sensor name mask */
        "sensor_type":<text>,        /* sensor type */
        "type":<text>,            /* trigger: 0 - in specified range, 1 - out of specified range */
        "upper_bound":<text>

Thank you in advance!

Annanda

13

Create notification for custom sensors

Re: Create notification for custom sensors

Hi Annanda!

I will check your code more carefully and soon provide you the result.
Please wait.

Diana Cheley
Wialon Hosting Expert
Gurtam
14

Create notification for custom sensors

Re: Create notification for custom sensors

Hi Diana

Have you found time to look at my code?

Is it possible that maybe the notifications are not set up correctly in Wialon?

Best regards
Annanda

15

Create notification for custom sensors

Re: Create notification for custom sensors

tyretrack wrote:

Hi Diana

Have you found time to look at my code?

Is it possible that maybe the notifications are not set up correctly in Wialon?

Best regards
Annanda

Hi Annanda,

please look example here how to create notification sensor control https://sdk.wialon.com/playground/dHg2zier/3

The easy way is to look through API parameters in notification when create on monitoring site and just take for obj all parameters except  :
id":0,"itemId":93,"callMode":"create"

Regarding getting all notifications  there is a problem to get undefined result in your code because when you use call-back function in method sess.updateDataFlags  it needs to do something with the result get in callback (all notifications) as method is asynchronous call, for example to show it, to print and etc.  Some of way is using more complicated code (to return result) like with Promise / Async/await
Or you just can get result our-side call-back

For example, this function will return mass (massive with resource and its notifications) when executed.

 function getAllNotifications() { 
        
        var sess = window.wialon.core.Session.getInstance();
        var flags = window.wialon.item.Item.dataFlag.base | window.wialon.item.Resource.dataFlag.base | window.wialon.item.Item.dataFlag.messages | window.wialon.item.Resource.dataFlag.notifications;

        sess.loadLibrary("resourceNotifications"); // load Notification Library 
        var mass = [];
        sess.updateDataFlags( // load items to current session
            [{ type: "type", data: "avl_resource", flags: flags, mode: 1 }], // Items (avl_resource) specification
            function (code) { // updateDataFlags callback
                if (code) { console.log(window.wialon.core.Errors.getErrorText(code)); return; } // exit if error code 
                
           
            });
              // get loaded 'avl_resource's items with edit notification access 
                var res = sess.getItems("avl_resource");
                var mass = [] //result massive
                for (var i = 0; i < res.length; i++) { // construct Select list using found resources    
                    var obj = {resName: res[i].getName(), allNotif: res[i].getNotifications()}
                    mass.push(obj);
                }
                console.log(mass);
                return mass;
    }
Diana Cheley
Wialon Hosting Expert
Gurtam
16

Create notification for custom sensors

Re: Create notification for custom sensors

Hi Diana

I am afraid still no joy!

Below the result in the console with your example (I've also attached as an image)

Array(2)
0: {resName: "tyrecare", allNotif: null}
1: {resName: "tyretrack1", allNotif: null}
length: 2
__proto__: Array(0)

Could it be that there is something wrong with the notifications I've set up in Wialon?  (There are just a few)

For what it's worth, I've also tried your example with a Promise, but to no avail:

async function getAllNotifications() {

    var sess = window.wialon.core.Session.getInstance();
    var flags = window.wialon.item.Item.dataFlag.base | window.wialon.item.Resource.dataFlag.base | window.wialon.item.Item.dataFlag.messages |
    window.wialon.item.Resource.dataFlag.notifications;

    let result = await new Promise((resolve, reject) => {
        sess.updateDataFlags(
           ({ type: "type", data: "avl_resource", flags: flags, mode: 1 }),
            (code) => {
                if (code) {
                    reject({ type: API_ERROR, error: code });
                    return;
                }
               
                var res = sess.getItems("avl_resource");
                var mass = () //result massive
                for (var i = 0; i < res.length; i++) { // construct Select list using found resources   
                    var obj = { resName: res(i).getName(), allNotif: res(i).getNotifications() }
                    mass.push(obj);
                }
                resolve(mass);
            },
        );
    });

    console.log(result);
}

Please advise!

Regards
Annanda

  • Create notification for custom sensors
17

Create notification for custom sensors

Re: Create notification for custom sensors

Annanda, please look PM message

Diana Cheley
Wialon Hosting Expert
Gurtam