protected override Device CreateDeviceData()
        {
            Debug.Print("Initializing device data");
            
            Device dd = new Device()
            {
                deviceClass = new DeviceClass()
                {
                    name = Resources.GetString(Resources.StringResources.DeviceClass),
                    version = Resources.GetString(Resources.StringResources.Version)
                },
                id = new Guid(Resources.GetBytes(Resources.BinaryResources.guid)),
                key = Resources.GetString(Resources.StringResources.Key),
                name = Resources.GetString(Resources.StringResources.DeviceName),
                network = new DeviceNetwork()
                {
                    name = Resources.GetString(Resources.StringResources.Network),
                    description = Resources.GetString(Resources.StringResources.NetworkDesc)
                },
                status = DeviceStatus.OK,
                
            };
            
            Debug.Print("Done initializing device data.");

            
            
            return dd;
        }
Beispiel #2
0
        /// <summary>
        /// Gets a command
        /// </summary>
        /// <param name="device">Device data of the device that is getting a command</param>
        /// <returns>Command data structure or null if there are no pending commands</returns>
        /// <remarks>
        /// This method returns all the commands that are waiting at the server since last GetCommand call. It returns immediately, regardless if there are commands to execute or not.
        /// </remarks>
        public DeviceCommand GetCommand(Device device)
        {
            if (CommandQueue.Count != 0)
            {
                return (DeviceCommand)CommandQueue.Dequeue();
            }
            string TimeString = LastTimeStamp.ToString("yyyy-MM-ddTHH\\%3Amm\\%3Ass.fff000");
            string Url = CloudUrl + DeviceCommand + device.id.ToString() + GetCommandName + TimeString;

            byte[] bts = null;
            WebResponse resp = MakeRequest(Url, null, GetMethod, device.id.ToString(), device.key);

            if (resp.ContentType.IndexOf(ContentType) >= 0)
            {

                if (resp.ContentLength > 0)
                {
                    using (Stream rs = resp.GetResponseStream())
                    {
                        bts = new byte[(int)rs.Length];
                        rs.Read(bts, 0, bts.Length);
                        rs.Close();
                    }
                }
            }
            resp.Close();

            if (bts != null)
            {
                JsonFormatter js = new JsonFormatter();
                object o = js.FromJson(bts, typeof(DeviceCommand));

                DeviceCommand dc = o as DeviceCommand;
                if (dc != null && dc.status.IsNullOrEmpty())
                {
                    return dc;
                }
                ArrayList al = o as ArrayList;
                if (al != null)
                {
                    if (al.Count == 0) return null;

                    for (int x = 0; x < al.Count; ++x)
                    {
                        dc = (DeviceCommand)al[x];
                        if (dc.status.IsNullOrEmpty())
                        {
                            CommandQueue.Enqueue(dc);
                        }
                    }
                    DeviceCommand ldc = (DeviceCommand)al[al.Count - 1];
                    LastTimeStamp = ldc.timestamp;
                    //LastTimeStamp += new TimeSpan(0, 0, 0, 0, 1);

                    return CommandQueue.Count == 0 ? null : (DeviceCommand)CommandQueue.Dequeue();
                }
            }
            return null;
        }
 /// <summary>
 /// Initializes the device
 /// </summary>
 /// <returns>True if the initialization was successfull</returns> 
 /// <remarks>
 /// This function should be called by implementers before any calls to other DeveiceEngine's functions.
 /// </remarks>
 virtual public bool Init()
 {
     IsConnected = false;
     IsOnline = false;
     ReconnectCount = DefaultReconnectCount;
     //DcClient = client;
     if (Initializing != null)
     {
         if (!Initializing(this, new EventArgs())) return false;
     }
     //PreInit();
     DeviceData = CreateDeviceData();
     CreateEquipment();
     //PostInit();
     if (Initialized != null)
     {
         if (!Initialized(this, new EventArgs())) return false;
     }
     return true;
 }
        /// <summary>
        /// Updates command status
        /// </summary>
        /// <param name="device">Device data of the device that is updating a command status</param>
        /// <param name="CommandId">ID of the command to be updated</param>
        /// <param name="status">Status of the command</param>
        /// <returns>True if the status update was successfull; false - otherwise</returns>
        /// <remarks>
        /// The devices are using this method to notify the server of command completion and its result. 
        /// </remarks>
        public bool UpdateCommandStatus(Device device, string CommandId, CommandStatus status)
        {
            //bool rv = false;
            HttpWebResponse resp = MakeRequest(
                CloudUrl + DeviceCommand + device.id.ToString() + CommandStatus + CommandId,
                status,
                PutMethod,
                device.id.ToString(),
                device.key) as HttpWebResponse;

            return resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.NoContent;
        }
        /// <summary>
        /// Gets a command
        /// </summary>
        /// <param name="device">Device data of the device that is getting a command</param>
        /// <param name="onlyUnprocessed">Get commands with empty status only</param>
        /// <returns>Command data structure or null if there are no pending commands</returns>
        /// <remarks>
        /// This method returns all the commands that are waiting at the server since last GetCommand call. It returns immediately, regardless if there are commands to execute or not.
        /// </remarks>
        public DeviceCommand GetCommand(Device device, bool onlyUnprocessed = true)
        {
            if (CommandQueue.Count != 0)
            {
                return (DeviceCommand)CommandQueue.Dequeue();
            }
            string TimeString = UriExtensions.EscapeDataString(LastTimeStamp);
            string Url = CloudUrl + DeviceCommand + device.id.ToString() + PollCommandName + TimeString + "&waitTimeout=0";

            byte[] bts = null;
            WebResponse resp = MakeRequest(Url, null, GetMethod, device.id.ToString(), device.key);

            if (resp.ContentType.IndexOf(ContentType) >= 0)
            {

                if (resp.ContentLength > 0)
                {
                    using (Stream rs = resp.GetResponseStream())
                    {
                        bts = new byte[(int)rs.Length];
                        rs.Read(bts, 0, bts.Length);
                        rs.Close();
                    }
                }
            }
            resp.Close();

            if (bts != null)
            {
                JsonFormatter js = new JsonFormatter();
                object o = js.FromJson(bts, typeof(DeviceCommand));

                DeviceCommand dc = o as DeviceCommand;
                if (dc != null && (!onlyUnprocessed || StringExtensions.IsNullOrEmpty(dc.status)))
                {
                    return dc;
                }
                ArrayList al = o as ArrayList;
                if (al != null)
                {
                    if (al.Count == 0) return null;

                    for (int x = 0; x < al.Count; ++x)
                    {
                        dc = (DeviceCommand)al[x];
                        if (!onlyUnprocessed || StringExtensions.IsNullOrEmpty(dc.status))
                        {
                            CommandQueue.Enqueue(dc);
                        }
                    }
                    DeviceCommand ldc = (DeviceCommand)al[al.Count - 1];
                    LastTimeStamp = ldc.timestamp;

                    return CommandQueue.Count == 0 ? null : (DeviceCommand)CommandQueue.Dequeue();
                }
            }
            return null;
        }
        /// <summary>
        /// Sends a notification
        /// </summary>
        /// <param name="device">Device data of the device that is sending the notification</param>
        /// <param name="notification">Notification to be sent</param>
        /// <returns>True if the notification succeeds; false - otherwise</returns>
        /// <remarks>
        /// This method can be used by devices to send notifications.
        /// </remarks>
        public bool PostNotification(Device device, INotification notification)
        {
            //bool rv = false;

            HttpWebResponse resp = MakeRequest(
                CloudUrl + DeviceCommand + device.id.ToString() + NotificationCommand,
                notification.Data,
                PostMethod,
                device.id.ToString(),
                device.key) as HttpWebResponse;
            return resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Created;
                //resp.Close();
        }
 /// <summary>
 /// Registers a device
 /// </summary>
 /// <param name="device">Device data structure</param>
 /// <returns>True, if registration succeeds; false - otherwise</returns>
 /// <remarks>
 /// This method is called by device to register it at the server.
 /// </remarks>
 public bool SetDevice(Device device)
 {
     //bool rv = false;
     HttpWebResponse resp = MakeRequest(CloudUrl + DeviceCommand + device.id.ToString(), 
         device, 
         PutMethod, 
         device.id.ToString(), 
         device.key) as HttpWebResponse;
     return resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.NoContent;
 }
