Exemple #1
0
        public async Task SynchronizeDb()
        {
            var db = _db;

            subplateformres = await db.subplateformres.ToListAsync();

            var tStation = await db.tStation.ToListAsync();

            nodemap = await db.nodemap.ToListAsync();

            foreach (var st in tStation)
            {
                var ndMap = nodemap.Find(n => n.objId == st.objId);
                if (ndMap == null)
                {
                    continue;
                }
                NdList.Add(new SuperBStation(new TStation()
                {
                    ID        = st.objId,
                    Name      = st.objName,
                    Des       = st.objDesc,
                    Type      = (EnumType)st.objTypeId,
                    ParentID  = st.parentId ?? 0,
                    Latitude  = st.latitude,
                    Longitude = st.longitude
                },
                                             ndMap.nodeIsid));
            }

            var tDevice = await db.tDevice.ToListAsync();

            foreach (var dev in tDevice)
            {
                var ndMap = NdList.Find(n => n.OrgiData.ID == dev.parentId);
                if (ndMap == null)
                {
                    continue;
                }
                DevList.Add(new SuperBDevice(new TDevice()
                {
                    ID           = dev.objId,
                    Name         = dev.objName,
                    Des          = dev.objDesc,
                    Type         = (EnumType)dev.objTypeId,
                    ParentID     = dev.parentId ?? 0,
                    BeginRunTime = dev.beginRunTime.HasValue ?
                                   SuperBTagBase.Transfer(dev.beginRunTime.Value) :
                                   SuperBTagBase.Transfer(DateTime.Now),
                    DeviceType = (EnumDeviceType)dev.devTypeId,
                    Productor  = dev.productor,
                    Version    = dev.version,
                },
                                             ndMap.NodeIsid));
            }

            tagmap = await db.tagmap.ToListAsync();

            devicemap = await db.devicemap.ToListAsync();

            alarmmap = await db.alarmmap.ToListAsync();

            var taic = await db.taic.ToListAsync();

            foreach (var aic in taic)
            {
                var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId);
                var tagMap = tagmap.Find(t => t.objId == aic.objId);
                if (devMap == null || tagMap == null)
                {
                    continue;
                }
                TagList.Add(new SuperB_Taic(new TAIC()
                {
                    ID          = aic.objId,
                    Name        = aic.objName,
                    Des         = aic.objDesc,
                    Type        = (EnumType)aic.objTypeId,
                    ParentID    = aic.parentId ?? 0,
                    AlarmEnable = (EnumEnable)aic.alarmEnable,
                    AlarmLevel  = (EnumAlarmLevel)aic.alarmLevel,
                    HiLimit1    = aic.hiLimit1,
                    HiLimit2    = aic.hiLimit2,
                    HiLimit3    = aic.hiLimit3,
                    LoLimit1    = aic.loLimit1,
                    LoLimit2    = aic.loLimit2,
                    LoLimit3    = aic.loLimit3,
                },
                                            tagMap.tagIsid, alarmmap.ToArray()));
            }
            var taoc = await db.taoc.ToListAsync();

            foreach (var aic in taoc)
            {
                var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId);
                var tagMap = tagmap.Find(t => t.objId == aic.objId);
                if (devMap == null || tagMap == null)
                {
                    continue;
                }
                TagList.Add(new SuperB_Taoc(new TAOC()
                {
                    ID            = aic.objId,
                    Name          = aic.objName,
                    Des           = aic.objDesc,
                    Type          = (EnumType)aic.objTypeId,
                    ParentID      = aic.parentId ?? 0,
                    ControlEnable = (EnumEnable)aic.controlEnable,
                    MaxVal        = aic.maxVal,
                    MinVal        = aic.minVal,
                    Percision     = aic.percision,
                    Saved         = (EnumEnable)aic.saved,
                    Stander       = aic.stander,
                    Unit          = aic.unit,
                },
                                            tagMap.tagIsid, alarmmap.ToArray()));
            }
            var tdic = await db.tdic.ToListAsync();

            foreach (var aic in tdic)
            {
                var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId);
                var tagMap = tagmap.Find(t => t.objId == aic.objId);
                if (devMap == null || tagMap == null)
                {
                    continue;
                }
                TagList.Add(new SuperB_Tdic(new TDIC()
                {
                    ID              = aic.objId,
                    Name            = aic.objName,
                    Des             = aic.objDesc,
                    Type            = (EnumType)aic.objTypeId,
                    ParentID        = aic.parentId ?? 0,
                    Saved           = (EnumEnable)aic.saved,
                    AlarmEnable     = (EnumEnable)aic.alarmEnable,
                    AlarmLevel      = (EnumAlarmLevel)aic.alarmLevel,
                    AlarmThresbhold = (EnumEnable)aic.alarmThresbhold,
                    Desc0           = aic.desc0,
                    Desc1           = aic.desc1,
                },
                                            tagMap.tagIsid, alarmmap.ToArray()));
            }
            var tdoc = await db.tdoc.ToListAsync();

            foreach (var aic in tdoc)
            {
                var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId);
                var tagMap = tagmap.Find(t => t.objId == aic.objId);
                if (devMap == null || tagMap == null)
                {
                    continue;
                }
                TagList.Add(new SuperB_Tdoc(new TDOC()
                {
                    ID            = aic.objId,
                    Name          = aic.objName,
                    Des           = aic.objDesc,
                    Type          = (EnumType)aic.objTypeId,
                    ParentID      = aic.parentId ?? 0,
                    ControlEnable = (EnumEnable)aic.controlEnable,
                    Saved         = (EnumEnable)aic.saved,
                    Desc0         = aic.desc0,
                    Desc1         = aic.desc1,
                },
                                            tagMap.tagIsid, alarmmap.ToArray()));
            }
            var tdsc = await db.tdsc.ToListAsync();

            foreach (var aic in tdsc)
            {
                var devMap = DevList.Find(n => n.OrgiData.ID == aic.parentId);
                var tagMap = tagmap.Find(t => t.objId == aic.objId);
                if (devMap == null || tagMap == null)
                {
                    continue;
                }
                TagList.Add(new SuperB_Tdsc(new TDSC()
                {
                    ID          = aic.objId,
                    Name        = aic.objName,
                    Des         = aic.objDesc,
                    Type        = (EnumType)aic.objTypeId,
                    ParentID    = aic.parentId ?? 0,
                    Saved       = (EnumEnable)aic.saved,
                    AlarmEnable = (EnumEnable)aic.alarmEnable,
                },
                                            tagMap.tagIsid, alarmmap.ToArray()));
            }
        }
