Exemple #1
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            if (!Common.IsClientConnected)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotConnected"));
                context.Response.Write(collection);
                return;
            }
            try
            {
                Common.Client.UnsubscribeAll();
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("OK"));
                context.Response.Write(collection);
                return;
            }
            catch { }
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #2
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            string forget = context.Request["forget"];

            if (forget != null && forget == "true")
                Common.ClearSavedLogin();

            if (Common.IsUserLoggedIn)
            {
                Common.Client.UserLogout();
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("LoggedOut"));
                context.Response.Write(collection);
                return;
            }
            else
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #3
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            Common.CheckSavedLogin();

            if (Common.IsUserLoggedIn)
            {
                Objects.User user = Common.Client.UserCurrent;
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("LoggedIn"));
                ObjectCollection userCollection = new ObjectCollection();
                userCollection.Add(new StringValue("id"), new NumberValue(user.ID));
                userCollection.Add(new StringValue("username"), new StringValue(user.Username));
                userCollection.Add(new StringValue("name"), new StringValue(user.Name));
                userCollection.Add(new StringValue("description"), new StringValue(user.Description));
                userCollection.Add(new StringValue("permission"), new NumberValue(user.Permission));
                collection.Add(new StringValue("user"), userCollection);
                context.Response.Write(collection);
            }
            else
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
            }
        }
Exemple #4
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            string subscription = context.Request["subscription"];

            if (subscription == null || subscription == "")
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidSubscription"));
                context.Response.Write(collection);
                return;
            }

            Guid guid = Guid.Empty;
            try
            {
                guid = new Guid(subscription);
            }
            catch { }
            if (guid == Guid.Empty)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidSubscription"));
                context.Response.Write(collection);
                return;
            }

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
            try
            {
                Common.Client.Unsubscribe(guid);
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("OK"));
                context.Response.Write(collection);
                return;
            }
            catch { }
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #5
0
 public void ProcessRequest(HttpContext context)
 {
     context.Response.ContentEncoding = System.Text.Encoding.UTF8;
     context.Response.ContentType = "text/plain";
     context.Response.Expires = -1;
     if (Common.IsClientConnected)
         Common.Client.Close();
     Common.Client = null;
     ObjectCollection collection = new ObjectCollection();
     collection.Add(new StringValue("status"), new StringValue("OK"));
     context.Response.Write(collection);
 }
Exemple #6
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
            Objects.Device[] devices = Common.Client.DeviceList();
            if (devices == null)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
            else
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("OK"));
                ArrayCollection devicesCollection = new ArrayCollection();
                for (int i = 0; i < devices.Length; i++)
                {
                    Util.DeviceProfileAdapter profile = Util.DeviceProfileAdapter.CreateAdapter(devices[i].Profile);
                    if (!profile.IsValid) continue;
                    ObjectCollection deviceCollection = new ObjectCollection();
                    deviceCollection.Add(new StringValue("guid"), new StringValue(devices[i].Guid.ToString()));
                    deviceCollection.Add(new StringValue("name"), new StringValue(devices[i].Name));
                    deviceCollection.Add(new StringValue("description"), new StringValue(devices[i].Description));
                    deviceCollection.Add(new StringValue("type"), new StringValue(devices[i].Type.ToString()));
                    deviceCollection.Add(new StringValue("status"), new StringValue(devices[i].Status.ToString()));
                    deviceCollection.Add(new StringValue("building"), new StringValue(profile.Building.ToString()));
                    deviceCollection.Add(new StringValue("floor"), new NumberValue(profile.Floor));
                    deviceCollection.Add(new StringValue("x"), new NumberValue(profile.X));
                    deviceCollection.Add(new StringValue("y"), new NumberValue(profile.Y));
                    deviceCollection.Add(new StringValue("z"), new NumberValue(profile.Z));
                    devicesCollection.Add(deviceCollection);
                }
                collection.Add(new StringValue("devices"), devicesCollection);
                context.Response.Write(collection);
                return;
            }
        }
Exemple #7
0
        private void DoWork()
        {
            HttpContext context = Result.Context;
            HttpContext.Current = context;

            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }

            ClientEvent[] events = Common.Client.ClientEventGet(0);
            if (events == null)
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
            else
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("OK"));
                ArrayCollection eventArray = new ArrayCollection();
                for (int i = 0; i < events.Length; i++)
                {
                    ClientEvent e = events[i];
                    ObjectCollection ec = new ObjectCollection();
                    ec.Add(new StringValue("type"), new StringValue(e.Type.ToString()));
                    eventArray.Add(ec);
                }
                collection.Add(new StringValue("events"), eventArray);
                context.Response.Write(collection);
                return;
            }
        }
