public static void Main(string[] args)
        {
            string apiKey            = "";
            string authToken         = "";
            string managedDeviceType = "demotest";
            string managedDeviceId   = "1234";

            if (string.IsNullOrEmpty(apiKey) || string.IsNullOrEmpty(authToken))
            {
                Console.Write("please Enter API Key : ");
                apiKey = Console.ReadLine();
                Console.WriteLine();
                Console.Write("Please Enter Auth Token : ");
                authToken = Console.ReadLine();
            }
            IBMWIoTP.ApiClient client = new IBMWIoTP.ApiClient(apiKey, authToken);
            client.Timeout = 5000;
            try {
                //Organization

                Console.WriteLine("get org info");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetOrganizationDetail()));

                //Bulk Operations

                Console.WriteLine("get all device list");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetAllDevices()));

                IBMWIoTP.RegisterDevicesInfo [] bulk = new IBMWIoTP.RegisterDevicesInfo[1];
                var info = new IBMWIoTP.RegisterDevicesInfo();
                info.deviceId   = "123qwe";
                info.typeId     = managedDeviceType;
                info.authToken  = "1qaz2wsx3edc4rfv";
                info.deviceInfo = new IBMWIoTP.DeviceInfo();
                info.location   = new IBMWIoTP.LocationInfo();
                info.metadata   = new {};
                bulk[0]         = info;

                Console.WriteLine("get Bulk reg ");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.RegisterMultipleDevices(bulk)));

                IBMWIoTP.DeviceListElement [] removeBulk = new IBMWIoTP.DeviceListElement[1];
                var del = new IBMWIoTP.DeviceListElement();
                del.deviceId = "123qwe";
                del.typeId   = managedDeviceType;

                removeBulk[0] = del;
                Console.WriteLine("get Bulk remove ");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.DeleteMultipleDevices(removeBulk)));

                //Device Types

                Console.WriteLine("GetAllDeviceTypes ");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetAllDeviceTypes()));

                DeviceTypeInfo devty = new DeviceTypeInfo();
                devty.classId    = "Gateway";
                devty.deviceInfo = new DeviceInfo();
                devty.id         = "gatewaypi";
                devty.metadata   = new {};
                Console.WriteLine("RegisterDeviceType");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.RegisterDeviceType(devty)));

                Console.WriteLine("UpdateDeviceType ");
                var u = new IBMWIoTP.DeviceTypeInfoUpdate();
                u.description = "test";
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.UpdateDeviceType("gatewaypi", u)));

                Console.WriteLine("GetDeviceType ");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceType("gatewaypi")));

                //Devices

                Console.WriteLine("ListDevices");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.ListDevices(managedDeviceType)));

                string newDeviceId = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                var    newdevice   = new RegisterSingleDevicesInfo();
                newdevice.deviceId   = newDeviceId;
                newdevice.authToken  = "testtest";
                newdevice.deviceInfo = new IBMWIoTP.DeviceInfo();
                newdevice.location   = new IBMWIoTP.LocationInfo();
                newdevice.metadata   = new {};
                Console.WriteLine("RegisterDevice");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.RegisterDevice("gatewaypi", newdevice)));

                var update = new IBMWIoTP.UpdateDevicesInfo();
                update.deviceInfo = new IBMWIoTP.DeviceInfo();
                Console.WriteLine("UpdateDeviceInfo");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.UpdateDeviceInfo("gatewaypi", newDeviceId, update)));

                Console.WriteLine("GetDeviceInfo");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceInfo("gatewaypi", newDeviceId)));

                Console.WriteLine("GetGatewayConnectedDevice");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetGatewayConnectedDevice("gatewaypi", newDeviceId)));

                Console.WriteLine("GetDeviceLocationInfo");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceLocationInfo("gatewaypi", newDeviceId)));

                Console.WriteLine("UpdateDeviceLocationInfo");
                var loc = new IBMWIoTP.LocationInfo();
                loc.accuracy         = 1;
                loc.measuredDateTime = DateTime.Now.ToString("o");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.UpdateDeviceLocationInfo("gatewaypi", newDeviceId, loc)));

                Console.WriteLine("GetDeviceManagementInfo");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceManagementInfo(managedDeviceType, managedDeviceId)));

                Console.WriteLine("UnregisterDevice");
                client.UnregisterDevice("gatewaypi", newDeviceId);


                Console.WriteLine("DeleteDeviceType ");
                client.DeleteDeviceType("gatewaypi");


                Console.WriteLine("AddDeviceDiagLogs");
                var log = new IBMWIoTP.LogInfo();
                log.message  = "test";
                log.severity = 1;

                client.AddDeviceDiagLogs(managedDeviceType, managedDeviceId, log);

                Console.WriteLine("GetAllDiagnosticLogs");
                var logResult = client.GetAllDiagnosticLogs(managedDeviceType, managedDeviceId);
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(logResult));



                Console.WriteLine("GetDiagnosticLog");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDiagnosticLog(managedDeviceType, managedDeviceId, logResult[0]["id"])));

                Console.WriteLine("DeleteDiagnosticLog");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.DeleteDiagnosticLog(managedDeviceType, managedDeviceId, logResult[0]["id"])));

                Console.WriteLine("ClearAllDiagnosticLogs");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.ClearAllDiagnosticLogs(managedDeviceType, managedDeviceId)));



                Console.WriteLine("AddErrorCode");
                var err = new IBMWIoTP.ErrorCodeInfo();
                err.errorCode = 0;
                err.timestamp = "";
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.AddErrorCode(managedDeviceType, managedDeviceId, err)));

                Console.WriteLine("GetDeviceErrorCodes");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceErrorCodes(managedDeviceType, managedDeviceId)));

                Console.WriteLine("ClearDeviceErrorCodes");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.ClearDeviceErrorCodes(managedDeviceType, managedDeviceId)));

                Console.WriteLine("Problem ");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceConnectionLogs(managedDeviceType, managedDeviceId)));

                Console.WriteLine("data usage ");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDataUsage("2016", "2016", false)));


                Console.WriteLine("Service Status");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetServiceStatus()));

                Console.WriteLine("lastEvents");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetLastEvents(managedDeviceType, managedDeviceId)));

                Console.WriteLine("lastEvents of type");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetLastEventsByEventType(managedDeviceType, managedDeviceId, "test")));


                Console.WriteLine("GetAllDeviceManagementRequests");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetAllDeviceManagementRequests()));

                Console.WriteLine("InitiateDeviceManagementRequest");
                IBMWIoTP.DeviceMgmtparameter [] param = new IBMWIoTP.DeviceMgmtparameter[1];
                IBMWIoTP.DeviceMgmtparameter    p     = new IBMWIoTP.DeviceMgmtparameter();
                p.name   = "rebootAfter";
                p.value  = "100";
                param[0] = p;
                IBMWIoTP.DeviceListElement [] deviceList = new IBMWIoTP.DeviceListElement[1];
                IBMWIoTP.DeviceListElement    ele        = new IBMWIoTP.DeviceListElement();
                ele.typeId    = managedDeviceType;
                ele.deviceId  = managedDeviceId;
                deviceList[0] = ele;
                var response = client.InitiateDeviceManagementRequest("device/reboot", param, deviceList);             //new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<dynamic>(jsonResponse);
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(response));
                string id = response["reqId"];

                Console.WriteLine("GetDeviceManagementRequest");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceManagementRequest(id)));

                Console.WriteLine("GetDeviceManagementRequestStatus");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceManagementRequestStatus(id)));

                Console.WriteLine("GetDeviceManagementRequestStatus");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceManagementRequestStatus(id, ele.typeId, ele.deviceId)));

                Console.WriteLine("DeleteDeviceManagementRequest");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.DeleteDeviceManagementRequest(id)));

                Console.WriteLine("weather");
                Console.WriteLine(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(client.GetDeviceLocationWeather(managedDeviceType, managedDeviceId)));
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            Console.Write("Press any key to exit . . . ");
            Console.ReadKey();
        }