Exemple #2
0
        /// <summary>
        /// Represents a client that communicates with an Appscape repository.
        ///
        /// Generally you shouldn't need to create an instance of this unless you messed up Appscape and are desperately trying to fix it by rewriting it, in which case you really should've just rolled back the source code. xD
        /// </summary>
        /// <param name="ip_address">IP address of the repo. This can be found in Package_Grabber.clients.Keys.</param>
        public AppscapeClient(string ip_address)
        {
            try {
                var c = Package_Grabber.clients[ip_address];
                c.OnReceived += (object s, NetReceivedEventArgs <NetObject> a) =>
                {
                    var obj = (ObjectModel)a.Data.Object;
                    switch (_mode)
                    {
                    case "getapps":
                        try
                        {
                            Packages = (List <AppscapePackage>)obj.OptionalObject;
                            var h = OnPackagesReceived;
                            if (h != null)
                            {
                                h(obj, new EventArgs());
                            }
                        }
                        catch
                        {
                        }

                        break;

                    case "get_devlist":
                        try {
                            if (obj.OptionalObject != null)
                            {
                                if (DevList == null)
                                {
                                    DevList = new Dictionary <string, AppscapeModder>();
                                }
                                var lst = (List <string>)obj.OptionalObject;
                                foreach (string json in lst)
                                {
                                    var dev = JsonConvert.DeserializeObject <AppscapeModder>(json);
                                    if (!DevList.ContainsKey(dev.DevKey))
                                    {
                                        DevList.Add(dev.DevKey, dev);
                                    }
                                }
                                var h = OnDevListSync;
                                if (h != null)
                                {
                                    h(obj, new EventArgs());
                                }
                            }
                        }
                        catch
                        {
                        }
                        break;

                    case "getskins":
                        try
                        {
                            Skins = (List <SkinData>)obj.OptionalObject;
                            var h = OnSkinDataReceived;
                            if (h != null)
                            {
                                h(obj, new EventArgs());
                            }
                        }
                        catch
                        {
                        }
                        break;

                    default:
                        try {
                            var      command = (ObjectModel)a.Data.Object;
                            string[] args    = command.Command.Split(' ');
                            switch (args[0])
                            {
                            case "package_bytes":
                                byte[] package = (byte[])command.OptionalObject;
                                File.WriteAllBytes($"{Paths.Home}{OSInfo.DirectorySeparator}Downloads{OSInfo.DirectorySeparator}{args[1]}", package);
                                var h = OnSuccessfulDownload;
                                if (h != null)
                                {
                                    h($"{Paths.Home}{OSInfo.DirectorySeparator}Downloads{ OSInfo.DirectorySeparator}{args[1]}", new EventArgs());
                                }
                                break;

                            case "skin_bytes":
                                byte[] skin = (byte[])command.OptionalObject;
                                File.WriteAllBytes($"{Paths.Home}{OSInfo.DirectorySeparator}Downloads{OSInfo.DirectorySeparator}{args[1]}", skin);
                                var ha = OnSuccessfulDownload;
                                if (ha != null)
                                {
                                    ha($"{Paths.Home}{OSInfo.DirectorySeparator}Downloads{ OSInfo.DirectorySeparator}{args[1]}", new EventArgs());
                                }
                                break;
                            }
                        }
                        catch
                        {
                        }
                        break;
                    }
                };
                _IP = ip_address;
            }
            catch (Exception ex)
            {
                throw new ArgumentException("IP address not found in client list.");
            }
        }