Exemple #8
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            // Get the request
            string old_password = context.Request["old_password"];
            string new_password = context.Request["new_password"];

            if (old_password == null || new_password == null || old_password.Length != Util.Hash.StringLength || new_password.Length != Util.Hash.StringLength)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidPassword"));
                context.Response.Write(collection);
                return;
            }

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }

            if (Common.Client.UserChangePassword(old_password, new_password))
            {
                Common.ClearSavedLogin();
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("PasswordChanged"));
                context.Response.Write(collection);
                return;
            }
            else
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidPassword"));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #9
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
            else
            {
                ResourceAdapter adapter = ResourceAdapter.CreateAdapter(Common.Client.ResourceGetIndex());
                if (!adapter.IsValid)
                {
                    ObjectCollection collection = new ObjectCollection();

                    collection.Add(new StringValue("status"), new StringValue("Error"));
                    context.Response.Write(collection);
                    return;
                }
                else
                {
                    ObjectCollection collection = new ObjectCollection();

                    collection.Add(new StringValue("status"), new StringValue("OK"));

                    ArrayCollection buildingsArray = new ArrayCollection();
                    for (int i = 0; i < adapter.Buildings.Length; i++)
                    {
                        Building building = adapter.Buildings[i];
                        ObjectCollection buildingCollection = new ObjectCollection();
                        buildingCollection.Add(new StringValue("guid"), new StringValue(building.Guid.ToString()));
                        buildingCollection.Add(new StringValue("name"), new StringValue(building.Name));
                        buildingCollection.Add(new StringValue("description"), new StringValue(building.Description));
                        buildingCollection.Add(new StringValue("longitude"), new NumberValue(building.Longitude));
                        buildingCollection.Add(new StringValue("latitude"), new NumberValue(building.Latitude));
                        buildingCollection.Add(new StringValue("altitude"), new NumberValue(building.Altitude));
                        Floor[] floors = building.Floors;
                        if (floors != null)
                        {
                            ArrayCollection floorsArray = new ArrayCollection();
                            for (int j = 0; j < floors.Length; j++)
                            {
                                Floor floor = floors[j];
                                ObjectCollection floorCollection = new ObjectCollection();
                                floorCollection.Add(new StringValue("name"), new StringValue(floor.Name));
                                floorCollection.Add(new StringValue("description"), new StringValue(floor.Description));
                                floorCollection.Add(new StringValue("level"), new NumberValue(floor.Level));
                                floorCollection.Add(new StringValue("resource"), new StringValue(floor.Resource.ToString()));
                                floorsArray.Add(floorCollection);
                            }
                            buildingCollection.Add(new StringValue("floors"), floorsArray);
                        }

                        buildingsArray.Add(buildingCollection);
                    }
                    collection.Add(new StringValue("buildings"), buildingsArray);
                    context.Response.Write(collection.ToString());

                    return;
                }
            }
        }
