Ejemplo n.º 1
0
 public void UpdateDevsensorValues()
 {
     ICommandRepository repo = AutofacDependencyResolver.Current.GetService<ICommandRepository>();
     ICommandQueueService cqs = AutofacDependencyResolver.Current.GetService<ICommandQueueService>();
     ISensorMapper mapper = AutofacDependencyResolver.Current.GetService<ISensorMapper>();
     using (var db = new DbContext())
     {
         var l = db.device_sensor
             .Include("device")
             //.Where(d => d.device_sensor_model == "d")
             .Where(d => d.device_sensor_type != "0")
             .ToList();
         foreach (var d in l)
         {
             var c = repo.Create(d.device_sensor_pin, null, d.device.device_host,
                 d.device_sensor_type.Substring(0, 1).ToLower());
             cqs.Enqueue(c, s =>
             {
                 string unit = "";
                 int v;
                 if (Int32.TryParse(s, out v))
                 {
                     s = mapper.Map(d.device_sensor_model, v).ToString();
                     unit = mapper.GetUnit(d.device_sensor_model);
                 }
                 Clients.All.updateSensorValue(d.device_sensorid, s, unit);
             });
         }
     }
 }
Ejemplo n.º 2
0
 public List<ICommand> GetUnsentCommands()
 {
     var result = new List<ICommand>();
     try
     {
         using (var db = new DbContext())
         {
     //                    var r = db.command
     //                        .SqlQuery("select * from command where command_status = 0 and created_at < now()")
     //                        .ToList();
             var r = db.command
                 .Where(c => c.command_status == 0)
                 .Where(c => c.created_at < DateTime.Now)
                 .ToList();
             foreach (var c in r)
             {
                 ICommand command = new Command(c.commandid, (int) c.command_pin, c.command_value, c.command_host,
                     c.command_type, 0);
                 result.Add(command);
             }
         }
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
     }
     return result;
 }
Ejemplo n.º 3
0
 public ICommand Create(int pin, int? value, string host, string type)
 {
     try
     {
         ICommand c = new Command(-1, pin, value, host, type, 0);
         if (value == null) return c;
         var cm = new command()
         {
             command_pin = pin,
             command_value = value,
             command_host = host,
             command_type = type
         };
         DbContext db = new DbContext();
         db.command.Add(cm);
         db.SaveChanges();
         c.CommandId = cm.commandid;
         return c;
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
     }
     return null;
 }
Ejemplo n.º 4
0
Archivo: Command.cs Proyecto: skohub/Sh
 public void SetComplete()
 {
     using (var db = new DbContext())
     {
         var c = db.command.Find(CommandId);
         if (c == null) return;
         c.command_status = 1;
         db.SaveChanges();
     }
 }
Ejemplo n.º 5
0
 public void SetDeviceValue(int devsensorid, int value)
 {
     ICommandRepository repo = AutofacDependencyResolver.Current.GetService<ICommandRepository>();
     ICommandQueueService cqs = AutofacDependencyResolver.Current.GetService<ICommandQueueService>();
     ISensorMapper mapper = AutofacDependencyResolver.Current.GetService<ISensorMapper>();
     using (var db = new DbContext())
     {
         var d = db.device_sensor.First(n => n.device_sensorid == devsensorid);
         var mapped = mapper.Unmap(d.device_sensor_model, value);
         var c = repo.Create(d.device_sensor_pin, mapped, d.device.device_host,
                 d.device_sensor_type.Substring(0, 1));
         cqs.Enqueue(c, s => Clients.All.updateSensorValue(d.device_sensorid, value, mapper.GetUnit(d.device_sensor_model)));
     }
 }
Ejemplo n.º 6
0
 public ISensorValue Create(int pin, decimal value, string host, string type)
 {
     try
     {
         ISensorValue sv = new SensorValue() {sensorid = pin, sensor_value = value, host = host, tipe = type};
         using (var db = new DbContext())
         {
             var svm = new sensor_value() {sensorid = pin, sensor_value1 = value, host = host, tipe = type};
             db.sensor_value.Add(svm);
             db.SaveChanges();
         }
         return sv;
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
     }
     return null;
 }
Ejemplo n.º 7
0
 public SensorMapper()
 {
     using (var db = new DbContext())
     {
         var l = db.sensor_param;
         foreach (var sensorParam in l)
         {
             var mapType = new MapType()
             {
                 Min = sensorParam.sensor_param_min,
                 Max = sensorParam.sensor_param_max,
                 MapMin = sensorParam.sensor_param_map_min,
                 MapMax = sensorParam.sensor_param_map_max,
                 Unit = sensorParam.sensor_param_unit
             };
             _mapTypes.Add(sensorParam.sensor_param_name, mapType);
         }
     }
 }
Ejemplo n.º 8
0
        public IEnumerable<IVirtualDevice> GetList()
        {
            const string sql = @"select
                    device_sensor.device_sensorid,
                    device_sensor.device_sensor_name,
                    device_sensor.device_sensor_delay,
                    ds2.device_sensor_pin,
                    ds2.device_sensor_name as device_sensor_read_name,
                    ds2.device_sensor_type,
                    ds2.device_sensor_model,
                    device.device_host,
                    virtual_program.virtual_programid,
                    virtual_program.virtual_program_op,
                    virtual_program.virtual_program_value,
                    virtual_operand.virtual_operand_name
                from device_sensor
                    join virtual_program on virtual_program.virtual_program_sensorid = device_sensor.device_sensorid
                    join device_sensor ds2 on ds2.device_sensorid = virtual_program_sensorid_read
                    join device on device.deviceid = ds2.device_sensor_deviceid
                    join virtual_operand on virtual_operand.virtual_operandid = virtual_program.virtual_program_op
                where device_sensor.device_sensor_state = 1";

            using (var db = new DbContext())
            {
                DbRawSqlQuery<ConditionDto> dtos = db.Database.SqlQuery<ConditionDto>(sql);
                foreach (var dto in dtos)
                {
                    var dev = FindOrCreateVirtualDevice(dto);
                    var c = new Condition(dto.virtual_programid, dto.device_sensor_read_name, dto.device_sensor_pin,
                        dto.device_host, dto.virtual_operand_name, dto.virtual_program_value, dto.device_sensor_type,
                        dto.device_sensor_model, _mapper);
                    dev.AddCondition(c);
                }
            }
            var result = _list.Select(p => p.Value).ToList();
            _list.Clear();
            return result;
        }
Ejemplo n.º 9
0
 public List<ICommand> GetCommandsForDevice(int deviceSensorid, int action)
 {
     var result = new List<ICommand>();
     try
     {
         using (var db = new DbContext())
         {
             var r = db.command_group
                 .Where(n => n.devsensor_id == deviceSensorid)
                 .Where(n => n.action == action)
                 .ToList();
             foreach (var cg in r)
             {
                 ICommand c = new Command(-1, cg.pin, cg.value, cg.host, cg.type, cg.delay);
                 result.Add(c);
             }
         }
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
     }
     return result;
 }
Ejemplo n.º 10
0
 public void MarkAllComplete()
 {
     try
     {
         using (var db = new DbContext())
         {
             var r = db.command
                 .Where(n => n.command_status == 0)
                 .ToList();
             foreach (var c in r)
             {
                 c.command_status = 1;
             }
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         Logger.Error(e.Message);
     }
 }