Beispiel #8
0
        // Obsolete!
        /// <summary>
        /// Polls command for execution
        /// </summary>
        /// <param name="device">Device data of the device that is polling a command</param>
        /// <returns>Command data structure or null if there are no commands</returns>
        /// <remarks>
        /// This method returns the next command from the command queue. If there are no commands in the queue, it waits for ~30 seconds to receive a new command.
        /// </remarks>
        public DeviceCommand PollCommand(Device device)
        {
            if (CommandQueue.Count != 0)
            {
                return (DeviceCommand)CommandQueue.Dequeue();
            }
            //string TimeString = time.Year.ToString() + "-"
            //    + time.Month.ToString() + "-"
            //    + time.Day.ToString() + "T"
            //    + time.Hour.ToString() + ":"
            //    + time.Minute.ToString() + ":"
            //    + time.Second.ToString();
            string Url = CloudUrl + DeviceCommand + device.id.ToString() + PollCommandName;// +TimeString;

            byte[] bts = null;
            WebResponse resp = MakeRequest(Url, null, GetMethod, device.id.ToString(), device.key);

            if (resp.ContentType.IndexOf(ContentType) > 0)
            {

                if (resp.ContentLength > 0)
                {
                    using (Stream rs = resp.GetResponseStream())
                    {
                        bts = new byte[(int)rs.Length];
                        rs.Read(bts, 0, bts.Length);
                        rs.Close();
                    }
                }
            }
            resp.Close();

            if (bts != null)
            {
                JsonFormatter js = new JsonFormatter();
                object o = js.FromJson(bts, typeof(DeviceCommand));

                DeviceCommand dc = o as DeviceCommand;
                if (dc != null)
                {
                    return dc;
                }
                ArrayList al = o as ArrayList;
                if (al != null)
                {
                    if (al.Count == 0) return null;

                    for (int x = 1; x < al.Count; ++x)
                    {
                        CommandQueue.Enqueue(al[x]);
                    }
                    return (DeviceCommand)al[0];
                }
            }
            return null;
        }