Exemple #10
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }

            ClientEvent[] events = Common.Client.ClientEventGet(0);
            if (events == null)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
            else
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("OK"));
                ArrayCollection eventArray = new ArrayCollection();
                for (int i = 0; i < events.Length; i++)
                {
                    ClientEvent e = events[i];
                    switch (e.Type)
                    {
                        case ClientEventType.DeviceCreation:
                        case ClientEventType.DeviceUpdate:
                        case ClientEventType.DeviceRemoval:
                        case ClientEventType.SubscriptionNotification:
                        case ClientEventType.SubscriptionUpdate:
                        case ClientEventType.UserUpdate:
                        case ClientEventType.UserLogout:
                            break;
                        default:
                            continue;
                    }
                    ObjectCollection eventCollection = new ObjectCollection();
                    eventCollection.Add(new StringValue("type"), new StringValue(e.Type.ToString()));
                    if (e.Timestamp != null)
                        eventCollection.Add(new StringValue("timestamp"), new DateValue(e.Timestamp));
                    if (e.Type == ClientEventType.DeviceCreation || e.Type == ClientEventType.DeviceUpdate)
                    {
                        if (e.Device == null || e.Device.Guid == Guid.Empty) continue;
                        Util.DeviceProfileAdapter profile = Util.DeviceProfileAdapter.CreateAdapter(e.Device.Profile);
                        if (!profile.IsValid) continue;
                        ObjectCollection deviceCollection = new ObjectCollection();
                        deviceCollection.Add(new StringValue("guid"), new StringValue(e.Device.Guid.ToString()));
                        deviceCollection.Add(new StringValue("name"), new StringValue(e.Device.Name));
                        deviceCollection.Add(new StringValue("description"), new StringValue(e.Device.Description));
                        deviceCollection.Add(new StringValue("type"), new StringValue(e.Device.Type.ToString()));
                        deviceCollection.Add(new StringValue("status"), new StringValue(e.Device.Status.ToString()));
                        deviceCollection.Add(new StringValue("building"), new StringValue(profile.Building.ToString()));
                        deviceCollection.Add(new StringValue("floor"), new NumberValue(profile.Floor));
                        deviceCollection.Add(new StringValue("x"), new NumberValue(profile.X));
                        deviceCollection.Add(new StringValue("y"), new NumberValue(profile.Y));
                        deviceCollection.Add(new StringValue("z"), new NumberValue(profile.Z));
                        eventCollection.Add(new StringValue("device"), deviceCollection);
                    }
                    else if (e.Type == ClientEventType.DeviceRemoval)
                    {
                        if (e.Device != null && e.Device.Guid != Guid.Empty)
                            eventCollection.Add(new StringValue("guid"), new StringValue(e.Device.Guid.ToString()));
                        else if (e.Guid != Guid.Empty)
                            eventCollection.Add(new StringValue("guid"), new StringValue(e.Guid.ToString()));
                        else
                            continue;
                    }
                    else if (e.Type == ClientEventType.UserUpdate)
                    {
                        if (e.User == null || !e.User.IsValid || e.User.ID != Common.Client.UserCurrent.ID)
                            continue;
                        ObjectCollection userCollection = new ObjectCollection();
                        userCollection.Add(new StringValue("id"), new NumberValue(e.User.ID));
                        userCollection.Add(new StringValue("username"), new StringValue(e.User.Username));
                        userCollection.Add(new StringValue("name"), new StringValue(e.User.Name));
                        userCollection.Add(new StringValue("description"), new StringValue(e.User.Description));
                        userCollection.Add(new StringValue("permission"), new NumberValue(e.User.Permission));
                        eventCollection.Add(new StringValue("user"), userCollection);
                    }
                    else if (e.Type == ClientEventType.UserLogout)
                    {
                        //no argument
                    }
                    else if (e.Type == ClientEventType.SubscriptionUpdate)
                    {
                        if (e.Subscription == null || e.Subscription.Guid == Guid.Empty || e.Subscription.Device == Guid.Empty)
                            continue;
                        ObjectCollection subscriptionCollection = new ObjectCollection();
                        subscriptionCollection.Add(new StringValue("guid"), new StringValue(e.Subscription.Guid.ToString()));
                        subscriptionCollection.Add(new StringValue("device"), new StringValue(e.Subscription.Device.ToString()));
                        subscriptionCollection.Add(new StringValue("status"), new StringValue(e.Subscription.Status.ToString()));
                        eventCollection.Add(new StringValue("subscription"), subscriptionCollection);
                    }
                    else if (e.Type == ClientEventType.SubscriptionNotification)
                    {
                        if (e.Data == null || e.Data.Device == Guid.Empty || e.Guid == Guid.Empty)
                            continue;
                        eventCollection.Add(new StringValue("guid"), new StringValue(e.Guid.ToString()));
                        ObjectCollection dataCollection = new ObjectCollection();
                        dataCollection.Add(new StringValue("device"), new StringValue(e.Data.Device.ToString()));
                        dataCollection.Add(new StringValue("timestamp"), new DateValue(e.Data.Timestamp));
                        dataCollection.Add(new StringValue("service"), new NumberValue(e.Data.Service));
                        dataCollection.Add(new StringValue("type"), new StringValue(e.Data.Type.ToString()));
                        Value value = null;
                        object o = Util.DataAdapter.Decode(e.Data);
                        if (o.GetType() == typeof(int))
                            value = new NumberValue((int)o);
                        else if (o.GetType() == typeof(double))
                            value = new NumberValue((double)o);
                        else if (o.GetType() == typeof(float))
                            value = new NumberValue((float)o);
                        else if (o.GetType() == typeof(bool))
                            value = new BoolValue((bool)o);
                        else if (o.GetType() == typeof(string))
                            value = new StringValue(o.ToString());
                        else if (o.GetType() == typeof(byte))
                            value = new NumberValue((byte)o);
                        else if (o.GetType() == typeof(byte[]))
                        {
                            ArrayCollection array = new ArrayCollection();
                            byte[] bytes = (byte[])o;
                            for (int j = 0; j < bytes.Length; j++)
                                array.Add(new NumberValue(bytes[j]));
                            value = array;
                        }
                        else
                            value = new NumberValue(o.ToString());
                        dataCollection.Add(new StringValue("value"), value);
                        eventCollection.Add(new StringValue("data"), dataCollection);
                    }
                    eventArray.Add(eventCollection);
                }
                collection.Add(new StringValue("events"), eventArray);
                context.Response.Write(collection);
                return;
            }
        }
