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; } }
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; } } }
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; } }
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; } }
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; } }