Exemple #3
0
        public async Task <bool> UpdateOrAddDevice(TDevice data)
        {
            var node = nodemap.Find(m => m.objId == data.ParentID);

            if (node == null)
            {
                return(false);              //没有父节点,返回
            }
            var dev = DevList.Find(n => n.OrgiData.ID == data.ID);

            if (dev == null)    //新增设备,返回新增的设备,等web新增成功,插入数据库
            {
                dev = new SuperBDevice(data, node.nodeIsid);
                var map = new SqlDeviceMap()
                {
                    devIsid = dev.Device.dvIsid, objId = data.ID, groupIsid = Uuid.Create16Token()
                };
                var req = await HttpUtil.PostAsync($"http://{cfg.AppSettings["serverUrl"]}/inIoT/devServer/deviceRes",
                                                   JsonSrialize.Srialize(new DevModel()
                {
                    dvIsid      = dev.Device.dvIsid,
                    dvName      = dev.Device.dvName,
                    dvTypeCode  = dev.Device.dvTypeCode,
                    managerIsid = cfg.AppSettings["managerIsid"],
                    nodeIsid    = dev.NodeIsid,
                    addrRes     = new MqttCommon.Setup.AddResource()
                    {
                        communicationType = 1,
                        dvAddr1           = "1.1.1.1",
                        dvAddr2           = "2222"
                    }
                }), SuperBCenter.ServerToken);

                if (req == null || req.codeid != 0)
                {
                    return(false);
                }

                try
                {
                    await _db.tDevice.AddAsync(dev.DbData);

                    if (!devicemap.Exists(d => d.objId == dev.OrgiData.ID && d.devIsid == dev.NodeIsid))
                    {
                        await _db.devicemap.AddAsync(map);

                        devicemap.Add(map);
                    }
                    await _db.SaveChangesAsync();

                    DevList.Add(dev);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    return(false);
                }
            }
            else
            {
                if (!devicemap.Exists(d => d.objId == dev.OrgiData.ID && d.devIsid == dev.Device.dvIsid))
                {
                    return(false);
                }
                if (dev.Update(data))
                {
                    try
                    {
                        _db.tDevice.Update(dev.DbData);
                        await _db.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        return(false);
                    }
                }
            }

            return(true);
        }