Exemple #11
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            string device = context.Request["device"];
            string type = context.Request["type"];
            string value = context.Request["value"];

            if (device == null || device == "")
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidDevice"));
                context.Response.Write(collection);
                return;
            }

            if (value == null || value == "")
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidValue"));
                context.Response.Write(collection);
                return;
            }

            if (type == null || type == "")
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidValue"));
                context.Response.Write(collection);
                return;
            }
            Guid device_guid = Guid.Empty;
            try
            {
                device_guid = new Guid(device);
            }
            catch { }
            if (device_guid == Guid.Empty)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidDevice"));
                context.Response.Write(collection);
                return;
            }

            Objects.Data data = new Objects.Data { Device = device_guid, Timestamp = DateTime.Now };
            try
            {
                switch (type)
                {
                    case "Bool":
                        Util.DataAdapter.Encode(Convert.ToBoolean(value), data);
                        break;
                    case "String":
                        Util.DataAdapter.Encode(value, data);
                        break;
                    case "Double":
                        Util.DataAdapter.Encode(Convert.ToDouble(value), data);
                        break;
                    case "Float":
                        Util.DataAdapter.Encode(Convert.ToSingle(value), data);
                        break;
                    case "Int":
                        Util.DataAdapter.Encode(Convert.ToInt32(value), data);
                        break;
                    case "UInt":
                        Util.DataAdapter.Encode(Convert.ToUInt32(value), data);
                        break;
                    case "Long":
                        Util.DataAdapter.Encode(Convert.ToInt64(value), data);
                        break;
                    case "ULong":
                        Util.DataAdapter.Encode(Convert.ToUInt64(value), data);
                        break;
                    case "Short":
                        Util.DataAdapter.Encode(Convert.ToInt16(value), data);
                        break;
                    case "UShort":
                        Util.DataAdapter.Encode(Convert.ToUInt16(value), data);
                        break;
                    case "Byte":
                        Util.DataAdapter.Encode(Convert.ToByte(Convert.ToInt32(value)), data);
                        break;
                    case "Bytes":
                        {
                            string[] values = value.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
                            byte[] bytes = new byte[values.Length];
                            for (int i = 0; i < values.Length; i++)
                                bytes[i] = Convert.ToByte(Convert.ToInt32(values[i].Trim()) & 255);
                            Util.DataAdapter.Encode(bytes, data);
                        }
                        break;
                    default:
                        throw new InvalidCastException();
                }
            }
            catch
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidValue"));
                context.Response.Write(collection);
                return;
            }

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
            if(!Common.Client.UserHasPermission( Objects.User.PERMISSION_CONTROL))
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("PermissionDenied"));
                context.Response.Write(collection);
                return;
            }
            try
            {
                Common.Client.DataSend(data);
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("OK"));
                context.Response.Write(collection);
                return;
            }
            catch
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #12
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            // Get the request
            string uri = context.Request["uri"];
            string username = context.Request["username"];
            string password = context.Request["password"];
            string remember = context.Request["remember"];

            // Check input
            if (uri == null || !Uri.IsWellFormedUriString(uri, UriKind.Absolute))
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("InvalidUri"));
                context.Response.Write(collection);
                return;
            }
            if (!Util.Validator.IsUsername(username))
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("InvalidUsername"));
                context.Response.Write(collection);
                return;
            }
            if (password == null || password.Length != Util.Hash.StringLength)
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("InvalidPassword"));
                context.Response.Write(collection);
                return;
            }
            if (Common.IsUserLoggedIn)
            {
                Objects.User user = Common.Client.UserCurrent;
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("AlreadyLoggedIn"));
                ObjectCollection userCollection = new ObjectCollection();
                userCollection.Add(new StringValue("id"), new NumberValue(user.ID));
                userCollection.Add(new StringValue("username"), new StringValue(user.Username));
                userCollection.Add(new StringValue("name"), new StringValue(user.Name));
                userCollection.Add(new StringValue("description"), new StringValue(user.Description));
                userCollection.Add(new StringValue("permission"), new NumberValue(user.Permission));
                collection.Add(new StringValue("user"), userCollection);
                context.Response.Write(collection);
                return;
            }

            // Now try to connect to the server
            if (Common.Client == null || Common.Client.Uri.ToLower() != uri.ToLower())
            {
                if (Common.Client != null)
                {
                    try { Common.Client.Close(); }
                    finally { Common.Client = null; }
                }

                try { Common.Client = new Reactivity.Clients.Client(uri); }
                catch (Exception e)
                {
                    ObjectCollection collection = new ObjectCollection();
                    collection.Add(new StringValue("status"), new StringValue("ConnectionFailed"));
                    collection.Add(new StringValue("error"), new StringValue(e.ToString()));
                    context.Response.Write(collection);
                    return;
                }
            }

            // Now try to login
            try
            {
                if (Common.Client.UserLogin(username, password))
                {
                    Objects.User user = Common.Client.UserCurrent;
                    ObjectCollection collection = new ObjectCollection();
                    collection.Add(new StringValue("status"), new StringValue("LoggedIn"));
                    ObjectCollection userCollection = new ObjectCollection();
                    userCollection.Add(new StringValue("id"), new NumberValue(user.ID));
                    userCollection.Add(new StringValue("username"), new StringValue(user.Username));
                    userCollection.Add(new StringValue("name"), new StringValue(user.Name));
                    userCollection.Add(new StringValue("description"), new StringValue(user.Description));
                    userCollection.Add(new StringValue("permission"), new NumberValue(user.Permission));
                    collection.Add(new StringValue("user"), userCollection);
                    if (remember != null && remember == "true")
                    {
                        HttpCookie cookie_uri = new HttpCookie("reactivity_uri", uri.ToLower());
                        HttpCookie cookie_username = new HttpCookie("reactivity_username", username);
                        HttpCookie cookie_password = new HttpCookie("reactivity_password", password);
                        cookie_uri.Expires = DateTime.Now.AddMonths(1);
                        cookie_username.Expires = DateTime.Now.AddMonths(1);
                        cookie_password.Expires = DateTime.Now.AddMonths(1);
                        cookie_uri.Path = "/";
                        cookie_username.Path = "/";
                        cookie_password.Path = "/";
                        HttpContext.Current.Response.Cookies.Add(cookie_uri);
                        HttpContext.Current.Response.Cookies.Add(cookie_username);
                        HttpContext.Current.Response.Cookies.Add(cookie_password);
                    }
                    context.Response.Write(collection);
                }
                else
                {
                    ObjectCollection collection = new ObjectCollection();

                    collection.Add(new StringValue("status"), new StringValue("InvalidPassword"));
                    context.Response.Write(collection);
                }
                return;
            }
            catch(Exception e)
            {
                ObjectCollection collection = new ObjectCollection();

                collection.Add(new StringValue("status"), new StringValue("Error"));
                collection.Add(new StringValue("error"), new StringValue(e.ToString()));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #13
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            string device = context.Request["device"];
            string service = context.Request["service"];
            string start_date = context.Request["start_date"];
            string end_date = context.Request["end_date"];
            string type = context.Request["type"];

            // Parse input
            Guid device_guid = Guid.Empty;
            try
            {
                if (device != null && device != "")
                    device_guid = new Guid(device);
            }
            catch { }
            if (device_guid == Guid.Empty)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidDevice"));
                context.Response.Write(collection);
                return;
            }

            short service_short = 0;
            try
            {
                if (service != null && service != "")
                    service_short = Convert.ToInt16(service);
            }
            catch { }
            if (service_short == 0)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidService"));
                context.Response.Write(collection);
                return;
            }

            Objects.StatisticsType type_type;
            if(type == "1")
                type_type = Reactivity.Objects.StatisticsType.Minutely;
            else if(type == "2")
                type_type = Reactivity.Objects.StatisticsType.Hourly;
            else if(type == "3")
                type_type = Reactivity.Objects.StatisticsType.Daily;
            else if (type == "4")
                type_type = Reactivity.Objects.StatisticsType.Monthly;
            else
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidType"));
                context.Response.Write(collection);
                return;
            }

            DateTime start_date_datetime = new DateTime(1970, 1, 1);
            DateTime end_date_datetime = new DateTime(1970, 1, 1);
            try
            {
                if (start_date != null && start_date != "" && end_date != null && end_date != "")
                {
                    start_date_datetime = start_date_datetime.AddMilliseconds(Convert.ToDouble(start_date));
                    end_date_datetime = end_date_datetime.AddMilliseconds(Convert.ToDouble(end_date));
                    start_date_datetime = start_date_datetime.ToLocalTime();
                    end_date_datetime = end_date_datetime.ToLocalTime();
                    if (start_date_datetime > end_date_datetime)
                    {
                        ObjectCollection collection = new ObjectCollection();
                        collection.Add(new StringValue("status"), new StringValue("InvalidDateRange"));
                        context.Response.Write(collection);
                        return;
                    }
                }
                else
                {
                    ObjectCollection collection = new ObjectCollection();
                    collection.Add(new StringValue("status"), new StringValue("InvalidDateRange"));
                    context.Response.Write(collection);
                    return;
                }
            }
            catch
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidDateRange"));
                context.Response.Write(collection);
                return;
            }

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
            if (!Common.Client.UserHasPermission(Objects.User.PERMISSION_SUBSCRIBE))
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("PermissionDenied"));
                context.Response.Write(collection);
                return;
            }

            try
            {
                Objects.Statistics[] stats = Common.Client.StatisticsQuery(device_guid, service_short, start_date_datetime, end_date_datetime, type_type);
                if (stats != null)
                {
                    Array.Sort<Objects.Statistics>(stats);
                    Array.Reverse(stats);
                    ObjectCollection collection = new ObjectCollection();
                    collection.Add(new StringValue("status"), new StringValue("OK"));
                    collection.Add(new StringValue("date"), new StringValue(start_date_datetime.ToString()));
                    ArrayCollection array = new ArrayCollection();
                    for (int i = 0; i < stats.Length; i++)
                    {
                        ObjectCollection stat = new ObjectCollection();
                        stat.Add(new StringValue("device"), new StringValue(stats[i].Device.ToString()));
                        stat.Add(new StringValue("service"), new NumberValue(stats[i].Service));
                        stat.Add(new StringValue("type"), new NumberValue(Convert.ToByte(stats[i].Type)));
                        stat.Add(new StringValue("date"), new DateValue(stats[i].Date));
                        stat.Add(new StringValue("count"), new NumberValue(stats[i].Count));
                        stat.Add(new StringValue("value"), new NumberValue(stats[i].Value));
                        array.Add(stat);
                    }
                    collection.Add(new StringValue("statistics"), array);
                    context.Response.Write(collection);
                    return;
                }
            }
            catch { }
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
        }
