public WorkScheduleVersionService(GPnaviServerContext context) { _context = context; }
public UserStatusService(GPnaviServerContext context) { _context = context; }
public UserService(GPnaviServerContext context, ILogger <UserService> logger) { _context = context; _logger = logger; }
/// <summary> /// センサーイベントコントローラ /// </summary> /// <param name="json">受信メッセージ</param> /// <returns></returns> private async Task SensorEventControllerAsync(string json) { _logger.LogInformation(LoggingEvents.IotHubReceive, "SensorEventControllerAsync START"); var options = new DbContextOptionsBuilder <GPnaviServerContext>() .UseSqlServer(Configuration.GetConnectionString("DefaultConnection")) .Options; using (var context = new GPnaviServerContext(options)) { try { _logger.LogTrace(LoggingEvents.IotHubReceive, "パース"); var apiSensorEvent = JsonConvert.DeserializeObject <ApiSensorEvent>(json); _logger.LogTrace(LoggingEvents.IotHubReceive, "バリデーションチェック"); var(result, message) = IsInvalidApiSensorEvent(apiSensorEvent); if (result) { _logger.LogError(LoggingEvents.Validation, $"バリデーションエラー {message}"); return; } _logger.LogTrace(LoggingEvents.IotHubReceive, "センサーマスタ検索"); var sensorMaster = context.SensorMasters.FirstOrDefault(e => e.SensorId.Equals(apiSensorEvent.sensor_id)); if (sensorMaster == null) { _logger.LogError(LoggingEvents.Validation, $"センサーマスタにセンサーID {apiSensorEvent.sensor_id} が存在しない"); return; } _logger.LogTrace(LoggingEvents.IotHubReceive, "センサーマスタ登録内容のバリデーションチェック"); if (!(sensorMaster.SensorType.Equals(ApiConstant.SENSOR_TYPE_POT) || sensorMaster.SensorType.Equals(ApiConstant.SENSOR_TYPE_TRASH))) { _logger.LogError(LoggingEvents.Validation, $"センサーマスタのセンサーデバイス区分 {sensorMaster.SensorType} が不正"); return; } if (string.IsNullOrWhiteSpace(sensorMaster.DisplayName)) { _logger.LogError(LoggingEvents.Validation, $"センサーマスタの表示名 {sensorMaster.DisplayName} が不正"); return; } _logger.LogTrace(LoggingEvents.IotHubReceive, "突発作業状態を検索"); var sensorStatus = context.SensorStatuses.FirstOrDefault(e => e.SensorId.Equals(sensorMaster.SensorId)); if (sensorStatus != null) { _logger.LogTrace(LoggingEvents.IotHubReceive, "前回の発生時刻と比較"); var timeSpan = DateTime.Now - sensorStatus.OccurrenceDate; if (timeSpan.TotalMinutes < SensorTimeSpanMinutes) { _logger.LogTrace(LoggingEvents.IotHubReceive, "頻発しているのでイベントを捨てる"); return; } else { _logger.LogTrace(LoggingEvents.IotHubReceive, "突発作業発生時刻を更新"); sensorStatus.OccurrenceDate = DateTime.Now; } } else { _logger.LogTrace(LoggingEvents.IotHubReceive, "突発作業状態を追加"); sensorStatus = new SensorStatus { SensorId = sensorMaster.SensorId, SensorType = sensorMaster.SensorType, DisplayName = sensorMaster.DisplayName, OccurrenceDate = DateTime.Now }; context.Add(sensorStatus); } _logger.LogTrace(LoggingEvents.IotHubReceive, "保存する"); await context.SaveChangesAsync(); _logger.LogTrace(LoggingEvents.IotHubReceive, "センサー検知を送信する"); var displayMessage = sensorMaster.SensorType.Equals(ApiConstant.SENSOR_TYPE_TRASH) ? string.Format(MessageTrash, sensorStatus.DisplayName) : string.Format(MessagePot, sensorStatus.DisplayName); var apiSensorPush = new ApiSensorPush { message_name = ApiConstant.MESSAGE_SENSOR_PUSH, sensor_id = sensorStatus.SensorId, sensor_type = sensorStatus.SensorType, display_message = displayMessage, date = sensorStatus.OccurrenceDate.ToString(DateTimeFormat, CultureInfoApi) }; var apiSensorPushJson = JsonConvert.SerializeObject(apiSensorPush); var androidTokens = context.UserStatuses.Where(e => e.DeviceType.Equals(ApiConstant.DEVICE_TYPE_ANDROID)).Select(e => e.DeviceToken).ToList(); if (androidTokens.Count > 0) { await _pushToNotificationHub.SendNotificationGcmAsync(apiSensorPushJson, androidTokens); } else { _logger.LogWarning(LoggingEvents.IotHubReceive, "Androidデバイスが登録されていない"); } var iotTokens = context.UserStatuses.Where(e => e.DeviceType.Equals(ApiConstant.DEVICE_TYPE_IOT)).Select(e => e.DeviceToken).ToList(); if (iotTokens.Count > 0) { await _pushToNotificationHub.SendNotificationWindowsAsync(apiSensorPushJson, iotTokens); } else { _logger.LogWarning(LoggingEvents.IotHubReceive, "IoTデバイスが登録されていない"); } _logger.LogWarning(LoggingEvents.PushMessageAsync, $"センサー検知の送信に成功しました。{apiSensorPushJson}"); } catch (JsonWriterException ex) { _logger.LogError(LoggingEvents.ApiFormat, $"装置間IFフォーマットに変換できませんでした。{ex.Message}"); } catch (JsonReaderException ex) { _logger.LogError(LoggingEvents.ApiFormat, $"装置間IFフォーマットではありません。{ex.Message}"); } catch (Exception ex) { _logger.LogError(LoggingEvents.Exception, ex, "SensorEventControllerAsync unknown error."); } } }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new GPnaviServerContext(serviceProvider.GetRequiredService <DbContextOptions <GPnaviServerContext> >())) { bool isDirty = false; if (!context.UserMasters.Any()) { // 担当者マスタの初期設定 context.UserMasters.AddRange( new UserMaster { LoginId = "999", Password = PasswordUtility.Hash("999"), LoginName = "初期設定管理者", Role = ApiConstant.ROLE_ADMIN, IsValid = true, }, new UserMaster { LoginId = "111", Password = PasswordUtility.Hash("1234"), LoginName = "山田太郎", Role = ApiConstant.ROLE_WORK, IsValid = true, }, new UserMaster { LoginId = "222", Password = PasswordUtility.Hash("5678"), LoginName = "鈴木次郎", Role = ApiConstant.ROLE_WORK, IsValid = true, }, new UserMaster { LoginId = "333", Password = PasswordUtility.Hash("0000"), LoginName = "佐藤三郎", Role = ApiConstant.ROLE_WORK, IsValid = true, } ); isDirty = true; } if (!context.WorkScheduleMasters.Any()) { // WSマスタの初期設定 context.WorkScheduleMasters.AddRange( new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "チルド2便納品", ShortName = "チルド納品", Priority = "H", IconId = "0003", Time = "30", Holiday = "0", Row = 1 }, new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "ヤマザキ2便検収・納品", ShortName = "ヤマザキ検収・納品", Priority = "H", IconId = "0003", Time = "30", Holiday = "0", Row = 2 }, new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "店頭ガラス清掃", ShortName = "ガラス清掃", Priority = "L", IconId = "0008", Time = "15", Holiday = "0", Row = 3 }, new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "入口ドア清掃", ShortName = "ドア清掃", Priority = "L", IconId = "0008", Time = "10", Holiday = "0", Row = 4 }, new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "チルド2便納品", ShortName = "チルド納品", Priority = "H", IconId = "0003", Time = "30", Holiday = "1", Row = 5 }, new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "雑誌検収・納品", ShortName = "検収・納品", Priority = "M", IconId = "0003", Time = "15", Holiday = "1", Row = 6 }, new WorkScheduleMaster { Version = 0, Start = "7:00", Name = "入口ドア清掃", ShortName = "ドア清掃", Priority = "L", IconId = "0008", Time = "10", Holiday = "1", Row = 7 } ); isDirty = true; } if (!context.HolidayMasters.Any()) { // 休日マスタの初期設定 context.HolidayMasters.AddRange( new HolidayMaster { Holiday = new DateTime(2018, 9, 17) }, new HolidayMaster { Holiday = new DateTime(2018, 9, 23) }, new HolidayMaster { Holiday = new DateTime(2018, 10, 8) }, new HolidayMaster { Holiday = new DateTime(2018, 11, 3) }, new HolidayMaster { Holiday = new DateTime(2018, 11, 23) }, new HolidayMaster { Holiday = new DateTime(2018, 12, 23) } ); isDirty = true; } if (!context.SensorMasters.Any()) { // センサーマスタの初期設定 context.SensorMasters.AddRange( new SensorMaster { SensorId = "9001", SensorType = ApiConstant.SENSOR_TYPE_POT, DisplayName = "ポット1" }, new SensorMaster { SensorId = "9002", SensorType = ApiConstant.SENSOR_TYPE_POT, DisplayName = "ポット2" }, new SensorMaster { SensorId = "9003", SensorType = ApiConstant.SENSOR_TYPE_TRASH, DisplayName = "ごみ箱1" }, new SensorMaster { SensorId = "9004", SensorType = ApiConstant.SENSOR_TYPE_TRASH, DisplayName = "ごみ箱2" } ); isDirty = true; } if (isDirty) { context.SaveChanges(); } } }
public UserVersionService(GPnaviServerContext context) { _context = context; }
public WorkScheduleService(GPnaviServerContext context, ILogger <WorkScheduleService> logger) { _context = context; _logger = logger; }