private Objects.Data GetRandomData(short service) { Objects.Data data = new Objects.Data { Device = device.Guid, Timestamp = DateTime.Now, Service = service }; Util.DataAdapter.Encode(random.NextDouble() * fluctuation + value - (fluctuation / 2), data); return data; }
void timer_Elapsed(object sender, ElapsedEventArgs e) { if (device == null || client == null) return; if (value == 0 && fluctuation == 0) return; try { if (device.Type == Util.DeviceType.ACNode) { double voltage = random.NextDouble() * 1.0 + 220 - (1.0 / 2); double current = random.NextDouble() * fluctuation + value - (fluctuation / 2); if (!relay) current = 0.0; double power = voltage * current; Objects.Data voltaged = new Objects.Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.ACNode_Voltage }; Objects.Data currentd = new Objects.Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.ACNode_Current }; Objects.Data powerd = new Objects.Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.ACNode_Power }; Objects.Data relayd = new Objects.Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.ACNode_Relay }; Util.DataAdapter.Encode(voltage, voltaged); Util.DataAdapter.Encode(current, currentd); Util.DataAdapter.Encode(power, powerd); Util.DataAdapter.Encode(relay, relayd); client.DataUpload(new Objects.Data[] { voltaged, powerd, relayd }); } else if (device.Type == Util.DeviceType.AccelerationSensor) { client.DataUpload(new Objects.Data[] { GetRandomData(Util.ServiceType.AccelerationSensor_X), GetRandomData(Util.ServiceType.AccelerationSensor_Y), GetRandomData(Util.ServiceType.AccelerationSensor_Z) }); } else if (device.Type == Util.DeviceType.MotionSensor) { Objects.Data data = new Objects.Data { Device = device.Guid, Timestamp = DateTime.Now, Service = Util.ServiceType.MotionSensor_Motion }; Util.DataAdapter.Encode((random.NextDouble() > 0.5), data); client.DataUpload(data); } else { client.DataUpload(GetRandomData(Util.ServiceType.Default)); } } catch { } }
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; } }