Exemple #14
0
        public void ProcessRequest(HttpContext context)
        {
            // Common settings
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "text/plain";
            context.Response.Expires = -1;

            string device = context.Request["device"];
            string service = context.Request["service"];

            if (device == null || device == "")
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidDevice"));
                context.Response.Write(collection);
                return;
            }

            Guid device_guid = Guid.Empty;
            try
            {
                device_guid = new Guid(device);
            }
            catch { }
            if (device_guid == Guid.Empty)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("InvalidDevice"));
                context.Response.Write(collection);
                return;
            }

            if (!Common.IsUserLoggedIn)
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("NotLoggedIn"));
                context.Response.Write(collection);
                return;
            }
            if (!Common.Client.UserHasPermission(Objects.User.PERMISSION_SUBSCRIBE))
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("PermissionDenied"));
                context.Response.Write(collection);
                return;
            }
            try
            {
                Objects.Subscription subscription;
                if (service != null)
                    subscription = Common.Client.Subscribe(device_guid, Convert.ToInt16(service));
                else
                    subscription = Common.Client.Subscribe(device_guid);
                if (subscription != null)
                {
                    ObjectCollection collection = new ObjectCollection();
                    collection.Add(new StringValue("status"), new StringValue("OK"));
                    ObjectCollection subscriptionCollection = new ObjectCollection();
                    subscriptionCollection.Add(new StringValue("guid"), new StringValue(subscription.Guid.ToString()));
                    subscriptionCollection.Add(new StringValue("device"), new StringValue(subscription.Device.ToString()));
                    subscriptionCollection.Add(new StringValue("status"), new StringValue(subscription.Status.ToString()));
                    collection.Add(new StringValue("subscription"), subscriptionCollection);
                    context.Response.Write(collection);
                    return;
                }
            }
            catch { }
            {
                ObjectCollection collection = new ObjectCollection();
                collection.Add(new StringValue("status"), new StringValue("Error"));
                context.Response.Write(collection);
                return;
            }
        }