Example #2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("============================ IBM WatsonIoTP Sample ============================");

            bool isSync = false;
            List <ConectedDevice> devices = new List <ConectedDevice>();

            Console.WriteLine("Gateway connectd device management sample");
            Console.WriteLine("Check Out Following Device manage action on connected device ");
            Console.WriteLine("1.Reboot");
            Console.WriteLine("2.Reset");
            Console.WriteLine("3.Firmware download and update");
            Console.WriteLine("4.Any key to exit");
            Console.Write("Please enter your choice :");
            int val = int.Parse(Console.ReadLine());

            DeviceInfo simpleDeviceInfo = new DeviceInfo();

            simpleDeviceInfo.description         = "My device";
            simpleDeviceInfo.deviceClass         = "My device class";
            simpleDeviceInfo.manufacturer        = "My device manufacturer";
            simpleDeviceInfo.fwVersion           = "Device Firmware Version";
            simpleDeviceInfo.hwVersion           = "Device HW Version";
            simpleDeviceInfo.model               = "My device model";
            simpleDeviceInfo.serialNumber        = "12345";
            simpleDeviceInfo.descriptiveLocation = "My device location";

            GatewayManagement gwMgmtClient = new GatewayManagement("GatewayCreds.txt", isSync);

            gwMgmtClient.deviceInfo    = simpleDeviceInfo;
            gwMgmtClient.mgmtCallback += (string reqestId, string responseCode) => {
                Console.WriteLine("req Id:" + reqestId + "	responseCode:"+ responseCode);
            };
            gwMgmtClient.actionCallback += (string reqestId, string action) => {
                Console.WriteLine("req Id:" + reqestId + "	Action:"+ action + " called");
                if (action == "reboot")
                {
                    gwMgmtClient.sendResponse(reqestId, DeviceManagement.RESPONSECODE_ACCEPTED, "");

                    Thread.Sleep(2000);
                    gwMgmtClient.disconnect();

                    Console.WriteLine("disconnected");
                    Thread.Sleep(5000);

                    Console.WriteLine("Re connected");
                    gwMgmtClient.connect();

                    gwMgmtClient.managedGateway(4000, true, true);
                }
                if (action == "reset")
                {
                    gwMgmtClient.sendResponse(reqestId, DeviceManagement.RESPONSECODE_FUNCTION_NOT_SUPPORTED, "");
                }
            };
            gwMgmtClient.fwCallback += (string action, DeviceFirmware fw) => {
                if (action == "download")
                {
                    gwMgmtClient.setState(DeviceManagement.UPDATESTATE_DOWNLOADING);
                    Console.WriteLine("Start downloading new Firmware from " + fw.uri);
                    Thread.Sleep(2000);
                    Console.WriteLine("completed Download");
                    gwMgmtClient.setState(DeviceManagement.UPDATESTATE_DOWNLOADED);
                }
                if (action == "update")
                {
                    gwMgmtClient.setUpdateState(DeviceManagement.UPDATESTATE_IN_PROGRESS);
                    Console.WriteLine("Start Updating new Firmware ");
                    Thread.Sleep(2000);
                    Console.WriteLine("Updated new Firmware ");
                    gwMgmtClient.setUpdateState(DeviceManagement.UPDATESTATE_SUCCESS);
                }
            };
            gwMgmtClient.connect();
            Console.WriteLine("Manage");
            gwMgmtClient.managedGateway(4000, true, true);
            //Connected device
            ConectedDevice device1 = new ConectedDevice("testgwdev", "1234", gwMgmtClient);

            device1.isActionEnabled    = true;
            device1.isFramewareEnabled = true;
            device1.canReboot          = true;
            device1.canReset           = false;
            devices.Add(device1);
            gwMgmtClient.managedDevice(device1.deviceType, device1.deviceId, 4000, device1.isActionEnabled, device1.isFramewareEnabled);
            gwMgmtClient.connectedDeviceActionCallback += (string devicetype, string deviceid, string reqestId, string action) => {
                Console.WriteLine(devicetype + ": " + deviceid + ":" + reqestId + ":" + action);
                var itm = devices.Find(x => x.deviceId == deviceid);
                if (itm is ConectedDevice)
                {
                    if (action == GatewayManagement.ACTION_REBOOT)
                    {
                        if (itm.canReboot)
                        {
                            gwMgmtClient.sendResponse(reqestId, DeviceManagement.RESPONSECODE_ACCEPTED, "", itm.deviceType, itm.deviceId);
                            Thread.Sleep(2000);
                            gwMgmtClient.managedDevice(itm.deviceType, itm.deviceId, 4000, itm.isActionEnabled, itm.isFramewareEnabled);
                        }
                        else
                        {
                            gwMgmtClient.sendResponse(reqestId, DeviceManagement.RESPONSECODE_FUNCTION_NOT_SUPPORTED, "", itm.deviceType, itm.deviceId);
                        }
                    }
                    if (action == GatewayManagement.ACTION_RESET)
                    {
                        if (itm.canReset)
                        {
                            gwMgmtClient.sendResponse(reqestId, DeviceManagement.RESPONSECODE_ACCEPTED, "", itm.deviceType, itm.deviceId);
                        }
                        else
                        {
                            gwMgmtClient.sendResponse(reqestId, DeviceManagement.RESPONSECODE_FUNCTION_NOT_SUPPORTED, "", itm.deviceType, itm.deviceId);
                        }
                    }
                }
            };



            gwMgmtClient.connectedDevicefwActionCallback += (string devicetype, string deviceid, string action, DeviceActionReq req) => {
                ConectedDevice itm = devices.Find(x => x.deviceId == deviceid);
                switch (action)
                {
                case GatewayManagement.FIRMWARE_ACTION_INFO:
                    itm.infoHandler(req);
                    break;

                case GatewayManagement.FIRMWARE_ACTION_DOWNLOAD:
                    itm.downloadHandler(req);
                    break;

                case GatewayManagement.FIRMWARE_ACTION_UPDATE:
                    itm.updatedHandler(req);
                    break;

                default:

                    break;
                }
            };

            IBMWIoTP.ApplicationClient appClient = new ApplicationClient("AppCreds.txt");
            IBMWIoTP.ApiClient         client    = appClient.GetAPIClient();
            client.Timeout = 5000;
            IBMWIoTP.DeviceMgmtparameter [] param = new IBMWIoTP.DeviceMgmtparameter[1];
            IBMWIoTP.DeviceMgmtparameter    p     = new IBMWIoTP.DeviceMgmtparameter();
            p.name   = "uri";
            p.value  = "https://raw.githubusercontent.com/ibm-watson-iot/iot-python/master/CHANGES.txt";
            param[0] = p;
            IBMWIoTP.DeviceListElement [] deviceList = new IBMWIoTP.DeviceListElement[1];
            IBMWIoTP.DeviceListElement    ele        = new IBMWIoTP.DeviceListElement();
            ele.typeId    = "testgw";
            ele.deviceId  = "1212";
            deviceList[0] = ele;

            switch (val)
            {
            case 1: client.InitiateDeviceManagementRequest("device/reboot", param, deviceList);
                break;

            case 2: client.InitiateDeviceManagementRequest("device/factoryReset", param, deviceList);
                break;

            case 3: client.InitiateDeviceManagementRequest("firmware/download", param, deviceList);
                Thread.Sleep(15000);
                client.InitiateDeviceManagementRequest("firmware/update", new IBMWIoTP.DeviceMgmtparameter[0], deviceList);
                break;

            default:
                gwMgmtClient.disconnect();
                break;
            }
        }