Example #1
0
        private static void initializeAgentContext(AgentContext context)
        {
            string login    = "******";
            string password = "******";

            VariableDefinition vd = new VariableDefinition("currentUser_login", singleString, true, false, "Имя пользователя", ContextUtils.GROUP_REMOTE);

            vd.setGetter(new DelegatedVariableGetter((con, def, caller, request) => authData));
            vd.setSetter(new DelegatedVariableSetter((con, def, caller, request, value) => { authData = value; }));
            vd.setGroup(ContextUtils.createGroup(ContextUtils.GROUP_REMOTE, "Учетные данные"));
            context.addVariableDefinition(vd);

            vd = new VariableDefinition("currentUser_pswd", singleString, true, false, "Пароль", ContextUtils.GROUP_REMOTE);
            vd.setGetter(new DelegatedVariableGetter((con, def, caller, request) => authData2));
            vd.setSetter(new DelegatedVariableSetter((con, def, caller, request, value) => { authData2 = value; }));
            vd.setGroup(ContextUtils.createGroup(ContextUtils.GROUP_REMOTE, "Учетные данные"));
            context.addVariableDefinition(vd);

            vd = new VariableDefinition("user_list", VTF_USERS, true, false, "Список пользователей", ContextUtils.GROUP_REMOTE);
            vd.setGetter(new DelegatedVariableGetter((con, def, caller, request) => VDT_USERS));
            vd.setSetter(new DelegatedVariableSetter((con, def, caller, request, value) => { VDT_USERS = value; }));
            vd.setGroup(ContextUtils.createGroup(ContextUtils.GROUP_REMOTE, "Данные с сервера"));
            context.addVariableDefinition(vd);

            vd = new VariableDefinition("device_list", VTF_DEVICES, true, false, "Список устройств", ContextUtils.GROUP_REMOTE);
            vd.setGetter(new DelegatedVariableGetter((con, def, caller, request) => VDT_DEVICES));
            vd.setSetter(new DelegatedVariableSetter((con, def, caller, request, value) => { VDT_DEVICES = value; }));
            vd.setGroup(ContextUtils.createGroup(ContextUtils.GROUP_REMOTE, "Данные с сервера"));
            context.addVariableDefinition(vd);

            Task <AuthResponse> signin = Connect(login, password);

            signin.Wait();
            if (signin.Result.status)
            {
                var records = new DataTable(singleString);
                records.addRecord().addString(login);
                context.setVariable("currentUser_login", records);

                records = new DataTable(singleString);
                records.addRecord().addString(password);
                context.setVariable("currentUser_pswd", records);
            }

            Task <UserListResponse> getUserList = GetUserList();

            getUserList.Wait();
            if (getUserList.Result.status && getUserList.Result.user_list != null)
            {
                var records = new DataTable(VTF_USERS);
                foreach (var user in getUserList.Result.user_list)
                {
                    records.addRecord().addString(user.login).addString(user.device_access);
                }
                context.setVariable("user_list", records);
            }

            Task <DeviceListResponse> getDevList = GetDeviceList();

            getDevList.Wait();
            var recordsdev = new DataTable(VTF_DEVICES);

            if (getDevList.Result.status && getDevList.Result.devices_list != null)
            {
                foreach (var device in getDevList.Result.devices_list)
                {
                    if (device.devEui == "323632317D37590E")
                    {
                        // запрос архива данных
                        Task <DataDeviceResponse> getDataArchive = GetDataFromDevice(device.devEui);
                        getDataArchive.Wait();
                        var dataDevice      = getDataArchive.Result.data_list;
                        var dataTableDevice = new DataTable(VTF_DATADEVICES);
                        foreach (var x in dataDevice)
                        {
                            var packet = DataPacket.Parse(x.data);
                            dataTableDevice.addRecord().addInt(packet.type).addInt(packet.charge).addDate(packet.dateTime).addInt(packet.temperature).addInt(packet.input1).addInt(packet.input2).addInt(packet.input3).addInt(packet.input4);
                        }

                        // добавляем всю информацию
                        recordsdev.addRecord().addString(device.devEui).addString(device.devName).addString(device.devType == null ? " " : device.devType).addDataTable(dataTableDevice);
                    }
                }
                context.setVariable("device_list", recordsdev);
            }

            var ed = new EventDefinition(E_EVENT, EFT_EVENT, "Agent Event", ContextUtils.GROUP_REMOTE);

            context.addEventDefinition(ed);

            context.addEventListener(E_EVENT, new DefaultContextEventListener <CallerController <CallerData> >(
                                         (anEvent) =>
            {
                Console.Out.WriteLine(("User has confirmed event with data: " + anEvent.ToString()));
            }));

            DataTable records2 = new DataTable(EFT_EVENT);

            records2.addRecord().addFloat((float)15);
            context.fireEvent(E_EVENT, EventLevel.INFO, records2 /*(float)(random.Next() * 1000000)*/);
            //context.start();
        }