コード例 #1
0
        private async Task HandleAttribute(List <BStruBase> atts)
        {
            PlateformDevs pl = new PlateformDevs();
            ConcurrentDictionary <string, List <TagRes> > groupBuffer = new ConcurrentDictionary <string, List <TagRes> >();

            foreach (var att in atts)
            {
                switch (att.Type)
                {
                case EnumType.STATION:
                {
                    if (!(att is TStation st))
                    {
                        continue;
                    }
                    await SuperBRepertory.Inst.UpdateOrAddStation(st);
                }
                break;

                case EnumType.DEVICE:
                {
                    if (!(att is TDevice dev))
                    {
                        continue;
                    }
                    await SuperBRepertory.Inst.UpdateOrAddDevice(dev);
                }
                break;

                case EnumType.AI:
                case EnumType.AO:
                case EnumType.DI:
                case EnumType.DO:
                case EnumType.STRIN:
                {
                    await SuperBRepertory.Inst.BeginUpdateOrAddTag(att, async (tag, groupIsid, change) =>
                        {
                            if (change == MqttCommon.ChangeType.Add)
                            {
                                groupBuffer.AddOrUpdate(groupIsid, new List <TagRes>(),
                                                        (oKey, oVal) => { oVal.Add(tag.Tag); return(oVal); });
                            }
                            else if (change == MqttCommon.ChangeType.Update)
                            {
                                return(true);
                            }

                            return(false);
                        });
                }
                break;

                default:
                    break;
                }

                foreach (var pair in groupBuffer)
                {
                    var req = await HttpUtil.GetAsync <GroupResResponse>(
                        $"http://{cfg.AppSettings["serverUrl"]}/inIoT/devServer/tagGroup/{pair.Key}",
                        SuperBCenter.ServerToken);

                    if (req == null)
                    {
                        continue;
                    }
                    if (req.codeid != 0)
                    {
                        var devMap = SuperBRepertory.Inst.devicemap.Find(d => d.groupIsid == pair.Key);
                        if (devMap == null)
                        {
                            LogHelper.Error($"GroupRes {pair.Key} cannot find map to device");
                            continue;
                        }
                        var device = SuperBRepertory.Inst.DevList.Find(d => d.Device.dvIsid == devMap.devIsid);
                        if (device == null)
                        {
                            LogHelper.Error($"GroupRes {pair.Key} cannot find device");
                            continue;
                        }
                        var res = await HttpUtil.PostAsync($"http://{cfg.AppSettings["serverUrl"]}/inIoT/devServer/tagGroup",
                                                           JsonSrialize.Srialize(new GroupRes()
                        {
                            groupIsid       = pair.Key,
                            collectPeriod   = 2,
                            dvIsid          = device.Device.dvIsid,
                            nodeIsid        = device.NodeIsid,
                            overTime        = 2,
                            relinkCount     = 2,
                            relinkPeriod    = 2,
                            transProtocolId = "1",
                        }),
                                                           SuperBCenter.ServerToken);

                        if (res == null || res.codeid != 0)
                        {
                            LogHelper.Error($"GroupRes {pair.Key} cannot add");
                            continue;
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: SuperBCenter.cs プロジェクト: nocook/SuperB
        public void Init()
        {
            var token = HttpUtil.LoginAsync($"http://{cfg.AppSettings["serverUrl"]}/inIoT/userServer/tologin",
                                            new LoginUser()
            {
                username = "******",
                pwd      = "12345"
            });

            token.Wait();

            if (string.IsNullOrEmpty(token.Result))
            {
                LogHelper.Error($"LoginAsync {cfg.AppSettings["serverUrl"]} error");
                return;
            }
            ServerToken = JsonSrialize.Desrialize <LoginResponse>(token.Result).data.token;

            var req1 = HttpUtil.GetAsync <GroupResResponse>($"http://{cfg.AppSettings["serverUrl"]}/inIoT/devServer/tagGroup/G433635714868056",
                                                            ServerToken);

            req1.Wait();
            var req = HttpUtil.PostAsync($"http://{cfg.AppSettings["serverUrl"]}/inIoT/devServer/deviceRes",
                                         JsonSrialize.Srialize(new DevModel()
            {
                dvIsid      = "dfsdf",
                dvName      = "ssssssf",
                dvTypeCode  = "15",
                managerIsid = cfg.AppSettings["managerIsid"],
                nodeIsid    = "100114",
                addrRes     = new MqttCommon.Setup.AddResource()
                {
                    communicationType = 1,
                    dvAddr1           = "1.1.1.1",
                    dvAddr2           = "2222"
                }
            }), SuperBCenter.ServerToken);


            SdkMgr.Inst.Init().Wait();
            //   SuperBRepertory.Inst.SynchronizeDb().Wait();

            //    MqttMgr.Inst.Init().Wait();

            //   SetupMgr.Inst.Init().Wait();
            return;


            string ss = JsonSrialize.Srialize(new DevModel[]
            {
                new DevModel()
                {
                    addrRes = new AddResource()
                    {
                        communicationType = 0,
                        connectParam1     = "9600",
                        connectParam2     = "8",
                        connectParam3     = "1",
                        connectParam4     = "1",
                        dvAddr1           = "3",
                        dvAddr2           = "1",
                    },
                    dvName      = "sdf",
                    dvTypeCode  = "10",
                    managerIsid = "mgrTest2",
                    nodeIsid    = "zhc-yq",
                    dvIsid      = Uuid.Create16Token(),
                }
            });

            HttpUtil.PostAsync("http://10.10.12.164:8080/inIoT/devServer/deviceRes/list", ss, token.Result);
        }
コード例 #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);
        }