1

Creating new Unit Groups

Тема: Creating new Unit Groups

Could you please assist with creating and updating Unit Groups?

I’ve used the following code to create and update unit Groups:

        let prms = { "creatorId": userId, "name": groupName, "dataFlags": 1 };
        let remote = wialon.core.Remote.getInstance();
        remote.remoteCall('core/create_unit_group', prms);

and

        let prms = { "itemId": groupId, "name": groupName, "dataFlags": 1 };
        let remote = wialon.core.Remote.getInstance();
        remote.remoteCall('item/update_name', prms);


Unfortunately, I get strange behavior. Most of the time everything works as expected but sometimes I get the same group created more than once.  So I figured if I could check if a group has been created I could control this behavior so I tried this code, but it fails altogether:

async function submitNewUnitsGroup(groupName) {
    try {       
        let session = global.session;
        let userId = session.getCurrUser().getId();     

        let flag = wialon.item.UnitGroup.base.dataFlag.base;
       
        let unitGroupCreated = await session.createUnitGroup(userId, groupName, flag,
            function (error, data) {
                if (error) {
                    console.log("unitGroupJS: submitNewUnitsGroupForm - ERROR " + error + " (" + wialon.core.Errors.getErrorText(error)) + ")";
                    return false;
                }
                else {
                    console.log("unitGroupJS: submitNewUnitsGroupForm - SUCCESS (" + data + " new group added.)");
                    return true;
                }
            }
        );
        return unitGroupCreated;
    } catch (e) {
        console.error('tyretrackJS: addNewUnitsGroup - ERROR: ', e);
    }
}

I would appreciate it if you could help me.