NodeJS Sample Code

The following is an complete example of how to use all of the functionality of Zeus API in NodeJS.


// Notes: You can obtain your authaccount and authpass,
// when you have made login on ZeusWeb, going to URL: api_credentials.html
// - IP should have this format "192.168.1.1", or the domain name
// - If you want the client connect to a secure server (SSL), the SSL parameter must be True 

async function main () 
{
    var c =  new ZeusAPI.Client();
    // Get your yourAuthAccount and yourAuthpass by logging into ZeusWeb and going to api_credentials.html
    await c.init('IP of the Server', 'Port', false, 'yourAuthAccount', 'yourPassword', 'yourAuthpass');    

    if(c.Is_Created() == true)
    {
        c._Accept_Encoding = true;
        c._Content_Encoding = true;

        // Gets historian of selected channels (0, 1) of station "MyStation" since yesterday until now 
        c.GetHistorical('MyStation', new Date(2018,1,1), new Date(),[0,1]).then (ret=>{
            if(ret == null)
                console.log(c.Get_ZeusAPILastError());
            else{
                ret.forEach(function(element){
                    console.log("DATE: " + element.DateOfRecord + " REASON: " + element.Reason + " CHANNEL_ID: " + element.ChannelID + " VALUE: " + element.Value);
                });
            }

            //  Upload historians for station "MyStation" 
            let historical = [
                new ZeusAPI.Historical (new Date().toZeusStringms(), 0, "0", 10.2), 
                new ZeusAPI.Historical (new Date().toZeusStringms(), 1, "0", 15.5)
            ] ;
            c.SetHistorical ('MyStation', historical). then (ret=>{
                if(ret == false)
                    console.log(c.Get_ZeusAPILastError());
            });
        });

        // Gets the real time values / last known values of the station "MyStation" 
        c.GetLastKnownValues('MyStation').then (ret=>{
            if(ret == null) {
                console.log (c.Get_ZeusAPILastError());
            }else{
                
                let values = { };
                values['0'] = 52.5;
                values['1'] = 40.3;

                // Uploads the changed values to the Server
                c.SetLastKnownValues('MyStation', new ZeusAPI.LastKnownValues(ret.lastConnectionTime, ret.RSSI, ret.PowerSupply, values)).then (ret=>{
                    if (ret == false)
                        console.log(c.Get_ZeusAPILastError());
                });
            }        
        });

        // Gets alarms of all stations that user can see, since yesterday until now
        c.GetAlarm(ZeusAPI.add_minutes(new Date(), -1440), ZeusAPI.add_minutes(new Date(), 120)).then (ret=> {
            if(ret == null)
                console.log(c.Get_ZeusAPILastError());
            else{
                ret.forEach(function(element){
                    console.log("DATE: " + element.DateOfRecord + " STATION: " + element.StationID + " REASON: " + element.Reason + " TEXT: " + element.AlarmText);
                });
            }

            // Upload alarms for station "MyStation"
            let alarm = [
                new ZeusAPI.Alarms ('MyStation', new Date().toZeusStringms(), 1, 21, 'This is my high level alarm',''), 
                new ZeusAPI.Alarms ('MyStation', new Date().toZeusStringms(), 3, 22, 'his is my restored alarm','')
            ] ;
            c.SetAlarms (alarm). then (ret => {
                if(ret == false)
                    console.log(c.Get_ZeusAPILastError());
            });
        });

        // Send a message to the station "MyStation"
        await c.SendMessage ('MyStation','my message').then (ret=>{
            if(ret == false)
                console.log(c.Get_ZeusAPILastError());
        });

        // Gets enqueued messages pending of execution on server
        c.PendingMessages().then (ret=>{
            if(ret == null)
                console.log(c.Get_ZeusAPILastError());
            else{
                ret.forEach(function(element){
                    console.log('KEY: ' + element.Key + ' VALUE: ' + element.Value);
                });
            }
        });

        // Gets ProgrammableRelayPeriod of selected output (0) of station "MyStation"
        await c.GetConfigurationRelay('MyStation', 0).then (ret=>{
            if(ret == null)
                console.log(c.Get_ZeusAPILastError());
            else{
                ret.forEach(function(element){
                    console.log('WEEKDAYS: ' + element.Weekdays  + ' ACTIVATIONTIME: ' + element.ActivationTime + ' DURATION: ' + element.Duration);
                });               
            }
        });

        // Send a Relay Configuration to the station "MyStation" to output 0 '
        let myFrameTime = [new ZeusAPI.ProgrammableRelayPeriod(["L", "M", "X"], new Date(), 5), new ZeusAPI.ProgrammableRelayPeriod(["L", "M", "X", "J", "V", "S", "D"], new Date(), 20)];
        c.SetConfigurationRelay('MyStation', 0, myFrameTime). then (ret => {
            if(ret == false)
                console.log(c.Get_ZeusAPILastError());
        });

    }else{
        console.log(c.Get_ZeusAPILastError());
    }
}

main();